Fix cannot switch between multiple TTS engines

- We didn't reset reset radio button state when user click back
while dialog was popped. Change the UI, when user really accepts the
dialog content, then changes radio button state.
- Migrate TtsEnginePreferenceFragment to RadioButtonPickerFragment

Fixes: 135588938
Test: manual
Change-Id: Ia824e08d59c77a23e6590ff0f5b7d897a73a1ff8
This commit is contained in:
Raff Tsai
2019-06-24 12:57:49 +08:00
parent 5f5ac0b32f
commit a9fc062fd1
6 changed files with 361 additions and 393 deletions

View File

@@ -1,186 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.tts;
import android.content.Context;
import android.content.DialogInterface;
import android.speech.tts.TextToSpeech.EngineInfo;
import android.util.Log;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import androidx.annotation.VisibleForTesting;
public class TtsEnginePreference extends Preference {
private static final String TAG = "TtsEnginePreference";
/**
* The engine information for the engine this preference represents.
* Contains it's name, label etc. which are used for display.
*/
private final EngineInfo mEngineInfo;
/**
* The shared radio button state, which button is checked etc.
*/
private final RadioButtonGroupState mSharedState;
private RadioButton mRadioButton;
/**
* When true, the change callbacks on the radio button will not
* fire.
*/
private volatile boolean mPreventRadioButtonCallbacks;
private final CompoundButton.OnCheckedChangeListener mRadioChangeListener =
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
onRadioButtonClicked(buttonView, isChecked);
}
};
public TtsEnginePreference(Context context, EngineInfo info, RadioButtonGroupState state) {
super(context);
setWidgetLayoutResource(R.layout.preference_widget_radiobutton);
setLayoutResource(R.layout.preference_radio);
setIconSpaceReserved(false);
mSharedState = state;
mEngineInfo = info;
mPreventRadioButtonCallbacks = false;
setKey(mEngineInfo.name);
setTitle(mEngineInfo.label);
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
if (mSharedState == null) {
throw new IllegalStateException("Call to getView() before a call to" +
"setSharedState()");
}
final RadioButton rb = view.itemView.findViewById(android.R.id.checkbox);
rb.setOnCheckedChangeListener(mRadioChangeListener);
boolean isChecked = getKey().equals(mSharedState.getCurrentKey());
if (isChecked) {
mSharedState.setCurrentChecked(rb);
}
mPreventRadioButtonCallbacks = true;
rb.setChecked(isChecked);
mPreventRadioButtonCallbacks = false;
mRadioButton = rb;
}
@Override
public void onClick() {
mRadioButton.setChecked(true);
}
private boolean shouldDisplayDataAlert() {
return !mEngineInfo.system;
}
private void displayDataAlert(
DialogInterface.OnClickListener positiveOnClickListener,
DialogInterface.OnClickListener negativeOnClickListener) {
Log.i(TAG, "Displaying data alert for :" + mEngineInfo.name);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(android.R.string.dialog_alert_title)
.setMessage(getContext().getString(
R.string.tts_engine_security_warning, mEngineInfo.label))
.setCancelable(true)
.setPositiveButton(android.R.string.ok, positiveOnClickListener)
.setNegativeButton(android.R.string.cancel, negativeOnClickListener);
AlertDialog dialog = builder.create();
dialog.show();
}
private void onRadioButtonClicked(final CompoundButton buttonView,
boolean isChecked) {
if (mPreventRadioButtonCallbacks ||
(mSharedState.getCurrentChecked() == buttonView)) {
return;
}
if (isChecked) {
// Should we alert user? if that's true, delay making engine current one.
if (shouldDisplayDataAlert()) {
displayDataAlert(new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
makeCurrentEngine(buttonView);
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Undo the click.
buttonView.setChecked(false);
}
});
} else {
// Privileged engine, set it current
makeCurrentEngine(buttonView);
}
}
}
private void makeCurrentEngine(Checkable current) {
if (mSharedState.getCurrentChecked() != null) {
mSharedState.getCurrentChecked().setChecked(false);
}
mSharedState.setCurrentChecked(current);
mSharedState.setCurrentKey(getKey());
callChangeListener(mSharedState.getCurrentKey());
}
/**
* Holds all state that is common to this group of radio buttons, such
* as the currently selected key and the currently checked compound button.
* (which corresponds to this key).
*/
public interface RadioButtonGroupState {
String getCurrentKey();
Checkable getCurrentChecked();
void setCurrentKey(String key);
void setCurrentChecked(Checkable current);
}
}

