From 38126694b7019000de150fce3ac77f1c6a9f6a0d Mon Sep 17 00:00:00 2001 From: Chris Arriola Date: Thu, 14 Oct 2021 12:17:37 -0500 Subject: [PATCH] feat: Bump Places to 2.5.0 (#263) Change-Id: Ia4c924843c6423a889cb02920630538c38b6afda --- demo-java/app/build.gradle | 3 +- .../placesdemo/AutocompleteTestActivity.java | 9 +- .../placesdemo/CurrentPlaceTestActivity.java | 3 +- .../com/example/placesdemo/FieldSelector.java | 6 +- .../placesdemo/PlaceAndPhotoTestActivity.java | 445 +++++++++--------- .../layout/place_and_photo_test_activity.xml | 31 +- demo-java/app/src/main/res/values/strings.xml | 9 + .../gradle/wrapper/gradle-wrapper.properties | 2 +- demo-kotlin/app/build.gradle | 4 +- .../placesdemo/PlaceAndPhotoTestActivity.kt | 55 ++- .../layout/place_and_photo_test_activity.xml | 31 +- .../app/src/main/res/values/strings.xml | 9 + .../gradle/wrapper/gradle-wrapper.properties | 2 +- snippets/app/build.gradle | 3 +- .../com/google/places/PlacesIconActivity.java | 25 + .../places/kotlin/PlacesIconActivity.kt | 21 + .../gradle/wrapper/gradle-wrapper.properties | 2 +- 17 files changed, 424 insertions(+), 236 deletions(-) create mode 100644 snippets/app/src/main/java/com/google/places/PlacesIconActivity.java create mode 100644 snippets/app/src/main/java/com/google/places/kotlin/PlacesIconActivity.kt diff --git a/demo-java/app/build.gradle b/demo-java/app/build.gradle index 490e1836..8bba9393 100644 --- a/demo-java/app/build.gradle +++ b/demo-java/app/build.gradle @@ -61,9 +61,10 @@ dependencies { implementation "androidx.activity:activity:1.3.1" implementation "androidx.fragment:fragment:1.3.6" implementation 'com.android.volley:volley:1.2.1' + implementation "com.github.bumptech.glide:glide:4.9.0" // GMS - gmsImplementation 'com.google.android.libraries.places:places:2.4.0' + gmsImplementation 'com.google.android.libraries.places:places:2.5.0' gmsImplementation 'com.google.android.gms:play-services-maps:17.0.1' gmsImplementation 'com.google.maps.android:android-maps-utils:2.2.6' diff --git a/demo-java/app/src/gms/java/com/example/placesdemo/AutocompleteTestActivity.java b/demo-java/app/src/gms/java/com/example/placesdemo/AutocompleteTestActivity.java index ba834b9b..05244823 100644 --- a/demo-java/app/src/gms/java/com/example/placesdemo/AutocompleteTestActivity.java +++ b/demo-java/app/src/gms/java/com/example/placesdemo/AutocompleteTestActivity.java @@ -50,7 +50,6 @@ import java.util.List; import androidx.annotation.IdRes; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.appcompat.app.AlertDialog; @@ -113,7 +112,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } @Override - protected void onSaveInstanceState(@NonNull Bundle bundle) { + protected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(bundle); fieldSelector.onSaveInstanceState(bundle); } @@ -138,17 +137,16 @@ private void setupAutocompleteSupportFragment() { .setActivityMode(getMode())); } - @NonNull private PlaceSelectionListener getPlaceSelectionListener() { return new PlaceSelectionListener() { @Override - public void onPlaceSelected(@NonNull Place place) { + public void onPlaceSelected(Place place) { responseView.setText( StringUtil.stringifyAutocompleteWidget(place, isDisplayRawResultsChecked())); } @Override - public void onError(@NonNull Status status) { + public void onError(Status status) { responseView.setText(status.getStatusMessage()); } }; @@ -245,7 +243,6 @@ private String getHint() { return getTextViewValue(R.id.autocomplete_hint); } - @NonNull private List getCountries() { String countryString = getTextViewValue(R.id.autocomplete_country); if (TextUtils.isEmpty(countryString)) { diff --git a/demo-java/app/src/gms/java/com/example/placesdemo/CurrentPlaceTestActivity.java b/demo-java/app/src/gms/java/com/example/placesdemo/CurrentPlaceTestActivity.java index 4200db56..61f534e4 100644 --- a/demo-java/app/src/gms/java/com/example/placesdemo/CurrentPlaceTestActivity.java +++ b/demo-java/app/src/gms/java/com/example/placesdemo/CurrentPlaceTestActivity.java @@ -34,7 +34,6 @@ import java.util.List; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresPermission; import androidx.appcompat.app.AppCompatActivity; @@ -90,7 +89,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { } @Override - protected void onSaveInstanceState(@NonNull Bundle bundle) { + protected void onSaveInstanceState(Bundle bundle) { super.onSaveInstanceState(bundle); fieldSelector.onSaveInstanceState(bundle); } diff --git a/demo-java/app/src/gms/java/com/example/placesdemo/FieldSelector.java b/demo-java/app/src/gms/java/com/example/placesdemo/FieldSelector.java index 74e1d569..421d5616 100644 --- a/demo-java/app/src/gms/java/com/example/placesdemo/FieldSelector.java +++ b/demo-java/app/src/gms/java/com/example/placesdemo/FieldSelector.java @@ -39,7 +39,6 @@ import java.util.List; import java.util.Map; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; @@ -166,7 +165,7 @@ public String getSelectedString() { } - public void onSaveInstanceState(@NonNull Bundle bundle) { + public void onSaveInstanceState(Bundle bundle) { List fields = getSelectedFields(); ArrayList serializedFields = new ArrayList<>(); @@ -217,9 +216,8 @@ private static void updateView(View view, State state) { } } - @NonNull @Override - public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + public View getView(int position, @Nullable View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); State state = getItem(position); updateView(view, state); diff --git a/demo-java/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.java b/demo-java/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.java index 561650d5..10fdf414 100644 --- a/demo-java/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.java +++ b/demo-java/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.java @@ -16,6 +16,7 @@ package com.example.placesdemo; +import com.bumptech.glide.Glide; import com.google.android.gms.tasks.Task; import com.google.android.libraries.places.api.Places; import com.google.android.libraries.places.api.model.PhotoMetadata; @@ -41,7 +42,6 @@ import java.util.List; import androidx.annotation.IdRes; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; import androidx.appcompat.app.AlertDialog; @@ -51,249 +51,274 @@ * Activity for testing {@link PlacesClient#fetchPlace(FetchPlaceRequest)}. */ public class PlaceAndPhotoTestActivity extends AppCompatActivity { - private static final String FETCHED_PHOTO_KEY = "photo_image"; - private PlacesClient placesClient; - private ImageView photoView; - private TextView responseView; - private PhotoMetadata photo; - private FieldSelector fieldSelector; - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Use whatever theme was set from the MainActivity. - int theme = getIntent().getIntExtra(MainActivity.THEME_RES_ID_EXTRA, 0); - if (theme != 0) { - setTheme(theme); - } - setContentView(R.layout.place_and_photo_test_activity); + private static final String FETCHED_PHOTO_KEY = "photo_image"; + private PlacesClient placesClient; + private ImageView photoView; + private ImageView iconView; + private TextView responseView; + private PhotoMetadata photo; + private FieldSelector fieldSelector; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Use whatever theme was set from the MainActivity. + int theme = getIntent().getIntExtra(MainActivity.THEME_RES_ID_EXTRA, 0); + if (theme != 0) { + setTheme(theme); + } - // Retrieve a PlacesClient (previously initialized - see MainActivity) - placesClient = Places.createClient(this); - if (savedInstanceState != null) { - photo = savedInstanceState.getParcelable(FETCHED_PHOTO_KEY); - } + setContentView(R.layout.place_and_photo_test_activity); + // Retrieve a PlacesClient (previously initialized - see MainActivity) + placesClient = Places.createClient(this); + if (savedInstanceState != null) { + photo = savedInstanceState.getParcelable(FETCHED_PHOTO_KEY); + } - // Set up view objects - responseView = findViewById(R.id.response); - photoView = findViewById(R.id.photo); - CheckBox fetchPhotoCheckbox = findViewById(R.id.fetch_photo_checkbox); - fetchPhotoCheckbox.setOnCheckedChangeListener( + // Set up view objects + responseView = findViewById(R.id.response); + photoView = findViewById(R.id.photo); + iconView = findViewById(R.id.icon); + CheckBox fetchPhotoCheckbox = findViewById(R.id.fetch_photo_checkbox); + fetchPhotoCheckbox.setOnCheckedChangeListener( (buttonView, isChecked) -> setPhotoSizingEnabled(isChecked)); - CheckBox customPhotoCheckbox = findViewById(R.id.use_custom_photo_reference); - customPhotoCheckbox.setOnCheckedChangeListener( + CheckBox customPhotoCheckbox = findViewById(R.id.use_custom_photo_reference); + customPhotoCheckbox.setOnCheckedChangeListener( (buttonView, isChecked) -> setCustomPhotoReferenceEnabled(isChecked)); - fieldSelector = - new FieldSelector( + fieldSelector = + new FieldSelector( findViewById(R.id.use_custom_fields), findViewById(R.id.custom_fields_list), savedInstanceState); - // Set listeners for programmatic Fetch Place - findViewById(R.id.fetch_place_and_photo_button).setOnClickListener(view -> fetchPlace()); + // Set listeners for programmatic Fetch Place + findViewById(R.id.fetch_place_and_photo_button).setOnClickListener(view -> fetchPlace()); - // UI initialization - setLoading(false); - setPhotoSizingEnabled(fetchPhotoCheckbox.isChecked()); - setCustomPhotoReferenceEnabled(customPhotoCheckbox.isChecked()); - if (photo != null) { - fetchPhoto(photo); + // UI initialization + setLoading(false); + setPhotoSizingEnabled(fetchPhotoCheckbox.isChecked()); + setCustomPhotoReferenceEnabled(customPhotoCheckbox.isChecked()); + if (photo != null) { + fetchPhoto(photo); + } } - } - - @Override - protected void onSaveInstanceState(@NonNull Bundle bundle) { - super.onSaveInstanceState(bundle); - fieldSelector.onSaveInstanceState(bundle); - bundle.putParcelable(FETCHED_PHOTO_KEY, photo); - } - - /** - * Fetches the {@link Place} specified via the UI and displays it. May also trigger {@link - * #fetchPhoto(PhotoMetadata)} if set in the UI. - */ - private void fetchPlace() { - responseView.setText(null); - photoView.setImageBitmap(null); - dismissKeyboard(findViewById(R.id.place_id_field)); - - final boolean isFetchPhotoChecked = isFetchPhotoChecked(); - List placeFields = getPlaceFields(); - String customPhotoReference = getCustomPhotoReference(); - if (!validateInputs(isFetchPhotoChecked, placeFields, customPhotoReference)) { - return; + + @Override + protected void onSaveInstanceState(Bundle bundle) { + super.onSaveInstanceState(bundle); + fieldSelector.onSaveInstanceState(bundle); + bundle.putParcelable(FETCHED_PHOTO_KEY, photo); } - setLoading(true); + /** + * Fetches the {@link Place} specified via the UI and displays it. May also trigger {@link + * #fetchPhoto(PhotoMetadata)} if set in the UI. + */ + private void fetchPlace() { + responseView.setText(null); + photoView.setImageBitmap(null); + iconView.setImageBitmap(null); + + dismissKeyboard(findViewById(R.id.place_id_field)); + + final boolean isFetchPhotoChecked = isFetchPhotoChecked(); + final boolean isFetchIconChecked = isFetchIconChecked(); + List placeFields = getPlaceFields(); + String customPhotoReference = getCustomPhotoReference(); + if (!validateInputs(isFetchPhotoChecked, isFetchIconChecked, placeFields, + customPhotoReference)) { + return; + } + + setLoading(true); + + FetchPlaceRequest request = FetchPlaceRequest.newInstance(getPlaceId(), placeFields); + Task placeTask = placesClient.fetchPlace(request); + + placeTask.addOnSuccessListener( + (response) -> { + responseView.setText(StringUtil.stringify(response, isDisplayRawResultsChecked())); + if (isFetchPhotoChecked) { + attemptFetchPhoto(response.getPlace()); + } + if (isFetchIconChecked) { + attemptFetchIcon(response.getPlace()); + } + }); + + placeTask.addOnFailureListener( + (exception) -> { + exception.printStackTrace(); + responseView.setText(exception.getMessage()); + }); + + placeTask.addOnCompleteListener(response -> setLoading(false)); + } + + private void attemptFetchPhoto(Place place) { + List photoMetadatas = place.getPhotoMetadatas(); + if (photoMetadatas != null && !photoMetadatas.isEmpty()) { + fetchPhoto(photoMetadatas.get(0)); + } + } + + private void attemptFetchIcon(Place place) { + iconView.setImageBitmap(null); + iconView.setBackgroundColor(place.getIconBackgroundColor()); + String url = place.getIconUrl(); + Glide.with(this).load(url).into(iconView); + } + + /** + * Fetches a Bitmap using the Places API and displays it. + * + * @param photoMetadata from a {@link Place} instance. + */ + private void fetchPhoto(PhotoMetadata photoMetadata) { + photo = photoMetadata; + + photoView.setImageBitmap(null); + setLoading(true); + + String customPhotoReference = getCustomPhotoReference(); + if (!TextUtils.isEmpty(customPhotoReference)) { + photoMetadata = PhotoMetadata.builder(customPhotoReference).build(); + } + + FetchPhotoRequest.Builder photoRequestBuilder = FetchPhotoRequest.builder(photoMetadata); + + Integer maxWidth = readIntFromTextView(R.id.photo_max_width); + if (maxWidth != null) { + photoRequestBuilder.setMaxWidth(maxWidth); + } + + Integer maxHeight = readIntFromTextView(R.id.photo_max_height); + if (maxHeight != null) { + photoRequestBuilder.setMaxHeight(maxHeight); + } + + Task photoTask = placesClient.fetchPhoto(photoRequestBuilder.build()); - FetchPlaceRequest request = FetchPlaceRequest.newInstance(getPlaceId(), placeFields); - Task placeTask = placesClient.fetchPlace(request); + photoTask.addOnSuccessListener( + response -> { + Bitmap bitmap = response.getBitmap(); + photoView.setImageBitmap(bitmap); + StringUtil.prepend(responseView, StringUtil.stringify(bitmap)); + }); - placeTask.addOnSuccessListener( - (response) -> { - responseView.setText(StringUtil.stringify(response, isDisplayRawResultsChecked())); - if (isFetchPhotoChecked) { - attemptFetchPhoto(response.getPlace()); - } - }); + photoTask.addOnFailureListener( + exception -> { + exception.printStackTrace(); + StringUtil.prepend(responseView, "Photo: " + exception.getMessage()); + }); - placeTask.addOnFailureListener( - (exception) -> { - exception.printStackTrace(); - responseView.setText(exception.getMessage()); - }); + photoTask.addOnCompleteListener(response -> setLoading(false)); + } - placeTask.addOnCompleteListener(response -> setLoading(false)); - } + ////////////////////////// + // Helper methods below // + ////////////////////////// - private void attemptFetchPhoto(Place place) { - List photoMetadatas = place.getPhotoMetadatas(); - if (photoMetadatas != null && !photoMetadatas.isEmpty()) { - fetchPhoto(photoMetadatas.get(0)); + private void dismissKeyboard(EditText focusedEditText) { + InputMethodManager imm = (InputMethodManager) getSystemService( + Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(focusedEditText.getWindowToken(), 0); } - } - - /** - * Fetches a Bitmap using the Places API and displays it. - * - * @param photoMetadata from a {@link Place} instance. - */ - private void fetchPhoto(PhotoMetadata photoMetadata) { - photo = photoMetadata; - - photoView.setImageBitmap(null); - setLoading(true); - - String customPhotoReference = getCustomPhotoReference(); - if (!TextUtils.isEmpty(customPhotoReference)) { - photoMetadata = PhotoMetadata.builder(customPhotoReference).build(); + + private boolean validateInputs(boolean isFetchPhotoChecked, boolean isFetchIconChecked, + List placeFields, String customPhotoReference) { + if (isFetchPhotoChecked) { + if (!placeFields.contains(Field.PHOTO_METADATAS)) { + responseView.setText( + "'Also fetch photo?' is selected, but PHOTO_METADATAS Place Field is not."); + return false; + } + } else if (!TextUtils.isEmpty(customPhotoReference)) { + responseView.setText( + "Using 'Custom photo reference', but 'Also fetch photo?' is not selected."); + return false; + } + if (isFetchIconChecked && !placeFields.contains(Field.ICON_URL)) { + responseView.setText(R.string.fetch_icon_missing_fields_warning); + return false; + } + + return true; } - FetchPhotoRequest.Builder photoRequestBuilder = FetchPhotoRequest.builder(photoMetadata); + private String getPlaceId() { + return ((TextView) findViewById(R.id.place_id_field)).getText().toString(); + } - Integer maxWidth = readIntFromTextView(R.id.photo_max_width); - if (maxWidth != null) { - photoRequestBuilder.setMaxWidth(maxWidth); + private List getPlaceFields() { + if (((CheckBox) findViewById(R.id.use_custom_fields)).isChecked()) { + return fieldSelector.getSelectedFields(); + } else { + return fieldSelector.getAllFields(); + } } - Integer maxHeight = readIntFromTextView(R.id.photo_max_height); - if (maxHeight != null) { - photoRequestBuilder.setMaxHeight(maxHeight); + private boolean isDisplayRawResultsChecked() { + return ((CheckBox) findViewById(R.id.display_raw_results)).isChecked(); } - Task photoTask = placesClient.fetchPhoto(photoRequestBuilder.build()); - - photoTask.addOnSuccessListener( - response -> { - Bitmap bitmap = response.getBitmap(); - photoView.setImageBitmap(bitmap); - StringUtil.prepend(responseView, StringUtil.stringify(bitmap)); - }); - - photoTask.addOnFailureListener( - exception -> { - exception.printStackTrace(); - StringUtil.prepend(responseView, "Photo: " + exception.getMessage()); - }); - - photoTask.addOnCompleteListener(response -> setLoading(false)); - } - - ////////////////////////// - // Helper methods below // - ////////////////////////// - - private void dismissKeyboard(EditText focusedEditText) { - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(focusedEditText.getWindowToken(), 0); - } - - private boolean validateInputs( - boolean isFetchPhotoChecked, List placeFields, String customPhotoReference) { - if (isFetchPhotoChecked) { - if (!placeFields.contains(Field.PHOTO_METADATAS)) { - responseView.setText( - "'Also fetch photo?' is selected, but PHOTO_METADATAS Place Field is not."); - return false; - } - } else if (!TextUtils.isEmpty(customPhotoReference)) { - responseView.setText( - "Using 'Custom photo reference', but 'Also fetch photo?' is not selected."); - return false; + private boolean isFetchPhotoChecked() { + return ((CheckBox) findViewById(R.id.fetch_photo_checkbox)).isChecked(); } - return true; - } + private boolean isFetchIconChecked() { + return ((CheckBox) findViewById(R.id.fetch_icon_checkbox)).isChecked(); + } - private String getPlaceId() { - return ((TextView) findViewById(R.id.place_id_field)).getText().toString(); - } + private String getCustomPhotoReference() { + return ((TextView) findViewById(R.id.custom_photo_reference)).getText().toString(); + } - private List getPlaceFields() { - if (((CheckBox) findViewById(R.id.use_custom_fields)).isChecked()) { - return fieldSelector.getSelectedFields(); - } else { - return fieldSelector.getAllFields(); + private void setPhotoSizingEnabled(boolean enabled) { + setEnabled(R.id.photo_max_width, enabled); + setEnabled(R.id.photo_max_height, enabled); } - } - - private boolean isDisplayRawResultsChecked() { - return ((CheckBox) findViewById(R.id.display_raw_results)).isChecked(); - } - - private boolean isFetchPhotoChecked() { - return ((CheckBox) findViewById(R.id.fetch_photo_checkbox)).isChecked(); - } - - private String getCustomPhotoReference() { - return ((TextView) findViewById(R.id.custom_photo_reference)).getText().toString(); - } - - private void setPhotoSizingEnabled(boolean enabled) { - setEnabled(R.id.photo_max_width, enabled); - setEnabled(R.id.photo_max_height, enabled); - } - - private void setCustomPhotoReferenceEnabled(boolean enabled) { - setEnabled(R.id.custom_photo_reference, enabled); - } - - private void setEnabled(@IdRes int resId, boolean enabled) { - TextView view = findViewById(resId); - view.setEnabled(enabled); - view.setText(""); - } - - @Nullable - private Integer readIntFromTextView(@IdRes int resId) { - Integer intValue = null; - View view = findViewById(resId); - - if (view instanceof TextView) { - CharSequence contents = ((TextView) view).getText(); - if (!TextUtils.isEmpty(contents)) { - try { - intValue = Integer.parseInt(contents.toString()); - } catch (NumberFormatException e) { - showErrorAlert(R.string.error_alert_message_invalid_photo_size); - } - } + + private void setCustomPhotoReferenceEnabled(boolean enabled) { + setEnabled(R.id.custom_photo_reference, enabled); + } + + private void setEnabled(@IdRes int resId, boolean enabled) { + TextView view = findViewById(resId); + view.setEnabled(enabled); + view.setText(""); } - return intValue; - } + @Nullable + private Integer readIntFromTextView(@IdRes int resId) { + Integer intValue = null; + View view = findViewById(resId); + + if (view instanceof TextView) { + CharSequence contents = ((TextView) view).getText(); + if (!TextUtils.isEmpty(contents)) { + try { + intValue = Integer.parseInt(contents.toString()); + } catch (NumberFormatException e) { + showErrorAlert(R.string.error_alert_message_invalid_photo_size); + } + } + } - private void showErrorAlert(@StringRes int messageResId) { - new AlertDialog.Builder(this) - .setTitle(R.string.error_alert_title) - .setMessage(messageResId) - .show(); - } + return intValue; + } - private void setLoading(boolean loading) { - findViewById(R.id.loading).setVisibility(loading ? View.VISIBLE : View.INVISIBLE); - } + private void showErrorAlert(@StringRes int messageResId) { + new AlertDialog.Builder(this) + .setTitle(R.string.error_alert_title) + .setMessage(messageResId) + .show(); + } + + private void setLoading(boolean loading) { + findViewById(R.id.loading).setVisibility(loading ? View.VISIBLE : View.INVISIBLE); + } } diff --git a/demo-java/app/src/main/res/layout/place_and_photo_test_activity.xml b/demo-java/app/src/main/res/layout/place_and_photo_test_activity.xml index 1872a0aa..a3dc4b4a 100644 --- a/demo-java/app/src/main/res/layout/place_and_photo_test_activity.xml +++ b/demo-java/app/src/main/res/layout/place_and_photo_test_activity.xml @@ -17,6 +17,7 @@ + android:text="@string/place_id_SFO"/> + + + + + + + + Find Current Place + Icon View + Also fetch icon? + \'Also fetch icon?\' is selected, but ICON_URL Place Field is not. + Image view to display a Place Icon Image + Icon with background: + Place photo: + + ChIJVVVVVYx3j4ARP-3NGldc8qQ + Search Search a Place This activity demonstrates as-you-type programmatic place predictions. Tap on the search icon on the toolbar and search for a place. diff --git a/demo-java/gradle/wrapper/gradle-wrapper.properties b/demo-java/gradle/wrapper/gradle-wrapper.properties index e06a74cc..7ca52308 100644 --- a/demo-java/gradle/wrapper/gradle-wrapper.properties +++ b/demo-java/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Tue Apr 07 10:21:51 PDT 2020 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/demo-kotlin/app/build.gradle b/demo-kotlin/app/build.gradle index 4d9c1084..99109a9e 100644 --- a/demo-kotlin/app/build.gradle +++ b/demo-kotlin/app/build.gradle @@ -36,6 +36,7 @@ android { } } +// Deprecated task generateV3(type: Copy) { group "V3 Beta" description "Copies source code from GMS to V3 BETA." @@ -55,9 +56,10 @@ dependencies { implementation 'com.android.support:multidex:1.0.3' implementation 'com.google.android.material:material:1.4.0' implementation 'com.android.volley:volley:1.2.1' + implementation "com.github.bumptech.glide:glide:4.9.0" // GMS - gmsImplementation 'com.google.android.libraries.places:places:2.4.0' + gmsImplementation 'com.google.android.libraries.places:places:2.5.0' // V3 v3Implementation name:'places-maps-sdk-3.1.0-beta', ext:'aar' diff --git a/demo-kotlin/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.kt b/demo-kotlin/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.kt index e4f19f6b..3c359c94 100644 --- a/demo-kotlin/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.kt +++ b/demo-kotlin/app/src/gms/java/com/example/placesdemo/PlaceAndPhotoTestActivity.kt @@ -28,6 +28,7 @@ import androidx.annotation.IdRes import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity +import com.bumptech.glide.Glide import com.google.android.libraries.places.api.Places import com.google.android.libraries.places.api.model.PhotoMetadata import com.google.android.libraries.places.api.model.Place @@ -43,6 +44,7 @@ import com.google.android.libraries.places.api.net.PlacesClient class PlaceAndPhotoTestActivity : AppCompatActivity() { private lateinit var placesClient: PlacesClient private lateinit var photoView: ImageView + private lateinit var iconView: ImageView private lateinit var responseView: TextView private lateinit var fieldSelector: FieldSelector @@ -68,10 +70,19 @@ class PlaceAndPhotoTestActivity : AppCompatActivity() { // Set up view objects responseView = findViewById(R.id.response) photoView = findViewById(R.id.photo) + iconView = findViewById(R.id.icon) val fetchPhotoCheckbox = findViewById(R.id.fetch_photo_checkbox) - fetchPhotoCheckbox.setOnCheckedChangeListener { _, isChecked: Boolean -> setPhotoSizingEnabled(isChecked) } + fetchPhotoCheckbox.setOnCheckedChangeListener { _, isChecked: Boolean -> + setPhotoSizingEnabled( + isChecked + ) + } val customPhotoCheckbox = findViewById(R.id.use_custom_photo_reference) - customPhotoCheckbox.setOnCheckedChangeListener { _, isChecked: Boolean -> setCustomPhotoReferenceEnabled(isChecked) } + customPhotoCheckbox.setOnCheckedChangeListener { _, isChecked: Boolean -> + setCustomPhotoReferenceEnabled( + isChecked + ) + } fieldSelector = FieldSelector( findViewById(R.id.use_custom_fields), findViewById(R.id.custom_fields_list), @@ -102,11 +113,20 @@ class PlaceAndPhotoTestActivity : AppCompatActivity() { private fun fetchPlace() { responseView.text = null photoView.setImageBitmap(null) + iconView.setImageBitmap(null) + dismissKeyboard(findViewById(R.id.place_id_field)) val isFetchPhotoChecked = isFetchPhotoChecked + val isFetchIconChecked = isFetchIconChecked val placeFields = placeFields val customPhotoReference = customPhotoReference - if (!validateInputs(isFetchPhotoChecked, placeFields, customPhotoReference)) { + if (!validateInputs( + isFetchPhotoChecked, + isFetchIconChecked, + placeFields, + customPhotoReference + ) + ) { return } setLoading(true) @@ -117,6 +137,9 @@ class PlaceAndPhotoTestActivity : AppCompatActivity() { if (isFetchPhotoChecked) { attemptFetchPhoto(response.place) } + if (isFetchIconChecked) { + attemptFetchIcon(response.place) + } } placeTask.addOnFailureListener { exception: Exception -> exception.printStackTrace() @@ -132,6 +155,13 @@ class PlaceAndPhotoTestActivity : AppCompatActivity() { } } + private fun attemptFetchIcon(place: Place) { + iconView.setImageBitmap(null) + iconView.setBackgroundColor(place.iconBackgroundColor) + val url = place.iconUrl + Glide.with(this).load(url).into(iconView) + } + /** * Fetches a Bitmap using the Places API and displays it. * @@ -177,14 +207,24 @@ class PlaceAndPhotoTestActivity : AppCompatActivity() { } private fun validateInputs( - isFetchPhotoChecked: Boolean, placeFields: List, customPhotoReference: String): Boolean { + isFetchPhotoChecked: Boolean, + isFetchIconChecked: Boolean, + placeFields: List, + customPhotoReference: String + ): Boolean { if (isFetchPhotoChecked) { if (!placeFields.contains(Place.Field.PHOTO_METADATAS)) { - responseView.text = "'Also fetch photo?' is selected, but PHOTO_METADATAS Place Field is not." + responseView.text = + "'Also fetch photo?' is selected, but PHOTO_METADATAS Place Field is not." return false } } else if (!TextUtils.isEmpty(customPhotoReference)) { - responseView.text = "Using 'Custom photo reference', but 'Also fetch photo?' is not selected." + responseView.text = + "Using 'Custom photo reference', but 'Also fetch photo?' is not selected." + return false + } + if (isFetchIconChecked && !placeFields.contains(Field.ICON_URL)) { + responseView.setText(R.string.fetch_icon_missing_fields_warning) return false } return true @@ -206,6 +246,9 @@ class PlaceAndPhotoTestActivity : AppCompatActivity() { private val isFetchPhotoChecked: Boolean get() = findViewById(R.id.fetch_photo_checkbox).isChecked + private val isFetchIconChecked: Boolean + get() = findViewById(R.id.fetch_icon_checkbox).isChecked + private val customPhotoReference: String get() = findViewById(R.id.custom_photo_reference).text.toString() diff --git a/demo-kotlin/app/src/main/res/layout/place_and_photo_test_activity.xml b/demo-kotlin/app/src/main/res/layout/place_and_photo_test_activity.xml index 1872a0aa..a3dc4b4a 100644 --- a/demo-kotlin/app/src/main/res/layout/place_and_photo_test_activity.xml +++ b/demo-kotlin/app/src/main/res/layout/place_and_photo_test_activity.xml @@ -17,6 +17,7 @@ + android:text="@string/place_id_SFO"/> + + + + + + + + Find Current Place + Icon View + Also fetch icon? + \'Also fetch icon?\' is selected, but ICON_URL Place Field is not. + Image view to display a Place Icon Image + Icon with background: + Place photo: + + ChIJVVVVVYx3j4ARP-3NGldc8qQ + Programmatic Autocomplete + Geocoding Search Search a Place diff --git a/demo-kotlin/gradle/wrapper/gradle-wrapper.properties b/demo-kotlin/gradle/wrapper/gradle-wrapper.properties index d034183a..d3e0ee37 100644 --- a/demo-kotlin/gradle/wrapper/gradle-wrapper.properties +++ b/demo-kotlin/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip diff --git a/snippets/app/build.gradle b/snippets/app/build.gradle index c6b8b417..2ff50470 100644 --- a/snippets/app/build.gradle +++ b/snippets/app/build.gradle @@ -37,10 +37,11 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.1' implementation 'com.android.volley:volley:1.2.1' + implementation "com.github.bumptech.glide:glide:4.9.0" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // [END_EXCLUDE] - implementation 'com.google.android.libraries.places:places:2.4.0' + implementation 'com.google.android.libraries.places:places:2.5.0' } // [END maps_android_places_install_snippet] diff --git a/snippets/app/src/main/java/com/google/places/PlacesIconActivity.java b/snippets/app/src/main/java/com/google/places/PlacesIconActivity.java new file mode 100644 index 00000000..c8152107 --- /dev/null +++ b/snippets/app/src/main/java/com/google/places/PlacesIconActivity.java @@ -0,0 +1,25 @@ +package com.google.places; + +import android.widget.ImageView; + +import androidx.appcompat.app.AppCompatActivity; + +import com.bumptech.glide.Glide; +import com.google.android.libraries.places.api.model.Place; + +class PlacesIconActivity extends AppCompatActivity { + + private ImageView imageView; + + private void getPlacesIcon(Place place) { + // [START maps_places_places_icon_and_bg_color] + // Set the image view's background color to match the place's icon background color + imageView.setBackgroundColor(place.getIconBackgroundColor()); + + // Fetch the icon using Glide and set the result in the image view + Glide.with(this) + .load(place.getIconUrl()) + .into(imageView); + // [END maps_places_places_icon_and_bg_color] + } +} diff --git a/snippets/app/src/main/java/com/google/places/kotlin/PlacesIconActivity.kt b/snippets/app/src/main/java/com/google/places/kotlin/PlacesIconActivity.kt new file mode 100644 index 00000000..03cafc32 --- /dev/null +++ b/snippets/app/src/main/java/com/google/places/kotlin/PlacesIconActivity.kt @@ -0,0 +1,21 @@ +package com.google.places.kotlin + +import android.widget.ImageView +import androidx.appcompat.app.AppCompatActivity +import com.google.android.libraries.places.api.model.Place +import com.bumptech.glide.Glide + +class PlacesIconActivity : AppCompatActivity() { + private val imageView: ImageView! + private fun getPlacesIcon(place: Place) { + // [START maps_places_places_icon_and_bg_color] + // Set the image view's background color to match the place's icon background color + imageView.setBackgroundColor(place.iconBackgroundColor) + + // Fetch the icon using Glide and set the result in the image view + Glide.with(this) + .load(place.iconUrl) + .into(imageView) + // [END maps_places_places_icon_and_bg_color] + } +} \ No newline at end of file diff --git a/snippets/gradle/wrapper/gradle-wrapper.properties b/snippets/gradle/wrapper/gradle-wrapper.properties index f585ee46..6fed96bd 100644 --- a/snippets/gradle/wrapper/gradle-wrapper.properties +++ b/snippets/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip