From 37a052ea2457ef548ec7ed38d1548281a6e5965e Mon Sep 17 00:00:00 2001 From: Asser Date: Sun, 19 Feb 2017 18:22:17 +1100 Subject: [PATCH] T0X.02-Solution-PlacePicker --- .../example/android/shushme/MainActivity.java | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/example/android/shushme/MainActivity.java b/app/src/main/java/com/example/android/shushme/MainActivity.java index 756eff59..def2033d 100644 --- a/app/src/main/java/com/example/android/shushme/MainActivity.java +++ b/app/src/main/java/com/example/android/shushme/MainActivity.java @@ -16,6 +16,8 @@ * limitations under the License. */ +import android.content.ContentValues; +import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.support.annotation.NonNull; @@ -29,12 +31,17 @@ import android.widget.CheckBox; import android.widget.Toast; +import com.example.android.shushme.provider.PlaceContract; import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GooglePlayServicesNotAvailableException; +import com.google.android.gms.common.GooglePlayServicesRepairableException; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; import com.google.android.gms.location.LocationServices; +import com.google.android.gms.location.places.Place; import com.google.android.gms.location.places.Places; +import com.google.android.gms.location.places.ui.PlacePicker; public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, @@ -43,6 +50,7 @@ public class MainActivity extends AppCompatActivity implements // Constants public static final String TAG = MainActivity.class.getSimpleName(); private static final int PERMISSIONS_REQUEST_FINE_LOCATION = 111; + private static final int PLACE_PICKER_REQUEST = 1; // Member variables private PlaceListAdapter mAdapter; @@ -119,13 +127,48 @@ public void onAddPlaceButtonClicked(View view) { Toast.makeText(this, getString(R.string.need_location_permission_message), Toast.LENGTH_LONG).show(); return; } - // TODO (1) Create a PlacePicker.IntentBuilder and call startActivityForResult - // TODO (2) Handle GooglePlayServices exceptions - Toast.makeText(this, getString(R.string.location_permissions_granted_message), Toast.LENGTH_LONG).show(); + try { + // Start a new Activity for the Place Picker API, this will trigger {@code #onActivityResult} + // when a place is selected or with the user cancels. + PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); + Intent i = builder.build(this); + startActivityForResult(i, PLACE_PICKER_REQUEST); + } catch (GooglePlayServicesRepairableException e) { + Log.e(TAG, String.format("GooglePlayServices Not Available [%s]", e.getMessage())); + } catch (GooglePlayServicesNotAvailableException e) { + Log.e(TAG, String.format("GooglePlayServices Not Available [%s]", e.getMessage())); + } catch (Exception e) { + Log.e(TAG, String.format("PlacePicker Exception: %s", e.getMessage())); + } } - // TODO (3) Implement onActivityResult and check that the requestCode is PLACE_PICKER_REQUEST - // TODO (4) In onActivityResult, use PlacePicker.getPlace to extract the Place ID and insert it into the DB + + /*** + * Called when the Place Picker Activity returns back with a selected place (or after canceling) + * + * @param requestCode The request code passed when calling startActivityForResult + * @param resultCode The result code specified by the second activity + * @param data The Intent that carries the result data. + */ + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == PLACE_PICKER_REQUEST && resultCode == RESULT_OK) { + Place place = PlacePicker.getPlace(this, data); + if (place == null) { + Log.i(TAG, "No place selected"); + return; + } + + // Extract the place information from the API + String placeName = place.getName().toString(); + String placeAddress = place.getAddress().toString(); + String placeID = place.getId(); + + // Insert a new place into DB + ContentValues contentValues = new ContentValues(); + contentValues.put(PlaceContract.PlaceEntry.COLUMN_PLACE_ID, placeID); + getContentResolver().insert(PlaceContract.PlaceEntry.CONTENT_URI, contentValues); + } + } @Override public void onResume() {