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.

SpeakPunctuationPreference.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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;
  17. import android.content.Context;
  18. import android.content.DialogInterface;
  19. import android.content.SharedPreferences;
  20. import android.preference.DialogPreference;
  21. import android.text.Editable;
  22. import android.util.AttributeSet;
  23. import android.view.View;
  24. import android.widget.Button;
  25. import android.widget.EditText;
  26. import android.widget.RadioButton;
  27. import android.widget.SeekBar;
  28. import android.widget.TextView;
  29. public class SpeakPunctuationPreference extends DialogPreference {
  30. private RadioButton mAll;
  31. private RadioButton mCustom;
  32. private EditText mPunctuationCharacters;
  33. private VoiceSettings mSettings;
  34. public SpeakPunctuationPreference(Context context, AttributeSet attrs, int defStyle) {
  35. super(context, attrs, defStyle);
  36. setDialogLayoutResource(R.layout.speak_punctuation_preference);
  37. setPositiveButtonText(android.R.string.ok);
  38. setNegativeButtonText(android.R.string.cancel);
  39. }
  40. public SpeakPunctuationPreference(Context context, AttributeSet attrs) {
  41. this(context, attrs, 0);
  42. }
  43. public SpeakPunctuationPreference(Context context) {
  44. this(context, null);
  45. }
  46. public void setVoiceSettings(VoiceSettings settings) {
  47. mSettings = settings;
  48. onDataChanged(mSettings.getPunctuationLevel(), mSettings.getPunctuationCharacters());
  49. }
  50. private void onDataChanged(int level, String characters) {
  51. switch (level) {
  52. case SpeechSynthesis.PUNCT_ALL:
  53. callChangeListener(getContext().getText(R.string.punctuation_all));
  54. break;
  55. case SpeechSynthesis.PUNCT_SOME:
  56. if (characters == null || characters.isEmpty()) {
  57. callChangeListener(getContext().getText(R.string.punctuation_none));
  58. } else {
  59. callChangeListener(String.format(getContext().getText(R.string.punctuation_custom_fmt).toString(), characters));
  60. }
  61. break;
  62. case SpeechSynthesis.PUNCT_NONE:
  63. callChangeListener(getContext().getText(R.string.punctuation_none));
  64. break;
  65. }
  66. }
  67. @Override
  68. protected View onCreateDialogView() {
  69. View root = super.onCreateDialogView();
  70. mAll = (RadioButton)root.findViewById(R.id.all);
  71. mCustom = (RadioButton)root.findViewById(R.id.custom);
  72. mPunctuationCharacters = (EditText)root.findViewById(R.id.punctuation_characters);
  73. return root;
  74. }
  75. @Override
  76. protected void onBindDialogView(View view) {
  77. super.onBindDialogView(view);
  78. if (mSettings.getPunctuationLevel() == SpeechSynthesis.PUNCT_ALL) {
  79. mAll.toggle();
  80. } else {
  81. mCustom.toggle();
  82. }
  83. mPunctuationCharacters.setText(mSettings.getPunctuationCharacters());
  84. }
  85. @Override
  86. public void onClick(DialogInterface dialog, int which) {
  87. switch (which) {
  88. case DialogInterface.BUTTON_POSITIVE:
  89. Editable text = mPunctuationCharacters.getText();
  90. String characters = null;
  91. int level;
  92. if (text != null) {
  93. characters = text.toString();
  94. }
  95. if (characters == null || characters.isEmpty()) {
  96. level = mAll.isChecked() ? SpeechSynthesis.PUNCT_ALL : SpeechSynthesis.PUNCT_NONE;
  97. } else {
  98. level = mAll.isChecked() ? SpeechSynthesis.PUNCT_ALL : SpeechSynthesis.PUNCT_SOME;
  99. }
  100. onDataChanged(level, characters);
  101. if (shouldCommit()) {
  102. SharedPreferences.Editor editor = getEditor();
  103. if (editor != null) {
  104. editor.putString(VoiceSettings.PREF_PUNCTUATION_CHARACTERS, characters);
  105. editor.putString(VoiceSettings.PREF_PUNCTUATION_LEVEL, Integer.toString(level));
  106. editor.commit();
  107. }
  108. }
  109. break;
  110. }
  111. super.onClick(dialog, which);
  112. }
  113. }