Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Sign-In error on cancel

I'm having some weird issue which I haven't been able to resolve. I have an Android app that has the Google sign-in. The app is following the MVP pattern and uses fragments instead of activities. The log-in part is from here: https://developers.google.com/identity/sign-in/android/

It works all fine until I try to dismiss the sign-in dialog (where you select the user) by tapping outside of it. I would assume that it should give me a status code 12501 (SIGN_IN_CANCELLED) but instead I'm getting code 13 (ERROR). Is my assumption wrong? Should I just ignore the code 13 and not show the user any error if it occurs?

Below is the most relevant code

Activity:

public class SignInActivity extends AppCompatActivity implements SignInFragment.SignInListener {    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

        // Add sign in fragment
        mSignInFragment = new SignInFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .replace(R.id.sign_in_frame, mSignInFragment, TAG)
                .commit();
    }

    @Override
    public void startSignIn() {
        Intent signInIntent = mSignInFragment.getGoogleSignInClient().getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            mSignInFragment.handleSignInResult(task);
        }
    }
}

Fragment:

public class SignInFragment extends DaggerFragment implements SignInContract.View, GoogleApiClient.OnConnectionFailedListener {

    private SignInFragment.SignInListener signInListener;

    public interface SignInListener {
        void startSignIn();
        void startMainActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken( clientId )
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(getActivity(), gso);

        // Calling this to force user select dialog to appear everytime
        mGoogleSignInClient.signOut();

        signInButton.setOnClickListener(v -> {
            signInListener.startSignIn();
        }
    }

    protected void handleSignInResult(Task<GoogleSignInAccount> task) {
        try {
            GoogleSignInAccount acct = task.getResult(ApiException.class);
            String idToken = acct.getIdToken();
            presenter.deliverTokenToServer(idToken);

        } catch (ApiException e) {
            e.printStackTrace();
            Log.d(TAG, "handleSignInResult:" + e.getStatusCode());
            Snackbar snackbar = Snackbar.make(getView(), R.string.error_login_failed_try_again, Snackbar.LENGTH_LONG);
            snackbar.show();

        }
    }
}

In the gradle files I've used 'com.google.android.gms:play-services-auth:15.0.1' and 'com.google.gms:google-services:4.0.1'

like image 265
fnx Avatar asked Aug 02 '18 14:08

fnx


1 Answers

I finally found it. It's a regression in Google Play libraries. And it's fixed in play-services-auth:16.0.0. Here is some details.

implementation com.google.android.gms:play-services-auth:16.0.0
like image 68
Aleksei Potapkin Avatar answered Sep 28 '22 22:09

Aleksei Potapkin