Showing google places api status.

master
Tomasz Półgrabia 2016-10-04 23:14:00 +02:00
parent 90238d5515
commit af3120f874
4 changed files with 36 additions and 14 deletions

View File

@ -54,7 +54,11 @@ public class PlacesAdapter extends ArrayAdapter<GooglePlaceResult> {
placeAddressWidget.setText(item.getVicinity());
TextView placeRateWidget = (TextView) resultView.findViewById(R.id.place_rate);
placeRateWidget.setText("" + item.getRating());
if (item.getRating() != null && !item.getRating().equals(Double.NaN)) {
placeRateWidget.setText("" + item.getRating());
} else {
placeRateWidget.setText("N/A");
}
ImageView placePreviewWidget = (ImageView)resultView.findViewById(R.id.place_img_preview);
placePreviewWidget.setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.noimage));

View File

@ -12,6 +12,7 @@ public class GooglePlacesResponse {
private String nextPageToken;
private String originalPageToken;
private String status;
public List<GooglePlaceResult> getPlaces() {
return places;
@ -37,12 +38,21 @@ public class GooglePlacesResponse {
return originalPageToken;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "GooglePlacesResponse{" +
"places=" + places +
", nextPageToken='" + nextPageToken + '\'' +
", originalPageToken='" + originalPageToken + '\'' +
", status='" + status + '\'' +
'}';
}
}

View File

@ -179,6 +179,8 @@ public class PlacesFragment extends Fragment {
if ((places == null || places.isEmpty()) && !noMoreResults) {
fetchNearbyPlacesAndPresent(location);
} else {
lg.debug("Places: {}, no more results: {}", places, noMoreResults);
}
}

View File

@ -3,31 +3,21 @@ package pl.tpolgrabia.urbanexplorer.worker;
import android.content.Context;
import android.location.Location;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import org.apache.http.HttpStatus;
import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.googleutils.callback.PlacesCallback;
import pl.tpolgrabia.googleutils.dto.GooglePlacePhoto;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResponse;
import pl.tpolgrabia.googleutils.dto.GooglePlaceResult;
import pl.tpolgrabia.googleutils.utils.PlacesUtils;
import pl.tpolgrabia.urbanexplorer.AppConstants;
import pl.tpolgrabia.urbanexplorer.R;
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;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Semaphore;
/**
* Created by tpolgrabia on 03.10.16.
@ -37,9 +27,11 @@ public class GooglePlacesWorker extends AsyncTask<GooglePlacesRequest, Integer,
private static final Logger lg = LoggerFactory.getLogger(GooglePlacesWorker.class);
private final PlacesUtils placesUtils;
private final Context ctx;
public GooglePlacesWorker(Context ctx) {
this.placesUtils = new PlacesUtils(ctx, AppConstants.GOOGLE_API_KEY);
this.ctx = ctx;
}
@Override
@ -64,9 +56,12 @@ public class GooglePlacesWorker extends AsyncTask<GooglePlacesRequest, Integer,
if (placesResponse != null && placesResponse.code() == HttpStatus.SC_OK) {
GooglePlacesResponse response = new GooglePlacesResponse();
response.setPlaces(placesResponse.body().getResults());
response.setNextPageToken(placesResponse.body().getNextPageToken());
final GooglePlaceResponse responseBody = placesResponse.body();
lg.debug("Google response body: {}", responseBody);
response.setPlaces(responseBody.getResults());
response.setNextPageToken(responseBody.getNextPageToken());
response.setOriginalPageToken(param.getPageToken());
response.setStatus(responseBody.getStatus());
result.add(response);
}
@ -85,6 +80,17 @@ public class GooglePlacesWorker extends AsyncTask<GooglePlacesRequest, Integer,
lg.debug("Post execute {}", googlePlacesResponses);
for (GooglePlacesResponse response : googlePlacesResponses) {
final String googleStatus = response.getStatus();
if (!"OK".equals(googleStatus) && !"SUCCESS".equals(googleStatus)) {
if (!"OVER_QUERY_LIMIT".equals(googleStatus)) {
Toast.makeText(ctx, "Google returned status {}", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ctx,
"This application has exceeded free google places api daily limit - 150k." +
" Sorry for that - I can nothing do more except from buying the premium plan which" +
" is nearly zero-probable - this is free app", Toast.LENGTH_LONG).show();
}
}
EventBus.getDefault().post(response);
}
}