Blissify: Smart Pixels [2/2]

Change-Id: I143232d73bdc7d79eace779db484cb837cabc7f4
diff --git a/res/values/bliss_arrays.xml b/res/values/bliss_arrays.xml
index 8154b8b..ed6a753 100644
--- a/res/values/bliss_arrays.xml
+++ b/res/values/bliss_arrays.xml
@@ -572,6 +572,51 @@
         <item>1</item>
         <item>2</item>
         <item>3</item>
-    </string-array>    
+    </string-array>
 
+    <!-- Smart Pixels -->
+    <string-array name="smart_pixels_percent_strings" translatable="false">
+        <item>12</item>
+        <item>25</item>
+        <item>38</item>
+        <item>50</item>
+        <item>62</item>
+        <item>75</item>
+        <item>88</item>
+    </string-array>
+
+    <string-array name="smart_pixels_percent_values" translatable="false">
+        <item>0</item>
+        <item>1</item>
+        <item>2</item>
+        <item>3</item>
+        <item>4</item>
+        <item>5</item>
+        <item>6</item>
+    </string-array>
+
+    <string-array name="smart_pixels_shift_times">
+        <item>15 seconds</item>
+        <item>30 seconds</item>
+        <item>1 minute</item>
+        <item>2 minutes</item>
+        <item>5 minutes</item>
+        <item>10 minutes</item>
+        <item>20 minutes</item>
+        <item>30 minutes</item>
+        <item>1 hour</item>
+    </string-array>
+
+    <string-array name="smart_pixels_shift_values">
+        <item>0</item>
+        <item>1</item>
+        <item>2</item>
+        <item>3</item>
+        <item>4</item>
+        <item>5</item>
+        <item>6</item>
+        <item>7</item>
+        <item>8</item>
+    </string-array>
+    
 </resources>
diff --git a/res/values/bliss_strings.xml b/res/values/bliss_strings.xml
index 94dfe0e..3259622 100644
--- a/res/values/bliss_strings.xml
+++ b/res/values/bliss_strings.xml
@@ -471,5 +471,14 @@
     <string name="screen_off_animation_crt">CRT</string>
     <string name="screen_off_animation_scale">Scale</string>
     <string name="screen_off_animation_title">Screen off animation</string>
-
+    
+    <!-- Smart Pixels -->
+    <string name="smart_pixels_title">Smart Pixels</string>
+    <string name="smart_pixels_summary">Save battery by shutting off extra pixels</string>
+    <string name="smart_pixels_on_power_save_title">Auto-enable on battery saver</string>
+    <string name="smart_pixels_on_power_save_summary">Enable with battery saver to increase power savings</string>
+    <string name="smart_pixels_percent">Percent of pixels to disable</string>
+    <string name="smart_pixels_shift">Burn-in protection interval</string>
+    
 </resources>
+
diff --git a/res/xml/blissify_misc.xml b/res/xml/blissify_misc.xml
index 68d45cf..4a76ab3 100644
--- a/res/xml/blissify_misc.xml
+++ b/res/xml/blissify_misc.xml
@@ -69,4 +69,11 @@
 
     </PreferenceCategory>
 
+    <com.bliss.support.preferences.SystemSettingMasterSwitchPreference
+        android:key="smart_pixels_enable"
+        android:fragment="com.blissroms.blissify.fragments.misc.SmartPixels"
+        android:title="@string/smart_pixels_title"
+        android:summary="@string/smart_pixels_summary"
+        android:defaultValue="false" />
+
 </PreferenceScreen>
diff --git a/res/xml/smart_pixels.xml b/res/xml/smart_pixels.xml
new file mode 100644
index 0000000..dde4d95
--- /dev/null
+++ b/res/xml/smart_pixels.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 BlissRoms 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.
+-->
+<PreferenceScreen
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
+	android:title="@string/smart_pixels_title">
+
+    <com.bliss.support.preferences.SystemSettingSwitchPreference
+        android:key="smart_pixels_on_power_save"
+        android:title="@string/smart_pixels_on_power_save_title"
+        android:summary="@string/smart_pixels_on_power_save_summary"
+        android:defaultValue="false"/>
+
+    <com.bliss.support.preferences.SystemSettingListPreference
+        android:key="smart_pixels_pattern"
+        android:entries="@array/smart_pixels_percent_strings"
+        android:entryValues="@array/smart_pixels_percent_values"
+        android:defaultValue="5"
+        android:title="@string/smart_pixels_percent" />
+
+    <com.bliss.support.preferences.SystemSettingListPreference
+        android:key="smart_pixels_shift_timeout"
+        android:entries="@array/smart_pixels_shift_times"
+        android:entryValues="@array/smart_pixels_shift_values"
+        android:defaultValue="5"
+        android:title="@string/smart_pixels_shift" />
+
+</PreferenceScreen>
diff --git a/src/com/blissroms/blissify/fragments/misc/MiscSettings.java b/src/com/blissroms/blissify/fragments/misc/MiscSettings.java
index afad4b3..316baf4 100644
--- a/src/com/blissroms/blissify/fragments/misc/MiscSettings.java
+++ b/src/com/blissroms/blissify/fragments/misc/MiscSettings.java
@@ -36,6 +36,7 @@
 import androidx.preference.PreferenceCategory;
 import androidx.preference.Preference.OnPreferenceChangeListener;
 import androidx.preference.SwitchPreference;
