diff --git a/app/build.gradle b/app/build.gradle index 66172bad..4a0fc28f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,6 +26,7 @@ dependencies { }) compile 'com.android.support:appcompat-v7:25.1.0' compile 'com.android.support:recyclerview-v7:25.0.1' - // TODO (3) Add play-services-places and play-services-location dependencies + compile 'com.google.android.gms:play-services-places:9.8.0' + compile 'com.google.android.gms:play-services-location:9.8.0' testCompile 'junit:junit:4.12' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 764cd012..44d2738d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,9 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> - + @@ -25,6 +27,6 @@ - - + + \ No newline at end of file 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 a33cf634..7316b784 100644 --- a/app/src/main/java/com/example/android/shushme/MainActivity.java +++ b/app/src/main/java/com/example/android/shushme/MainActivity.java @@ -16,15 +16,33 @@ * limitations under the License. */ +import android.content.pm.PackageManager; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.View; +import android.widget.CheckBox; +import android.widget.Toast; -public class MainActivity extends AppCompatActivity { +import com.google.android.gms.common.ConnectionResult; +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.Places; + +public class MainActivity extends AppCompatActivity implements + ConnectionCallbacks, + OnConnectionFailedListener { // Constants public static final String TAG = MainActivity.class.getSimpleName(); + private static final int PERMISSIONS_REQUEST_FINE_LOCATION = 111; // Member variables private PlaceListAdapter mAdapter; @@ -46,12 +64,82 @@ protected void onCreate(Bundle savedInstanceState) { mAdapter = new PlaceListAdapter(this); mRecyclerView.setAdapter(mAdapter); - // TODO (4) Create a GoogleApiClient with the LocationServices API and GEO_DATA_API + + // Build up the LocationServices API client + // Uses the addApi method to request the LocationServices API + // Also uses enableAutoManage to automatically when to connect/suspend the client + GoogleApiClient client = new GoogleApiClient.Builder(this) + .addConnectionCallbacks(this) + .addOnConnectionFailedListener(this) + .addApi(LocationServices.API) + .addApi(Places.GEO_DATA_API) + .enableAutoManage(this, this) + .build(); + + } + + /*** + * Called when the Google API Client is successfully connected + * + * @param connectionHint Bundle of data provided to clients by Google Play services + */ + @Override + public void onConnected(@Nullable Bundle connectionHint) { + Log.i(TAG, "API Client Connection Successful!"); + } + + /*** + * Called when the Google API Client is suspended + * + * @param cause cause The reason for the disconnection. Defined by constants CAUSE_*. + */ + @Override + public void onConnectionSuspended(int cause) { + Log.i(TAG, "API Client Connection Suspended!"); + } + + /*** + * Called when the Google API Client failed to connect to Google Play Services + * + * @param result A ConnectionResult that can be used for resolving the error + */ + @Override + public void onConnectionFailed(@NonNull ConnectionResult result) { + Log.e(TAG, "API Client Connection Failed!"); } - // TODO (5) Override onConnected, onConnectionSuspended and onConnectionFailed for GoogleApiClient - // TODO (7) Override onResume and inside it initialize the location permissions checkbox - // TODO (8) Implement onLocationPermissionClicked to handle the CheckBox click event - // TODO (9) Implement the Add Place Button click event to show a toast message with the permission status + /*** + * Button Click event handler to handle clicking the "Add new location" Button + * + * @param view + */ + public void onAddPlaceButtonClicked(View view) { + if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) + != PackageManager.PERMISSION_GRANTED) { + Toast.makeText(this, getString(R.string.need_location_permission_message), Toast.LENGTH_LONG).show(); + return; + } + Toast.makeText(this, getString(R.string.location_permissions_granted_message), Toast.LENGTH_LONG).show(); + } + + @Override + public void onResume() { + super.onResume(); + // Initialize location permissions checkbox + CheckBox locationPermissions = (CheckBox) findViewById(R.id.location_permission_checkbox); + if (ActivityCompat.checkSelfPermission(MainActivity.this, + android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + locationPermissions.setChecked(false); + } else { + locationPermissions.setChecked(true); + locationPermissions.setEnabled(false); + } + } + + public void onLocationPermissionClicked(View view) { + ActivityCompat.requestPermissions(MainActivity.this, + new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, + PERMISSIONS_REQUEST_FINE_LOCATION); + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 101323e4..f422715d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -59,14 +59,49 @@ android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> - + + + + + + + + + + + + +