[2/2] OmniControl: add SystemSettingsColorSelectPreference

Change-Id: I11d6bfa157cd92dfda88e31f2759dd934f404e6f
diff --git a/app/src/main/java/org/omnirom/control/BatteryLightSettingsFragment.java b/app/src/main/java/org/omnirom/control/BatteryLightSettingsFragment.java
index cbe4681..3d9db90 100644
--- a/app/src/main/java/org/omnirom/control/BatteryLightSettingsFragment.java
+++ b/app/src/main/java/org/omnirom/control/BatteryLightSettingsFragment.java
@@ -28,19 +28,18 @@
 import androidx.preference.PreferenceScreen;
 
 import org.omnirom.control.R;
-import org.omnirom.omnilib.preference.ColorSelectPreference;
+import org.omnirom.omnilib.preference.SystemSettingsColorSelectPreference;
 import org.omnirom.omnilib.preference.SystemSettingSwitchPreference;
 
 public class BatteryLightSettingsFragment extends AbstractSettingsFragment implements
                     Preference.OnPreferenceChangeListener {
 
     private static final String DEFAULT_VALUES = "reset_to_default_key";
-    private static final String LOW_COLOR_PREF = "low_color";
-    private static final String MEDIUM_COLOR_PREF = "medium_color";
-    private static final String FULL_COLOR_PREF = "full_color";
-    private static final String REALLY_FULL_COLOR_PREF = "really_full_color";
-    private static final String KEY_CATEGORY_GENERAL = "general_section";
-    private static final String FAST_COLOR_PREF = "fast_color";
+    private static final String LOW_COLOR_PREF = "battery_light_low_color";
+    private static final String MEDIUM_COLOR_PREF = "battery_light_medium_color";
+    private static final String FULL_COLOR_PREF = "battery_light_full_color";
+    private static final String REALLY_FULL_COLOR_PREF = "battery_light_really_full_color";
+    private static final String FAST_COLOR_PREF = "fast_battery_light_color";
     private static final String FAST_CHARGING_LED_PREF = "fast_charging_led_enabled";
     private static final String BATTERY_DND_PREF = "battery_light_allow_on_dnd";
     private static final String BATTERY_LIGHT_PREF = "battery_light_enabled";
@@ -49,11 +48,11 @@
     private static final String KEY_CATEGORY_FAST_CHARGE = "fast_color_cat";
     private static final String KEY_CATEGORY_CHARGE_COLORS = "colors_list";
 
-    private ColorSelectPreference mLowColorPref;
-    private ColorSelectPreference mMediumColorPref;
-    private ColorSelectPreference mFullColorPref;
-    private ColorSelectPreference mReallyFullColorPref;
-    private ColorSelectPreference mFastColorPref;
+    private SystemSettingsColorSelectPreference mLowColorPref;
+    private SystemSettingsColorSelectPreference mMediumColorPref;
+    private SystemSettingsColorSelectPreference mFullColorPref;
+    private SystemSettingsColorSelectPreference mReallyFullColorPref;
+    private SystemSettingsColorSelectPreference mFastColorPref;
     private Preference mDefaultValues;
     private SystemSettingSwitchPreference mDndPref;
     private SystemSettingSwitchPreference mEnabledPref;
@@ -82,6 +81,7 @@
 
         PreferenceScreen prefSet = getPreferenceScreen();
         ContentResolver resolver = getContext().getContentResolver();
+        Resources res = getResources();
 
         mDefaultValues = (Preference) findPreference(DEFAULT_VALUES);
         mDefaultValues.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@@ -104,20 +104,20 @@
             mFastBatteryLightEnabledPref = (SystemSettingSwitchPreference) findPreference(FAST_CHARGING_LED_PREF);
 
             // Low, Medium and full color preferences
-            mLowColorPref = (ColorSelectPreference) findPreference(LOW_COLOR_PREF);
-            mLowColorPref.setOnPreferenceChangeListener(this);
+            mLowColorPref = (SystemSettingsColorSelectPreference) findPreference(LOW_COLOR_PREF);
+            mLowColorPref.setDefaultValue(res.getInteger(com.android.internal.R.integer.config_notificationsBatteryLowARGB));
 
-            mMediumColorPref = (ColorSelectPreference) findPreference(MEDIUM_COLOR_PREF);
-            mMediumColorPref.setOnPreferenceChangeListener(this);
+            mMediumColorPref = (SystemSettingsColorSelectPreference) findPreference(MEDIUM_COLOR_PREF);
+            mMediumColorPref.setDefaultValue(res.getInteger(com.android.internal.R.integer.config_notificationsBatteryMediumARGB));
 
-            mFullColorPref = (ColorSelectPreference) findPreference(FULL_COLOR_PREF);
-            mFullColorPref.setOnPreferenceChangeListener(this);
+            mFullColorPref = (SystemSettingsColorSelectPreference) findPreference(FULL_COLOR_PREF);
+            mFullColorPref.setDefaultValue(res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));
 
