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 android.content.Context;
17 import android.hardware.Sensor;
18 import android.hardware.SensorManager;
19 import android.provider.Settings;
20 import android.support.v14.preference.SwitchPreference;
21 import android.support.v7.preference.Preference;
22 
23 import com.android.settings.core.PreferenceController;
24 
25 import static android.provider.Settings.Secure.WAKE_GESTURE_ENABLED;
26 
27 public class LiftToWakePreferenceController extends PreferenceController implements
28         Preference.OnPreferenceChangeListener {
29 
30     private static final String KEY_LIFT_TO_WAKE = "lift_to_wake";
31 
LiftToWakePreferenceController(Context context)32     public LiftToWakePreferenceController(Context context) {
33         super(context);
34     }
35 
36     @Override
isAvailable()37     public boolean isAvailable() {
38         SensorManager sensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
39         return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null;
40     }
41 
42     @Override
getPreferenceKey()43     public String getPreferenceKey() {
44         return KEY_LIFT_TO_WAKE;
45     }
46 
47     @Override
onPreferenceChange(Preference preference, Object newValue)48     public boolean onPreferenceChange(Preference preference, Object newValue) {
49         boolean value = (Boolean) newValue;
50         Settings.Secure.putInt(mContext.getContentResolver(), WAKE_GESTURE_ENABLED, value ? 1 : 0);
51         return true;
52     }
53 
54     @Override
updateState(Preference preference)55     public void updateState(Preference preference) {
56         int value = Settings.Secure.getInt(mContext.getContentResolver(), WAKE_GESTURE_ENABLED, 0);
57         ((SwitchPreference) preference).setChecked(value != 0);
58     }
59 }
60