Skip to content

Bugfixes for older versions of Android #382

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 5 commits into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;

import com.firebase.ui.auth.util.SmartLock;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.credentials.CredentialsApi;
import com.google.firebase.FirebaseApp;
Expand Down Expand Up @@ -106,4 +107,8 @@ public static Intent createBaseIntent(
.putExtra(ExtraConstants.EXTRA_FLOW_PARAMS,
checkNotNull(flowParams, "flowParams cannot be null"));
}

public SmartLock getSmartLockInstance(AppCompatBase activity, String tag) {
return SmartLock.getInstance(activity, tag);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this method do anything besides pass through? Seems like we could just use the static method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It just passes through. As you noted below, this makes shadowing it possible.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ public class RegisterEmailActivity extends AppCompatBase implements View.OnClick
private EmailFieldValidator mEmailFieldValidator;
private PasswordFieldValidator mPasswordFieldValidator;
private RequiredFieldValidator mNameValidator;
private SmartLock mSmartLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_email_layout);

mSmartLock = mActivityHelper.getSmartLockInstance(this, TAG);

String email = getIntent().getStringExtra(ExtraConstants.EXTRA_EMAIL);
mEmailEditText = (EditText) findViewById(R.id.email);

Expand Down Expand Up @@ -156,13 +159,12 @@ public void onComplete(@NonNull Task<Void> task) {
// This executes even if the name change fails, since
// the account creation succeeded and we want to save
// the credential to SmartLock (if enabled).
SmartLock.getInstance(RegisterEmailActivity.this, TAG)
.saveCredentialsOrFinish(
RegisterEmailActivity.this,
mActivityHelper,
firebaseUser,
password,
null);
mSmartLock.saveCredentialsOrFinish(
RegisterEmailActivity.this,
mActivityHelper,
firebaseUser,
password,
null);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ public class SignInActivity extends AppCompatBase implements View.OnClickListene
private EditText mPasswordEditText;
private EmailFieldValidator mEmailValidator;
private RequiredFieldValidator mPasswordValidator;
private SmartLock mSmartLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in_layout);

mSmartLock = mActivityHelper.getSmartLockInstance(this, TAG);

String email = getIntent().getStringExtra(ExtraConstants.EXTRA_EMAIL);