-            mReallyFullColorPref = (ColorSelectPreference) findPreference(REALLY_FULL_COLOR_PREF);
-            mReallyFullColorPref.setOnPreferenceChangeListener(this);
+            mReallyFullColorPref = (SystemSettingsColorSelectPreference) findPreference(REALLY_FULL_COLOR_PREF);
+            mReallyFullColorPref.setDefaultValue(res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));
 
-            mFastColorPref = (ColorSelectPreference) findPreference(FAST_COLOR_PREF);
-            mFastColorPref.setOnPreferenceChangeListener(this);
+            mFastColorPref = (SystemSettingsColorSelectPreference) findPreference(FAST_COLOR_PREF);
+            mFastColorPref.setDefaultValue(res.getInteger(com.android.internal.R.integer.config_notificationsFastBatteryARGB));
 
             // Does the Device support fast charge ?
             if (!getResources().getBoolean(com.android.internal.R.bool.config_FastChargingLedSupported)) {
@@ -145,9 +145,6 @@
             boolean value = (Boolean) objValue;
             // If enabled, disable all but really full color preference.
             updateEnablement(value);
-        } else {
-            ColorSelectPreference lightPref = (ColorSelectPreference) preference;
-            updateValues(lightPref.getKey(), lightPref.getColor());
         }
         return true;
     }
@@ -194,82 +191,46 @@
     }
 
     protected void resetColors() {
-        ContentResolver resolver = getContext().getContentResolver();
-        Resources res = getResources();
-
-        // Reset to the framework default colors
-        Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_LOW_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsBatteryLowARGB));
-        Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_MEDIUM_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsBatteryMediumARGB));
-        Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_FULL_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));
-        Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_REALLY_FULL_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));
-        Settings.System.putInt(resolver, Settings.System.OMNI_FAST_BATTERY_LIGHT_COLOR,
-                res.getInteger(com.android.internal.R.integer.config_notificationsFastBatteryARGB));
-        refreshDefault();
-    }
-
-    private void refreshDefault() {
-        ContentResolver resolver = getContext().getContentResolver();
-        Resources res = getResources();
-
         if (mLowColorPref != null) {
-            int lowColor = Settings.System.getInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_LOW_COLOR,
-                    res.getInteger(com.android.internal.R.integer.config_notificationsBatteryLowARGB));
-            mLowColorPref.setColor(lowColor);
+            mLowColorPref.resetToDefaultValue();
         }
 
         if (mMediumColorPref != null) {
-            int mediumColor = Settings.System.getInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_MEDIUM_COLOR,
-                    res.getInteger(com.android.internal.R.integer.config_notificationsBatteryMediumARGB));
-            mMediumColorPref.setColor(mediumColor);
+            mMediumColorPref.resetToDefaultValue();
         }
 
         if (mFullColorPref != null) {
-            int fullColor = Settings.System.getInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_FULL_COLOR,
-                    res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));
-            mFullColorPref.setColor(fullColor);
+            mFullColorPref.resetToDefaultValue();
         }
 
         if (mReallyFullColorPref != null) {
-            int reallyFullColor = Settings.System.getInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_REALLY_FULL_COLOR,
-                    res.getInteger(com.android.internal.R.integer.config_notificationsBatteryFullARGB));
-            mReallyFullColorPref.setColor(reallyFullColor);
+            mReallyFullColorPref.resetToDefaultValue();
         }
 
         if (mFastColorPref != null) {
-            int fastColor = Settings.System.getInt(resolver, Settings.System.OMNI_FAST_BATTERY_LIGHT_COLOR,
-                    res.getInteger(com.android.internal.R.integer.config_notificationsFastBatteryARGB));
-            mFastColorPref.setColor(fastColor);
+            mFastColorPref.resetToDefaultValue();
         }
     }
 
