mobile/android/base/fxa/receivers/FxAccountUpgradeReceiver.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/fxa/receivers/FxAccountUpgradeReceiver.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     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.receivers;
     1.9 +
    1.10 +import java.util.LinkedList;
    1.11 +import java.util.List;
    1.12 +import java.util.concurrent.Executor;
    1.13 +import java.util.concurrent.Executors;
    1.14 +
    1.15 +import org.mozilla.gecko.background.common.log.Logger;
    1.16 +import org.mozilla.gecko.fxa.FirefoxAccounts;
    1.17 +import org.mozilla.gecko.fxa.FxAccountConstants;
    1.18 +import org.mozilla.gecko.fxa.authenticator.AndroidFxAccount;
    1.19 +import org.mozilla.gecko.fxa.login.State;
    1.20 +import org.mozilla.gecko.fxa.login.State.StateLabel;
    1.21 +import org.mozilla.gecko.sync.Utils;
    1.22 +
    1.23 +import android.accounts.Account;
    1.24 +import android.content.BroadcastReceiver;
    1.25 +import android.content.Context;
    1.26 +import android.content.Intent;
    1.27 +
    1.28 +/**
    1.29 + * A receiver that takes action when our Android package is upgraded (replaced).
    1.30 + */
    1.31 +public class FxAccountUpgradeReceiver extends BroadcastReceiver {
    1.32 +  private static final String LOG_TAG = FxAccountUpgradeReceiver.class.getSimpleName();
    1.33 +
    1.34 +  /**
    1.35 +   * Produce a list of Runnable instances to be executed sequentially on
    1.36 +   * upgrade.
    1.37 +   * <p>
    1.38 +   * Each Runnable will be executed sequentially on a background thread. Any
    1.39 +   * unchecked Exception thrown will be caught and ignored.
    1.40 +   *
    1.41 +   * @param context Android context.
    1.42 +   * @return list of Runnable instances.
    1.43 +   */
    1.44 +  protected List<Runnable> onUpgradeRunnables(Context context) {
    1.45 +    List<Runnable> runnables = new LinkedList<Runnable>();
    1.46 +    runnables.add(new MaybeUnpickleRunnable(context));
    1.47 +    // Recovering accounts that are in the Doghouse should happen *after* we
    1.48 +    // unpickle any accounts saved to disk.
    1.49 +    runnables.add(new AdvanceFromDoghouseRunnable(context));
    1.50 +    return runnables;
    1.51 +  }
    1.52 +
    1.53 +  @Override
    1.54 +  public void onReceive(final Context context, Intent intent) {
    1.55 +    Logger.setThreadLogTag(FxAccountConstants.GLOBAL_LOG_TAG);
    1.56 +    Logger.info(LOG_TAG, "Upgrade broadcast received.");
    1.57 +
    1.58 +    // Iterate Runnable instances one at a time.
    1.59 +    final Executor executor = Executors.newSingleThreadExecutor();
    1.60 +    for (final Runnable runnable : onUpgradeRunnables(context)) {
    1.61 +      executor.execute(new Runnable() {
    1.62 +        @Override
    1.63 +        public void run() {
    1.64 +          try {
    1.65 +            runnable.run();
    1.66 +          } catch (Exception e) {
    1.67 +            // We really don't want to throw on a background thread, so we
    1.68 +            // catch, log, and move on.
    1.69 +            Logger.error(LOG_TAG, "Got exception executing background upgrade Runnable; ignoring.", e);
    1.70 +          }
    1.71 +        }
    1.72 +      });
    1.73 +    }
    1.74 +  }
    1.75 +
    1.76 +  /**
    1.77 +   * A Runnable that tries to unpickle any pickled Firefox Accounts.
    1.78 +   */
    1.79 +  protected static class MaybeUnpickleRunnable implements Runnable {
    1.80 +    protected final Context context;
    1.81 +
    1.82 +    public MaybeUnpickleRunnable(Context context) {
    1.83 +      this.context = context;
    1.84 +    }
    1.85 +
    1.86 +    @Override
    1.87 +    public void run() {
    1.88 +      // Querying the accounts will unpickle any pickled Firefox Account.
    1.89 +      Logger.info(LOG_TAG, "Trying to unpickle any pickled Firefox Account.");
    1.90 +      FirefoxAccounts.getFirefoxAccounts(context);
    1.91 +    }
    1.92 +  }
    1.93 +
    1.94 +  /**
    1.95 +   * A Runnable that tries to advance existing Firefox Accounts that are in the
    1.96 +   * Doghouse state to the Separated state.
    1.97 +   * <p>
    1.98 +   * This is our main deprecation-and-upgrade mechanism: in some way, the
    1.99 +   * Account gets moved to the Doghouse state. If possible, an upgraded version
   1.100 +   * of the package advances to Separated, prompting the user to re-connect the
   1.101 +   * Account.
   1.102 +   */
   1.103 +  protected static class AdvanceFromDoghouseRunnable implements Runnable {
   1.104 +    protected final Context context;
   1.105 +
   1.106 +    public AdvanceFromDoghouseRunnable(Context context) {
   1.107 +      this.context = context;
   1.108 +    }
   1.109 +
   1.110 +    @Override
   1.111 +    public void run() {
   1.112 +      final Account[] accounts = FirefoxAccounts.getFirefoxAccounts(context);
   1.113 +      Logger.info(LOG_TAG, "Trying to advance " + accounts.length + " existing Firefox Accounts from the Doghouse to Separated (if necessary).");
   1.114 +      for (Account account : accounts) {
   1.115 +        try {
   1.116 +          final AndroidFxAccount fxAccount = new AndroidFxAccount(context, account);
   1.117 +          // For great debugging.
   1.118 +          if (FxAccountConstants.LOG_PERSONAL_INFORMATION) {
   1.119 +            fxAccount.dump();
   1.120 +          }
   1.121 +          State state = fxAccount.getState();
   1.122 +          if (state == null || state.getStateLabel() != StateLabel.Doghouse) {
   1.123 +            Logger.debug(LOG_TAG, "Account named like " + Utils.obfuscateEmail(account.name) + " is not in the Doghouse; skipping.");
   1.124 +            continue;
   1.125 +          }
   1.126 +          Logger.debug(LOG_TAG, "Account named like " + Utils.obfuscateEmail(account.name) + " is in the Doghouse; advancing to Separated.");
   1.127 +          fxAccount.setState(state.makeSeparatedState());
   1.128 +        } catch (Exception e) {
   1.129 +          Logger.warn(LOG_TAG, "Got exception trying to advance account named like " + Utils.obfuscateEmail(account.name) +
   1.130 +              " from Doghouse to Separated state; ignoring.", e);
   1.131 +        }
   1.132 +      }
   1.133 +    }
   1.134 +  }
   1.135 +}

mercurial