Skip to content

Commit 63e3f95

Browse files
SUPERCILEXsamtstern
authored andcommitted
Fix broken play services checks (#462)
1 parent 81dbbca commit 63e3f95

17 files changed

+124
-653
lines changed
Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,96 @@
11
package com.firebase.ui.auth;
22

33
import android.content.Context;
4+
import android.content.DialogInterface;
45
import android.content.Intent;
6+
import android.net.ConnectivityManager;
57
import android.os.Bundle;
8+
import android.util.Log;
69

710
import com.firebase.ui.auth.ui.ActivityHelper;
811
import com.firebase.ui.auth.ui.AppCompatBase;
912
import com.firebase.ui.auth.ui.ExtraConstants;
1013
import com.firebase.ui.auth.ui.FlowParameters;
14+
import com.firebase.ui.auth.util.PlayServicesHelper;
1115
import com.firebase.ui.auth.util.signincontainer.SignInDelegate;
1216

1317
public class KickoffActivity extends AppCompatBase {
18+
private static final String TAG = "KickoffActivity";
19+
private static final String IS_WAITING_FOR_PLAY_SERVICES = "is_waiting_for_play_services";
20+
private static final int RC_PLAY_SERVICES = 1;
21+
22+
private boolean mIsWaitingForPlayServices = false;
23+
24+
public static Intent createIntent(Context context, FlowParameters flowParams) {
25+
return ActivityHelper.createBaseIntent(context, KickoffActivity.class, flowParams);
26+
}
27+
1428
@Override
1529
protected void onCreate(Bundle savedInstance) {
1630
super.onCreate(savedInstance);
17-
if (savedInstance == null) {
18-
SignInDelegate.delegate(this, mActivityHelper.getFlowParams());
31+
if (savedInstance == null || savedInstance.getBoolean(IS_WAITING_FOR_PLAY_SERVICES)) {
32+
if (isOffline()) {
33+
Log.d(TAG, "No network connection");
34+
finish(ErrorCodes.NO_NETWORK,
35+
IdpResponse.getErrorCodeIntent(ErrorCodes.NO_NETWORK));
36+
return;
37+
}
38+
39+
boolean isPlayServicesAvailable = PlayServicesHelper.makePlayServicesAvailable(
40+
this,
41+
RC_PLAY_SERVICES,
42+
new DialogInterface.OnCancelListener() {
43+
@Override
44+
public void onCancel(DialogInterface dialog) {
45+
finish(ResultCodes.CANCELED,
46+
IdpResponse.getErrorCodeIntent(
47+
ErrorCodes.UNKNOWN_ERROR));
48+
}
49+
});
50+
51+
if (isPlayServicesAvailable) {
52+
SignInDelegate.delegate(KickoffActivity.this, mActivityHelper.getFlowParams());
53+
} else {
54+
mIsWaitingForPlayServices = true;
55+
}
1956
}
2057
}
2158

2259
@Override
2360
public void onSaveInstanceState(Bundle outState) {
2461
// It doesn't matter what we put here, we just don't want outState to be empty
2562
outState.putBoolean(ExtraConstants.HAS_EXISTING_INSTANCE, true);
63+
outState.putBoolean(IS_WAITING_FOR_PLAY_SERVICES, mIsWaitingForPlayServices);
2664
super.onSaveInstanceState(outState);
2765
}
2866

2967
@Override
3068
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3169
super.onActivityResult(requestCode, resultCode, data);
32-
SignInDelegate delegate = SignInDelegate.getInstance(this);
33-
if (delegate != null) {
34-
delegate.onActivityResult(requestCode, resultCode, data);
70+
if (requestCode == RC_PLAY_SERVICES) {
71+
if (resultCode == ResultCodes.OK) {
72+
SignInDelegate.delegate(KickoffActivity.this, mActivityHelper.getFlowParams());
73+
} else {
74+
finish(ResultCodes.CANCELED,
75+
IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR));
76+
}
77+
} else {
78+
SignInDelegate delegate = SignInDelegate.getInstance(this);
79+
if (delegate != null) delegate.onActivityResult(requestCode, resultCode, data);
3580
}
3681
}
3782

38-
public static Intent createIntent(Context context, FlowParameters flowParams) {
39-
return ActivityHelper.createBaseIntent(context, KickoffActivity.class, flowParams);
83+
/**
84+
* Check if there is an active or soon-to-be-active network connection.
85+
*
86+
* @return true if there is no network connection, false otherwise.
87+
*/
88+
private boolean isOffline() {
89+
ConnectivityManager manager =
90+
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
91+
92+
return !(manager != null
93+
&& manager.getActiveNetworkInfo() != null
94+
&& manager.getActiveNetworkInfo().isConnectedOrConnecting());
4095
}
4196
}

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
import com.firebase.ui.auth.ui.TaskFailureLogger;
2323
import com.firebase.ui.auth.ui.User;
2424
import com.firebase.ui.auth.ui.email.fieldvalidators.EmailFieldValidator;
25-
import com.firebase.ui.auth.util.FirebaseAuthWrapperFactory;
25+
import com.firebase.ui.auth.util.GoogleApiConstants;
26+
import com.google.android.gms.auth.api.Auth;
2627
import com.google.android.gms.auth.api.credentials.Credential;
28+
import com.google.android.gms.auth.api.credentials.CredentialPickerConfig;
29+
import com.google.android.gms.auth.api.credentials.HintRequest;
30+
import com.google.android.gms.common.ConnectionResult;
31+
import com.google.android.gms.common.api.GoogleApiClient;
2732
import com.google.android.gms.tasks.OnCompleteListener;
2833
import com.google.android.gms.tasks.OnSuccessListener;
2934
import com.google.android.gms.tasks.Task;
@@ -216,18 +221,36 @@ public void onSuccess(ProviderQueryResult result) {
216221
}
217222

218223
private void showEmailAutoCompleteHint() {
219-
PendingIntent hintIntent = FirebaseAuthWrapperFactory
220-
.getFirebaseAuthWrapper(mHelper.getAppName())
221-
.getEmailHintIntent(getActivity());
222-
if (hintIntent != null) {
223-
try {
224-
startIntentSenderForResult(hintIntent.getIntentSender(), RC_HINT, null, 0, 0, 0, null);
225-
} catch (IntentSender.SendIntentException e) {
226-
Log.e(TAG, "Unable to start hint intent", e);
227-
}
224+
try {
225+
startIntentSenderForResult(getEmailHintIntent().getIntentSender(), RC_HINT, null, 0, 0, 0, null);
226+
} catch (IntentSender.SendIntentException e) {
227+
Log.e(TAG, "Unable to start hint intent", e);
228228
}
229229
}
230230

231+
private PendingIntent getEmailHintIntent() {
232+
GoogleApiClient client = new GoogleApiClient.Builder(getContext())
233+
.addApi(Auth.CREDENTIALS_API)
234+
.enableAutoManage(getActivity(), GoogleApiConstants.AUTO_MANAGE_ID3,
235+
new GoogleApiClient.OnConnectionFailedListener() {
236+
@Override
237+
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
238+
Log.e(TAG,
239+
"Client connection failed: " + connectionResult.getErrorMessage());
240+
}
241+
})
242+
.build();
243+
244+
HintRequest hintRequest = new HintRequest.Builder()
245+
.setHintPickerConfig(new CredentialPickerConfig.Builder()
246+
.setShowCancelButton(true)
247+
.build())
248+
.setEmailAddressIdentifierSupported(true)
249+
.build();
250+
251+
return Auth.CredentialsApi.getHintPickerIntent(client, hintRequest);
252+
}
253+
231254
@Override
232255
public void onClick(View view) {
233256
int id = view.getId();

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

Lines changed: 0 additions & 76 deletions
This file was deleted.

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)