Made following changes to Settings:
- Moved the Voice Input setting from Digital assistant app to Languages & input. - Bundled Voice Input and Text-to-speech settings together under Speech category. Bug: 218609449 Test: refactoring CL. Existing unit tests still pass. Change-Id: I3107a410ed35685f5f1081cbe448b105b5c79c24
This commit is contained in:
123
src/com/android/settings/language/DefaultVoiceInputPicker.java
Normal file
123
src/com/android/settings/language/DefaultVoiceInputPicker.java
Normal file
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.language;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
|
||||
import com.android.settingslib.applications.DefaultAppInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Controls the Voice Input setting. */
|
||||
public class DefaultVoiceInputPicker extends DefaultAppPickerFragment {
|
||||
|
||||
private VoiceInputHelper mHelper;
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return SettingsEnums.DEFAULT_VOICE_INPUT_PICKER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mHelper = new VoiceInputHelper(context);
|
||||
mHelper.buildUi();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.default_voice_settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<VoiceInputDefaultAppInfo> getCandidates() {
|
||||
final List<VoiceInputDefaultAppInfo> candidates = new ArrayList<>();
|
||||
final Context context = getContext();
|
||||
|
||||
for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
|
||||
final boolean enabled = true;
|
||||
candidates.add(new VoiceInputDefaultAppInfo(context, mPm, mUserId, info, enabled));
|
||||
}
|
||||
return candidates;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getDefaultKey() {
|
||||
final ComponentName currentService = getCurrentService(mHelper);
|
||||
if (currentService == null) {
|
||||
return null;
|
||||
}
|
||||
return currentService.flattenToShortString();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean setDefaultKey(String value) {
|
||||
for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
|
||||
if (TextUtils.equals(value, info.mKey)) {
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.VOICE_RECOGNITION_SERVICE, value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Gets the current recognition service component. */
|
||||
public static ComponentName getCurrentService(VoiceInputHelper helper) {
|
||||
return helper.mCurrentRecognizer;
|
||||
}
|
||||
|
||||
/** Stores the info of the Voice Input provider. */
|
||||
public static class VoiceInputDefaultAppInfo extends DefaultAppInfo {
|
||||
|
||||
public VoiceInputHelper.BaseInfo mInfo;
|
||||
|
||||
public VoiceInputDefaultAppInfo(Context context, PackageManager pm, int userId,
|
||||
VoiceInputHelper.BaseInfo info, boolean enabled) {
|
||||
super(context, pm, userId, info.mComponentName, null /* summary */, enabled);
|
||||
mInfo = info;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return mInfo.mKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence loadLabel() {
|
||||
return mInfo.mLabel;
|
||||
}
|
||||
|
||||
/** Gets the setting intent. */
|
||||
public Intent getSettingIntent() {
|
||||
if (mInfo.mSettings == null) {
|
||||
return null;
|
||||
}
|
||||
return new Intent(Intent.ACTION_MAIN).setComponent(mInfo.mSettings);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.language;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
|
||||
import com.android.settingslib.applications.DefaultAppInfo;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
|
||||
/** Controller of the Voice Input preference. */
|
||||
public class DefaultVoiceInputPreferenceController extends DefaultAppPreferenceController
|
||||
implements LifecycleObserver, OnResume, OnPause {
|
||||
|
||||
private static final String KEY_VOICE_INPUT = "voice_input_settings";
|
||||
|
||||
private VoiceInputHelper mHelper;
|
||||
private PreferenceScreen mScreen;
|
||||
private Preference mPreference;
|
||||
private Context mContext;
|
||||
|
||||
public DefaultVoiceInputPreferenceController(Context context, Lifecycle lifecycle) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
mHelper = new VoiceInputHelper(context);
|
||||
mHelper.buildUi();
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return mContext.getPackageManager().hasSystemFeature(
|
||||
PackageManager.FEATURE_VOICE_RECOGNIZERS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_VOICE_INPUT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
mScreen = screen;
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
super.displayPreference(screen);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
updatePreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(mPreference);
|
||||
updatePreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {}
|
||||
|
||||
@Override
|
||||
protected DefaultAppInfo getDefaultAppInfo() {
|
||||
final String defaultKey = getDefaultAppKey();
|
||||
if (defaultKey == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (VoiceInputHelper.RecognizerInfo info : mHelper.mAvailableRecognizerInfos) {
|
||||
if (TextUtils.equals(defaultKey, info.mKey)) {
|
||||
return new DefaultVoiceInputPicker.VoiceInputDefaultAppInfo(mContext,
|
||||
mPackageManager, mUserId, info, true /* enabled */);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Intent getSettingIntent(DefaultAppInfo info) {
|
||||
final DefaultAppInfo appInfo = getDefaultAppInfo();
|
||||
if (appInfo == null
|
||||
|| !(appInfo instanceof DefaultVoiceInputPicker.VoiceInputDefaultAppInfo)) {
|
||||
return null;
|
||||
}
|
||||
return ((DefaultVoiceInputPicker.VoiceInputDefaultAppInfo) appInfo).getSettingIntent();
|
||||
}
|
||||
|
||||
private void updatePreference() {
|
||||
if (mPreference == null) {
|
||||
return;
|
||||
}
|
||||
mHelper.buildUi();
|
||||
if (isAvailable()) {
|
||||
if (mScreen.findPreference(getPreferenceKey()) == null) {
|
||||
// add it if it's not on scree
|
||||
mScreen.addPreference(mPreference);
|
||||
}
|
||||
} else {
|
||||
mScreen.removePreference(mPreference);
|
||||
}
|
||||
}
|
||||
|
||||
private String getDefaultAppKey() {
|
||||
final ComponentName currentService = DefaultVoiceInputPicker.getCurrentService(mHelper);
|
||||
if (currentService == null) {
|
||||
return null;
|
||||
}
|
||||
return currentService.flattenToShortString();
|
||||
}
|
||||
}
|
@@ -44,8 +44,9 @@ public class LanguageAndInputSettings extends DashboardFragment {
|
||||
private static final String TAG = "LangAndInputSettings";
|
||||
|
||||
private static final String KEY_KEYBOARDS_CATEGORY = "keyboards_category";
|
||||
private static final String KEY_SPEECH_CATEGORY = "speech_category";
|
||||
private static final String KEY_TEXT_TO_SPEECH = "tts_settings_summary";
|
||||
private static final String KEY_POINTER_AND_TTS_CATEGORY = "pointer_and_tts_category";
|
||||
private static final String KEY_POINTER_CATEGORY = "pointer_category";
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
@@ -98,15 +99,22 @@ public class LanguageAndInputSettings extends DashboardFragment {
|
||||
Arrays.asList(virtualKeyboardPreferenceController,
|
||||
physicalKeyboardPreferenceController)));
|
||||
|
||||
// Pointer and Tts
|
||||
// Speech
|
||||
final DefaultVoiceInputPreferenceController defaultVoiceInputPreferenceController =
|
||||
new DefaultVoiceInputPreferenceController(context, lifecycle);
|
||||
final TtsPreferenceController ttsPreferenceController =
|
||||
new TtsPreferenceController(context, KEY_TEXT_TO_SPEECH);
|
||||
controllers.add(defaultVoiceInputPreferenceController);
|
||||
controllers.add(ttsPreferenceController);
|
||||
controllers.add(new PreferenceCategoryController(context,
|
||||
KEY_SPEECH_CATEGORY).setChildren(
|
||||
Arrays.asList(defaultVoiceInputPreferenceController, ttsPreferenceController)));
|
||||
|
||||
// Pointer
|
||||
final PointerSpeedController pointerController = new PointerSpeedController(context);
|
||||
controllers.add(pointerController);
|
||||
controllers.add(new PreferenceCategoryController(context,
|
||||
KEY_POINTER_AND_TTS_CATEGORY).setChildren(
|
||||
Arrays.asList(pointerController, ttsPreferenceController)));
|
||||
KEY_POINTER_CATEGORY).setChildren(Arrays.asList(pointerController)));
|
||||
|
||||
// Input Assistance
|
||||
controllers.add(new SpellCheckerPreferenceController(context));
|
||||
|
175
src/com/android/settings/language/VoiceInputHelper.java
Normal file
175
src/com/android/settings/language/VoiceInputHelper.java
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright (C) 2022 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.language;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.content.res.XmlResourceParser;
|
||||
import android.provider.Settings;
|
||||
import android.speech.RecognitionService;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.util.Xml;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/** Helper class of the Voice Input setting. */
|
||||
public final class VoiceInputHelper {
|
||||
static final String TAG = "VoiceInputHelper";
|
||||
final Context mContext;
|
||||
|
||||
final List<ResolveInfo> mAvailableRecognition;
|
||||
|
||||
/**
|
||||
* Base info of the Voice Input provider.
|
||||
*
|
||||
* TODO: Remove this superclass as we only have 1 class now (RecognizerInfo).
|
||||
*/
|
||||
public static class BaseInfo implements Comparable<BaseInfo> {
|
||||
public final ServiceInfo mService;
|
||||
public final ComponentName mComponentName;
|
||||
public final String mKey;
|
||||
public final ComponentName mSettings;
|
||||
public final CharSequence mLabel;
|
||||
public final String mLabelStr;
|
||||
public final CharSequence mAppLabel;
|
||||
|
||||
public BaseInfo(PackageManager pm, ServiceInfo service, String settings) {
|
||||
mService = service;
|
||||
mComponentName = new ComponentName(service.packageName, service.name);
|
||||
mKey = mComponentName.flattenToShortString();
|
||||
mSettings = settings != null
|
||||
? new ComponentName(service.packageName, settings) : null;
|
||||
mLabel = service.loadLabel(pm);
|
||||
mLabelStr = mLabel.toString();
|
||||
mAppLabel = service.applicationInfo.loadLabel(pm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(BaseInfo another) {
|
||||
return mLabelStr.compareTo(another.mLabelStr);
|
||||
}
|
||||
}
|
||||
|
||||
/** Info of the speech recognizer (i.e. recognition service). */
|
||||
public static class RecognizerInfo extends BaseInfo {
|
||||
public final boolean mSelectableAsDefault;
|
||||
|
||||
public RecognizerInfo(PackageManager pm,
|
||||
ServiceInfo serviceInfo,
|
||||
String settings,
|
||||
boolean selectableAsDefault) {
|
||||
super(pm, serviceInfo, settings);
|
||||
this.mSelectableAsDefault = selectableAsDefault;
|
||||
}
|
||||
}
|
||||
|
||||
final ArrayList<RecognizerInfo> mAvailableRecognizerInfos = new ArrayList<>();
|
||||
|
||||
ComponentName mCurrentRecognizer;
|
||||
|
||||
public VoiceInputHelper(Context context) {
|
||||
mContext = context;
|
||||
|
||||
mAvailableRecognition = mContext.getPackageManager().queryIntentServices(
|
||||
new Intent(RecognitionService.SERVICE_INTERFACE),
|
||||
PackageManager.GET_META_DATA);
|
||||
}
|
||||
|
||||
/** Draws the UI of the Voice Input picker page. */
|
||||
public void buildUi() {
|
||||
// Get the currently selected recognizer from the secure setting.
|
||||
String currentSetting = Settings.Secure.getString(
|
||||
mContext.getContentResolver(), Settings.Secure.VOICE_RECOGNITION_SERVICE);
|
||||
if (currentSetting != null && !currentSetting.isEmpty()) {
|
||||
mCurrentRecognizer = ComponentName.unflattenFromString(currentSetting);
|
||||
} else {
|
||||
mCurrentRecognizer = null;
|
||||
}
|
||||
|
||||
// Iterate through all the available recognizers and load up their info to show
|
||||
// in the preference.
|
||||
int size = mAvailableRecognition.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
ResolveInfo resolveInfo = mAvailableRecognition.get(i);
|
||||
ComponentName comp = new ComponentName(resolveInfo.serviceInfo.packageName,
|
||||
resolveInfo.serviceInfo.name);
|
||||
ServiceInfo si = resolveInfo.serviceInfo;
|
||||
String settingsActivity = null;
|
||||
// Always show in voice input settings unless specifically set to False.
|
||||
boolean selectableAsDefault = true;
|
||||
try (XmlResourceParser parser = si.loadXmlMetaData(mContext.getPackageManager(),
|
||||
RecognitionService.SERVICE_META_DATA)) {
|
||||
if (parser == null) {
|
||||
throw new XmlPullParserException("No " + RecognitionService.SERVICE_META_DATA
|
||||
+ " meta-data for " + si.packageName);
|
||||
}
|
||||
|
||||
Resources res = mContext.getPackageManager().getResourcesForApplication(
|
||||
si.applicationInfo);
|
||||
|
||||
AttributeSet attrs = Xml.asAttributeSet(parser);
|
||||
|
||||
int type;
|
||||
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
|
||||
&& type != XmlPullParser.START_TAG) {
|
||||
// Intentionally do nothing.
|
||||
}
|
||||
|
||||
String nodeName = parser.getName();
|
||||
if (!"recognition-service".equals(nodeName)) {
|
||||
throw new XmlPullParserException(
|
||||
"Meta-data does not start with recognition-service tag");
|
||||
}
|
||||
|
||||
TypedArray array = res.obtainAttributes(attrs,
|
||||
com.android.internal.R.styleable.RecognitionService);
|
||||
settingsActivity = array.getString(
|
||||
com.android.internal.R.styleable.RecognitionService_settingsActivity);
|
||||
selectableAsDefault = array.getBoolean(
|
||||
com.android.internal.R.styleable.RecognitionService_selectableAsDefault,
|
||||
true);
|
||||
array.recycle();
|
||||
} catch (XmlPullParserException e) {
|
||||
Log.e(TAG, "error parsing recognition service meta-data", e);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "error parsing recognition service meta-data", e);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(TAG, "error parsing recognition service meta-data", e);
|
||||
}
|
||||
// The current recognizer must always be shown in the settings, whatever its
|
||||
// selectableAsDefault value is.
|
||||
if (selectableAsDefault || comp.equals(mCurrentRecognizer)) {
|
||||
mAvailableRecognizerInfos.add(new RecognizerInfo(mContext.getPackageManager(),
|
||||
resolveInfo.serviceInfo, settingsActivity, selectableAsDefault));
|
||||
}
|
||||
}
|
||||
Collections.sort(mAvailableRecognizerInfos);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user