/** * 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)); } }
void succeedSignIn() { debugLog("succeedSignIn"); mSignInFailureReason = null; mConnectOnStart = true; mUserInitiatedSignIn = false; mConnecting = false; notifyListener(true); }
/** Called when we are disconnected from the Google API client. */ @Override public void onConnectionSuspended(int cause) { debugLog("onConnectionSuspended, cause=" + cause); disconnect(); mSignInFailureReason = null; debugLog("Making extraordinary call to onSignInFailed callback"); mConnecting = false; notifyListener(false); }
/** 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; 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(); }
/** * 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); }
/** * Starts a user-initiated sign-in flow. This should be called when the user clicks on a "Sign In" * button. As a result, authentication/consent dialogs may show up. At the end of the process, the * GameHelperListener's onSignInSucceeded() or onSignInFailed() methods will be called. */ public void beginUserInitiatedSignIn() { debugLog("beginUserInitiatedSignIn: resetting attempt count."); resetSignInCancellations(); mSignInCancelled = false; mConnectOnStart = true; if (mGoogleApiClient.isConnected()) { // nothing to do logWarn( "beginUserInitiatedSignIn() called when already connected. " + "Calling listener directly to notify of success."); notifyListener(true); return; } else if (mConnecting) { logWarn( "beginUserInitiatedSignIn() called when already connecting. " + "Be patient! You can only call this method after you get an " + "onSignInSucceeded() or onSignInFailed() callback. Suggestion: disable " + "the sign-in button on startup and also when it's clicked, and re-enable " + "when you get the callback."); // ignore call (listener will get a callback when the connection // process finishes) return; } debugLog("Starting USER-INITIATED sign-in flow."); // indicate that user is actively trying to sign in (so we know to // resolve // connection problems by showing dialogs) mUserInitiatedSignIn = true; if (mConnectionResult != null) { // We have a pending connection result from a previous failure, so // start with that. debugLog("beginUserInitiatedSignIn: continuing pending sign-in flow."); mConnecting = true; resolveConnectionResult(); } else { // We don't have a pending connection result, so start anew. debugLog("beginUserInitiatedSignIn: starting new sign-in flow."); mConnecting = true; connect(); } }