• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.assist;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.os.UserHandle;
23 import android.provider.Settings;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceScreen;
26 import android.support.v7.preference.TwoStatePreference;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.internal.app.AssistUtils;
30 import com.android.settings.core.PreferenceController;
31 import com.android.settings.core.lifecycle.Lifecycle;
32 import com.android.settings.core.lifecycle.LifecycleObserver;
33 import com.android.settings.core.lifecycle.events.OnPause;
34 import com.android.settings.core.lifecycle.events.OnResume;
35 
36 import java.util.Arrays;
37 import java.util.List;
38 
39 public class AssistFlashScreenPreferenceController extends PreferenceController
40         implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause {
41 
42     private static final String KEY_FLASH = "flash";
43 
44     private final AssistUtils mAssistUtils;
45     private final SettingObserver mSettingObserver;
46     private PreferenceScreen mScreen;
47     private Preference mPreference;
48 
AssistFlashScreenPreferenceController(Context context, Lifecycle lifecycle)49     public AssistFlashScreenPreferenceController(Context context, Lifecycle lifecycle) {
50         super(context);
51         mAssistUtils = new AssistUtils(context);
52         mSettingObserver = new SettingObserver();
53 
54         if (lifecycle != null) {
55             lifecycle.addObserver(this);
56         }
57     }
58 
59     @Override
isAvailable()60     public boolean isAvailable() {
61         return getCurrentAssist() != null && allowDisablingAssistDisclosure();
62     }
63 
64     @Override
getPreferenceKey()65     public String getPreferenceKey() {
66         return KEY_FLASH;
67     }
68 
69     @Override
displayPreference(PreferenceScreen screen)70     public void displayPreference(PreferenceScreen screen) {
71         mScreen = screen;
72         mPreference = screen.findPreference(getPreferenceKey());
73         super.displayPreference(screen);
74     }
75 
76     @Override
onResume()77     public void onResume() {
78         mSettingObserver.register(mContext.getContentResolver(), true);
79         updatePreference();
80     }
81 
82     @Override
updateState(Preference preference)83     public void updateState(Preference preference) {
84         updatePreference();
85     }
86 
87     @Override
onPause()88     public void onPause() {
89         mSettingObserver.register(mContext.getContentResolver(), false);
90     }
91 
92     @Override
onPreferenceChange(Preference preference, Object newValue)93     public boolean onPreferenceChange(Preference preference, Object newValue) {
94         Settings.Secure.putInt(mContext.getContentResolver(),
95                 Settings.Secure.ASSIST_DISCLOSURE_ENABLED,
96                 (boolean) newValue ? 1 : 0);
97         return true;
98     }
99 
updatePreference()100     private void updatePreference() {
101         if (mPreference == null || !(mPreference instanceof TwoStatePreference)) {
102             return;
103         }
104         if (isAvailable()) {
105             if (mScreen.findPreference(getPreferenceKey()) == null) {
106                 // add it if it's not on scree
107                 mScreen.addPreference(mPreference);
108             }
109         } else {
110             mScreen.removePreference(mPreference);
111         }
112         ComponentName assistant = getCurrentAssist();
113 
114         boolean isContextChecked = AssistContextPreferenceController.isChecked(mContext);
115 
116         mPreference.setEnabled(isContextChecked && isPreInstalledAssistant(assistant));
117         ((TwoStatePreference) mPreference).setChecked(willShowFlash(assistant));
118     }
119 
120     @VisibleForTesting
willShowFlash(ComponentName assistant)121     boolean willShowFlash(ComponentName assistant) {
122         return AssistUtils.shouldDisclose(mContext, assistant);
123     }
124 
125     @VisibleForTesting
isPreInstalledAssistant(ComponentName assistant)126     boolean isPreInstalledAssistant(ComponentName assistant) {
127         return AssistUtils.isPreinstalledAssistant(mContext, assistant);
128     }
129 
130     @VisibleForTesting
allowDisablingAssistDisclosure()131     boolean allowDisablingAssistDisclosure() {
132         return AssistUtils.allowDisablingAssistDisclosure(mContext);
133     }
134 
getCurrentAssist()135     private ComponentName getCurrentAssist() {
136         return mAssistUtils.getAssistComponentForUser(UserHandle.myUserId());
137     }
138 
139     class SettingObserver extends AssistSettingObserver {
140 
141         private final Uri URI =
142                 Settings.Secure.getUriFor(Settings.Secure.ASSIST_DISCLOSURE_ENABLED);
143         private final Uri CONTEXT_URI =
144                 Settings.Secure.getUriFor(Settings.Secure.ASSIST_STRUCTURE_ENABLED);
145 
146         @Override
getSettingUris()147         protected List<Uri> getSettingUris() {
148             return Arrays.asList(URI, CONTEXT_URI);
149         }
150 
151         @Override
onSettingChange()152         public void onSettingChange() {
153             updatePreference();
154         }
155     }
156 
157 
158 }
159