2

Currently, I'm migrating from API Client Library for Java to Drive API for Android

However, compared with API Client Library for Java's, I realize the account picker from Drive API for Android are very slow. Sometimes, it takes up to 5 seconds waiting time.

Very fast (API Client Library for Java)

private static final GoogleAccountCredential googleAccountCredential = GoogleAccountCredential.usingOAuth2(context, 
    Arrays.asList(
        DriveScopes.DRIVE_APPDATA,
        // Legacy. Shall be removed after a while...
        DriveScopes.DRIVE
    )
);

startActivityForResult(googleAccountCredential.newChooseAccountIntent(), RequestCode.REQUEST_ACCOUNT_PICKER_LOAD_FROM_CLOUD);

enter image description here

This account picker appears almost instantly.


Very slow (Drive API for Android)

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();

mGoogleApiClient.connect();

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
    if (!result.hasResolution()) {
        // show the localized error dialog.
        GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
        return;
    }
    try {
        result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
    } catch (SendIntentException e) {
        Log.e(TAG, "Exception while starting resolution activity", e);
    }
}

enter image description here

Usually, when I invoke the account picker for the first time, it takes quite a long time to wait. Sometimes, the waiting time can take up to 5 seconds.

I understand that our application code invoke Google Play Services via IPC. Hence, there might be some slowness. However, I don't expect it to be as slow as up to 5 seconds.

Is there any thing I can do, to make account picker UI appear as fast as possible?

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • It has been discussed [here already](http://stackoverflow.com/questions/34701380/how-can-i-enforce-googleapiclient-to-prompt-account-chooser-ui-each-time-i-call/34706726#34706726). Try to use account picker (startActivityForResult(....)) with 'setAccountName(email)'. I did not experience any delays. – seanpj Jan 11 '16 at 14:18
  • Thanks. I also don't experience delay by using account picker from `startActivityForResult`. Have you ever try to use `result.startResolutionForResult` to show account picker? Do you experience any delay? As I'm not sure whether the delay is only happen in my device, or it is a common case. Thanks. – Cheok Yan Cheng Jan 11 '16 at 18:33

2 Answers2

3

As described by @seanpj in https://stackoverflow.com/a/34706726/72437

startActivityForResult(AccountPicker.newChooseAccountIntent(null,
    null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null),
    REQ_ACCPICK);

will be much faster.

One of the reason, it doesn't show your profile picture in the pop up dialog. I believe those profile pictures required network activities, which make the process slow.

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
1

Please try the following (I don't use addScope):

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
        .requestEmail()
        .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .addApi(Drive.API)
        .build();

The account picker appears fast (my phone: Motorola, API 16).

BNK
  • 23,994
  • 8
  • 77
  • 87
  • Thanks. Going to try it tonight tonight. I was wondering can you help to experiment my code? As I wish to know whether the slowness is only happening in my own device (Nexus 5), or it happens in most of the devices? – Cheok Yan Cheng Jan 11 '16 at 02:34
  • I tried your code, nothing display, except using `.enableAutoManage`. The code I tried `mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) .addApi(Drive.API) .addScope(Drive.SCOPE_APPFOLDER) //.addConnectionCallbacks(this) //.addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect();`. It displays fast too. – BNK Jan 11 '16 at 02:51
  • That's pretty weird. Maybe you need to wait for longer time like 10 seconds? (Yes. It takes that long sometimes) As, the code is merely copy n paste from official Android Drive API example - https://github.com/googledrive/android-demos/blob/master/app/src/main/java/com/google/android/gms/drive/sample/demo/BaseDemoActivity.java#L80 – Cheok Yan Cheng Jan 11 '16 at 02:54
  • Opps sorry. I forget, the UI is triggered by `result.startResolutionForResult`. I had modified the code. – Cheok Yan Cheng Jan 11 '16 at 02:56
  • I tried your modified code, the picker displays fast too, IMO, you can test with some other phones or perhaps even another faster network (wifi, 3G)? :) – BNK Jan 11 '16 at 04:19