2929import com .google .android .gms .auth .api .credentials .CredentialRequestResponse ;
3030import com .google .android .gms .auth .api .credentials .CredentialsClient ;
3131import com .google .android .gms .auth .api .credentials .IdentityProviders ;
32+ import com .google .android .gms .common .api .CommonStatusCodes ;
3233import com .google .android .gms .common .api .ResolvableApiException ;
3334import com .google .android .gms .tasks .OnCompleteListener ;
3435import com .google .android .gms .tasks .OnFailureListener ;
@@ -93,14 +94,26 @@ public void onCreate(Bundle savedInstance) {
9394 }
9495
9596 FlowParameters flowParams = getFlowParams ();
96- if (flowParams .enableCredentials ) {
97+
98+ // Only support password credentials if email auth is enabled
99+ boolean supportPasswords = false ;
100+ for (AuthUI .IdpConfig config : flowParams .providerInfo ) {
101+ if (EmailAuthProvider .PROVIDER_ID .equals (config .getProviderId ())) {
102+ supportPasswords = true ;
103+ }
104+ }
105+ List <String > accountTypes = getSupportedAccountTypes ();
106+
107+ // If the request will be empty, avoid the step entirely
108+ boolean willRequestCredentials = supportPasswords || accountTypes .size () > 0 ;
109+
110+ if (flowParams .enableCredentials && willRequestCredentials ) {
97111 getDialogHolder ().showLoadingDialog (R .string .fui_progress_dialog_loading );
98112
99113 mCredentialsClient = GoogleApiUtils .getCredentialsClient (getActivity ());
100-
101114 mCredentialsClient .request (new CredentialRequest .Builder ()
102- .setPasswordLoginSupported (true )
103- .setAccountTypes (getSupportedAccountTypes () .toArray (new String [0 ]))
115+ .setPasswordLoginSupported (supportPasswords )
116+ .setAccountTypes (accountTypes .toArray (new String [accountTypes . size () ]))
104117 .build ())
105118 .addOnCompleteListener (this );
106119 } else {
@@ -109,7 +122,7 @@ public void onCreate(Bundle savedInstance) {
109122 }
110123
111124 @ Override
112- public void onSaveInstanceState (Bundle outState ) {
125+ public void onSaveInstanceState (@ NonNull Bundle outState ) {
113126 // It doesn't matter what we put here, we just don't want outState to be empty
114127 outState .putBoolean (ExtraConstants .HAS_EXISTING_INSTANCE , true );
115128 super .onSaveInstanceState (outState );
@@ -122,19 +135,19 @@ public void onComplete(@NonNull Task<CredentialRequestResponse> task) {
122135 // Auto sign-in success
123136 handleCredential (task .getResult ().getCredential ());
124137 return ;
125- } else {
126- if ( task .getException () instanceof ResolvableApiException ) {
127- ResolvableApiException rae = ( ResolvableApiException ) task . getException ();
138+ } else if ( task . getException () instanceof ResolvableApiException ) {
139+ ResolvableApiException rae = ( ResolvableApiException ) task .getException ();
140+ if ( rae . getStatusCode () == CommonStatusCodes . RESOLUTION_REQUIRED ) {
128141 try {
129142 startIntentSenderForResult (rae .getResolution ().getIntentSender (),
130143 RC_CREDENTIALS_READ );
131144 return ;
132145 } catch (IntentSender .SendIntentException e ) {
133146 Log .e (TAG , "Failed to send Credentials intent." , e );
134147 }
135- } else {
136- Log .e (TAG , "Non-resolvable exception:\n " + task .getException ());
137148 }
149+ } else {
150+ Log .e (TAG , "Non-resolvable exception:\n " + task .getException ());
138151 }
139152 startAuthMethodChoice ();
140153 }
0 commit comments