/**
   * This method builds an apiClient instance
   *
   * @param accountName
   */
  private void initializeApiClient(String accountName) {

    GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this, this, this);
    builder.setAccountName(accountName);
    builder.addApi(Plus.API);
    builder.addScope(Plus.SCOPE_PLUS_LOGIN);
    apiClient = builder.build();
  }
  /**
   * Creates a GoogleApiClient.Builder for use with @link{#setup}. Normally, you do not have to do
   * this; use this method only if you need to make nonstandard setup (e.g. adding extra scopes for
   * other APIs) on the GoogleApiClient.Builder before calling @link{#setup}.
   */
  public GoogleApiClient.Builder createApiClientBuilder() {
    if (mSetupDone) {
      String error =
          "GameHelper: you called GameHelper.createApiClientBuilder() after "
              + "calling setup. You can only get a client builder BEFORE performing setup.";
      logError(error);
      throw new IllegalStateException(error);
    }

    GoogleApiClient.Builder builder = new GoogleApiClient.Builder(mActivity, this, this);

    if (0 != (mRequestedClients & CLIENT_GAMES)) {
      builder.addApi(Games.API, mGamesApiOptions);
      builder.addScope(Games.SCOPE_GAMES);
    }

    if (0 != (mRequestedClients & CLIENT_PLUS)) {
      builder.addApi(Plus.API);
      builder.addScope(Plus.SCOPE_PLUS_LOGIN);
    }

    if (0 != (mRequestedClients & CLIENT_APPSTATE)) {
      builder.addApi(AppStateManager.API);
      builder.addScope(AppStateManager.SCOPE_APP_STATE);
    }

    if (0 != (mRequestedClients & CLIENT_SNAPSHOT)) {
      builder.addScope(Drive.SCOPE_APPFOLDER);
      builder.addApi(Drive.API);
    }

    mGoogleApiClientBuilder = builder;
    return builder;
  }
  /**
   * Build the GoogleApiClient if it has not already been built.
   *
   * @return Our GoogleApiClient
   */
  private synchronized GoogleApiClient buildGoogleApiClient() {
    if (this.mGoogleApiClient != null) {
      return this.mGoogleApiClient;
    }

    GoogleApiClient.Builder builder =
        new GoogleApiClient.Builder(webView.getContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build());

    for (Scope scope : this.scopes) {
      builder.addScope(scope);
    }

    this.mGoogleApiClient = builder.build();
    return this.mGoogleApiClient;
  }
  /** Starts the helper. Call this from your Activity's onStart(). */
  public void start() {
    Activity activity = getActivity("start()");
    if (activity == null) {
      return;
    }

    if (mStarted) {
      LOGW(TAG, "Helper already started. Ignoring redundant call.");
      return;
    }

    mStarted = true;
    if (mResolving) {
      // if resolving, don't reconnect the plus client
      LOGD(TAG, "Helper ignoring signal to start because we're resolving a failure.");
      return;
    }
    LOGD(TAG, "Helper starting. Connecting " + mAccountName);
    if (mGoogleApiClient == null) {
      LOGD(TAG, "Creating client.");

      GoogleApiClient.Builder builder = new GoogleApiClient.Builder(activity);
      for (String scope : AUTH_SCOPES) {
        builder.addScope(new Scope(scope));
      }
      mGoogleApiClient =
          builder
              .addApi(Plus.API)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .setAccountName(mAccountName)
              .build();
    }
    LOGD(TAG, "Connecting client.");
    mGoogleApiClient.connect();
  }
  /**
   * Performs setup on this GameHelper object. Call this from the onCreate() method of your
   * Activity. This will create the clients and do a few other initialization tasks. Next,
   * call @link{#onStart} from the onStart() method of your Activity.
   *
   * @param listener The listener to be notified of sign-in events.
   */
  public void setup(GameHelperListener listener) {
    if (mSetupDone) {
      String error = "GameHelper: you cannot call GameHelper.setup() more than once!";
      logError(error);
      throw new IllegalStateException(error);
    }
    mListener = listener;
    debugLog("Setup: requested clients: " + mRequestedClients);

    if (mGoogleApiClientBuilder == null) {
      // we don't have a builder yet, so create one
      createApiClientBuilder();
    }

    mGoogleApiClient = mGoogleApiClientBuilder.build();
    mGoogleApiClientBuilder = null;
    mSetupDone = true;
  }
  protected void initGoogle() {
    GoogleApiClient.Builder googleBuilder = new GoogleApiClient.Builder(activity);
    googleBuilder.addApi(Plus.API);
    googleBuilder.addScope(Plus.SCOPE_PLUS_LOGIN);
    googleBuilder.addConnectionCallbacks(
        new GoogleApiClient.ConnectionCallbacks() {
          @Override
          public void onConnected(Bundle bundle) {
            Log.i("Menyou", "google connected");
            if (googleLoginListener != null) {
              Person user = Plus.PeopleApi.getCurrentPerson(googleClient);
              Log.i("Menyou", "Google logged in: " + user);
              googleLoginListener.onSuccess(
                  user.getId(), user.getName().getFormatted(), null, null);
              googleLoginListener = null;
            }
          }

          @Override
          public void onConnectionSuspended(int cause) {
            Log.i("Menyou", "google connection suspended: " + cause);
            if (googleLoginListener != null) {
              googleLoginListener.onFailed();
              googleLoginListener = null;
            }
          }
        });
    googleBuilder.addOnConnectionFailedListener(
        new GoogleApiClient.OnConnectionFailedListener() {
          @Override
          public void onConnectionFailed(ConnectionResult result) {
            Log.i("Menyou", "google connection failed: " + result);
            googleConnectionResult = result;
            if (googleLoginListener != null) startGoogleLogin(googleLoginListener);
          }
        });
    googleClient = googleBuilder.build();
  }