Debian Codegraph @

android/src/com/reecedunn/espeak/preference/SpeakPunctuationPreference.java

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