diff -r 000000000000 -r 6474c204b198 mobile/android/base/fxa/receivers/FxAccountUpgradeReceiver.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mobile/android/base/fxa/receivers/FxAccountUpgradeReceiver.java Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,132 @@ +/* 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.receivers; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +import org.mozilla.gecko.background.common.log.Logger; +import org.mozilla.gecko.fxa.FirefoxAccounts; +import org.mozilla.gecko.fxa.FxAccountConstants; +import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount; +import org.mozilla.gecko.fxa.login.State; +import org.mozilla.gecko.fxa.login.State.StateLabel; +import org.mozilla.gecko.sync.Utils; + +import android.accounts.Account; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +/** + * A receiver that takes action when our Android package is upgraded (replaced). + */ +public class FxAccountUpgradeReceiver extends BroadcastReceiver { + private static final String LOG_TAG = FxAccountUpgradeReceiver.class.getSimpleName(); + + /** + * Produce a list of Runnable instances to be executed sequentially on + * upgrade. + *
+ * Each Runnable will be executed sequentially on a background thread. Any
+ * unchecked Exception thrown will be caught and ignored.
+ *
+ * @param context Android context.
+ * @return list of Runnable instances.
+ */
+ protected List
+ * This is our main deprecation-and-upgrade mechanism: in some way, the
+ * Account gets moved to the Doghouse state. If possible, an upgraded version
+ * of the package advances to Separated, prompting the user to re-connect the
+ * Account.
+ */
+ protected static class AdvanceFromDoghouseRunnable implements Runnable {
+ protected final Context context;
+
+ public AdvanceFromDoghouseRunnable(Context context) {
+ this.context = context;
+ }
+
+ @Override
+ public void run() {
+ final Account[] accounts = FirefoxAccounts.getFirefoxAccounts(context);
+ Logger.info(LOG_TAG, "Trying to advance " + accounts.length + " existing Firefox Accounts from the Doghouse to Separated (if necessary).");
+ for (Account account : accounts) {
+ try {
+ final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);
+ // For great debugging.
+ if (FxAccountConstants.LOG_PERSONAL_INFORMATION) {
+ fxAccount.dump();
+ }
+ State state = fxAccount.getState();
+ if (state == null || state.getStateLabel() != StateLabel.Doghouse) {
+ Logger.debug(LOG_TAG, "Account named like " + Utils.obfuscateEmail(account.name) + " is not in the Doghouse; skipping.");
+ continue;
+ }
+ Logger.debug(LOG_TAG, "Account named like " + Utils.obfuscateEmail(account.name) + " is in the Doghouse; advancing to Separated.");
+ fxAccount.setState(state.makeSeparatedState());
+ } catch (Exception e) {
+ Logger.warn(LOG_TAG, "Got exception trying to advance account named like " + Utils.obfuscateEmail(account.name) +
+ " from Doghouse to Separated state; ignoring.", e);
+ }
+ }
+ }
+ }
+}