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