[3/3] OmniLib: fix OmniLib attrs

Change-Id: I20e04ce0d7c1b3841e7602c61ab4cda272e5cc03
diff --git a/res/values/custom_attrs.xml b/res/values/custom_attrs.xml
index cd815f4..d8e3c59 100644
--- a/res/values/custom_attrs.xml
+++ b/res/values/custom_attrs.xml
@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
+
 <!--
 /*
 ** Copyright 2013, The ChameleonOS Open Source Project
@@ -16,8 +17,8 @@
 ** limitations under the License.
 */
 -->
-<resources>
 
+<resources>
     <declare-styleable name="SeekBarPreference">
         <attr name="min" format="integer" />
         <attr name="interval" format="integer" />
@@ -27,5 +28,6 @@
 
     <declare-styleable name="ColorSelectPreference">
         <attr name="ledPreview" format="boolean" />
+        <attr name="withAlpha" format="boolean" />
     </declare-styleable>
 </resources>
diff --git a/src/org/omnirom/omnilib/preference/ColorSelectDialog.java b/src/org/omnirom/omnilib/preference/ColorSelectDialog.java
index f029ce8..1decfb3 100644
--- a/src/org/omnirom/omnilib/preference/ColorSelectDialog.java
+++ b/src/org/omnirom/omnilib/preference/ColorSelectDialog.java
@@ -75,11 +75,11 @@
     private NotificationManager mNoMan;
     private Context mContext;
 
