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.Dialog;
     21 import android.content.IntentSender;
     22 import android.support.v4.app.FragmentActivity;
     23 
     24 import com.google.android.gms.common.ConnectionResult;
     25 import com.google.android.gms.common.GooglePlayServicesClient;
     26 
     27 /**
     28  * Helper to handle errors from Google Play Services connections.
     29  *
     30  */
     31 public class GoogleServicesConnectionFailedHelper implements
     32         GooglePlayServicesClient.OnConnectionFailedListener {
     33 
     34     FragmentActivity mActivity;
     35     int mRequestCode = -1;
     36 
     37     public GoogleServicesConnectionFailedHelper(FragmentActivity mActivity, int requestCode) {
     38         this.mActivity = mActivity;
     39         mRequestCode = requestCode;
     40     }
     41 
     42     @Override
     43     public void onConnectionFailed(ConnectionResult connectionResult) {
     44 
     45         /*
     46          * Google Play services can resolve some errors it detects.
     47          * If the error has a resolution, try sending an Intent to
     48          * start a Google Play services activity that can resolve
     49          * error.
     50          */
     51         if (connectionResult.hasResolution()) {
     52             try {
     53                 // Start an Activity that tries to resolve the error
     54                 connectionResult.startResolutionForResult(mActivity, mRequestCode);
     55                 /*
     56                  * Thrown if Google Play services canceled the original
     57                  * PendingIntent
     58                  */
     59             } catch (IntentSender.SendIntentException e) {
     60                 // Log the error
     61                 e.printStackTrace();
     62             }
     63         } else {
     64             /*
     65              * If no resolution is available, display a dialog to the
     66              * user with the error.
     67              */
     68             PlayHelper.ErrorDialogFragment fragment = new PlayHelper.ErrorDialogFragment();
     69             fragment.setDialog(new Dialog(mActivity));
     70             fragment.show(mActivity.getSupportFragmentManager(), null);
     71 
     72         }
     73     }
     74 }
     75 
     76