diff options
author | Anirudh Dewani <anirudhd@google.com> | 2013-07-26 13:06:19 -0400 |
---|---|---|
committer | Anirudh Dewani <anirudhd@google.com> | 2013-09-05 17:15:09 -0400 |
commit | 5a321af732da3d0454b32cde6639eb7daf2d4620 (patch) | |
tree | 893cdcb35d69638720267392eaf86db504ece307 /common | |
parent | cfc743a1139b616191f422768d44eb8e1bc9bf27 (diff) | |
download | android-5a321af732da3d0454b32cde6639eb7daf2d4620.tar.gz |
Play Services Utility
Change-Id: I415576f5ab5f81d2f9eb46f33fafdabf5c85fa8d
Diffstat (limited to 'common')
-rw-r--r-- | common/src/com/example/android/common/play/PlayHelper.java | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/common/src/com/example/android/common/play/PlayHelper.java b/common/src/com/example/android/common/play/PlayHelper.java new file mode 100644 index 00000000..c38c2bb6 --- /dev/null +++ b/common/src/com/example/android/common/play/PlayHelper.java @@ -0,0 +1,102 @@ +package com.example.android.common.play; + +import android.app.Activity; +import android.app.Dialog; +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GooglePlayServicesUtil; + +/** + * Helper class for Google Play Services functions. + * <ul> + * <li> + * Checks availability + * </li> + * <li> + * Validates version for version bound features + * </li> + * </ul> + */ +public class PlayHelper { + + /** + * Checks for Google Play Services installation on the device. If found, validates the + * installed version against the requested version. If the service is installed but + * can't be used, the utility initiates a recovery with user intervention. + * + * @param context The context to be associated with the request. For compatibility with 1.6+, + * subclass {@link FragmentActivity}. + * @param requestCode If we need to download Google Play Services, the download activity will be + * started using {@link Activity#startActivityForResult(Intent, int)} + * @param versionCode The minimum required version of the library. + * @return True, if successful. + */ + public static boolean checkGooglePlayServiceAvailability( + FragmentActivity context, int requestCode, int versionCode) { + + // Query for the status of Google Play services on the device + int statusCode = GooglePlayServicesUtil + .isGooglePlayServicesAvailable(context); + + if ((statusCode == ConnectionResult.SUCCESS ) + && (GooglePlayServicesUtil.GOOGLE_PLAY_SERVICES_VERSION_CODE >= versionCode)) { + return true; + } else { + if (GooglePlayServicesUtil.isUserRecoverableError(statusCode)) { + Dialog eDialog = GooglePlayServicesUtil.getErrorDialog(statusCode, + context, requestCode); + // If Google Play services can provide an error dialog + if (eDialog != null) { + // Create a new DialogFragment for the error dialog + ErrorDialogFragment errorFragment = + new ErrorDialogFragment(); + // Set the dialog in the DialogFragment + errorFragment.setDialog(eDialog); + // Show the error dialog in the DialogFragment + errorFragment.show( + context.getSupportFragmentManager(), + "Activity Recognition"); + } + } else { + return false; + } + } + return false; + } + + /** + * Checks for Google Play Services installation on the device. If the service is installed but + * can't be used, the utility initiates a recovery with user intervention. + * + * @param context The context to be associated with the request. For compatibility with 1.6+, + * subclass {@link FragmentActivity}. + * @return True, if successful. + */ + public static boolean checkGooglePlayServiceAvailability(FragmentActivity context, + int requestCode) { + return checkGooglePlayServiceAvailability(context, requestCode, -1); + } + + // Define a DialogFragment that displays the error dialog + public static class ErrorDialogFragment extends DialogFragment { + // Global field to contain the error dialog + private Dialog mDialog; + // Default constructor. Sets the dialog field to null + public ErrorDialogFragment() { + super(); + mDialog = null; + } + // Set the dialog to display + public void setDialog(Dialog dialog) { + mDialog = dialog; + } + // Return a Dialog to the DialogFragment. + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + return mDialog; + } + } +} |