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