Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions app/src/main/java/com/example/android/shushme/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down