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