+import com.bliss.support.preferences.SystemSettingMasterSwitchPreference;
 
 import java.util.Locale;
 import android.text.TextUtils;
@@ -57,6 +58,9 @@
 
     private static final String FINGERPRINT_VIB = "fingerprint_success_vib";
 
+    private static final String SMART_PIXELS_ENABLED = "smart_pixels_enable";
+
+    private SystemSettingMasterSwitchPreference mSmartPixelsEnabled;
     private FingerprintManager mFingerprintManager;
     private SwitchPreference mFingerprintVib;
     private SwitchPreference mShowCpuInfo;
@@ -86,6 +90,16 @@
                 Settings.Global.SHOW_CPU_OVERLAY, 0) == 1);
         mShowCpuInfo.setOnPreferenceChangeListener(this);
 
+        mSmartPixelsEnabled = (SystemSettingMasterSwitchPreference) findPreference(SMART_PIXELS_ENABLED);
+        mSmartPixelsEnabled.setOnPreferenceChangeListener(this);
+        int smartPixelsEnabled = Settings.System.getInt(getContentResolver(),
+                SMART_PIXELS_ENABLED, 0);
+        mSmartPixelsEnabled.setChecked(smartPixelsEnabled != 0);
+
+        if (!getResources().getBoolean(com.android.internal.R.bool.config_enableSmartPixels)) {
+            getPreferenceScreen().removePreference(mSmartPixelsEnabled);
+        }
+        
     }
 
     private void writeCpuInfoOptions(boolean value) {
@@ -117,6 +131,13 @@
             return true;
         }
 
+        if (preference == mSmartPixelsEnabled) {
+            boolean value = (Boolean) objValue;
+            Settings.System.putInt(getContentResolver(),
+		            SMART_PIXELS_ENABLED, value ? 1 : 0);
+            return true;
+        }
+        
         return false;
     }
 
diff --git a/src/com/blissroms/blissify/fragments/misc/SmartPixels.java b/src/com/blissroms/blissify/fragments/misc/SmartPixels.java
new file mode 100644
index 0000000..d44504a
--- /dev/null
+++ b/src/com/blissroms/blissify/fragments/misc/SmartPixels.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2018 CarbonROM
+ * Copyright (C) 2019 BlissRoms
+ *
+ * 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.blissroms.blissify.fragments.misc;
+
+import android.content.Context;
+import android.content.ContentResolver;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.os.UserHandle;
+import android.os.PowerManager;
+import androidx.preference.*;
+import android.provider.Settings;
+
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+
+import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
+
+import com.bliss.support.preferences.SystemSettingSwitchPreference;
+
+public class SmartPixels extends SettingsPreferenceFragment implements
+        Preference.OnPreferenceChangeListener {
+        private static final String TAG = "SmartPixels";
+
+        private static final String ON_POWER_SAVE = "smart_pixels_on_power_save";
+
+        private SystemSettingSwitchPreference mSmartPixelsOnPowerSave;
+
+        ContentResolver resolver;
+
+        @Override
+        public void onCreate(Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
+            addPreferencesFromResource(R.xml.smart_pixels);
+            resolver = getActivity().getContentResolver();
+
+            mSmartPixelsOnPowerSave = (SystemSettingSwitchPreference) findPreference(ON_POWER_SAVE);
+
+            updateDependency();
+        }
+
+        @Override
+        public int getMetricsCategory() {
+            return MetricsEvent.BLISSIFY;
+        }
+
+        @Override
+        public void onResume() {
+            super.onResume();
+        }
+
+        @Override
+        public void onPause() {
+            super.onPause();
+        }
+
+        public boolean onPreferenceChange(Preference preference, Object objValue) {
+            final String key = preference.getKey();
+            updateDependency();
+            return true;
+        }
+
+        private void updateDependency() {
+            boolean mUseOnPowerSave = (Settings.System.getIntForUser(
+                    resolver, Settings.System.SMART_PIXELS_ON_POWER_SAVE,
+                    0, UserHandle.USER_CURRENT) == 1);
+            PowerManager pm = (PowerManager)getActivity().getSystemService(Context.POWER_SERVICE);
+            if (pm.isPowerSaveMode() && mUseOnPowerSave) {
+                mSmartPixelsOnPowerSave.setEnabled(false);
+            } else {
+                mSmartPixelsOnPowerSave.setEnabled(true);
+            }
+        }
+    }