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