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