Skip to content

Commit 599adb1

Browse files
authored
chore: add snippets for isOpen (#523)
* feat: add Kotlin snippets for isOpen * fix: stragglers in snippets: PlaceTypes and string interpolation
1 parent 3684bc5 commit 599adb1

12 files changed

+344
-6
lines changed

snippets/app/src/main/java/com/google/places/CurrentPlaceActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.google.places;
216

317
import android.content.pm.PackageManager;

snippets/app/src/main/java/com/google/places/PlaceAutocompleteActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void startAutocompleteIntent() {
149149
Intent intent = result.getData();
150150
if (intent != null) {
151151
Place place = Autocomplete.getPlaceFromIntent(intent);
152-
Log.i(TAG, "Place: {$place.getName()}, ${place.getId()}");
152+
Log.i(TAG, "Place: ${place.getName()}, ${place.getId()}");
153153
}
154154
} else if (result.getResultCode() == Activity.RESULT_CANCELED) {
155155
// The user canceled the operation.

snippets/app/src/main/java/com/google/places/PlaceDetailsActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.google.places;
216

317
import android.util.Log;
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.places;
16+
17+
import android.annotation.SuppressLint;
18+
19+
import androidx.annotation.NonNull;
20+
import androidx.appcompat.app.AppCompatActivity;
21+
22+
import com.google.android.gms.tasks.Task;
23+
import com.google.android.libraries.places.api.model.Place;
24+
import com.google.android.libraries.places.api.net.FetchPlaceRequest;
25+
import com.google.android.libraries.places.api.net.FetchPlaceResponse;
26+
import com.google.android.libraries.places.api.net.IsOpenRequest;
27+
import com.google.android.libraries.places.api.net.IsOpenResponse;
28+
import com.google.android.libraries.places.api.net.PlacesClient;
29+
30+
import java.util.ArrayList;
31+
import java.util.Arrays;
32+
import java.util.Calendar;
33+
import java.util.List;
34+
35+
public class PlaceIsOpenActivity extends AppCompatActivity {
36+
private PlacesClient placesClient;
37+
private Boolean isOpen;
38+
39+
/**
40+
* Check if the place is open at the time specified in the input fields.
41+
* Requires a Place object that includes Place.Field.ID
42+
*/
43+
@SuppressLint("SetTextI18n")
44+
private void isOpenByPlaceObject() {
45+
// [START maps_places_place_is_open]
46+
@NonNull
47+
Calendar isOpenCalendar = Calendar.getInstance();
48+
String placeId = "ChIJD3uTd9hx5kcR1IQvGfr8dbk";
49+
// Specify the required fields for an isOpen request.
50+
List<Place.Field> placeFields = new ArrayList<>(Arrays.asList(
51+
Place.Field.BUSINESS_STATUS,
52+
Place.Field.CURRENT_OPENING_HOURS,
53+
Place.Field.ID,
54+
Place.Field.OPENING_HOURS,
55+
Place.Field.UTC_OFFSET
56+
));
57+
58+
FetchPlaceRequest request = FetchPlaceRequest.newInstance(placeId, placeFields);
59+
Task<FetchPlaceResponse> placeTask = placesClient.fetchPlace(request);
60+
61+
placeTask.addOnSuccessListener(
62+
(placeResponse) -> {
63+
Place place = placeResponse.getPlace();
64+
IsOpenRequest isOpenRequest;
65+
66+
try {
67+
isOpenRequest = IsOpenRequest.newInstance(place, isOpenCalendar.getTimeInMillis());
68+
} catch (IllegalArgumentException e) {
69+
e.printStackTrace();
70+
return;
71+
}
72+
Task<IsOpenResponse> isOpenTask = placesClient.isOpen(isOpenRequest);
73+
74+
isOpenTask.addOnSuccessListener(
75+
(isOpenResponse) -> isOpen = isOpenResponse.isOpen());
76+
// [START_EXCLUDE]
77+
placeTask.addOnFailureListener(
78+
Throwable::printStackTrace);
79+
// [END_EXCLUDE]
80+
});
81+
// [START_EXCLUDE]
82+
placeTask.addOnFailureListener(
83+
Throwable::printStackTrace);
84+
// [END_EXCLUDE]
85+
// [END maps_places_place_is_open]
86+
}
87+
88+
/**
89+
* Check if the place is open at the time specified in the input fields.
90+
* Use the Place ID in the input field for the isOpenRequest.
91+
*/
92+
@SuppressLint("SetTextI18n")
93+
private void isOpenByPlaceId() {
94+
// [START maps_places_id_is_open]
95+
@NonNull
96+
Calendar isOpenCalendar = Calendar.getInstance();
97+
String placeId = "ChIJD3uTd9hx5kcR1IQvGfr8dbk";
98+
IsOpenRequest isOpenRequest;
99+
100+
try {
101+
isOpenRequest = IsOpenRequest.newInstance(placeId, isOpenCalendar.getTimeInMillis());
102+
} catch (IllegalArgumentException e) {
103+
e.printStackTrace();
104+
return;
105+
}
106+
107+
Task<IsOpenResponse> placeTask = placesClient.isOpen(isOpenRequest);
108+
109+
placeTask.addOnSuccessListener(
110+
(response) ->
111+
isOpen = response.isOpen());
112+
// [START_EXCLUDE]
113+
placeTask.addOnFailureListener(
114+
Throwable::printStackTrace);
115+
// [END_EXCLUDE]
116+
// [END maps_places_id_is_open]
117+
}
118+
}

snippets/app/src/main/java/com/google/places/PlacePhotosActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.google.places;
216

317
import android.graphics.Bitmap;

snippets/app/src/main/java/com/google/places/PlacesIconActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.google.places;
216

317
import android.widget.ImageView;

snippets/app/src/main/java/com/google/places/kotlin/CurrentPlaceActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.google.places.kotlin
216

317
import android.Manifest.permission

snippets/app/src/main/java/com/google/places/kotlin/PlaceAutocompleteActivity.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import com.google.android.gms.maps.model.LatLng
2626
import com.google.android.libraries.places.api.Places
2727
import com.google.android.libraries.places.api.model.AutocompleteSessionToken
2828
import com.google.android.libraries.places.api.model.Place
29+
import com.google.android.libraries.places.api.model.PlaceTypes
2930
import com.google.android.libraries.places.api.model.RectangularBounds
30-
import com.google.android.libraries.places.api.model.TypeFilter
3131
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest
3232
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse
3333
import com.google.android.libraries.places.api.net.PlacesClient
@@ -97,7 +97,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
9797
// [END maps_places_autocomplete_location_restriction]
9898

9999
// [START maps_places_autocomplete_type_filter]
100-
autocompleteFragment.setTypesFilter(listOf(TypeFilter.ADDRESS.toString()))
100+
autocompleteFragment.setTypesFilter(listOf(PlaceTypes.ADDRESS))
101101
// [END maps_places_autocomplete_type_filter]
102102

103103
// [START maps_places_autocomplete_type_filter_multiple]
@@ -107,7 +107,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
107107
val fields: List<Place.Field> = ArrayList()
108108
// [START maps_places_intent_type_filter]
109109
val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields)
110-
.setTypesFilter(listOf(TypeFilter.ADDRESS.toString()))
110+
.setTypesFilter(listOf(PlaceTypes.ADDRESS))
111111
.build(this)
112112
// [END maps_places_intent_type_filter]
113113

@@ -140,7 +140,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
140140
if (intent != null) {
141141
val place = Autocomplete.getPlaceFromIntent(intent)
142142
Log.i(
143-
TAG, "Place: {\$place.getName()}, \${place.getId()}"
143+
TAG, "Place: ${place.name}, ${place.id}"
144144
)
145145
}
146146
} else if (result.resultCode == Activity.RESULT_CANCELED) {
@@ -169,7 +169,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
169169
//.setLocationRestriction(bounds)
170170
.setOrigin(LatLng(-33.8749937, 151.2041382))
171171
.setCountries("AU", "NZ")
172-
.setTypesFilter(listOf(TypeFilter.ADDRESS.toString()))
172+
.setTypesFilter(listOf(PlaceTypes.ADDRESS))
173173
.setSessionToken(token)
174174
.setQuery(query)
175175
.build()

snippets/app/src/main/java/com/google/places/kotlin/PlaceDetailsActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.google.places.kotlin
216

317
import android.util.Log

snippets/app/src/main/java/com/google/places/kotlin/PlacePhotosActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package com.google.places.kotlin
216

317
import android.util.Log

0 commit comments

Comments
 (0)