/** * 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; }
/** * 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(); }
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(); }
/** 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(); }