Search in sources :

Example 1 with UserRecoverableAuthException

use of com.google.android.gms.auth.UserRecoverableAuthException in project AnyMemo by helloworld1.

the class GoogleAccountActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result == null) {
            activityComponents().errorUtil().showFatalError(getString(R.string.google_sign_in_empty_error_text), null);
            return;
        }
        if (!result.isSuccess()) {
            activityComponents().errorUtil().showFatalError(getString(R.string.google_sign_in_not_successful_error_text), null);
            return;
        }
        GoogleSignInAccount acct = result.getSignInAccount();
        if (acct == null) {
            activityComponents().errorUtil().showFatalError(getString(R.string.google_sign_in_account_empty_result), null);
            return;
        }
        // Get account information
        final String email = acct.getEmail();
        appComponents().executorService().submit(new Runnable() {

            @Override
            public void run() {
                try {
                    String token = GoogleAuthUtil.getToken(GoogleAccountActivity.this, email, AMEnv.GDRIVE_SCOPE);
                    onAuthenticated(token);
                } catch (IOException e) {
                    activityComponents().errorUtil().showFatalError("IO Error", e);
                } catch (UserRecoverableAuthException e) {
                    startActivityForResult(e.getIntent(), RC_AUTH_TOKEN);
                } catch (GoogleAuthException e) {
                    activityComponents().errorUtil().showFatalError("GoogleAuthException", e);
                }
            }
        });
    } else if (requestCode == RC_AUTH_TOKEN) {
        Bundle extra = data.getExtras();
        if (extra == null) {
            activityComponents().errorUtil().showFatalError("RC_AUTH_TOKEN does not have extra", null);
            return;
        }
        String token = extra.getString("authtoken");
        if (Strings.isNullOrEmpty(token)) {
            activityComponents().errorUtil().showFatalError("RC_AUTH_TOKEN does not have token", null);
            return;
        }
        onAuthenticated(token);
    }
}
Also used : GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) Bundle(android.os.Bundle) GoogleAuthException(com.google.android.gms.auth.GoogleAuthException) IOException(java.io.IOException) UserRecoverableAuthException(com.google.android.gms.auth.UserRecoverableAuthException) GoogleSignInResult(com.google.android.gms.auth.api.signin.GoogleSignInResult)

Aggregations

Bundle (android.os.Bundle)1 GoogleAuthException (com.google.android.gms.auth.GoogleAuthException)1 UserRecoverableAuthException (com.google.android.gms.auth.UserRecoverableAuthException)1 GoogleSignInAccount (com.google.android.gms.auth.api.signin.GoogleSignInAccount)1 GoogleSignInResult (com.google.android.gms.auth.api.signin.GoogleSignInResult)1 IOException (java.io.IOException)1