2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.settings.applications;
19 import android.app.AlertDialog;
20 import android.content.ComponentName;
21 import android.content.DialogInterface;
22 import android.os.Bundle;
23 import android.preference.Preference;
24 import android.preference.SwitchPreference;
25 import android.provider.Settings;
27 import com.android.internal.logging.MetricsLogger;
28 import com.android.settings.InstrumentedFragment;
29 import com.android.settings.R;
30 import com.android.settings.SettingsPreferenceFragment;
31 import com.android.settings.voice.VoiceInputListPreference;
34 * Settings screen to manage everything about assist.
36 public class ManageAssist extends SettingsPreferenceFragment
37 implements Preference.OnPreferenceChangeListener {
39 private static final String KEY_DEFAULT_ASSIST = "default_assist";
40 private static final String KEY_CONTEXT = "context";
41 private static final String KEY_SCREENSHOT = "screenshot";
42 private static final String KEY_VOICE_INPUT = "voice_input_settings";
44 private DefaultAssistPreference mDefaultAssitPref;
45 private SwitchPreference mContextPref;
46 private SwitchPreference mScreenshotPref;
47 private VoiceInputListPreference mVoiceInputPref;
50 public void onCreate(Bundle icicle) {
51 super.onCreate(icicle);
52 addPreferencesFromResource(R.xml.manage_assist);
54 mDefaultAssitPref = (DefaultAssistPreference) findPreference(KEY_DEFAULT_ASSIST);
55 mDefaultAssitPref.setOnPreferenceChangeListener(this);
57 mContextPref = (SwitchPreference) findPreference(KEY_CONTEXT);
58 mContextPref.setChecked(Settings.Secure.getInt(getContentResolver(),
59 Settings.Secure.ASSIST_STRUCTURE_ENABLED, 1) != 0);
60 mContextPref.setOnPreferenceChangeListener(this);
62 mScreenshotPref = (SwitchPreference) findPreference(KEY_SCREENSHOT);
63 mScreenshotPref.setChecked(Settings.Secure.getInt(getContentResolver(),
64 Settings.Secure.ASSIST_SCREENSHOT_ENABLED, 1) != 0);
65 mScreenshotPref.setOnPreferenceChangeListener(this);
67 mVoiceInputPref = (VoiceInputListPreference) findPreference(KEY_VOICE_INPUT);
72 protected int getMetricsCategory() {
73 return MetricsLogger.APPLICATIONS_MANAGE_ASSIST;
77 public boolean onPreferenceChange(Preference preference, Object newValue) {
78 if (preference == mContextPref) {
79 Settings.Secure.putInt(getContentResolver(), Settings.Secure.ASSIST_STRUCTURE_ENABLED,
80 (boolean) newValue ? 1 : 0);
83 if (preference == mScreenshotPref) {
84 Settings.Secure.putInt(getContentResolver(), Settings.Secure.ASSIST_SCREENSHOT_ENABLED,
85 (boolean) newValue ? 1 : 0);
88 if (preference == mDefaultAssitPref) {
89 String newAssitPackage = (String)newValue;
90 if (newAssitPackage == null ||
91 newAssitPackage.contentEquals(DefaultAssistPreference.ITEM_NONE_VALUE)) {
92 setDefaultAssist(DefaultAssistPreference.ITEM_NONE_VALUE);
96 final String currentPackage = mDefaultAssitPref.getValue();
97 if (currentPackage == null || !newAssitPackage.contentEquals(currentPackage)) {
98 confirmNewAssist(newAssitPackage);
105 private void updateUi() {
106 mDefaultAssitPref.refreshAssistApps();
108 final ComponentName currentAssist = mDefaultAssitPref.getCurrentAssist();
109 final boolean hasAssistant = currentAssist != null;
111 getPreferenceScreen().addPreference(mContextPref);
113 getPreferenceScreen().removePreference(mContextPref);
116 mVoiceInputPref.setAssistRestrict(currentAssist);
117 mVoiceInputPref.refreshVoiceInputs();
120 private void confirmNewAssist(final String newAssitPackage) {
121 final int selected = mDefaultAssitPref.findIndexOfValue(newAssitPackage);
122 final CharSequence appLabel = mDefaultAssitPref.getEntries()[selected];
124 final String title = getString(R.string.assistant_security_warning_title, appLabel);
125 final String message = getString(R.string.assistant_security_warning, appLabel);
127 final DialogInterface.OnClickListener onAgree = new DialogInterface.OnClickListener() {
129 public void onClick(DialogInterface dialog, int which) {
130 setDefaultAssist(newAssitPackage);
134 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
135 builder.setTitle(title)
138 .setPositiveButton(R.string.assistant_security_warning_agree, onAgree)
139 .setNegativeButton(R.string.assistant_security_warning_disagree, null);
140 AlertDialog dialog = builder.create();
144 private void setDefaultAssist(String assistPackage) {
145 mDefaultAssitPref.setValue(assistPackage);