-    protected ColorSelectDialog(Context context, int initialColor, boolean showLedPreview) {
+    protected ColorSelectDialog(Context context, int initialColor, boolean showLedPreview, boolean withAlpha) {
         super(context);
         mContext = context;
         mShowLedPreview = showLedPreview;
-        mWithAlpha = false;
+        mWithAlpha = withAlpha;
         mMultiColor = getContext().getResources().getBoolean(R.bool.config_has_multi_color_led);
         init(initialColor);
     }
diff --git a/src/org/omnirom/omnilib/preference/ColorSelectPreference.java b/src/org/omnirom/omnilib/preference/ColorSelectPreference.java
index 4592873..755ff44 100644
--- a/src/org/omnirom/omnilib/preference/ColorSelectPreference.java
+++ b/src/org/omnirom/omnilib/preference/ColorSelectPreference.java
@@ -21,11 +21,13 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.res.Resources;
+import android.content.res.TypedArray;
 import android.graphics.drawable.ShapeDrawable;
 import android.graphics.drawable.shapes.RectShape;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceViewHolder;
 import android.util.AttributeSet;
+import android.util.TypedValue;
 import android.widget.ImageView;
 
 import org.omnirom.omnilib.R;
@@ -41,6 +43,7 @@
     private Dialog mDialog;
 
     private boolean mShowLedPreview;
+    private boolean mWithAlpha;
 
     /**
      * @param context
@@ -67,7 +70,22 @@
     private void init(Context context, AttributeSet attrs) {
         setLayoutResource(R.layout.preference_color_select);
         mResources = getContext().getResources();
-        mShowLedPreview = attrs.getAttributeBooleanValue(null, "ledPreview", false);
+        if (attrs != null) {
+            final TypedArray attributes = context.obtainStyledAttributes(attrs,
+                    R.styleable.ColorSelectPreference);
+            final TypedValue useWithAlpha = attributes.peekValue(
+                    R.styleable.ColorSelectPreference_withAlpha);
+            if (useWithAlpha != null) {
+                mWithAlpha = (useWithAlpha.type == TypedValue.TYPE_INT_BOOLEAN
+                                && useWithAlpha.data != 0);
+            }
+            final TypedValue useLedPreview = attributes.peekValue(
+                    R.styleable.ColorSelectPreference_ledPreview);
+            if (useLedPreview != null) {
+                mShowLedPreview = (useLedPreview.type == TypedValue.TYPE_INT_BOOLEAN
+                                && useLedPreview.data != 0);
+            }
+        }
     }
 
     public void setColor(int color) {
@@ -108,7 +126,7 @@
 
     public Dialog getDialog() {
         final ColorSelectDialog d = new ColorSelectDialog(getContext(),
-                0xFF000000 | mColorValue, mShowLedPreview);
+                0xFF000000 | mColorValue, mShowLedPreview, mWithAlpha);
 
         d.setButton(AlertDialog.BUTTON_POSITIVE, mResources.getString(R.string.ok),
                 new DialogInterface.OnClickListener() {
@@ -147,4 +165,12 @@
     public void onDismiss(DialogInterface dialog) {
         mDialog = null;
     }
+
+    public void setWithAlpha(boolean value) {
+        mWithAlpha = value;
+    }
+
+    public void setWithLedPreview(boolean value) {
+        mShowLedPreview = value;
+    }
 }
diff --git a/src/org/omnirom/omnilib/preference/SeekBarPreference.java b/src/org/omnirom/omnilib/preference/SeekBarPreference.java
index bf82c79..488d31a 100644
--- a/src/org/omnirom/omnilib/preference/SeekBarPreference.java
+++ b/src/org/omnirom/omnilib/preference/SeekBarPreference.java
@@ -1,23 +1,24 @@
 /*
-** Copyright 2013, The ChameleonOS Open Source Project
-** Copyright 2016, The OmniROM 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.
-*/
+ ** Copyright 2013, The ChameleonOS Open Source Project
+ ** Copyright 2016, The OmniROM 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.omnilib.preference;
 
 import android.content.Context;
 import android.content.res.TypedArray;
+import android.util.TypedValue;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceViewHolder;
 import android.util.AttributeSet;
@@ -29,6 +30,7 @@
 import android.widget.SeekBar;
 import android.widget.SeekBar.OnSeekBarChangeListener;
 import android.widget.TextView;
+
 import org.omnirom.omnilib.R;
 
 public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
@@ -36,14 +38,13 @@
     private final String TAG = getClass().getName();
 
     private static final String ANDROIDNS = "http://schemas.android.com/apk/res/android";
-    private static final String SETTINGS = "http://schemas.android.com/apk/lib/res/org.omnirom.omnilib";
     private static final int DEFAULT_VALUE = 50;
 
-    private int mMaxValue      = 100;
-    private int mMinValue      = 0;
-    private int mInterval      = 1;
+    private int mMaxValue = 100;
+    private int mMinValue = 0;
+    private int mInterval = 1;
     private int mCurrentValue;
-    private String mUnitsLeft  = "";
+    private String mUnitsLeft = "";
     private String mUnitsRight = "";
     private SeekBar mSeekBar;
     private TextView mStatusText;
@@ -65,25 +66,47 @@
 
     private void setValuesFromXml(Context context, AttributeSet attrs) {
         mMaxValue = attrs.getAttributeIntValue(ANDROIDNS, "max", 100);
-        mMinValue = attrs.getAttributeIntValue(SETTINGS, "min", 0);
 
-        Integer id = attrs.getAttributeResourceValue(SETTINGS, "unitsRight", 0);
-        if (id > 0) {
-            mUnitsRight = context.getResources().getString(id);
+        final TypedArray attributes = context.obtainStyledAttributes(attrs,
+                R.styleable.SeekBarPreference);
+
+        TypedValue minAttr =
+                attributes.peekValue(R.styleable.SeekBarPreference_min);
+        if (minAttr != null && minAttr.type == TypedValue.TYPE_INT_DEC) {
+            mMinValue = minAttr.data;
         }
 
-        id = attrs.getAttributeResourceValue(SETTINGS, "unitsLeft", 0);
-        if (id > 0) {
-            mUnitsLeft = context.getResources().getString(id);
+        TypedValue unitsLeftAttr =
+                attributes.peekValue(R.styleable.SeekBarPreference_unitsLeft);
+        CharSequence data = null;
+        if (unitsLeftAttr != null && unitsLeftAttr.type == TypedValue.TYPE_STRING) {
+            if (unitsLeftAttr.resourceId != 0) {
+                data = context.getText(unitsLeftAttr.resourceId);
+            } else {
+                data = unitsLeftAttr.string;
+            }
+        }
+        mUnitsLeft = (data == null) ? "" : data.toString();
+
+        TypedValue unitsRightAttr =
+                attributes.peekValue(R.styleable.SeekBarPreference_unitsRight);
+        data = null;
+        if (unitsRightAttr != null && unitsRightAttr.type == TypedValue.TYPE_STRING) {
+            if (unitsRightAttr.resourceId != 0) {
+                data = context.getText(unitsRightAttr.resourceId);
+            } else {
+                data = unitsRightAttr.string;
+            }
+        }
+        mUnitsRight = (data == null) ? "" : data.toString();
+
+        TypedValue intervalAttr =
+                attributes.peekValue(R.styleable.SeekBarPreference_interval);
+        if (intervalAttr != null && intervalAttr.type == TypedValue.TYPE_INT_DEC) {
+            mInterval = intervalAttr.data;
         }
 
-        try {
-            String newInterval = attrs.getAttributeValue(SETTINGS, "interval");
-            if(newInterval != null)
-                mInterval = Integer.parseInt(newInterval);
-        } catch(Exception e) {
-            Log.e(TAG, "Invalid interval value", e);
-        }
+        attributes.recycle();
     }
 
     @Override
@@ -105,13 +128,13 @@
         mSeekBar.setOnSeekBarChangeListener(this);
         mSeekBar.setEnabled(isEnabled());
 
-        mStatusText = (TextView)holder.findViewById(R.id.seekBarPrefValue);
+        mStatusText = (TextView) holder.findViewById(R.id.seekBarPrefValue);
         mStatusText.setText(String.valueOf(mCurrentValue));
         mStatusText.setMinimumWidth(30);
 
-        TextView unitsRight = (TextView)holder.findViewById(R.id.seekBarPrefUnitsRight);
+        TextView unitsRight = (TextView) holder.findViewById(R.id.seekBarPrefUnitsRight);
         unitsRight.setText(mUnitsRight);
-        TextView unitsLeft = (TextView)holder.findViewById(R.id.seekBarPrefUnitsLeft);
+        TextView unitsLeft = (TextView) holder.findViewById(R.id.seekBarPrefUnitsLeft);
         unitsLeft.setText(mUnitsLeft);
     }
 
@@ -119,12 +142,12 @@
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
         int newValue = progress + mMinValue;
 
-        if(newValue > mMaxValue) {
+        if (newValue > mMaxValue) {
             newValue = mMaxValue;
-        } else if(newValue < mMinValue) {
+        } else if (newValue < mMinValue) {
             newValue = mMinValue;
-        } else if(mInterval != 1 && newValue % mInterval != 0) {
-            newValue = Math.round(((float)newValue)/mInterval)*mInterval;
+        } else if (mInterval != 1 && newValue % mInterval != 0) {
+            newValue = Math.round(((float) newValue) / mInterval) * mInterval;
         }
 
         // change rejected, revert to the previous value
@@ -140,7 +163,8 @@
     }
 
     @Override
-    public void onStartTrackingTouch(SeekBar seekBar) {}
+    public void onStartTrackingTouch(SeekBar seekBar) {
+    }
 
     @Override
     public void onStopTrackingTouch(SeekBar seekBar) {
@@ -148,7 +172,7 @@
     }
 
     @Override
-    protected Object onGetDefaultValue(TypedArray ta, int index){
+    protected Object onGetDefaultValue(TypedArray ta, int index) {
         int defaultValue = ta.getInt(index, DEFAULT_VALUE);
         return defaultValue;
     }
@@ -160,8 +184,8 @@
         } else {
             int temp = 0;
             try {
-                temp = (Integer)defaultValue;
-            } catch(Exception ex) {
+                temp = (Integer) defaultValue;
+            } catch (Exception ex) {
                 Log.e(TAG, "Invalid default value: " + defaultValue.toString());
             }
             persistInt(temp);
@@ -188,7 +212,7 @@
     }
 
     @Override
-    public void setEnabled (boolean enabled) {
+    public void setEnabled(boolean enabled) {
         if (mSeekBar != null) {
             mSeekBar.setEnabled(enabled);
         }