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 android.hardware.display; 18 19 import android.annotation.TestApi; 20 import android.content.Context; 21 import android.os.Build; 22 import android.os.SystemProperties; 23 import android.provider.Settings; 24 import android.text.TextUtils; 25 26 import com.android.internal.R; 27 28 /** 29 * AmbientDisplayConfiguration encapsulates reading access to the configuration of ambient display. 30 * 31 * {@hide} 32 */ 33 @TestApi 34 public class AmbientDisplayConfiguration { 35 36 private final Context mContext; 37 private final boolean mAlwaysOnByDefault; 38 39 /** {@hide} */ 40 @TestApi AmbientDisplayConfiguration(Context context)41 public AmbientDisplayConfiguration(Context context) { 42 mContext = context; 43 mAlwaysOnByDefault = mContext.getResources().getBoolean(R.bool.config_dozeAlwaysOnEnabled); 44 } 45 46 /** {@hide} */ enabled(int user)47 public boolean enabled(int user) { 48 return pulseOnNotificationEnabled(user) 49 || pulseOnLongPressEnabled(user) 50 || alwaysOnEnabled(user) 51 || wakeLockScreenGestureEnabled(user) 52 || wakeDisplayGestureEnabled(user) 53 || pickupGestureEnabled(user) 54 || tapGestureEnabled(user) 55 || doubleTapGestureEnabled(user) 56 || quickPickupSensorEnabled(user) 57 || screenOffUdfpsEnabled(user); 58 } 59 60 /** {@hide} */ pulseOnNotificationEnabled(int user)61 public boolean pulseOnNotificationEnabled(int user) { 62 return boolSettingDefaultOn(Settings.Secure.DOZE_ENABLED, user) 63 && pulseOnNotificationAvailable(); 64 } 65 66 /** {@hide} */ pulseOnNotificationAvailable()67 public boolean pulseOnNotificationAvailable() { 68 return ambientDisplayAvailable(); 69 } 70 71 /** {@hide} */ pickupGestureEnabled(int user)72 public boolean pickupGestureEnabled(int user) { 73 return boolSettingDefaultOn(Settings.Secure.DOZE_PICK_UP_GESTURE, user) 74 && dozePickupSensorAvailable(); 75 } 76 77 /** {@hide} */ dozePickupSensorAvailable()78 public boolean dozePickupSensorAvailable() { 79 return mContext.getResources().getBoolean(R.bool.config_dozePulsePickup); 80 } 81 82 /** {@hide} */ tapGestureEnabled(int user)83 public boolean tapGestureEnabled(int user) { 84 return boolSettingDefaultOn(Settings.Secure.DOZE_TAP_SCREEN_GESTURE, user) 85 && tapSensorAvailable(); 86 } 87 88 /** {@hide} */ tapSensorAvailable()89 public boolean tapSensorAvailable() { 90 return !TextUtils.isEmpty(tapSensorType()); 91 } 92 93 /** {@hide} */ doubleTapGestureEnabled(int user)94 public boolean doubleTapGestureEnabled(int user) { 95 return boolSettingDefaultOn(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, user) 96 && doubleTapSensorAvailable(); 97 } 98 99 /** {@hide} */ doubleTapSensorAvailable()100 public boolean doubleTapSensorAvailable() { 101 return !TextUtils.isEmpty(doubleTapSensorType()); 102 } 103 104 /** {@hide} */ quickPickupSensorEnabled(int user)105 public boolean quickPickupSensorEnabled(int user) { 106 return !TextUtils.isEmpty(quickPickupSensorType()) && !alwaysOnEnabled(user); 107 } 108 109 /** {@hide} */ screenOffUdfpsEnabled(int user)110 public boolean screenOffUdfpsEnabled(int user) { 111 return !TextUtils.isEmpty(udfpsLongPressSensorType()) 112 && boolSettingDefaultOff("screen_off_udfps_enabled", user); 113 } 114 115 /** {@hide} */ wakeScreenGestureAvailable()116 public boolean wakeScreenGestureAvailable() { 117 return mContext.getResources() 118 .getBoolean(R.bool.config_dozeWakeLockScreenSensorAvailable); 119 } 120 121 /** {@hide} */ wakeLockScreenGestureEnabled(int user)122 public boolean wakeLockScreenGestureEnabled(int user) { 123 return boolSettingDefaultOn(Settings.Secure.DOZE_WAKE_LOCK_SCREEN_GESTURE, user) 124 && wakeScreenGestureAvailable(); 125 } 126 127 /** {@hide} */ wakeDisplayGestureEnabled(int user)128 public boolean wakeDisplayGestureEnabled(int user) { 129 return boolSettingDefaultOn(Settings.Secure.DOZE_WAKE_DISPLAY_GESTURE, user) 130 && wakeScreenGestureAvailable(); 131 } 132 133 /** {@hide} */ getWakeLockScreenDebounce()134 public long getWakeLockScreenDebounce() { 135 return mContext.getResources().getInteger(R.integer.config_dozeWakeLockScreenDebounce); 136 } 137 138 /** {@hide} */ doubleTapSensorType()139 public String doubleTapSensorType() { 140 return mContext.getResources().getString(R.string.config_dozeDoubleTapSensorType); 141 } 142 143 /** {@hide} */ tapSensorType()144 public String tapSensorType() { 145 return mContext.getResources().getString(R.string.config_dozeTapSensorType); 146 } 147 148 /** {@hide} */ longPressSensorType()149 public String longPressSensorType() { 150 return mContext.getResources().getString(R.string.config_dozeLongPressSensorType); 151 } 152 153 /** {@hide} */ udfpsLongPressSensorType()154 public String udfpsLongPressSensorType() { 155 return mContext.getResources().getString(R.string.config_dozeUdfpsLongPressSensorType); 156 } 157 158 /** {@hide} */ quickPickupSensorType()159 public String quickPickupSensorType() { 160 return mContext.getResources().getString(R.string.config_quickPickupSensorType); 161 } 162 163 /** {@hide} */ pulseOnLongPressEnabled(int user)164 public boolean pulseOnLongPressEnabled(int user) { 165 return pulseOnLongPressAvailable() && boolSettingDefaultOff( 166 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, user); 167 } 168 pulseOnLongPressAvailable()169 private boolean pulseOnLongPressAvailable() { 170 return !TextUtils.isEmpty(longPressSensorType()); 171 } 172 173 /** 174 * Returns if Always-on-Display functionality is enabled on the display for a specified user. 175 * 176 * {@hide} 177 */ 178 @TestApi alwaysOnEnabled(int user)179 public boolean alwaysOnEnabled(int user) { 180 return boolSetting(Settings.Secure.DOZE_ALWAYS_ON, user, mAlwaysOnByDefault ? 1 : 0) 181 && alwaysOnAvailable() && !accessibilityInversionEnabled(user); 182 } 183 184 /** 185 * Returns if Always-on-Display functionality is available on the display. 186 * 187 * {@hide} 188 */ 189 @TestApi alwaysOnAvailable()190 public boolean alwaysOnAvailable() { 191 return (alwaysOnDisplayDebuggingEnabled() || alwaysOnDisplayAvailable()) 192 && ambientDisplayAvailable(); 193 } 194 195 /** 196 * Returns if Always-on-Display functionality is available on the display for a specified user. 197 * 198 * {@hide} 199 */ 200 @TestApi alwaysOnAvailableForUser(int user)201 public boolean alwaysOnAvailableForUser(int user) { 202 return alwaysOnAvailable() && !accessibilityInversionEnabled(user); 203 } 204 205 /** {@hide} */ ambientDisplayComponent()206 public String ambientDisplayComponent() { 207 return mContext.getResources().getString(R.string.config_dozeComponent); 208 } 209 210 /** {@hide} */ accessibilityInversionEnabled(int user)211 public boolean accessibilityInversionEnabled(int user) { 212 return boolSettingDefaultOff(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, user); 213 } 214 215 /** {@hide} */ ambientDisplayAvailable()216 public boolean ambientDisplayAvailable() { 217 return !TextUtils.isEmpty(ambientDisplayComponent()); 218 } 219 220 /** {@hide} */ dozeSuppressed(int user)221 public boolean dozeSuppressed(int user) { 222 return boolSettingDefaultOff(Settings.Secure.SUPPRESS_DOZE, user); 223 } 224 alwaysOnDisplayAvailable()225 private boolean alwaysOnDisplayAvailable() { 226 return mContext.getResources().getBoolean(R.bool.config_dozeAlwaysOnDisplayAvailable); 227 } 228 alwaysOnDisplayDebuggingEnabled()229 private boolean alwaysOnDisplayDebuggingEnabled() { 230 return SystemProperties.getBoolean("debug.doze.aod", false) && Build.IS_DEBUGGABLE; 231 } 232 boolSettingDefaultOn(String name, int user)233 private boolean boolSettingDefaultOn(String name, int user) { 234 return boolSetting(name, user, 1); 235 } 236 boolSettingDefaultOff(String name, int user)237 private boolean boolSettingDefaultOff(String name, int user) { 238 return boolSetting(name, user, 0); 239 } 240 boolSetting(String name, int user, int def)241 private boolean boolSetting(String name, int user, int def) { 242 return Settings.Secure.getIntForUser(mContext.getContentResolver(), name, def, user) != 0; 243 } 244 } 245