cmparts: Replace remaining observers with Observatory

Change-Id: I6f537b3b3820dac719b0a63fd1bbe319b7565cd5
diff --git a/src/org/cyanogenmod/cmparts/hardware/DisplayRotation.java b/src/org/cyanogenmod/cmparts/hardware/DisplayRotation.java
index a057bf5..779fa31 100644
--- a/src/org/cyanogenmod/cmparts/hardware/DisplayRotation.java
+++ b/src/org/cyanogenmod/cmparts/hardware/DisplayRotation.java
@@ -17,18 +17,16 @@
 package org.cyanogenmod.cmparts.hardware;
 
 import android.content.Context;
-import android.database.ContentObserver;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.Handler;
+import android.provider.Settings;
+import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.CheckBoxPreference;
 import android.support.v7.preference.Preference;
-import android.support.v7.preference.Preference.OnPreferenceChangeListener;
 import android.support.v7.preference.PreferenceScreen;
-import android.support.v14.preference.SwitchPreference;
-import android.provider.Settings;
 
 import com.android.internal.view.RotationPolicy;
+
 import org.cyanogenmod.cmparts.R;
 import org.cyanogenmod.cmparts.SettingsPreferenceFragment;
 
diff --git a/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java b/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java
index b7e96c7..6cca88e 100644
--- a/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java
+++ b/src/org/cyanogenmod/cmparts/livedisplay/LiveDisplay.java
@@ -15,14 +15,10 @@
  */
 package org.cyanogenmod.cmparts.livedisplay;
 
-import android.content.ContentResolver;
 import android.content.Context;
 import android.content.res.Resources;
-import android.database.ContentObserver;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.Handler;
-import android.os.UserHandle;
 import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.ListPreference;
 import android.support.v7.preference.Preference;
@@ -47,6 +43,7 @@
 import cyanogenmod.hardware.DisplayMode;
 import cyanogenmod.hardware.LiveDisplayConfig;
 import cyanogenmod.hardware.LiveDisplayManager;
+import cyanogenmod.preference.SettingsHelper;
 import cyanogenmod.providers.CMSettings;
 
 import static cyanogenmod.hardware.LiveDisplayManager.FEATURE_CABC;
@@ -58,7 +55,7 @@
 import static cyanogenmod.hardware.LiveDisplayManager.MODE_OUTDOOR;
 
 public class LiveDisplay extends SettingsPreferenceFragment implements Searchable,
