зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1220892 - Part 3: Remove Task sub-classes. r=mcomella
--HG-- extra : commitid : 17ZIKjkJ8OE extra : rebase_source : 2b75ff0db176019548c601e31b07ce385ad216a5 extra : histedit_source : 2e90bb0d2a628e38e1a379f346c980e9d1c25178
This commit is contained in:
Родитель
4fc6268fba
Коммит
0085b53d8d
|
@ -895,11 +895,6 @@ sync_java_files = [TOPSRCDIR + '/mobile/android/services/src/main/java/org/mozil
|
|||
'fxa/sync/FxAccountSyncStatusHelper.java',
|
||||
'fxa/sync/SchedulePolicy.java',
|
||||
'fxa/SyncStatusListener.java',
|
||||
'fxa/tasks/FxAccountCodeResender.java',
|
||||
'fxa/tasks/FxAccountCreateAccountTask.java',
|
||||
'fxa/tasks/FxAccountSetupTask.java',
|
||||
'fxa/tasks/FxAccountSignInTask.java',
|
||||
'fxa/tasks/FxAccountUnlockCodeResender.java',
|
||||
'sync/AlreadySyncingException.java',
|
||||
'sync/BackoffHandler.java',
|
||||
'sync/BadRequiredFieldJSONException.java',
|
||||
|
|
|
@ -107,8 +107,9 @@ public class RemoteTabsStaticFragment extends HomeFragment implements OnClickLis
|
|||
final EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
|
||||
mUrlOpenListener.onUrlOpen(url, flags);
|
||||
} else if (id == R.id.remote_tabs_needs_verification_resend_email) {
|
||||
// Send a fresh email; this displays a toast, so the user gets feedback.
|
||||
FirefoxAccounts.resendVerificationEmail(getActivity());
|
||||
final Intent intent = new Intent(FxAccountConstants.ACTION_FXA_CONFIRM_ACCOUNT);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startActivity(intent);
|
||||
} else if (id == R.id.remote_tabs_needs_verification_help) {
|
||||
// Don't allow switch-to-tab.
|
||||
final EnumSet<OnUrlOpenListener.Flags> flags = EnumSet.noneOf(OnUrlOpenListener.Flags.class);
|
||||
|
|
|
@ -17,7 +17,6 @@ import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
|
|||
import org.mozilla.gecko.fxa.login.State;
|
||||
import org.mozilla.gecko.fxa.sync.FxAccountSyncAdapter;
|
||||
import org.mozilla.gecko.fxa.sync.FxAccountSyncStatusHelper;
|
||||
import org.mozilla.gecko.fxa.tasks.FxAccountCodeResender;
|
||||
import org.mozilla.gecko.sync.ThreadPool;
|
||||
import org.mozilla.gecko.sync.Utils;
|
||||
|
||||
|
@ -26,7 +25,6 @@ import android.accounts.AccountManager;
|
|||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
|
||||
/**
|
||||
|
@ -303,26 +301,4 @@ public class FirefoxAccounts {
|
|||
final String LOCALE = Utils.getLanguageTag(locale);
|
||||
return res.getString(R.string.fxaccount_link_old_firefox, VERSION, OS, LOCALE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resends the account verification email, and displays an appropriate
|
||||
* toast on both send success and failure. Note that because the underlying implementation
|
||||
* uses {@link AsyncTask}, the provided context must be UI-capable, and this
|
||||
* method called from the UI thread (see
|
||||
* {@link org.mozilla.gecko.fxa.tasks.FxAccountCodeResender#resendCode(Context, AndroidFxAccount)}
|
||||
* for more).
|
||||
*
|
||||
* @param context a UI-capable Android context.
|
||||
* @return true if an account exists, false otherwise.
|
||||
*/
|
||||
public static boolean resendVerificationEmail(final Context context) {
|
||||
final Account account = getFirefoxAccount(context);
|
||||
if (account == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);
|
||||
FxAccountCodeResender.resendCode(context, fxAccount);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,66 +4,7 @@
|
|||
|
||||
package org.mozilla.gecko.fxa.activities;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mozilla.gecko.AppConstants;
|
||||
import org.mozilla.gecko.R;
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountUtils;
|
||||
import org.mozilla.gecko.background.fxa.PasswordStretcher;
|
||||
import org.mozilla.gecko.background.fxa.QuickPasswordStretcher;
|
||||
import org.mozilla.gecko.fxa.FxAccountConstants;
|
||||
import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
|
||||
import org.mozilla.gecko.fxa.login.Engaged;
|
||||
import org.mozilla.gecko.fxa.login.State;
|
||||
import org.mozilla.gecko.fxa.tasks.FxAccountSetupTask.ProgressDisplay;
|
||||
import org.mozilla.gecko.fxa.tasks.FxAccountUnlockCodeResender;
|
||||
import org.mozilla.gecko.sync.ExtendedJSONObject;
|
||||
import org.mozilla.gecko.sync.SyncConfiguration;
|
||||
import org.mozilla.gecko.sync.Utils;
|
||||
import org.mozilla.gecko.sync.setup.Constants;
|
||||
import org.mozilla.gecko.sync.setup.activities.ActivityUtils;
|
||||
import org.mozilla.gecko.util.ColorUtils;
|
||||
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.animation.LayoutTransition;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.Spannable;
|
||||
import android.text.TextWatcher;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.text.method.PasswordTransformationMethod;
|
||||
import android.text.method.SingleLineTransformationMethod;
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.util.Patterns;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.View.OnFocusChangeListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.AutoCompleteTextView;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
|
||||
abstract public class FxAccountAbstractSetupActivity extends FxAccountAbstractActivity implements ProgressDisplay {
|
||||
abstract public class FxAccountAbstractSetupActivity extends FxAccountAbstractActivity {
|
||||
public static final String EXTRA_EMAIL = "email";
|
||||
public static final String EXTRA_PASSWORD = "password";
|
||||
public static final String EXTRA_PASSWORD_SHOWN = "password_shown";
|
||||
|
|
|
@ -38,7 +38,6 @@ import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
|
|||
import org.mozilla.gecko.fxa.login.Married;
|
||||
import org.mozilla.gecko.fxa.login.State;
|
||||
import org.mozilla.gecko.fxa.sync.FxAccountSyncStatusHelper;
|
||||
import org.mozilla.gecko.fxa.tasks.FxAccountCodeResender;
|
||||
import org.mozilla.gecko.sync.ExtendedJSONObject;
|
||||
import org.mozilla.gecko.sync.SharedPreferencesClientsDataDelegate;
|
||||
import org.mozilla.gecko.sync.SyncConfiguration;
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.fxa.tasks;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.mozilla.gecko.R;
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient20;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
|
||||
import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
|
||||
import org.mozilla.gecko.fxa.login.Engaged;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* A helper class that provides a simple interface for requesting
|
||||
* a Firefox Account verification email to be resent.
|
||||
*/
|
||||
public class FxAccountCodeResender {
|
||||
private static final String LOG_TAG = FxAccountCodeResender.class.getSimpleName();
|
||||
|
||||
private static class FxAccountResendCodeTask extends FxAccountSetupTask<Void> {
|
||||
protected static final String LOG_TAG = FxAccountResendCodeTask.class.getSimpleName();
|
||||
|
||||
protected final byte[] sessionToken;
|
||||
|
||||
public FxAccountResendCodeTask(Context context, byte[] sessionToken, FxAccountClient client, RequestDelegate<Void> delegate) {
|
||||
super(context, null, client, null, delegate);
|
||||
this.sessionToken = sessionToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InnerRequestDelegate<Void> doInBackground(Void... arg0) {
|
||||
try {
|
||||
client.resendCode(sessionToken, innerDelegate);
|
||||
latch.await();
|
||||
return innerDelegate;
|
||||
} catch (Exception e) {
|
||||
Logger.error(LOG_TAG, "Got exception signing in.", e);
|
||||
delegate.handleError(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ResendCodeDelegate implements RequestDelegate<Void> {
|
||||
public final Context context;
|
||||
|
||||
public ResendCodeDelegate(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(Exception e) {
|
||||
Logger.warn(LOG_TAG, "Got exception requesting fresh confirmation link; ignoring.", e);
|
||||
Toast.makeText(context, R.string.fxaccount_confirm_account_verification_link_not_sent, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleFailure(FxAccountClientRemoteException e) {
|
||||
handleError(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleSuccess(Void result) {
|
||||
Toast.makeText(context, R.string.fxaccount_confirm_account_verification_link_sent, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resends the account verification email, and displays an appropriate
|
||||
* toast on both send success and failure. Note that because the underlying implementation
|
||||
* uses {@link AsyncTask}, the provided context must be UI-capable and
|
||||
* this method called from the UI thread.
|
||||
*
|
||||
* Note that it may actually be possible to run this (and the {@link AsyncTask}) method
|
||||
* from a background thread - but this hasn't been tested.
|
||||
*
|
||||
* @param context A UI-capable Android context.
|
||||
* @param fxAccount The Firefox Account to resend the code to.
|
||||
*/
|
||||
public static void resendCode(Context context, AndroidFxAccount fxAccount) {
|
||||
RequestDelegate<Void> delegate = new ResendCodeDelegate(context);
|
||||
|
||||
byte[] sessionToken;
|
||||
try {
|
||||
sessionToken = ((Engaged) fxAccount.getState()).getSessionToken();
|
||||
} catch (Exception e) {
|
||||
delegate.handleError(e);
|
||||
return;
|
||||
}
|
||||
if (sessionToken == null) {
|
||||
delegate.handleError(new IllegalStateException("sessionToken should not be null"));
|
||||
return;
|
||||
}
|
||||
|
||||
Executor executor = Executors.newSingleThreadExecutor();
|
||||
FxAccountClient client = new FxAccountClient20(fxAccount.getAccountServerURI(), executor);
|
||||
new FxAccountResendCodeTask(context, sessionToken, client, delegate).execute();
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.fxa.tasks;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse;
|
||||
import org.mozilla.gecko.background.fxa.PasswordStretcher;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class FxAccountCreateAccountTask extends FxAccountSetupTask<LoginResponse> {
|
||||
private static final String LOG_TAG = FxAccountCreateAccountTask.class.getSimpleName();
|
||||
|
||||
protected final byte[] emailUTF8;
|
||||
protected final PasswordStretcher passwordStretcher;
|
||||
|
||||
public FxAccountCreateAccountTask(Context context, ProgressDisplay progressDisplay, String email, PasswordStretcher passwordStretcher, FxAccountClient client, Map<String, String> queryParameters, RequestDelegate<LoginResponse> delegate) throws UnsupportedEncodingException {
|
||||
super(context, progressDisplay, client, queryParameters, delegate);
|
||||
this.emailUTF8 = email.getBytes("UTF-8");
|
||||
this.passwordStretcher = passwordStretcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InnerRequestDelegate<LoginResponse> doInBackground(Void... arg0) {
|
||||
try {
|
||||
client.createAccountAndGetKeys(emailUTF8, passwordStretcher, queryParameters, innerDelegate);
|
||||
latch.await();
|
||||
return innerDelegate;
|
||||
} catch (Exception e) {
|
||||
Logger.error(LOG_TAG, "Got exception logging in.", e);
|
||||
delegate.handleError(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.fxa.tasks;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
|
||||
import org.mozilla.gecko.fxa.tasks.FxAccountSetupTask.InnerRequestDelegate;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
/**
|
||||
* An <code>AsyncTask</code> wrapper around signing up for, and signing in to, a
|
||||
* Firefox Account.
|
||||
* <p>
|
||||
* It's strange to add explicit blocking to callback-threading code, but we do
|
||||
* it here to take advantage of Android's built in support for background work.
|
||||
* We really want to avoid making a threading mistake that brings down the whole
|
||||
* process.
|
||||
*/
|
||||
public abstract class FxAccountSetupTask<T> extends AsyncTask<Void, Void, InnerRequestDelegate<T>> {
|
||||
private static final String LOG_TAG = FxAccountSetupTask.class.getSimpleName();
|
||||
|
||||
public interface ProgressDisplay {
|
||||
public void showProgress();
|
||||
public void dismissProgress();
|
||||
}
|
||||
|
||||
protected final Context context;
|
||||
protected final FxAccountClient client;
|
||||
protected final ProgressDisplay progressDisplay;
|
||||
|
||||
// Initialized lazily.
|
||||
protected byte[] quickStretchedPW;
|
||||
|
||||
// AsyncTask's are one-time-use, so final members are fine.
|
||||
protected final CountDownLatch latch = new CountDownLatch(1);
|
||||
protected final InnerRequestDelegate<T> innerDelegate = new InnerRequestDelegate<T>(latch);
|
||||
|
||||
protected final Map<String, String> queryParameters;
|
||||
|
||||
protected final RequestDelegate<T> delegate;
|
||||
|
||||
public FxAccountSetupTask(Context context, ProgressDisplay progressDisplay, FxAccountClient client, Map<String, String> queryParameters, RequestDelegate<T> delegate) {
|
||||
this.context = context;
|
||||
this.client = client;
|
||||
this.delegate = delegate;
|
||||
this.progressDisplay = progressDisplay;
|
||||
this.queryParameters = queryParameters;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
if (progressDisplay != null) {
|
||||
progressDisplay.showProgress();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(InnerRequestDelegate<T> result) {
|
||||
if (progressDisplay != null) {
|
||||
progressDisplay.dismissProgress();
|
||||
}
|
||||
|
||||
// We are on the UI thread, and need to invoke these callbacks here to allow UI updating.
|
||||
if (innerDelegate.failure != null) {
|
||||
delegate.handleFailure(innerDelegate.failure);
|
||||
} else if (innerDelegate.exception != null) {
|
||||
delegate.handleError(innerDelegate.exception);
|
||||
} else {
|
||||
delegate.handleSuccess(result.response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled(InnerRequestDelegate<T> result) {
|
||||
if (progressDisplay != null) {
|
||||
progressDisplay.dismissProgress();
|
||||
}
|
||||
delegate.handleError(new IllegalStateException("Task was cancelled."));
|
||||
}
|
||||
|
||||
protected static class InnerRequestDelegate<T> implements RequestDelegate<T> {
|
||||
protected final CountDownLatch latch;
|
||||
public T response;
|
||||
public Exception exception;
|
||||
public FxAccountClientRemoteException failure;
|
||||
|
||||
protected InnerRequestDelegate(CountDownLatch latch) {
|
||||
this.latch = latch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(Exception e) {
|
||||
Logger.error(LOG_TAG, "Got exception.");
|
||||
this.exception = e;
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleFailure(FxAccountClientRemoteException e) {
|
||||
Logger.warn(LOG_TAG, "Got failure.");
|
||||
this.failure = e;
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleSuccess(T result) {
|
||||
Logger.info(LOG_TAG, "Got success.");
|
||||
this.response = result;
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.fxa.tasks;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse;
|
||||
import org.mozilla.gecko.background.fxa.PasswordStretcher;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class FxAccountSignInTask extends FxAccountSetupTask<LoginResponse> {
|
||||
protected static final String LOG_TAG = FxAccountSignInTask.class.getSimpleName();
|
||||
|
||||
protected final byte[] emailUTF8;
|
||||
protected final PasswordStretcher passwordStretcher;
|
||||
|
||||
public FxAccountSignInTask(Context context, ProgressDisplay progressDisplay, String email, PasswordStretcher passwordStretcher, FxAccountClient client, Map<String, String> queryParameters, RequestDelegate<LoginResponse> delegate) throws UnsupportedEncodingException {
|
||||
super(context, progressDisplay, client, queryParameters, delegate);
|
||||
this.emailUTF8 = email.getBytes("UTF-8");
|
||||
this.passwordStretcher = passwordStretcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InnerRequestDelegate<LoginResponse> doInBackground(Void... arg0) {
|
||||
try {
|
||||
client.loginAndGetKeys(emailUTF8, passwordStretcher, queryParameters, innerDelegate);
|
||||
latch.await();
|
||||
return innerDelegate;
|
||||
} catch (Exception e) {
|
||||
Logger.error(LOG_TAG, "Got exception signing in.", e);
|
||||
delegate.handleError(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,105 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.fxa.tasks;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.mozilla.gecko.R;
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClient20;
|
||||
import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* A helper class that provides a simple interface for requesting a Firefox
|
||||
* Account unlock account email be (re)-sent.
|
||||
*/
|
||||
public class FxAccountUnlockCodeResender {
|
||||
private static final String LOG_TAG = FxAccountUnlockCodeResender.class.getSimpleName();
|
||||
|
||||
private static class FxAccountUnlockCodeTask extends FxAccountSetupTask<Void> {
|
||||
protected static final String LOG_TAG = FxAccountUnlockCodeTask.class.getSimpleName();
|
||||
|
||||
protected final byte[] emailUTF8;
|
||||
|
||||
public FxAccountUnlockCodeTask(Context context, byte[] emailUTF8, FxAccountClient client, RequestDelegate<Void> delegate) {
|
||||
super(context, null, client, null, delegate);
|
||||
this.emailUTF8 = emailUTF8;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InnerRequestDelegate<Void> doInBackground(Void... arg0) {
|
||||
try {
|
||||
client.resendUnlockCode(emailUTF8, innerDelegate);
|
||||
latch.await();
|
||||
return innerDelegate;
|
||||
} catch (Exception e) {
|
||||
Logger.error(LOG_TAG, "Got exception signing in.", e);
|
||||
delegate.handleError(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ResendUnlockCodeDelegate implements RequestDelegate<Void> {
|
||||
public final Context context;
|
||||
|
||||
public ResendUnlockCodeDelegate(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(Exception e) {
|
||||
Logger.warn(LOG_TAG, "Got exception requesting fresh unlock code; ignoring.", e);
|
||||
Toast.makeText(context, R.string.fxaccount_unlock_code_not_sent, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleFailure(FxAccountClientRemoteException e) {
|
||||
handleError(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleSuccess(Void result) {
|
||||
Toast.makeText(context, R.string.fxaccount_unlock_code_sent, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resends the account unlock email, and displays an appropriate toast on both
|
||||
* send success and failure. Note that because the underlying implementation
|
||||
* uses {@link AsyncTask}, the provided context must be UI-capable and this
|
||||
* method called from the UI thread.
|
||||
*
|
||||
* Note that it may actually be possible to run this (and the
|
||||
* {@link AsyncTask}) method from a background thread - but this hasn't been
|
||||
* tested.
|
||||
*
|
||||
* @param context
|
||||
* A UI-capable Android context.
|
||||
* @param authServerURI
|
||||
* to send request to.
|
||||
* @param emailUTF8
|
||||
* bytes of email address identifying account; null indicates a local failure.
|
||||
*/
|
||||
public static void resendUnlockCode(Context context, String authServerURI, byte[] emailUTF8) {
|
||||
RequestDelegate<Void> delegate = new ResendUnlockCodeDelegate(context);
|
||||
|
||||
if (emailUTF8 == null) {
|
||||
delegate.handleError(new IllegalArgumentException("emailUTF8 must not be null"));
|
||||
return;
|
||||
}
|
||||
|
||||
final Executor executor = Executors.newSingleThreadExecutor();
|
||||
final FxAccountClient client = new FxAccountClient20(authServerURI, executor);
|
||||
new FxAccountUnlockCodeTask(context, emailUTF8, client, delegate).execute();
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче