1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.display;
15 
16 import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED;
17 
18 import android.content.Context;
19 import android.hardware.Sensor;
20 import android.hardware.SensorManager;
21 import android.provider.Settings;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.SwitchPreference;
25 
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settingslib.core.AbstractPreferenceController;
28 
29 public class LiftToWakePreferenceController extends AbstractPreferenceController implements
30         PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
31 
32     private static final String KEY_LIFT_TO_WAKE = "lift_to_wake";
33 
LiftToWakePreferenceController(Context context)34     public LiftToWakePreferenceController(Context context) {
35         super(context);
36     }
37 
38     @Override
isAvailable()39     public boolean isAvailable() {
40         SensorManager sensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
41         return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null;
42     }
43 
44     @Override
getPreferenceKey()45     public String getPreferenceKey() {
46         return KEY_LIFT_TO_WAKE;
47     }
48 
49     @Override
onPreferenceChange(Preference preference, Object newValue)50     public boolean onPreferenceChange(Preference preference, Object newValue) {
51         boolean value = (Boolean) newValue;
52         Settings.Secure.putInt(mContext.getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
53         return true;
54     }
55 
56     @Override
updateState(Preference preference)57     public void updateState(Preference preference) {
58         int value = Settings.Secure.getInt(mContext.getContentResolver(), WAKE_GESTURE_ENABLED, 0);
59         ((SwitchPreference) preference).setChecked(value != 0);
60     }
61 }
62