OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.sync; | 5 package org.chromium.components.sync; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.content.ContentResolver; | 8 import android.content.ContentResolver; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.SyncStatusObserver; | 10 import android.content.SyncStatusObserver; |
11 import android.os.Bundle; | 11 import android.os.Bundle; |
12 import android.os.StrictMode; | 12 import android.os.StrictMode; |
13 | 13 |
14 import org.chromium.base.Callback; | 14 import org.chromium.base.Callback; |
15 import org.chromium.base.ObserverList; | 15 import org.chromium.base.ObserverList; |
16 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
17 import org.chromium.sync.signin.AccountManagerHelper; | 17 import org.chromium.components.sync.signin.AccountManagerHelper; |
18 | 18 |
19 import javax.annotation.concurrent.ThreadSafe; | 19 import javax.annotation.concurrent.ThreadSafe; |
20 | 20 |
21 /** | 21 /** |
22 * A helper class to handle the current status of sync for Chrome in Android set
tings. | 22 * A helper class to handle the current status of sync for Chrome in Android set
tings. |
23 * | 23 * |
24 * It also provides an observer to be used whenever Android sync settings change
. | 24 * It also provides an observer to be used whenever Android sync settings change
. |
25 * | 25 * |
26 * This class is a collection of static methods so that no references to its obj
ect can be | 26 * This class is a collection of static methods so that no references to its obj
ect can be |
27 * stored. This is important because tests need to be able to overwrite the obje
ct with a | 27 * stored. This is important because tests need to be able to overwrite the obje
ct with a |
28 * mock content resolver and know that no references to the old one are cached. | 28 * mock content resolver and know that no references to the old one are cached. |
29 * | 29 * |
30 * This class must be initialized via updateAccount() on startup if the user is
signed in. | 30 * This class must be initialized via updateAccount() on startup if the user is
signed in. |
31 */ | 31 */ |
32 @ThreadSafe | 32 @ThreadSafe |
33 public class AndroidSyncSettings { | 33 public class AndroidSyncSettings { |
34 | |
35 public static final String TAG = "AndroidSyncSettings"; | 34 public static final String TAG = "AndroidSyncSettings"; |
36 | 35 |
37 /** | 36 /** |
38 * Lock for ensuring singleton instantiation across threads. | 37 * Lock for ensuring singleton instantiation across threads. |
39 */ | 38 */ |
40 private static final Object CLASS_LOCK = new Object(); | 39 private static final Object CLASS_LOCK = new Object(); |
41 | 40 |
42 private static AndroidSyncSettings sInstance; | 41 private static AndroidSyncSettings sInstance; |
43 | 42 |
44 private final Object mLock = new Object(); | 43 private final Object mLock = new Object(); |
(...skipping 11 matching lines...) Expand all Loading... |
56 private boolean mChromeSyncEnabled = false; | 55 private boolean mChromeSyncEnabled = false; |
57 | 56 |
58 private boolean mMasterSyncEnabled = false; | 57 private boolean mMasterSyncEnabled = false; |
59 | 58 |
60 private final ObserverList<AndroidSyncSettingsObserver> mObservers = | 59 private final ObserverList<AndroidSyncSettingsObserver> mObservers = |
61 new ObserverList<AndroidSyncSettingsObserver>(); | 60 new ObserverList<AndroidSyncSettingsObserver>(); |
62 | 61 |
63 /** | 62 /** |
64 * Provides notifications when Android sync settings have changed. | 63 * Provides notifications when Android sync settings have changed. |
65 */ | 64 */ |
66 public interface AndroidSyncSettingsObserver { | 65 public interface AndroidSyncSettingsObserver { public void androidSyncSettin
gsChanged(); } |
67 public void androidSyncSettingsChanged(); | |
68 } | |
69 | 66 |
70 private static void ensureInitialized(Context context) { | 67 private static void ensureInitialized(Context context) { |
71 synchronized (CLASS_LOCK) { | 68 synchronized (CLASS_LOCK) { |
72 if (sInstance == null) { | 69 if (sInstance == null) { |
73 SyncContentResolverDelegate contentResolver = | 70 SyncContentResolverDelegate contentResolver = |
74 new SystemSyncContentResolverDelegate(); | 71 new SystemSyncContentResolverDelegate(); |
75 sInstance = new AndroidSyncSettings(context, contentResolver); | 72 sInstance = new AndroidSyncSettings(context, contentResolver); |
76 } | 73 } |
77 } | 74 } |
78 } | 75 } |
79 | 76 |
80 @VisibleForTesting | 77 @VisibleForTesting |
81 public static void overrideForTests(Context context, | 78 public static void overrideForTests( |
82 SyncContentResolverDelegate contentResolver) { | 79 Context context, SyncContentResolverDelegate contentResolver) { |
83 synchronized (CLASS_LOCK) { | 80 synchronized (CLASS_LOCK) { |
84 sInstance = new AndroidSyncSettings(context, contentResolver); | 81 sInstance = new AndroidSyncSettings(context, contentResolver); |
85 } | 82 } |
86 } | 83 } |
87 | 84 |
88 /** | 85 /** |
89 * @param context the context the ApplicationContext will be retrieved from. | 86 * @param context the context the ApplicationContext will be retrieved from. |
90 * @param syncContentResolverDelegate an implementation of {@link SyncConten
tResolverDelegate}. | 87 * @param syncContentResolverDelegate an implementation of {@link SyncConten
tResolverDelegate}. |
91 */ | 88 */ |
92 private AndroidSyncSettings(Context context, | 89 private AndroidSyncSettings( |
93 SyncContentResolverDelegate syncContentResolverDelegate) { | 90 Context context, SyncContentResolverDelegate syncContentResolverDele
gate) { |
94 mApplicationContext = context.getApplicationContext(); | 91 mApplicationContext = context.getApplicationContext(); |
95 mSyncContentResolverDelegate = syncContentResolverDelegate; | 92 mSyncContentResolverDelegate = syncContentResolverDelegate; |
96 mContractAuthority = getContractAuthority(); | 93 mContractAuthority = getContractAuthority(); |
97 | 94 |
98 updateCachedSettings(); | 95 updateCachedSettings(); |
99 | 96 |
100 mSyncContentResolverDelegate.addStatusChangeListener( | 97 mSyncContentResolverDelegate.addStatusChangeListener( |
101 ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, | 98 ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, |
102 new AndroidSyncSettingsChangedObserver()); | 99 new AndroidSyncSettingsChangedObserver()); |
103 } | 100 } |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 mAccount, mContractAuthority, Bundle.EMPTY); | 226 mAccount, mContractAuthority, Bundle.EMPTY); |
230 } | 227 } |
231 StrictMode.setThreadPolicy(oldPolicy); | 228 StrictMode.setThreadPolicy(oldPolicy); |
232 | 229 |
233 // Disable the syncability of Chrome for all other accounts. | 230 // Disable the syncability of Chrome for all other accounts. |
234 AccountManagerHelper.get(mApplicationContext).getGoogleAccounts(new Call
back<Account[]>() { | 231 AccountManagerHelper.get(mApplicationContext).getGoogleAccounts(new Call
back<Account[]>() { |
235 @Override | 232 @Override |
236 public void onResult(Account[] accounts) { | 233 public void onResult(Account[] accounts) { |
237 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWr
ites(); | 234 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWr
ites(); |
238 for (Account account : accounts) { | 235 for (Account account : accounts) { |
239 if (!account.equals(mAccount) && mSyncContentResolverDelegat
e.getIsSyncable( | 236 if (!account.equals(mAccount) |
240 account, mContractAuthority) > 0) { | 237 && mSyncContentResolverDelegate.getIsSyncable( |
| 238 account, mContractAuthority) |
| 239 > 0) { |
241 mSyncContentResolverDelegate.setIsSyncable(account, mCon
tractAuthority, 0); | 240 mSyncContentResolverDelegate.setIsSyncable(account, mCon
tractAuthority, 0); |
242 } | 241 } |
243 } | 242 } |
244 StrictMode.setThreadPolicy(oldPolicy); | 243 StrictMode.setThreadPolicy(oldPolicy); |
245 } | 244 } |
246 }); | 245 }); |
247 } | 246 } |
248 | 247 |
249 /** | 248 /** |
250 * Helper class to be used by observers whenever sync settings change. | 249 * Helper class to be used by observers whenever sync settings change. |
(...skipping 18 matching lines...) Expand all Loading... |
269 * | 268 * |
270 * @return Whether either chromeSyncEnabled or masterSyncEnabled changed. | 269 * @return Whether either chromeSyncEnabled or masterSyncEnabled changed. |
271 */ | 270 */ |
272 private boolean updateCachedSettings() { | 271 private boolean updateCachedSettings() { |
273 synchronized (mLock) { | 272 synchronized (mLock) { |
274 boolean oldChromeSyncEnabled = mChromeSyncEnabled; | 273 boolean oldChromeSyncEnabled = mChromeSyncEnabled; |
275 boolean oldMasterSyncEnabled = mMasterSyncEnabled; | 274 boolean oldMasterSyncEnabled = mMasterSyncEnabled; |
276 | 275 |
277 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites
(); | 276 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites
(); |
278 if (mAccount != null) { | 277 if (mAccount != null) { |
279 mIsSyncable = mSyncContentResolverDelegate.getIsSyncable( | 278 mIsSyncable = |
280 mAccount, mContractAuthority) == 1; | 279 mSyncContentResolverDelegate.getIsSyncable(mAccount, mCo
ntractAuthority) |
| 280 == 1; |
281 mChromeSyncEnabled = mSyncContentResolverDelegate.getSyncAutomat
ically( | 281 mChromeSyncEnabled = mSyncContentResolverDelegate.getSyncAutomat
ically( |
282 mAccount, mContractAuthority); | 282 mAccount, mContractAuthority); |
283 } else { | 283 } else { |
284 mIsSyncable = false; | 284 mIsSyncable = false; |
285 mChromeSyncEnabled = false; | 285 mChromeSyncEnabled = false; |
286 } | 286 } |
287 mMasterSyncEnabled = mSyncContentResolverDelegate.getMasterSyncAutom
atically(); | 287 mMasterSyncEnabled = mSyncContentResolverDelegate.getMasterSyncAutom
atically(); |
288 StrictMode.setThreadPolicy(oldPolicy); | 288 StrictMode.setThreadPolicy(oldPolicy); |
289 | 289 |
290 return oldChromeSyncEnabled != mChromeSyncEnabled | 290 return oldChromeSyncEnabled != mChromeSyncEnabled |
291 || oldMasterSyncEnabled != mMasterSyncEnabled; | 291 || oldMasterSyncEnabled != mMasterSyncEnabled; |
292 } | 292 } |
293 } | 293 } |
294 | 294 |
295 private void notifyObservers() { | 295 private void notifyObservers() { |
296 for (AndroidSyncSettingsObserver observer : mObservers) { | 296 for (AndroidSyncSettingsObserver observer : mObservers) { |
297 observer.androidSyncSettingsChanged(); | 297 observer.androidSyncSettingsChanged(); |
298 } | 298 } |
299 } | 299 } |
300 | 300 |
301 private String getContractAuthority() { | 301 private String getContractAuthority() { |
302 return mApplicationContext.getPackageName(); | 302 return mApplicationContext.getPackageName(); |
303 } | 303 } |
304 } | 304 } |
OLD | NEW |