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 } 57 58 /** {@hide} */ pulseOnNotificationEnabled(int user)59 public boolean pulseOnNotificationEnabled(int user) { 60 return boolSettingDefaultOn(Settings.Secure.DOZE_ENABLED, user) 61 && pulseOnNotificationAvailable(); 62 } 63 64 /** {@hide} */ pulseOnNotificationAvailable()65 public boolean pulseOnNotificationAvailable() { 66 return ambientDisplayAvailable(); 67 } 68 69 /** {@hide} */ pickupGestureEnabled(int user)70 public boolean pickupGestureEnabled(int user) { 71 return boolSettingDefaultOn(Settings.Secure.DOZE_PICK_UP_GESTURE, user) 72 && dozePickupSensorAvailable(); 73 } 74 75 /** {@hide} */ dozePickupSensorAvailable()76 public boolean dozePickupSensorAvailable() { 77 return mContext.getResources().getBoolean(R.bool.config_dozePulsePickup); 78 } 79 80 /** {@hide} */ tapGestureEnabled(int user)81 public boolean tapGestureEnabled(int user) { 82 return boolSettingDefaultOn(Settings.Secure.DOZE_TAP_SCREEN_GESTURE, user) 83 && tapSensorAvailable(); 84 } 85 86 /** {@hide} */ tapSensorAvailable()87 public boolean tapSensorAvailable() { 88 return !TextUtils.isEmpty(tapSensorType()); 89 } 90 91 /** {@hide} */ doubleTapGestureEnabled(int user)92 public boolean doubleTapGestureEnabled(int user) { 93 return boolSettingDefaultOn(Settings.Secure.DOZE_DOUBLE_TAP_GESTURE, user) 94 && doubleTapSensorAvailable(); 95 } 96 97 /** {@hide} */ doubleTapSensorAvailable()98 public boolean doubleTapSensorAvailable() { 99 return !TextUtils.isEmpty(doubleTapSensorType()); 100 } 101 102 /** {@hide} */ wakeScreenGestureAvailable()103 public boolean wakeScreenGestureAvailable() { 104 return mContext.getResources() 105 .getBoolean(R.bool.config_dozeWakeLockScreenSensorAvailable); 106 } 107 108 /** {@hide} */ wakeLockScreenGestureEnabled(int user)109 public boolean wakeLockScreenGestureEnabled(int user) { 110 return boolSettingDefaultOn(Settings.Secure.DOZE_WAKE_LOCK_SCREEN_GESTURE, user) 111 && wakeScreenGestureAvailable(); 112 } 113 114 /** {@hide} */ wakeDisplayGestureEnabled(int user)115 public boolean wakeDisplayGestureEnabled(int user) { 116 return boolSettingDefaultOn(Settings.Secure.DOZE_WAKE_DISPLAY_GESTURE, user) 117 && wakeScreenGestureAvailable(); 118 } 119 120 /** {@hide} */ getWakeLockScreenDebounce()121 public long getWakeLockScreenDebounce() { 122 return mContext.getResources().getInteger(R.integer.config_dozeWakeLockScreenDebounce); 123 } 124 125 /** {@hide} */ doubleTapSensorType()126 public String doubleTapSensorType() { 127 return mContext.getResources().getString(R.string.config_dozeDoubleTapSensorType); 128 } 129 130 /** {@hide} */ tapSensorType()131 public String tapSensorType() { 132 return mContext.getResources().getString(R.string.config_dozeTapSensorType); 133 } 134 135 /** {@hide} */ longPressSensorType()136 public String longPressSensorType() { 137 return mContext.getResources().getString(R.string.config_dozeLongPressSensorType); 138 } 139 140 /** {@hide} */ pulseOnLongPressEnabled(int user)141 public boolean pulseOnLongPressEnabled(int user) { 142 return pulseOnLongPressAvailable() && boolSettingDefaultOff( 143 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, user); 144 } 145 pulseOnLongPressAvailable()146 private boolean pulseOnLongPressAvailable() { 147 return !TextUtils.isEmpty(longPressSensorType()); 148 } 149 150 /** 151 * Returns if Always-on-Display functionality is enabled on the display for a specified user. 152 * 153 * {@hide} 154 */ 155 @TestApi alwaysOnEnabled(int user)156 public boolean alwaysOnEnabled(int user) { 157 return boolSetting(Settings.Secure.DOZE_ALWAYS_ON, user, mAlwaysOnByDefault ? 1 : 0) 158 && alwaysOnAvailable() && !accessibilityInversionEnabled(user); 159 } 160 161 /** 162 * Returns if Always-on-Display functionality is available on the display. 163 * 164 * {@hide} 165 */ 166 @TestApi alwaysOnAvailable()167 public boolean alwaysOnAvailable() { 168 return (alwaysOnDisplayDebuggingEnabled() || alwaysOnDisplayAvailable()) 169 && ambientDisplayAvailable(); 170 } 171 172 /** 173 * Returns if Always-on-Display functionality is available on the display for a specified user. 174 * 175 * {@hide} 176 */ 177 @TestApi alwaysOnAvailableForUser(int user)178 public boolean alwaysOnAvailableForUser(int user) { 179 return alwaysOnAvailable() && !accessibilityInversionEnabled(user); 180 } 181 182 /** {@hide} */ ambientDisplayComponent()183 public String ambientDisplayComponent() { 184 return mContext.getResources().getString(R.string.config_dozeComponent); 185 } 186 187 /** {@hide} */ accessibilityInversionEnabled(int user)188 public boolean accessibilityInversionEnabled(int user) { 189 return boolSettingDefaultOff(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, user); 190 } 191 192 /** {@hide} */ ambientDisplayAvailable()193 public boolean ambientDisplayAvailable() { 194 return !TextUtils.isEmpty(ambientDisplayComponent()); 195 } 196 alwaysOnDisplayAvailable()197 private boolean alwaysOnDisplayAvailable() { 198 return mContext.getResources().getBoolean(R.bool.config_dozeAlwaysOnDisplayAvailable); 199 } 200 alwaysOnDisplayDebuggingEnabled()201 private boolean alwaysOnDisplayDebuggingEnabled() { 202 return SystemProperties.getBoolean("debug.doze.aod", false) && Build.IS_DEBUGGABLE; 203 } 204 boolSettingDefaultOn(String name, int user)205 private boolean boolSettingDefaultOn(String name, int user) { 206 return boolSetting(name, user, 1); 207 } 208 boolSettingDefaultOff(String name, int user)209 private boolean boolSettingDefaultOff(String name, int user) { 210 return boolSetting(name, user, 0); 211 } 212 boolSetting(String name, int user, int def)213 private boolean boolSetting(String name, int user, int def) { 214 return Settings.Secure.getIntForUser(mContext.getContentResolver(), name, def, user) != 0; 215 } 216 } 217