Settings: Import {Secure,System}SettingSwitchPreference

Change-Id: Iada6ca65ce2f4a8e79f1e17ea7e4dc0cd80c4cd6
diff --git a/src/com/android/settings/support/SecureSettingSwitchPreference.java b/src/com/android/settings/support/SecureSettingSwitchPreference.java
new file mode 100644
index 0000000..c10fffe
--- /dev/null
+++ b/src/com/android/settings/support/SecureSettingSwitchPreference.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2014 The CyanogenMod project
+ *               2020 Android Ice Cold 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 com.android.settings.support;
+
+import android.content.Context;
+import android.util.AttributeSet;
+
+import androidx.preference.SwitchPreference;
+
+public class SecureSettingSwitchPreference extends SwitchPreference {
+
+    public SecureSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        setPreferenceDataStore(new SecureSettingsStore(context.getContentResolver()));
+    }
+
+    public SecureSettingSwitchPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setPreferenceDataStore(new SecureSettingsStore(context.getContentResolver()));
+    }
+
+    public SecureSettingSwitchPreference(Context context) {
+        super(context);
+        setPreferenceDataStore(new SecureSettingsStore(context.getContentResolver()));
+    }
+
+    @Override
+    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
+        // This is what default TwoStatePreference implementation is doing without respecting
+        // real default value:
+        //setChecked(restoreValue ? getPersistedBoolean(mChecked)
+        //        : (Boolean) defaultValue);
+        // Instead, we better do
+        setChecked(restoreValue ? getPersistedBoolean((Boolean) defaultValue)
+                : (Boolean) defaultValue);
+    }
+}
diff --git a/src/com/android/settings/support/SecureSettingsStore.java b/src/com/android/settings/support/SecureSettingsStore.java
new file mode 100644
index 0000000..3ff9d7d
--- /dev/null
+++ b/src/com/android/settings/support/SecureSettingsStore.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2020 Android Ice Cold Project
+ *                    CarbonROM
+ *
+ * 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 com.android.settings.support;
+
+import android.content.ContentResolver;
+import android.preference.PreferenceDataStore;
+import android.provider.Settings;
+
+public class SecureSettingsStore extends androidx.preference.PreferenceDataStore
+        implements PreferenceDataStore {
+
+    private ContentResolver mContentResolver;
+
+    public SecureSettingsStore(ContentResolver contentResolver) {
+        mContentResolver = contentResolver;
+    }
+
+    public boolean getBoolean(String key, boolean defValue) {
+        return getInt(key, defValue ? 1 : 0) != 0;
+    }
+
+    public float getFloat(String key, float defValue) {
+        return Settings.Secure.getFloat(mContentResolver, key, defValue);
+    }
+
+    public int getInt(String key, int defValue) {
+        return Settings.Secure.getInt(mContentResolver, key, defValue);
+    }
+
+    public long getLong(String key, long defValue) {
+        return Settings.Secure.getLong(mContentResolver, key, defValue);
+    }
+
+    public String getString(String key, String defValue) {
+        String result = Settings.Secure.getString(mContentResolver, key);
+        return result == null ? defValue : result;
+    }
+
+    public void putBoolean(String key, boolean value) {
+        putInt(key, value ? 1 : 0);
+    }
+
+    public void putFloat(String key, float value) {
+        Settings.Secure.putFloat(mContentResolver, key, value);
+    }
+
+    public void putInt(String key, int value) {
+        Settings.Secure.putInt(mContentResolver, key, value);
+    }
+
+    public void putLong(String key, long value) {
+        Settings.Secure.putLong(mContentResolver, key, value);
+    }
+
+    public void putString(String key, String value) {
+        Settings.Secure.putString(mContentResolver, key, value);
+    }
+
+}
diff --git a/src/com/android/settings/support/SystemSettingSwitchPreference.java b/src/com/android/settings/support/SystemSettingSwitchPreference.java
new file mode 100644
index 0000000..15ae6ed
--- /dev/null
+++ b/src/com/android/settings/support/SystemSettingSwitchPreference.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2014 The CyanogenMod Project
+ * Copyright (C) 2017 AICP
+ *
+ * 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 com.android.settings.support;
+
+import android.content.Context;
+import androidx.preference.SwitchPreference;
+import android.util.AttributeSet;
+
+public class SystemSettingSwitchPreference extends SwitchPreference {
+
+    public SystemSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
+    }
+
+    public SystemSettingSwitchPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
+    }
+
+    public SystemSettingSwitchPreference(Context context) {
+        super(context);
+        setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
+    }
+
+    @Override
+    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
+        // This is what default TwoStatePreference implementation is doing without respecting
+        // real default value:
+        //setChecked(restoreValue ? getPersistedBoolean(mChecked)
+        //        : (Boolean) defaultValue);
+        // Instead, we better do
+        setChecked(restoreValue ? getPersistedBoolean((Boolean) defaultValue)
+                : (Boolean) defaultValue);
+    }
+}
diff --git a/src/com/android/settings/support/SystemSettingsStore.java b/src/com/android/settings/support/SystemSettingsStore.java
new file mode 100644
index 0000000..7bd6132
--- /dev/null
+++ b/src/com/android/settings/support/SystemSettingsStore.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2017 AICP
+ *
+ * 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 com.android.settings.support;
+
+import android.content.ContentResolver;
+import android.preference.PreferenceDataStore;
+import android.provider.Settings;
+
+public class SystemSettingsStore extends androidx.preference.PreferenceDataStore
+        implements PreferenceDataStore {
+
+    private ContentResolver mContentResolver;
+
+    public SystemSettingsStore(ContentResolver contentResolver) {
+        mContentResolver = contentResolver;
+    }
+
+    public boolean getBoolean(String key, boolean defValue) {
+        return getInt(key, defValue ? 1 : 0) != 0;
+    }
+
+    public float getFloat(String key, float defValue) {
+        return Settings.System.getFloat(mContentResolver, key, defValue);
+    }
+
+    public int getInt(String key, int defValue) {
+        return Settings.System.getInt(mContentResolver, key, defValue);
+    }
+
+    public long getLong(String key, long defValue) {
+        return Settings.System.getLong(mContentResolver, key, defValue);
+    }
+
+    public String getString(String key, String defValue) {
+        String result = Settings.System.getString(mContentResolver, key);
+        return result == null ? defValue : result;
+    }
+
+    public void putBoolean(String key, boolean value) {
+        putInt(key, value ? 1 : 0);
+    }
+
+    public void putFloat(String key, float value) {
+        Settings.System.putFloat(mContentResolver, key, value);
+    }
+
+    public void putInt(String key, int value) {
+        Settings.System.putInt(mContentResolver, key, value);
+    }
+
+    public void putLong(String key, long value) {
+        Settings.System.putLong(mContentResolver, key, value);
+    }
+
+    public void putString(String key, String value) {
+        Settings.System.putString(mContentResolver, key, value);
+    }
+
+}