/** * Handle activity result. Call this method from your Activity's onActivityResult callback. If the * activity result pertains to the sign-in process, processes it appropriately. */ public void onActivityResult(int requestCode, int responseCode, Intent intent) { debugLog( "onActivityResult: req=" + (requestCode == RC_RESOLVE ? "RC_RESOLVE" : String.valueOf(requestCode)) + ", resp=" + GameHelperUtils.activityResponseCodeToString(responseCode)); if (requestCode != RC_RESOLVE) { debugLog("onActivityResult: request code not meant for us. Ignoring."); return; } // no longer expecting a resolution mExpectingResolution = false; if (!mConnecting) { debugLog("onActivityResult: ignoring because we are not connecting."); return; } // We're coming back from an activity that was launched to resolve a // connection problem. For example, the sign-in UI. if (responseCode == Activity.RESULT_OK) { // Ready to try to connect again. debugLog("onAR: Resolution was RESULT_OK, so connecting current client again."); connect(); } else if (responseCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) { debugLog("onAR: Resolution was RECONNECT_REQUIRED, so reconnecting."); connect(); } else if (responseCode == Activity.RESULT_CANCELED) { // User cancelled. debugLog("onAR: Got a cancellation result, so disconnecting."); mSignInCancelled = true; mConnectOnStart = false; mUserInitiatedSignIn = false; mSignInFailureReason = null; // cancelling is not a failure! mConnecting = false; mGoogleApiClient.disconnect(); // increment # of cancellations int prevCancellations = getSignInCancellations(); int newCancellations = incrementSignInCancellations(); debugLog( "onAR: # of cancellations " + prevCancellations + " --> " + newCancellations + ", max " + mMaxAutoSignInAttempts); notifyListener(false); } else { // Whatever the problem we were trying to solve, it was not // solved. So give up and show an error message. debugLog( "onAR: responseCode=" + GameHelperUtils.activityResponseCodeToString(responseCode) + ", so giving up."); giveUp(new SignInFailureReason(mConnectionResult.getErrorCode(), responseCode)); } }
@Override public String toString() { return "SignInFailureReason(serviceErrorCode:" + GameHelperUtils.errorCodeToString(mServiceErrorCode) + ((mActivityResultCode == NO_ACTIVITY_RESULT_CODE) ? ")" : (",activityResultCode:" + GameHelperUtils.activityResponseCodeToString(mActivityResultCode) + ")")); }
/** Handles a connection failure. */ @Override public void onConnectionFailed(ConnectionResult result) { // save connection result for later reference debugLog("onConnectionFailed"); mConnectionResult = result; debugLog("Connection failure:"); debugLog(" - code: " + GameHelperUtils.errorCodeToString(mConnectionResult.getErrorCode())); debugLog(" - resolvable: " + mConnectionResult.hasResolution()); debugLog(" - details: " + mConnectionResult.toString()); int cancellations = getSignInCancellations(); boolean shouldResolve = false; if (mUserInitiatedSignIn) { debugLog("onConnectionFailed: WILL resolve because user initiated sign-in."); shouldResolve = true; } else if (mSignInCancelled) { debugLog("onConnectionFailed WILL NOT resolve (user already cancelled once)."); shouldResolve = false; } else if (cancellations < mMaxAutoSignInAttempts) { debugLog( "onConnectionFailed: WILL resolve because we have below the max# of " + "attempts, " + cancellations + " < " + mMaxAutoSignInAttempts); shouldResolve = true; } else { shouldResolve = false; debugLog( "onConnectionFailed: Will NOT resolve; not user-initiated and max attempts " + "reached: " + cancellations + " >= " + mMaxAutoSignInAttempts); } if (!shouldResolve) { // Fail and wait for the user to want to sign in. debugLog("onConnectionFailed: since we won't resolve, failing now."); mConnectionResult = result; mConnecting = false; notifyListener(false); return; } debugLog("onConnectionFailed: resolving problem..."); // Resolve the connection result. This usually means showing a dialog or // starting an Activity that will allow the user to give the appropriate // consents so that sign-in can be successful. resolveConnectionResult(); }
/** Shows an error dialog that's appropriate for the failure reason. */ public static void showFailureDialog(Activity activity, int actResp, int errorCode) { if (activity == null) { Log.e("GameHelper", "*** No Activity. Can't show failure dialog!"); return; } Dialog errorDialog = null; switch (actResp) { case GamesActivityResultCodes.RESULT_APP_MISCONFIGURED: errorDialog = makeSimpleDialog( activity, GameHelperUtils.getString(activity, GameHelperUtils.R_APP_MISCONFIGURED)); break; case GamesActivityResultCodes.RESULT_SIGN_IN_FAILED: errorDialog = makeSimpleDialog( activity, GameHelperUtils.getString(activity, GameHelperUtils.R_SIGN_IN_FAILED)); break; case GamesActivityResultCodes.RESULT_LICENSE_FAILED: errorDialog = makeSimpleDialog( activity, GameHelperUtils.getString(activity, GameHelperUtils.R_LICENSE_FAILED)); break; default: // No meaningful Activity response code, so generate default Google // Play services dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode, activity, RC_UNUSED, null); if (errorDialog == null) { // get fallback dialog Log.e("GameHelper", "No standard error dialog available. Making fallback dialog."); errorDialog = makeSimpleDialog( activity, GameHelperUtils.getString(activity, GameHelperUtils.R_UNKNOWN_ERROR) + " " + GameHelperUtils.errorCodeToString(errorCode)); } } errorDialog.show(); }
/** * Give up on signing in due to an error. Shows the appropriate error message to the user, using a * standard error dialog as appropriate to the cause of the error. That dialog will indicate to * the user how the problem can be solved (for example, re-enable Google Play Services, upgrade to * a new version, etc). */ void giveUp(SignInFailureReason reason) { mConnectOnStart = false; disconnect(); mSignInFailureReason = reason; if (reason.mActivityResultCode == GamesActivityResultCodes.RESULT_APP_MISCONFIGURED) { // print debug info for the developer GameHelperUtils.printMisconfiguredDebugInfo(mAppContext); } showFailureDialog(); mConnecting = false; notifyListener(false); }