1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/fxa/activities/FxAccountConfirmAccountActivity.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,239 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.fxa.activities; 1.9 + 1.10 +import java.util.concurrent.Executor; 1.11 +import java.util.concurrent.Executors; 1.12 + 1.13 +import org.mozilla.gecko.R; 1.14 +import org.mozilla.gecko.background.common.log.Logger; 1.15 +import org.mozilla.gecko.background.fxa.FxAccountClient; 1.16 +import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate; 1.17 +import org.mozilla.gecko.background.fxa.FxAccountClient20; 1.18 +import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException; 1.19 +import org.mozilla.gecko.fxa.FirefoxAccounts; 1.20 +import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; 1.21 +import org.mozilla.gecko.fxa.login.Engaged; 1.22 +import org.mozilla.gecko.fxa.login.State; 1.23 +import org.mozilla.gecko.fxa.login.State.Action; 1.24 +import org.mozilla.gecko.fxa.sync.FxAccountSyncStatusHelper; 1.25 +import org.mozilla.gecko.sync.setup.activities.ActivityUtils; 1.26 + 1.27 +import android.accounts.Account; 1.28 +import android.app.Activity; 1.29 +import android.content.Context; 1.30 +import android.os.Bundle; 1.31 +import android.view.View; 1.32 +import android.view.View.OnClickListener; 1.33 +import android.widget.TextView; 1.34 +import android.widget.Toast; 1.35 + 1.36 +/** 1.37 + * Activity which displays account created successfully screen to the user, and 1.38 + * starts them on the email verification path. 1.39 + */ 1.40 +public class FxAccountConfirmAccountActivity extends FxAccountAbstractActivity implements OnClickListener { 1.41 + private static final String LOG_TAG = FxAccountConfirmAccountActivity.class.getSimpleName(); 1.42 + 1.43 + // Set in onCreate. 1.44 + protected TextView verificationLinkTextView; 1.45 + protected View resendLink; 1.46 + 1.47 + // Set in onResume. 1.48 + protected AndroidFxAccount fxAccount; 1.49 + 1.50 + protected final InnerSyncStatusDelegate syncStatusDelegate = new InnerSyncStatusDelegate(); 1.51 + 1.52 + public FxAccountConfirmAccountActivity() { 1.53 + super(CANNOT_RESUME_WHEN_NO_ACCOUNTS_EXIST); 1.54 + } 1.55 + 1.56 + /** 1.57 + * {@inheritDoc} 1.58 + */ 1.59 + @Override 1.60 + public void onCreate(Bundle icicle) { 1.61 + Logger.debug(LOG_TAG, "onCreate(" + icicle + ")"); 1.62 + 1.63 + super.onCreate(icicle); 1.64 + setContentView(R.layout.fxaccount_confirm_account); 1.65 + 1.66 + verificationLinkTextView = (TextView) ensureFindViewById(null, R.id.verification_link_text, "verification link text"); 1.67 + resendLink = ensureFindViewById(null, R.id.resend_confirmation_email_link, "resend confirmation email link"); 1.68 + resendLink.setOnClickListener(this); 1.69 + 1.70 + View backToBrowsingButton = ensureFindViewById(null, R.id.button, "back to browsing button"); 1.71 + backToBrowsingButton.setOnClickListener(new OnClickListener() { 1.72 + @Override 1.73 + public void onClick(View v) { 1.74 + ActivityUtils.openURLInFennec(v.getContext(), null); 1.75 + setResult(Activity.RESULT_OK); 1.76 + finish(); 1.77 + } 1.78 + }); 1.79 + } 1.80 + 1.81 + @Override 1.82 + public void onResume() { 1.83 + super.onResume(); 1.84 + this.fxAccount = getAndroidFxAccount(); 1.85 + if (fxAccount == null) { 1.86 + Logger.warn(LOG_TAG, "Could not get Firefox Account."); 1.87 + setResult(RESULT_CANCELED); 1.88 + finish(); 1.89 + return; 1.90 + } 1.91 + 1.92 + FxAccountSyncStatusHelper.getInstance().startObserving(syncStatusDelegate); 1.93 + 1.94 + refresh(); 1.95 + 1.96 + fxAccount.requestSync(FirefoxAccounts.NOW); 1.97 + } 1.98 + 1.99 + @Override 1.100 + public void onPause() { 1.101 + super.onPause(); 1.102 + FxAccountSyncStatusHelper.getInstance().stopObserving(syncStatusDelegate); 1.103 + 1.104 + if (fxAccount != null) { 1.105 + fxAccount.requestSync(FirefoxAccounts.SOON); 1.106 + } 1.107 + } 1.108 + 1.109 + protected class InnerSyncStatusDelegate implements FirefoxAccounts.SyncStatusListener { 1.110 + protected final Runnable refreshRunnable = new Runnable() { 1.111 + @Override 1.112 + public void run() { 1.113 + refresh(); 1.114 + } 1.115 + }; 1.116 + 1.117 + @Override 1.118 + public Context getContext() { 1.119 + return FxAccountConfirmAccountActivity.this; 1.120 + } 1.121 + 1.122 + @Override 1.123 + public Account getAccount() { 1.124 + return fxAccount.getAndroidAccount(); 1.125 + } 1.126 + 1.127 + @Override 1.128 + public void onSyncStarted() { 1.129 + Logger.info(LOG_TAG, "Got sync started message; ignoring."); 1.130 + } 1.131 + 1.132 + @Override 1.133 + public void onSyncFinished() { 1.134 + if (fxAccount == null) { 1.135 + return; 1.136 + } 1.137 + Logger.info(LOG_TAG, "Got sync finished message; refreshing."); 1.138 + runOnUiThread(refreshRunnable); 1.139 + } 1.140 + } 1.141 + 1.142 + protected void refresh() { 1.143 + final State state = fxAccount.getState(); 1.144 + final Action neededAction = state.getNeededAction(); 1.145 + switch (neededAction) { 1.146 + case NeedsVerification: 1.147 + // This is what we're here to handle. 1.148 + break; 1.149 + case NeedsPassword: 1.150 + case NeedsUpgrade: 1.151 + case None: 1.152 + default: 1.153 + // We're not in the right place! Redirect to status. 1.154 + Logger.warn(LOG_TAG, "No need to verifiy Firefox Account that needs action " + neededAction.toString() + 1.155 + " (in state " + state.getStateLabel() + ")."); 1.156 + setResult(RESULT_CANCELED); 1.157 + this.redirectToActivity(FxAccountStatusActivity.class); 1.158 + return; 1.159 + } 1.160 + 1.161 + final String email = fxAccount.getEmail(); 1.162 + final String text = getResources().getString(R.string.fxaccount_confirm_account_verification_link, email); 1.163 + verificationLinkTextView.setText(text); 1.164 + 1.165 + boolean resendLinkShouldBeEnabled = ((Engaged) state).getSessionToken() != null; 1.166 + resendLink.setEnabled(resendLinkShouldBeEnabled); 1.167 + resendLink.setClickable(resendLinkShouldBeEnabled); 1.168 + } 1.169 + 1.170 + public static class FxAccountResendCodeTask extends FxAccountSetupTask<Void> { 1.171 + protected static final String LOG_TAG = FxAccountResendCodeTask.class.getSimpleName(); 1.172 + 1.173 + protected final byte[] sessionToken; 1.174 + 1.175 + public FxAccountResendCodeTask(Context context, byte[] sessionToken, FxAccountClient client, RequestDelegate<Void> delegate) { 1.176 + super(context, null, client, delegate); 1.177 + this.sessionToken = sessionToken; 1.178 + } 1.179 + 1.180 + @Override 1.181 + protected InnerRequestDelegate<Void> doInBackground(Void... arg0) { 1.182 + try { 1.183 + client.resendCode(sessionToken, innerDelegate); 1.184 + latch.await(); 1.185 + return innerDelegate; 1.186 + } catch (Exception e) { 1.187 + Logger.error(LOG_TAG, "Got exception signing in.", e); 1.188 + delegate.handleError(e); 1.189 + } 1.190 + return null; 1.191 + } 1.192 + } 1.193 + 1.194 + protected static class ResendCodeDelegate implements RequestDelegate<Void> { 1.195 + public final Context context; 1.196 + 1.197 + public ResendCodeDelegate(Context context) { 1.198 + this.context = context; 1.199 + } 1.200 + 1.201 + @Override 1.202 + public void handleError(Exception e) { 1.203 + Logger.warn(LOG_TAG, "Got exception requesting fresh confirmation link; ignoring.", e); 1.204 + Toast.makeText(context, R.string.fxaccount_confirm_account_verification_link_not_sent, Toast.LENGTH_LONG).show(); 1.205 + } 1.206 + 1.207 + @Override 1.208 + public void handleFailure(FxAccountClientRemoteException e) { 1.209 + handleError(e); 1.210 + } 1.211 + 1.212 + @Override 1.213 + public void handleSuccess(Void result) { 1.214 + Toast.makeText(context, R.string.fxaccount_confirm_account_verification_link_sent, Toast.LENGTH_SHORT).show(); 1.215 + } 1.216 + } 1.217 + 1.218 + public static void resendCode(Context context, AndroidFxAccount fxAccount) { 1.219 + RequestDelegate<Void> delegate = new ResendCodeDelegate(context); 1.220 + 1.221 + byte[] sessionToken; 1.222 + try { 1.223 + sessionToken = ((Engaged) fxAccount.getState()).getSessionToken(); 1.224 + } catch (Exception e) { 1.225 + delegate.handleError(e); 1.226 + return; 1.227 + } 1.228 + if (sessionToken == null) { 1.229 + delegate.handleError(new IllegalStateException("sessionToken should not be null")); 1.230 + return; 1.231 + } 1.232 + 1.233 + Executor executor = Executors.newSingleThreadExecutor(); 1.234 + FxAccountClient client = new FxAccountClient20(fxAccount.getAccountServerURI(), executor); 1.235 + new FxAccountResendCodeTask(context, sessionToken, client, delegate).execute(); 1.236 + } 1.237 + 1.238 + @Override 1.239 + public void onClick(View v) { 1.240 + resendCode(this, fxAccount); 1.241 + } 1.242 +}