From 8df43d40c6e634f52cce1aedc195e569340bf602 Mon Sep 17 00:00:00 2001 From: Hrishikesh Kadam Date: Fri, 8 Dec 2017 18:57:46 +0530 Subject: [PATCH 01/10] T0X.01-Solution-GoogleApiClient --- .gitignore | 3 + app/build.gradle | 22 +++- app/src/main/AndroidManifest.xml | 11 +- .../example/android/shushme/MainActivity.java | 109 +++++++++++++++++- app/src/main/res/layout/activity_main.xml | 38 +++++- app/src/main/res/values/strings.xml | 5 +- build.gradle | 3 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 8 files changed, 175 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index acc2157a..720422ef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ .DS_Store +# local keystore files +keystore.properties + # built application files *.apk *.ap_ diff --git a/app/build.gradle b/app/build.gradle index 66172bad..654796fc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,16 @@ apply plugin: 'com.android.application' +def keystorePropertiesFile = rootProject.file("keystore.properties") +def keystoreProperties = new Properties() +keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) + android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" + compileSdkVersion 26 + buildToolsVersion "26.0.3" defaultConfig { applicationId "com.example.android.shushme" minSdkVersion 16 - targetSdkVersion 25 + targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -17,6 +21,10 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + + buildTypes.each { + it.resValue 'string', 'GOOGLE_API_KEY', keystoreProperties['GOOGLE_API_KEY'] + } } dependencies { @@ -24,8 +32,10 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - 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.android.support:appcompat-v7:26.1.0' + compile 'com.android.support:recyclerview-v7:26.1.0' + // TODO COMPLETED (3) Add play-services-places and play-services-location dependencies + compile 'com.google.android.gms:play-services-places:11.6.2' + compile 'com.google.android.gms:play-services-location:11.6.2' testCompile 'junit:junit:4.12' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 764cd012..96b85fdf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,10 @@ + + + + - + + @@ -25,6 +32,4 @@ - - \ 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..7aef31f5 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,31 @@ * 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.location.LocationServices; +import com.google.android.gms.location.places.Places; + +public class MainActivity extends AppCompatActivity implements + GoogleApiClient.ConnectionCallbacks, + GoogleApiClient.OnConnectionFailedListener { // Constants public static final String TAG = MainActivity.class.getSimpleName(); + private static final int PERMISSIONS_REQUEST_FINE_LOCATION = 111; // Member variables private PlaceListAdapter mAdapter; @@ -38,6 +54,8 @@ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Log.v(TAG, "-> onCreate"); + setContentView(R.layout.activity_main); // Set up the recycler view @@ -46,12 +64,91 @@ 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 + // TODO COMPLETED (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(); + } + + // TODO COMPLETED (5) Override onConnected, onConnectionSuspended and onConnectionFailed for GoogleApiClient + + /*** + * 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 COMPLETED (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) { + Log.v(TAG, "-> onAddPlaceButtonClicked"); + + 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(); } - // 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 + // TODO COMPLETED (7) Override onResume and inside it initialize the location permissions checkbox + @Override + public void onResume() { + super.onResume(); + Log.v(TAG, "-> 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); + } + } + + // TODO COMPLETED (8) Implement onLocationPermissionClicked to handle the CheckBox click event + public void onLocationPermissionClicked(View view) { + Log.v(TAG, "-> onLocationPermissionClicked"); + + 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..3482ac33 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -59,14 +59,50 @@ android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> - + + + + + + + + + + + +