Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / sync / android / java / src / org / chromium / sync / notifier / SyncStatusHelper.java
index 77b6b30..5178387 100644 (file)
@@ -10,9 +10,8 @@ import android.content.Context;
 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;
 
@@ -103,7 +102,7 @@ public class SyncStatusHelper {
 
             mAccount = account;
 
-            StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
+            StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
             mSyncAutomatically = mSyncContentResolverDelegate.getSyncAutomatically(
                     account, mContractAuthority);
             mIsSyncable = mSyncContentResolverDelegate.getIsSyncable(account, mContractAuthority);
@@ -116,7 +115,7 @@ public class SyncStatusHelper {
         @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;
@@ -125,7 +124,7 @@ public class SyncStatusHelper {
         @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;
@@ -186,7 +185,7 @@ public class SyncStatusHelper {
     }
 
     private void updateMasterSyncAutomaticallySetting() {
-        StrictMode.ThreadPolicy oldPolicy = temporarilyAllowDiskWritesAndDiskReads();
+        StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
         synchronized (mCachedSettings) {
             mCachedMasterSyncAutomatically = mSyncContentResolverDelegate
                     .getMasterSyncAutomatically();
@@ -284,7 +283,7 @@ public class SyncStatusHelper {
                 mCachedSettings.getSyncAutomatically(account);
         }
 
-        notifyObservers();
+        notifyObserversIfAccountSettingsChanged();
         return returnValue;
     }
 
@@ -317,7 +316,7 @@ public class SyncStatusHelper {
             returnValue = mCachedSettings.getSyncAutomatically(account);
         }
 
-        notifyObservers();
+        notifyObserversIfAccountSettingsChanged();
         return returnValue;
     }
 
@@ -344,7 +343,7 @@ public class SyncStatusHelper {
             mCachedSettings.setSyncAutomatically(account, true);
         }
 
-        notifyObservers();
+        notifyObserversIfAccountSettingsChanged();
     }
 
     /**
@@ -357,7 +356,7 @@ public class SyncStatusHelper {
             mCachedSettings.setSyncAutomatically(account, false);
         }
 
-        notifyObservers();
+        notifyObserversIfAccountSettingsChanged();
     }
 
     /**
@@ -371,7 +370,7 @@ public class SyncStatusHelper {
             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.
@@ -398,35 +397,22 @@ public class SyncStatusHelper {
         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) {
@@ -436,8 +422,13 @@ public class SyncStatusHelper {
         return didGetStatusUpdate;
     }
 
+    private void notifyObserversIfAccountSettingsChanged() {
+        if (getAndClearDidUpdateStatus()) {
+            notifyObservers();
+        }
+    }
+
     private void notifyObservers() {
-        if (!getAndClearDidUpdateStatus()) return;
         for (SyncSettingsChangedObserver observer : mObservers) {
             observer.syncSettingsChanged();
         }