Skip to content

Commit c9ee4dc

Browse files
authored
Merge pull request #226 from samtstern/master
Fixes for various issues
2 parents 07ae798 + 14998c5 commit c9ee4dc

File tree

9 files changed

+236
-51
lines changed

9 files changed

+236
-51
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.firebase.uidemo.auth;
1616

17-
import android.app.Activity;
1817
import android.content.Context;
1918
import android.content.Intent;
2019
import android.os.Bundle;
@@ -23,6 +22,7 @@
2322
import android.support.annotation.StringRes;
2423
import android.support.annotation.StyleRes;
2524
import android.support.design.widget.Snackbar;
25+
import android.support.v7.app.AppCompatActivity;
2626
import android.view.View;
2727
import android.widget.Button;
2828
import android.widget.CheckBox;
@@ -38,7 +38,7 @@
3838
import butterknife.ButterKnife;
3939
import butterknife.OnClick;
4040

41-
public class AuthUiActivity extends Activity {
41+
public class AuthUiActivity extends AppCompatActivity {
4242

4343
private static final String UNCHANGED_CONFIG_VALUE = "CHANGE-ME";
4444

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.firebase.uidemo.auth;
1616

17-
import android.app.Activity;
1817
import android.content.Context;
1918
import android.content.DialogInterface;
2019
import android.content.Intent;
@@ -24,6 +23,7 @@
2423
import android.support.annotation.StringRes;
2524
import android.support.design.widget.Snackbar;
2625
import android.support.v7.app.AlertDialog;
26+
import android.support.v7.app.AppCompatActivity;
2727
import android.text.TextUtils;
2828
import android.view.View;
2929
import android.widget.ImageView;
@@ -46,7 +46,7 @@
4646
import butterknife.ButterKnife;
4747
import butterknife.OnClick;
4848

49-
public class SignedInActivity extends Activity {
49+
public class SignedInActivity extends AppCompatActivity {
5050

5151
@BindView(android.R.id.content)
5252
View mRootView;

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import android.content.Context;
1919
import android.content.Intent;
2020
import android.os.Bundle;
21+
import android.support.annotation.NonNull;
2122
import android.support.annotation.Nullable;
23+
import android.support.v4.app.FragmentActivity;
2224
import android.text.TextUtils;
25+
import android.util.Log;
2326
import android.view.View;
2427
import android.view.View.OnClickListener;
2528

@@ -28,21 +31,26 @@
2831
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
2932
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
3033
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
34+
import com.google.android.gms.common.ConnectionResult;
3135
import com.google.android.gms.common.api.GoogleApiClient;
3236
import com.google.firebase.auth.AuthCredential;
3337
import com.google.firebase.auth.GoogleAuthProvider;
3438

35-
public class GoogleProvider implements IDPProvider, OnClickListener {
39+
public class GoogleProvider implements
40+
IDPProvider, OnClickListener, GoogleApiClient.OnConnectionFailedListener {
41+
3642
public static final String TOKEN_KEY = "token_key";
3743

44+
private static final String TAG = "GoogleProvider";
45+
private static final int AUTO_MANAGE_ID = 1;
3846
private static final int RC_SIGN_IN = 20;
3947
private static final String ERROR_KEY = "error";
4048
private static final String CLIENT_ID_KEY = "client_id_key";
4149
private GoogleApiClient mGoogleApiClient;
4250
private Activity mActivity;
4351
private IDPCallback mIDPCallback;
4452

45-
public GoogleProvider(Activity activity, IDPProviderParcel parcel, @Nullable String email) {
53+
public GoogleProvider(FragmentActivity activity, IDPProviderParcel parcel, @Nullable String email) {
4654
mActivity = activity;
4755
String mClientId = parcel.getProviderExtra().getString(CLIENT_ID_KEY);
4856
GoogleSignInOptions googleSignInOptions;
@@ -51,15 +59,16 @@ public GoogleProvider(Activity activity, IDPProviderParcel parcel, @Nullable Str
5159
.DEFAULT_SIGN_IN)
5260
.requestEmail()
5361
.requestIdToken(mClientId);
62+
5463
if (!TextUtils.isEmpty(email)) {
5564
builder.setAccountName(email);
5665
}
5766
googleSignInOptions = builder.build();
5867

5968
mGoogleApiClient = new GoogleApiClient.Builder(activity)
69+
.enableAutoManage(activity, AUTO_MANAGE_ID, this)
6070
.addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
6171
.build();
62-
mGoogleApiClient.connect();
6372
}
6473

6574
public String getName(Context context) {
@@ -133,5 +142,10 @@ public void onClick(View view) {
133142
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
134143
startLogin(mActivity);
135144
}
145+
146+
@Override
147+
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
148+
Log.w(TAG, "onConnectionFailed:" + connectionResult);
149+
}
136150
}
137151

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

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import android.app.Activity;
1818
import android.content.Context;
19+
import android.content.DialogInterface;
1920
import android.content.Intent;
2021
import android.os.Bundle;
2122
import android.support.annotation.NonNull;
@@ -27,10 +28,10 @@
2728
import com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity;
2829
import com.firebase.ui.auth.ui.idp.IDPSignInContainerActivity;
2930
import com.firebase.ui.auth.util.CredentialsAPI;
31+
import com.firebase.ui.auth.util.PlayServicesHelper;
3032
import com.google.android.gms.auth.api.credentials.Credential;
3133
import com.google.android.gms.auth.api.credentials.IdentityProviders;
3234
import com.google.android.gms.tasks.OnCompleteListener;
33-
import com.google.android.gms.tasks.OnFailureListener;
3435
import com.google.android.gms.tasks.OnSuccessListener;
3536
import com.google.android.gms.tasks.Task;
3637
import com.google.firebase.auth.AuthResult;
@@ -55,12 +56,30 @@ public class ChooseAccountActivity extends ActivityBase {
5556
private static final int RC_IDP_SIGNIN = 3;
5657
private static final int RC_AUTH_METHOD_PICKER = 4;
5758
private static final int RC_EMAIL_FLOW = 5;
59+
private static final int RC_PLAY_SERVICES = 6;
5860

59-
protected CredentialsAPI mCredentialsApi;
61+
private CredentialsAPI mCredentialsApi;
6062

6163
@Override
6264
protected void onCreate(Bundle savedInstance) {
6365
super.onCreate(savedInstance);
66+
67+
// Make Google Play Services available at the correct version, if possible
68+
boolean madeAvailable = PlayServicesHelper.makePlayServicesAvailable(this, RC_PLAY_SERVICES,
69+
new DialogInterface.OnCancelListener() {
70+
@Override
71+
public void onCancel(DialogInterface dialogInterface) {
72+
Log.w(TAG, "playServices:dialog.onCancel()");
73+
finish(RESULT_CANCELED, new Intent());
74+
}
75+
});
76+
77+
if (!madeAvailable) {
78+
Log.w(TAG, "playServices: could not make available.");
79+
finish(RESULT_CANCELED, new Intent());
80+
return;
81+
}
82+
6483
mCredentialsApi = new CredentialsAPI(this, new CredentialsAPI.CallbackInterface() {
6584
@Override
6685
public void onAsyncTaskFinished() {
@@ -72,29 +91,33 @@ public void onAsyncTaskFinished() {
7291
@Override
7392
protected void onStart() {
7493
super.onStart();
75-
if (mCredentialsApi.isGoogleApiClient()) {
76-
mCredentialsApi.getGoogleApiClient().connect();
94+
if (mCredentialsApi != null) {
95+
mCredentialsApi.onStart();
7796
}
7897
}
7998

8099
@Override
81100
protected void onStop() {
82-
if (mCredentialsApi.isGoogleApiClient()
83-
&& mCredentialsApi.getGoogleApiClient().isConnected()) {
84-
mCredentialsApi.getGoogleApiClient().disconnect();
85-
}
86101
super.onStop();
102+
if (mCredentialsApi != null) {
103+
mCredentialsApi.onStop();
104+
}
87105
}
88106

107+
/**
108+
* Called when the Credentials API connects.
109+
*/
89110
public void onCredentialsApiConnected(
90111
CredentialsAPI credentialsApi,
91112
ActivityHelper activityHelper) {
92-
// called back when the CredentialsAPI connects
113+
93114
String email = credentialsApi.getEmailFromCredential();
94115
String password = credentialsApi.getPasswordFromCredential();
95116
String accountType = credentialsApi.getAccountTypeFromCredential();
96-
if (credentialsApi.isPlayServicesAvailable()
117+
118+
if (PlayServicesHelper.isPlayServicesAvailable(this)
97119
&& credentialsApi.isCredentialsAvailable()) {
120+
98121
if (credentialsApi.isAutoSignInAvailable()) {
99122
credentialsApi.googleSilentSignIn();
100123
// TODO: (serikb) authenticate Firebase user and continue to application
@@ -149,6 +172,7 @@ private void logInWithCredential(
149172
final String email,
150173
final String password,
151174
final String accountType) {
175+
152176
if (email != null
153177
&& mCredentialsApi.isCredentialsAvailable()
154178
&& !mCredentialsApi.isSignInResolutionNeeded()) {
@@ -187,29 +211,36 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
187211
Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
188212
}
189213

190-
if (requestCode == RC_CREDENTIALS_READ) {
191-
if (resultCode == RESULT_OK) {
192-
// credential selected from SmartLock, log in with that credential
193-
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
194-
mCredentialsApi.handleCredential(credential);
195-
mCredentialsApi.resolveSignIn();
196-
logInWithCredential(
197-
mCredentialsApi.getEmailFromCredential(),
198-
mCredentialsApi.getPasswordFromCredential(),
199-
mCredentialsApi.getAccountTypeFromCredential()
200-
);
201-
} else if (resultCode == RESULT_CANCELED) {
202-
// Smart lock selector cancelled, go to the AuthMethodPicker screen
203-
startAuthMethodChoice(mActivityHelper);
204-
} else if (resultCode == RESULT_FIRST_USER) {
205-
// TODO: (serikb) figure out flow
206-
}
207-
} else if (requestCode == RC_IDP_SIGNIN) {
208-
finish(resultCode, new Intent());
209-
} else if (requestCode == RC_AUTH_METHOD_PICKER) {
210-
finish(resultCode, new Intent());
211-
} else if (requestCode == RC_EMAIL_FLOW) {
212-
finish(resultCode, new Intent());
214+
switch (requestCode) {
215+
case RC_CREDENTIALS_READ:
216+
if (resultCode == RESULT_OK) {
217+
// credential selected from SmartLock, log in with that credential
218+
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
219+
mCredentialsApi.handleCredential(credential);
220+
mCredentialsApi.resolveSignIn();
221+
logInWithCredential(
222+
mCredentialsApi.getEmailFromCredential(),
223+
mCredentialsApi.getPasswordFromCredential(),
224+
mCredentialsApi.getAccountTypeFromCredential()
225+
);
226+
} else if (resultCode == RESULT_CANCELED) {
227+
// Smart lock selector cancelled, go to the AuthMethodPicker screen
228+
startAuthMethodChoice(mActivityHelper);
229+
} else if (resultCode == RESULT_FIRST_USER) {
230+
// TODO: (serikb) figure out flow
231+
}
232+
break;
233+
case RC_IDP_SIGNIN:
234+
case RC_AUTH_METHOD_PICKER:
235+
case RC_EMAIL_FLOW:
236+
finish(resultCode, new Intent());
237+
break;
238+
case RC_PLAY_SERVICES:
239+
if (resultCode != RESULT_OK) {
240+
finish(resultCode, new Intent());
241+
}
242+
break;
243+
213244
}
214245
}
215246

auth/src/main/java/com/firebase/ui/auth/ui/account_link/SaveCredentialsActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ protected void onStart() {
8787
mCredentialsApiClient.connect();
8888
}
8989

90+
@Override
91+
protected void onStop() {
92+
super.onStop();
93+
if (mCredentialsApiClient != null) {
94+
mCredentialsApiClient.disconnect();
95+
}
96+
}
97+
9098

9199
@Override
92100
public void onConnected(@Nullable Bundle bundle) {

auth/src/main/java/com/firebase/ui/auth/util/CredentialsAPI.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.google.android.gms.auth.api.credentials.IdentityProviders;
3131
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
3232
import com.google.android.gms.common.ConnectionResult;
33-
import com.google.android.gms.common.GoogleApiAvailability;
3433
import com.google.android.gms.common.api.CommonStatusCodes;
3534
import com.google.android.gms.common.api.GoogleApiClient;
3635
import com.google.android.gms.common.api.ResultCallback;
@@ -65,6 +64,9 @@ public CredentialsAPI(Activity activity, CallbackInterface callback) {
6564
requestCredentials(true /* shouldResolve */, false /* onlyPasswords */);
6665
}
6766

67+
public boolean isPlayServicesAvailable() {
68+
return PlayServicesHelper.isPlayServicesAvailable(mActivity);
69+
}
6870

6971
public boolean isCredentialsAvailable() {
7072
// TODO: (serikb) find the way to check if Credentials is available on top of play services
@@ -97,13 +99,6 @@ public void resolveSavedEmails(Activity activity) {
9799
}
98100
}
99101

100-
public boolean isPlayServicesAvailable() {
101-
return GoogleApiAvailability
102-
.getInstance()
103-
.isGooglePlayServicesAvailable(mActivity.getApplicationContext())
104-
== ConnectionResult.SUCCESS;
105-
}
106-
107102
public String getEmailFromCredential() {
108103
if (mCredential == null) {
109104
return null;
@@ -168,6 +163,12 @@ public void handleCredential(Credential credential) {
168163
}
169164

170165
public void requestCredentials(final boolean shouldResolve, boolean onlyPasswords) {
166+
if (!PlayServicesHelper.isPlayServicesAvailable(mActivity)) {
167+
// TODO(samstern): it would probably be better to not actually call the method
168+
// in this case.
169+
return;
170+
}
171+
171172
CredentialRequest.Builder crBuilder = new CredentialRequest.Builder()
172173
.setPasswordLoginSupported(true);
173174

@@ -215,6 +216,20 @@ private void hideProgress() {
215216
}
216217
}
217218

219+
public void onStart() {
220+
if (mGoogleApiClient != null) {
221+
mGoogleApiClient.connect();
222+
}
223+
}
224+
225+
public void onStop() {
226+
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
227+
mGoogleApiClient.disconnect();;
228+
}
229+
230+
hideProgress();
231+
}
232+
218233
@Override
219234
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
220235
Log.w(TAG, "onConnectionFailed:" + connectionResult);

0 commit comments

Comments
 (0)