[2/3] OmniGears: battery styles

Change-Id: Ib5032a8c6d5ea31da9ed3303e5653673c5fbd2d7
diff --git a/res/values/custom_arrays.xml b/res/values/custom_arrays.xml
index 8a9e2a1..f80d506 100644
--- a/res/values/custom_arrays.xml
+++ b/res/values/custom_arrays.xml
@@ -504,4 +504,22 @@
         <item>2</item>
         <item>3</item>
     </string-array>
+
+    <string-array name="entries_battery_styles" translatable="false">
+        <item>@string/battery_style_default</item>
+        <item>@string/battery_style_meter_horizontal</item>
+        <item>@string/battery_style_circle</item>
+        <item>@string/battery_style_text</item>
+        <item>@string/battery_style_round_rect</item>
+        <item>@string/battery_style_droid</item>
+    </string-array>
+
+    <string-array name="values_battery_styles" translatable="false">
+        <item>0</item>
+        <item>1</item>
+        <item>2</item>
+        <item>3</item>
+        <item>4</item>
+        <item>5</item>
+    </string-array>
 </resources>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 0ad04f6..056470f 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -180,8 +180,9 @@
     <string name="statusbar_battery_summary">Set style of status bar battery display</string>
     <string name="battery_style_title">Style</string>
     <string name="battery_percent_title">Show percent</string>
+    <string name="battery_percent_summary">Show battery percentage</string>
     <string name="statusbar_percent_inside_title">Percent overlay</string>
-    <string name="statusbar_percent_inside_summary">Display percent on top of the the battery image</string>
+    <string name="statusbar_percent_inside_summary">Display battery percentage on top of the the battery image</string>
     <string name="statusbar_charging_image_title">Charging indicator</string>
     <string name="statusbar_category_charging_title">Charging</string>
     <string name="statusbar_charging_image_summary">Display bolt indicator while charging. Hides percent overlay</string>
@@ -639,4 +640,11 @@
     <string name="omni_theme_select_summary">Browse and select installed Omni themes</string>
 
     <string name="keys_action_notifications">Notifications</string>
+
+    <string name="battery_style_text">Text only</string>
+    <string name="battery_style_default">Default</string>
+    <string name="battery_style_meter_horizontal">Horizontal bar</string>
+    <string name="battery_style_circle">Circle</string>
+    <string name="battery_style_droid">Droid</string>
+    <string name="battery_style_round_rect">Square</string>
 </resources>
diff --git a/res/xml/bars_settings.xml b/res/xml/bars_settings.xml
index 5655f3c..9de6cdd 100644
--- a/res/xml/bars_settings.xml
+++ b/res/xml/bars_settings.xml
@@ -39,6 +39,18 @@
                 android:summary="@string/network_traffic_summary"
                 android:fragment="org.omnirom.omnigears.interfacesettings.NetworkTraffic" />
 
+            <org.omnirom.omnigears.preference.SystemSettingSwitchPreference
+                android:key="status_bar_logo"
+                android:title="@string/status_bar_logo_title"
+                android:summary="@string/status_bar_logo_summary"
+                android:defaultValue="false" />
+
+            <Preference
+                android:key="statusbar_battery"
+                android:title="@string/battery_title"
+                android:summary="@string/statusbar_battery_summary"
+                android:fragment="org.omnirom.omnigears.interfacesettings.StatusbarBatterySettings" />
+
         </PreferenceCategory>
 
         <PreferenceCategory
