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