[3/3] OmniGears: Battery led customization: activate led preview

Change-Id: I8d75fe547802c7c0d54fc2903ac744b485725731
diff --git a/res/values/custom_attrs.xml b/res/values/custom_attrs.xml
index 3283a4f..ce2d08f 100644
--- a/res/values/custom_attrs.xml
+++ b/res/values/custom_attrs.xml
@@ -24,4 +24,8 @@
         <attr name="unitsLeft" format="string|reference" />
         <attr name="unitsRight" format="string|reference" />
     </declare-styleable>
+
+    <declare-styleable name="ColorSelectPreference">
+        <attr name="ledPreview" format="boolean" />
+    </declare-styleable>
 </resources>
diff --git a/res/xml/battery_light_settings.xml b/res/xml/battery_light_settings.xml
index 6e15acc..c5ef473 100644
--- a/res/xml/battery_light_settings.xml
+++ b/res/xml/battery_light_settings.xml
@@ -47,21 +47,25 @@
 
         <org.omnirom.omnigears.preference.ColorSelectPreference
             android:key="low_color"
+            ledPreview="true"
             android:title="@string/battery_light_low_color_title"
             android:persistent="false" />
 
         <org.omnirom.omnigears.preference.ColorSelectPreference
             android:key="medium_color"
+            ledPreview="true"
             android:title="@string/battery_light_medium_color_title"
             android:persistent="false" />
 
         <org.omnirom.omnigears.preference.ColorSelectPreference
             android:key="full_color"
+            ledPreview="true"
             android:title="@string/battery_light_full_color_title"
             android:persistent="false" />
 
         <org.omnirom.omnigears.preference.ColorSelectPreference
             android:key="really_full_color"
+            ledPreview="true"
             android:title="@string/battery_light_really_full_color_title"
             android:persistent="false" />
 
@@ -80,6 +84,7 @@
 
         <org.omnirom.omnigears.preference.ColorSelectPreference
             android:key="fast_color"
+            ledPreview="true"
             android:title="@string/fast_charging_light_color_title"
             android:summary="@string/fast_charging_light_color_summary"
             android:persistent="false"
diff --git a/src/org/omnirom/omnigears/preference/ColorSelectDialog.java b/src/org/omnirom/omnigears/preference/ColorSelectDialog.java
index 68f4f81..14a7516 100644
--- a/src/org/omnirom/omnigears/preference/ColorSelectDialog.java
+++ b/src/org/omnirom/omnigears/preference/ColorSelectDialog.java
@@ -19,6 +19,7 @@
 
 import android.app.Activity;
 import android.app.AlertDialog;
+import android.app.NotificationManager;
 import android.content.Context;
 import android.graphics.Color;
 import android.graphics.PixelFormat;
@@ -70,8 +71,14 @@
     private LedColorAdapter mLedColorAdapter;
     private boolean mWithAlpha;
 
-    protected ColorSelectDialog(Context context, int initialColor) {
+    private boolean mShowLedPreview;
+    private NotificationManager mNoMan;
+    private Context mContext;
+
+    protected ColorSelectDialog(Context context, int initialColor, boolean showLedPreview) {
         super(context);
+        mContext = context;
+        mShowLedPreview = showLedPreview;
         mWithAlpha = false;
         mMultiColor = getContext().getResources().getBoolean(R.bool.config_has_multi_color_led);
         init(initialColor);
@@ -96,6 +103,9 @@
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View layout = mInflater.inflate(R.layout.dialog_battery_settings, null);
 
+        mNoMan = (NotificationManager)
+                mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+
         mColorPicker = (ColorPickerView) layout.findViewById(R.id.color_picker_view);
         mHexColorInput = (EditText) layout.findViewById(R.id.hex_color_input);
         mNewColor = (ColorPanelView) layout.findViewById(R.id.color_panel);
@@ -109,6 +119,7 @@
         mHexColorInput.setOnFocusChangeListener(this);
         setAlphaSliderVisible(mWithAlpha);
         mColorPicker.setColor(color, true);
+        showLed(color);
 
         mColorList = (Spinner) layout.findViewById(R.id.color_list_spinner);
         mLedColorAdapter = new LedColorAdapter(
@@ -149,6 +160,7 @@
     public Bundle onSaveInstanceState() {
         Bundle state = super.onSaveInstanceState();
         state.putInt(STATE_KEY_COLOR, getColor());
+        switchOffLed();
         return state;
     }
 
@@ -166,6 +178,30 @@
 
         mNewColor.setColor(color);
         mHexColorInput.setText(String.format(Locale.US, format, color & mask));
+
+        showLed(color);
+    }
+
+    private void showLed(int color) {
+        if (mShowLedPreview) {
+            if (color == 0xFFFFFFFF) {
+                // argb white doesn't work
+                color = 0xffffff;
+            }
+            mNoMan.forceShowLedLight(color);
+        }
+    }
+
+    public void switchOffLed() {
+        if (mShowLedPreview) {
+            mNoMan.forceShowLedLight(-1);
+        }
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+        switchOffLed();
     }
 
     public void setAlphaSliderVisible(boolean visible) {
diff --git a/src/org/omnirom/omnigears/preference/ColorSelectPreference.java b/src/org/omnirom/omnigears/preference/ColorSelectPreference.java
index d969a33..1cca051 100644
--- a/src/org/omnirom/omnigears/preference/ColorSelectPreference.java
+++ b/src/org/omnirom/omnigears/preference/ColorSelectPreference.java
@@ -40,6 +40,8 @@
     private int mColorValue;
     private Dialog mDialog;
 
+    private boolean mShowLedPreview;
+
     /**
      * @param context
      * @param attrs
@@ -47,18 +49,25 @@
     public ColorSelectPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
         mColorValue = DEFAULT_COLOR;
-        init();
+        init(context, attrs);
     }
 
     public ColorSelectPreference(Context context, int color) {
         super(context, null);
         mColorValue = color;
-        init();
+        init(context, null);
     }
 
-    private void init() {
+    public ColorSelectPreference(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        mColorValue = DEFAULT_COLOR;
+        init(context, attrs);
+    }
+
+    private void init(Context context, AttributeSet attrs) {
         setLayoutResource(R.layout.preference_color_select);
         mResources = getContext().getResources();
+        mShowLedPreview = attrs.getAttributeBooleanValue(null, "ledPreview", false);
     }
 
     public void setColor(int color) {
@@ -99,19 +108,25 @@
 
     public Dialog getDialog() {
         final ColorSelectDialog d = new ColorSelectDialog(getContext(),
-                0xFF000000 | mColorValue);
+                0xFF000000 | mColorValue, mShowLedPreview);
 
         d.setButton(AlertDialog.BUTTON_POSITIVE, mResources.getString(R.string.ok),
                 new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 mColorValue =  d.getColor() & 0x00FFFFFF; // strip alpha, led does not support it
+                d.switchOffLed();
                 updatePreferenceViews();
                 callChangeListener(new Integer(mColorValue));
             }
         });
         d.setButton(AlertDialog.BUTTON_NEGATIVE, mResources.getString(R.string.cancel),
-                (DialogInterface.OnClickListener) null);
+                new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                d.switchOffLed();
+            }
+        });
 
         return d;
     }