Created
November 28, 2018 11:09
-
-
Save jcbribeiro/2b4e745e99de7afbedccd960e675249b to your computer and use it in GitHub Desktop.
Awareness - Snapshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package pt.ipleiria.awarenesspdm; | |
| import android.Manifest; | |
| import android.content.pm.PackageManager; | |
| import android.graphics.Bitmap; | |
| import android.location.Location; | |
| import android.os.AsyncTask; | |
| import android.os.Bundle; | |
| import android.provider.Settings; | |
| import android.support.annotation.NonNull; | |
| import android.support.v4.app.ActivityCompat; | |
| import android.support.v4.content.ContextCompat; | |
| import android.support.v7.app.AlertDialog; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.widget.ImageView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import com.google.android.gms.awareness.Awareness; | |
| import com.google.android.gms.awareness.snapshot.DetectedActivityResponse; | |
| import com.google.android.gms.awareness.snapshot.HeadphoneStateResponse; | |
| import com.google.android.gms.awareness.snapshot.LocationResponse; | |
| import com.google.android.gms.awareness.snapshot.PlacesResponse; | |
| import com.google.android.gms.awareness.snapshot.WeatherResponse; | |
| import com.google.android.gms.awareness.state.HeadphoneState; | |
| import com.google.android.gms.awareness.state.Weather; | |
| import com.google.android.gms.common.ConnectionResult; | |
| import com.google.android.gms.common.api.GoogleApiClient; | |
| import com.google.android.gms.location.ActivityRecognitionResult; | |
| import com.google.android.gms.location.DetectedActivity; | |
| import com.google.android.gms.location.places.Place; | |
| import com.google.android.gms.location.places.PlaceLikelihood; | |
| import com.google.android.gms.location.places.PlacePhotoMetadata; | |
| import com.google.android.gms.location.places.PlacePhotoMetadataBuffer; | |
| import com.google.android.gms.location.places.PlacePhotoMetadataResult; | |
| import com.google.android.gms.location.places.Places; | |
| import com.google.android.gms.tasks.OnFailureListener; | |
| import com.google.android.gms.tasks.OnSuccessListener; | |
| import java.sql.Timestamp; | |
| import java.util.List; | |
| public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener { | |
| public static final String TAG_SNAPSHOT = "snapshot"; | |
| private static final int REQUEST_CODE_FLPERMISSION = 42; | |
| private TextView textView_main; | |
| private GoogleApiClient mGoogleApiClient; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| textView_main = findViewById(R.id.textView_main); | |
| mGoogleApiClient = new GoogleApiClient | |
| .Builder(this) | |
| .addApi(Places.GEO_DATA_API) | |
| .addApi(Places.PLACE_DETECTION_API) | |
| .enableAutoManage(this, this) | |
| .build(); | |
| } | |
| public void onClick_button_snapshot(View view) { | |
| printHeadphoneState(); | |
| printLocation(); | |
| printDetectedActivity(); | |
| printWeather(); | |
| printNearbyPlaces(); | |
| } | |
| private void printHeadphoneState() { | |
| Awareness.getSnapshotClient(this).getHeadphoneState() | |
| .addOnSuccessListener(new OnSuccessListener<HeadphoneStateResponse>() { | |
| @Override | |
| public void onSuccess(HeadphoneStateResponse headphoneStateResponse) { | |
| HeadphoneState hs = headphoneStateResponse.getHeadphoneState(); | |
| boolean pluggedIn = hs.getState() == HeadphoneState.PLUGGED_IN; | |
| Timestamp timestamp = new Timestamp(System.currentTimeMillis()); | |
| String text = "\n\n[HeadphoneState @ " + timestamp + "]\n" | |
| + "\tHeadphones are " + (pluggedIn ? "plugged in" : "unplugged"); | |
| textView_main.setText(text + textView_main.getText()); | |
| } | |
| }) | |
| .addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| Log.e(TAG_SNAPSHOT, "Could not get headphone state: " + e); | |
| Toast.makeText(MainActivity.this, "Could not get headphone state: " + e, | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| private void checkFineLocationPermission() { | |
| if (ContextCompat.checkSelfPermission( | |
| MainActivity.this, | |
| Manifest.permission.ACCESS_FINE_LOCATION) != | |
| PackageManager.PERMISSION_GRANTED) { | |
| ActivityCompat.requestPermissions( | |
| MainActivity.this, | |
| new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | |
| REQUEST_CODE_FLPERMISSION // todo: declare this constant | |
| ); | |
| } | |
| try { | |
| int locationMode = Settings.Secure.getInt(getContentResolver(), | |
| Settings.Secure.LOCATION_MODE); | |
| if (locationMode != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) { | |
| Toast.makeText(this, | |
| "Error: high accuracy location mode must be enabled in the device.", | |
| Toast.LENGTH_LONG).show(); | |
| return; | |
| } | |
| } catch (Settings.SettingNotFoundException e) { | |
| Toast.makeText(this, "Error: could not access location mode.", | |
| Toast.LENGTH_LONG).show(); | |
| e.printStackTrace(); | |
| return; | |
| } | |
| } | |
| private void printLocation() { | |
| checkFineLocationPermission(); | |
| Awareness.getSnapshotClient(this).getLocation() | |
| .addOnSuccessListener(new OnSuccessListener<LocationResponse>() { | |
| @Override | |
| public void onSuccess(LocationResponse locationResponse) { | |
| Location location = locationResponse.getLocation(); | |
| Timestamp timestamp = new Timestamp(System.currentTimeMillis()); | |
| String text = "\n\n[Location @ " + timestamp + "]\n" | |
| + "\tLat:" + location.getLatitude() + ", Lng:" + location.getLongitude(); | |
| textView_main.setText(text + textView_main.getText()); | |
| } | |
| }) | |
| .addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| Log.e(TAG_SNAPSHOT, "Could not get Location: " + e); | |
| Toast.makeText(MainActivity.this, "Could not get Location: " + e, | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| private void printWeather() { | |
| checkFineLocationPermission(); | |
| Awareness.getSnapshotClient(this).getWeather() | |
| .addOnSuccessListener(new OnSuccessListener<WeatherResponse>() { | |
| @Override | |
| public void onSuccess(WeatherResponse weatherResponse) { | |
| Weather weather = weatherResponse.getWeather(); | |
| Timestamp timestamp = new Timestamp(System.currentTimeMillis()); | |
| String text = "\n\n[Weather @ " + timestamp + "]\n" | |
| + "\tWeather: " + weather; | |
| for (int condition : weather.getConditions()) { | |
| switch (condition) { | |
| case Weather.CONDITION_CLEAR: | |
| text += " Clear weather condition."; | |
| break; | |
| case Weather.CONDITION_CLOUDY: | |
| text += " Cloudy weather condition."; | |
| break; | |
| case Weather.CONDITION_FOGGY: | |
| text += " Foggy weather condition."; | |
| break; | |
| case Weather.CONDITION_HAZY: | |
| text += " Hazy weather condition."; | |
| break; | |
| case Weather.CONDITION_ICY: | |
| text += " Icy weather condition."; | |
| break; | |
| case Weather.CONDITION_RAINY: | |
| text += " Rainy weather condition."; | |
| break; | |
| case Weather.CONDITION_SNOWY: | |
| text += " Snowy weather condition."; | |
| break; | |
| case Weather.CONDITION_STORMY: | |
| text += " Stormy weather condition."; | |
| break; | |
| case Weather.CONDITION_UNKNOWN: | |
| text += " Unknown weather condition."; | |
| break; | |
| case Weather.CONDITION_WINDY: | |
| text += " Windy weather condition."; | |
| break; | |
| } | |
| } | |
| textView_main.setText(text + textView_main.getText()); | |
| } | |
| }) | |
| .addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| Log.e(TAG_SNAPSHOT, "Could not get Weather: " + e); | |
| Toast.makeText(MainActivity.this, "Could not get Weather: " + e, | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| private void printDetectedActivity() { | |
| Awareness.getSnapshotClient(this).getDetectedActivity() | |
| .addOnSuccessListener(new OnSuccessListener<DetectedActivityResponse>() { | |
| @Override | |
| public void onSuccess(DetectedActivityResponse dar) { | |
| ActivityRecognitionResult arr = dar.getActivityRecognitionResult(); | |
| DetectedActivity probableActivity = arr.getMostProbableActivity(); | |
| int confidence = probableActivity.getConfidence(); | |
| String activityStr = probableActivity.toString(); | |
| Timestamp timestamp = new Timestamp(System.currentTimeMillis()); | |
| String text = "\n\n[DetectedActivity @ " + timestamp + "]\n" | |
| + "\tActivity: " + activityStr + ", Confidence: " + confidence + "/100"; | |
| textView_main.setText(text + textView_main.getText()); | |
| } | |
| }) | |
| .addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| Log.e(TAG_SNAPSHOT, "Could not get Detected Activity: " + e); | |
| Toast.makeText(MainActivity.this, "Could not get Detected Activity: " + e, | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| private void printNearbyPlaces() { | |
| checkFineLocationPermission(); | |
| Awareness.getSnapshotClient(this).getPlaces() | |
| .addOnSuccessListener(new OnSuccessListener<PlacesResponse>() { | |
| @Override | |
| public void onSuccess(PlacesResponse placesResponse) { | |
| List<PlaceLikelihood> pll = placesResponse.getPlaceLikelihoods(); | |
| // Show the top 5 possible location results. | |
| String plText = ""; | |
| for (int i = 0; i < (pll.size() < 5 ? pll.size() : 5); i++) { | |
| PlaceLikelihood pl = pll.get(i); | |
| if (i == 0) { // place com mais likelihood | |
| Place place = pl.getPlace(); | |
| PhotoTask photoTask = new PhotoTask(); | |
| photoTask.execute(place.getId(), place.getName().toString()); | |
| } | |
| plText += "\t#" + i + ": " + pl.getPlace().getName().toString() | |
| + "\n\tlikelihood: " + pl.getLikelihood() | |
| + "\n\taddress: " + pl.getPlace().getAddress() | |
| + "\n\tlocation: " + pl.getPlace().getLatLng() | |
| + "\n\twebsite: " + pl.getPlace().getWebsiteUri() | |
| + "\n\tplaceTypes: " + pl.getPlace().getPlaceTypes() | |
| + "\t" + printPlaceTypes(pl.getPlace().getPlaceTypes()) + "\n"; | |
| } | |
| Timestamp timestamp = new Timestamp(System.currentTimeMillis()); | |
| String text = "\n\n[Places @ " + timestamp + "]\n" + plText; | |
| textView_main.setText(text + textView_main.getText()); | |
| } | |
| }) | |
| .addOnFailureListener(new OnFailureListener() { | |
| @Override | |
| public void onFailure(@NonNull Exception e) { | |
| Log.e(TAG_SNAPSHOT, "Could not get Places: " + e); | |
| Toast.makeText(MainActivity.this, "Could not get Places: " + e, | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| } | |
| private String printPlaceTypes(List<Integer> placeTypes) { | |
| String res = ""; | |
| for (int placeType : | |
| placeTypes) { | |
| switch (placeType) { | |
| case Place.TYPE_ACCOUNTING : res += "TYPE_ACCOUNTING"; break; | |
| case Place.TYPE_ADMINISTRATIVE_AREA_LEVEL_1 : res += "TYPE_ADMINISTRATIVE_AREA_LEVEL_1"; break; | |
| case Place.TYPE_ADMINISTRATIVE_AREA_LEVEL_2 : res += "TYPE_ADMINISTRATIVE_AREA_LEVEL_2"; break; | |
| case Place.TYPE_ADMINISTRATIVE_AREA_LEVEL_3 : res += "TYPE_ADMINISTRATIVE_AREA_LEVEL_3"; break; | |
| case Place.TYPE_AIRPORT : res += "TYPE_AIRPORT"; break; | |
| case Place.TYPE_AMUSEMENT_PARK : res += "TYPE_AMUSEMENT_PARK"; break; | |
| case Place.TYPE_AQUARIUM : res += "TYPE_AQUARIUM"; break; | |
| case Place.TYPE_ART_GALLERY : res += "TYPE_ART_GALLERY"; break; | |
| case Place.TYPE_ATM : res += "TYPE_ATM"; break; | |
| case Place.TYPE_BAKERY : res += "TYPE_BAKERY"; break; | |
| case Place.TYPE_BANK : res += "TYPE_BANK"; break; | |
| case Place.TYPE_BAR : res += "TYPE_BAR"; break; | |
| case Place.TYPE_BEAUTY_SALON : res += "TYPE_BEAUTY_SALON"; break; | |
| case Place.TYPE_BICYCLE_STORE : res += "TYPE_BICYCLE_STORE"; break; | |
| case Place.TYPE_BOOK_STORE : res += "TYPE_BOOK_STORE"; break; | |
| case Place.TYPE_BOWLING_ALLEY : res += "TYPE_BOWLING_ALLEY"; break; | |
| case Place.TYPE_BUS_STATION : res += "TYPE_BUS_STATION"; break; | |
| case Place.TYPE_CAFE : res += "TYPE_CAFE"; break; | |
| case Place.TYPE_CAMPGROUND : res += "TYPE_CAMPGROUND"; break; | |
| case Place.TYPE_CAR_DEALER : res += "TYPE_CAR_DEALER"; break; | |
| case Place.TYPE_CAR_RENTAL : res += "TYPE_CAR_RENTAL"; break; | |
| case Place.TYPE_CAR_REPAIR : res += "TYPE_CAR_REPAIR"; break; | |
| case Place.TYPE_CAR_WASH : res += "TYPE_CAR_WASH"; break; | |
| case Place.TYPE_CASINO : res += "TYPE_CASINO"; break; | |
| case Place.TYPE_CEMETERY : res += "TYPE_CEMETERY"; break; | |
| case Place.TYPE_CHURCH : res += "TYPE_CHURCH"; break; | |
| case Place.TYPE_CITY_HALL : res += "TYPE_CITY_HALL"; break; | |
| case Place.TYPE_CLOTHING_STORE : res += "TYPE_CLOTHING_STORE"; break; | |
| case Place.TYPE_COLLOQUIAL_AREA : res += "TYPE_COLLOQUIAL_AREA"; break; | |
| case Place.TYPE_CONVENIENCE_STORE : res += "TYPE_CONVENIENCE_STORE"; break; | |
| case Place.TYPE_COUNTRY : res += "TYPE_COUNTRY"; break; | |
| case Place.TYPE_COURTHOUSE : res += "TYPE_COURTHOUSE"; break; | |
| case Place.TYPE_DENTIST : res += "TYPE_DENTIST"; break; | |
| case Place.TYPE_DEPARTMENT_STORE : res += "TYPE_DEPARTMENT_STORE"; break; | |
| case Place.TYPE_DOCTOR : res += "TYPE_DOCTOR"; break; | |
| case Place.TYPE_ELECTRICIAN : res += "TYPE_ELECTRICIAN"; break; | |
| case Place.TYPE_ELECTRONICS_STORE : res += "TYPE_ELECTRONICS_STORE"; break; | |
| case Place.TYPE_EMBASSY : res += "TYPE_EMBASSY"; break; | |
| case Place.TYPE_ESTABLISHMENT : res += "TYPE_ESTABLISHMENT"; break; | |
| case Place.TYPE_FINANCE : res += "TYPE_FINANCE"; break; | |
| case Place.TYPE_FIRE_STATION : res += "TYPE_FIRE_STATION"; break; | |
| case Place.TYPE_FLOOR : res += "TYPE_FLOOR"; break; | |
| case Place.TYPE_FLORIST : res += "TYPE_FLORIST"; break; | |
| case Place.TYPE_FOOD : res += "TYPE_FOOD"; break; | |
| case Place.TYPE_FUNERAL_HOME : res += "TYPE_FUNERAL_HOME"; break; | |
| case Place.TYPE_FURNITURE_STORE : res += "TYPE_FURNITURE_STORE"; break; | |
| case Place.TYPE_GAS_STATION : res += "TYPE_GAS_STATION"; break; | |
| case Place.TYPE_GENERAL_CONTRACTOR : res += "TYPE_GENERAL_CONTRACTOR"; break; | |
| case Place.TYPE_GEOCODE : res += "TYPE_GEOCODE"; break; | |
| case Place.TYPE_GROCERY_OR_SUPERMARKET : res += "TYPE_GROCERY_OR_SUPERMARKET"; break; | |
| case Place.TYPE_GYM : res += "TYPE_GYM"; break; | |
| case Place.TYPE_HAIR_CARE : res += "TYPE_HAIR_CARE"; break; | |
| case Place.TYPE_HARDWARE_STORE : res += "TYPE_HARDWARE_STORE"; break; | |
| case Place.TYPE_HEALTH : res += "TYPE_HEALTH"; break; | |
| case Place.TYPE_HINDU_TEMPLE : res += "TYPE_HINDU_TEMPLE"; break; | |
| case Place.TYPE_HOME_GOODS_STORE : res += "TYPE_HOME_GOODS_STORE"; break; | |
| case Place.TYPE_HOSPITAL : res += "TYPE_HOSPITAL"; break; | |
| case Place.TYPE_INSURANCE_AGENCY : res += "TYPE_INSURANCE_AGENCY"; break; | |
| case Place.TYPE_INTERSECTION : res += "TYPE_INTERSECTION"; break; | |
| case Place.TYPE_JEWELRY_STORE : res += "TYPE_JEWELRY_STORE"; break; | |
| case Place.TYPE_LAUNDRY : res += "TYPE_LAUNDRY"; break; | |
| case Place.TYPE_LAWYER : res += "TYPE_LAWYER"; break; | |
| case Place.TYPE_LIBRARY : res += "TYPE_LIBRARY"; break; | |
| case Place.TYPE_LIQUOR_STORE : res += "TYPE_LIQUOR_STORE"; break; | |
| case Place.TYPE_LOCALITY : res += "TYPE_LOCALITY"; break; | |
| case Place.TYPE_LOCAL_GOVERNMENT_OFFICE : res += "TYPE_LOCAL_GOVERNMENT_OFFICE"; break; | |
| case Place.TYPE_LOCKSMITH : res += "TYPE_LOCKSMITH"; break; | |
| case Place.TYPE_LODGING : res += "TYPE_LODGING"; break; | |
| case Place.TYPE_MEAL_DELIVERY : res += "TYPE_MEAL_DELIVERY"; break; | |
| case Place.TYPE_MEAL_TAKEAWAY : res += "TYPE_MEAL_TAKEAWAY"; break; | |
| case Place.TYPE_MOSQUE : res += "TYPE_MOSQUE"; break; | |
| case Place.TYPE_MOVIE_RENTAL : res += "TYPE_MOVIE_RENTAL"; break; | |
| case Place.TYPE_MOVIE_THEATER : res += "TYPE_MOVIE_THEATER"; break; | |
| case Place.TYPE_MOVING_COMPANY : res += "TYPE_MOVING_COMPANY"; break; | |
| case Place.TYPE_MUSEUM : res += "TYPE_MUSEUM"; break; | |
| case Place.TYPE_NATURAL_FEATURE : res += "TYPE_NATURAL_FEATURE"; break; | |
| case Place.TYPE_NEIGHBORHOOD : res += "TYPE_NEIGHBORHOOD"; break; | |
| case Place.TYPE_NIGHT_CLUB : res += "TYPE_NIGHT_CLUB"; break; | |
| case Place.TYPE_OTHER : res += "TYPE_OTHER"; break; | |
| case Place.TYPE_PAINTER : res += "TYPE_PAINTER"; break; | |
| case Place.TYPE_PARK : res += "TYPE_PARK"; break; | |
| case Place.TYPE_PARKING : res += "TYPE_PARKING"; break; | |
| case Place.TYPE_PET_STORE : res += "TYPE_PET_STORE"; break; | |
| case Place.TYPE_PHARMACY : res += "TYPE_PHARMACY"; break; | |
| case Place.TYPE_PHYSIOTHERAPIST : res += "TYPE_PHYSIOTHERAPIST"; break; | |
| case Place.TYPE_PLACE_OF_WORSHIP : res += "TYPE_PLACE_OF_WORSHIP"; break; | |
| case Place.TYPE_PLUMBER : res += "TYPE_PLUMBER"; break; | |
| case Place.TYPE_POINT_OF_INTEREST : res += "TYPE_POINT_OF_INTEREST"; break; | |
| case Place.TYPE_POLICE : res += "TYPE_POLICE"; break; | |
| case Place.TYPE_POLITICAL : res += "TYPE_POLITICAL"; break; | |
| case Place.TYPE_POSTAL_CODE : res += "TYPE_POSTAL_CODE"; break; | |
| case Place.TYPE_POSTAL_CODE_PREFIX : res += "TYPE_POSTAL_CODE_PREFIX"; break; | |
| case Place.TYPE_POSTAL_TOWN : res += "TYPE_POSTAL_TOWN"; break; | |
| case Place.TYPE_POST_BOX : res += "TYPE_POST_BOX"; break; | |
| case Place.TYPE_POST_OFFICE : res += "TYPE_POST_OFFICE"; break; | |
| case Place.TYPE_PREMISE : res += "TYPE_PREMISE"; break; | |
| case Place.TYPE_REAL_ESTATE_AGENCY : res += "TYPE_REAL_ESTATE_AGENCY"; break; | |
| case Place.TYPE_RESTAURANT : res += "TYPE_RESTAURANT"; break; | |
| case Place.TYPE_ROOFING_CONTRACTOR : res += "TYPE_ROOFING_CONTRACTOR"; break; | |
| case Place.TYPE_ROOM : res += "TYPE_ROOM"; break; | |
| case Place.TYPE_ROUTE : res += "TYPE_ROUTE"; break; | |
| case Place.TYPE_RV_PARK : res += "TYPE_RV_PARK"; break; | |
| case Place.TYPE_SCHOOL : res += "TYPE_SCHOOL"; break; | |
| case Place.TYPE_SHOE_STORE : res += "TYPE_SHOE_STORE"; break; | |
| case Place.TYPE_SHOPPING_MALL : res += "TYPE_SHOPPING_MALL"; break; | |
| case Place.TYPE_SPA : res += "TYPE_SPA"; break; | |
| case Place.TYPE_STADIUM : res += "TYPE_STADIUM"; break; | |
| case Place.TYPE_STORAGE : res += "TYPE_STORAGE"; break; | |
| case Place.TYPE_STORE : res += "TYPE_STORE"; break; | |
| case Place.TYPE_STREET_ADDRESS : res += "TYPE_STREET_ADDRESS"; break; | |
| case Place.TYPE_SUBLOCALITY : res += "TYPE_SUBLOCALITY"; break; | |
| case Place.TYPE_SUBLOCALITY_LEVEL_1 : res += "TYPE_SUBLOCALITY_LEVEL_1"; break; | |
| case Place.TYPE_SUBLOCALITY_LEVEL_2 : res += "TYPE_SUBLOCALITY_LEVEL_2"; break; | |
| case Place.TYPE_SUBLOCALITY_LEVEL_3 : res += "TYPE_SUBLOCALITY_LEVEL_3"; break; | |
| case Place.TYPE_SUBLOCALITY_LEVEL_4 : res += "TYPE_SUBLOCALITY_LEVEL_4"; break; | |
| case Place.TYPE_SUBLOCALITY_LEVEL_5 : res += "TYPE_SUBLOCALITY_LEVEL_5"; break; | |
| case Place.TYPE_SUBPREMISE : res += "TYPE_SUBPREMISE"; break; | |
| case Place.TYPE_SUBWAY_STATION : res += "TYPE_SUBWAY_STATION"; break; | |
| case Place.TYPE_SYNAGOGUE : res += "TYPE_SYNAGOGUE"; break; | |
| case Place.TYPE_SYNTHETIC_GEOCODE : res += "TYPE_SYNTHETIC_GEOCODE"; break; | |
| case Place.TYPE_TAXI_STAND : res += "TYPE_TAXI_STAND"; break; | |
| case Place.TYPE_TRAIN_STATION : res += "TYPE_TRAIN_STATION"; break; | |
| case Place.TYPE_TRANSIT_STATION : res += "TYPE_TRANSIT_STATION"; break; | |
| case Place.TYPE_TRAVEL_AGENCY : res += "TYPE_TRAVEL_AGENCY"; break; | |
| case Place.TYPE_UNIVERSITY : res += "TYPE_UNIVERSITY"; break; | |
| case Place.TYPE_VETERINARY_CARE : res += "TYPE_VETERINARY_CARE"; break; | |
| case Place.TYPE_ZOO : res += "TYPE_ZOO"; break; | |
| } | |
| res += "\t"; | |
| } | |
| return res; | |
| } | |
| @Override | |
| public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { | |
| Toast.makeText(this, connectionResult.getErrorMessage(), Toast.LENGTH_SHORT).show(); | |
| } | |
| /* | |
| Notes: | |
| -- you must enable the "Google Places API for Android" for the project in | |
| the Google Developers Console: | |
| https://developers.google.com/places/android-api/signup#api-console | |
| -- you must connect to the to the Places API using the Google Play services API client: | |
| https://developers.google.com/places/android-api/start#connect-client | |
| -- for executing, use something like: | |
| new PhotoTask().execute(place.getId(), place.getName().toString()); | |
| */ | |
| private class PhotoTask extends AsyncTask<String, Void, Bitmap> { | |
| private String placeName; | |
| @Override | |
| protected Bitmap doInBackground(String... params) { | |
| if (params.length != 2) { | |
| throw new IllegalArgumentException("The place id must be the first parameter; and the place name must be the second parameter."); | |
| } | |
| final String placeId = params[0]; | |
| placeName = params[1]; | |
| Bitmap bitmap = null; | |
| PlacePhotoMetadataResult result = Places.GeoDataApi.getPlacePhotos(mGoogleApiClient, placeId).await(); | |
| if (result.getStatus().isSuccess()) { | |
| PlacePhotoMetadataBuffer photoMetadataBuffer = result.getPhotoMetadata(); | |
| if (photoMetadataBuffer.getCount() > 0 && !isCancelled()) { | |
| // Get the first bitmap. | |
| PlacePhotoMetadata photo = photoMetadataBuffer.get(0); | |
| bitmap = photo.getPhoto(mGoogleApiClient).await().getBitmap(); | |
| } | |
| // Release the PlacePhotoMetadataBuffer. | |
| photoMetadataBuffer.release(); | |
| } | |
| return bitmap; | |
| } | |
| @Override | |
| protected void onPostExecute(Bitmap bitmap) { | |
| if (bitmap != null) { | |
| ImageView imageView = new ImageView(MainActivity.this); | |
| imageView.setImageBitmap(bitmap); | |
| new AlertDialog.Builder(MainActivity.this). | |
| setMessage(placeName). | |
| setView(imageView) | |
| .show(); | |
| } else { | |
| Toast.makeText(MainActivity.this, "Could not find a photo for " + placeName, Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment