[1/2] OmniLib: add SystemSettingsColorSelectPreference

Change-Id: Ibf830386f9cfcb5d77e1696e4786f483bfd7bd3f
diff --git a/res/values/custom_attrs.xml b/res/values/custom_attrs.xml
index b17034b..5be682d 100644
--- a/res/values/custom_attrs.xml
+++ b/res/values/custom_attrs.xml
@@ -32,6 +32,12 @@
         <attr name="withAlpha" format="boolean" />
     </declare-styleable>
 
+    <declare-styleable name="SystemSettingsColorSelectPreference">
+        <attr name="ledPreview" format="boolean" />
+        <attr name="multiColor" format="boolean" />
+        <attr name="withAlpha" format="boolean" />
+    </declare-styleable>
+
     <declare-styleable name="SecureSettingsSwitch">
         <attr name="initialValue" format="integer" />
     </declare-styleable>
diff --git a/src/org/omnirom/omnilib/preference/ColorSelectPreference.java b/src/org/omnirom/omnilib/preference/ColorSelectPreference.java
index 28ff78c..14db8f2 100644
--- a/src/org/omnirom/omnilib/preference/ColorSelectPreference.java
+++ b/src/org/omnirom/omnilib/preference/ColorSelectPreference.java
@@ -39,7 +39,7 @@
 
     private ImageView mLightColorView;
     private Resources mResources;
-    private int mColorValue;
+    protected int mColorValue;
     private AlertDialog mDialog;
 
     private boolean mShowLedPreview;
@@ -68,7 +68,7 @@
         init(context, attrs);
     }
 
-    private void init(Context context, AttributeSet attrs) {
+    protected void init(Context context, AttributeSet attrs) {
         setLayoutResource(R.layout.preference_color_select);
         mResources = getContext().getResources();
         if (attrs != null) {
@@ -139,14 +139,15 @@
                 new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
+                int color = 0;
                 if (mShowLedPreview) {
-                    mColorValue =  d.getColor() & 0x00FFFFFF; // strip alpha, led does not support it
+                    color =  d.getColor() & 0x00FFFFFF; // strip alpha, led does not support it
                     d.switchOffLed();
                 } else {
-                    mColorValue =  d.getColor();
+                    color =  d.getColor();
                 }
-                updatePreferenceViews();
-                callChangeListener(new Integer(mColorValue));
+                setColor(color);
+                callChangeListener(new Integer(color));
             }
         });
         d.setButton(AlertDialog.BUTTON_NEGATIVE, mResources.getString(android.R.string.cancel),
diff --git a/src/org/omnirom/omnilib/preference/SystemSettingsColorSelectPreference.java b/src/org/omnirom/omnilib/preference/SystemSettingsColorSelectPreference.java
new file mode 100644
index 0000000..cda2087
--- /dev/null
+++ b/src/org/omnirom/omnilib/preference/SystemSettingsColorSelectPreference.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2021 The OmniROM project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.omnirom.omnilib.preference;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+
+public class SystemSettingsColorSelectPreference extends ColorSelectPreference {
+    private int mDefaultColor = ColorSelectPreference.DEFAULT_COLOR;
+
+    public SystemSettingsColorSelectPreference(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    public SystemSettingsColorSelectPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public SystemSettingsColorSelectPreference(Context context) {
+        super(context, null);
+    }
+
+    @Override
+    protected boolean persistInt(int value) {
+        if (!shouldPersist()) {
+            return false;
+        }
+
+        if (value == getPersistedInt(~value)) {
+            // It's already there, so the same as persisting
+            return true;
+        }
+
+        Settings.System.putInt(getContext().getContentResolver(), getKey(), value);
+        return true;
+    }
+
+    @Override
+    protected int getPersistedInt(int defaultReturnValue) {
+        if (!shouldPersist()) {
+            return defaultReturnValue;
+        }
+
+        return Settings.System.getInt(getContext().getContentResolver(),
+                getKey(), defaultReturnValue);
+    }
+
+    public void updateColor() {
+        super.setColor(getPersistedInt(mColorValue));
+    }
+
+    @Override
+    public void setColor(int color) {
+        super.setColor(color);
+        persistInt(color);
+    }
+
+    @Override
+    public void setDefaultValue(Object defaultValue) {
+        mDefaultColor = (Integer) defaultValue;
+        super.setDefaultValue(defaultValue);
+        super.setColor(getPersistedInt((Integer) defaultValue));
+    }
+
+    public void resetToDefaultValue() {
+        setColor(mDefaultColor);
+    }
+}