Home | History | Annotate | Download | only in play
      1 /*
      2  * Copyright 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 
     18 package com.example.android.common.play;
     19 
     20 import android.app.Activity;
     21 import android.app.Dialog;
     22 import android.os.Bundle;
     23 import android.support.v4.app.DialogFragment;
     24 import android.support.v4.app.FragmentActivity;
     25 
     26 import com.google.android.gms.common.ConnectionResult;
     27 import com.google.android.gms.common.GooglePlayServicesUtil;
     28 
     29 /**
     30  * Helper class for Google Play Services functions.
     31  * <ul>
     32  *     <li>
     33  *         Checks availability
     34  *     </li>
     35  *     <li>
     36  *         Validates version for version bound features
     37  *     </li>
     38  * </ul>
     39  */
     40 public class PlayHelper {
     41 
     42     /**
     43      * Checks for Google Play Services installation on the device. If found, validates the
     44      * installed version against the requested version. If the service is installed but
     45      * can't be used, the utility initiates a recovery with user intervention.
     46      *
     47      * @param context The context to be associated with the request. For compatibility with 1.6+,
     48      *                subclass {@link FragmentActivity}.
     49      * @param requestCode If we need to download Google Play Services, the download activity will be
     50      *                    started using {@link Activity#startActivityForResult(Intent, int)}
     51      * @param versionCode The minimum required version of the library.
     52      * @return True, if successful.
     53      */
     54     public static boolean checkGooglePlayServiceAvailability(
     55             FragmentActivity context, int requestCode, int versionCode) {
     56 
     57         // Query for the status of Google Play services on the device
     58         int statusCode = GooglePlayServicesUtil
     59                 .isGooglePlayServicesAvailable(context);
     60 
     61         if ((statusCode == ConnectionResult.SUCCESS )
     62                 &&  (GooglePlayServicesUtil.GOOGLE_PLAY_SERVICES_VERSION_CODE >=  versionCode)) {
     63             return true;
     64         } else {
     65             if (GooglePlayServicesUtil.isUserRecoverableError(statusCode)) {
     66                 Dialog eDialog = GooglePlayServicesUtil.getErrorDialog(statusCode,
     67                         context, requestCode);
     68                 // If Google Play services can provide an error dialog
     69                 if (eDialog != null) {
     70                     // Create a new DialogFragment for the error dialog
     71                     ErrorDialogFragment errorFragment =
     72                             new ErrorDialogFragment();
     73                     // Set the dialog in the DialogFragment
     74                     errorFragment.setDialog(eDialog);
     75                     // Show the error dialog in the DialogFragment
     76                     errorFragment.show(
     77                             context.getSupportFragmentManager(),
     78                             "Activity Recognition");
     79                 }
     80             } else {
     81                 return false;
     82             }
     83         }
     84         return false;
     85     }
     86 
     87     /**
     88      * Checks for Google Play Services installation on the device. If the service is installed but
     89      * can't be used, the utility initiates a recovery with user intervention.
     90      *
     91      * @param context The context to be associated with the request. For compatibility with 1.6+,
     92      *                subclass {@link FragmentActivity}.
     93      * @return True, if successful.
     94      */
     95     public static boolean checkGooglePlayServiceAvailability(FragmentActivity context,
     96             int requestCode) {
     97         return checkGooglePlayServiceAvailability(context, requestCode, -1);
     98     }
     99 
    100     // Define a DialogFragment that displays the error dialog
    101     public static class ErrorDialogFragment extends DialogFragment {
    102         // Global field to contain the error dialog
    103         private Dialog mDialog;
    104         // Default constructor. Sets the dialog field to null
    105         public ErrorDialogFragment() {
    106             super();
    107             mDialog = null;
    108         }
    109         // Set the dialog to display
    110         public void setDialog(Dialog dialog) {
    111             mDialog = dialog;
    112         }
    113         // Return a Dialog to the DialogFragment.
    114         @Override
    115         public Dialog onCreateDialog(Bundle savedInstanceState) {
    116             return mDialog;
    117         }
    118     }
    119 
    120 }
    121