import android.content.SyncStatusObserver;
import android.os.StrictMode;
-import com.google.common.annotations.VisibleForTesting;
-
import org.chromium.base.ObserverList;
+import org.chromium.base.VisibleForTesting;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
mAccount = account;
- StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
+ StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
mSyncAutomatically = mSyncContentResolverDelegate.getSyncAutomatically(
account, mContractAuthority);
mIsSyncable = mSyncContentResolverDelegate.getIsSyncable(account, mContractAuthority);
@VisibleForTesting
protected void setIsSyncableInternal(Account account) {
mIsSyncable = 1;
- StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
+ StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
mSyncContentResolverDelegate.setIsSyncable(account, mContractAuthority, 1);
StrictMode.setThreadPolicy(oldPolicy);
mDidUpdate = true;
@VisibleForTesting
protected void setSyncAutomaticallyInternal(Account account, boolean value) {
mSyncAutomatically = value;
- StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
+ StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
mSyncContentResolverDelegate.setSyncAutomatically(account, mContractAuthority, value);
StrictMode.setThreadPolicy(oldPolicy);
mDidUpdate = true;
}
private void updateMasterSyncAutomaticallySetting() {
- StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
+ StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
synchronized (mCachedSettings) {
mCachedMasterSyncAutomatically = mSyncContentResolverDelegate
.getMasterSyncAutomatically();
mCachedSettings.getSyncAutomatically(account);
}
- notifyObservers();
+ notifyObserversIfAccountSettingsChanged();
return returnValue;
}
returnValue = mCachedSettings.getSyncAutomatically(account);
}
- notifyObservers();
+ notifyObserversIfAccountSettingsChanged();
return returnValue;
}
mCachedSettings.setSyncAutomatically(account, true);
}
- notifyObservers();
+ notifyObserversIfAccountSettingsChanged();
}
/**
mCachedSettings.setSyncAutomatically(account, false);
}
- notifyObservers();
+ notifyObserversIfAccountSettingsChanged();
}
/**
mCachedSettings.setIsSyncable(account);
}
- StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
+ StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
// Disable the syncability of Chrome for all other accounts. Don't use
// our cache as we're touching many accounts that aren't signed in, so this saves
// extra calls to Android sync configuration.
public void onStatusChanged(int which) {
if (ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS == which) {
// Sync settings have changed; update our in-memory caches
- updateMasterSyncAutomaticallySetting();
synchronized (mCachedSettings) {
mCachedSettings.updateSyncSettingsForAccount(
ChromeSigninController.get(mApplicationContext).getSignedInUser());
}
- notifyObservers();
+
+ boolean oldMasterSyncEnabled = isMasterSyncAutomaticallyEnabled();
+ updateMasterSyncAutomaticallySetting();
+ boolean didMasterSyncChanged =
+ oldMasterSyncEnabled != isMasterSyncAutomaticallyEnabled();
+ // Notify observers if MasterSync or account level settings change.
+ if (didMasterSyncChanged || getAndClearDidUpdateStatus())
+ notifyObservers();
}
}
}
- /**
- * Sets a new StrictMode.ThreadPolicy based on the current one, but allows disk reads
- * and disk writes.
- *
- * The return value is the old policy, which must be applied after the disk access is finished,
- * by using StrictMode.setThreadPolicy(oldPolicy).
- *
- * @return the policy before allowing reads and writes.
- */
- private static StrictMode.ThreadPolicy temporarilyAllowDiskWritesAndDiskReads() {
- StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
- StrictMode.ThreadPolicy.Builder newPolicy =
- new StrictMode.ThreadPolicy.Builder(oldPolicy);
- newPolicy.permitDiskReads();
- newPolicy.permitDiskWrites();
- StrictMode.setThreadPolicy(newPolicy.build());
- return oldPolicy;
- }
-
private boolean getAndClearDidUpdateStatus() {
boolean didGetStatusUpdate;
synchronized (mCachedSettings) {
return didGetStatusUpdate;
}
+ private void notifyObserversIfAccountSettingsChanged() {
+ if (getAndClearDidUpdateStatus()) {
+ notifyObservers();
+ }
+ }
+
private void notifyObservers() {
- if (!getAndClearDidUpdateStatus()) return;
for (SyncSettingsChangedObserver observer : mObservers) {
observer.syncSettingsChanged();
}