| 1 | package net.mandaria.tippytipper.preferences; |
| 2 | |
| 3 | /* The following code was written by Matthew Wiggins |
| 4 | * and is released under the APACHE 2.0 license |
| 5 | * |
| 6 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | */ |
| 8 | |
| 9 | import net.mandaria.tippytipper.*; |
| 10 | import android.content.Context; |
| 11 | import android.util.AttributeSet; |
| 12 | import android.view.Gravity; |
| 13 | import android.view.View; |
| 14 | import android.view.ViewGroup.LayoutParams; |
| 15 | import android.preference.DialogPreference; |
| 16 | import android.widget.SeekBar; |
| 17 | import android.widget.TextView; |
| 18 | import android.widget.LinearLayout; |
| 19 | import android.content.res.*; |
| 20 | |
| 21 | public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener |
| 22 | { |
| 23 | private static final String androidns="http://schemas.android.com/apk/res/android"; |
| 24 | private static final String appns="http://schemas.android.com/apk/res/net.mandaria.tippytipper"; |
| 25 | |
| 26 | private SeekBar mSeekBar; |
| 27 | private TextView mSplashText,mValueText; |
| 28 | private Context mContext; |
| 29 | |
| 30 | private String mDialogMessage, mSuffix; |
| 31 | private int mDefault, mMax, mMin, mValue = 0; |
| 32 | |
| 33 | public SeekBarPreference(Context context, AttributeSet attrs) { |
| 34 | super(context,attrs); |
| 35 | mContext = context; |
| 36 | |
| 37 | mDialogMessage = attrs.getAttributeValue(androidns,"dialogMessage"); |
| 38 | mSuffix = attrs.getAttributeValue(androidns,"text"); |
| 39 | mDefault = attrs.getAttributeIntValue(androidns,"defaultValue", 0); |
| 40 | mMax = attrs.getAttributeIntValue(androidns,"max", 100); |
| 41 | |
| 42 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekBarPreference); |
| 43 | mMin = a.getInt(R.styleable.SeekBarPreference_min, 0); |
| 44 | mMax = mMax - mMin; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected View onCreateDialogView() { |
| 49 | LinearLayout.LayoutParams params; |
| 50 | LinearLayout layout = new LinearLayout(mContext); |
| 51 | layout.setOrientation(LinearLayout.VERTICAL); |
| 52 | layout.setPadding(6,6,6,6); |
| 53 | |
| 54 | mSplashText = new TextView(mContext); |
| 55 | if (mDialogMessage != null) |
| 56 | mSplashText.setText(mDialogMessage); |
| 57 | layout.addView(mSplashText); |
| 58 | |
| 59 | mValueText = new TextView(mContext); |
| 60 | mValueText.setGravity(Gravity.CENTER_HORIZONTAL); |
| 61 | mValueText.setTextSize(32); |
| 62 | params = new LinearLayout.LayoutParams( |
| 63 | LayoutParams.FILL_PARENT, |
| 64 | LayoutParams.WRAP_CONTENT); |
| 65 | layout.addView(mValueText, params); |
| 66 | |
| 67 | mSeekBar = new SeekBar(mContext); |
| 68 | mSeekBar.setOnSeekBarChangeListener(this); |
| 69 | layout.addView(mSeekBar, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); |
| 70 | |
| 71 | if (shouldPersist()) |
| 72 | mValue = getPersistedInt(mDefault); |
| 73 | |
| 74 | mSeekBar.setMax(mMax); |
| 75 | mSeekBar.setProgress(mValue); |
| 76 | return layout; |
| 77 | } |
| 78 | @Override |
| 79 | protected void onBindDialogView(View v) { |
| 80 | super.onBindDialogView(v); |
| 81 | mSeekBar.setMax(mMax); |
| 82 | mSeekBar.setProgress(mValue - mMin); |
| 83 | } |
| 84 | @Override |
| 85 | protected void onSetInitialValue(boolean restore, Object defaultValue) |
| 86 | { |
| 87 | super.onSetInitialValue(restore, defaultValue); |
| 88 | if (restore) |
| 89 | { |
| 90 | try |
| 91 | { |
| 92 | mValue = shouldPersist() ? getPersistedInt(mDefault) : 0; |
| 93 | } |
| 94 | catch(Exception ex) |
| 95 | { |
| 96 | mValue = mDefault; |
| 97 | } |
| 98 | } |
| 99 | else |
| 100 | mValue = (Integer)defaultValue; |
| 101 | } |
| 102 | |
| 103 | public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) |
| 104 | { |
| 105 | String t = String.valueOf(value + mMin); |
| 106 | mValueText.setText(mSuffix == null ? t : t.concat(mSuffix)); |
| 107 | if (shouldPersist()) |
| 108 | persistInt(value + mMin); |
| 109 | callChangeListener(new Integer(value + mMin)); |
| 110 | } |
| 111 | public void onStartTrackingTouch(SeekBar seek) {} |
| 112 | public void onStopTrackingTouch(SeekBar seek) {} |
| 113 | |
| 114 | public void setMax(int max) { mMax = max; } |
| 115 | public int getMax() { return mMax; } |
| 116 | |
| 117 | public void setMin(int min) {mMin = min;} |
| 118 | public int getMin() { return mMin;} |
| 119 | |
| 120 | public void setProgress(int progress) { |
| 121 | mValue = progress; |
| 122 | if (mSeekBar != null) |
| 123 | mSeekBar.setProgress(progress); |
| 124 | } |
| 125 | public int getProgress() { return mValue; } |
| 126 | } |