-        Preference.OnPreferenceChangeListener {
+        Preference.OnPreferenceChangeListener, SettingsHelper.OnSettingsChangeListener {
 
     private static final String TAG = "LiveDisplay";
 
@@ -83,8 +80,12 @@
     private static final String COLOR_PROFILE_SUMMARY =
             KEY_LIVE_DISPLAY_COLOR_PROFILE + "_%s_summary";
 
-    private final Handler mHandler = new Handler();
-    private final SettingsObserver mObserver = new SettingsObserver();
+    private final Uri DISPLAY_TEMPERATURE_DAY_URI =
+            CMSettings.System.getUriFor(CMSettings.System.DISPLAY_TEMPERATURE_DAY);
+    private final Uri DISPLAY_TEMPERATURE_NIGHT_URI =
+            CMSettings.System.getUriFor(CMSettings.System.DISPLAY_TEMPERATURE_NIGHT);
+    private final Uri DISPLAY_TEMPERATURE_MODE_URI =
+            CMSettings.System.getUriFor(CMSettings.System.DISPLAY_TEMPERATURE_MODE);
 
     private ListPreference mLiveDisplay;
 
@@ -216,13 +217,14 @@
         updateModeSummary();
         updateTemperatureSummary();
         updateColorProfileSummary(null);
-        mObserver.register(true);
+        SettingsHelper.get(getActivity()).startWatching(this, DISPLAY_TEMPERATURE_DAY_URI,
+                DISPLAY_TEMPERATURE_MODE_URI, DISPLAY_TEMPERATURE_NIGHT_URI);
     }
 
     @Override
     public void onPause() {
         super.onPause();
-        mObserver.register(false);
+        SettingsHelper.get(getActivity()).stopWatching(this);
     }
 
     private static String getStringForResourceName(Resources res,
@@ -355,35 +357,10 @@
         return true;
     }
 
-    private final class SettingsObserver extends ContentObserver {
-        private final Uri DISPLAY_TEMPERATURE_DAY_URI =
-                CMSettings.System.getUriFor(CMSettings.System.DISPLAY_TEMPERATURE_DAY);
-        private final Uri DISPLAY_TEMPERATURE_NIGHT_URI =
-                CMSettings.System.getUriFor(CMSettings.System.DISPLAY_TEMPERATURE_NIGHT);
-        private final Uri DISPLAY_TEMPERATURE_MODE_URI =
-                CMSettings.System.getUriFor(CMSettings.System.DISPLAY_TEMPERATURE_MODE);
-
-        public SettingsObserver() {
-            super(mHandler);
-        }
-
-        public void register(boolean register) {
-            final ContentResolver cr = getContentResolver();
-            if (register) {
-                cr.registerContentObserver(DISPLAY_TEMPERATURE_DAY_URI, false, this, UserHandle.USER_ALL);
-                cr.registerContentObserver(DISPLAY_TEMPERATURE_NIGHT_URI, false, this, UserHandle.USER_ALL);
-                cr.registerContentObserver(DISPLAY_TEMPERATURE_MODE_URI, false, this, UserHandle.USER_ALL);
-            } else {
-                cr.unregisterContentObserver(this);
-            }
-        }
-
-        @Override
-        public void onChange(boolean selfChange, Uri uri) {
-            super.onChange(selfChange, uri);
-            updateModeSummary();
-            updateTemperatureSummary();
-        }
+    @Override
+    public void onSettingsChanged(Uri uri) {
+        updateModeSummary();
+        updateTemperatureSummary();
     }
 
 
diff --git a/src/org/cyanogenmod/cmparts/widget/CMBaseSystemSettingSwitchBar.java b/src/org/cyanogenmod/cmparts/widget/CMBaseSystemSettingSwitchBar.java
index 5d368f0..250d482 100644
--- a/src/org/cyanogenmod/cmparts/widget/CMBaseSystemSettingSwitchBar.java
+++ b/src/org/cyanogenmod/cmparts/widget/CMBaseSystemSettingSwitchBar.java
@@ -16,19 +16,18 @@
 
 package org.cyanogenmod.cmparts.widget;
 
-import android.content.ContentResolver;
 import android.content.Context;
-import android.database.ContentObserver;
 import android.net.Uri;
-import android.os.Handler;
 import android.widget.Switch;
 
+import cyanogenmod.preference.SettingsHelper;
 import cyanogenmod.providers.CMSettings;
 
-public class CMBaseSystemSettingSwitchBar implements SwitchBar.OnSwitchChangeListener {
+public class CMBaseSystemSettingSwitchBar implements SwitchBar.OnSwitchChangeListener,
+        SettingsHelper.OnSettingsChangeListener {
+
     private Context mContext;
     private SwitchBar mSwitchBar;
-    private SettingsObserver mSettingsObserver;
     private boolean mListeningToOnSwitchChange = false;
 
     private boolean mStateMachineEvent;
@@ -48,7 +47,6 @@
         mSettingKey = key;
         mDefaultState = defaultState ? 1 : 0;
         mCallback = callback;
-        mSettingsObserver = new SettingsObserver(new Handler());
         setupSwitchBar();
     }
 
@@ -73,7 +71,8 @@
         mContext = context;
         if (!mListeningToOnSwitchChange) {
             mSwitchBar.addOnSwitchChangeListener(this);
-            mSettingsObserver.observe();
+            SettingsHelper.get(mContext).startWatching(
+                    this, CMSettings.System.getUriFor(mSettingKey));
 
             mListeningToOnSwitchChange = true;
         }
@@ -82,7 +81,7 @@
     public void pause() {
         if (mListeningToOnSwitchChange) {
             mSwitchBar.removeOnSwitchChangeListener(this);
-            mSettingsObserver.unobserve();
+            SettingsHelper.get(mContext).stopWatching(this);
 
             mListeningToOnSwitchChange = false;
         }
@@ -121,35 +120,8 @@
         }
     }
 
-    class SettingsObserver extends ContentObserver {
-        SettingsObserver(Handler handler) {
-            super(handler);
-        }
-
-        void observe() {
-            ContentResolver resolver = mContext.getContentResolver();
-            resolver.registerContentObserver(CMSettings.System.getUriFor(
-                    mSettingKey), false, this);
-            update();
-        }
-
-        void unobserve() {
-            ContentResolver resolver = mContext.getContentResolver();
-            resolver.unregisterContentObserver(this);
-        }
-
-        @Override
-        public void onChange(boolean selfChange) {
-            update();
-        }
-
-        @Override
-        public void onChange(boolean selfChange, Uri uri) {
-            update();
-        }
-
-        public void update() {
-            setSwitchState();
-        }
+    @Override
+    public void onSettingsChanged(Uri uri) {
+        setSwitchState();
     }
 }