Merge "Implement default assist app setting" into mnc-dev
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* Copyright (C) 2015 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.applications;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.provider.Settings;
|
||||
import android.service.voice.VoiceInteractionService;
|
||||
import android.service.voice.VoiceInteractionServiceInfo;
|
||||
import android.speech.RecognitionService;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.android.settings.AppListPreferenceWithSettings;
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DefaultAssistPreference extends AppListPreferenceWithSettings {
|
||||
|
||||
private static final String TAG = DefaultAssistPreference.class.getSimpleName();
|
||||
|
||||
private final List<Info> mAvailableAssistants = new ArrayList<>();
|
||||
|
||||
public DefaultAssistPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setShowItemNone(true);
|
||||
setDialogTitle(R.string.choose_assist_title);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean persistString(String value) {
|
||||
final Info info = findAssistantByPackageName(value);
|
||||
if (info == null) {
|
||||
setAssistNone();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (info.isVoiceInteractionService()) {
|
||||
setAssistService(info);
|
||||
} else {
|
||||
setAssistActivity(info);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setAssistNone() {
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.ASSISTANT, ITEM_NONE_VALUE);
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.VOICE_INTERACTION_SERVICE, "");
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.VOICE_RECOGNITION_SERVICE, getDefaultRecognizer());
|
||||
|
||||
setSummary(getContext().getText(R.string.default_assist_none));
|
||||
setSettingsComponent(null);
|
||||
}
|
||||
|
||||
private void setAssistService(Info serviceInfo) {
|
||||
final String serviceComponentName = serviceInfo.component.flattenToShortString();
|
||||
final String serviceRecognizerName = new ComponentName(
|
||||
serviceInfo.component.getPackageName(),
|
||||
serviceInfo.voiceInteractionServiceInfo.getRecognitionService())
|
||||
.flattenToShortString();
|
||||
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.ASSISTANT, serviceComponentName);
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.VOICE_INTERACTION_SERVICE, serviceComponentName);
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.VOICE_RECOGNITION_SERVICE, serviceRecognizerName);
|
||||
|
||||
setSummary(getEntry());
|
||||
final String settingsActivity =
|
||||
serviceInfo.voiceInteractionServiceInfo.getSettingsActivity();
|
||||
setSettingsComponent(settingsActivity == null ?
|
||||
null :
|
||||
new ComponentName(serviceInfo.component.getPackageName(), settingsActivity));
|
||||
}
|
||||
|
||||
private void setAssistActivity(Info activityInfo) {
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.ASSISTANT, activityInfo.component.flattenToShortString());
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.VOICE_INTERACTION_SERVICE, "");
|
||||
Settings.Secure.putString(getContext().getContentResolver(),
|
||||
Settings.Secure.VOICE_RECOGNITION_SERVICE, getDefaultRecognizer());
|
||||
|
||||
setSummary(getEntry());
|
||||
setSettingsComponent(null);
|
||||
}
|
||||
|
||||
private String getDefaultRecognizer() {
|
||||
ResolveInfo resolveInfo = getContext().getPackageManager().resolveService(
|
||||
new Intent(RecognitionService.SERVICE_INTERFACE),
|
||||
PackageManager.GET_META_DATA);
|
||||
if (resolveInfo == null || resolveInfo.serviceInfo == null) {
|
||||
Log.w(TAG, "Unable to resolve default voice recognition service.");
|
||||
return "";
|
||||
}
|
||||
|
||||
return new ComponentName(resolveInfo.serviceInfo.packageName,
|
||||
resolveInfo.serviceInfo.name).flattenToShortString();
|
||||
}
|
||||
|
||||
private Info findAssistantByPackageName(String packageName) {
|
||||
for (int i = 0; i < mAvailableAssistants.size(); ++i) {
|
||||
Info info = mAvailableAssistants.get(i);
|
||||
if (info.component.getPackageName().equals(packageName)) {
|
||||
return info;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addAssistServices() {
|
||||
PackageManager pm = getContext().getPackageManager();
|
||||
|
||||
List<ResolveInfo> services = pm.queryIntentServices(
|
||||
new Intent(VoiceInteractionService.SERVICE_INTERFACE),
|
||||
PackageManager.GET_META_DATA);
|
||||
for (int i = 0; i < services.size(); ++i) {
|
||||
ResolveInfo resolveInfo = services.get(i);
|
||||
VoiceInteractionServiceInfo voiceInteractionServiceInfo =
|
||||
new VoiceInteractionServiceInfo(pm, resolveInfo.serviceInfo);
|
||||
if (!voiceInteractionServiceInfo.getSupportsAssist()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
mAvailableAssistants.add(new Info(
|
||||
new ComponentName(resolveInfo.serviceInfo.packageName,
|
||||
resolveInfo.serviceInfo.name),
|
||||
voiceInteractionServiceInfo));
|
||||
}
|
||||
}
|
||||
|
||||
private void addAssistActivities() {
|
||||
PackageManager pm = getContext().getPackageManager();
|
||||
|
||||
List<ResolveInfo> activities = pm.queryIntentActivities(
|
||||
new Intent(Intent.ACTION_ASSIST),
|
||||
PackageManager.MATCH_DEFAULT_ONLY);
|
||||
for (int i = 0; i < activities.size(); ++i) {
|
||||
ResolveInfo resolveInfo = activities.get(i);
|
||||
mAvailableAssistants.add(new Info(
|
||||
new ComponentName(resolveInfo.activityInfo.packageName,
|
||||
resolveInfo.activityInfo.name)));
|
||||
}
|
||||
}
|
||||
|
||||
public ComponentName getCurrentAssist() {
|
||||
String currentSetting = Settings.Secure.getString(getContext().getContentResolver(),
|
||||
Settings.Secure.ASSISTANT);
|
||||
return currentSetting == null ? null : ComponentName.unflattenFromString(currentSetting);
|
||||
}
|
||||
|
||||
public void refreshAssistApps() {
|
||||
mAvailableAssistants.clear();
|
||||
addAssistServices();
|
||||
addAssistActivities();
|
||||
|
||||
List<String> packages = new ArrayList<>();
|
||||
for (int i = 0; i < mAvailableAssistants.size(); ++i) {
|
||||
String packageName = mAvailableAssistants.get(i).component.getPackageName();
|
||||
if (packages.contains(packageName)) {
|
||||
// A service appears before an activity thus overrides it if from the same package.
|
||||
continue;
|
||||
}
|
||||
packages.add(packageName);
|
||||
}
|
||||
|
||||
ComponentName currentAssist = getCurrentAssist();
|
||||
setPackageNames(packages.toArray(new String[packages.size()]),
|
||||
currentAssist == null ? null : currentAssist.getPackageName());
|
||||
}
|
||||
|
||||
private static class Info {
|
||||
public final ComponentName component;
|
||||
public final VoiceInteractionServiceInfo voiceInteractionServiceInfo;
|
||||
|
||||
Info(ComponentName component) {
|
||||
this.component = component;
|
||||
this.voiceInteractionServiceInfo = null;
|
||||
}
|
||||
|
||||
Info(ComponentName component, VoiceInteractionServiceInfo voiceInteractionServiceInfo) {
|
||||
this.component = component;
|
||||
this.voiceInteractionServiceInfo = voiceInteractionServiceInfo;
|
||||
}
|
||||
|
||||
public boolean isVoiceInteractionService() {
|
||||
return voiceInteractionServiceInfo != null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package com.android.settings.applications;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.ComponentName;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.preference.Preference;
|
||||
import android.preference.SwitchPreference;
|
||||
@@ -25,6 +28,7 @@ import com.android.internal.logging.MetricsLogger;
|
||||
import com.android.settings.InstrumentedFragment;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.voice.VoiceInputListPreference;
|
||||
|
||||
/**
|
||||
* Settings screen to manage everything about assist.
|
||||
@@ -32,18 +36,29 @@ import com.android.settings.SettingsPreferenceFragment;
|
||||
public class ManageAssist extends SettingsPreferenceFragment
|
||||
implements Preference.OnPreferenceChangeListener {
|
||||
|
||||
private static final String KEY_DEFAULT_ASSIST = "default_assist";
|
||||
private static final String KEY_CONTEXT = "context";
|
||||
private static final String KEY_VOICE_INPUT = "voice_input_settings";
|
||||
|
||||
private DefaultAssistPreference mDefaultAssitPref;
|
||||
private SwitchPreference mContextPref;
|
||||
private VoiceInputListPreference mVoiceInputPref;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
addPreferencesFromResource(R.xml.manage_assist);
|
||||
|
||||
mDefaultAssitPref = (DefaultAssistPreference) findPreference(KEY_DEFAULT_ASSIST);
|
||||
mDefaultAssitPref.setOnPreferenceChangeListener(this);
|
||||
|
||||
mContextPref = (SwitchPreference) findPreference(KEY_CONTEXT);
|
||||
mContextPref.setChecked(Settings.Secure.getInt(getContentResolver(),
|
||||
Settings.Secure.ASSIST_STRUCTURE_ENABLED, 1) != 0);
|
||||
mContextPref.setOnPreferenceChangeListener(this);
|
||||
|
||||
mVoiceInputPref = (VoiceInputListPreference) findPreference(KEY_VOICE_INPUT);
|
||||
updateUi();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,6 +73,64 @@ public class ManageAssist extends SettingsPreferenceFragment
|
||||
(boolean) newValue ? 1 : 0);
|
||||
return true;
|
||||
}
|
||||
if (preference == mDefaultAssitPref) {
|
||||
String newAssitPackage = (String)newValue;
|
||||
if (newAssitPackage == null ||
|
||||
newAssitPackage.contentEquals(DefaultAssistPreference.ITEM_NONE_VALUE)) {
|
||||
setDefaultAssist(DefaultAssistPreference.ITEM_NONE_VALUE);
|
||||
return false;
|
||||
}
|
||||
|
||||
final String currentPackage = mDefaultAssitPref.getValue();
|
||||
if (currentPackage == null || !newAssitPackage.contentEquals(currentPackage)) {
|
||||
confirmNewAssist(newAssitPackage);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void updateUi() {
|
||||
mDefaultAssitPref.refreshAssistApps();
|
||||
|
||||
final ComponentName currentAssist = mDefaultAssitPref.getCurrentAssist();
|
||||
final boolean hasAssistant = currentAssist != null;
|
||||
if (hasAssistant) {
|
||||
getPreferenceScreen().addPreference(mContextPref);
|
||||
} else {
|
||||
getPreferenceScreen().removePreference(mContextPref);
|
||||
}
|
||||
|
||||
mVoiceInputPref.setAssistRestrict(currentAssist);
|
||||
mVoiceInputPref.refreshVoiceInputs();
|
||||
}
|
||||
|
||||
private void confirmNewAssist(final String newAssitPackage) {
|
||||
final int selected = mDefaultAssitPref.findIndexOfValue(newAssitPackage);
|
||||
final CharSequence appLabel = mDefaultAssitPref.getEntries()[selected];
|
||||
|
||||
final String title = getString(R.string.assistant_security_warning_title, appLabel);
|
||||
final String message = getString(R.string.assistant_security_warning, appLabel);
|
||||
|
||||
final DialogInterface.OnClickListener onAgree = new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
setDefaultAssist(newAssitPackage);
|
||||
}
|
||||
};
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
builder.setTitle(title)
|
||||
.setMessage(message)
|
||||
.setCancelable(true)
|
||||
.setPositiveButton(R.string.assistant_security_warning_agree, onAgree)
|
||||
.setNegativeButton(R.string.assistant_security_warning_disagree, null);
|
||||
AlertDialog dialog = builder.create();
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void setDefaultAssist(String assistPackage) {
|
||||
mDefaultAssitPref.setValue(assistPackage);
|
||||
updateUi();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user