View File

@@ -4,111 +4,44 @@ import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.EngineInfo;
import android.speech.tts.TtsEngines;
import android.util.Log;
import android.widget.Checkable;
import androidx.preference.PreferenceCategory;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.tts.TtsEnginePreference.RadioButtonGroupState;
import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.widget.CandidateInfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SearchIndexable
public class TtsEnginePreferenceFragment extends SettingsPreferenceFragment
implements RadioButtonGroupState {
public class TtsEnginePreferenceFragment extends RadioButtonPickerFragment {
private static final String TAG = "TtsEnginePrefFragment";
private static final int VOICE_DATA_INTEGRITY_CHECK = 1977;
/** The currently selected engine. */
private String mCurrentEngine;
/**
* The engine checkbox that is currently checked. Saves us a bit of effort in deducing the right
* one from the currently selected engine.
*/
private Checkable mCurrentChecked;
/**
* The previously selected TTS engine. Useful for rollbacks if the users choice is not loaded or
* fails a voice integrity check.
*/
private String mPreviousEngine;
private PreferenceCategory mEnginePreferenceCategory;
private TextToSpeech mTts = null;
private TtsEngines mEnginesHelper = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.tts_engine_picker);
mEnginePreferenceCategory =
(PreferenceCategory) findPreference("tts_engine_preference_category");
mEnginesHelper = new TtsEngines(getActivity().getApplicationContext());
mTts = new TextToSpeech(getActivity().getApplicationContext(), null);
initSettings();
}
@Override
public int getMetricsCategory() {
return SettingsEnums.TTS_ENGINE_SETTINGS;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mTts != null) {
mTts.shutdown();
mTts = null;
}
}
private void initSettings() {
if (mTts != null) {
mCurrentEngine = mTts.getCurrentEngine();
}
mEnginePreferenceCategory.removeAll();
List<EngineInfo> engines = mEnginesHelper.getEngines();
for (EngineInfo engine : engines) {
TtsEnginePreference enginePref =
new TtsEnginePreference(getPrefContext(), engine, this);
mEnginePreferenceCategory.addPreference(enginePref);
}
}
@Override
public Checkable getCurrentChecked() {
return mCurrentChecked;
}
@Override
public String getCurrentKey() {
return mCurrentEngine;
}
@Override
public void setCurrentChecked(Checkable current) {
mCurrentChecked = current;
}
private Context mContext;
private Map<String, EngineCandidateInfo> mEngineMap;
/**
* The initialization listener used when the user changes his choice of engine (as opposed to
* when then screen is being initialized for the first time).
@@ -121,6 +54,116 @@ public class TtsEnginePreferenceFragment extends SettingsPreferenceFragment
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
mContext = getContext().getApplicationContext();
mEnginesHelper = new TtsEngines(mContext);
mEngineMap = new HashMap<>();
mTts = new TextToSpeech(mContext, null);
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mTts != null) {
mTts.shutdown();
mTts = null;
}
}
@Override
public int getMetricsCategory() {
return SettingsEnums.TTS_ENGINE_SETTINGS;
}
/**
* Step 3: We have now bound to the TTS engine the user requested. We will attempt to check
* voice data for the engine if we successfully bound to it, or revert to the previous engine if
* we didn't.
*/
public void onUpdateEngine(int status) {
if (status == TextToSpeech.SUCCESS) {
Log.d(TAG, "Updating engine: Successfully bound to the engine: "
+ mTts.getCurrentEngine());
android.provider.Settings.Secure.putString(
mContext.getContentResolver(), TTS_DEFAULT_SYNTH, mTts.getCurrentEngine());
} else {
Log.d(TAG, "Updating engine: Failed to bind to engine, reverting.");
if (mPreviousEngine != null) {
// This is guaranteed to at least bind, since mPreviousEngine would be
// null if the previous bind to this engine failed.
mTts = new TextToSpeech(mContext, null, mPreviousEngine);
updateCheckedState(mPreviousEngine);
}
mPreviousEngine = null;
}
}
@Override
protected void onRadioButtonConfirmed(String selectedKey) {
final EngineCandidateInfo info = mEngineMap.get(selectedKey);
// Should we alert user? if that's true, delay making engine current one.
if (shouldDisplayDataAlert(info)) {
displayDataAlert(info, (dialog, which) -> {
setDefaultKey(selectedKey);
});
} else {
// Privileged engine, set it current
setDefaultKey(selectedKey);
}
}
@Override
protected List<? extends CandidateInfo> getCandidates() {
final List<EngineCandidateInfo> infos = new ArrayList<>();
final List<EngineInfo> engines = mEnginesHelper.getEngines();
for (EngineInfo engine : engines) {
final EngineCandidateInfo info = new EngineCandidateInfo(engine);
infos.add(info);
mEngineMap.put(engine.name, info);
}
return infos;
}
@Override
protected String getDefaultKey() {
return mEnginesHelper.getDefaultEngine();
}
@Override
protected boolean setDefaultKey(String key) {
updateDefaultEngine(key);
updateCheckedState(key);
return true;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.tts_engine_picker;
}
private boolean shouldDisplayDataAlert(EngineCandidateInfo info) {
return !info.isSystem();
}
private void displayDataAlert(EngineCandidateInfo info,
DialogInterface.OnClickListener positiveOnClickListener) {
Log.i(TAG, "Displaying data alert for :" + info.getKey());
final AlertDialog dialog = new AlertDialog.Builder(getPrefContext())
.setTitle(android.R.string.dialog_alert_title)
.setMessage(mContext.getString(
R.string.tts_engine_security_warning, info.loadLabel()))
.setCancelable(true)
.setPositiveButton(android.R.string.ok, positiveOnClickListener)
.setNegativeButton(android.R.string.cancel, null)
.create();
dialog.show();
}
private void updateDefaultEngine(String engine) {
Log.d(TAG, "Updating default synth to : " + engine);
@@ -146,41 +189,36 @@ public class TtsEnginePreferenceFragment extends SettingsPreferenceFragment
// Step 3 is continued on #onUpdateEngine (below) which is called when
// the app binds successfully to the engine.
Log.i(TAG, "Updating engine : Attempting to connect to engine: " + engine);
mTts = new TextToSpeech(getActivity().getApplicationContext(), mUpdateListener, engine);
mTts = new TextToSpeech(mContext, mUpdateListener, engine);
Log.i(TAG, "Success");
}
/**
* Step 3: We have now bound to the TTS engine the user requested. We will attempt to check
* voice data for the engine if we successfully bound to it, or revert to the previous engine if
* we didn't.
*/
public void onUpdateEngine(int status) {
if (status == TextToSpeech.SUCCESS) {
Log.d(
public static class EngineCandidateInfo extends CandidateInfo {
private final EngineInfo mEngineInfo;
TAG,
"Updating engine: Successfully bound to the engine: "
+ mTts.getCurrentEngine());
android.provider.Settings.Secure.putString(
getContentResolver(), TTS_DEFAULT_SYNTH, mTts.getCurrentEngine());
} else {
Log.d(TAG, "Updating engine: Failed to bind to engine, reverting.");
if (mPreviousEngine != null) {
// This is guaranteed to at least bind, since mPreviousEngine would be
// null if the previous bind to this engine failed.
mTts =
new TextToSpeech(
getActivity().getApplicationContext(), null, mPreviousEngine);
}
mPreviousEngine = null;
EngineCandidateInfo(EngineInfo engineInfo) {
super(true /* enabled */);
mEngineInfo = engineInfo;
}
}
@Override
public void setCurrentKey(String key) {
mCurrentEngine = key;
updateDefaultEngine(mCurrentEngine);
@Override
public CharSequence loadLabel() {
return mEngineInfo.label;
}
@Override
public Drawable loadIcon() {
return null;
}
@Override
public String getKey() {
return mEngineInfo.name;
}
public boolean isSystem() {
return mEngineInfo.system;
}
}
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =