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 6.8KB

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