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.Context;
20 import android.net.Uri;
21 import android.os.UserHandle;
22 import android.provider.Settings;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 import androidx.preference.TwoStatePreference;
27 
28 import com.android.internal.app.AssistUtils;
29 import com.android.settings.core.PreferenceControllerMixin;
30 import com.android.settingslib.core.AbstractPreferenceController;
31 import com.android.settingslib.core.lifecycle.Lifecycle;
32 import com.android.settingslib.core.lifecycle.LifecycleObserver;
33 import com.android.settingslib.core.lifecycle.events.OnPause;
34 import com.android.settingslib.core.lifecycle.events.OnResume;
35 
36 import java.util.Arrays;
37 import java.util.List;
38 
39 public class AssistContextPreferenceController extends AbstractPreferenceController
40         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
41         LifecycleObserver, OnResume, OnPause {
42 
43     private static final String KEY_CONTEXT = "context";
44 
45     private final AssistUtils mAssistUtils;
46     private final SettingObserver mSettingObserver;
47 
48     private Preference mPreference;
49     private PreferenceScreen mScreen;
50 
AssistContextPreferenceController(Context context, Lifecycle lifecycle)51     public AssistContextPreferenceController(Context context, Lifecycle lifecycle) {
52         super(context);
53         mAssistUtils = new AssistUtils(context);
54         mSettingObserver = new SettingObserver();
55 
56         if (lifecycle != null) {
57             lifecycle.addObserver(this);
58         }
59     }
60 
61     @Override
isAvailable()62     public boolean isAvailable() {
63         return mAssistUtils.getAssistComponentForUser(UserHandle.myUserId()) != null;
64     }
65 
66     @Override
getPreferenceKey()67     public String getPreferenceKey() {
68         return KEY_CONTEXT;
69     }
70 
71     @Override
displayPreference(PreferenceScreen screen)72     public void displayPreference(PreferenceScreen screen) {
73         mScreen = screen;
74         mPreference = screen.findPreference(getPreferenceKey());
75         super.displayPreference(screen);
76     }
77 
78     @Override
onResume()79     public void onResume() {
80         mSettingObserver.register(mContext.getContentResolver(), true);
81         updatePreference();
82     }
83 
84     @Override
updateState(Preference preference)85     public void updateState(Preference preference) {
86         updatePreference();
87     }
88 
89     @Override
onPause()90     public void onPause() {
91         mSettingObserver.register(mContext.getContentResolver(), false);
92     }
93 
94 
updatePreference()95     private void updatePreference() {
96         if (mPreference == null || !(mPreference instanceof TwoStatePreference)) {
97             return;
98         }
99         if (isAvailable()) {
100             if (mScreen.findPreference(getPreferenceKey()) == null) {
101                 // add it if it's not on scree
102                 mScreen.addPreference(mPreference);
103             }
104         } else {
105             mScreen.removePreference(mPreference);
106         }
107 
108         ((TwoStatePreference) mPreference).setChecked(isChecked(mContext));
109     }
110 
111     @Override
onPreferenceChange(Preference preference, Object newValue)112     public boolean onPreferenceChange(Preference preference, Object newValue) {
113         Settings.Secure.putInt(mContext.getContentResolver(),
114                 Settings.Secure.ASSIST_STRUCTURE_ENABLED,
115                 (boolean) newValue ? 1 : 0);
116         return true;
117     }
118 
isChecked(Context context)119     static boolean isChecked(Context context) {
120         return Settings.Secure.getInt(context.getContentResolver(),
121                 Settings.Secure.ASSIST_STRUCTURE_ENABLED, 1) != 0;
122     }
123 
124     class SettingObserver extends AssistSettingObserver {
125 
126         private final Uri URI =
127                 Settings.Secure.getUriFor(Settings.Secure.ASSIST_STRUCTURE_ENABLED);
128 
129         @Override
getSettingUris()130         protected List<Uri> getSettingUris() {
131             return Arrays.asList(URI);
132         }
133 
134         @Override
onSettingChange()135         public void onSettingChange() {
136             updatePreference();
137         }
138     }
139 }
140