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