summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDirk Rettschlag <dirk.rettschlag@gmail.com>2014-05-26 13:46:10 +0200
committerLorDClockaN <davor@losinj.com>2014-06-19 11:23:25 +0200
commitcc0892202f75a97492babf037b45dedfd5f235e9 (patch)
tree17cabf32883c0cb87c983a3b63df0a5d14c3d3d2
parenta5cdbd1ca4bc9b1473b25a6b049dbe2fc4dd3c9b (diff)
add MultiChoiceSetting
This will add a new settings widget that allows to select multiple values. It also implements methods to define min and max count of selectable values. use as follows: <com.aokp.romcontrol.settings.MultiChoiceSetting android:key="romcontrol_test_setting" android:layout_width="match_parent" android:layout_height="wrap_content" android:dialogTitle="@string/romcontrol_test_title" android:title="@string/romcontrol_test_title" android:entries="@array/romcontrol_test_entries" android:entryValues="@array/romcontrol_test_values" android:defaultValue="0|1" aokp:minSelectionCount="2" aokp:maxSelectionCount="4" /> Change-Id: I391b5b3e1da9e29ccf8abbc96fd670f448a25a98 Signed-off-by: Dirk Rettschlag <dirk.rettschlag@gmail.com>
-rw-r--r--res/values/attrs.xml5
-rw-r--r--res/values/strings.xml12
-rw-r--r--src/com/aokp/romcontrol/settings/BaseSetting.java4
-rw-r--r--src/com/aokp/romcontrol/settings/MultiChoiceSetting.java236
4 files changed, 256 insertions, 1 deletions
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index f88ce42..d542200 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -43,6 +43,11 @@
<attr name="colorPickerShowAlphaSlider" format="boolean"/>
</declare-styleable>
+ <declare-styleable name="MultiChoiceSetting">
+ <attr name="minSelectionCount" format="integer"/>
+ <attr name="maxSelectionCount" format="integer"/>
+ </declare-styleable>
+
<declare-styleable name="PagerSlidingTabStrip">
<attr name="indicatorColor" format="color" />
<attr name="underlineColor" format="color" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a8e4028..e7ca43c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -17,7 +17,7 @@
*/
-->
-<resources>
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">ROM control</string>
<string name="rom_control">ROM control</string>
@@ -42,6 +42,16 @@
<!-- Menu Items -->
<string name="menu_show_drawer_icon">Show in app drawer</string>
+ <!-- MultiChoiceSetting -->
+ <plurals name="multichoice_at_least">
+ <item quantity="one">Please select at least <xliff:g id="number">%d</xliff:g> item.</item>
+ <item quantity="other">Please select at least <xliff:g id="number">%d</xliff:g> items.</item>
+ </plurals>
+ <plurals name="multichoice_at_most">
+ <item quantity="one">Please select at most <xliff:g id="number">%d</xliff:g> item.</item>
+ <item quantity="other">Please select at most <xliff:g id="number">%d</xliff:g> items.</item>
+ </plurals>
+
<!-- About -->
<string name="devs">The Crew</string>
<string name="aokp" translatable="false">Android Open Kang Project</string>
diff --git a/src/com/aokp/romcontrol/settings/BaseSetting.java b/src/com/aokp/romcontrol/settings/BaseSetting.java
index 993b53c..f8fa039 100644
--- a/src/com/aokp/romcontrol/settings/BaseSetting.java
+++ b/src/com/aokp/romcontrol/settings/BaseSetting.java
@@ -238,6 +238,10 @@ public class BaseSetting extends LinearLayout {
aKey = key;
}
+ public void setDefaultValue(String defaultValue) {
+ aDefaultValue = defaultValue;
+ }
+
/**
* @return returns the supplied default value. null if none was provided.
*/
diff --git a/src/com/aokp/romcontrol/settings/MultiChoiceSetting.java b/src/com/aokp/romcontrol/settings/MultiChoiceSetting.java
new file mode 100644
index 0000000..cab419a
--- /dev/null
+++ b/src/com/aokp/romcontrol/settings/MultiChoiceSetting.java
@@ -0,0 +1,236 @@
+package com.aokp.romcontrol.settings;
+
+import android.app.AlertDialog;
+import android.app.AlertDialog.Builder;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.res.Resources;
+import android.content.res.TypedArray;
+import android.text.TextUtils;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.KeyEvent;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.Toast;
+
+import java.util.ArrayList;
+import java.util.Collections;
+
+import com.aokp.romcontrol.R;
+
+/**
+ * Setting toggle which allows choosing multiple items
+ * <p/>
+ * <ul><b>Supported attributes (in addition to {@link BaseSetting} attributes)</b>
+ * <li>android:entryValues
+ * <li>android:entryValueEntries
+ * </ul>
+ */
+public class MultiChoiceSetting extends BaseSetting implements OnClickListener {
+
+ private String[] mEntries;
+ private String[] mValues;
+ private int mMinSelectionCount = 0;
+ private int mMaxSelectionCount = 0;
+ private ArrayList<String> mAvailableValues = new ArrayList<String>();
+ private ArrayList<String> mSelectedValues = new ArrayList<String>();
+
+ public MultiChoiceSetting(Context context) {
+ this(context, null);
+ }
+
+ public MultiChoiceSetting(Context context, AttributeSet attrs) {
+ this(context, attrs, 0);
+ }
+
+ public MultiChoiceSetting(Context context, AttributeSet attrs, int defStyle) {
+ super(context, attrs, defStyle);
+
+ if (attrs != null) {
+ TypedArray typedArray = null;
+
+ try {
+ int[] attrsMultiChoiceSetting = new int[]{
+ android.R.attr.entries,
+ android.R.attr.entryValues
+ };
+ typedArray = context.obtainStyledAttributes(attrs, attrsMultiChoiceSetting);
+ int entriesRes = typedArray.getResourceId(0, 0);
+ if (entriesRes > 0) {
+ mEntries = getResources().getStringArray(entriesRes);
+ } else {
+ mEntries = new String[0];
+ }
+
+ int valuesRes = typedArray.getResourceId(1, 0);
+ if (valuesRes > 0) {
+ mValues = getResources().getStringArray(valuesRes);
+
+ } else {
+ mValues = new String[0];
+ }
+
+ } finally {
+ if (typedArray != null) {
+ typedArray.recycle();
+ }
+ }
+
+ try {
+ typedArray = context.obtainStyledAttributes(attrs, R.styleable.MultiChoiceSetting);
+
+ mMinSelectionCount = typedArray.getInteger(0, mMinSelectionCount);
+ mMaxSelectionCount = typedArray.getInteger(1, mMaxSelectionCount);
+
+ } finally {
+ if (typedArray != null) {
+ typedArray.recycle();
+ }
+ }
+ }
+ addView(View.inflate(context, R.layout.setting_colorpicker, mRootView));
+
+ setOnClickListener(this);
+ setFocusable(true);
+ }
+
+ @Override
+ public void onClick(View v) {
+
+ final AlertDialog d = new Builder(getContext())
+ .setTitle(getTitle())
+ .setCancelable(true)
+ .setPositiveButton(R.string.toggles_display_close, null)
+ .setOnKeyListener(new DialogInterface.OnKeyListener() {
+
+ @Override
+ public boolean onKey(DialogInterface dialog, int keyCode,
+ KeyEvent event) {
+ if (keyCode == KeyEvent.KEYCODE_BACK) {
+ if (checkSelectionCount()) {
+ dialog.dismiss();
+ }
+ return true;
+ }
+ return false;
+ }
+ })
+ .setMultiChoiceItems(mEntries, getCheckedValues(),
+ new DialogInterface.OnMultiChoiceClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which, boolean isChecked) {
+ String selectedValue = mAvailableValues.get(which);
+ if (isChecked) {
+ mSelectedValues.add(selectedValue);
+ } else {
+ mSelectedValues.remove(selectedValue);
+ }
+ setValue(TextUtils.join("|",mSelectedValues));
+ }
+ })
+ .create();
+
+ d.setOnShowListener(new DialogInterface.OnShowListener() {
+
+ @Override
+ public void onShow(DialogInterface dialog) {
+
+ Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
+ b.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View view) {
+ if (checkSelectionCount()) {
+ d.dismiss();
+ }
+ }
+ });
+ }
+ });
+
+ d.show();
+
+ }
+
+ public String[] getEntries() {
+ return mEntries;
+ }
+
+ public void setEntries(String[] entries) {
+ mEntries = entries;
+ }
+
+ public void setEntries(int entriesResId) {
+ setEntries(getContext().getResources().getStringArray(entriesResId));
+ }
+
+ public String[] getEntryValues() {
+ return mValues;
+ }
+
+ public void setEntryValues(String[] entryValues) {
+ mValues = entryValues;
+ }
+
+ public void setEntryValues(int entryValuesResId) {
+ setEntryValues(getContext().getResources().getStringArray(entryValuesResId));
+ }
+
+ private boolean[] getCheckedValues() {
+ boolean checkedValues[] = new boolean[mValues.length];
+
+ mSelectedValues.clear();
+ mAvailableValues.clear();
+
+ Collections.addAll(mAvailableValues, mValues);
+
+ String values = getValue();
+
+ if (TextUtils.isEmpty(values)) {
+ values = getDefaultValue();
+ }
+
+ if (!TextUtils.isEmpty(values)) {
+ String[] split = TextUtils.split(values, "\\|");
+
+ Collections.addAll(mSelectedValues, split);
+
+ for (int i = 0; i < checkedValues.length; i++) {
+ String selectedValue = mAvailableValues.get(i);
+ if (mSelectedValues.contains(selectedValue)) {
+ checkedValues[i] = true;
+ }
+ }
+ }
+
+ return checkedValues;
+ }
+
+ private boolean checkSelectionCount() {
+ Context context = getContext();
+ Resources res = getResources();
+
+ if (mMaxSelectionCount > 0 && mSelectedValues.size() > mMaxSelectionCount) {
+ Toast toast = Toast.makeText(context,
+ res.getQuantityString(R.plurals.multichoice_at_most, mMaxSelectionCount,
+ mMaxSelectionCount),
+ Toast.LENGTH_SHORT);
+ toast.show();
+ return false;
+ } else if (mMinSelectionCount > 0 && mSelectedValues.size() < mMinSelectionCount) {
+ Toast toast = Toast.makeText(context,
+ res.getQuantityString(R.plurals.multichoice_at_least, mMinSelectionCount,
+ mMinSelectionCount),
+ Toast.LENGTH_SHORT);
+ toast.show();
+ return false;
+ }
+
+ return true;
+ }
+
+ public void updateSummary(String summary) {
+ setSummary(summary);
+ }
+}