Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I switch accounts under the NEW Google Drive Android API

My authorization flow in the new Google Drive Android API is as follows:

  1. Menu: SELECT ACCOUNT
  2. connect();
  3. onConnectionFailed() result.startResolutionForResult() invokes AccountSelectDialog / DriveAuthorization
  4. onConnected() do your stuff

Works like a charm. Now repeating with the aim to switch accounts:

  1. Menu: SELECT ACCOUNT
  2. connect();
  3. onConnected()

Here, I have no chance to get to the AccountSelectDialog since I never get onConnectionFailed() with 'result' to invoke startResolutionForResult(). What am I missing this time?

like image 292
seanpj Avatar asked Feb 06 '14 17:02

seanpj


2 Answers

First, add the Plus.API:

mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Drive.API).addApi(Plus.API).addScope(Drive.SCOPE_APPFOLDER).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();

Then you can switch accounts like this:

public void onClick(View view) {
  if (view.getId() == R.id.sign_out_button) {
    if (mGoogleApiClient.isConnected()) {
      Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
      mGoogleApiClient.disconnect();
      mGoogleApiClient.connect();
    }
  }
}

For more, see here.

like image 200
Mark Avatar answered Oct 07 '22 07:10

Mark


Just call

mGoogleApiClient.clearDefaultAccountAndReconnect();

have a look at the docs.

This will call the onConnectionFailed callback that will present the layout to choose among the available Google accounts:

@Override
public void onConnectionFailed(ConnectionResult connectionResult) 
{
    if (connectionResult.hasResolution()) {
        try {                                              
            connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
            // Unable to resolve, message user appropriately
        }
    } else {                                           
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
    }

}
like image 41
Xavi Gil Avatar answered Oct 07 '22 07:10

Xavi Gil