| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package com.reecedunn.espeak.preference; |
| 19 |
|
| 20 |
import android.content.Context; |
| 21 |
import android.content.DialogInterface; |
| 22 |
import android.content.SharedPreferences; |
| 23 |
import android.os.Build; |
| 24 |
import android.preference.DialogPreference; |
| 25 |
import android.preference.PreferenceManager; |
| 26 |
import android.util.AttributeSet; |
| 27 |
import android.view.View; |
| 28 |
import android.widget.Button; |
| 29 |
import android.widget.SeekBar; |
| 30 |
import android.widget.TextView; |
| 31 |
|
| 32 |
import com.reecedunn.espeak.R; |
| 33 |
|
| 34 |
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener |
| 35 |
{ |
| 36 |
private SeekBar mSeekBar; |
| 37 |
private TextView mValueText; |
| 38 |
|
| 39 |
private int mOldProgress = 0; |
| 40 |
private int mProgress = 0; |
| 41 |
private int mDefaultValue = 0; |
| 42 |
private int mMin = 0; |
| 43 |
private int mMax = 100; |
| 44 |
private String mFormatter = "%s"; |
| 45 |
|
| 46 |
public void setProgress(int progress) { |
| 47 |
mProgress = progress; |
| 48 |
String text = Integer.toString(mProgress); |
| 49 |
callChangeListener(text); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
mOldProgress = mProgress; |
| 57 |
} |
| 58 |
|
| 59 |
public int getProgress() { |
| 60 |
return mProgress; |
| 61 |
} |
| 62 |
|
| 63 |
public void setDefaultValue(int defaultValue) { |
| 64 |
mDefaultValue = defaultValue; |
| 65 |
} |
| 66 |
|
| 67 |
public int getDefaultValue() { |
| 68 |
return mDefaultValue; |
| 69 |
} |
| 70 |
|
| 71 |
public void setMin(int min) { |
| 72 |
mMin = min; |
| 73 |
} |
| 74 |
|
| 75 |
public int getMin() { |
| 76 |
return mMin; |
| 77 |
} |
| 78 |
|
| 79 |
public void setMax(int max) { |
| 80 |
mMax = max; |
| 81 |
} |
| 82 |
|
| 83 |
public int getMax() { |
| 84 |
return mMax; |
| 85 |
} |
| 86 |
|
| 87 |
public void setFormatter(String formatter) { |
| 88 |
mFormatter = formatter; |
| 89 |
} |
| 90 |
|
| 91 |
public String getFormatter() { |
| 92 |
return mFormatter; |
| 93 |
} |
| 94 |
|
| 95 |
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) |
| 96 |
{ |
| 97 |
super(context, attrs, defStyle); |
| 98 |
setDialogLayoutResource(R.layout.seekbar_preference); |
| 99 |
setLayoutResource(R.layout.information_view); |
| 100 |
setPositiveButtonText(android.R.string.ok); |
| 101 |
setNegativeButtonText(android.R.string.cancel); |
| 102 |
} |
| 103 |
|
| 104 |
public SeekBarPreference(Context context, AttributeSet attrs) |
| 105 |
{ |
| 106 |
this(context, attrs, 0); |
| 107 |
} |
| 108 |
|
| 109 |
public SeekBarPreference(Context context) |
| 110 |
{ |
| 111 |
this(context, null); |
| 112 |
} |
| 113 |
|
| 114 |
private void persistSettings(int progress) { |
| 115 |
mProgress = progress; |
| 116 |
String text = Integer.toString(mProgress); |
| 117 |
callChangeListener(text); |
| 118 |
if (shouldCommit()) { |
| 119 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) |
| 120 |
{ |
| 121 |
PreferenceManager preferenceManager = getPreferenceManager(); |
| 122 |
preferenceManager.setStorageDeviceProtected (); |
| 123 |
} |
| 124 |
SharedPreferences.Editor editor = getEditor(); |
| 125 |
editor.putString(getKey(), text); |
| 126 |
editor.commit(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
protected View onCreateDialogView() { |
| 132 |
View root = super.onCreateDialogView(); |
| 133 |
mSeekBar = (SeekBar)root.findViewById(R.id.seekBar); |
| 134 |
mValueText = (TextView)root.findViewById(R.id.valueText); |
| 135 |
|
| 136 |
Button reset = (Button)root.findViewById(R.id.resetToDefault); |
| 137 |
reset.setOnClickListener(new View.OnClickListener(){ |
| 138 |
@Override |
| 139 |
public void onClick(View v) |
| 140 |
{ |
| 141 |
int defaultValue = getDefaultValue(); |
| 142 |
mSeekBar.setProgress(defaultValue - mMin); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
persistSettings(defaultValue); |
| 148 |
} |
| 149 |
}); |
| 150 |
return root; |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
protected void onBindDialogView(View view) { |
| 155 |
super.onBindDialogView(view); |
| 156 |
mSeekBar.setOnSeekBarChangeListener(this); |
| 157 |
mSeekBar.setMax(mMax - mMin); |
| 158 |
mSeekBar.setProgress(mProgress - mMin); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
mOldProgress = mProgress; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void onClick(DialogInterface dialog, int which) { |
| 168 |
switch (which) { |
| 169 |
case DialogInterface.BUTTON_POSITIVE: |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
mOldProgress = mSeekBar.getProgress() + mMin; |
| 174 |
break; |
| 175 |
} |
| 176 |
super.onClick(dialog, which); |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public void onDismiss(DialogInterface dialog) { |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
persistSettings(mOldProgress); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) |
| 202 |
{ |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
String text = String.format(getFormatter(), Integer.toString(progress + mMin)); |
| 211 |
mValueText.setText(text); |
| 212 |
mSeekBar.setContentDescription(text); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void onStartTrackingTouch(SeekBar seekBar) |
| 217 |
{ |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public void onStopTrackingTouch(SeekBar seekBar) |
| 222 |
{ |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
persistSettings(mSeekBar.getProgress() + mMin); |
| 228 |
} |
| 229 |
} |