diff --git a/res/xml/statusbar_battery_settings.xml b/res/xml/statusbar_battery_settings.xml
new file mode 100644
index 0000000..0c1c868
--- /dev/null
+++ b/res/xml/statusbar_battery_settings.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--  Copyright (C) 2015-2018 The OmniROM Project
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+-->
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
+
+        <ListPreference
+            android:key="statusbar_battery_style"
+            android:title="@string/battery_style_title"
+            android:entries="@array/entries_battery_styles"
+            android:entryValues="@array/values_battery_styles"
+            android:persistent="false"
+            android:defaultValue="0"/>
+
+        <org.omnirom.omnigears.preference.SystemSettingSwitchPreference
+            android:key="statusbar_battery_percent"
+            android:title="@string/battery_percent_title"
+            android:summary="@string/battery_percent_summary"
+            android:defaultValue="false"/>
+
+        <org.omnirom.omnigears.preference.SystemSettingSwitchPreference
+            android:key="statusbar_battery_percent_inside"
+            android:title="@string/statusbar_percent_inside_title"
+            android:summary="@string/statusbar_percent_inside_summary"
+            android:defaultValue="false"/>
+
+        <PreferenceCategory
+            android:key="statusbar_category_charging"
+            android:title="@string/statusbar_category_charging_title">
+
+            <org.omnirom.omnigears.preference.SystemSettingSwitchPreference
+                android:key="statusbar_battery_charging_image"
+                android:title="@string/statusbar_charging_image_title"
+                android:summary="@string/statusbar_charging_image_summary"
+                android:defaultValue="true"/>
+            <!--<org.omnirom.omnigears.preference.SystemCheckBoxPreference
+                android:key="statusbar_battery_charging_color_enable"
+                android:title="@string/statusbar_battery_charging_color_enable_title"
+                android:summary="@string/statusbar_battery_charging_color_enable_summary"
+                android:defaultValue="true"/>
+            <org.omnirom.omnigears.preference.ColorPickerPreference
+                android:key="statusbar_battery_charging_color"
+                android:title="@string/statusbar_battery_charging_color_title"
+                android:persistent="false"
+            android:dependency ="statusbar_battery_charging_color_enable"/>-->
+
+        </PreferenceCategory>
+</PreferenceScreen>
diff --git a/res/xml/style_settings.xml b/res/xml/style_settings.xml
index d7e3a3b..1bbfc6b 100644
--- a/res/xml/style_settings.xml
+++ b/res/xml/style_settings.xml
@@ -108,15 +108,4 @@
             android:dependency="status_bar_custom_header" />
 
     </PreferenceCategory>
-
-    <PreferenceCategory
-        android:key="category_statusbar"
-        android:title="@string/statusbar_title">
-
-        <org.omnirom.omnigears.preference.SystemSettingSwitchPreference
-            android:key="status_bar_logo"
-            android:title="@string/status_bar_logo_title"
-            android:summary="@string/status_bar_logo_summary"
-            android:defaultValue="false" />
-    </PreferenceCategory>
 </PreferenceScreen>
