diff --git a/app/build.gradle b/app/build.gradle index c83808d..530d443 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -24,4 +24,5 @@ dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support:appcompat-v7:21.0.2' compile 'com.android.support:support-annotations:22.0.0' + compile 'com.google.android.gms:play-services-gcm:7.0.0' } diff --git a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java index 43d15aa..4960cfa 100644 --- a/app/src/main/java/com/example/android/sunshine/app/MainActivity.java +++ b/app/src/main/java/com/example/android/sunshine/app/MainActivity.java @@ -19,15 +19,19 @@ import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import com.example.android.sunshine.app.sync.SunshineSyncAdapter; +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GooglePlayServicesUtil; public class MainActivity extends ActionBarActivity implements ForecastFragment.Callback { private final String LOG_TAG = MainActivity.class.getSimpleName(); private static final String DETAILFRAGMENT_TAG = "DFTAG"; + private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; private boolean mTwoPane; private String mLocation; @@ -61,6 +65,13 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment. forecastFragment.setUseTodayLayout(!mTwoPane); SunshineSyncAdapter.initializeSyncAdapter(this); + + if (!checkPlayServices()) { + // this is where we could either prompt a user that they should install + // the latest version of Google Play Services, or add an error snackbar + // that some features won't be available. + } + } @Override @@ -82,16 +93,22 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment. startActivity(new Intent(this, SettingsActivity.class)); return true; } - return super.onOptionsItemSelected(item); } @Override protected void onResume() { super.onResume(); - String location = Utility.getPreferredLocation( this ); + + // If Google Play Services is not available, some features, such as GCM-powered weather + // alerts, will not be available. + if (!checkPlayServices()) { + // Store regID as null + } + + String location = Utility.getPreferredLocation(this); // update the location in our second pane using the fragment manager - if (location != null && !location.equals(mLocation)) { + if (location != null && !location.equals(mLocation)) { ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_forecast); if ( null != ff ) { ff.onLocationChanged(); @@ -125,4 +142,24 @@ public class MainActivity extends ActionBarActivity implements ForecastFragment. startActivity(intent); } } + + /** + * Check the device to make sure it has the Google Play Services APK. If + * it doesn't, display a dialog that allows users to download the APK from + * the Google Play Store or enable it in the device's system settings. + */ + private boolean checkPlayServices() { + int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); + if (resultCode != ConnectionResult.SUCCESS) { + if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { + GooglePlayServicesUtil.getErrorDialog(resultCode, this, + PLAY_SERVICES_RESOLUTION_REQUEST).show(); + } else { + Log.i(LOG_TAG, "This device is not supported."); + finish(); + } + return false; + } + return true; + } }