|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.receivers; |
|
6 |
|
7 import org.mozilla.gecko.sync.CredentialException; |
|
8 import org.mozilla.gecko.background.common.GlobalConstants; |
|
9 import org.mozilla.gecko.background.common.log.Logger; |
|
10 import org.mozilla.gecko.sync.SyncConfiguration; |
|
11 import org.mozilla.gecko.sync.ThreadPool; |
|
12 import org.mozilla.gecko.sync.Utils; |
|
13 import org.mozilla.gecko.sync.config.ConfigurationMigrator; |
|
14 import org.mozilla.gecko.sync.setup.Constants; |
|
15 import org.mozilla.gecko.sync.setup.SyncAccounts; |
|
16 import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters; |
|
17 |
|
18 import android.accounts.Account; |
|
19 import android.accounts.AccountManager; |
|
20 import android.content.BroadcastReceiver; |
|
21 import android.content.Context; |
|
22 import android.content.Intent; |
|
23 |
|
24 public class UpgradeReceiver extends BroadcastReceiver { |
|
25 private static final String LOG_TAG = "UpgradeReceiver"; |
|
26 |
|
27 @Override |
|
28 public void onReceive(final Context context, Intent intent) { |
|
29 Logger.debug(LOG_TAG, "Broadcast received."); |
|
30 // Should filter for specific MY_PACKAGE_REPLACED intent, but Android does |
|
31 // not expose it. |
|
32 ThreadPool.run(new Runnable() { |
|
33 @Override |
|
34 public void run() { |
|
35 final AccountManager accountManager = AccountManager.get(context); |
|
36 final Account[] accounts = SyncAccounts.syncAccounts(context); |
|
37 |
|
38 for (Account a : accounts) { |
|
39 if ("1".equals(accountManager.getUserData(a, Constants.DATA_ENABLE_ON_UPGRADE))) { |
|
40 SyncAccounts.setSyncAutomatically(a, true); |
|
41 accountManager.setUserData(a, Constants.DATA_ENABLE_ON_UPGRADE, "0"); |
|
42 } |
|
43 } |
|
44 } |
|
45 }); |
|
46 |
|
47 /** |
|
48 * Bug 761682: migrate preferences forward. |
|
49 */ |
|
50 ThreadPool.run(new Runnable() { |
|
51 @Override |
|
52 public void run() { |
|
53 AccountManager accountManager = AccountManager.get(context); |
|
54 final Account[] accounts = SyncAccounts.syncAccounts(context); |
|
55 |
|
56 for (Account account : accounts) { |
|
57 Logger.info(LOG_TAG, "Migrating preferences on upgrade for Android account named " + Utils.obfuscateEmail(account.name) + "."); |
|
58 |
|
59 SyncAccountParameters params; |
|
60 try { |
|
61 params = SyncAccounts.blockingFromAndroidAccountV0(context, accountManager, account); |
|
62 } catch (CredentialException e) { |
|
63 Logger.warn(LOG_TAG, "Caught exception fetching account parameters while trying to migrate preferences; ignoring.", e); |
|
64 continue; |
|
65 } |
|
66 |
|
67 final String product = GlobalConstants.BROWSER_INTENT_PACKAGE; |
|
68 final String username = params.username; |
|
69 final String serverURL = params.serverURL; |
|
70 final String profile = "default"; |
|
71 try { |
|
72 ConfigurationMigrator.ensurePrefsAreVersion(SyncConfiguration.CURRENT_PREFS_VERSION, context, accountManager, account, |
|
73 product, username, serverURL, profile); |
|
74 } catch (Exception e) { |
|
75 Logger.warn(LOG_TAG, "Caught exception trying to migrate preferences; ignoring.", e); |
|
76 continue; |
|
77 } |
|
78 } |
|
79 } |
|
80 }); |
|
81 } |
|
82 } |