diff --git a/src/org/omnirom/omnigears/interfacesettings/StatusbarBatterySettings.java b/src/org/omnirom/omnigears/interfacesettings/StatusbarBatterySettings.java
new file mode 100644
index 0000000..c794c70
--- /dev/null
+++ b/src/org/omnirom/omnigears/interfacesettings/StatusbarBatterySettings.java
@@ -0,0 +1,164 @@
+/*
+ *  Copyright (C) 2015-2018 The OmniROM Project
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+*/
+package org.omnirom.omnigears.interfacesettings;
+
+import android.app.AlertDialog;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.net.TrafficStats;
+import android.os.Bundle;
+import android.os.RemoteException;
+import android.support.v7.preference.CheckBoxPreference;
+import android.support.v7.preference.ListPreference;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceGroup;
+import android.support.v7.preference.PreferenceScreen;
+import android.support.v7.preference.PreferenceCategory;
+import android.support.v14.preference.PreferenceFragment;
+import android.support.v14.preference.SwitchPreference;
+import android.provider.SearchIndexableResource;
+import android.provider.Settings;
+import android.provider.Settings.SettingNotFoundException;
+
+import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.internal.util.omni.DeviceUtils;
+import com.android.settings.Utils;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.Indexable;
+
+import com.android.internal.util.omni.OmniSwitchConstants;
+import com.android.internal.util.omni.PackageUtils;
+
+import java.util.List;
+import java.util.ArrayList;
+
+//import org.omnirom.omnigears.preference.ColorPickerPreference;
+import org.omnirom.omnigears.preference.SeekBarPreference;
+import org.omnirom.omnigears.preference.SystemCheckBoxPreference;
+
+public class StatusbarBatterySettings extends SettingsPreferenceFragment implements
+        Preference.OnPreferenceChangeListener, Indexable {
+    private static final String TAG = "StatusbarBatterySettings";
+
+    private static final String STATUSBAR_BATTERY_STYLE = "statusbar_battery_style";
+    private static final String STATUSBAR_BATTERY_PERCENT = "statusbar_battery_percent";
+    //private static final String STATUSBAR_CHARGING_COLOR = "statusbar_battery_charging_color";
+    private static final String STATUSBAR_BATTERY_PERCENT_INSIDE = "statusbar_battery_percent_inside";
+    private static final String STATUSBAR_BATTERY_SHOW_BOLT = "statusbar_battery_charging_image";
+
+    private ListPreference mBatteryStyle;
+    private Preference mBatteryPercent;
+    //private ColorPickerPreference mChargingColor;
+    private Preference mPercentInside;
+    private Preference mShowBolt;
+    private int mBatteryStyleValue;
+
+    @Override
+    public int getMetricsCategory() {
+        return MetricsEvent.OMNI_SETTINGS;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        addPreferencesFromResource(R.xml.statusbar_battery_settings);
+
+        PreferenceScreen prefScreen = getPreferenceScreen();
+        ContentResolver resolver = getActivity().getContentResolver();
+
+        mBatteryStyle = (ListPreference) findPreference(STATUSBAR_BATTERY_STYLE);
+        mBatteryStyleValue = Settings.System.getInt(resolver,
+                Settings.System.STATUSBAR_BATTERY_STYLE, 0);
+
+        mBatteryStyle.setValue(Integer.toString(mBatteryStyleValue));
+        mBatteryStyle.setSummary(mBatteryStyle.getEntry());
+        mBatteryStyle.setOnPreferenceChangeListener(this);
+
+
+        /*mChargingColor = (ColorPickerPreference) prefScreen.findPreference(STATUSBAR_CHARGING_COLOR);
+        int chargingColor = Settings.System.getInt(resolver, Settings.System.STATUSBAR_BATTERY_CHARGING_COLOR, 0xFFFFFFFF);
+        mChargingColor.setColor(chargingColor);
+        String hexColor = String.format("#%08X", chargingColor);
+        mChargingColor.setSummary(hexColor);
+        mChargingColor.setOnPreferenceChangeListener(this);*/
+
+        mPercentInside = findPreference(STATUSBAR_BATTERY_PERCENT_INSIDE);
+        mBatteryPercent = findPreference(STATUSBAR_BATTERY_PERCENT);
+        mShowBolt = findPreference(STATUSBAR_BATTERY_SHOW_BOLT);
+
+        //updateEnablement();
+    }
+
+    @Override
+    public boolean onPreferenceTreeClick(Preference preference) {
+        // If we didn't handle it, let preferences handle it.
+        return super.onPreferenceTreeClick(preference);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        ContentResolver resolver = getActivity().getContentResolver();
+        if (preference == mBatteryStyle) {
+            mBatteryStyleValue = Integer.valueOf((String) newValue);
+            int index = mBatteryStyle.findIndexOfValue((String) newValue);
+            mBatteryStyle.setSummary(
+                    mBatteryStyle.getEntries()[index]);
+            Settings.System.putInt(getContentResolver(),
+                    Settings.System.STATUSBAR_BATTERY_STYLE, mBatteryStyleValue);
+        /*} else if (preference == mChargingColor) {
+            String hexColor = String.format("#%08X", mChargingColor.getColor());
+            mChargingColor.setSummary(hexColor);
+            Settings.System.putInt(resolver,
+                    Settings.System.STATUSBAR_BATTERY_CHARGING_COLOR, mChargingColor.getColor());*/
+        }
+        //updateEnablement();
+        return true;
+    }
+
+    /*private void updateEnablement() {
+        mPercentInside.setEnabled(mBatteryStyleValue != 3 && mBatteryStyleValue != 4);
+        mShowBolt.setEnabled(mBatteryStyleValue != 3 && mBatteryStyleValue != 4);
+        mBatteryPercent.setEnabled(mBatteryStyleValue != 3);
+    }*/
+
+    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+                @Override
+                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
+                        boolean enabled) {
+                    ArrayList<SearchIndexableResource> result =
+                            new ArrayList<SearchIndexableResource>();
+
+                    SearchIndexableResource sir = new SearchIndexableResource(context);
+                    sir.xmlResId = R.xml.statusbar_battery_settings;
+                    result.add(sir);
+
+                    return result;
+                }
+
+                @Override
+                public List<String> getNonIndexableKeys(Context context) {
+                    ArrayList<String> result = new ArrayList<String>();
+                    return result;
+                }
+            };
+}