support: Extend default switches from LOS

Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
Change-Id: I097db871126466a763f7e4852caa0f4e4d73c2ed
diff --git a/src/com/bliss/support/preferences/GlobalSettingSwitchPreference.java b/src/com/bliss/support/preferences/GlobalSettingSwitchPreference.java
index fec3f77..01b2153 100644
--- a/src/com/bliss/support/preferences/GlobalSettingSwitchPreference.java
+++ b/src/com/bliss/support/preferences/GlobalSettingSwitchPreference.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 The CyanogenMod project
+ * Copyright (C) 2016-2017 crDroid Android
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package com.bliss.support.preferences;
 
 import android.content.Context;
@@ -57,7 +56,8 @@
     }
 
     @Override
-    protected boolean isPersisted() {
-        return Settings.Global.getString(getContext().getContentResolver(), getKey()) != null;
+    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
+        setChecked(Settings.Global.getString(getContext().getContentResolver(), getKey()) != null ? getPersistedBoolean(isChecked())
+                : (Boolean) defaultValue);
     }
 }
diff --git a/src/com/bliss/support/preferences/SecureSettingSwitchPreference.java b/src/com/bliss/support/preferences/SecureSettingSwitchPreference.java
index d38ee96..a488e04 100644
--- a/src/com/bliss/support/preferences/SecureSettingSwitchPreference.java
+++ b/src/com/bliss/support/preferences/SecureSettingSwitchPreference.java
@@ -22,7 +22,9 @@
 import android.os.UserHandle;
 import android.util.AttributeSet;
 
-public class SecureSettingSwitchPreference extends SwitchPreference {
+import lineageos.preference.SelfRemovingSwitchPreference;
+
+public class SecureSettingSwitchPreference extends SelfRemovingSwitchPreference {
 
     public SecureSettingSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
@@ -33,24 +35,22 @@
     }
 
     public SecureSettingSwitchPreference(Context context) {
-        super(context, null);
-    }
-
-    @Override
-    protected boolean persistBoolean(boolean value) {
-        Settings.Secure.putIntForUser(getContext().getContentResolver(),
-            getKey(), value ? 1 : 0, UserHandle.USER_CURRENT);
-        return true;
-    }
-
-    @Override
-    protected boolean getPersistedBoolean(boolean defaultReturnValue) {
-        return Settings.Secure.getIntForUser(getContext().getContentResolver(),
-                getKey(), defaultReturnValue ? 1 : 0, UserHandle.USER_CURRENT) != 0;
+        super(context);
     }
 
     @Override
     protected boolean isPersisted() {
         return Settings.Secure.getString(getContext().getContentResolver(), getKey()) != null;
     }
+
+    @Override
+    protected void putBoolean(String key, boolean value) {
+        Settings.Secure.putIntForUser(getContext().getContentResolver(), key, value ? 1 : 0, UserHandle.USER_CURRENT);
+    }
+
+    @Override
+    protected boolean getBoolean(String key, boolean defaultValue) {
+        return Settings.Secure.getIntForUser(getContext().getContentResolver(),
+                key, defaultValue ? 1 : 0, UserHandle.USER_CURRENT) != 0;
+    }
 }
diff --git a/src/com/bliss/support/preferences/SecureSettingsStore.java b/src/com/bliss/support/preferences/SecureSettingsStore.java
index 8b31f47..d44bcf0 100644
--- a/src/com/bliss/support/preferences/SecureSettingsStore.java
+++ b/src/com/bliss/support/preferences/SecureSettingsStore.java
@@ -18,6 +18,7 @@
 package com.bliss.support.preferences;
 
 import android.content.ContentResolver;
+import android.os.UserHandle;
 import android.preference.PreferenceDataStore;
 import android.provider.Settings;
 
diff --git a/src/com/bliss/support/preferences/SystemSettingSwitchPreference.java b/src/com/bliss/support/preferences/SystemSettingSwitchPreference.java
index 42de247..8224f80 100644
--- a/src/com/bliss/support/preferences/SystemSettingSwitchPreference.java
+++ b/src/com/bliss/support/preferences/SystemSettingSwitchPreference.java
@@ -22,38 +22,35 @@
 import android.os.UserHandle;
 import android.util.AttributeSet;
 
-public class SystemSettingSwitchPreference extends SwitchPreference {
+import lineageos.preference.SelfRemovingSwitchPreference;
+
+public class SystemSettingSwitchPreference extends SelfRemovingSwitchPreference {
 
     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, null);
-        setPreferenceDataStore(new SystemSettingsStore(context.getContentResolver()));
-    }
-
-    @Override
-    protected boolean persistBoolean(boolean value) {
-        Settings.System.putIntForUser(getContext().getContentResolver(),
-            getKey(), value ? 1 : 0, UserHandle.USER_CURRENT);
-        return true;
-    }
-
-    @Override
-    protected boolean getPersistedBoolean(boolean defaultReturnValue) {
-        return Settings.System.getIntForUser(getContext().getContentResolver(),
-                getKey(), defaultReturnValue ? 1 : 0, UserHandle.USER_CURRENT) != 0;
+        super(context);
     }
 
     @Override
     protected boolean isPersisted() {
         return Settings.System.getString(getContext().getContentResolver(), getKey()) != null;
     }
+
+    @Override
+    protected void putBoolean(String key, boolean value) {
+        Settings.System.putIntForUser(getContext().getContentResolver(), key, value ? 1 : 0, UserHandle.USER_CURRENT);
+    }
+
+    @Override
+    protected boolean getBoolean(String key, boolean defaultValue) {
+        return Settings.System.getIntForUser(getContext().getContentResolver(),
+                key, defaultValue ? 1 : 0, UserHandle.USER_CURRENT) != 0;
+    }
 }