Skip to content

Commit 8ba0b59

Browse files
committed
Various fixes from feedback
1 parent 5a08bba commit 8ba0b59

File tree

9 files changed

+93
-50
lines changed

9 files changed

+93
-50
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# FirebaseUI for Android — UI Bindings for Firebase
22

3-
FirebaseUI is an open-source library for Android that allows you to quickly connect common UI elements to the [Firebase](https://www.firebase.com) database for data storage, allowing views to be updated in realtime as they change, providing simple interfaces for common tasks like displaying lists or collections of items, and displaying prompts for user authentication.
3+
FirebaseUI is an open-source library for Android that allows you to quickly connect common UI elements to the [Firebase](https://www.firebase.com) database for data storage, allowing views to be updated in realtime as they change, providing simple interfaces for common tasks like displaying lists or collections of items, and displaying prompts for user authentication.
44

55
A compatible FirebaseUI client is also available for [iOS](https://github.com/firebase/firebaseui-ios).
66

@@ -12,7 +12,7 @@ A compatible FirebaseUI client is also available for [iOS](https://github.com/fi
1212
4. [Using FirebaseUI to Populate a RecyclerView](#using-firebaseui-to-populate-a-recyclerview)
1313
5. [Contributing](#contributing)
1414

15-
## Installation
15+
## Installation
1616

1717
If your Android app already uses Firebase, you have added a dependency to the Firebase SDK to your dependencies.
1818
In this step we'll add the FirebaseUI library as another dependency.
@@ -94,6 +94,18 @@ If you're using Facebook authentication, add the following to your `<application
9494
android:value="@string/facebook_app_id" />
9595
```
9696

97+
If you're using Google authentication, add the following to your `<application>` tag.
98+
99+
```
100+
<!-- Google Configuration -->
101+
<meta-data
102+
android:name="com.firebase.ui.GoogleClientId"
103+
android:value="@string/google_client_id" />
104+
```
105+
106+
If you're using Google Sign-in you'll also need to ensure that your `google-services.json` file is created
107+
and placed in your app folder.
108+
97109
### Inherit from FirebaseLoginBaseActivity
98110

99111
Now we get to the juicy bits. Open your `MainActivity` and change your activity to extend `FirebaseLoginBaseActivity`
@@ -113,7 +125,7 @@ public class MainActivity extends FirebaseLoginBaseActivity {
113125
public Firebase getFirebaseRef() {
114126
// TODO: Return your Firebase ref
115127
}
116-
128+
117129
@Override
118130
public void onFirebaseLoginSuccess(AuthData authData) {
119131
// TODO: Handle successful login
@@ -382,7 +394,7 @@ You're done! You now have a minimal, yet fully functional, chat app in about 30
382394
## Using FirebaseUI to Populate a RecyclerView
383395
384396
RecyclerView is the new preferred way to handle potentially long lists of items. Since Firebase collections
385-
can contain many items, there is an `FirebaseRecyclerViewAdapter` too. Here's how you use it:
397+
can contain many items, there is an `FirebaseRecyclerAdapter` too. Here's how you use it:
386398

387399
1. Create a custom ViewHolder class
388400
2. Create a custom subclass FirebaseListAdapter

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
android:name="com.facebook.sdk.ApplicationId"
4040
android:value="@string/facebook_app_id" />
4141

42+
<!-- Google Configuration -->
43+
<meta-data
44+
android:name="com.firebase.ui.GoogleClientId"
45+
android:value="@string/google_client_id" />
46+
4247

4348
</application>
4449

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Context;
77
import android.content.Intent;
88
import android.os.Bundle;
9+
import android.util.Log;
910
import android.view.LayoutInflater;
1011
import android.view.View;
1112
import android.widget.EditText;
@@ -91,6 +92,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
9192
mView.findViewById(R.id.loading_section).setVisibility(View.GONE);
9293

9394
builder.setView(mView);
95+
9496
return builder.create();
9597
}
9698

@@ -110,20 +112,10 @@ public void reset() {
110112
}
111113

112114
public void logout() {
113-
switch (mActiveProvider) {
114-
case twitter:
115-
mTwitterAuthProvider.logout();
116-
break;
117-
case facebook:
118-
mFacebookAuthProvider.logout();
119-
break;
120-
case google:
121-
mGoogleAuthProvider.logout();
122-
break;
123-
case password:
124-
mPasswordAuthProvider.logout();
125-
break;
126-
}
115+
if (mTwitterAuthProvider != null) mTwitterAuthProvider.logout();
116+
if (mFacebookAuthProvider != null) mFacebookAuthProvider.logout();
117+
if (mGoogleAuthProvider != null) mGoogleAuthProvider.logout();
118+
if (mPasswordAuthProvider != null) mPasswordAuthProvider.logout();
127119
mRef.unauth();
128120
}
129121

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class FirebaseLoginError {
44
public String message;
5-
public int error;
5+
public FirebaseResponse error;
66

7-
public FirebaseLoginError(int error, String message) {
7+
public FirebaseLoginError(FirebaseResponse error, String message) {
88
this.message = message;
99
this.error = error;
1010
}
1111

1212
public String toString() {
13-
return Integer.toString(error) + ": " + message;
13+
return error.toString() + ": " + message;
1414
}
1515

1616
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.firebase.ui.auth.core;
22

3-
public class FirebaseResponse {
4-
public static int WRONG_CREDENTIALS = 0;
5-
public static int PROVIDER_NOT_ENABLED = 1;
6-
public static int LOGIN_CANCELLED = 2;
7-
public static int MISC_PROVIDER_ERROR = 3;
8-
public static int INVALID_PROVIDER_APP_ID = 4;
9-
public static int INVALID_PROVIDER_APP_KEY = 5;
10-
public static int MISSING_PROVIDER_APP_ID = 6;
11-
public static int MISSING_PROVIDER_APP_KEY = 7;
3+
public enum FirebaseResponse {
4+
WRONG_CREDENTIALS,
5+
PROVIDER_NOT_ENABLED,
6+
LOGIN_CANCELLED,
7+
MISC_PROVIDER_ERROR,
8+
INVALID_PROVIDER_APP_ID,
9+
INVALID_PROVIDER_APP_KEY,
10+
MISSING_PROVIDER_APP_ID,
11+
MISSING_PROVIDER_APP_KEY
1212
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.firebase.ui.auth.google;
2+
3+
public class GoogleActions {
4+
private static int base = 9000;
5+
public static int SIGN_IN = base+0;
6+
public static int SIGN_OUT = base+1;
7+
}

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.content.pm.PackageManager;
88
import android.os.Bundle;
99
import android.support.v4.app.FragmentActivity;
10+
import android.util.Log;
1011

1112
import com.firebase.client.Firebase;
1213
import com.firebase.ui.auth.core.FirebaseAuthProvider;
@@ -26,16 +27,17 @@
2627

2728
public class GoogleAuthProvider extends FirebaseAuthProvider implements
2829
GoogleApiClient.OnConnectionFailedListener,
30+
GoogleApiClient.ConnectionCallbacks,
2931
GoogleOAuthTaskHandler {
3032

3133
public final static String PROVIDER_NAME = "google";
3234
public static final SocialProvider PROVIDER_TYPE = SocialProvider.google;
33-
private static final int RC_SIGN_IN = 9001;
3435
private final String TAG = "GoogleAuthProvider";
3536
private GoogleApiClient mGoogleApiClient;
3637
private TokenAuthHandler mHandler;
3738
private Activity mActivity;
3839
private Firebase mRef;
40+
private Integer onConnectedAction;
3941

4042
public GoogleAuthProvider(Context context, Firebase ref, TokenAuthHandler handler) {
4143
mActivity = (Activity) context;
@@ -82,19 +84,41 @@ public GoogleAuthProvider(Context context, Firebase ref, TokenAuthHandler handle
8284
public Firebase getFirebaseRef() { return mRef; }
8385
public SocialProvider getProviderType() { return PROVIDER_TYPE; };
8486

87+
@Override
88+
public void onConnected(Bundle bundle) {
89+
if (onConnectedAction == GoogleActions.SIGN_IN) {
90+
login();
91+
} else if (onConnectedAction == GoogleActions.SIGN_OUT) {
92+
logout();
93+
}
94+
95+
onConnectedAction = 0;
96+
}
97+
98+
@Override
99+
public void onConnectionSuspended(int i) {}
100+
85101
public void logout() {
86-
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
87-
new ResultCallback<Status>() {
88-
@Override
89-
public void onResult(Status status) {
90-
revokeAccess();
91-
}
92-
});
102+
if (mGoogleApiClient.isConnected()) {
103+
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
104+
new ResultCallback<Status>() {
105+
@Override
106+
public void onResult(Status status) {
107+
revokeAccess();
108+
}
109+
});
110+
} else {
111+
onConnectedAction = GoogleActions.SIGN_OUT;
112+
}
93113
}
94114

95115
public void login() {
96-
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
97-
mActivity.startActivityForResult(signInIntent, RC_SIGN_IN);
116+
if (mGoogleApiClient.isConnected()) {
117+
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
118+
mActivity.startActivityForResult(signInIntent, GoogleActions.SIGN_IN);
119+
} else {
120+
onConnectedAction = GoogleActions.SIGN_IN;
121+
}
98122
}
99123
private void revokeAccess() {
100124
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
@@ -107,12 +131,12 @@ public void onResult(Status status) {
107131
}
108132

109133
public void onActivityResult(int requestCode, int resultCode, Intent data) {
110-
if (requestCode == RC_SIGN_IN && resultCode == -1) {
134+
if (requestCode == GoogleActions.SIGN_IN && resultCode == -1) {
111135
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
112136
handleSignInResult(result);
113137
}
114138

115-
if (requestCode == RC_SIGN_IN && resultCode == 0) {
139+
if (requestCode == GoogleActions.SIGN_IN && resultCode == 0) {
116140
mHandler.onUserError(new FirebaseLoginError(FirebaseResponse.LOGIN_CANCELLED, "User closed login dialog."));
117141
}
118142
}

library/src/main/java/com/firebase/ui/auth/twitter/TwitterAuthProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.firebase.ui.auth.core.FirebaseAuthProvider;
99
import com.firebase.ui.auth.core.FirebaseLoginError;
1010
import com.firebase.ui.auth.core.FirebaseOAuthToken;
11+
import com.firebase.ui.auth.core.FirebaseResponse;
1112
import com.firebase.ui.auth.core.SocialProvider;
1213
import com.firebase.ui.auth.core.TokenAuthHandler;
1314

@@ -48,9 +49,11 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
4849
data.getStringExtra("user_id"));
4950
onFirebaseTokenReceived(token, mHandler);
5051
} else if (resultCode == TwitterActions.USER_ERROR) {
51-
mHandler.onUserError(new FirebaseLoginError(data.getIntExtra("code", 0), data.getStringExtra("error")));
52+
FirebaseResponse error = FirebaseResponse.values()[data.getIntExtra("code", 0)];
53+
mHandler.onUserError(new FirebaseLoginError(error, data.getStringExtra("error")));
5254
} else if (resultCode == TwitterActions.PROVIDER_ERROR) {
53-
mHandler.onProviderError(new FirebaseLoginError(data.getIntExtra("code", 0), data.getStringExtra("error")));
55+
FirebaseResponse error = FirebaseResponse.values()[data.getIntExtra("code", 0)];
56+
mHandler.onProviderError(new FirebaseLoginError(error, data.getStringExtra("error")));
5457
}
5558
}
5659
}

library/src/main/java/com/firebase/ui/auth/twitter/TwitterPromptActivity.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class TwitterPromptActivity extends Activity {
2626

2727
@Override
2828
public void onBackPressed() {
29-
sendResultError(TwitterActions.USER_ERROR, FirebaseResponse.LOGIN_CANCELLED, "User closed login prompt.");
29+
sendResultError(TwitterActions.USER_ERROR, FirebaseResponse.LOGIN_CANCELLED.ordinal(), "User closed login prompt.");
3030
super.onBackPressed();
3131
}
3232

@@ -46,12 +46,12 @@ protected void onCreate(Bundle savedInstanceState) {
4646
} catch (NullPointerException e) {}
4747

4848
if (twitterKey == null || twitterSecret == null) {
49-
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.MISSING_PROVIDER_APP_KEY, "Missing Twitter key/secret, are they set in your AndroidManifest.xml?");
49+
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.MISSING_PROVIDER_APP_KEY.ordinal(), "Missing Twitter key/secret, are they set in your AndroidManifest.xml?");
5050
return;
5151
}
5252

5353
if (twitterKey.compareTo("") == 0|| twitterSecret.compareTo("") == 0) {
54-
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.INVALID_PROVIDER_APP_KEY, "Invalid Twitter key/secret, are they set in your res/values/strings.xml?");
54+
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.INVALID_PROVIDER_APP_KEY.ordinal(), "Invalid Twitter key/secret, are they set in your res/values/strings.xml?");
5555
return;
5656
}
5757

@@ -75,7 +75,7 @@ protected RequestToken doInBackground(Void... params) {
7575
try {
7676
token = mTwitter.getOAuthRequestToken("oauth://cb");
7777
} catch (TwitterException te) {
78-
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.MISC_PROVIDER_ERROR, te.toString());
78+
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.MISC_PROVIDER_ERROR.ordinal(), te.toString());
7979
}
8080
return token;
8181
}
@@ -90,7 +90,7 @@ public void onPageFinished(final WebView view, final String url) {
9090
if (url.contains("oauth_verifier")) {
9191
getTwitterOAuthTokenAndLogin(token, Uri.parse(url).getQueryParameter("oauth_verifier"));
9292
} else if (url.contains("denied")) {
93-
sendResultError(TwitterActions.USER_ERROR, FirebaseResponse.LOGIN_CANCELLED, "User denied access to their account.");
93+
sendResultError(TwitterActions.USER_ERROR, FirebaseResponse.LOGIN_CANCELLED.ordinal(), "User denied access to their account.");
9494
}
9595
}
9696
}
@@ -108,7 +108,7 @@ protected AccessToken doInBackground(Void... params) {
108108
try {
109109
accessToken = mTwitter.getOAuthAccessToken(requestToken, oauthVerifier);
110110
} catch (TwitterException te) {
111-
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.MISC_PROVIDER_ERROR, te.toString());
111+
sendResultError(TwitterActions.PROVIDER_ERROR, FirebaseResponse.MISC_PROVIDER_ERROR.ordinal(), te.toString());
112112
}
113113
return accessToken;
114114
}

0 commit comments

Comments
 (0)