mobile/android/base/fxa/activities/FxAccountAbstractActivity.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.fxa.activities;
michael@0 6
michael@0 7 import org.mozilla.gecko.background.common.log.Logger;
michael@0 8 import org.mozilla.gecko.background.fxa.FxAccountAgeLockoutHelper;
michael@0 9 import org.mozilla.gecko.fxa.FirefoxAccounts;
michael@0 10 import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
michael@0 11 import org.mozilla.gecko.sync.setup.activities.ActivityUtils;
michael@0 12 import org.mozilla.gecko.sync.setup.activities.LocaleAware.LocaleAwareActivity;
michael@0 13
michael@0 14 import android.accounts.Account;
michael@0 15 import android.app.Activity;
michael@0 16 import android.content.Intent;
michael@0 17 import android.os.SystemClock;
michael@0 18 import android.view.View;
michael@0 19 import android.view.View.OnClickListener;
michael@0 20 import android.widget.TextView;
michael@0 21
michael@0 22 public abstract class FxAccountAbstractActivity extends LocaleAwareActivity {
michael@0 23 private static final String LOG_TAG = FxAccountAbstractActivity.class.getSimpleName();
michael@0 24
michael@0 25 protected final boolean cannotResumeWhenAccountsExist;
michael@0 26 protected final boolean cannotResumeWhenNoAccountsExist;
michael@0 27 protected final boolean cannotResumeWhenLockedOut;
michael@0 28
michael@0 29 public static final int CAN_ALWAYS_RESUME = 0;
michael@0 30 public static final int CANNOT_RESUME_WHEN_ACCOUNTS_EXIST = 1 << 0;
michael@0 31 public static final int CANNOT_RESUME_WHEN_NO_ACCOUNTS_EXIST = 1 << 1;
michael@0 32 public static final int CANNOT_RESUME_WHEN_LOCKED_OUT = 1 << 2;
michael@0 33
michael@0 34 public FxAccountAbstractActivity(int resume) {
michael@0 35 super();
michael@0 36 this.cannotResumeWhenAccountsExist = 0 != (resume & CANNOT_RESUME_WHEN_ACCOUNTS_EXIST);
michael@0 37 this.cannotResumeWhenNoAccountsExist = 0 != (resume & CANNOT_RESUME_WHEN_NO_ACCOUNTS_EXIST);
michael@0 38 this.cannotResumeWhenLockedOut = 0 != (resume & CANNOT_RESUME_WHEN_LOCKED_OUT);
michael@0 39 }
michael@0 40
michael@0 41 /**
michael@0 42 * Many Firefox Accounts activities shouldn't display if an account already
michael@0 43 * exists or if account creation is locked out due to an age verification
michael@0 44 * check failing (getting started, create account, sign in). This function
michael@0 45 * redirects as appropriate.
michael@0 46 */
michael@0 47 protected void redirectIfAppropriate() {
michael@0 48 if (cannotResumeWhenAccountsExist || cannotResumeWhenNoAccountsExist) {
michael@0 49 final Account account = FirefoxAccounts.getFirefoxAccount(this);
michael@0 50 if (cannotResumeWhenAccountsExist && account != null) {
michael@0 51 redirectToActivity(FxAccountStatusActivity.class);
michael@0 52 return;
michael@0 53 }
michael@0 54 if (cannotResumeWhenNoAccountsExist && account == null) {
michael@0 55 redirectToActivity(FxAccountGetStartedActivity.class);
michael@0 56 return;
michael@0 57 }
michael@0 58 }
michael@0 59 if (cannotResumeWhenLockedOut) {
michael@0 60 if (FxAccountAgeLockoutHelper.isLockedOut(SystemClock.elapsedRealtime())) {
michael@0 61 this.setResult(RESULT_CANCELED);
michael@0 62 redirectToActivity(FxAccountCreateAccountNotAllowedActivity.class);
michael@0 63 return;
michael@0 64 }
michael@0 65 }
michael@0 66 }
michael@0 67
michael@0 68 @Override
michael@0 69 public void onResume() {
michael@0 70 super.onResume();
michael@0 71 redirectIfAppropriate();
michael@0 72 }
michael@0 73
michael@0 74 protected void launchActivity(Class<? extends Activity> activityClass) {
michael@0 75 Intent intent = new Intent(this, activityClass);
michael@0 76 // Per http://stackoverflow.com/a/8992365, this triggers a known bug with
michael@0 77 // the soft keyboard not being shown for the started activity. Why, Android, why?
michael@0 78 intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
michael@0 79 startActivity(intent);
michael@0 80 }
michael@0 81
michael@0 82 protected void redirectToActivity(Class<? extends Activity> activityClass) {
michael@0 83 launchActivity(activityClass);
michael@0 84 finish();
michael@0 85 }
michael@0 86
michael@0 87 /**
michael@0 88 * Helper to find view or error if it is missing.
michael@0 89 *
michael@0 90 * @param id of view to find.
michael@0 91 * @param description to print in error.
michael@0 92 * @return non-null <code>View</code> instance.
michael@0 93 */
michael@0 94 public View ensureFindViewById(View v, int id, String description) {
michael@0 95 View view;
michael@0 96 if (v != null) {
michael@0 97 view = v.findViewById(id);
michael@0 98 } else {
michael@0 99 view = findViewById(id);
michael@0 100 }
michael@0 101 if (view == null) {
michael@0 102 String message = "Could not find view " + description + ".";
michael@0 103 Logger.error(LOG_TAG, message);
michael@0 104 throw new RuntimeException(message);
michael@0 105 }
michael@0 106 return view;
michael@0 107 }
michael@0 108
michael@0 109 public void linkifyTextViews(View view, int[] textViews) {
michael@0 110 for (int id : textViews) {
michael@0 111 TextView textView;
michael@0 112 if (view != null) {
michael@0 113 textView = (TextView) view.findViewById(id);
michael@0 114 } else {
michael@0 115 textView = (TextView) findViewById(id);
michael@0 116 }
michael@0 117
michael@0 118 if (textView == null) {
michael@0 119 Logger.warn(LOG_TAG, "Could not process links for view with id " + id + ".");
michael@0 120 continue;
michael@0 121 }
michael@0 122
michael@0 123 ActivityUtils.linkifyTextView(textView, false);
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 protected void launchActivityOnClick(final View view, final Class<? extends Activity> activityClass) {
michael@0 128 view.setOnClickListener(new OnClickListener() {
michael@0 129 @Override
michael@0 130 public void onClick(View v) {
michael@0 131 FxAccountAbstractActivity.this.launchActivity(activityClass);
michael@0 132 }
michael@0 133 });
michael@0 134 }
michael@0 135
michael@0 136 /**
michael@0 137 * Helper to fetch (unique) Android Firefox Account if one exists, or return null.
michael@0 138 */
michael@0 139 protected AndroidFxAccount getAndroidFxAccount() {
michael@0 140 Account account = FirefoxAccounts.getFirefoxAccount(this);
michael@0 141 if (account == null) {
michael@0 142 return null;
michael@0 143 }
michael@0 144 return new AndroidFxAccount(this, account);
michael@0 145 }
michael@0 146 }

mercurial