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.sync.receivers; michael@0: michael@0: import org.mozilla.gecko.sync.CredentialException; michael@0: import org.mozilla.gecko.background.common.GlobalConstants; michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.SyncConfiguration; michael@0: import org.mozilla.gecko.sync.ThreadPool; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: import org.mozilla.gecko.sync.config.ConfigurationMigrator; michael@0: import org.mozilla.gecko.sync.setup.Constants; michael@0: import org.mozilla.gecko.sync.setup.SyncAccounts; michael@0: import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters; michael@0: michael@0: import android.accounts.Account; michael@0: import android.accounts.AccountManager; michael@0: import android.content.BroadcastReceiver; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: michael@0: public class UpgradeReceiver extends BroadcastReceiver { michael@0: private static final String LOG_TAG = "UpgradeReceiver"; michael@0: michael@0: @Override michael@0: public void onReceive(final Context context, Intent intent) { michael@0: Logger.debug(LOG_TAG, "Broadcast received."); michael@0: // Should filter for specific MY_PACKAGE_REPLACED intent, but Android does michael@0: // not expose it. michael@0: ThreadPool.run(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: final AccountManager accountManager = AccountManager.get(context); michael@0: final Account[] accounts = SyncAccounts.syncAccounts(context); michael@0: michael@0: for (Account a : accounts) { michael@0: if ("1".equals(accountManager.getUserData(a, Constants.DATA_ENABLE_ON_UPGRADE))) { michael@0: SyncAccounts.setSyncAutomatically(a, true); michael@0: accountManager.setUserData(a, Constants.DATA_ENABLE_ON_UPGRADE, "0"); michael@0: } michael@0: } michael@0: } michael@0: }); michael@0: michael@0: /** michael@0: * Bug 761682: migrate preferences forward. michael@0: */ michael@0: ThreadPool.run(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: AccountManager accountManager = AccountManager.get(context); michael@0: final Account[] accounts = SyncAccounts.syncAccounts(context); michael@0: michael@0: for (Account account : accounts) { michael@0: Logger.info(LOG_TAG, "Migrating preferences on upgrade for Android account named " + Utils.obfuscateEmail(account.name) + "."); michael@0: michael@0: SyncAccountParameters params; michael@0: try { michael@0: params = SyncAccounts.blockingFromAndroidAccountV0(context, accountManager, account); michael@0: } catch (CredentialException e) { michael@0: Logger.warn(LOG_TAG, "Caught exception fetching account parameters while trying to migrate preferences; ignoring.", e); michael@0: continue; michael@0: } michael@0: michael@0: final String product = GlobalConstants.BROWSER_INTENT_PACKAGE; michael@0: final String username = params.username; michael@0: final String serverURL = params.serverURL; michael@0: final String profile = "default"; michael@0: try { michael@0: ConfigurationMigrator.ensurePrefsAreVersion(SyncConfiguration.CURRENT_PREFS_VERSION, context, accountManager, account, michael@0: product, username, serverURL, profile); michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Caught exception trying to migrate preferences; ignoring.", e); michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: }