Ejemplo n.º 1
0
  /** 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();
  }
Ejemplo n.º 2
0
  /**
   * 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();
    }
  }