Search code examples
javaandroidgoogle-drive-android-api

Screen dims for a second while connecting to Google Drive using GDAA


As the title explains, this is the only problem that occurs while connecting to Google Drive. It connects and creates files successfully but, getting a dark screen by each request is not really favourable. I've tested it on several real devices and got the same result. Can this problem be solved? Any suggestion is appreciated. Here is my code:

mGoogleSignInClient = buildGoogleSignInClient();
startActivityForResult(mGoogleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);

Build Google Drive SignIn client:

  private GoogleSignInClient buildGoogleSignInClient () {
    Log.i(TAG, "signIn build");
    GoogleSignInOptions signInOptions =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(Drive.SCOPE_FILE)
                    .build();
    return GoogleSignIn.getClient(this, signInOptions);
}

onActivityResult:

 @Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case COMPLETE_AUTHORIZATION_REQUEST_CODE:
            // Called after user is signed in.
            if (resultCode == RESULT_OK) {
                Log.i(TAG, "Signed in successfully.");
                mDriveClient = Drive.getDriveClient(this, GoogleSignIn.getLastSignedInAccount(this));
                mDriveResourceClient = Drive.getDriveResourceClient(this, GoogleSignIn.getLastSignedInAccount(this));

                createDriveFile();
            }
            break;
    }
}

Solution

  • After a long search I came up with this solution:

    public Task<GoogleSignInAccount> silentSignIn ()
    

    By using this method the user will be signed in silently and thereby the screen will remain unchanged. Here is the full functioning code:

    Task<GoogleSignInAccount> task = googleSignInClient.silentSignIn();
    if (task.isSuccessful()) {
            GoogleSignInAccount signInAccount = task.getResult();
            Log.i(TAG, "immediate result available (silentSignIn)");
            updateViewWithGoogleSignIn(signInAccount);
        } else {
            // There's no immediate result ready, displays some progress indicator and waits for the
            // async callback.
            task.addOnCompleteListener(new OnCompleteListener<GoogleSignInAccount>() {
                @Override
                public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                    try {
                        GoogleSignInAccount signInAccount = task.getResult(ApiException.class);
                        updateViewWithGoogleSignIn(signInAccount);
                    } catch (ApiException apiException) {
                        // You can get from apiException.getStatusCode() the detailed error code
                        // e.g. GoogleSignInStatusCodes.SIGN_IN_REQUIRED means user needs to take
                        // explicit action to finish sign-in;
                        // Please refer to GoogleSignInStatusCodes Javadoc for details
                        updateButtonsAndStatusFromErrorCode(apiException.getStatusCode());
                    }
                }
            });
        }
    

    Get access to Drive contents and do the necessary tasks:

    private void updateViewWithGoogleSignIn(GoogleSignInAccount signInAccount) {
        // Build a drive client.
        mDriveClient = Drive.getDriveClient(getApplicationContext(), signInAccount);
        // Build a drive resource client.
        mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount);
    
        // TODO:
    }
    

    For more information about silent sign in: documentation