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.components.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.components.signin.AccountManagerHelper; | 17 import org.chromium.components.signin.AccountManagerHelper; |
18 import org.chromium.components.signin.ChromeSigninController; | 18 import org.chromium.components.signin.ChromeSigninController; |
19 | 19 |
| 20 import javax.annotation.Nullable; |
20 import javax.annotation.concurrent.ThreadSafe; | 21 import javax.annotation.concurrent.ThreadSafe; |
21 | 22 |
22 /** | 23 /** |
23 * A helper class to handle the current status of sync for Chrome in Android set
tings. | 24 * A helper class to handle the current status of sync for Chrome in Android set
tings. |
24 * | 25 * |
25 * It also provides an observer to be used whenever Android sync settings change
. | 26 * It also provides an observer to be used whenever Android sync settings change
. |
26 * | 27 * |
27 * This class is a collection of static methods so that no references to its obj
ect can be | 28 * This class is a collection of static methods so that no references to its obj
ect can be |
28 * stored. This is important because tests need to be able to overwrite the obje
ct with a | 29 * stored. This is important because tests need to be able to overwrite the obje
ct with a |
29 * mock content resolver and know that no references to the old one are cached. | 30 * mock content resolver and know that no references to the old one are cached. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 * @param context the context the ApplicationContext will be retrieved from. | 88 * @param context the context the ApplicationContext will be retrieved from. |
88 * @param syncContentResolverDelegate an implementation of {@link SyncConten
tResolverDelegate}. | 89 * @param syncContentResolverDelegate an implementation of {@link SyncConten
tResolverDelegate}. |
89 */ | 90 */ |
90 private AndroidSyncSettings( | 91 private AndroidSyncSettings( |
91 Context context, SyncContentResolverDelegate syncContentResolverDele
gate) { | 92 Context context, SyncContentResolverDelegate syncContentResolverDele
gate) { |
92 mApplicationContext = context.getApplicationContext(); | 93 mApplicationContext = context.getApplicationContext(); |
93 mSyncContentResolverDelegate = syncContentResolverDelegate; | 94 mSyncContentResolverDelegate = syncContentResolverDelegate; |
94 mContractAuthority = getContractAuthority(); | 95 mContractAuthority = getContractAuthority(); |
95 | 96 |
96 mAccount = ChromeSigninController.get().getSignedInUser(); | 97 mAccount = ChromeSigninController.get().getSignedInUser(); |
97 updateSyncability(); | 98 updateSyncability(null); |
98 updateCachedSettings(); | 99 updateCachedSettings(); |
99 | 100 |
100 mSyncContentResolverDelegate.addStatusChangeListener( | 101 mSyncContentResolverDelegate.addStatusChangeListener( |
101 ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, | 102 ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, |
102 new AndroidSyncSettingsChangedObserver()); | 103 new AndroidSyncSettingsChangedObserver()); |
103 } | 104 } |
104 | 105 |
105 /** | 106 /** |
106 * Checks whether sync is currently enabled from Chrome for the currently si
gned in account. | 107 * Checks whether sync is currently enabled from Chrome for the currently si
gned in account. |
107 * | 108 * |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 */ | 151 */ |
151 public static void disableChromeSync(Context context) { | 152 public static void disableChromeSync(Context context) { |
152 ensureInitialized(context); | 153 ensureInitialized(context); |
153 sInstance.setChromeSyncEnabled(false); | 154 sInstance.setChromeSyncEnabled(false); |
154 } | 155 } |
155 | 156 |
156 /** | 157 /** |
157 * Must be called when a new account is signed in. | 158 * Must be called when a new account is signed in. |
158 */ | 159 */ |
159 public static void updateAccount(Context context, Account account) { | 160 public static void updateAccount(Context context, Account account) { |
| 161 updateAccount(context, account, null); |
| 162 } |
| 163 |
| 164 /** |
| 165 * Must be called when a new account is signed in. |
| 166 * @param callback Callback that will be called after updating account is fi
nished. Boolean |
| 167 * passed to the callback indicates whether syncability was
changed. |
| 168 */ |
| 169 @VisibleForTesting |
| 170 public static void updateAccount( |
| 171 Context context, Account account, @Nullable Callback<Boolean> callba
ck) { |
160 ensureInitialized(context); | 172 ensureInitialized(context); |
161 synchronized (sInstance.mLock) { | 173 synchronized (sInstance.mLock) { |
162 sInstance.mAccount = account; | 174 sInstance.mAccount = account; |
163 sInstance.updateSyncability(); | 175 sInstance.updateSyncability(callback); |
164 } | 176 } |
165 if (sInstance.updateCachedSettings()) { | 177 if (sInstance.updateCachedSettings()) { |
166 sInstance.notifyObservers(); | 178 sInstance.notifyObservers(); |
167 } | 179 } |
168 } | 180 } |
169 | 181 |
170 /** | 182 /** |
171 * Returns the contract authority to use when requesting sync. | 183 * Returns the contract authority to use when requesting sync. |
172 */ | 184 */ |
173 public static String getContractAuthority(Context context) { | 185 public static String getContractAuthority(Context context) { |
(...skipping 16 matching lines...) Expand all Loading... |
190 */ | 202 */ |
191 public static void unregisterObserver(Context context, AndroidSyncSettingsOb
server observer) { | 203 public static void unregisterObserver(Context context, AndroidSyncSettingsOb
server observer) { |
192 ensureInitialized(context); | 204 ensureInitialized(context); |
193 synchronized (sInstance.mLock) { | 205 synchronized (sInstance.mLock) { |
194 sInstance.mObservers.removeObserver(observer); | 206 sInstance.mObservers.removeObserver(observer); |
195 } | 207 } |
196 } | 208 } |
197 | 209 |
198 private void setChromeSyncEnabled(boolean value) { | 210 private void setChromeSyncEnabled(boolean value) { |
199 synchronized (mLock) { | 211 synchronized (mLock) { |
200 updateSyncability(); | 212 updateSyncability(null); |
201 if (value == mChromeSyncEnabled || mAccount == null) return; | 213 if (value == mChromeSyncEnabled || mAccount == null) return; |
202 mChromeSyncEnabled = value; | 214 mChromeSyncEnabled = value; |
203 | 215 |
204 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites
(); | 216 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites
(); |
205 mSyncContentResolverDelegate.setSyncAutomatically(mAccount, mContrac
tAuthority, value); | 217 mSyncContentResolverDelegate.setSyncAutomatically(mAccount, mContrac
tAuthority, value); |
206 StrictMode.setThreadPolicy(oldPolicy); | 218 StrictMode.setThreadPolicy(oldPolicy); |
207 } | 219 } |
208 notifyObservers(); | 220 notifyObservers(); |
209 } | 221 } |
210 | 222 |
211 /** | 223 /** |
212 * Ensure Chrome is registered with the Android Sync Manager iff signed in. | 224 * Ensure Chrome is registered with the Android Sync Manager iff signed in. |
213 * | 225 * |
214 * This is what causes the "Chrome" option to appear in Settings -> Accounts
-> Sync . | 226 * This is what causes the "Chrome" option to appear in Settings -> Accounts
-> Sync . |
215 * This function must be called within a synchronized block. | 227 * This function must be called within a synchronized block. |
216 */ | 228 */ |
217 private void updateSyncability() { | 229 private void updateSyncability(@Nullable final Callback<Boolean> callback) { |
218 boolean shouldBeSyncable = mAccount != null; | 230 boolean shouldBeSyncable = mAccount != null; |
219 if (mIsSyncable == shouldBeSyncable) return; | 231 if (mIsSyncable == shouldBeSyncable) { |
| 232 if (callback != null) callback.onResult(false); |
| 233 return; |
| 234 } |
220 | 235 |
221 mIsSyncable = shouldBeSyncable; | 236 mIsSyncable = shouldBeSyncable; |
222 | 237 |
223 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); | 238 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
224 // Make account syncable if there is one. | 239 // Make account syncable if there is one. |
225 if (shouldBeSyncable) { | 240 if (shouldBeSyncable) { |
226 mSyncContentResolverDelegate.setIsSyncable(mAccount, mContractAuthor
ity, 1); | 241 mSyncContentResolverDelegate.setIsSyncable(mAccount, mContractAuthor
ity, 1); |
227 // This reduces unnecessary resource usage. See http://crbug.com/480
688 for details. | 242 // This reduces unnecessary resource usage. See http://crbug.com/480
688 for details. |
228 mSyncContentResolverDelegate.removePeriodicSync( | 243 mSyncContentResolverDelegate.removePeriodicSync( |
229 mAccount, mContractAuthority, Bundle.EMPTY); | 244 mAccount, mContractAuthority, Bundle.EMPTY); |
230 } | 245 } |
231 StrictMode.setThreadPolicy(oldPolicy); | 246 StrictMode.setThreadPolicy(oldPolicy); |
232 | 247 |
233 // Disable the syncability of Chrome for all other accounts. | 248 // Disable the syncability of Chrome for all other accounts. |
234 AccountManagerHelper.get().getGoogleAccounts(new Callback<Account[]>() { | 249 AccountManagerHelper.get().getGoogleAccounts(new Callback<Account[]>() { |
235 @Override | 250 @Override |
236 public void onResult(Account[] accounts) { | 251 public void onResult(Account[] accounts) { |
237 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWr
ites(); | 252 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWr
ites(); |
238 for (Account account : accounts) { | 253 for (Account account : accounts) { |
239 if (!account.equals(mAccount) | 254 if (!account.equals(mAccount) |
240 && mSyncContentResolverDelegate.getIsSyncable( | 255 && mSyncContentResolverDelegate.getIsSyncable( |
241 account, mContractAuthority) | 256 account, mContractAuthority) |
242 > 0) { | 257 > 0) { |
243 mSyncContentResolverDelegate.setIsSyncable(account, mCon
tractAuthority, 0); | 258 mSyncContentResolverDelegate.setIsSyncable(account, mCon
tractAuthority, 0); |
244 } | 259 } |
245 } | 260 } |
246 StrictMode.setThreadPolicy(oldPolicy); | 261 StrictMode.setThreadPolicy(oldPolicy); |
| 262 |
| 263 if (callback != null) callback.onResult(true); |
247 } | 264 } |
248 }); | 265 }); |
249 } | 266 } |
250 | 267 |
251 /** | 268 /** |
252 * Helper class to be used by observers whenever sync settings change. | 269 * Helper class to be used by observers whenever sync settings change. |
253 * | 270 * |
254 * To register the observer, call AndroidSyncSettings.registerObserver(...). | 271 * To register the observer, call AndroidSyncSettings.registerObserver(...). |
255 */ | 272 */ |
256 private class AndroidSyncSettingsChangedObserver implements SyncStatusObserv
er { | 273 private class AndroidSyncSettingsChangedObserver implements SyncStatusObserv
er { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 private void notifyObservers() { | 315 private void notifyObservers() { |
299 for (AndroidSyncSettingsObserver observer : mObservers) { | 316 for (AndroidSyncSettingsObserver observer : mObservers) { |
300 observer.androidSyncSettingsChanged(); | 317 observer.androidSyncSettingsChanged(); |
301 } | 318 } |
302 } | 319 } |
303 | 320 |
304 private String getContractAuthority() { | 321 private String getContractAuthority() { |
305 return mApplicationContext.getPackageName(); | 322 return mApplicationContext.getPackageName(); |
306 } | 323 } |
307 } | 324 } |
OLD | NEW |