mEmailEditText = (EditText) findViewById(R.id.email);
Expand Down Expand Up @@ -100,7 +103,7 @@ private void signIn(final String email, final String password) {
@Override
public void onSuccess(AuthResult authResult) {
// Save credential in SmartLock (if enabled)
SmartLock.getInstance(SignInActivity.this, TAG)
mSmartLock
.saveCredentialsOrFinish(SignInActivity.this,
mActivityHelper,
authResult.getUser(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ public class AuthMethodPickerActivity extends IDPBaseActivity
private static final int RC_EMAIL_FLOW = 2;
private static final int RC_ACCOUNT_LINK = 3;
private ArrayList<IdpProvider> mIdpProviders;
private SmartLock mSmartLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auth_method_picker_layout);

mSmartLock = mActivityHelper.getSmartLockInstance(this, TAG);
findViewById(R.id.email_provider).setOnClickListener(this);

populateIdpList(mActivityHelper.getFlowParams().providerInfo);
Expand Down Expand Up @@ -167,7 +168,7 @@ public void onSuccess(final IdpResponse response) {
.addOnCompleteListener(new CredentialSignInHandler(
AuthMethodPickerActivity.this,
mActivityHelper,
SmartLock.getInstance(AuthMethodPickerActivity.this, TAG),
mSmartLock,
RC_ACCOUNT_LINK,
response));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ public class IdpSignInContainerActivity extends IDPBaseActivity implements IdpCa
private IdpProvider mIdpProvider;
private String mProvider;
private String mEmail;
private SmartLock mSmartLock;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mSmartLock = mActivityHelper.getSmartLockInstance(this, TAG);
mProvider = getIntent().getStringExtra(ExtraConstants.EXTRA_PROVIDER);
mEmail = getIntent().getStringExtra(ExtraConstants.EXTRA_EMAIL);
IdpConfig providerConfig = null;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void onSuccess(final IdpResponse response) {
.addOnCompleteListener(new CredentialSignInHandler(
IdpSignInContainerActivity.this,
mActivityHelper,
SmartLock.getInstance(IdpSignInContainerActivity.this, TAG),
mSmartLock,
RC_WELCOME_BACK_IDP,
response));
}
Expand Down
19 changes: 16 additions & 3 deletions auth/src/main/java/com/firebase/ui/auth/util/SmartLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import android.content.Intent;
import android.content.IntentSender;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand All @@ -36,6 +38,7 @@
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.Builder;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.firebase.auth.FacebookAuthProvider;
Expand All @@ -49,7 +52,7 @@ public class SmartLock extends Fragment
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
ResultCallback<Status> {
private static final String TAG = "CredentialsSaveBase";
private static final String TAG = "SmartLockFragment";
private static final int RC_SAVE = 100;
private static final int RC_UPDATE_SERVICE = 28;

Expand Down Expand Up @@ -252,12 +255,18 @@ public void saveCredentialsOrFinish(AppCompatBase activity,
return;
}

mCredentialsApiClient = new GoogleApiClient.Builder(activity)
if (mActivity.isFinishing()) {
finish();
return;
}

mCredentialsApiClient = new Builder(activity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Auth.CREDENTIALS_API)
.enableAutoManage(activity, this)
.build();
mCredentialsApiClient.connect();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nothing wrong with this but the fact that you had to do it is concerning. If you can reliably reproduce a situation where this is critical could you file a bug and tell the Gms client folks about it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll see if I can isolate it.

}

public static SmartLock getInstance(AppCompatBase activity, String tag) {
Expand All @@ -269,7 +278,11 @@ public static SmartLock getInstance(AppCompatBase activity, String tag) {
Fragment fragment = fm.findFragmentByTag(tag);
if (fragment == null || !(fragment instanceof SmartLock)) {
result = new SmartLock();
ft.add(result, tag).disallowAddToBackStack().commit();
try {
ft.add(result, tag).disallowAddToBackStack().commit();
} catch (IllegalStateException e) {
Log.e(TAG, "Can not add fragment", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case should we return null or propagate the error somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed this to return null, and updated the usage to check for null.

}
} else {
result = (SmartLock) fragment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package com.firebase.ui.auth.test_helpers;

import com.firebase.ui.auth.ui.ActivityHelper;
import com.firebase.ui.auth.ui.AppCompatBase;
import com.firebase.ui.auth.util.SmartLock;
import com.google.android.gms.auth.api.credentials.CredentialsApi;
import com.google.firebase.auth.FirebaseAuth;

Expand All @@ -26,6 +28,7 @@
public class ActivityHelperShadow {
public static FirebaseAuth firebaseAuth;
public static CredentialsApi credentialsApi;
public static SmartLock smartLock;

public ActivityHelperShadow() {
if (firebaseAuth == null) {
Expand All @@ -34,6 +37,9 @@ public ActivityHelperShadow() {
if (credentialsApi == null) {
credentialsApi = Mockito.mock(CredentialsApi.class);
}
if (smartLock == null) {
smartLock = Mockito.mock(SmartLock.class);
}
}

@Implementation
Expand All @@ -45,4 +51,9 @@ public FirebaseAuth getFirebaseAuth() {
public CredentialsApi getCredentialsApi() {
return credentialsApi;
}

@Implementation
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I think I see why you implemented this method, is it just so you can shadow it easily?

public SmartLock getSmartLockInstance(AppCompatBase activity, String tag) {
return smartLock;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.AuthUI.IdpConfig;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.ui.ActivityHelper;
import com.firebase.ui.auth.ui.AppCompatBase;
import com.firebase.ui.auth.ui.FlowParameters;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
Expand All @@ -27,9 +30,13 @@

import java.util.ArrayList;
import java.util.List;
import org.mockito.ArgumentCaptor;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class TestHelper {
Expand Down Expand Up @@ -80,4 +87,24 @@ public static GoogleApiAvailability makeMockGoogleApiAvailability() {

return availability;
}

public static void verifySmartLockSave(String providerId, String email, String password) {
ArgumentCaptor<FirebaseUser> userArgumentCaptor =
ArgumentCaptor.forClass(FirebaseUser.class);
ArgumentCaptor<IdpResponse> idpResponseArgumentCaptor =
ArgumentCaptor.forClass(IdpResponse.class);
ArgumentCaptor<String> passwordArgumentCaptor = ArgumentCaptor.forClass(String.class);
verify(ActivityHelperShadow.smartLock).saveCredentialsOrFinish(
any(AppCompatBase.class), any(ActivityHelper.class), userArgumentCaptor.capture(),
passwordArgumentCaptor.capture(), idpResponseArgumentCaptor.capture());
assertEquals(email, userArgumentCaptor.getValue().getEmail());
assertEquals(password, passwordArgumentCaptor.getValue());
if (providerId == null) {
assertNull(idpResponseArgumentCaptor.getValue());
} else {
assertEquals(
providerId,
idpResponseArgumentCaptor.getValue().getProviderType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.firebase.ui.auth.test_helpers.CustomRobolectricGradleTestRunner;
import com.firebase.ui.auth.test_helpers.FakeAuthResult;
import com.firebase.ui.auth.test_helpers.FirebaseAuthWrapperImplShadow;
import com.firebase.ui.auth.test_helpers.SmartLockResult;
import com.firebase.ui.auth.test_helpers.TestConstants;
import com.firebase.ui.auth.test_helpers.TestHelper;
import com.firebase.ui.auth.util.PlayServicesHelper;
Expand All @@ -45,7 +44,6 @@

import java.util.Arrays;

import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -119,18 +117,9 @@ public void testSignUpButton_successfulRegistrationShouldContinueToSaveCredentia
true,
null));

SmartLockResult result = SmartLockResult.newInstance(registerEmailActivity,
"RegisterEmailActivity",
TestConstants.PASSWORD,
null);

Button button = (Button) registerEmailActivity.findViewById(R.id.button_create);
button.performClick();

try {
result.await();
} catch (InterruptedException e) {
assertTrue("Interrupted waiting for result", false);
}
TestHelper.verifySmartLockSave(null, TestConstants.EMAIL, TestConstants.PASSWORD);
}
}
Loading