23
23
import android .support .v4 .app .FragmentActivity ;
24
24
import android .text .TextUtils ;
25
25
import android .util .Log ;
26
- import android .view .View ;
27
- import android .view .View .OnClickListener ;
28
26
29
27
import com .firebase .ui .auth .AuthUI .IdpConfig ;
30
28
import com .firebase .ui .auth .IdpResponse ;
35
33
import com .google .android .gms .auth .api .signin .GoogleSignInOptions ;
36
34
import com .google .android .gms .auth .api .signin .GoogleSignInResult ;
37
35
import com .google .android .gms .common .ConnectionResult ;
36
+ import com .google .android .gms .common .api .CommonStatusCodes ;
38
37
import com .google .android .gms .common .api .GoogleApiClient ;
39
38
import com .google .android .gms .common .api .Scope ;
39
+ import com .google .android .gms .common .api .Status ;
40
40
import com .google .firebase .auth .AuthCredential ;
41
41
import com .google .firebase .auth .GoogleAuthProvider ;
42
42
43
- public class GoogleProvider implements
44
- IdpProvider , OnClickListener , GoogleApiClient .OnConnectionFailedListener {
43
+ public class GoogleProvider implements IdpProvider , GoogleApiClient .OnConnectionFailedListener {
45
44
private static final String TAG = "GoogleProvider" ;
46
45
private static final int RC_SIGN_IN = 20 ;
47
46
private static final String ERROR_KEY = "error" ;
48
47
49
48
private GoogleApiClient mGoogleApiClient ;
50
- private Activity mActivity ;
51
- private IdpCallback mIDPCallback ;
49
+ private FragmentActivity mActivity ;
50
+ private IdpConfig mIdpConfig ;
51
+ private IdpCallback mIdpCallback ;
52
52
53
53
public GoogleProvider (FragmentActivity activity , IdpConfig idpConfig ) {
54
54
this (activity , idpConfig , null );
55
55
}
56
56
57
57
public GoogleProvider (FragmentActivity activity , IdpConfig idpConfig , @ Nullable String email ) {
58
58
mActivity = activity ;
59
- String clientId = activity .getString (R .string .default_web_client_id );
59
+ mIdpConfig = idpConfig ;
60
+
61
+ mGoogleApiClient = new GoogleApiClient .Builder (mActivity )
62
+ .enableAutoManage (mActivity , GoogleApiHelper .getSafeAutoManageId (), this )
63
+ .addApi (Auth .GOOGLE_SIGN_IN_API , getSignInOptions (email ))
64
+ .build ();
65
+ }
66
+
67
+ public static AuthCredential createAuthCredential (IdpResponse response ) {
68
+ return GoogleAuthProvider .getCredential (response .getIdpToken (), null );
69
+ }
70
+
71
+ private GoogleSignInOptions getSignInOptions (@ Nullable String email ) {
72
+ String clientId = mActivity .getString (R .string .default_web_client_id );
60
73
61
74
GoogleSignInOptions .Builder builder =
62
75
new GoogleSignInOptions .Builder (GoogleSignInOptions .DEFAULT_SIGN_IN )
63
76
.requestEmail ()
64
77
.requestIdToken (clientId );
65
78
66
- if (activity .getResources ().getIdentifier (
67
- "google_permissions" , "array" , activity .getPackageName ()) != 0 ) {
79
+ if (mActivity .getResources ().getIdentifier (
80
+ "google_permissions" , "array" , mActivity .getPackageName ()) != 0 ) {
68
81
Log .w (TAG , "DEVELOPER WARNING: You have defined R.array.google_permissions but that is"
69
82
+ " no longer respected as of FirebaseUI 1.0.0. Please see README for IDP scope"
70
83
+ " configuration instructions." );
71
84
}
72
85
73
86
// Add additional scopes
74
- for (String scopeString : idpConfig .getScopes ()) {
87
+ for (String scopeString : mIdpConfig .getScopes ()) {
75
88
builder .requestScopes (new Scope (scopeString ));
76
89
}
77
90
78
91
if (!TextUtils .isEmpty (email )) {
79
92
builder .setAccountName (email );
80
93
}
81
94
82
- mGoogleApiClient = new GoogleApiClient .Builder (activity )
83
- .enableAutoManage (activity , GoogleApiHelper .getSafeAutoManageId (), this )
84
- .addApi (Auth .GOOGLE_SIGN_IN_API , builder .build ())
85
- .build ();
86
- }
87
-
88
- public static AuthCredential createAuthCredential (IdpResponse response ) {
89
- return GoogleAuthProvider .getCredential (response .getIdpToken (), null );
95
+ return builder .build ();
90
96
}
91
97
92
98
public String getName (Context context ) {
@@ -100,7 +106,7 @@ public String getProviderId() {
100
106
101
107
@ Override
102
108
public void setAuthenticationCallback (IdpCallback callback ) {
103
- mIDPCallback = callback ;
109
+ mIdpCallback = callback ;
104
110
}
105
111
106
112
public void disconnect () {
@@ -121,7 +127,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
121
127
GoogleSignInResult result = Auth .GoogleSignInApi .getSignInResultFromIntent (data );
122
128
if (result != null ) {
123
129
if (result .isSuccess ()) {
124
- mIDPCallback .onSuccess (createIdpResponse (result .getSignInAccount ()));
130
+ mIdpCallback .onSuccess (createIdpResponse (result .getSignInAccount ()));
125
131
} else {
126
132
onError (result );
127
133
}
@@ -138,21 +144,26 @@ public void startLogin(Activity activity) {
138
144
}
139
145
140
146
private void onError (GoogleSignInResult result ) {
141
- String errorMessage = result .getStatus ().getStatusMessage ();
142
- onError (result .getStatus ().getStatusCode () + " " + errorMessage );
147
+ Status status = result .getStatus ();
148
+
149
+ if (status .getStatusCode () == CommonStatusCodes .INVALID_ACCOUNT ) {
150
+ mGoogleApiClient .stopAutoManage (mActivity );
151
+ mGoogleApiClient .disconnect ();
152
+ mGoogleApiClient = new GoogleApiClient .Builder (mActivity )
153
+ .enableAutoManage (mActivity , GoogleApiHelper .getSafeAutoManageId (), this )
154
+ .addApi (Auth .GOOGLE_SIGN_IN_API , getSignInOptions (null ))
155
+ .build ();
156
+ startLogin (mActivity );
157
+ } else {
158
+ onError (status .getStatusCode () + " " + status .getStatusMessage ());
159
+ }
143
160
}
144
161
145
162
private void onError (String errorMessage ) {
146
163
Log .e (TAG , "Error logging in with Google. " + errorMessage );
147
164
Bundle extra = new Bundle ();
148
165
extra .putString (ERROR_KEY , errorMessage );
149
- mIDPCallback .onFailure (extra );
150
- }
151
-
152
- @ Override
153
- public void onClick (View view ) {
154
- Auth .GoogleSignInApi .signOut (mGoogleApiClient );
155
- startLogin (mActivity );
166
+ mIdpCallback .onFailure (extra );
156
167
}
157
168
158
169
@ Override
0 commit comments