OmniGears: add NumberPickerPreference

Change-Id: I53a28a3680c5fc19a1fdb2055945278e64a70cff
diff --git a/res/layout/preference_number_picker_dialog.xml b/res/layout/preference_number_picker_dialog.xml
new file mode 100644
index 0000000..6dac6dc
--- /dev/null
+++ b/res/layout/preference_number_picker_dialog.xml
@@ -0,0 +1,14 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical" >
+
+    <NumberPicker
+        android:id="@+id/number_picker"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="6dip"
+        android:layout_marginTop="6dip" />
+
+</LinearLayout>
diff --git a/src/org/omnirom/omnigears/preference/NumberPickerPreference.java b/src/org/omnirom/omnigears/preference/NumberPickerPreference.java
new file mode 100644
index 0000000..5afa3b8
--- /dev/null
+++ b/src/org/omnirom/omnigears/preference/NumberPickerPreference.java
@@ -0,0 +1,117 @@
+/*
+ * Based on: http://www.lukehorvat.com/blog/android-numberpickerdialogpreference/
+ * Thanks to the original author!
+ */
+
+package org.omnirom.omnigears.preference;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.preference.DialogPreference;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.NumberPicker;
+import android.widget.TextView;
+
+import com.android.settings.R;
+/**
+* A {@link DialogPreference} that provides a user with the means to select an integer from a {@link NumberPicker}, and persist it.
+*
+* @author lukehorvat
+*
+*/
+public class NumberPickerPreference extends DialogPreference
+{
+    private static final int DEFAULT_MIN_VALUE = 0;
+    private static final int DEFAULT_MAX_VALUE = 100;
+    private static final int DEFAULT_VALUE = 0;
+
+    private int mMinValue;
+    private int mMaxValue;
+    private int mValue;
+    private NumberPicker mNumberPicker;
+
+    public NumberPickerPreference(Context context) {
+        this(context, null);
+    }
+
+    public NumberPickerPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+
+        setMinValue(DEFAULT_MIN_VALUE);
+        setMaxValue(DEFAULT_MAX_VALUE);
+
+        // set layout
+        setDialogLayoutResource(R.layout.preference_number_picker_dialog);
+        setPositiveButtonText(android.R.string.ok);
+        setNegativeButtonText(android.R.string.cancel);
+        setDialogIcon(null);
+    }
+
+    @Override
+    protected void onSetInitialValue(boolean restore, Object defaultValue) {
+        setValue(restore ? getPersistedInt(DEFAULT_VALUE) : (Integer) defaultValue);
+    }
+
+    @Override
+    protected Object onGetDefaultValue(TypedArray a, int index) {
+        return a.getInt(index, DEFAULT_VALUE);
+    }
+
+    @Override
+    protected void onBindDialogView(View view) {
+        super.onBindDialogView(view);
+
+        mNumberPicker = (NumberPicker) view.findViewById(R.id.number_picker);
+        mNumberPicker.setMinValue(mMinValue);
+        mNumberPicker.setMaxValue(mMaxValue);
+        mNumberPicker.setValue(mValue);
+    }
+
+    public int getMinValue() {
+        return mMinValue;
+    }
+
+    public void setMinValue(int minValue) {
+        mMinValue = minValue;
+        setValue(Math.max(mValue, mMinValue));
+    }
+
+    public int getMaxValue() {
+        return mMaxValue;
+    }
+
+    public void setMaxValue(int maxValue) {
+        mMaxValue = maxValue;
+        setValue(Math.min(mValue, mMaxValue));
+    }
+
+    public int getValue() {
+        return mValue;
+    }
+
+    public void setValue(int value) {
+        value = Math.max(Math.min(value, mMaxValue), mMinValue);
+
+        if (value != mValue) {
+            mValue = value;
+            persistInt(value);
+            notifyChanged();
+        }
+    }
+
+    @Override
+    protected void onDialogClosed(boolean positiveResult) {
+        super.onDialogClosed(positiveResult);
+
+        // when the user selects "OK", persist the new value
+        if (positiveResult) {
+            int numberPickerValue = mNumberPicker.getValue();
+            if (callChangeListener(numberPickerValue)) {
+                setValue(numberPickerValue);
+            }
+        }
+    }
+}