Skip to content

Prep for final refactors #1188

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 8 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,29 @@
package com.firebase.ui.auth.data.model;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;

import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.FirebaseUiException;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class IntentRequiredException extends FirebaseUiException {
private final Intent mIntent;
private final int mRequestCode;

public IntentRequiredException(@NonNull Intent intent, int requestCode) {
super(ErrorCodes.UNKNOWN_ERROR);
mIntent = intent;
mRequestCode = requestCode;
}

@NonNull
public Intent getIntent() {
return mIntent;
}

public int getRequestCode() {
return mRequestCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.firebase.ui.auth.data.model;

import android.app.PendingIntent;
import android.support.annotation.NonNull;
import android.support.annotation.RestrictTo;

import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.FirebaseUiException;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class PendingIntentRequiredException extends FirebaseUiException {
private final PendingIntent mPendingIntent;
private final int mRequestCode;

public PendingIntentRequiredException(@NonNull PendingIntent pendingIntent, int requestCode) {
super(ErrorCodes.UNKNOWN_ERROR);
mPendingIntent = pendingIntent;
mRequestCode = requestCode;
}

@NonNull
public PendingIntent getPendingIntent() {
return mPendingIntent;
}

public int getRequestCode() {
return mRequestCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class Resource<T> {
private final T mValue;
private final Exception mException;

private boolean mUsed;

private Resource(State state, T value, Exception exception) {
mState = state;
mValue = value;
Expand Down Expand Up @@ -62,14 +64,20 @@ public State getState() {

@Nullable
public final Exception getException() {
mUsed = true;
return mException;
}

@Nullable
public T getValue() {
mUsed = true;
return mValue;
}

public boolean isUsed() {
return mUsed;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.data.model.User;
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
Expand Down
35 changes: 0 additions & 35 deletions auth/src/main/java/com/firebase/ui/auth/ui/TaskFailureLogger.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
Expand All @@ -15,17 +14,15 @@
import com.firebase.ui.auth.data.model.Resource;
import com.firebase.ui.auth.ui.HelperActivityBase;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.viewmodel.PendingResolution;
import com.firebase.ui.auth.viewmodel.ResolutionCodes;
import com.firebase.ui.auth.util.ui.FlowUtils;
import com.firebase.ui.auth.viewmodel.smartlock.SmartLockHandler;
import com.google.android.gms.auth.api.credentials.Credential;

/**
* Invisible Activity used for saving credentials to SmartLock.
*/
public class CredentialSaveActivity extends HelperActivityBase {

private static final String TAG = "SmartlockSave";
private static final String TAG = "CredentialSaveActivity";

private SmartLockHandler mHandler;
private IdpResponse mIdpResponse;
Expand All @@ -50,7 +47,7 @@ protected void onCreate(Bundle savedInstanceState) {
Credential credential = getIntent().getParcelableExtra(ExtraConstants.EXTRA_CREDENTIAL);
mIdpResponse = getIntent().getParcelableExtra(ExtraConstants.EXTRA_IDP_RESPONSE);

mHandler.getSaveOperation().observe(this, new Observer<Resource<Void>>() {
mHandler.getOperation().observe(this, new Observer<Resource<Void>>() {
@Override
public void onChanged(@Nullable Resource<Void> resource) {
if (resource == null) {
Expand All @@ -62,20 +59,8 @@ public void onChanged(@Nullable Resource<Void> resource) {
}
});

mHandler.getPendingResolution().observe(this, new Observer<PendingResolution>() {
@Override
public void onChanged(@Nullable PendingResolution resolution) {
if (resolution == null) {
Log.w(TAG, "getPendingResolution:onChanged: null");
return;
}

onPendingResolution(resolution);
}
});

// Avoid double-saving
Resource<Void> currentOp = mHandler.getSaveOperation().getValue();
Resource<Void> currentOp = mHandler.getOperation().getValue();
if (currentOp == null) {
Log.d(TAG, "Launching save operation.");
mHandler.saveCredentials(credential);
Expand All @@ -86,10 +71,8 @@ public void onChanged(@Nullable PendingResolution resolution) {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Forward activity results to the ViewModel
if (!mHandler.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure there's no issue unconditionally calling both?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, as long as we have different request codes (we do: 100 and 101). I felt like we'd get nipped in the behind later if onActivityResult was called non-deterministically. (Especially since fragments rely on that call.)

mHandler.onActivityResult(requestCode, resultCode);
}

private void onSaveOperation(@NonNull Resource<Void> resource) {
Expand All @@ -99,23 +82,11 @@ private void onSaveOperation(@NonNull Resource<Void> resource) {
break;
case SUCCESS:
case FAILURE:
finish(RESULT_OK, mIdpResponse.toIntent());
if (!resource.isUsed()
&& !FlowUtils.handleError(this, resource.getException())) {
finish(RESULT_OK, mIdpResponse.toIntent());
}
break;
}
}

private void onPendingResolution(@NonNull PendingResolution resolution) {
if (resolution.getRequestCode() == ResolutionCodes.RC_CRED_SAVE) {
try {
startIntentSenderForResult(
resolution.getPendingIntent().getIntentSender(),
resolution.getRequestCode(),
null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Failed to send resolution.", e);
finish(RESULT_OK, mIdpResponse.toIntent());
};
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void onCreate(Bundle savedInstanceState) {

mHandler = ViewModelProviders.of(this).get(RecoverPasswordHandler.class);
mHandler.init(getFlowHolder().getArguments());
mHandler.getProgressLiveData().observe(this, new Observer<Resource<String>>() {
mHandler.getOperation().observe(this, new Observer<Resource<String>>() {
@Override
public void onChanged(Resource<String> resource) {
if (resource.getState() == State.LOADING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import com.firebase.ui.auth.data.model.User;
import com.firebase.ui.auth.data.remote.ProfileMerger;
import com.firebase.ui.auth.ui.FragmentBase;
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.ui.idp.WelcomeBackIdpPrompt;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.firebase.ui.auth.util.ui.ImeHelper;
import com.firebase.ui.auth.util.ui.PreambleHandler;
import com.firebase.ui.auth.util.ui.fieldvalidators.BaseValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected void onCreate(Bundle savedInstanceState) {
mHandler.init(getFlowHolder().getArguments());

// Observe the state of the main auth operation
mHandler.getSignInOperation().observe(this, new Observer<Resource<IdpResponse>>() {
mHandler.getOperation().observe(this, new Observer<Resource<IdpResponse>>() {
@Override
public void onChanged(@Nullable Resource<IdpResponse> resource) {
onSignInOperation(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
import com.firebase.ui.auth.provider.TwitterProvider;
import com.firebase.ui.auth.ui.AppCompatBase;
import com.firebase.ui.auth.ui.HelperActivityBase;
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.ui.email.EmailActivity;
import com.firebase.ui.auth.ui.phone.PhoneActivity;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import com.firebase.ui.auth.provider.TwitterProvider;
import com.firebase.ui.auth.ui.AppCompatBase;
import com.firebase.ui.auth.ui.HelperActivityBase;
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public enum FirebaseAuthError {

ERROR_CREDENTIAL_ALREADY_IN_USE("This credential is already associated with a different user account."),

ERROR_USER_DISABLED( "The user account has been disabled by an administrator."),
ERROR_USER_DISABLED("The user account has been disabled by an administrator."),

ERROR_USER_TOKEN_EXPIRED("The user's credential has expired. The user must sign in again."),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.firebase.ui.auth.util.data;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.tasks.OnFailureListener;

public class TaskFailureLogger implements OnFailureListener {
private String mTag;
private String mMessage;

public TaskFailureLogger(@NonNull String tag, @NonNull String message) {
mTag = tag;
mMessage = message;
}

@Override
public void onFailure(@NonNull Exception e) {
Log.w(mTag, mMessage, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
import com.firebase.ui.auth.provider.TwitterProvider;
import com.firebase.ui.auth.ui.FragmentBase;
import com.firebase.ui.auth.ui.HelperActivityBase;
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.ui.idp.CredentialSignInHandler;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import com.firebase.ui.auth.data.model.FlowParameters;
import com.firebase.ui.auth.data.model.User;
import com.firebase.ui.auth.ui.FragmentBase;
import com.firebase.ui.auth.ui.TaskFailureLogger;
import com.firebase.ui.auth.ui.email.EmailActivity;
import com.firebase.ui.auth.ui.idp.AuthMethodPickerActivity;
import com.firebase.ui.auth.ui.phone.PhoneActivity;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.ui.auth.util.GoogleApiUtils;
import com.firebase.ui.auth.util.data.ProviderUtils;
import com.firebase.ui.auth.util.data.TaskFailureLogger;
import com.google.android.gms.auth.api.credentials.Credential;
import com.google.android.gms.auth.api.credentials.CredentialRequest;
import com.google.android.gms.auth.api.credentials.CredentialRequestResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public BucketedTextChangeListener(EditText editText, int expectedContentLength,
}

/**
* For example, passing in ("-", 6) would return the following result: {"", "-", "--", "---",
* "----", "-----", "------"}
* For example, passing in ("-", 6) would return the following result:
* {"", "-", "--", "---", "----", "-----", "------"}
*
* @param repeatableChar the char to repeat to the specified length
* @param length the maximum length of repeated chars
Expand Down
42 changes: 42 additions & 0 deletions auth/src/main/java/com/firebase/ui/auth/util/ui/FlowUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.firebase.ui.auth.util.ui;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.IntentSender;
import android.support.annotation.NonNull;

import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.data.model.IntentRequiredException;
import com.firebase.ui.auth.data.model.PendingIntentRequiredException;
import com.firebase.ui.auth.ui.HelperActivityBase;

public final class FlowUtils {
private FlowUtils() {
throw new AssertionError("No instance for you!");
}

public static boolean handleError(@NonNull HelperActivityBase activity, @NonNull Exception e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am totally fine with this, but do you think it makes sense to just have all of our Resources return a common exception class with a code and a cause and then we could just switch on code instead of using instanceof?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd like to, but we already have code that sets the failure to task.getException(). I'm not sure if we'd want to wrap everything in a FirebaseUiException, but that would definitely make this stuff cleaner. Ideas?

if (e instanceof IntentRequiredException) {
IntentRequiredException typed = (IntentRequiredException) e;
activity.startActivityForResult(typed.getIntent(), typed.getRequestCode());
return true;
} else if (e instanceof PendingIntentRequiredException) {
PendingIntentRequiredException typed = (PendingIntentRequiredException) e;
startIntentSenderForResult(activity, typed.getPendingIntent(), typed.getRequestCode());
return true;
}

return false;
}

private static void startIntentSenderForResult(HelperActivityBase activity,
PendingIntent intent,
int requestCode) {
try {
activity.startIntentSenderForResult(
intent.getIntentSender(), requestCode, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
activity.finish(Activity.RESULT_CANCELED, IdpResponse.getErrorIntent(e));
}
}
}
Loading