mobile/android/base/sync/receivers/UpgradeReceiver.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/receivers/UpgradeReceiver.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     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.sync.receivers;
     1.9 +
    1.10 +import org.mozilla.gecko.sync.CredentialException;
    1.11 +import org.mozilla.gecko.background.common.GlobalConstants;
    1.12 +import org.mozilla.gecko.background.common.log.Logger;
    1.13 +import org.mozilla.gecko.sync.SyncConfiguration;
    1.14 +import org.mozilla.gecko.sync.ThreadPool;
    1.15 +import org.mozilla.gecko.sync.Utils;
    1.16 +import org.mozilla.gecko.sync.config.ConfigurationMigrator;
    1.17 +import org.mozilla.gecko.sync.setup.Constants;
    1.18 +import org.mozilla.gecko.sync.setup.SyncAccounts;
    1.19 +import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters;
    1.20 +
    1.21 +import android.accounts.Account;
    1.22 +import android.accounts.AccountManager;
    1.23 +import android.content.BroadcastReceiver;
    1.24 +import android.content.Context;
    1.25 +import android.content.Intent;
    1.26 +
    1.27 +public class UpgradeReceiver extends BroadcastReceiver {
    1.28 +  private static final String LOG_TAG = "UpgradeReceiver";
    1.29 +
    1.30 +  @Override
    1.31 +  public void onReceive(final Context context, Intent intent) {
    1.32 +    Logger.debug(LOG_TAG, "Broadcast received.");
    1.33 +    // Should filter for specific MY_PACKAGE_REPLACED intent, but Android does
    1.34 +    // not expose it.
    1.35 +    ThreadPool.run(new Runnable() {
    1.36 +      @Override
    1.37 +      public void run() {
    1.38 +        final AccountManager accountManager = AccountManager.get(context);
    1.39 +        final Account[] accounts = SyncAccounts.syncAccounts(context);
    1.40 +
    1.41 +        for (Account a : accounts) {
    1.42 +          if ("1".equals(accountManager.getUserData(a, Constants.DATA_ENABLE_ON_UPGRADE))) {
    1.43 +            SyncAccounts.setSyncAutomatically(a, true);
    1.44 +            accountManager.setUserData(a, Constants.DATA_ENABLE_ON_UPGRADE, "0");
    1.45 +          }
    1.46 +        }
    1.47 +      }
    1.48 +    });
    1.49 +
    1.50 +    /**
    1.51 +     * Bug 761682: migrate preferences forward.
    1.52 +     */
    1.53 +    ThreadPool.run(new Runnable() {
    1.54 +      @Override
    1.55 +      public void run() {
    1.56 +        AccountManager accountManager = AccountManager.get(context);
    1.57 +        final Account[] accounts = SyncAccounts.syncAccounts(context);
    1.58 +
    1.59 +        for (Account account : accounts) {
    1.60 +          Logger.info(LOG_TAG, "Migrating preferences on upgrade for Android account named " + Utils.obfuscateEmail(account.name) + ".");
    1.61 +
    1.62 +          SyncAccountParameters params;
    1.63 +          try {
    1.64 +            params = SyncAccounts.blockingFromAndroidAccountV0(context, accountManager, account);
    1.65 +          } catch (CredentialException e) {
    1.66 +            Logger.warn(LOG_TAG, "Caught exception fetching account parameters while trying to migrate preferences; ignoring.", e);
    1.67 +            continue;
    1.68 +          }
    1.69 +
    1.70 +          final String product = GlobalConstants.BROWSER_INTENT_PACKAGE;
    1.71 +          final String username = params.username;
    1.72 +          final String serverURL = params.serverURL;
    1.73 +          final String profile = "default";
    1.74 +          try {
    1.75 +            ConfigurationMigrator.ensurePrefsAreVersion(SyncConfiguration.CURRENT_PREFS_VERSION, context, accountManager, account,
    1.76 +                product, username, serverURL, profile);
    1.77 +          } catch (Exception e) {
    1.78 +            Logger.warn(LOG_TAG, "Caught exception trying to migrate preferences; ignoring.", e);
    1.79 +            continue;
    1.80 +          }
    1.81 +        }
    1.82 +      }
    1.83 +    });
    1.84 +  }
    1.85 +}

mercurial