Skip to content

Commit

Permalink
chore: add snippets for isOpen (#523)
Browse files Browse the repository at this point in the history
* feat: add Kotlin snippets for isOpen

* fix: stragglers in snippets: PlaceTypes and string interpolation
  • Loading branch information
wangela authored Apr 28, 2023
1 parent 3684bc5 commit 599adb1
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
118 changes: 118 additions & 0 deletions snippets/app/src/main/java/com/google/places/PlaceIsOpenActivity.java
Original file line number Diff line number Diff line change
@@ -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<Place.Field> 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<FetchPlaceResponse> 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<IsOpenResponse> 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<IsOpenResponse> placeTask = placesClient.isOpen(isOpenRequest);

placeTask.addOnSuccessListener(
(response) ->
isOpen = response.isOpen());
// [START_EXCLUDE]
placeTask.addOnFailureListener(
Throwable::printStackTrace);
// [END_EXCLUDE]
// [END maps_places_id_is_open]
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -107,7 +107,7 @@ class PlaceAutocompleteActivity : AppCompatActivity() {
val fields: List<Place.Field> = 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]

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading

0 comments on commit 599adb1

Please sign in to comment.