OmniGears: notification lights - support for devices without multi-color led

See also http://jira.omnirom.org/browse/UIREVIEW-4

For devices that dont have a multi-color led the color picker
dialog in notification lights makes not much sense

This creates an adapted dialog which supports a list of colors
that can be provided in the device overlay

To enable:
<bool name="config_has_multi_color_led" translatable="false">false</bool>

Default color list:
<string-array name="entries_led_colors" translatable="false">
<item>@string/led_color_green</item>
<item>@string/led_color_red</item>
</string-array>

<string-array name="values_led_colors" translatable="false">
<item>#FF00FF00</item>
<item>#FFFF0000</item>
</string-array>

PS2:
whitespace cleanup

PS3:
remove unneded separator line

Change-Id: I783cfcc42b1753148da31d521e09c134fab4311a
diff --git a/res/layout/dialog_light_settings.xml b/res/layout/dialog_light_settings.xml
index 6e7e01f..52d2bdb 100644
--- a/res/layout/dialog_light_settings.xml
+++ b/res/layout/dialog_light_settings.xml
@@ -54,7 +54,7 @@
     </LinearLayout>
 
     <LinearLayout
-        android:id="@+id/speed_title_view"
+        android:id="@+id/color_list_view"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/color_panel_view"
@@ -63,6 +63,47 @@
         android:layout_marginTop="4dp"
         android:orientation="vertical" >
 
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="4dp"
+            android:text="@string/color_list_title"
+            android:textAppearance="?android:attr/textAppearanceSmall" />
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="horizontal"
+            android:paddingBottom="4dip" >
+
+             <Spinner
+                android:id="@+id/color_list_spinner"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:layout_weight="1" />
+
+             <View
+                android:layout_width="8dip"
+                android:layout_height="match_parent" />
+
+             <org.omnirom.omnigears.notificationlight.ColorPanelView
+                android:id="@+id/color_list_panel"
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_weight="1" />
+        </LinearLayout>
+    </LinearLayout>
+
+    <LinearLayout
+        android:id="@+id/speed_title_view"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_below="@id/color_list_view"
+        android:layout_marginStart="10dp"
+        android:layout_marginEnd="10dp"
+        android:layout_marginTop="4dp"
+        android:orientation="vertical" >
+
         <View
             android:layout_width="match_parent"
             android:layout_height="2dp"
@@ -98,5 +139,4 @@
                 android:layout_weight="1" />
         </LinearLayout>
     </LinearLayout>
-
 </RelativeLayout>
diff --git a/res/layout/led_color_item.xml b/res/layout/led_color_item.xml
new file mode 100644
index 0000000..9052caf
--- /dev/null
+++ b/res/layout/led_color_item.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/textViewName"
+    android:paddingStart="4dp"
+    android:paddingEnd="4dp"
+    android:paddingTop="8dp"
+    android:paddingBottom="8dp"
+    android:textAppearance="?android:attr/textAppearanceMedium" >
+
+</TextView>
diff --git a/res/values/config.xml b/res/values/config.xml
new file mode 100644
index 0000000..33fef4f
--- /dev/null
+++ b/res/values/config.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--  Copyright (C) 2013 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/>.
+ -->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- does the device have multi color led support or only
+         a fixed set of led colors -->
+    <bool name="config_has_multi_color_led" translatable="false">true</bool>
+</resources>
diff --git a/res/values/custom_arrays.xml b/res/values/custom_arrays.xml
index 18eafe1..5e28804 100644
--- a/res/values/custom_arrays.xml
+++ b/res/values/custom_arrays.xml
@@ -137,4 +137,15 @@
         <item>50</item>
         <item>25</item>
     </string-array>
+
+    <!-- led color list -->
+    <string-array name="entries_led_colors" translatable="false">
+        <item>@string/led_color_green</item>
+        <item>@string/led_color_red</item>
+    </string-array>
+
+    <string-array name="values_led_colors" translatable="false">
+        <item>#FF00FF00</item>
+        <item>#FFFF0000</item>
+    </string-array>
 </resources>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index f49bc2e..0c2f4d5 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -96,6 +96,7 @@
     <!-- Notification light dialogs -->
     <string name="edit_light_settings">Edit light settings</string>
     <string name="pulse_speed_title">Pulse length and speed</string>
+    <string name="color_list_title">Color</string>
     <string name="default_time">Normal</string>
     <string name="custom_time">Custom</string>
     <string name="dialog_test">Test</string>
@@ -131,6 +132,9 @@
     <string name="pulse_speed_slow">Slow</string>
     <string name="pulse_speed_very_slow">Very slow</string>
 
+    <string name="led_color_green">Green</string>
+    <string name="led_color_red">Red</string>
+
     <!-- Battery light settings  -->
     <string name="battery_light_title">Battery light</string>
     <string name="battery_low_pulse_title">Pulse if battery low</string>
diff --git a/src/org/omnirom/omnigears/notificationlight/LightSettingsDialog.java b/src/org/omnirom/omnigears/notificationlight/LightSettingsDialog.java
index bda612b..d15f573 100644
--- a/src/org/omnirom/omnigears/notificationlight/LightSettingsDialog.java
+++ b/src/org/omnirom/omnigears/notificationlight/LightSettingsDialog.java
@@ -60,8 +60,12 @@
     private Spinner mPulseSpeedOn;
     private Spinner mPulseSpeedOff;
     private LayoutInflater mInflater;
