Skip to content

Commit

Permalink
feat: bump places to 2.7.0 (#442)
Browse files Browse the repository at this point in the history
* feat: update demos to v2.7.0

Add examples of setTypesFilter to replace deprecated setTypeFilter

* fix: restore deprecated TypeFilter demo for deprecated v3 version of app
  • Loading branch information
wangela authored Nov 2, 2022
1 parent 2315bdc commit 6e5cb15
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 69 deletions.
2 changes: 1 addition & 1 deletion demo-java/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dependencies {
implementation "com.github.bumptech.glide:glide:4.13.2"

// GMS
gmsImplementation 'com.google.android.libraries.places:places:2.6.0'
gmsImplementation 'com.google.android.libraries.places:places:2.7.0'
gmsImplementation 'com.google.android.gms:play-services-maps:18.0.2'
gmsImplementation 'com.google.maps.android:android-maps-utils:2.3.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@

package com.example.placesdemo;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
Expand All @@ -26,7 +40,6 @@
import com.google.android.libraries.places.api.model.LocationRestriction;
import com.google.android.libraries.places.api.model.Place;
import com.google.android.libraries.places.api.model.RectangularBounds;
import com.google.android.libraries.places.api.model.TypeFilter;
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest;
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse;
import com.google.android.libraries.places.api.net.PlacesClient;
Expand All @@ -36,25 +49,10 @@
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener;
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

/**
* Activity for testing Autocomplete (activity and fragment widgets, and programmatic).
*/
Expand All @@ -64,6 +62,7 @@ public class AutocompleteTestActivity extends AppCompatActivity {
private PlacesClient placesClient;
private TextView responseView;
private FieldSelector fieldSelector;
private EditText typesFilterEditText;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -83,13 +82,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

// Set up view objects
responseView = findViewById(R.id.response);
Spinner typeFilterSpinner = findViewById(R.id.autocomplete_type_filter);
typeFilterSpinner.setAdapter(
new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, Arrays.asList(TypeFilter.values())));
CheckBox useTypeFilterCheckBox = findViewById(R.id.autocomplete_use_type_filter);
useTypeFilterCheckBox.setOnCheckedChangeListener(
(buttonView, isChecked) -> typeFilterSpinner.setEnabled(isChecked));
typesFilterEditText = findViewById(R.id.autocomplete_types_filter_edittext);
CheckBox useTypesFilterCheckBox = findViewById(R.id.autocomplete_use_types_filter_checkbox);
useTypesFilterCheckBox.setOnCheckedChangeListener(
(buttonView, isChecked) -> typesFilterEditText.setEnabled(isChecked));
fieldSelector =
new FieldSelector(
findViewById(R.id.use_custom_fields),
Expand All @@ -108,7 +104,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

// UI initialization
setLoading(false);
typeFilterSpinner.setEnabled(false);
}

@Override
Expand All @@ -133,7 +128,7 @@ private void setupAutocompleteSupportFragment() {
.setCountries(getCountries())
.setLocationBias(getLocationBias())
.setLocationRestriction(getLocationRestriction())
.setTypeFilter(getTypeFilter())
.setTypesFilter(getTypesFilter())
.setActivityMode(getMode()));
}

Expand Down Expand Up @@ -184,7 +179,7 @@ private void startAutocompleteActivity() {
.setCountries(getCountries())
.setLocationBias(getLocationBias())
.setLocationRestriction(getLocationRestriction())
.setTypeFilter(getTypeFilter())
.setTypesFilter(getTypesFilter())
.build(this);
startActivityForResult(autocompleteIntent, AUTOCOMPLETE_REQUEST_CODE);
}
Expand All @@ -199,7 +194,7 @@ private void findAutocompletePredictions() {
.setOrigin((getOrigin()))
.setLocationBias(getLocationBias())
.setLocationRestriction(getLocationRestriction())
.setTypeFilter(getTypeFilter());
.setTypesFilter(getTypesFilter());

if (isUseSessionTokenChecked()) {
requestBuilder.setSessionToken(AutocompleteSessionToken.newInstance());
Expand Down Expand Up @@ -305,12 +300,14 @@ private LatLng getOrigin() {
return origin;
}

@Nullable
private TypeFilter getTypeFilter() {
Spinner typeFilter = findViewById(R.id.autocomplete_type_filter);
return typeFilter.isEnabled() ? (TypeFilter) typeFilter.getSelectedItem() : null;
private List<String> getTypesFilter() {
EditText typesFilterEditText = findViewById(R.id.autocomplete_types_filter_edittext);
return typesFilterEditText.isEnabled()
? Arrays.asList(typesFilterEditText.getText().toString().split("[\\s,]+"))
: new ArrayList<>();
}


private AutocompleteActivityMode getMode() {
boolean isOverlayMode =
((CheckBox) findViewById(R.id.autocomplete_activity_overlay_mode)).isChecked();
Expand Down
23 changes: 23 additions & 0 deletions demo-java/app/src/main/res/layout/autocomplete_test_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@
android:imeOptions="actionNext"
android:inputType="text"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<CheckBox
android:id="@+id/autocomplete_use_types_filter_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/autocomplete_use_types_filter"/>

<EditText
android:id="@+id/autocomplete_types_filter_edittext"
android:autofillHints=""
android:enabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/autocomplete_types_filter_hint"
android:inputType="text"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
22 changes: 18 additions & 4 deletions demo-java/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
<!-- Hint for the autocomplete prediction's and widget's countries field. -->
<string name="autocomplete_country_hint" translatable="false">Countries (e.g CH, US, RO)</string>

<!-- Hint Text for the autocomplete prediction's and widget's type filter field. -->
<string name="autocomplete_type_filter" translatable="false">Type Filter</string>

<!-- Label for the autocomplete prediction's and widget's location origin field. -->
<string name="autocomplete_location_origin_label" translatable="false">Location Origin (format: -33.2, 128.2)</string>

Expand All @@ -86,9 +83,26 @@
<!-- Hint for the autocomplete prediction's and widget's location northeast field. -->
<string name="autocomplete_location_north_east_hint" translatable="false">North East (lat, lng)</string>

<!-- Text for enabling autocomplete prediction's and widget's types filter field. -->
<string name="autocomplete_use_types_filter" translatable="false">Use TypesFilter?</string>

<!-- Hint for the autocomplete prediction's and widget's types filter field. -->
<string name="autocomplete_types_filter_hint" translatable="false">
<font size="16">
Comma separated list of up to 5 place types
</font>
</string>

<!-- KEEP UNTIL v3 REMOVED -->

<!-- Text for enabling autocomplete prediction's and widget's type filter field. -->
<string name="autocomplete_use_type_filter" translatable="false">Use TypeFilter?</string>
<string name="autocomplete_use_type_filter" translatable="false">Use TypeFilter? (Deprecated)</string>

<!-- Hint Text for the autocomplete prediction's and widget's type filter field. -->
<string name="autocomplete_type_filter" translatable="false">Type Filter</string>

<!-- END KEEP UNTIL v3 REMOVED -->


<!-- Text for enabling autocomplete prediction's use of session tokens. -->
<string name="autocomplete_use_session_token" translatable="false">(Predictions-only) Use session token?</string>
Expand Down
2 changes: 1 addition & 1 deletion demo-kotlin/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {
implementation "com.github.bumptech.glide:glide:4.13.2"

// GMS
gmsImplementation 'com.google.android.libraries.places:places:2.6.0'
gmsImplementation 'com.google.android.libraries.places:places:2.7.0'

// V3
v3Implementation name:'places-maps-sdk-3.1.0-beta', ext:'aar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ArrayAdapter
import android.widget.CheckBox
import android.widget.Spinner
import android.widget.TextView
import android.widget.*
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
Expand All @@ -36,7 +33,6 @@ import com.google.android.libraries.places.api.model.LocationBias
import com.google.android.libraries.places.api.model.LocationRestriction
import com.google.android.libraries.places.api.model.Place
import com.google.android.libraries.places.api.model.RectangularBounds
import com.google.android.libraries.places.api.model.TypeFilter
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse
import com.google.android.libraries.places.api.net.PlacesClient
Expand All @@ -55,6 +51,7 @@ class AutocompleteTestActivity : AppCompatActivity() {
private lateinit var placesClient: PlacesClient
private lateinit var responseView: TextView
private lateinit var fieldSelector: FieldSelector
private lateinit var typesFilterEditText: EditText

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -72,11 +69,9 @@ class AutocompleteTestActivity : AppCompatActivity() {

// Set up view objects
responseView = findViewById(R.id.response)
val typeFilterSpinner = findViewById<Spinner>(R.id.autocomplete_type_filter)
typeFilterSpinner.adapter = ArrayAdapter(
this, android.R.layout.simple_list_item_1, Arrays.asList(*TypeFilter.values()))
val useTypeFilterCheckBox = findViewById<CheckBox>(R.id.autocomplete_use_type_filter)
useTypeFilterCheckBox.setOnCheckedChangeListener { _, isChecked: Boolean -> typeFilterSpinner.isEnabled = isChecked }
typesFilterEditText = findViewById(R.id.autocomplete_types_filter_edittext)
val useTypesFilterCheckBox = findViewById<CheckBox>(R.id.autocomplete_use_types_filter_checkbox)
useTypesFilterCheckBox.setOnCheckedChangeListener { buttonView, isChecked: Boolean -> typesFilterEditText.isEnabled = isChecked }
fieldSelector = FieldSelector(
findViewById(R.id.use_custom_fields),
findViewById(R.id.custom_fields_list),
Expand All @@ -93,7 +88,6 @@ class AutocompleteTestActivity : AppCompatActivity() {

// UI initialization
setLoading(false)
typeFilterSpinner.isEnabled = false
}

override fun onSaveInstanceState(bundle: Bundle) {
Expand All @@ -114,7 +108,7 @@ class AutocompleteTestActivity : AppCompatActivity() {
.setCountries(countries)
.setLocationBias(locationBias)
.setLocationRestriction(locationRestriction)
.setTypeFilter(typeFilter)
.setTypesFilter(getTypesFilter())
.setActivityMode(mode)
}
}
Expand Down Expand Up @@ -163,7 +157,7 @@ class AutocompleteTestActivity : AppCompatActivity() {
.setCountries(countries)
.setLocationBias(locationBias)
.setLocationRestriction(locationRestriction)
.setTypeFilter(typeFilter)
.setTypesFilter(getTypesFilter())
.build(this)
startActivityForResult(autocompleteIntent, AUTOCOMPLETE_REQUEST_CODE)
}
Expand All @@ -176,7 +170,7 @@ class AutocompleteTestActivity : AppCompatActivity() {
.setOrigin(origin)
.setLocationBias(locationBias)
.setLocationRestriction(locationRestriction)
.setTypeFilter(typeFilter)
.setTypesFilter(getTypesFilter())
if (isUseSessionTokenChecked) {
requestBuilder.setSessionToken(AutocompleteSessionToken.newInstance())
}
Expand Down Expand Up @@ -257,11 +251,12 @@ class AutocompleteTestActivity : AppCompatActivity() {
return origin
}

private val typeFilter: TypeFilter?
get() {
val typeFilter = findViewById<Spinner>(R.id.autocomplete_type_filter)
return if (typeFilter.isEnabled) typeFilter.selectedItem as TypeFilter else null
}
private fun getTypesFilter(): List<String> {
typesFilterEditText = findViewById(R.id.autocomplete_types_filter_edittext)
return if (typesFilterEditText.isEnabled)
typesFilterEditText.text.toString().split("[\\s,]+".toRegex()) as List<String>
else emptyList<String>()
}

private val mode: AutocompleteActivityMode
get() {
Expand Down
23 changes: 23 additions & 0 deletions demo-kotlin/app/src/main/res/layout/autocomplete_test_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@
android:imeOptions="actionNext"
android:inputType="text"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<CheckBox
android:id="@+id/autocomplete_use_types_filter_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/autocomplete_use_types_filter"/>

<EditText
android:id="@+id/autocomplete_types_filter_edittext"
android:autofillHints=""
android:enabled="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/autocomplete_types_filter_hint"
android:inputType="text"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
22 changes: 18 additions & 4 deletions demo-kotlin/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,27 @@
<!-- Hint for the autocomplete prediction's and widget's countries field. -->
<string name="autocomplete_country_hint" translatable="false">Countries (e.g CH, US, RO)</string>

<!-- Text for enabling autocomplete prediction's and widget's types filter field. -->
<string name="autocomplete_use_types_filter" translatable="false">Use TypesFilter?</string>

<!-- Hint for the autocomplete prediction's and widget's types filter field. -->
<string name="autocomplete_types_filter_hint" translatable="false">
<font size="16">
Comma separated list of up to 5 place types
</font>
</string>


<!-- KEEP UNTIL v3 REMOVED -->

<!-- Text for enabling autocomplete prediction's and widget's type filter field. -->
<string name="autocomplete_use_type_filter" translatable="false">Use TypeFilter? (Deprecated)</string>

<!-- Hint Text for the autocomplete prediction's and widget's type filter field. -->
<string name="autocomplete_type_filter" translatable="false">Type Filter</string>

<!-- END KEEP UNTIL v3 REMOVED -->

<!-- Label for the autocomplete prediction's and widget's location origin field. -->
<string name="autocomplete_location_origin_label" translatable="false">Location Origin (format: -33.2, 128.2)</string>

Expand All @@ -80,10 +98,6 @@
<!-- Hint for the autocomplete prediction's and widget's location northeast field. -->
<string name="autocomplete_location_north_east_hint" translatable="false">North East (lat, lng)</string>


<!-- Text for enabling autocomplete prediction's and widget's type filter field. -->
<string name="autocomplete_use_type_filter" translatable="false">Use TypeFilter?</string>

<!-- Text for enabling autocomplete prediction's use of session tokens. -->
<string name="autocomplete_use_session_token" translatable="false">(Predictions-only) Use session token?</string>

Expand Down
2 changes: 0 additions & 2 deletions demo-kotlin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@ org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true

PLACES_API_KEY="YOUR_API_KEY"
Loading

0 comments on commit 6e5cb15

Please sign in to comment.