diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/src/java/com/example/android/common/play/GoogleServicesConnectionFailedHelper.java | 75 | ||||
-rw-r--r-- | common/src/java/com/example/android/common/play/PlayHelper.java (renamed from common/src/com/example/android/common/play/PlayHelper.java) | 18 |
2 files changed, 93 insertions, 0 deletions
diff --git a/common/src/java/com/example/android/common/play/GoogleServicesConnectionFailedHelper.java b/common/src/java/com/example/android/common/play/GoogleServicesConnectionFailedHelper.java new file mode 100644 index 00000000..0a80d880 --- /dev/null +++ b/common/src/java/com/example/android/common/play/GoogleServicesConnectionFailedHelper.java @@ -0,0 +1,75 @@ +/** + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.example.android.common.play; + +import android.app.Dialog; +import android.content.IntentSender; +import android.support.v4.app.FragmentActivity; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GooglePlayServicesClient; + +/** + * Helper to handle errors from Google Play Services connections. + * + */ +public class GoogleServicesConnectionFailedHelper implements + GooglePlayServicesClient.OnConnectionFailedListener { + + FragmentActivity mActivity; + int mRequestCode = -1; + + public GoogleServicesConnectionFailedHelper(FragmentActivity mActivity, int requestCode) { + this.mActivity = mActivity; + mRequestCode = requestCode; + } + + @Override + public void onConnectionFailed(ConnectionResult connectionResult) { + + /* + * Google Play services can resolve some errors it detects. + * If the error has a resolution, try sending an Intent to + * start a Google Play services activity that can resolve + * error. + */ + if (connectionResult.hasResolution()) { + try { + // Start an Activity that tries to resolve the error + connectionResult.startResolutionForResult(mActivity, mRequestCode); + /* + * Thrown if Google Play services canceled the original + * PendingIntent + */ + } catch (IntentSender.SendIntentException e) { + // Log the error + e.printStackTrace(); + } + } else { + /* + * If no resolution is available, display a dialog to the + * user with the error. + */ + PlayHelper.ErrorDialogFragment fragment = new PlayHelper.ErrorDialogFragment(); + fragment.setDialog(new Dialog(mActivity)); + fragment.show(mActivity.getSupportFragmentManager(), null); + + } + } +} + diff --git a/common/src/com/example/android/common/play/PlayHelper.java b/common/src/java/com/example/android/common/play/PlayHelper.java index c38c2bb6..95b8554c 100644 --- a/common/src/com/example/android/common/play/PlayHelper.java +++ b/common/src/java/com/example/android/common/play/PlayHelper.java @@ -1,3 +1,20 @@ +/* + * Copyright 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + package com.example.android.common.play; import android.app.Activity; @@ -99,4 +116,5 @@ public class PlayHelper { return mDialog; } } + } |