• 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.provider.Settings;
24 import android.util.Log;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.R;
31 import com.android.settings.overlay.FeatureFactory;
32 
33 public class AssistGestureSettingsPreferenceController extends GesturePreferenceController {
34 
35     private static final String TAG = "AssistGesture";
36     private static final String PREF_KEY_VIDEO = "gesture_assist_video";
37 
38     private static final String SECURE_KEY_ASSIST = ASSIST_GESTURE_ENABLED;
39     private static final String SECURE_KEY_SILENCE = ASSIST_GESTURE_SILENCE_ALERTS_ENABLED;
40     private static final int ON = 1;
41     private static final int OFF = 0;
42 
43     private final AssistGestureFeatureProvider mFeatureProvider;
44     private boolean mWasAvailable;
45 
46     private PreferenceScreen mScreen;
47     private Preference mPreference;
48 
49     @VisibleForTesting
50     boolean mAssistOnly;
51 
AssistGestureSettingsPreferenceController(Context context, String key)52     public AssistGestureSettingsPreferenceController(Context context, String key) {
53         super(context, key);
54         mFeatureProvider = FeatureFactory.getFactory(context).getAssistGestureFeatureProvider();
55         mWasAvailable = isAvailable();
56     }
57 
58     @Override
getAvailabilityStatus()59     public int getAvailabilityStatus() {
60         final boolean isSupported = mFeatureProvider.isSupported(mContext);
61         final boolean isSensorAvailable = mFeatureProvider.isSensorAvailable(mContext);
62         final boolean isAvailable = mAssistOnly ? isSupported : isSensorAvailable;
63         Log.d(TAG, "mAssistOnly:" + mAssistOnly + ", isSupported:" + isSupported
64                 + ", isSensorAvailable:" + isSensorAvailable);
65         return isAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
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
onStart()76     public void onStart() {
77         if (mWasAvailable != isAvailable()) {
78             // Only update the preference visibility if the availability has changed -- otherwise
79             // the preference may be incorrectly added to screens with collapsed sections.
80             updatePreference();
81             mWasAvailable = isAvailable();
82         }
83     }
84 
setAssistOnly(boolean assistOnly)85     public AssistGestureSettingsPreferenceController setAssistOnly(boolean assistOnly) {
86         mAssistOnly = assistOnly;
87         return this;
88     }
89 
updatePreference()90     private void updatePreference() {
91         if (mPreference == null) {
92             return;
93         }
94 
95         if (isAvailable()) {
96             if (mScreen.findPreference(getPreferenceKey()) == null) {
97                 mScreen.addPreference(mPreference);
98             }
99         } else {
100             mScreen.removePreference(mPreference);
101         }
102     }
103 
isAssistGestureEnabled()104     private boolean isAssistGestureEnabled() {
105         return Settings.Secure.getInt(mContext.getContentResolver(),
106                 SECURE_KEY_ASSIST, ON) != 0;
107     }
108 
isSilenceGestureEnabled()109     private boolean isSilenceGestureEnabled() {
110         return Settings.Secure.getInt(mContext.getContentResolver(),
111                 SECURE_KEY_SILENCE, ON) != 0;
112     }
113 
114     @Override
setChecked(boolean isChecked)115     public boolean setChecked(boolean isChecked) {
116         return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY_ASSIST,
117                 isChecked ? ON : OFF);
118     }
119 
120     @Override
getVideoPrefKey()121     protected String getVideoPrefKey() {
122         return PREF_KEY_VIDEO;
123     }
124 
125     @Override
getSummary()126     public CharSequence getSummary() {
127         boolean isEnabled = isAssistGestureEnabled() && mFeatureProvider.isSupported(mContext);
128         if (!mAssistOnly) {
129             isEnabled = isEnabled || isSilenceGestureEnabled();
130         }
131         return mContext.getText(
132                 isEnabled ? R.string.gesture_setting_on : R.string.gesture_setting_off);
133     }
134 
135     @Override
isChecked()136     public boolean isChecked() {
137         return Settings.Secure.getInt(mContext.getContentResolver(), SECURE_KEY_ASSIST, OFF) == ON;
138     }
139 }
140