eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SeekBarPreference.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. package com.reecedunn.espeak.preference;
  18. import android.content.Context;
  19. import android.content.DialogInterface;
  20. import android.content.SharedPreferences;
  21. import android.os.Build;
  22. import android.preference.DialogPreference;
  23. import android.preference.PreferenceManager;
  24. import android.util.AttributeSet;
  25. import android.view.View;
  26. import android.widget.Button;
  27. import android.widget.SeekBar;
  28. import android.widget.TextView;
  29. import com.reecedunn.espeak.R;
  30. public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener
  31. {
  32. private SeekBar mSeekBar;
  33. private TextView mValueText;
  34. private int mOldProgress = 0;
  35. private int mProgress = 0;
  36. private int mDefaultValue = 0;
  37. private int mMin = 0;
  38. private int mMax = 100;
  39. private String mFormatter = "%s";
  40. public void setProgress(int progress) {
  41. mProgress = progress;
  42. String text = Integer.toString(mProgress);
  43. callChangeListener(text);
  44. // Update the last saved value to the so it can be restored later if
  45. // the user cancels the dialog. This needs to be done here as well
  46. // as the onProgressChanged handler as the SeekBar will not be
  47. // initialized at this point.
  48. mOldProgress = mProgress;
  49. }
  50. public int getProgress() {
  51. return mProgress;
  52. }
  53. public void setDefaultValue(int defaultValue) {
  54. mDefaultValue = defaultValue;
  55. }
  56. public int getDefaultValue() {
  57. return mDefaultValue;
  58. }
  59. public void setMin(int min) {
  60. mMin = min;
  61. }
  62. public int getMin() {
  63. return mMin;
  64. }
  65. public void setMax(int max) {
  66. mMax = max;
  67. }
  68. public int getMax() {
  69. return mMax;
  70. }
  71. public void setFormatter(String formatter) {
  72. mFormatter = formatter;
  73. }
  74. public String getFormatter() {
  75. return mFormatter;
  76. }
  77. public SeekBarPreference(Context context, AttributeSet attrs, int defStyle)
  78. {
  79. super(context, attrs, defStyle);
  80. setDialogLayoutResource(R.layout.seekbar_preference);
  81. setLayoutResource(R.layout.information_view);
  82. setPositiveButtonText(android.R.string.ok);
  83. setNegativeButtonText(android.R.string.cancel);
  84. }
  85. public SeekBarPreference(Context context, AttributeSet attrs)
  86. {
  87. this(context, attrs, 0);
  88. }
  89. public SeekBarPreference(Context context)
  90. {
  91. this(context, null);
  92. }
  93. private void persistSettings(int progress) {
  94. mProgress = progress;
  95. String text = Integer.toString(mProgress);
  96. callChangeListener(text);
  97. if (shouldCommit()) {
  98. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
  99. {
  100. PreferenceManager preferenceManager = getPreferenceManager();
  101. preferenceManager.setStorageDeviceProtected ();
  102. }
  103. SharedPreferences.Editor editor = getEditor();
  104. editor.putString(getKey(), text);
  105. editor.commit();
  106. }
  107. }
  108. @Override
  109. protected View onCreateDialogView() {
  110. View root = super.onCreateDialogView();
  111. mSeekBar = (SeekBar)root.findViewById(R.id.seekBar);
  112. mValueText = (TextView)root.findViewById(R.id.valueText);
  113. Button reset = (Button)root.findViewById(R.id.resetToDefault);
  114. reset.setOnClickListener(new View.OnClickListener(){
  115. @Override
  116. public void onClick(View v)
  117. {
  118. int defaultValue = getDefaultValue();
  119. mSeekBar.setProgress(defaultValue - mMin);
  120. // Persist the value here to ensure that eSpeak is using the
  121. // new value the next time e.g. TalkBack reads part of the UI.
  122. persistSettings(defaultValue);
  123. }
  124. });
  125. return root;
  126. }
  127. @Override
  128. protected void onBindDialogView(View view) {
  129. super.onBindDialogView(view);
  130. mSeekBar.setOnSeekBarChangeListener(this);
  131. mSeekBar.setMax(mMax - mMin);
  132. mSeekBar.setProgress(mProgress - mMin);
  133. // Update the last saved value to the so it can be restored later if
  134. // the user cancels the dialog.
  135. mOldProgress = mProgress;
  136. }
  137. @Override
  138. public void onClick(DialogInterface dialog, int which) {
  139. switch (which) {
  140. case DialogInterface.BUTTON_POSITIVE:
  141. // Update the last saved value so this will be persisted when
  142. // the dialog is dismissed.
  143. mOldProgress = mSeekBar.getProgress() + mMin;
  144. break;
  145. }
  146. super.onClick(dialog, which);
  147. }
  148. @Override
  149. public void onDismiss(DialogInterface dialog) {
  150. // There are 3 ways to dismiss a dialog:
  151. // 1. Pressing the OK (positive) button.
  152. // 2. Pressing the Cancel (negative) button.
  153. // 3. Pressing the Back button.
  154. //
  155. // For [1], the new value needs to be persisted. For [2] and [3], the
  156. // old value needs to be persisted (so the last saved value is
  157. // restored). As there is no easy way to override the Dialog's back
  158. // button pressed handler, the following approach is used:
  159. //
  160. // 1. If the user presses the OK button, the last saved value is
  161. // updated to be the new value (see the onClick handler).
  162. //
  163. // 2. In all cases, the last saved value is persisted when the dialog
  164. // is closed (in this onDismiss handler).
  165. persistSettings(mOldProgress);
  166. }
  167. @Override
  168. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
  169. {
  170. // This callback gets called frequently when the user is moving the
  171. // slider, so constantly persisting the seeker value will be annoying.
  172. //
  173. // If the value is being set programatically, persisting the seeker
  174. // value here will cause the speech rate to be set to 80 WPM (via the
  175. // onBindDialogView handler).
  176. String text = String.format(getFormatter(), Integer.toString(progress + mMin));
  177. mValueText.setText(text);
  178. mSeekBar.setContentDescription(text);
  179. }
  180. @Override
  181. public void onStartTrackingTouch(SeekBar seekBar)
  182. {
  183. }
  184. @Override
  185. public void onStopTrackingTouch(SeekBar seekBar)
  186. {
  187. // After the user has let go of the slider, the new value is
  188. // persisted to ensure that eSpeak is using the new value the
  189. // next time e.g. TalkBack reads part of the UI.
  190. persistSettings(mSeekBar.getProgress() + mMin);
  191. }
  192. }