• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.annotation.UserIdInt;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.os.UserHandle;
24 import android.provider.Settings;
25 import android.support.v7.preference.Preference;
26 import android.support.annotation.VisibleForTesting;
27 import android.text.TextUtils;
28 
29 import com.android.internal.hardware.AmbientDisplayConfiguration;
30 import com.android.settings.R;
31 import com.android.settings.search.DatabaseIndexingUtils;
32 import com.android.settings.search.InlineSwitchPayload;
33 import com.android.settings.search.ResultPayload;
34 
35 import static android.provider.Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP;
36 
37 public class DoubleTapScreenPreferenceController extends GesturePreferenceController {
38 
39     private final int ON = 1;
40     private final int OFF = 0;
41 
42     private static final String PREF_KEY_VIDEO = "gesture_double_tap_screen_video";
43     private final String mDoubleTapScreenPrefKey;
44 
45     private final String SECURE_KEY = DOZE_PULSE_ON_DOUBLE_TAP;
46 
47     private AmbientDisplayConfiguration mAmbientConfig;
48     @UserIdInt
49     private final int mUserId;
50 
DoubleTapScreenPreferenceController(Context context, String key)51     public DoubleTapScreenPreferenceController(Context context, String key) {
52         super(context, key);
53         mUserId = UserHandle.myUserId();
54         mDoubleTapScreenPrefKey = key;
55     }
56 
setConfig(AmbientDisplayConfiguration config)57     public DoubleTapScreenPreferenceController setConfig(AmbientDisplayConfiguration config) {
58         mAmbientConfig = config;
59         return this;
60     }
61 
isSuggestionComplete(Context context, SharedPreferences prefs)62     public static boolean isSuggestionComplete(Context context, SharedPreferences prefs) {
63         return isSuggestionComplete(new AmbientDisplayConfiguration(context), prefs);
64     }
65 
66     @VisibleForTesting
isSuggestionComplete(AmbientDisplayConfiguration config, SharedPreferences prefs)67     static boolean isSuggestionComplete(AmbientDisplayConfiguration config,
68             SharedPreferences prefs) {
69         return !config.pulseOnDoubleTapAvailable()
70                 || prefs.getBoolean(DoubleTapScreenSettings.PREF_KEY_SUGGESTION_COMPLETE, false);
71     }
72 
73     @Override
getAvailabilityStatus()74     public int getAvailabilityStatus() {
75         if (mAmbientConfig == null) {
76             mAmbientConfig = new AmbientDisplayConfiguration(mContext);
77         }
78 
79         // No hardware support for Double Tap
80         if (!mAmbientConfig.doubleTapSensorAvailable()) {
81             return UNSUPPORTED_ON_DEVICE;
82         }
83 
84         // Can't change Double Tap when AOD is enabled.
85         if (!mAmbientConfig.ambientDisplayAvailable()) {
86             return DISABLED_DEPENDENT_SETTING;
87         }
88 
89         return AVAILABLE;
90     }
91 
92     @Override
isSliceable()93     public boolean isSliceable() {
94         return TextUtils.equals(getPreferenceKey(), "gesture_double_tap_screen");
95     }
96 
97     @Override
setChecked(boolean isChecked)98     public boolean setChecked(boolean isChecked) {
99         return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY,
100                 isChecked ? ON : OFF);
101     }
102 
103     @Override
getVideoPrefKey()104     protected String getVideoPrefKey() {
105         return PREF_KEY_VIDEO;
106     }
107 
108     @Override
isChecked()109     public boolean isChecked() {
110         return mAmbientConfig.pulseOnDoubleTapEnabled(mUserId);
111     }
112 
113     @Override
114     //TODO (b/69808376): Remove result payload
getResultPayload()115     public ResultPayload getResultPayload() {
116         final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
117                 DoubleTapScreenSettings.class.getName(), mDoubleTapScreenPrefKey,
118                 mContext.getString(R.string.display_settings));
119 
120         return new InlineSwitchPayload(SECURE_KEY, ResultPayload.SettingsSource.SECURE,
121                 ON /* onValue */, intent, isAvailable(), ON /* defaultValue */);
122     }
123 
124     @Override
canHandleClicks()125     protected boolean canHandleClicks() {
126         return !mAmbientConfig.alwaysOnEnabled(mUserId);
127     }
128 }