Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: components/sync/android/java/src/org/chromium/components/sync/AndroidSyncSettings.java

Issue 2847663003: Add callback to AndroidSyncSettings.updateAccount and fix related test (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 */
167 @VisibleForTesting
168 public static void updateAccount(
169 Context context, Account account, @Nullable Callback<Void> callback) {
160 ensureInitialized(context); 170 ensureInitialized(context);
161 synchronized (sInstance.mLock) { 171 synchronized (sInstance.mLock) {
162 sInstance.mAccount = account; 172 sInstance.mAccount = account;
163 sInstance.updateSyncability(); 173 sInstance.updateSyncability(callback);
164 } 174 }
165 if (sInstance.updateCachedSettings()) { 175 if (sInstance.updateCachedSettings()) {
166 sInstance.notifyObservers(); 176 sInstance.notifyObservers();
167 } 177 }
168 } 178 }
169 179
170 /** 180 /**
171 * Returns the contract authority to use when requesting sync. 181 * Returns the contract authority to use when requesting sync.
172 */ 182 */
173 public static String getContractAuthority(Context context) { 183 public static String getContractAuthority(Context context) {
(...skipping 16 matching lines...) Expand all
190 */ 200 */
191 public static void unregisterObserver(Context context, AndroidSyncSettingsOb server observer) { 201 public static void unregisterObserver(Context context, AndroidSyncSettingsOb server observer) {
192 ensureInitialized(context); 202 ensureInitialized(context);
193 synchronized (sInstance.mLock) { 203 synchronized (sInstance.mLock) {
194 sInstance.mObservers.removeObserver(observer); 204 sInstance.mObservers.removeObserver(observer);
195 } 205 }
196 } 206 }
197 207
198 private void setChromeSyncEnabled(boolean value) { 208 private void setChromeSyncEnabled(boolean value) {
199 synchronized (mLock) { 209 synchronized (mLock) {
200 updateSyncability(); 210 updateSyncability(null);
201 if (value == mChromeSyncEnabled || mAccount == null) return; 211 if (value == mChromeSyncEnabled || mAccount == null) return;
202 mChromeSyncEnabled = value; 212 mChromeSyncEnabled = value;
203 213
204 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites (); 214 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites ();
205 mSyncContentResolverDelegate.setSyncAutomatically(mAccount, mContrac tAuthority, value); 215 mSyncContentResolverDelegate.setSyncAutomatically(mAccount, mContrac tAuthority, value);
206 StrictMode.setThreadPolicy(oldPolicy); 216 StrictMode.setThreadPolicy(oldPolicy);
207 } 217 }
208 notifyObservers(); 218 notifyObservers();
209 } 219 }
210 220
211 /** 221 /**
212 * Ensure Chrome is registered with the Android Sync Manager iff signed in. 222 * Ensure Chrome is registered with the Android Sync Manager iff signed in.
213 * 223 *
214 * This is what causes the "Chrome" option to appear in Settings -> Accounts -> Sync . 224 * This is what causes the "Chrome" option to appear in Settings -> Accounts -> Sync .
215 * This function must be called within a synchronized block. 225 * This function must be called within a synchronized block.
216 */ 226 */
217 private void updateSyncability() { 227 private void updateSyncability(@Nullable final Callback<Void> callback) {
218 boolean shouldBeSyncable = mAccount != null; 228 boolean shouldBeSyncable = mAccount != null;
219 if (mIsSyncable == shouldBeSyncable) return; 229 if (mIsSyncable == shouldBeSyncable) {
230 if (callback != null) callback.onResult(null);
231 return;
232 }
220 233
221 mIsSyncable = shouldBeSyncable; 234 mIsSyncable = shouldBeSyncable;
222 235
223 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); 236 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
224 // Make account syncable if there is one. 237 // Make account syncable if there is one.
225 if (shouldBeSyncable) { 238 if (shouldBeSyncable) {
226 mSyncContentResolverDelegate.setIsSyncable(mAccount, mContractAuthor ity, 1); 239 mSyncContentResolverDelegate.setIsSyncable(mAccount, mContractAuthor ity, 1);
227 // This reduces unnecessary resource usage. See http://crbug.com/480 688 for details. 240 // This reduces unnecessary resource usage. See http://crbug.com/480 688 for details.
228 mSyncContentResolverDelegate.removePeriodicSync( 241 mSyncContentResolverDelegate.removePeriodicSync(
229 mAccount, mContractAuthority, Bundle.EMPTY); 242 mAccount, mContractAuthority, Bundle.EMPTY);
230 } 243 }
231 StrictMode.setThreadPolicy(oldPolicy); 244 StrictMode.setThreadPolicy(oldPolicy);
232 245
233 // Disable the syncability of Chrome for all other accounts. 246 // Disable the syncability of Chrome for all other accounts.
234 AccountManagerHelper.get().getGoogleAccounts(new Callback<Account[]>() { 247 AccountManagerHelper.get().getGoogleAccounts(new Callback<Account[]>() {
235 @Override 248 @Override
236 public void onResult(Account[] accounts) { 249 public void onResult(Account[] accounts) {
237 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWr ites(); 250 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWr ites();
238 for (Account account : accounts) { 251 for (Account account : accounts) {
239 if (!account.equals(mAccount) 252 if (!account.equals(mAccount)
240 && mSyncContentResolverDelegate.getIsSyncable( 253 && mSyncContentResolverDelegate.getIsSyncable(
241 account, mContractAuthority) 254 account, mContractAuthority)
242 > 0) { 255 > 0) {
243 mSyncContentResolverDelegate.setIsSyncable(account, mCon tractAuthority, 0); 256 mSyncContentResolverDelegate.setIsSyncable(account, mCon tractAuthority, 0);
244 } 257 }
245 } 258 }
246 StrictMode.setThreadPolicy(oldPolicy); 259 StrictMode.setThreadPolicy(oldPolicy);
260
261 if (callback != null) callback.onResult(null);
247 } 262 }
248 }); 263 });
249 } 264 }
250 265
251 /** 266 /**
252 * Helper class to be used by observers whenever sync settings change. 267 * Helper class to be used by observers whenever sync settings change.
253 * 268 *
254 * To register the observer, call AndroidSyncSettings.registerObserver(...). 269 * To register the observer, call AndroidSyncSettings.registerObserver(...).
255 */ 270 */
256 private class AndroidSyncSettingsChangedObserver implements SyncStatusObserv er { 271 private class AndroidSyncSettingsChangedObserver implements SyncStatusObserv er {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 private void notifyObservers() { 313 private void notifyObservers() {
299 for (AndroidSyncSettingsObserver observer : mObservers) { 314 for (AndroidSyncSettingsObserver observer : mObservers) {
300 observer.androidSyncSettingsChanged(); 315 observer.androidSyncSettingsChanged();
301 } 316 }
302 } 317 }
303 318
304 private String getContractAuthority() { 319 private String getContractAuthority() {
305 return mApplicationContext.getPackageName(); 320 return mApplicationContext.getPackageName();
306 } 321 }
307 } 322 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698