From 599adb1c0cec8551f0bfe6dc935b8f6f91389233 Mon Sep 17 00:00:00 2001 From: Angela Yu <5506675+wangela@users.noreply.github.com> Date: Fri, 28 Apr 2023 13:27:31 -0700 Subject: [PATCH] chore: add snippets for isOpen (#523) * feat: add Kotlin snippets for isOpen * fix: stragglers in snippets: PlaceTypes and string interpolation --- .../google/places/CurrentPlaceActivity.java | 14 +++ .../places/PlaceAutocompleteActivity.java | 2 +- .../google/places/PlaceDetailsActivity.java | 14 +++ .../google/places/PlaceIsOpenActivity.java | 118 ++++++++++++++++++ .../google/places/PlacePhotosActivity.java | 14 +++ .../com/google/places/PlacesIconActivity.java | 14 +++ .../places/kotlin/CurrentPlaceActivity.kt | 14 +++ .../kotlin/PlaceAutocompleteActivity.kt | 10 +- .../places/kotlin/PlaceDetailsActivity.kt | 14 +++ .../places/kotlin/PlacePhotosActivity.kt | 14 +++ .../places/kotlin/PlacesIconActivity.kt | 14 +++ .../places/kotlin/PlacesIsOpenActivity.kt | 108 ++++++++++++++++ 12 files changed, 344 insertions(+), 6 deletions(-) create mode 100644 snippets/app/src/main/java/com/google/places/PlaceIsOpenActivity.java create mode 100644 snippets/app/src/main/java/com/google/places/kotlin/PlacesIsOpenActivity.kt diff --git a/snippets/app/src/main/java/com/google/places/CurrentPlaceActivity.java b/snippets/app/src/main/java/com/google/places/CurrentPlaceActivity.java index 4fefda4d..fa132e54 100644 --- a/snippets/app/src/main/java/com/google/places/CurrentPlaceActivity.java +++ b/snippets/app/src/main/java/com/google/places/CurrentPlaceActivity.java @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places; import android.content.pm.PackageManager; diff --git a/snippets/app/src/main/java/com/google/places/PlaceAutocompleteActivity.java b/snippets/app/src/main/java/com/google/places/PlaceAutocompleteActivity.java index 2a20a543..747cbe7c 100644 --- a/snippets/app/src/main/java/com/google/places/PlaceAutocompleteActivity.java +++ b/snippets/app/src/main/java/com/google/places/PlaceAutocompleteActivity.java @@ -149,7 +149,7 @@ private void startAutocompleteIntent() { Intent intent = result.getData(); if (intent != null) { Place place = Autocomplete.getPlaceFromIntent(intent); - Log.i(TAG, "Place: {$place.getName()}, ${place.getId()}"); + Log.i(TAG, "Place: ${place.getName()}, ${place.getId()}"); } } else if (result.getResultCode() == Activity.RESULT_CANCELED) { // The user canceled the operation. diff --git a/snippets/app/src/main/java/com/google/places/PlaceDetailsActivity.java b/snippets/app/src/main/java/com/google/places/PlaceDetailsActivity.java index df1ac2b5..e01a895d 100644 --- a/snippets/app/src/main/java/com/google/places/PlaceDetailsActivity.java +++ b/snippets/app/src/main/java/com/google/places/PlaceDetailsActivity.java @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places; import android.util.Log; diff --git a/snippets/app/src/main/java/com/google/places/PlaceIsOpenActivity.java b/snippets/app/src/main/java/com/google/places/PlaceIsOpenActivity.java new file mode 100644 index 00000000..824aba02 --- /dev/null +++ b/snippets/app/src/main/java/com/google/places/PlaceIsOpenActivity.java @@ -0,0 +1,118 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.places; + +import android.annotation.SuppressLint; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; + +import com.google.android.gms.tasks.Task; +import com.google.android.libraries.places.api.model.Place; +import com.google.android.libraries.places.api.net.FetchPlaceRequest; +import com.google.android.libraries.places.api.net.FetchPlaceResponse; +import com.google.android.libraries.places.api.net.IsOpenRequest; +import com.google.android.libraries.places.api.net.IsOpenResponse; +import com.google.android.libraries.places.api.net.PlacesClient; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.List; + +public class PlaceIsOpenActivity extends AppCompatActivity { + private PlacesClient placesClient; + private Boolean isOpen; + + /** + * Check if the place is open at the time specified in the input fields. + * Requires a Place object that includes Place.Field.ID + */ + @SuppressLint("SetTextI18n") + private void isOpenByPlaceObject() { + // [START maps_places_place_is_open] + @NonNull + Calendar isOpenCalendar = Calendar.getInstance(); + String placeId = "ChIJD3uTd9hx5kcR1IQvGfr8dbk"; + // Specify the required fields for an isOpen request. + List placeFields = new ArrayList<>(Arrays.asList( + Place.Field.BUSINESS_STATUS, + Place.Field.CURRENT_OPENING_HOURS, + Place.Field.ID, + Place.Field.OPENING_HOURS, + Place.Field.UTC_OFFSET + )); + + FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields); + Task placeTask = placesClient.fetchPlace(request); + + placeTask.addOnSuccessListener( + (placeResponse) -> { + Place place = placeResponse.getPlace(); + IsOpenRequest isOpenRequest; + + try { + isOpenRequest = IsOpenRequest.newInstance(place, isOpenCalendar.getTimeInMillis()); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + return; + } + Task isOpenTask = placesClient.isOpen(isOpenRequest); + + isOpenTask.addOnSuccessListener( + (isOpenResponse) -> isOpen = isOpenResponse.isOpen()); + // [START_EXCLUDE] + placeTask.addOnFailureListener( + Throwable::printStackTrace); + // [END_EXCLUDE] + }); + // [START_EXCLUDE] + placeTask.addOnFailureListener( + Throwable::printStackTrace); + // [END_EXCLUDE] + // [END maps_places_place_is_open] + } + + /** + * Check if the place is open at the time specified in the input fields. + * Use the Place ID in the input field for the isOpenRequest. + */ + @SuppressLint("SetTextI18n") + private void isOpenByPlaceId() { + // [START maps_places_id_is_open] + @NonNull + Calendar isOpenCalendar = Calendar.getInstance(); + String placeId = "ChIJD3uTd9hx5kcR1IQvGfr8dbk"; + IsOpenRequest isOpenRequest; + + try { + isOpenRequest = IsOpenRequest.newInstance(placeId, isOpenCalendar.getTimeInMillis()); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + return; + } + + Task placeTask = placesClient.isOpen(isOpenRequest); + + placeTask.addOnSuccessListener( + (response) -> + isOpen = response.isOpen()); + // [START_EXCLUDE] + placeTask.addOnFailureListener( + Throwable::printStackTrace); + // [END_EXCLUDE] + // [END maps_places_id_is_open] + } +} diff --git a/snippets/app/src/main/java/com/google/places/PlacePhotosActivity.java b/snippets/app/src/main/java/com/google/places/PlacePhotosActivity.java index 23a3880f..1448400c 100644 --- a/snippets/app/src/main/java/com/google/places/PlacePhotosActivity.java +++ b/snippets/app/src/main/java/com/google/places/PlacePhotosActivity.java @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places; import android.graphics.Bitmap; diff --git a/snippets/app/src/main/java/com/google/places/PlacesIconActivity.java b/snippets/app/src/main/java/com/google/places/PlacesIconActivity.java index c8152107..47b94d40 100644 --- a/snippets/app/src/main/java/com/google/places/PlacesIconActivity.java +++ b/snippets/app/src/main/java/com/google/places/PlacesIconActivity.java @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places; import android.widget.ImageView; diff --git a/snippets/app/src/main/java/com/google/places/kotlin/CurrentPlaceActivity.kt b/snippets/app/src/main/java/com/google/places/kotlin/CurrentPlaceActivity.kt index 70504bae..2de55bc1 100644 --- a/snippets/app/src/main/java/com/google/places/kotlin/CurrentPlaceActivity.kt +++ b/snippets/app/src/main/java/com/google/places/kotlin/CurrentPlaceActivity.kt @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places.kotlin import android.Manifest.permission diff --git a/snippets/app/src/main/java/com/google/places/kotlin/PlaceAutocompleteActivity.kt b/snippets/app/src/main/java/com/google/places/kotlin/PlaceAutocompleteActivity.kt index 7ed6fb52..fca22355 100644 --- a/snippets/app/src/main/java/com/google/places/kotlin/PlaceAutocompleteActivity.kt +++ b/snippets/app/src/main/java/com/google/places/kotlin/PlaceAutocompleteActivity.kt @@ -26,8 +26,8 @@ import com.google.android.gms.maps.model.LatLng import com.google.android.libraries.places.api.Places import com.google.android.libraries.places.api.model.AutocompleteSessionToken import com.google.android.libraries.places.api.model.Place +import com.google.android.libraries.places.api.model.PlaceTypes 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 @@ -97,7 +97,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() { // [END maps_places_autocomplete_location_restriction] // [START maps_places_autocomplete_type_filter] - autocompleteFragment.setTypesFilter(listOf(TypeFilter.ADDRESS.toString())) + autocompleteFragment.setTypesFilter(listOf(PlaceTypes.ADDRESS)) // [END maps_places_autocomplete_type_filter] // [START maps_places_autocomplete_type_filter_multiple] @@ -107,7 +107,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() { val fields: List = ArrayList() // [START maps_places_intent_type_filter] val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields) - .setTypesFilter(listOf(TypeFilter.ADDRESS.toString())) + .setTypesFilter(listOf(PlaceTypes.ADDRESS)) .build(this) // [END maps_places_intent_type_filter] @@ -140,7 +140,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() { if (intent != null) { val place = Autocomplete.getPlaceFromIntent(intent) Log.i( - TAG, "Place: {\$place.getName()}, \${place.getId()}" + TAG, "Place: ${place.name}, ${place.id}" ) } } else if (result.resultCode == Activity.RESULT_CANCELED) { @@ -169,7 +169,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() { //.setLocationRestriction(bounds) .setOrigin(LatLng(-33.8749937, 151.2041382)) .setCountries("AU", "NZ") - .setTypesFilter(listOf(TypeFilter.ADDRESS.toString())) + .setTypesFilter(listOf(PlaceTypes.ADDRESS)) .setSessionToken(token) .setQuery(query) .build() diff --git a/snippets/app/src/main/java/com/google/places/kotlin/PlaceDetailsActivity.kt b/snippets/app/src/main/java/com/google/places/kotlin/PlaceDetailsActivity.kt index 965d4194..d4949490 100644 --- a/snippets/app/src/main/java/com/google/places/kotlin/PlaceDetailsActivity.kt +++ b/snippets/app/src/main/java/com/google/places/kotlin/PlaceDetailsActivity.kt @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places.kotlin import android.util.Log diff --git a/snippets/app/src/main/java/com/google/places/kotlin/PlacePhotosActivity.kt b/snippets/app/src/main/java/com/google/places/kotlin/PlacePhotosActivity.kt index 89bcf628..2950cd8b 100644 --- a/snippets/app/src/main/java/com/google/places/kotlin/PlacePhotosActivity.kt +++ b/snippets/app/src/main/java/com/google/places/kotlin/PlacePhotosActivity.kt @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places.kotlin import android.util.Log 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 index 51e3244d..b25a360f 100644 --- a/snippets/app/src/main/java/com/google/places/kotlin/PlacesIconActivity.kt +++ b/snippets/app/src/main/java/com/google/places/kotlin/PlacesIconActivity.kt @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package com.google.places.kotlin import android.widget.ImageView diff --git a/snippets/app/src/main/java/com/google/places/kotlin/PlacesIsOpenActivity.kt b/snippets/app/src/main/java/com/google/places/kotlin/PlacesIsOpenActivity.kt new file mode 100644 index 00000000..edb5cd18 --- /dev/null +++ b/snippets/app/src/main/java/com/google/places/kotlin/PlacesIsOpenActivity.kt @@ -0,0 +1,108 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.places.kotlin + +import android.annotation.SuppressLint +import androidx.appcompat.app.AppCompatActivity +import com.google.android.gms.tasks.Task +import com.google.android.libraries.places.api.model.Place +import com.google.android.libraries.places.api.net.FetchPlaceRequest +import com.google.android.libraries.places.api.net.FetchPlaceResponse +import com.google.android.libraries.places.api.net.IsOpenRequest +import com.google.android.libraries.places.api.net.IsOpenResponse +import com.google.android.libraries.places.api.net.PlacesClient +import java.util.Calendar + +class PlacesIsOpenActivity : AppCompatActivity() { + private lateinit var placesClient: PlacesClient + + /** + * Check if the place is open at the time specified in the input fields. + * Requires a Place object that includes Place.Field.ID + */ + @SuppressLint("SetTextI18n") + private fun isOpenByPlaceObject() { + // [START maps_places_place_is_open] + val isOpenCalendar: Calendar = Calendar.getInstance() + var place: Place + val placeId = "ChIJD3uTd9hx5kcR1IQvGfr8dbk" + // Specify the required fields for an isOpen request. + val placeFields: List = listOf( + Place.Field.BUSINESS_STATUS, + Place.Field.CURRENT_OPENING_HOURS, + Place.Field.ID, + Place.Field.OPENING_HOURS, + Place.Field.UTC_OFFSET + ) + + val placeRequest: FetchPlaceRequest = + FetchPlaceRequest.newInstance(placeId, placeFields) + val placeTask: Task = placesClient.fetchPlace(placeRequest) + placeTask.addOnSuccessListener { placeResponse -> + place = placeResponse.place + + val isOpenRequest: IsOpenRequest = try { + IsOpenRequest.newInstance(place, isOpenCalendar.timeInMillis) + } catch (e: IllegalArgumentException) { + e.printStackTrace() + return@addOnSuccessListener + } + val isOpenTask: Task = placesClient.isOpen(isOpenRequest) + isOpenTask.addOnSuccessListener { isOpenResponse -> + val isOpen = isOpenResponse.isOpen + } + // [START_EXCLUDE] + isOpenTask.addOnFailureListener { exception -> + exception.printStackTrace() + } + // [END_EXCLUDE] + } + // [START_EXCLUDE] + placeTask.addOnFailureListener { exception -> + exception.printStackTrace() + } + // [END_EXCLUDE] + // [END maps_places_place_is_open] + } + + /** + * Check if the place is open at the time specified in the input fields. + * Use the Place ID in the input field for the isOpenRequest. + */ + @SuppressLint("SetTextI18n") + private fun isOpenByPlaceId() { + // [START maps_places_id_is_open] + val isOpenCalendar: Calendar = Calendar.getInstance() + val placeId = "ChIJD3uTd9hx5kcR1IQvGfr8dbk" + + val request: IsOpenRequest = try { + IsOpenRequest.newInstance(placeId, isOpenCalendar.timeInMillis) + } catch (e: IllegalArgumentException) { + e.printStackTrace() + return + } + val isOpenTask: Task = placesClient.isOpen(request) + isOpenTask.addOnSuccessListener { response -> + val isOpen = response.isOpen + } + // [START_EXCLUDE] + isOpenTask.addOnFailureListener { exception -> + exception.printStackTrace() + } + isOpenTask.addOnCompleteListener { } + // [END_EXCLUDE] + // [END maps_places_id_is_open] + } +} \ No newline at end of file