1 /*
2  * Copyright (C) 2016 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.DOZE_DOUBLE_TAP_GESTURE;
20 
21 import android.annotation.UserIdInt;
22 import android.content.Context;
23 import android.content.SharedPreferences;
24 import android.hardware.display.AmbientDisplayConfiguration;
25 import android.os.UserHandle;
26 import android.provider.Settings;
27 import android.text.TextUtils;
28 
29 import androidx.annotation.VisibleForTesting;
30 
31 public class DoubleTapScreenPreferenceController extends GesturePreferenceController {
32 
33     private final int ON = 1;
34     private final int OFF = 0;
35 
36     private static final String PREF_KEY_VIDEO = "gesture_double_tap_screen_video";
37 
38     private final String SECURE_KEY = DOZE_DOUBLE_TAP_GESTURE;
39 
40     private AmbientDisplayConfiguration mAmbientConfig;
41     @UserIdInt
42     private final int mUserId;
43 
DoubleTapScreenPreferenceController(Context context, String key)44     public DoubleTapScreenPreferenceController(Context context, String key) {
45         super(context, key);
46         mUserId = UserHandle.myUserId();
47     }
48 
setConfig(AmbientDisplayConfiguration config)49     public DoubleTapScreenPreferenceController setConfig(AmbientDisplayConfiguration config) {
50         mAmbientConfig = config;
51         return this;
52     }
53 
isSuggestionComplete(Context context, SharedPreferences prefs)54     public static boolean isSuggestionComplete(Context context, SharedPreferences prefs) {
55         return isSuggestionComplete(new AmbientDisplayConfiguration(context), prefs);
56     }
57 
58     @VisibleForTesting
isSuggestionComplete(AmbientDisplayConfiguration config, SharedPreferences prefs)59     static boolean isSuggestionComplete(AmbientDisplayConfiguration config,
60             SharedPreferences prefs) {
61         return !config.doubleTapSensorAvailable()
62                 || prefs.getBoolean(DoubleTapScreenSettings.PREF_KEY_SUGGESTION_COMPLETE, false);
63     }
64 
65     @Override
getAvailabilityStatus()66     public int getAvailabilityStatus() {
67         // No hardware support for Double Tap
68         if (!getAmbientConfig().doubleTapSensorAvailable()) {
69             return UNSUPPORTED_ON_DEVICE;
70         }
71 
72         return AVAILABLE;
73     }
74 
75     @Override
isSliceable()76     public boolean isSliceable() {
77         return TextUtils.equals(getPreferenceKey(), "gesture_double_tap_screen");
78     }
79 
80     @Override
setChecked(boolean isChecked)81     public boolean setChecked(boolean isChecked) {
82         return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY,
83                 isChecked ? ON : OFF);
84     }
85 
86     @Override
getVideoPrefKey()87     protected String getVideoPrefKey() {
88         return PREF_KEY_VIDEO;
89     }
90 
91     @Override
isChecked()92     public boolean isChecked() {
93         return getAmbientConfig().doubleTapGestureEnabled(mUserId);
94     }
95 
getAmbientConfig()96     private AmbientDisplayConfiguration getAmbientConfig() {
97         if (mAmbientConfig == null) {
98             mAmbientConfig = new AmbientDisplayConfiguration(mContext);
99         }
100         return mAmbientConfig;
101     }
102 }
103