Debian Codegraph @

android/src/com/reecedunn/espeak/preference/SeekBarPreference.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 Beka Gozalishvili
3 * Copyright (C) 2013 Reece H. Dunn
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 // Update the last saved value to the so it can be restored later if
52 // the user cancels the dialog. This needs to be done here as well
53 // as the onProgressChanged handler as the SeekBar will not be
54 // initialized at this point.
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 // Persist the value here to ensure that eSpeak is using the
145 // new value the next time e.g. TalkBack reads part of the UI.
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 // Update the last saved value to the so it can be restored later if
161 // the user cancels the dialog.
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 // Update the last saved value so this will be persisted when
171 // the dialog is dismissed.
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 // There are 3 ways to dismiss a dialog:
182 // 1. Pressing the OK (positive) button.
183 // 2. Pressing the Cancel (negative) button.
184 // 3. Pressing the Back button.
185 //
186 // For [1], the new value needs to be persisted. For [2] and [3], the
187 // old value needs to be persisted (so the last saved value is
188 // restored). As there is no easy way to override the Dialog's back
189 // button pressed handler, the following approach is used:
190 //
191 // 1. If the user presses the OK button, the last saved value is
192 // updated to be the new value (see the onClick handler).
193 //
194 // 2. In all cases, the last saved value is persisted when the dialog
195 // is closed (in this onDismiss handler).
196
197 persistSettings(mOldProgress);
198 }
199
200 @Override
201 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
202 {
203 // This callback gets called frequently when the user is moving the
204 // slider, so constantly persisting the seeker value will be annoying.
205 //
206 // If the value is being set programatically, persisting the seeker
207 // value here will cause the speech rate to be set to 80 WPM (via the
208 // onBindDialogView handler).
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 // After the user has let go of the slider, the new value is
224 // persisted to ensure that eSpeak is using the new value the
225 // next time e.g. TalkBack reads part of the UI.
226
227 persistSettings(mSeekBar.getProgress() + mMin);
228 }
229 }