• 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.gestures;
18 
19 import static android.provider.Settings.Secure.ASSIST_GESTURE_ENABLED;
20 import static android.provider.Settings.Secure.ASSIST_GESTURE_SILENCE_ALERTS_ENABLED;
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.provider.Settings;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceScreen;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.settings.R;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settings.search.DatabaseIndexingUtils;
32 import com.android.settings.search.InlineSwitchPayload;
33 import com.android.settings.search.ResultPayload;
34 
35 public class AssistGestureSettingsPreferenceController extends GesturePreferenceController {
36 
37     private static final String PREF_KEY_VIDEO = "gesture_assist_video";
38 
39     private static final String SECURE_KEY_ASSIST = ASSIST_GESTURE_ENABLED;
40     private static final String SECURE_KEY_SILENCE = ASSIST_GESTURE_SILENCE_ALERTS_ENABLED;
41     private static final int ON = 1;
42     private static final int OFF = 0;
43 
44     private final String mAssistGesturePrefKey;
45     private final AssistGestureFeatureProvider mFeatureProvider;
46     private boolean mWasAvailable;
47 
48     private PreferenceScreen mScreen;
49     private Preference mPreference;
50 
51     @VisibleForTesting
52     boolean mAssistOnly;
53 
AssistGestureSettingsPreferenceController(Context context, String key)54     public AssistGestureSettingsPreferenceController(Context context,
55             String key) {
56         super(context, key);
57         mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider();
58         mWasAvailable = isAvailable();
59         mAssistGesturePrefKey = key;
60     }
61 
62     @Override
getAvailabilityStatus()63     public int getAvailabilityStatus() {
64         final boolean isAvailable = mAssistOnly ? mFeatureProvider.isSupported(mContext)
65                 : mFeatureProvider.isSensorAvailable(mContext);
66         return isAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
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         if (mWasAvailable != isAvailable()) {
79             // Only update the preference visibility if the availability has changed -- otherwise
80             // the preference may be incorrectly added to screens with collapsed sections.
81             updatePreference();
82             mWasAvailable = isAvailable();
83         }
84     }
85 
setAssistOnly(boolean assistOnly)86     public AssistGestureSettingsPreferenceController setAssistOnly(boolean assistOnly) {
87         mAssistOnly = assistOnly;
88         return this;
89     }
90 
updatePreference()91     private void updatePreference() {
92         if (mPreference == null) {
93             return;
94         }
95 
96         if (isAvailable()) {
97             if (mScreen.findPreference(getPreferenceKey()) == null) {
98                 mScreen.addPreference(mPreference);
99             }
100         } else {
101             mScreen.removePreference(mPreference);
102         }
103     }
104 
isAssistGestureEnabled()105     private boolean isAssistGestureEnabled() {
106         return Settings.Secure.getInt(mContext.getContentResolver(),
107                 SECURE_KEY_ASSIST, ON) != 0;
108     }
109 
isSilenceGestureEnabled()110     private boolean isSilenceGestureEnabled() {
111         return Settings.Secure.getInt(mContext.getContentResolver(),
112                 SECURE_KEY_SILENCE, ON) != 0;
113     }
114 
115     @Override
setChecked(boolean isChecked)116     public boolean setChecked(boolean isChecked) {
117         return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY_ASSIST,
118                 isChecked ? ON : OFF);
119     }
120 
121     @Override
getVideoPrefKey()122     protected String getVideoPrefKey() {
123         return PREF_KEY_VIDEO;
124     }
125 
126     @Override
getSummary()127     public CharSequence getSummary() {
128         boolean isEnabled = isAssistGestureEnabled() && mFeatureProvider.isSupported(mContext);
129         if (!mAssistOnly) {
130             isEnabled = isEnabled || isSilenceGestureEnabled();
131         }
132         return mContext.getText(
133                 isEnabled ? R.string.gesture_setting_on : R.string.gesture_setting_off);
134     }
135 
136     @Override
isChecked()137     public boolean isChecked() {
138         return Settings.Secure.getInt(mContext.getContentResolver(), SECURE_KEY_ASSIST, OFF) == ON;
139     }
140 
141     @Override
142     //TODO (b/69808376): Remove result payload
getResultPayload()143     public ResultPayload getResultPayload() {
144         final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
145                 AssistGestureSettings.class.getName(), mAssistGesturePrefKey,
146                 mContext.getString(R.string.display_settings));
147 
148         return new InlineSwitchPayload(SECURE_KEY_ASSIST, ResultPayload.SettingsSource.SECURE,
149                 ON /* onValue */, intent, isAvailable(), ON /* defaultValue */);
150     }
151 }
152