29
29
import com .google .android .gms .auth .api .credentials .CredentialRequestResponse ;
30
30
import com .google .android .gms .auth .api .credentials .CredentialsClient ;
31
31
import com .google .android .gms .auth .api .credentials .IdentityProviders ;
32
+ import com .google .android .gms .common .api .CommonStatusCodes ;
32
33
import com .google .android .gms .common .api .ResolvableApiException ;
33
34
import com .google .android .gms .tasks .OnCompleteListener ;
34
35
import com .google .android .gms .tasks .OnFailureListener ;
@@ -93,14 +94,26 @@ public void onCreate(Bundle savedInstance) {
93
94
}
94
95
95
96
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 ) {
97
111
getDialogHolder ().showLoadingDialog (R .string .fui_progress_dialog_loading );
98
112
99
113
mCredentialsClient = GoogleApiUtils .getCredentialsClient (getActivity ());
100
-
101
114
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 () ]))
104
117
.build ())
105
118
.addOnCompleteListener (this );
106
119
} else {
@@ -109,7 +122,7 @@ public void onCreate(Bundle savedInstance) {
109
122
}
110
123
111
124
@ Override
112
- public void onSaveInstanceState (Bundle outState ) {
125
+ public void onSaveInstanceState (@ NonNull Bundle outState ) {
113
126
// It doesn't matter what we put here, we just don't want outState to be empty
114
127
outState .putBoolean (ExtraConstants .HAS_EXISTING_INSTANCE , true );
115
128
super .onSaveInstanceState (outState );
@@ -122,19 +135,19 @@ public void onComplete(@NonNull Task<CredentialRequestResponse> task) {
122
135
// Auto sign-in success
123
136
handleCredential (task .getResult ().getCredential ());
124
137
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 ) {
128
141
try {
129
142
startIntentSenderForResult (rae .getResolution ().getIntentSender (),
130
143
RC_CREDENTIALS_READ );
131
144
return ;
132
145
} catch (IntentSender .SendIntentException e ) {
133
146
Log .e (TAG , "Failed to send Credentials intent." , e );
134
147
}
135
- } else {
136
- Log .e (TAG , "Non-resolvable exception:\n " + task .getException ());
137
148
}
149
+ } else {
150
+ Log .e (TAG , "Non-resolvable exception:\n " + task .getException ());
138
151
}
139
152
startAuthMethodChoice ();
140
153
}
0 commit comments