| 1 | package net.mandaria.tippytipper.preferences; |
| 2 | |
| 3 | import net.mandaria.tippytipper.R; |
| 4 | import net.mandaria.tippytipper.widgets.*; |
| 5 | import android.content.Context; |
| 6 | import android.util.AttributeSet; |
| 7 | import android.view.Gravity; |
| 8 | import android.view.View; |
| 9 | import android.preference.DialogPreference; |
| 10 | import android.widget.TableLayout; |
| 11 | import android.widget.TableRow; |
| 12 | import android.widget.TextView; |
| 13 | import android.content.res.*; |
| 14 | |
| 15 | public class NumberPickerPreference extends DialogPreference |
| 16 | { |
| 17 | private static final String androidns = "http://schemas.android.com/apk/res/android"; |
| 18 | private static final String appns = "http://schemas.android.com/apk/res/net.mandaria.tippytipper"; |
| 19 | |
| 20 | private NumberPicker mPickInteger; |
| 21 | private TextView mSplashText, mValueText; |
| 22 | private Context mContext; |
| 23 | |
| 24 | private String mDialogMessage, mSuffix; |
| 25 | private int mDefault, mMin, mMax, mValue = 0; |
| 26 | |
| 27 | public NumberPickerPreference(Context context, AttributeSet attrs) |
| 28 | { |
| 29 | super(context, attrs); |
| 30 | mContext = context; |
| 31 | |
| 32 | mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage"); |
| 33 | mSuffix = attrs.getAttributeValue(androidns, "text"); |
| 34 | mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0); |
| 35 | mMax = attrs.getAttributeIntValue(androidns,"max", 100); |
| 36 | |
| 37 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference); |
| 38 | mMin = a.getInt(R.styleable.SeekBarPreference_min, 0); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected View onCreateDialogView() |
| 43 | { |
| 44 | TableLayout.LayoutParams params; |
| 45 | TableLayout layout = new TableLayout(mContext); |
| 46 | layout.setPadding(6, 6, 6, 6); |
| 47 | |
| 48 | mSplashText = new TextView(mContext); |
| 49 | if (mDialogMessage != null) |
| 50 | mSplashText.setText(mDialogMessage); |
| 51 | |
| 52 | TableRow row_header = new TableRow(mContext); |
| 53 | row_header.addView(mSplashText); |
| 54 | |
| 55 | mPickInteger = new NumberPicker(mContext); |
| 56 | mPickInteger.setRange(mMin, mMax); |
| 57 | |
| 58 | TextView suffix = new TextView(mContext); |
| 59 | suffix.setText(mSuffix); |
| 60 | suffix.setTextSize(32); |
| 61 | |
| 62 | TableRow row_one = new TableRow(mContext); |
| 63 | row_one.setGravity(Gravity.CENTER); |
| 64 | row_one.addView(mPickInteger); |
| 65 | row_one.addView(suffix); |
| 66 | |
| 67 | layout.addView(row_header); |
| 68 | |
| 69 | TableLayout table_main = new TableLayout(mContext); |
| 70 | table_main.addView(row_one); |
| 71 | |
| 72 | TableRow row_main = new TableRow(mContext); |
| 73 | row_main.setGravity(Gravity.CENTER_HORIZONTAL); |
| 74 | row_main.addView(table_main); |
| 75 | |
| 76 | layout.addView(row_main); |
| 77 | |
| 78 | if (shouldPersist()) |
| 79 | mValue = getPersistedInt(mDefault); |
| 80 | |
| 81 | bindData(); |
| 82 | |
| 83 | return layout; |
| 84 | } |
| 85 | |
| 86 | private void bindData() |
| 87 | { |
| 88 | try |
| 89 | { |
| 90 | mPickInteger.setCurrent(mValue); |
| 91 | } |
| 92 | catch (Exception ex) |
| 93 | { |
| 94 | |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void onBindDialogView(View v) |
| 100 | { |
| 101 | super.onBindDialogView(v); |
| 102 | bindData(); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected void onSetInitialValue(boolean restore, Object defaultValue) |
| 107 | { |
| 108 | super.onSetInitialValue(restore, defaultValue); |
| 109 | if (restore) |
| 110 | { |
| 111 | try |
| 112 | { |
| 113 | mValue = shouldPersist() ? getPersistedInt(mDefault) : 0; |
| 114 | } |
| 115 | catch (Exception ex) |
| 116 | { |
| 117 | mValue = mDefault; |
| 118 | } |
| 119 | } |
| 120 | else |
| 121 | mValue = (Integer) defaultValue; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | protected void onDialogClosed(boolean positiveResult) |
| 126 | { |
| 127 | if (positiveResult == true) |
| 128 | { |
| 129 | super.onDialogClosed(positiveResult); |
| 130 | // HACK: "click" both picker inputs to validate inputs before closing the dialog |
| 131 | // this is to fix a problem of closing the dialog not causing the onFocusChange of the picker |
| 132 | // to be called |
| 133 | mPickInteger.onClick(null); |
| 134 | mValue = mPickInteger.getCurrent(); |
| 135 | if (shouldPersist()) |
| 136 | persistInt(mValue); |
| 137 | } |
| 138 | } |
| 139 | } |