-
-    private OnColorChangedListener mListener;
+    private boolean mMultiColor = true;
+    private Spinner mColorList;
+    private LinearLayout mColorListView;
+    private LinearLayout mColorPanelView;
+    private ColorPanelView mNewListColor;
+    private LedColorAdapter mLedColorAdapter;
 
     /**
      * @param context
@@ -87,6 +91,7 @@
             int initialSpeedOff, boolean onOffChangeable) {
         super(context);
 
+        mMultiColor = getContext().getResources().getBoolean(R.bool.config_has_multi_color_led);
         init(initialColor, initialSpeedOn, initialSpeedOff, onOffChangeable);
     }
 
@@ -112,6 +117,11 @@
         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);
+        mColorPanelView = (LinearLayout) layout.findViewById(R.id.color_panel_view);
+
+        mColorListView = (LinearLayout) layout.findViewById(R.id.color_list_view);
+        mColorList = (Spinner) layout.findViewById(R.id.color_list_spinner);
+        mNewListColor = (ColorPanelView) layout.findViewById(R.id.color_list_panel);
 
         mColorPicker.setOnColorChangedListener(this);
         mColorPicker.setColor(color, true);
@@ -136,8 +146,27 @@
         mPulseSpeedOn.setEnabled(onOffChangeable);
         mPulseSpeedOff.setEnabled((speedOn != 1) && onOffChangeable);
 
+        mColorList = (Spinner) layout.findViewById(R.id.color_list_spinner);
+        mLedColorAdapter = new LedColorAdapter(
+                R.array.entries_led_colors,
+                R.array.values_led_colors);
+        mColorList.setAdapter(mLedColorAdapter);
+        mColorList.setSelection(mLedColorAdapter.getColorPosition(color));
+        mColorList.setOnItemSelectedListener(mColorListListener);
+
         setView(layout);
         setTitle(R.string.edit_light_settings);
+
+        // show and hide the correct UI depending if we have multi-color led or not
+        if (mMultiColor){
+            mColorListView.setVisibility(View.GONE);
+            mColorPicker.setVisibility(View.VISIBLE);
+            mColorPanelView.setVisibility(View.VISIBLE);
+        } else {
+            mColorListView.setVisibility(View.VISIBLE);
+            mColorPicker.setVisibility(View.GONE);
+            mColorPanelView.setVisibility(View.GONE);
+        }
     }
 
     private AdapterView.OnItemSelectedListener mSelectionListener = new AdapterView.OnItemSelectedListener() {
@@ -152,6 +181,21 @@
         }
     };
 
+    private AdapterView.OnItemSelectedListener mColorListListener = new AdapterView.OnItemSelectedListener() {
+
+        @Override
+        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
+            int color = mLedColorAdapter.getColor(position);
+            if (color != -1){
+                mNewListColor.setColor(color);
+            }
+        }
+
+        @Override
+        public void onNothingSelected(AdapterView<?> parent) {
+        }
+    };
+
     @Override
     public Bundle onSaveInstanceState() {
         Bundle state = super.onSaveInstanceState();
@@ -173,10 +217,6 @@
 
         mNewColor.setColor(color);
         mHexColorInput.setText(String.format(Locale.US, format, color & mask));
-
-        if (mListener != null) {
-            mListener.onColorChanged(color);
-        }
     }
 
     public void setAlphaSliderVisible(boolean visible) {
@@ -185,7 +225,11 @@
     }
 
     public int getColor() {
-        return mColorPicker.getColor();
+        if (mMultiColor){
+            return mColorPicker.getColor();
+        } else {
+            return mNewListColor.getColor();
+        }
     }
 
     @SuppressWarnings("unchecked")
@@ -280,6 +324,75 @@
         }
     }
 
+    class LedColorAdapter extends BaseAdapter implements SpinnerAdapter {
+        private ArrayList<Pair<String, Integer>> mColors;
+
+        public LedColorAdapter(int ledColorResource, int ledValueResource) {
+            mColors = new ArrayList<Pair<String, Integer>>();
+
+            String[] color_names = getContext().getResources().getStringArray(ledColorResource);
+            String[] color_values = getContext().getResources().getStringArray(ledValueResource);
+
+            for(int i = 0; i < color_values.length; ++i) {
+                try {
+                    int color = Color.parseColor(color_values[i]);
+                    mColors.add(new Pair<String, Integer>(color_names[i], color));
+                } catch (IllegalArgumentException ex) {
+                    // Number format is incorrect, ignore entry
+                }
+            }
+        }
+
+        /**
+         * Will return the position of the spinner entry with the specified
+         * color. Returns -1 if there is no such entry.
+         */
+        public int getColorPosition(int color) {
+            for (int position = 0; position < getCount(); ++position) {
+                if (getItem(position).second.equals(color)) {
+                    return position;
+                }
+            }
+
+            return -1;
+        }
+
+        public int getColor(int position) {
+            Pair<String, Integer> item = getItem(position);
+            if (item != null){
+                return item.second;
+            }
+            return -1;
+        }
+
+        @Override
+        public int getCount() {
+            return mColors.size();
+        }
+
+        @Override
+        public Pair<String, Integer> getItem(int position) {
+            return mColors.get(position);
+        }
+
+        @Override
+        public long getItemId(int position) {
+            return position;
+        }
+
+        @Override
+        public View getView(int position, View view, ViewGroup parent) {
+            if (view == null) {
+                view = mInflater.inflate(R.layout.led_color_item, null);
+            }
+
+            Pair<String, Integer> entry = getItem(position);
+            ((TextView) view.findViewById(R.id.textViewName)).setText(entry.first);
+
+            return view;
+        }
+    }
+
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     }
@@ -299,9 +412,6 @@
                 }
                 mColorPicker.setColor(color);
                 mNewColor.setColor(color);
-                if (mListener != null) {
-                    mListener.onColorChanged(color);
-                }
             } catch (IllegalArgumentException ex) {
                 // Number format is incorrect, ignore
             }