Debugging.

master
Tomasz Półgrabia 2016-10-03 22:26:53 +02:00
parent b7fcb66735
commit a2da39c4e2
9 changed files with 58 additions and 211 deletions

View File

@ -1,7 +1,6 @@
package pl.tpolgrabia.urbanexplorer.adapters;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -10,7 +9,6 @@ import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhoto;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhotoRefResult;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import pl.tpolgrabia.urbanexplorer.AppConstants;
import pl.tpolgrabia.urbanexplorer.MainActivity;

View File

@ -1,17 +1,14 @@
package pl.tpolgrabia.urbanexplorer.dto;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhotoRefResult;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import java.util.List;
import java.util.Map;
/**
* Created by tpolgrabia on 03.10.16.
*/
public class GooglePlacesResponse {
private List<GooglePlaceResult> places;
private Map<String, List<GooglePlacePhotoRefResult>> photos;
public List<GooglePlaceResult> getPlaces() {
return places;
@ -21,19 +18,10 @@ public class GooglePlacesResponse {
this.places = places;
}
public Map<String, List<GooglePlacePhotoRefResult>> getPhotos() {
return photos;
}
public void setPhotos(Map<String, List<GooglePlacePhotoRefResult>> photos) {
this.photos = photos;
}
@Override
public String toString() {
return "GooglePlacesResponse{" +
"places=" + places +
", photos=" + photos +
'}';
}
}

View File

@ -5,6 +5,7 @@ import android.location.Location;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ListView;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.googleutils.callback.PlacesCallback;
@ -17,6 +18,7 @@ import pl.tpolgrabia.urbanexplorer.adapters.PlacesAdapter;
import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesRequest;
import pl.tpolgrabia.urbanexplorer.dto.GooglePlacesResponse;
import pl.tpolgrabia.urbanexplorer.fragments.PlacesFragment;
import retrofit2.Response;
import java.io.IOException;
import java.util.ArrayList;
@ -51,27 +53,24 @@ public class GooglePlacesWorker extends AsyncTask<GooglePlacesRequest, Integer,
for (final GooglePlacesRequest param : params) {
lg.debug("Excuting param {}", param);
Location location = param.getLocation();
final Semaphore sem = new Semaphore(0);
placesUtils.fetchNearbyPlaces(
location.getLatitude(),
location.getLongitude(),
param.getSearchRadius(),
param.getSearchItemType(),
param.getPageToken(),
new PlacesCallback() {
@Override
public void callback(Long statusCode, String statusMsg, List<GooglePlaceResult> googlePlaceResults) {
GooglePlacesResponse response = new GooglePlacesResponse();
response.setPlaces(googlePlaceResults);
result.add(response);
sem.release();
}
});
Response<List<GooglePlaceResult>> placesResponse = null;
try {
sem.acquire();
} catch (InterruptedException e) {
lg.error("Interrupted");
placesResponse = placesUtils.fetchNearbyPlaces(
location.getLatitude(),
location.getLongitude(),
param.getSearchRadius(),
param.getSearchItemType(),
param.getPageToken());
if (placesResponse.code() == HttpStatus.SC_OK) {
GooglePlacesResponse response = new GooglePlacesResponse();
response.setPlaces(placesResponse.body());
result.add(response);
}
} catch (IOException e) {
lg.error("I/O error", e);
}
}

View File

@ -34,4 +34,5 @@ dependencies {
compile project(path: ':urbanexplorerutils')
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
}

View File

@ -1,10 +1,8 @@
package pl.tpolgrabia.googleutils;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhotoRefResult;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import java.util.List;
@ -13,9 +11,18 @@ import java.util.List;
* Created by tpolgrabia on 02.10.16.
*/
public interface GooglePlacesService {
@GET("photo")
Call<List<GooglePlacePhotoRefResult>> fetchPhotosByRef(
@Query("max_width") Long maxWidth,
@Query("photoreference") String photoRef,
@Query("key") String apiKey);
@GET("nearbysearch/json")
Call<List<GooglePlaceResult>> fetchNearbyPlacesFirst(
@Query("key") String apiKey,
@Query("location") String location,
@Query("radius") Double radius,
@Query("type") String type);
@GET("nearbysearch/json")
Call<List<GooglePlaceResult>> fetchNearbyPlacesNext(
@Query("key") String apiKey,
@Query("location") String location,
@Query("radius") Double radius,
@Query("type") String type,
@Query("pagetoken") String pageToken);
}

View File

@ -1,14 +0,0 @@
package pl.tpolgrabia.googleutils.callback;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhotoRefResult;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import java.util.List;
/**
* Created by tpolgrabia on 02.10.16.
*/
public interface GooglePlacesPhotosCallback {
void onResponse(int code, String message, List<GooglePlacePhotoRefResult> body);
void onFailure(Throwable t);
}

View File

@ -6,4 +6,5 @@ package pl.tpolgrabia.googleutils.constants;
public class GooglePlacesConstants {
public static final String GOOGLE_PLACES_BASEURL = "https://maps.googleapis.com/maps/api/place/";
public static final Long PHOTO_MAX_WIDTH = 512L;
public static final String GOOGLE_MAPS_PLACES_API_BASEURL = "https://maps.googleapis.com/maps/api/place/";
}

View File

@ -1,59 +0,0 @@
package pl.tpolgrabia.googleutils.dto;
import com.google.gson.annotations.SerializedName;
import java.util.List;
/**
* Created by tpolgrabia on 02.10.16.
*/
public class GooglePlacePhotoRefResult {
@SerializedName("htmlAttributions")
private List<String> htmlAttributions;
private Long width;
private Long height;
@SerializedName("photoReference")
private String photoReference;
public List<String> getHtmlAttributions() {
return htmlAttributions;
}
public void setHtmlAttributions(List<String> htmlAttributions) {
this.htmlAttributions = htmlAttributions;
}
public Long getWidth() {
return width;
}
public void setWidth(Long width) {
this.width = width;
}
public Long getHeight() {
return height;
}
public void setHeight(Long height) {
this.height = height;
}
public String getPhotoReference() {
return photoReference;
}
public void setPhotoReference(String photoReference) {
this.photoReference = photoReference;
}
@Override
public String toString() {
return "GooglePlacePhotoRefResult{" +
"htmlAttributions=" + htmlAttributions +
", width=" + width +
", height=" + height +
", photoReference='" + photoReference + '\'' +
'}';
}
}

View File

@ -2,28 +2,19 @@ package pl.tpolgrabia.googleutils.utils;
import android.content.Context;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import org.apache.http.HttpStatus;
import org.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.googleutils.GooglePlacesService;
import pl.tpolgrabia.googleutils.callback.GooglePlacesPhotosCallback;
import pl.tpolgrabia.googleutils.callback.PlacesCallback;
import pl.tpolgrabia.googleutils.constants.GooglePlacesConstants;
import pl.tpolgrabia.googleutils.converter.GooglePlaceConverter;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhoto;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhotoRefResult;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.io.IOException;
import java.util.*;
import java.util.List;
/**
* Created by tpolgrabia on 27.09.16.
@ -42,12 +33,11 @@ public class PlacesUtils {
this.aq = new AQuery(ctx);
}
public void fetchNearbyPlaces(Double latitude,
Double longitude,
Double searchRadius,
String searchItemType,
String pageToken,
final PlacesCallback clbk) {
public Response<List<GooglePlaceResult>> fetchNearbyPlaces(Double latitude,
Double longitude,
Double searchRadius,
String searchItemType,
String pageToken) throws IOException {
if (latitude == null) {
throw new IllegalArgumentException("Latitude cannot be null");
@ -61,91 +51,27 @@ public class PlacesUtils {
throw new IllegalArgumentException("Search radius cannot be null");
}
String queryUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" +
"key=" + apiKey
+ "&location=" + latitude + "," + longitude
+ "&radius=" + searchRadius
+ "&type=" + searchItemType;
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
if (pageToken != null) {
queryUrl += "&pagetoken=" + pageToken;
}
aq.ajax(queryUrl,
JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject object, AjaxStatus status) {
lg.trace("Url: {}, object: {}, status: {}", url, object, status);
int statusCode = status.getCode();
String statusMessage = status.getMessage();
String statusError = status.getError();
if (statusCode != 200) {
lg.error("Invalid status code: {}, message: {}, error: {}",
statusCode,
statusMessage,
statusError);
clbk.callback((long)statusCode, statusMessage, null);
return;
}
String googleStatus = object.optString("status");
if (!"OK".equals(googleStatus)) {
lg.error("Invalid google status: {}", googleStatus);
clbk.callback((long)statusCode, googleStatus, null);
return;
}
try {
List<GooglePlaceResult> googleResults = GooglePlaceConverter.convertToPlaceResults(object.getJSONArray("results"));
clbk.callback((long) statusCode, googleStatus, googleResults);
} catch (Throwable t) {
lg.error("General error", t);
clbk.callback(-1L, "General error", null);
}
}
});
}
public List<GooglePlacePhotoRefResult> fetchPhotosByRefSync(String photosRef) throws IOException {
Response<List<GooglePlacePhotoRefResult>> results = fetchPhotosByRefInvocation(photosRef).execute();
return results.code() == HttpStatus.SC_OK ? results.body() : null;
}
public void fetchPhotosByRefAsync(String photosRef, final GooglePlacesPhotosCallback clbk) {
fetchPhotosByRefInvocation(photosRef).enqueue(new Callback<List<GooglePlacePhotoRefResult>>() {
@Override
public void onResponse(Call<List<GooglePlacePhotoRefResult>> call, Response<List<GooglePlacePhotoRefResult>> response) {
clbk.onResponse(response.code(), response.message(), response.body());
}
@Override
public void onFailure(Call<List<GooglePlacePhotoRefResult>> call, Throwable t) {
clbk.onFailure(t);
}
});
}
private Call<List<GooglePlacePhotoRefResult>> fetchPhotosByRefInvocation(String photosRef) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(GooglePlacesConstants.GOOGLE_PLACES_BASEURL)
.baseUrl(GooglePlacesConstants.GOOGLE_MAPS_PLACES_API_BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
GooglePlacesService service = retrofit.create(GooglePlacesService.class);
return service.fetchPhotosByRef(
GooglePlacesConstants.PHOTO_MAX_WIDTH,
photosRef,
apiKey);
}
public Map<String, List<GooglePlacePhotoRefResult>> fetchPhotosSync(Set<String> photoRefs) throws IOException {
HashMap<String, List<GooglePlacePhotoRefResult>> result = new HashMap<String, List<GooglePlacePhotoRefResult>>();
for (String photoRef : photoRefs) {
result.put(photoRef, fetchPhotosByRefSync(photoRef));
}
return pageToken != null ?
retrofit.create(GooglePlacesService.class).fetchNearbyPlacesNext(apiKey,
latitude + "," + longitude,
searchRadius,
searchItemType,
pageToken).execute()
: retrofit.create(GooglePlacesService.class).fetchNearbyPlacesFirst(apiKey,
latitude + "," + longitude,
searchRadius,
searchItemType).execute();
return result;
}
}