Skip to content

Commit cd42ad7

Browse files
committed
Cleans up BaseActivity and refactors Google*.java
1 parent 17c2f9b commit cd42ad7

File tree

11 files changed

+253
-304
lines changed

11 files changed

+253
-304
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
android:name="com.facebook.sdk.ApplicationId"
4141
android:value="@string/facebook_app_id" />
4242

43+
<!-- Google Configuration -->
4344
<activity android:name="com.firebase.ui.auth.google.GoogleSignInActivity" />
4445
<meta-data
4546
android:name="com.firebase.ui.GoogleClientId"

app/src/main/java/com/firebase/uidemo/RecyclerViewDemoActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public void onFirebaseLoginProviderError(FirebaseLoginError firebaseError) {
154154

155155
@Override
156156
public void onFirebaseLoginUserError(FirebaseLoginError firebaseError) {
157+
resetFirebaseLoginDialog();
157158
Log.i(TAG, "Login user error: " + firebaseError.toString());
158159
}
159160

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
minSdkVersion 16
1010
targetSdkVersion 22
1111
versionCode 1
12-
versionName "0.2.0"
12+
versionName "0.3.0"
1313
}
1414
buildTypes {
1515
release {

library/src/main/java/com/firebase/ui/auth/core/FirebaseLoginBaseActivity.java

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,9 @@ public abstract class FirebaseLoginBaseActivity extends AppCompatActivity {
1616

1717
private final String TAG = "FirebaseLoginBaseAct";
1818

19-
private GoogleAuthHelper mGoogleAuthHelper;
20-
private FacebookAuthHelper mFacebookAuthHelper;
21-
private TwitterAuthHelper mTwitterAuthHelper;
22-
private PasswordAuthHelper mPasswordAuthHelper;
2319
private Firebase.AuthStateListener mAuthStateListener;
24-
25-
TokenAuthHandler mHandler;
26-
2720
private FirebaseLoginDialog mDialog;
28-
29-
public SocialProvider mChosenProvider;
21+
private TokenAuthHandler mHandler;
3022

3123
/* Abstract methods for Login Events */
3224
protected abstract void onFirebaseLoginSuccess(AuthData authData);
@@ -45,63 +37,34 @@ public abstract class FirebaseLoginBaseActivity extends AppCompatActivity {
4537
*/
4638
protected abstract Firebase getFirebaseRef();
4739

48-
/* Login/Logout */
49-
50-
public void loginWithProvider(SocialProvider provider) {
51-
// TODO: what should happen if you're already authenticated?
52-
switch (provider) {
53-
case google:
54-
mGoogleAuthHelper.login();
55-
break;
56-
case facebook:
57-
mFacebookAuthHelper.login();
58-
break;
59-
case twitter:
60-
mTwitterAuthHelper.login();
61-
break;
62-
}
63-
64-
mChosenProvider = provider;
65-
}
66-
6740
public void logout() {
68-
switch (mChosenProvider) {
69-
case google:
70-
mGoogleAuthHelper.logout();
71-
break;
72-
case facebook:
73-
mFacebookAuthHelper.logout();
74-
break;
75-
case twitter:
76-
mFacebookAuthHelper.logout();
77-
break;
78-
}
79-
getFirebaseRef().unauth();
41+
mDialog.logout();
8042
}
8143

8244
public void onActivityResult(int requestCode, int resultCode, Intent data) {
83-
// TODO: If someone isn't extending this activity, they need to implement this by hand
84-
if (mDialog.isActive) {
85-
mDialog.onActivityResult(requestCode, resultCode, data);
86-
} else {
87-
mFacebookAuthHelper.mCallbackManager.onActivityResult(requestCode, resultCode, data);
88-
mTwitterAuthHelper.onActivityResult(requestCode, resultCode, data);
89-
mGoogleAuthHelper.onActivityResult(requestCode, resultCode, data);
90-
}
45+
mDialog.onActivityResult(requestCode, resultCode, data);
9146
}
9247

9348
public void showFirebaseLoginPrompt() {
9449
mDialog.show(getFragmentManager(), "");
9550
}
9651

52+
public void dismissFirebaseLoginPrompt() {
53+
mDialog.dismiss();
54+
}
55+
56+
public void resetFirebaseLoginDialog() {
57+
mDialog.reset();
58+
}
59+
9760
@Override
9861
protected void onStart() {
9962
super.onStart();
10063

10164
mHandler = new TokenAuthHandler() {
10265
@Override
10366
public void onSuccess(AuthData data) {
104-
67+
/* onFirebaseLoginSuccess is called by the AuthStateListener below */
10568
}
10669

10770
@Override
@@ -118,21 +81,14 @@ public void onProviderError(FirebaseLoginError err) {
11881
mAuthStateListener = new Firebase.AuthStateListener() {
11982
@Override
12083
public void onAuthStateChanged(AuthData authData) {
121-
if (authData != null) {
122-
mChosenProvider = SocialProvider.valueOf(authData.getProvider());
123-
onFirebaseLoginSuccess(authData);
124-
Log.d(TAG, "Auth data changed");
125-
} else {
126-
onFirebaseLogout();
127-
}
84+
if (authData != null) {
85+
onFirebaseLoginSuccess(authData);
86+
} else {
87+
onFirebaseLogout();
88+
}
12889
}
12990
};
13091

131-
mFacebookAuthHelper = new FacebookAuthHelper(this, getFirebaseRef(), mHandler);
132-
mGoogleAuthHelper = new GoogleAuthHelper(this, getFirebaseRef(), mHandler);
133-
mTwitterAuthHelper = new TwitterAuthHelper(this, getFirebaseRef(), mHandler);
134-
mPasswordAuthHelper = new PasswordAuthHelper(this, getFirebaseRef(), mHandler);
135-
13692
mDialog = new FirebaseLoginDialog();
13793
mDialog
13894
.setContext(this)

library/src/main/java/com/firebase/ui/auth/core/FirebaseLoginDialog.java

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@ public class FirebaseLoginDialog extends DialogFragment {
2525
TwitterAuthHelper mTwitterAuthHelper;
2626
GoogleAuthHelper mGoogleAuthHelper;
2727
PasswordAuthHelper mPasswordAuthHelper;
28-
2928
TokenAuthHandler mHandler;
3029
Firebase mRef;
3130
Context mContext;
32-
public Boolean isActive = false;
31+
View mView;
3332

3433
public void onStart() {
3534
super.onStart();
36-
isActive = true;
35+
if (mGoogleAuthHelper != null) mGoogleAuthHelper.onStart();
36+
}
37+
38+
public void onStop() {
39+
super.onStop();
40+
if (mGoogleAuthHelper != null) mGoogleAuthHelper.onStop();
3741
}
3842

3943
public void onDestroy() {
4044
super.onDestroy();
41-
isActive = false;
45+
if (mGoogleAuthHelper != null) mGoogleAuthHelper.onStop();
4246
}
4347

4448
public void onActivityResult(int requestCode, int resultCode, Intent data) {
@@ -55,7 +59,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
5559
}
5660
}
5761

58-
View mView;
5962
@Override
6063
public Dialog onCreateDialog(Bundle savedInstanceState) {
6164
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
@@ -90,6 +93,24 @@ public FirebaseLoginDialog setRef(Firebase ref) {
9093
return this;
9194
}
9295

96+
public FirebaseLoginDialog setContext(Context context) {
97+
mContext = context;
98+
return this;
99+
}
100+
101+
public void reset() {
102+
mView.findViewById(R.id.login_section).setVisibility(View.VISIBLE);
103+
mView.findViewById(R.id.loading_section).setVisibility(View.GONE);
104+
}
105+
106+
public void logout() {
107+
if (mFacebookAuthHelper != null) mFacebookAuthHelper.logout();
108+
if (mGoogleAuthHelper != null) mGoogleAuthHelper.logout();
109+
if (mTwitterAuthHelper != null) mTwitterAuthHelper.logout();
110+
if (mPasswordAuthHelper != null) mPasswordAuthHelper.logout();
111+
mRef.unauth();
112+
}
113+
93114
public FirebaseLoginDialog setHandler(final TokenAuthHandler handler) {
94115
//TODO: Make this idiomatic?
95116
final DialogFragment self = this;
@@ -113,24 +134,23 @@ public void onProviderError(FirebaseLoginError err) {
113134
return this;
114135
}
115136

116-
public FirebaseLoginDialog setContext(Context context) {
117-
mContext = context;
118-
return this;
119-
}
120-
121137
public FirebaseLoginDialog setProviderEnabled(SocialProvider provider) {
122138
switch (provider) {
123139
case facebook:
124-
mFacebookAuthHelper = new FacebookAuthHelper(mContext, mRef, mHandler);
140+
if (mFacebookAuthHelper == null)
141+
mFacebookAuthHelper = new FacebookAuthHelper(mContext, mRef, mHandler);
125142
break;
126143
case google:
127-
mGoogleAuthHelper = new GoogleAuthHelper(mContext, mRef, mHandler);
144+
if (mGoogleAuthHelper == null)
145+
mGoogleAuthHelper = new GoogleAuthHelper(mContext, mRef, mHandler);
128146
break;
129147
case twitter:
130-
mTwitterAuthHelper = new TwitterAuthHelper(mContext, mRef, mHandler);
148+
if (mTwitterAuthHelper == null)
149+
mTwitterAuthHelper = new TwitterAuthHelper(mContext, mRef, mHandler);
131150
break;
132151
case password:
133-
mPasswordAuthHelper = new PasswordAuthHelper(mContext, mRef, mHandler);
152+
if (mPasswordAuthHelper == null)
153+
mPasswordAuthHelper = new PasswordAuthHelper(mContext, mRef, mHandler);
134154
break;
135155
}
136156

@@ -141,17 +161,18 @@ private void showLoginOption(final FirebaseAuthHelper helper, int id) {
141161
mView.findViewById(id).setOnClickListener(new View.OnClickListener() {
142162
@Override
143163
public void onClick(View view) {
144-
if (helper.getProviderName().equals("password")) {
145-
EditText emailText = (EditText) mView.findViewById(R.id.email);
146-
EditText passwordText = (EditText) mView.findViewById(R.id.password);
147-
helper.login(emailText.getText().toString(), passwordText.getText().toString());
148-
} else {
149-
helper.login();
150-
}
151-
mView.findViewById(R.id.login_section).setVisibility(View.GONE);
152-
mView.findViewById(R.id.loading_section).setVisibility(View.VISIBLE);
164+
if (helper.getProviderName().equals("password")) {
165+
EditText emailText = (EditText) mView.findViewById(R.id.email);
166+
EditText passwordText = (EditText) mView.findViewById(R.id.password);
167+
helper.login(emailText.getText().toString(), passwordText.getText().toString());
168+
169+
passwordText.setText("");
170+
} else {
171+
helper.login();
172+
}
173+
mView.findViewById(R.id.login_section).setVisibility(View.GONE);
174+
mView.findViewById(R.id.loading_section).setVisibility(View.VISIBLE);
153175
}
154176
});
155-
156177
}
157178
}

library/src/main/java/com/firebase/ui/auth/facebook/FacebookAuthHelper.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.content.Context;
5+
import android.content.Intent;
56
import android.content.pm.ApplicationInfo;
67
import android.content.pm.PackageManager;
78
import android.os.Bundle;
@@ -25,12 +26,10 @@
2526

2627
public class FacebookAuthHelper extends FirebaseAuthHelper {
2728

28-
private final String LOG_TAG = "FacebookAuthHelper";
29-
3029
public static final String PROVIDER_NAME = "facebook";
31-
32-
private LoginManager mLoginManager;
30+
private final String TAG = "FacebookAuthHelper";
3331
public CallbackManager mCallbackManager;
32+
private LoginManager mLoginManager;
3433
private TokenAuthHandler mHandler;
3534
private Activity mActivity;
3635
private Firebase mRef;
@@ -60,7 +59,7 @@ public void onSuccess(LoginResult loginResult) {
6059

6160
@Override
6261
public void onCancel() {
63-
mHandler.onUserError(new FirebaseLoginError(FirebaseResponse.LOGIN_CANCELLED, "User closed login dialog"));
62+
mHandler.onUserError(new FirebaseLoginError(FirebaseResponse.LOGIN_CANCELLED, "User closed login dialog."));
6463
}
6564

6665
@Override
@@ -99,6 +98,10 @@ public void login() {
9998
}
10099
}
101100

101+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
102+
mCallbackManager.onActivityResult(requestCode, resultCode, data);
103+
}
104+
102105
public String getProviderName() {
103106
return PROVIDER_NAME;
104107
}

library/src/main/java/com/firebase/ui/auth/google/GoogleActions.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
public class GoogleActions {
44
private static int base = 1000;
5-
public static int REQUEST = base+0;
6-
public static int SUCCESS = base+1;
7-
public static int PROVIDER_ERROR = base+2;
8-
public static int USER_ERROR = base+3;
5+
public static int REQUEST_LOGIN = base+0;
6+
public static int REQUEST_LOGOUT = base+1;
7+
public static int SUCCESS = base+2;
8+
public static int PROVIDER_ERROR = base+3;
9+
public static int USER_ERROR = base+4;
910
}

0 commit comments

Comments
 (0)