Skip to content

Fix broken play services checks #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 22, 2016
69 changes: 62 additions & 7 deletions auth/src/main/java/com/firebase/ui/auth/KickoffActivity.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,96 @@
package com.firebase.ui.auth;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;

import com.firebase.ui.auth.ui.ActivityHelper;
import com.firebase.ui.auth.ui.AppCompatBase;
import com.firebase.ui.auth.ui.ExtraConstants;
import com.firebase.ui.auth.ui.FlowParameters;
import com.firebase.ui.auth.util.PlayServicesHelper;
import com.firebase.ui.auth.util.signincontainer.SignInDelegate;

public class KickoffActivity extends AppCompatBase {
private static final String TAG = "KickoffActivity";
private static final String IS_WAITING_FOR_PLAY_SERVICES = "is_waiting_for_play_services";
private static final int RC_PLAY_SERVICES = 1;

private boolean mIsWaitingForPlayServices = false;

public static Intent createIntent(Context context, FlowParameters flowParams) {
return ActivityHelper.createBaseIntent(context, KickoffActivity.class, flowParams);
}

@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
if (savedInstance == null) {
SignInDelegate.delegate(this, mActivityHelper.getFlowParams());
if (savedInstance == null || savedInstance.getBoolean(IS_WAITING_FOR_PLAY_SERVICES)) {
if (isOffline()) {
Log.d(TAG, "No network connection");
finish(ErrorCodes.NO_NETWORK,
IdpResponse.getErrorCodeIntent(ErrorCodes.NO_NETWORK));
return;
}

boolean isPlayServicesAvailable = PlayServicesHelper.makePlayServicesAvailable(
this,
RC_PLAY_SERVICES,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish(ResultCodes.CANCELED,
IdpResponse.getErrorCodeIntent(
ErrorCodes.UNKNOWN_ERROR));
}
});

if (isPlayServicesAvailable) {
SignInDelegate.delegate(KickoffActivity.this, mActivityHelper.getFlowParams());
} else {
mIsWaitingForPlayServices = true;
}
}
}

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SignInDelegate delegate = SignInDelegate.getInstance(this);
if (delegate != null) {
delegate.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PLAY_SERVICES) {
if (resultCode == ResultCodes.OK) {
SignInDelegate.delegate(KickoffActivity.this, mActivityHelper.getFlowParams());
} else {
finish(ResultCodes.CANCELED,
IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR));
}
} else {
SignInDelegate delegate = SignInDelegate.getInstance(this);
if (delegate != null) delegate.onActivityResult(requestCode, resultCode, data);
}
}

public static Intent createIntent(Context context, FlowParameters flowParams) {
return ActivityHelper.createBaseIntent(context, KickoffActivity.class, flowParams);
/**
* Check if there is an active or soon-to-be-active network connection.
*
* @return true if there is no network connection, false otherwise.
*/
private boolean isOffline() {
ConnectivityManager manager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

return !(manager != null
&& manager.getActiveNetworkInfo() != null
&& manager.getActiveNetworkInfo().isConnectedOrConnecting());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.ui.User;
import com.firebase.ui.auth.ui.email.fieldvalidators.EmailFieldValidator;
import com.firebase.ui.auth.util.FirebaseAuthWrapperFactory;
import com.firebase.ui.auth.util.GoogleApiConstants;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.CredentialPickerConfig;
import com.google.android.gms.auth.api.credentials.HintRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
Expand Down Expand Up @@ -216,18 +221,36 @@ public void onSuccess(ProviderQueryResult result) {
}

private void showEmailAutoCompleteHint() {
PendingIntent hintIntent = FirebaseAuthWrapperFactory
.getFirebaseAuthWrapper(mHelper.getAppName())
.getEmailHintIntent(getActivity());
if (hintIntent != null) {
try {
startIntentSenderForResult(hintIntent.getIntentSender(), RC_HINT, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Unable to start hint intent", e);
}
try {
startIntentSenderForResult(getEmailHintIntent().getIntentSender(), RC_HINT, null, 0, 0, 0, null);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Unable to start hint intent", e);
}
}

private PendingIntent getEmailHintIntent() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to move this to PlayServicesHelper or leave it here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can stay

GoogleApiClient client = new GoogleApiClient.Builder(getContext())
.addApi(Auth.CREDENTIALS_API)
.enableAutoManage(getActivity(), GoogleApiConstants.AUTO_MANAGE_ID3,
new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(TAG,
"Client connection failed: " + connectionResult.getErrorMessage());
}
})
.build();

HintRequest hintRequest = new HintRequest.Builder()
.setHintPickerConfig(new CredentialPickerConfig.Builder()
.setShowCancelButton(true)
.build())
.setEmailAddressIdentifierSupported(true)
.build();

return Auth.CredentialsApi.getHintPickerIntent(client, hintRequest);
}

@Override
public void onClick(View view) {
int id = view.getId();
Expand Down

This file was deleted.

This file was deleted.

Loading