[2/3] OmniGears: battery percent overlay display

PS2:
-add toggle for charging flash

PS3:
-add custom charging coloring

Change-Id: I92dba2bda1319e9cebc45a076744d07124e17bcc
diff --git a/res/layout/preference_color_picker.xml b/res/layout/preference_color_picker.xml
new file mode 100644
index 0000000..b944dd0
--- /dev/null
+++ b/res/layout/preference_color_picker.xml
@@ -0,0 +1,46 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:gravity="center_vertical"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:background="?android:attr/selectableItemBackground" >
+
+    <RelativeLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:paddingTop="16dp"
+        android:paddingBottom="16dp">
+
+        <TextView android:id="@+android:id/title"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:singleLine="true"
+            android:textAppearance="?android:attr/textAppearanceListItem"
+            android:ellipsize="marquee"
+            android:fadingEdge="horizontal" />
+
+        <TextView android:id="@+android:id/summary"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_below="@android:id/title"
+            android:layout_alignStart="@android:id/title"
+            android:textAppearance="?android:attr/textAppearanceListItemSecondary"
+            android:textColor="?android:attr/textColorSecondary"
+            android:maxLines="1" />
+    </RelativeLayout>
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginEnd="8dp">
+
+        <ImageView
+            android:id="@+id/light_color"
+            android:layout_width="@dimen/color_preference_width"
+            android:layout_height="@dimen/color_preference_height"
+            android:layout_gravity="center" />
+    </LinearLayout>
+</LinearLayout>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 3d7140f..36696ca 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -146,12 +146,19 @@
     <string name="led_color_red">Red</string>
     <string name="led_color_blue">Blue</string>
 
-    <string name="battery_title">Status bar battery</string>
+    <string name="statusbar_title">Status bar</string>
+    <string name="battery_title">Battery display</string>
     <string name="battery_style_title">Style</string>
     <string name="battery_percent_title">Show percent</string>
     <string name="battery_percent_none">Never</string>
     <string name="battery_percent_always">Always</string>
     <string name="battery_percent_expanded">Expanded</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_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>
+    <string name="statusbar_battery_charging_color_title">Color battery while charging</string>
 
     <string name="button_brightness_title">Brightness</string>
     <string name="button_brightness_summary">Button backlight brightness</string>
diff --git a/res/xml/bars_settings.xml b/res/xml/bars_settings.xml
index ac2878e..a26e7a2 100644
--- a/res/xml/bars_settings.xml
+++ b/res/xml/bars_settings.xml
@@ -18,28 +18,21 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
 
-        <com.android.settings.preference.SystemCheckBoxPreference
-            android:key="status_bar_brightness_control"
-            android:title="@string/status_bar_brightness_control_title"
-            android:summary="@string/status_bar_brightness_control_summary"
-            android:defaultValue="false"/>
-
         <PreferenceCategory
-            android:key="battery_category"
-            android:title="@string/battery_title" >
+            android:key="category_statusbar"
+            android:title="@string/statusbar_title">
 
-            <ListPreference
-                android:key="statusbar_battery_style"
-                android:title="@string/battery_style_title"
-                android:entries="@*android:array/entries_battery_styles"
-                android:entryValues="@*android:array/values_battery_styles"
-                android:persistent="false"/>
-            <ListPreference
-                android:key="statusbar_battery_percent"
-                android:title="@string/battery_percent_title"
-                android:entries="@array/entries_battery_percent"
-                android:entryValues="@array/values_battery_percent"
-                android:persistent="false"/>
+            <com.android.settings.preference.SystemCheckBoxPreference
+                android:key="status_bar_brightness_control"
+                android:title="@string/status_bar_brightness_control_title"
+                android:summary="@string/status_bar_brightness_control_summary"
+                android:defaultValue="false"/>
+
+            <PreferenceScreen
+                android:key="statusbar_battery"
+                android:title="@string/battery_title"
+                android:fragment="org.omnirom.omnigears.interfacesettings.StatusbarBatterySettings" />
+
         </PreferenceCategory>
 
         <!-- Network traffic meter -->
