michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.fxa.activities; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.background.fxa.FxAccountAgeLockoutHelper; michael@0: import org.mozilla.gecko.fxa.FirefoxAccounts; michael@0: import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; michael@0: import org.mozilla.gecko.sync.setup.activities.ActivityUtils; michael@0: import org.mozilla.gecko.sync.setup.activities.LocaleAware.LocaleAwareActivity; michael@0: michael@0: import android.accounts.Account; michael@0: import android.app.Activity; michael@0: import android.content.Intent; michael@0: import android.os.SystemClock; michael@0: import android.view.View; michael@0: import android.view.View.OnClickListener; michael@0: import android.widget.TextView; michael@0: michael@0: public abstract class FxAccountAbstractActivity extends LocaleAwareActivity { michael@0: private static final String LOG_TAG = FxAccountAbstractActivity.class.getSimpleName(); michael@0: michael@0: protected final boolean cannotResumeWhenAccountsExist; michael@0: protected final boolean cannotResumeWhenNoAccountsExist; michael@0: protected final boolean cannotResumeWhenLockedOut; michael@0: michael@0: public static final int CAN_ALWAYS_RESUME = 0; michael@0: public static final int CANNOT_RESUME_WHEN_ACCOUNTS_EXIST = 1 << 0; michael@0: public static final int CANNOT_RESUME_WHEN_NO_ACCOUNTS_EXIST = 1 << 1; michael@0: public static final int CANNOT_RESUME_WHEN_LOCKED_OUT = 1 << 2; michael@0: michael@0: public FxAccountAbstractActivity(int resume) { michael@0: super(); michael@0: this.cannotResumeWhenAccountsExist = 0 != (resume & CANNOT_RESUME_WHEN_ACCOUNTS_EXIST); michael@0: this.cannotResumeWhenNoAccountsExist = 0 != (resume & CANNOT_RESUME_WHEN_NO_ACCOUNTS_EXIST); michael@0: this.cannotResumeWhenLockedOut = 0 != (resume & CANNOT_RESUME_WHEN_LOCKED_OUT); michael@0: } michael@0: michael@0: /** michael@0: * Many Firefox Accounts activities shouldn't display if an account already michael@0: * exists or if account creation is locked out due to an age verification michael@0: * check failing (getting started, create account, sign in). This function michael@0: * redirects as appropriate. michael@0: */ michael@0: protected void redirectIfAppropriate() { michael@0: if (cannotResumeWhenAccountsExist || cannotResumeWhenNoAccountsExist) { michael@0: final Account account = FirefoxAccounts.getFirefoxAccount(this); michael@0: if (cannotResumeWhenAccountsExist && account != null) { michael@0: redirectToActivity(FxAccountStatusActivity.class); michael@0: return; michael@0: } michael@0: if (cannotResumeWhenNoAccountsExist && account == null) { michael@0: redirectToActivity(FxAccountGetStartedActivity.class); michael@0: return; michael@0: } michael@0: } michael@0: if (cannotResumeWhenLockedOut) { michael@0: if (FxAccountAgeLockoutHelper.isLockedOut(SystemClock.elapsedRealtime())) { michael@0: this.setResult(RESULT_CANCELED); michael@0: redirectToActivity(FxAccountCreateAccountNotAllowedActivity.class); michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void onResume() { michael@0: super.onResume(); michael@0: redirectIfAppropriate(); michael@0: } michael@0: michael@0: protected void launchActivity(Class activityClass) { michael@0: Intent intent = new Intent(this, activityClass); michael@0: // Per http://stackoverflow.com/a/8992365, this triggers a known bug with michael@0: // the soft keyboard not being shown for the started activity. Why, Android, why? michael@0: intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); michael@0: startActivity(intent); michael@0: } michael@0: michael@0: protected void redirectToActivity(Class activityClass) { michael@0: launchActivity(activityClass); michael@0: finish(); michael@0: } michael@0: michael@0: /** michael@0: * Helper to find view or error if it is missing. michael@0: * michael@0: * @param id of view to find. michael@0: * @param description to print in error. michael@0: * @return non-null View instance. michael@0: */ michael@0: public View ensureFindViewById(View v, int id, String description) { michael@0: View view; michael@0: if (v != null) { michael@0: view = v.findViewById(id); michael@0: } else { michael@0: view = findViewById(id); michael@0: } michael@0: if (view == null) { michael@0: String message = "Could not find view " + description + "."; michael@0: Logger.error(LOG_TAG, message); michael@0: throw new RuntimeException(message); michael@0: } michael@0: return view; michael@0: } michael@0: michael@0: public void linkifyTextViews(View view, int[] textViews) { michael@0: for (int id : textViews) { michael@0: TextView textView; michael@0: if (view != null) { michael@0: textView = (TextView) view.findViewById(id); michael@0: } else { michael@0: textView = (TextView) findViewById(id); michael@0: } michael@0: michael@0: if (textView == null) { michael@0: Logger.warn(LOG_TAG, "Could not process links for view with id " + id + "."); michael@0: continue; michael@0: } michael@0: michael@0: ActivityUtils.linkifyTextView(textView, false); michael@0: } michael@0: } michael@0: michael@0: protected void launchActivityOnClick(final View view, final Class activityClass) { michael@0: view.setOnClickListener(new OnClickListener() { michael@0: @Override michael@0: public void onClick(View v) { michael@0: FxAccountAbstractActivity.this.launchActivity(activityClass); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Helper to fetch (unique) Android Firefox Account if one exists, or return null. michael@0: */ michael@0: protected AndroidFxAccount getAndroidFxAccount() { michael@0: Account account = FirefoxAccounts.getFirefoxAccount(this); michael@0: if (account == null) { michael@0: return null; michael@0: } michael@0: return new AndroidFxAccount(this, account); michael@0: } michael@0: }