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
isPublicSlice()81     public boolean isPublicSlice() {
82         return true;
83     }
84 
85     @Override
setChecked(boolean isChecked)86     public boolean setChecked(boolean isChecked) {
87         return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY,
88                 isChecked ? ON : OFF);
89     }
90 
91     @Override
getVideoPrefKey()92     protected String getVideoPrefKey() {
93         return PREF_KEY_VIDEO;
94     }
95 
96     @Override
isChecked()97     public boolean isChecked() {
98         return getAmbientConfig().doubleTapGestureEnabled(mUserId);
99     }
100 
getAmbientConfig()101     private AmbientDisplayConfiguration getAmbientConfig() {
102         if (mAmbientConfig == null) {
103             mAmbientConfig = new AmbientDisplayConfiguration(mContext);
104         }
105         return mAmbientConfig;
106     }
107 }
108