diff --git a/res/xml/statusbar_battery_settings.xml b/res/xml/statusbar_battery_settings.xml
new file mode 100644
index 0000000..21aec74
--- /dev/null
+++ b/res/xml/statusbar_battery_settings.xml
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--  Copyright (C) 2015 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="@*android:array/entries_battery_styles"
+            android:entryValues="@*android:array/values_battery_styles"
+            android:persistent="false"/>
+        <ListPreference
+            android:key="statusbar_battery_percent"
+            android:title="@string/battery_percent_title"
+            android:entries="@array/entries_battery_percent"
+            android:entryValues="@array/values_battery_percent"
+            android:persistent="false"/>
+        <com.android.settings.preference.SystemCheckBoxPreference
+            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">
+
+            <com.android.settings.preference.SystemCheckBoxPreference
+                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.ui.ColorPickerPreference
+                android:key="statusbar_battery_charging_color"
+                android:title="@string/statusbar_battery_charging_color_title"
+                android:persistent="false" />
+
+        </PreferenceCategory>
+</PreferenceScreen>
diff --git a/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java b/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java
index 4e374b9..cf86166 100644
--- a/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java
+++ b/src/org/omnirom/omnigears/interfacesettings/BarsSettings.java
@@ -54,8 +54,6 @@
         Preference.OnPreferenceChangeListener, Indexable {
     private static final String TAG = "BarsSettings";
 
-    private static final String STATUSBAR_BATTERY_STYLE = "statusbar_battery_style";
-    private static final String STATUSBAR_BATTERY_PERCENT = "statusbar_battery_percent";
     private static final String NAVIGATION_BAR_CATEGORY = "navbar_category";
     private static final String NAVIGATION_BAR_RECENTS_STYLE = "navbar_recents_style";
     private static final String NETWORK_TRAFFIC_STATE = "network_traffic_state";
@@ -64,8 +62,6 @@
     private static final String NETWORK_TRAFFIC_AUTOHIDE = "network_traffic_autohide";
     private static final String NETWORK_TRAFFIC_AUTOHIDE_THRESHOLD = "network_traffic_autohide_threshold";
 
-    private ListPreference mBatteryStyle;
-    private ListPreference mBatteryPercent;
     private ListPreference mNavbarRecentsStyle;
     private ListPreference mNetTrafficState;
     private ListPreference mNetTrafficUnit;
@@ -88,22 +84,6 @@
         PreferenceScreen prefScreen = getPreferenceScreen();
         ContentResolver resolver = getActivity().getContentResolver();
 
-        mBatteryStyle = (ListPreference) findPreference(STATUSBAR_BATTERY_STYLE);
-        int batteryStyle = Settings.System.getInt(resolver,
-                Settings.System.STATUSBAR_BATTERY_STYLE, 0);
-
-        mBatteryStyle.setValue(Integer.toString(batteryStyle));
-        mBatteryStyle.setSummary(mBatteryStyle.getEntry());
-        mBatteryStyle.setOnPreferenceChangeListener(this);
-
-        mBatteryPercent = (ListPreference) findPreference(STATUSBAR_BATTERY_PERCENT);
-        int batteryPercent = Settings.System.getInt(resolver,
-                Settings.System.STATUSBAR_BATTERY_PERCENT, 2);
-
-        mBatteryPercent.setValue(Integer.toString(batteryPercent));
-        mBatteryPercent.setSummary(mBatteryPercent.getEntry());
-        mBatteryPercent.setOnPreferenceChangeListener(this);
-
         final boolean showNavBar = DeviceUtils.deviceSupportNavigationBar(getActivity());
         final PreferenceCategory navbarCategory =
                 (PreferenceCategory) prefScreen.findPreference(NAVIGATION_BAR_CATEGORY);
@@ -170,21 +150,7 @@
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         ContentResolver resolver = getActivity().getContentResolver();
-        if (preference == mBatteryStyle) {
-            int value = Integer.valueOf((String) newValue);
-            int index = mBatteryStyle.findIndexOfValue((String) newValue);
-            mBatteryStyle.setSummary(
-                    mBatteryStyle.getEntries()[index]);
-            Settings.System.putInt(getContentResolver(),
-                    Settings.System.STATUSBAR_BATTERY_STYLE, value);
-        } else if (preference == mBatteryPercent) {
-            int value = Integer.valueOf((String) newValue);
-            int index = mBatteryPercent.findIndexOfValue((String) newValue);
-            mBatteryPercent.setSummary(
-                    mBatteryPercent.getEntries()[index]);
-            Settings.System.putInt(getContentResolver(),
-                    Settings.System.STATUSBAR_BATTERY_PERCENT, value);
-        } else if (preference == mNavbarRecentsStyle) {
+        if (preference == mNavbarRecentsStyle) {
             int value = Integer.valueOf((String) newValue);
             if (value == 1) {
                 if (!isOmniSwitchInstalled()){
diff --git a/src/org/omnirom/omnigears/interfacesettings/StatusbarBatterySettings.java b/src/org/omnirom/omnigears/interfacesettings/StatusbarBatterySettings.java
new file mode 100644
index 0000000..fa279a4
--- /dev/null
+++ b/src/org/omnirom/omnigears/interfacesettings/StatusbarBatterySettings.java
@@ -0,0 +1,153 @@
+/*
+ *  Copyright (C) 2015 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.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.PreferenceGroup;
+import android.preference.PreferenceScreen;
+import android.preference.PreferenceCategory;
+import android.provider.SearchIndexableResource;
+import android.provider.Settings;
+import android.provider.Settings.SettingNotFoundException;
+
+import com.android.settings.SettingsPreferenceFragment;
+import com.android.settings.R;
+import com.android.internal.util.omni.DeviceUtils;
+import com.android.settings.Utils;
+import com.android.settings.preference.SeekBarPreference;
+import com.android.settings.preference.SystemCheckBoxPreference;
+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.ui.ColorPickerPreference;
+
+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 ListPreference mBatteryStyle;
+    private ListPreference mBatteryPercent;
+    private ColorPickerPreference mChargingColor;
+
+    @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);
+        int batteryStyle = Settings.System.getInt(resolver,
+                Settings.System.STATUSBAR_BATTERY_STYLE, 0);
+
+        mBatteryStyle.setValue(Integer.toString(batteryStyle));
+        mBatteryStyle.setSummary(mBatteryStyle.getEntry());
+        mBatteryStyle.setOnPreferenceChangeListener(this);
+
+        mBatteryPercent = (ListPreference) findPreference(STATUSBAR_BATTERY_PERCENT);
+        int batteryPercent = Settings.System.getInt(resolver,
+                Settings.System.STATUSBAR_BATTERY_PERCENT, 2);
+
+        mBatteryPercent.setValue(Integer.toString(batteryPercent));
+        mBatteryPercent.setSummary(mBatteryPercent.getEntry());
+        mBatteryPercent.setOnPreferenceChangeListener(this);
+
+        mChargingColor = (ColorPickerPreference) prefScreen.findPreference(STATUSBAR_CHARGING_COLOR);
+        // TODO 0xFFFFFFFF is not really the default - but the config value R.color.batterymeter_charge_color is not exposed
+        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);
+    }
+
+    @Override
+    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
+        // If we didn't handle it, let preferences handle it.
+        return super.onPreferenceTreeClick(preferenceScreen, preference);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        ContentResolver resolver = getActivity().getContentResolver();
+        if (preference == mBatteryStyle) {
+            int value = Integer.valueOf((String) newValue);
+            int index = mBatteryStyle.findIndexOfValue((String) newValue);
+            mBatteryStyle.setSummary(
+                    mBatteryStyle.getEntries()[index]);
+            Settings.System.putInt(getContentResolver(),
+                    Settings.System.STATUSBAR_BATTERY_STYLE, value);
+        } else if (preference == mBatteryPercent) {
+            int value = Integer.valueOf((String) newValue);
+            int index = mBatteryPercent.findIndexOfValue((String) newValue);
+            mBatteryPercent.setSummary(
+                    mBatteryPercent.getEntries()[index]);
+            Settings.System.putInt(getContentResolver(),
+                    Settings.System.STATUSBAR_BATTERY_PERCENT, value);
+        } 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());
+        }
+        return true;
+    }
+
+    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;
+                }
+            };
+}
diff --git a/src/org/omnirom/omnigears/ui/ColorPickerPreference.java b/src/org/omnirom/omnigears/ui/ColorPickerPreference.java
new file mode 100644
index 0000000..86a62d3
--- /dev/null
+++ b/src/org/omnirom/omnigears/ui/ColorPickerPreference.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2012 The CyanogenMod 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.omnigears.ui;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.res.Resources;
+import android.graphics.drawable.ShapeDrawable;
+import android.graphics.drawable.shapes.RectShape;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import com.android.settings.R;
+
+public class ColorPickerPreference extends Preference implements DialogInterface.OnDismissListener {
+
+    private static String TAG = "ColorPickerPreference";
+    public static final int DEFAULT_COLOR = 0xFFFFFFFF; //White
+
+    private ImageView mLightColorView;
+    private Resources mResources;
+    private int mColorValue;
+    private Dialog mDialog;
+
+    /**
+     * @param context
+     * @param attrs
+     */
+    public ColorPickerPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        mColorValue = DEFAULT_COLOR;
+        init();
+    }
+
+    public ColorPickerPreference(Context context, int color) {
+        super(context, null);
+        mColorValue = color;
+        init();
+    }
+
+    private void init() {
+        setLayoutResource(R.layout.preference_color_picker);
+        mResources = getContext().getResources();
+    }
+
+    public void setColor(int color) {
+        mColorValue = color;
+        updatePreferenceViews();
+    }
+
+    public int getColor() {
+        return mColorValue;
+    }
+
+    @Override
+    protected void onBindView(View view) {
+        super.onBindView(view);
+
+        mLightColorView = (ImageView) view.findViewById(R.id.light_color);
+
+        updatePreferenceViews();
+    }
+
+    private void updatePreferenceViews() {
+        final int width = (int) mResources.getDimension(R.dimen.color_preference_width);
+        final int height = (int) mResources.getDimension(R.dimen.color_preference_height);
+
+        if (mLightColorView != null) {
+            mLightColorView.setEnabled(true);
+            mLightColorView.setImageDrawable(createRectShape(width, height, mColorValue));
+        }
+    }
+
+    @Override
+    protected void onClick() {
+        if (mDialog != null && mDialog.isShowing()) return;
+        mDialog = getDialog();
+        mDialog.setOnDismissListener(this);
+        mDialog.show();
+    }
+
+    public Dialog getDialog() {
+        final ColorPickerDialog d = new ColorPickerDialog(getContext(), mColorValue);
+        d.setAlphaSliderVisible(true);
+
+        d.setButton(AlertDialog.BUTTON_POSITIVE, mResources.getString(R.string.ok),
+                new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                mColorValue =  d.getColor();
+                updatePreferenceViews();
+                callChangeListener(this);
+            }
+        });
+        d.setButton(AlertDialog.BUTTON_NEGATIVE, mResources.getString(R.string.cancel),
+                (DialogInterface.OnClickListener) null);
+
+        return d;
+    }
+
+    private static ShapeDrawable createRectShape(int width, int height, int color) {
+        ShapeDrawable shape = new ShapeDrawable(new RectShape());
+        shape.setIntrinsicHeight(height);
+        shape.setIntrinsicWidth(width);
+        shape.getPaint().setColor(color);
+        return shape;
+    }
+
+    @Override
+    public void onDismiss(DialogInterface dialog) {
+        mDialog = null;
+    }
+}