Skip to content

Commit 8a9a8e8

Browse files
SUPERCILEXsamtstern
authored andcommitted
[CLEANUP] Refactor various classes (#678)
* Do a whole bunch of refactoring * Use Builder interface in AuthUI.java * Add safety net in IdpResponse.Builder#build() * Fix refactor mistake * Also include GitHub in safety net * Cleanup tests * Fix broken tests * Fix more refactor mistakes * Fix broken test * Cleanup * Remove unnecessary builder interface * Remove lazy enum singleton
1 parent fb2bf1d commit 8a9a8e8

24 files changed

+171
-169
lines changed

app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public void onCreate(Bundle savedInstanceState) {
133133
if (auth.getCurrentUser() != null) {
134134
startSignedInActivity(null);
135135
finish();
136+
return;
136137
}
137138

138139
if (!isGoogleConfigured()) {

app/src/main/java/com/firebase/uidemo/auth/SignedInActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ public static Intent createIntent(
293293
Context context,
294294
IdpResponse idpResponse,
295295
SignedInConfig signedInConfig) {
296-
Intent in = IdpResponse.getIntent(idpResponse);
297-
in.setClass(context, SignedInActivity.class);
298-
in.putExtra(EXTRA_SIGNED_IN_CONFIG, signedInConfig);
299-
return in;
296+
Intent startIntent = idpResponse == null ? new Intent() : idpResponse.toIntent();
297+
298+
return startIntent.setClass(context, SignedInActivity.class)
299+
.putExtra(EXTRA_SIGNED_IN_CONFIG, signedInConfig);
300300
}
301301

302302
@Override

auth/src/main/java/com/firebase/ui/auth/IdpResponse.java

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919
import android.os.Parcelable;
2020
import android.support.annotation.NonNull;
2121
import android.support.annotation.Nullable;
22+
import android.support.annotation.RestrictTo;
23+
import android.text.TextUtils;
2224

2325
import com.firebase.ui.auth.ui.ExtraConstants;
26+
import com.google.firebase.auth.FacebookAuthProvider;
27+
import com.google.firebase.auth.GithubAuthProvider;
28+
import com.google.firebase.auth.GoogleAuthProvider;
29+
import com.google.firebase.auth.TwitterAuthProvider;
2430

2531
/**
2632
* A container that encapsulates the result of authenticating with an Identity Provider.
@@ -36,22 +42,6 @@ private IdpResponse(int errorCode) {
3642
this(null, null, null, null, errorCode);
3743
}
3844

39-
public IdpResponse(@NonNull String providerId, @NonNull String email) {
40-
this(providerId, email, null, null, ResultCodes.OK);
41-
}
42-
43-
public IdpResponse(@NonNull String providerId, @NonNull String email, @NonNull String token) {
44-
this(providerId, email, token, null, ResultCodes.OK);
45-
}
46-
47-
public IdpResponse(
48-
@NonNull String providerId,
49-
@NonNull String email,
50-
@NonNull String token,
51-
@NonNull String secret) {
52-
this(providerId, email, token, secret, ResultCodes.OK);
53-
}
54-
5545
private IdpResponse(
5646
String providerId,
5747
String email,
@@ -80,24 +70,28 @@ public static IdpResponse fromResultIntent(Intent resultIntent) {
8070
}
8171
}
8272

83-
public static Intent getIntent(IdpResponse response) {
84-
return new Intent().putExtra(ExtraConstants.EXTRA_IDP_RESPONSE, response);
73+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
74+
public static Intent getErrorCodeIntent(int errorCode) {
75+
return new IdpResponse(errorCode).toIntent();
8576
}
8677

87-
public static Intent getErrorCodeIntent(int errorCode) {
88-
return getIntent(new IdpResponse(errorCode));
78+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
79+
public Intent toIntent() {
80+
return new Intent().putExtra(ExtraConstants.EXTRA_IDP_RESPONSE, this);
8981
}
9082

9183
/**
9284
* Get the type of provider. e.g. {@link AuthUI#GOOGLE_PROVIDER}
9385
*/
86+
@NonNull
9487
public String getProviderType() {
9588
return mProviderId;
9689
}
9790

9891
/**
9992
* Get the email used to sign in.
10093
*/
94+
@Nullable
10195
public String getEmail() {
10296
return mEmail;
10397
}
@@ -156,4 +150,45 @@ public IdpResponse[] newArray(int size) {
156150
return new IdpResponse[size];
157151
}
158152
};
153+
154+
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
155+
public static class Builder {
156+
private String mProviderId;
157+
private String mEmail;
158+
private String mToken;
159+
private String mSecret;
160+
161+
public Builder(@NonNull String providerId, @Nullable String email) {
162+
mProviderId = providerId;
163+
mEmail = email;
164+
}
165+
166+
public Builder setToken(String token) {
167+
mToken = token;
168+
return this;
169+
}
170+
171+
public Builder setSecret(String secret) {
172+
mSecret = secret;
173+
return this;
174+
}
175+
176+
public IdpResponse build() {
177+
if ((mProviderId.equalsIgnoreCase(GoogleAuthProvider.PROVIDER_ID)
178+
|| mProviderId.equalsIgnoreCase(FacebookAuthProvider.PROVIDER_ID)
179+
|| mProviderId.equalsIgnoreCase(TwitterAuthProvider.PROVIDER_ID)
180+
|| mProviderId.equalsIgnoreCase(GithubAuthProvider.PROVIDER_ID))
181+
&& TextUtils.isEmpty(mToken)) {
182+
throw new IllegalStateException(
183+
"Token cannot be null when using a non-email provider.");
184+
}
185+
if (mProviderId.equalsIgnoreCase(TwitterAuthProvider.PROVIDER_ID)
186+
&& TextUtils.isEmpty(mSecret)) {
187+
throw new IllegalStateException(
188+
"Secret cannot be null when using the Twitter provider.");
189+
}
190+
191+
return new IdpResponse(mProviderId, mEmail, mToken, mSecret, ResultCodes.OK);
192+
}
193+
}
159194
}

auth/src/main/java/com/firebase/ui/auth/provider/FacebookProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.Intent;
2020
import android.os.Bundle;
2121
import android.support.annotation.LayoutRes;
22+
import android.support.annotation.Nullable;
2223
import android.support.annotation.StyleRes;
2324
import android.util.Log;
2425

@@ -182,18 +183,17 @@ public void onError(FacebookException error) {
182183
onFailure(extra);
183184
}
184185

185-
private IdpResponse createIdpResponse(String email, LoginResult loginResult) {
186-
return new IdpResponse(
187-
FacebookAuthProvider.PROVIDER_ID,
188-
email,
189-
loginResult.getAccessToken().getToken());
190-
}
191-
192-
private void onSuccess(String email, LoginResult loginResult) {
186+
private void onSuccess(@Nullable String email, LoginResult loginResult) {
193187
gcCallbackManager();
194188
mCallbackObject.onSuccess(createIdpResponse(email, loginResult));
195189
}
196190

191+
private IdpResponse createIdpResponse(@Nullable String email, LoginResult loginResult) {
192+
return new IdpResponse.Builder(FacebookAuthProvider.PROVIDER_ID, email)
193+
.setToken(loginResult.getAccessToken().getToken())
194+
.build();
195+
}
196+
197197
private void onFailure(Bundle bundle) {
198198
gcCallbackManager();
199199
mCallbackObject.onFailure(bundle);

auth/src/main/java/com/firebase/ui/auth/provider/GoogleProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ public void disconnect() {
127127
}
128128

129129
private IdpResponse createIdpResponse(GoogleSignInAccount account) {
130-
return new IdpResponse(
131-
GoogleAuthProvider.PROVIDER_ID, account.getEmail(), account.getIdToken());
130+
return new IdpResponse.Builder(GoogleAuthProvider.PROVIDER_ID, account.getEmail())
131+
.setToken(account.getIdToken())
132+
.build();
132133
}
133134

134135
@Override

auth/src/main/java/com/firebase/ui/auth/provider/TwitterProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,10 @@ private void onSuccess(IdpResponse response) {
120120
}
121121

122122
private IdpResponse createIdpResponse(String email) {
123-
return new IdpResponse(
124-
TwitterAuthProvider.PROVIDER_ID,
125-
email,
126-
mTwitterSession.getAuthToken().token,
127-
mTwitterSession.getAuthToken().secret);
123+
return new IdpResponse.Builder(TwitterAuthProvider.PROVIDER_ID, email)
124+
.setToken(mTwitterSession.getAuthToken().token)
125+
.setSecret(mTwitterSession.getAuthToken().secret)
126+
.build();
128127
}
129128
}
130129
}

auth/src/main/java/com/firebase/ui/auth/ui/BaseHelper.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,8 @@ public boolean isProgressDialogShowing() {
8080
return mProgressDialog != null && mProgressDialog.isShowing();
8181
}
8282

83-
public String getAppName() {
84-
return mFlowParams.appName;
85-
}
86-
87-
public FirebaseApp getFirebaseApp() {
88-
return FirebaseApp.getInstance(mFlowParams.appName);
89-
}
90-
9183
public FirebaseAuth getFirebaseAuth() {
92-
return FirebaseAuth.getInstance(getFirebaseApp());
84+
return FirebaseAuth.getInstance(FirebaseApp.getInstance(mFlowParams.appName));
9385
}
9486

9587
public CredentialsApi getCredentialsApi() {
@@ -104,22 +96,14 @@ public SaveSmartLock getSaveSmartLockInstance(FragmentActivity activity) {
10496
return SaveSmartLock.getInstance(activity, getFlowParams());
10597
}
10698

107-
public void saveCredentialsOrFinish(
108-
@Nullable SaveSmartLock saveSmartLock,
109-
Activity activity,
110-
FirebaseUser firebaseUser,
111-
@NonNull IdpResponse response) {
112-
saveCredentialsOrFinish(saveSmartLock, activity, firebaseUser, null, response);
113-
}
114-
11599
public void saveCredentialsOrFinish(
116100
@Nullable SaveSmartLock saveSmartLock,
117101
Activity activity,
118102
FirebaseUser firebaseUser,
119103
@Nullable String password,
120104
IdpResponse response) {
121105
if (saveSmartLock == null) {
122-
finishActivity(activity, ResultCodes.OK, IdpResponse.getIntent(response));
106+
finishActivity(activity, ResultCodes.OK, response.toIntent());
123107
} else {
124108
saveSmartLock.saveCredentialsOrFinish(
125109
firebaseUser,

auth/src/main/java/com/firebase/ui/auth/ui/FlowParameters.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@ public class FlowParameters implements Parcelable {
5252

5353
public final boolean allowNewEmailAccounts;
5454

55-
public final boolean isReauth;
56-
5755
@Nullable
5856
public final String reauthReason;
59-
57+
public final boolean isReauth;
6058

6159
public FlowParameters(
6260
@NonNull String appName,

auth/src/main/java/com/firebase/ui/auth/ui/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
7979
dest.writeParcelable(mPhotoUri, flags);
8080
}
8181

82-
public static final class Builder {
82+
public static class Builder {
8383
private String mEmail;
8484
private String mName;
8585
private String mProvider;

auth/src/main/java/com/firebase/ui/auth/ui/accountlink/WelcomeBackIdpPrompt.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public void onSuccess(AuthResult result) {
161161
TAG, "Error signing in with previous credential " + idpResponse.getProviderType()))
162162
.addOnCompleteListener(new FinishListener(idpResponse));
163163
} else {
164-
finish(ResultCodes.OK, IdpResponse.getIntent(idpResponse));
164+
finish(ResultCodes.OK, idpResponse.toIntent());
165165
}
166166
}
167167
})
@@ -200,7 +200,7 @@ private class FinishListener implements OnCompleteListener<AuthResult> {
200200
}
201201

202202
public void onComplete(@NonNull Task task) {
203-
finish(ResultCodes.OK, IdpResponse.getIntent(mIdpResponse));
203+
finish(ResultCodes.OK, mIdpResponse.toIntent());
204204
}
205205
}
206206
}

auth/src/main/java/com/firebase/ui/auth/ui/accountlink/WelcomeBackPasswordPrompt.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ public void onSuccess(AuthResult authResult) {
168168
mSaveSmartLock,
169169
authResult.getUser(),
170170
password,
171-
new IdpResponse(EmailAuthProvider.PROVIDER_ID, email));
171+
new IdpResponse.Builder(EmailAuthProvider.PROVIDER_ID, email)
172+
.build());
172173
} else {
173174
authResult.getUser()
174175
.linkWithCredential(authCredential)

auth/src/main/java/com/firebase/ui/auth/ui/email/RegisterEmailActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void onExistingEmailUser(User user) {
102102
WelcomeBackPasswordPrompt.createIntent(
103103
this,
104104
mActivityHelper.getFlowParams(),
105-
new IdpResponse(EmailAuthProvider.PROVIDER_ID, user.getEmail())),
105+
new IdpResponse.Builder(EmailAuthProvider.PROVIDER_ID, user.getEmail()).build()),
106106
RC_SIGN_IN);
107107

