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