OSDN Git Service

am 66b5a58a: Merge "Fix init order so we have something to measure." into mnc-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / applications / ManageAssist.java
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16
17 package com.android.settings.applications;
18
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;
26
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;
32
33 /**
34  * Settings screen to manage everything about assist.
35  */
36 public class ManageAssist extends SettingsPreferenceFragment
37         implements Preference.OnPreferenceChangeListener {
38
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";
43
44     private DefaultAssistPreference mDefaultAssitPref;
45     private SwitchPreference mContextPref;
46     private SwitchPreference mScreenshotPref;
47     private VoiceInputListPreference mVoiceInputPref;
48
49     @Override
50     public void onCreate(Bundle icicle) {
51         super.onCreate(icicle);
52         addPreferencesFromResource(R.xml.manage_assist);
53
54         mDefaultAssitPref = (DefaultAssistPreference) findPreference(KEY_DEFAULT_ASSIST);
55         mDefaultAssitPref.setOnPreferenceChangeListener(this);
56
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);
61
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);
66
67         mVoiceInputPref = (VoiceInputListPreference) findPreference(KEY_VOICE_INPUT);
68         updateUi();
69     }
70
71     @Override
72     protected int getMetricsCategory() {
73         return MetricsLogger.APPLICATIONS_MANAGE_ASSIST;
74     }
75
76     @Override
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);
81             return true;
82         }
83         if (preference == mScreenshotPref) {
84             Settings.Secure.putInt(getContentResolver(), Settings.Secure.ASSIST_SCREENSHOT_ENABLED,
85                     (boolean) newValue ? 1 : 0);
86             return true;
87         }
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);
93                 return false;
94             }
95
96             final String currentPackage = mDefaultAssitPref.getValue();
97             if (currentPackage == null || !newAssitPackage.contentEquals(currentPackage)) {
98                 confirmNewAssist(newAssitPackage);
99             }
100             return false;
101         }
102         return false;
103     }
104
105     private void updateUi() {
106         mDefaultAssitPref.refreshAssistApps();
107
108         final ComponentName currentAssist = mDefaultAssitPref.getCurrentAssist();
109         final boolean hasAssistant = currentAssist != null;
110         if (hasAssistant) {
111             getPreferenceScreen().addPreference(mContextPref);
112         } else {
113             getPreferenceScreen().removePreference(mContextPref);
114         }
115
116         mVoiceInputPref.setAssistRestrict(currentAssist);
117         mVoiceInputPref.refreshVoiceInputs();
118     }
119
120     private void confirmNewAssist(final String newAssitPackage) {
121         final int selected = mDefaultAssitPref.findIndexOfValue(newAssitPackage);
122         final CharSequence appLabel = mDefaultAssitPref.getEntries()[selected];
123
124         final String title = getString(R.string.assistant_security_warning_title, appLabel);
125         final String message = getString(R.string.assistant_security_warning, appLabel);
126
127         final DialogInterface.OnClickListener onAgree = new DialogInterface.OnClickListener() {
128             @Override
129             public void onClick(DialogInterface dialog, int which) {
130                 setDefaultAssist(newAssitPackage);
131             }
132         };
133
134         AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
135         builder.setTitle(title)
136                 .setMessage(message)
137                 .setCancelable(true)
138                 .setPositiveButton(R.string.assistant_security_warning_agree, onAgree)
139                 .setNegativeButton(R.string.assistant_security_warning_disagree, null);
140         AlertDialog dialog = builder.create();
141         dialog.show();
142     }
143
144     private void setDefaultAssist(String assistPackage) {
145         mDefaultAssitPref.setValue(assistPackage);
146         updateUi();
147     }
148 }