108108
setSlideAnimation();
@@ -115,7 +115,7 @@ public void onExistingIdpUser(User user) {
115115
this,
116116
mActivityHelper.getFlowParams(),
117117
user,
118-
new IdpResponse(EmailAuthProvider.PROVIDER_ID, user.getEmail()));
118+
new IdpResponse.Builder(EmailAuthProvider.PROVIDER_ID, user.getEmail()).build());
119119
mActivityHelper.startActivityForResult(intent, RC_WELCOME_BACK_IDP);
120120

121121
setSlideAnimation();

auth/src/main/java/com/firebase/ui/auth/ui/email/RegisterEmailFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ public void onComplete(@NonNull Task<Void> task) {
276276
getActivity(),
277277
user,
278278
password,
279-
new IdpResponse(EmailAuthProvider.PROVIDER_ID,
280-
email));
279+
new IdpResponse.Builder(EmailAuthProvider.PROVIDER_ID, email)
280+
.build());
281281
}
282282
});
283283
}

auth/src/main/java/com/firebase/ui/auth/ui/idp/CredentialSignInHandler.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ public class CredentialSignInHandler implements OnCompleteListener<AuthResult> {
4848
@Nullable
4949
private SaveSmartLock mSmartLock;
5050
private IdpResponse mResponse;
51-
private int mAccountLinkResultCode;
51+
private int mAccountLinkRequestCode;
5252

5353
public CredentialSignInHandler(
5454
Activity activity,
5555
BaseHelper helper,
5656
@Nullable SaveSmartLock smartLock,
57-
int accountLinkResultCode,
57+
int accountLinkRequestCode,
5858
IdpResponse response) {
5959
mActivity = activity;
6060
mHelper = helper;
6161
mSmartLock = smartLock;
6262
mResponse = response;
63-
mAccountLinkResultCode = accountLinkResultCode;
63+
mAccountLinkRequestCode = accountLinkRequestCode;
6464
}
6565

6666
@Override
@@ -71,6 +71,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
7171
mSmartLock,
7272
mActivity,
7373
firebaseUser,
74+
null,
7475
mResponse);
7576
} else {
7677
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
@@ -93,9 +94,13 @@ public void onFailure(@NonNull Exception e) {
9394
return;
9495
}
9596
} else {
96-
Log.e(TAG, "Unexpected exception when signing in with credential " + mResponse.getProviderType() + " unsuccessful. Visit https://console.firebase.google.com to enable it.",
97+
Log.e(TAG,
98+
"Unexpected exception when signing in with credential "
99+
+ mResponse.getProviderType()
100+
+ " unsuccessful. Visit https://console.firebase.google.com to enable it.",
97101
task.getException());
98102
}
103+
99104
mHelper.dismissDialog();
100105
}
101106
}
@@ -113,9 +118,9 @@ public void onSuccess(@NonNull ProviderQueryResult result) {
113118
mActivity,
114119
mHelper.getFlowParams(),
115120
mResponse
116-
), mAccountLinkResultCode);
121+
), mAccountLinkRequestCode);
117122
} else {
118-
// Start IDP welcome back flow
123+
// Start Idp welcome back flow
119124
mActivity.startActivityForResult(
120125
WelcomeBackIdpPrompt.createIntent(
121126
mActivity,
@@ -124,7 +129,7 @@ public void onSuccess(@NonNull ProviderQueryResult result) {
124129
.setProvider(provider)
125130
.build(),
126131
mResponse
127-
), mAccountLinkResultCode);
132+
), mAccountLinkRequestCode);
128133
}
129134
}
130135
}

auth/src/main/java/com/firebase/ui/auth/util/signincontainer/SaveSmartLock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
184184
}
185185

186186
private void finish() {
187-
finish(ResultCodes.OK, IdpResponse.getIntent(mResponse));
187+
finish(ResultCodes.OK, mResponse.toIntent());
188188
}
189189

190190
/**

0 commit comments

Comments
 (0)