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

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