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.1KB

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