-    /**
-     * Updates the default or application specific notification settings.
-     *
-     * @param key of the specific setting to update
-     * @param color
-     */
-    protected void updateValues(String key, Integer color) {
-        ContentResolver resolver = getContext().getContentResolver();
+    private void refreshDefault() {
+        if (mLowColorPref != null) {
+            mLowColorPref.updateColor();
+        }
 
-        if (key.equals(LOW_COLOR_PREF)) {
-            Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_LOW_COLOR,
-                    color);
-        } else if (key.equals(MEDIUM_COLOR_PREF)) {
-            Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_MEDIUM_COLOR,
-                    color);
-        } else if (key.equals(FULL_COLOR_PREF)) {
-            Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_FULL_COLOR,
-                    color);
-        } else if (key.equals(REALLY_FULL_COLOR_PREF)) {
-            Settings.System.putInt(resolver, Settings.System.OMNI_BATTERY_LIGHT_REALLY_FULL_COLOR,
-                    color);
-        } else if (key.equals(FAST_COLOR_PREF)) {
-            Settings.System.putInt(resolver, Settings.System.OMNI_FAST_BATTERY_LIGHT_COLOR,
-                    color);
+        if (mMediumColorPref != null) {
+            mMediumColorPref.updateColor();
+        }
+
+        if (mFullColorPref != null) {
+            mFullColorPref.updateColor();
+        }
+
+        if (mReallyFullColorPref != null) {
+            mReallyFullColorPref.updateColor();
+        }
+
+        if (mFastColorPref != null) {
+            mFastColorPref.updateColor();
         }
     }
 }
\ No newline at end of file
diff --git a/app/src/main/res/xml/battery_light_settings_preferences.xml b/app/src/main/res/xml/battery_light_settings_preferences.xml
index b12756c..bbc8cf7 100644
--- a/app/src/main/res/xml/battery_light_settings_preferences.xml
+++ b/app/src/main/res/xml/battery_light_settings_preferences.xml
@@ -42,25 +42,29 @@
         android:title="@string/battery_light_list_title"
         android:dependency="battery_light_enabled" >
 
-        <org.omnirom.omnilib.preference.ColorSelectPreference
-            android:key="low_color"
+        <org.omnirom.omnilib.preference.SystemSettingsColorSelectPreference
+            android:key="battery_light_low_color"
             omnilib:ledPreview="true"
-            android:title="@string/battery_light_low_color_title" />
+            android:title="@string/battery_light_low_color_title"
+            android:defaultValue="0" />
 
-        <org.omnirom.omnilib.preference.ColorSelectPreference
-            android:key="medium_color"
+        <org.omnirom.omnilib.preference.SystemSettingsColorSelectPreference
+            android:key="battery_light_medium_color"
             omnilib:ledPreview="true"
-            android:title="@string/battery_light_medium_color_title" />
+            android:title="@string/battery_light_medium_color_title"
+            android:defaultValue="0" />
 
-        <org.omnirom.omnilib.preference.ColorSelectPreference
-            android:key="full_color"
+        <org.omnirom.omnilib.preference.SystemSettingsColorSelectPreference
+            android:key="battery_light_full_color"
             omnilib:ledPreview="true"
-            android:title="@string/battery_light_full_color_title" />
+            android:title="@string/battery_light_full_color_title"
+            android:defaultValue="0" />
 
-        <org.omnirom.omnilib.preference.ColorSelectPreference
-            android:key="really_full_color"
+        <org.omnirom.omnilib.preference.SystemSettingsColorSelectPreference
+            android:key="battery_light_really_full_color"
             omnilib:ledPreview="true"
-            android:title="@string/battery_light_really_full_color_title" />
+            android:title="@string/battery_light_really_full_color_title"
+            android:defaultValue="0" />
 
     </PreferenceCategory>
 
@@ -75,12 +79,13 @@
             android:summary="@string/fast_charging_led_enabled_summary"
             android:defaultValue="false" />
 
-        <org.omnirom.omnilib.preference.ColorSelectPreference
-            android:key="fast_color"
+        <org.omnirom.omnilib.preference.SystemSettingsColorSelectPreference
+            android:key="fast_battery_light_color"
             omnilib:ledPreview="true"
             android:title="@string/fast_charging_light_color_title"
             android:summary="@string/fast_charging_light_color_summary"
-            android:dependency="fast_charging_led_enabled" />
+            android:dependency="fast_charging_led_enabled"
+            android:defaultValue="0" />
 
     </PreferenceCategory>