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.internal.hardware; 18 19 import com.android.internal.R; 20 21 import android.content.Context; 22 import android.os.Build; 23 import android.os.SystemProperties; 24 import android.provider.Settings; 25 import android.text.TextUtils; 26 27 public class AmbientDisplayConfiguration { 28 29 private final Context mContext; 30 31 public AmbientDisplayConfiguration(Context context) { 32 mContext = context; 33 } 34 35 public boolean enabled(int user) { 36 return pulseOnNotificationEnabled(user) 37 || pulseOnPickupEnabled(user) 38 || pulseOnDoubleTapEnabled(user) 39 || pulseOnLongPressEnabled(user) 40 || alwaysOnEnabled(user); 41 } 42 43 public boolean available() { 44 return pulseOnNotificationAvailable() || pulseOnPickupAvailable() 45 || pulseOnDoubleTapAvailable(); 46 } 47 48 public boolean pulseOnNotificationEnabled(int user) { 49 return boolSettingDefaultOn(Settings.Secure.DOZE_ENABLED, user) && pulseOnNotificationAvailable(); 50 } 51 52 public boolean pulseOnNotificationAvailable() { 53 return ambientDisplayAvailable(); 54 } 55 56 public boolean pulseOnPickupEnabled(int user) { 57 boolean settingEnabled = boolSettingDefaultOn(Settings.Secure.DOZE_PULSE_ON_PICK_UP, user); 58 return (settingEnabled || alwaysOnEnabled(user)) && pulseOnPickupAvailable(); 59 } 60 61 public boolean pulseOnPickupAvailable() { 62 return mContext.getResources().getBoolean(R.bool.config_dozePulsePickup) 63 && ambientDisplayAvailable(); 64 } 65 66 public boolean pulseOnPickupCanBeModified(int user) { 67 return !alwaysOnEnabled(user); 68 } 69 70 public boolean pulseOnDoubleTapEnabled(int user) { 71 return boolSettingDefaultOn(Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, user) 72 && pulseOnDoubleTapAvailable(); 73 } 74 75 public boolean pulseOnDoubleTapAvailable() { 76 return !TextUtils.isEmpty(doubleTapSensorType()) && ambientDisplayAvailable(); 77 } 78 79 public String doubleTapSensorType() { 80 return mContext.getResources().getString(R.string.config_dozeDoubleTapSensorType); 81 } 82 83 public String longPressSensorType() { 84 return mContext.getResources().getString(R.string.config_dozeLongPressSensorType); 85 } 86 87 public boolean pulseOnLongPressEnabled(int user) { 88 return pulseOnLongPressAvailable() && boolSettingDefaultOff( 89 Settings.Secure.DOZE_PULSE_ON_LONG_PRESS, user); 90 } 91 92 private boolean pulseOnLongPressAvailable() { 93 return !TextUtils.isEmpty(longPressSensorType()); 94 } 95 96 public boolean alwaysOnEnabled(int user) { 97 return boolSettingDefaultOn(Settings.Secure.DOZE_ALWAYS_ON, user) && alwaysOnAvailable() 98 && !accessibilityInversionEnabled(user); 99 } 100 101 public boolean alwaysOnAvailable() { 102 return (alwaysOnDisplayDebuggingEnabled() || alwaysOnDisplayAvailable()) 103 && ambientDisplayAvailable(); 104 } 105 106 public boolean alwaysOnAvailableForUser(int user) { 107 return alwaysOnAvailable() && !accessibilityInversionEnabled(user); 108 } 109 110 public String ambientDisplayComponent() { 111 return mContext.getResources().getString(R.string.config_dozeComponent); 112 } 113 114 public boolean accessibilityInversionEnabled(int user) { 115 return boolSettingDefaultOff(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, user); 116 } 117 118 private boolean ambientDisplayAvailable() { 119 return !TextUtils.isEmpty(ambientDisplayComponent()); 120 } 121 122 private boolean alwaysOnDisplayAvailable() { 123 return mContext.getResources().getBoolean(R.bool.config_dozeAlwaysOnDisplayAvailable); 124 } 125 126 private boolean alwaysOnDisplayDebuggingEnabled() { 127 return SystemProperties.getBoolean("debug.doze.aod", false) && Build.IS_DEBUGGABLE; 128 } 129 130 131 private boolean boolSettingDefaultOn(String name, int user) { 132 return boolSetting(name, user, 1); 133 } 134 135 private boolean boolSettingDefaultOff(String name, int user) { 136 return boolSetting(name, user, 0); 137 } 138 139 private boolean boolSetting(String name, int user, int def) { 140 return Settings.Secure.getIntForUser(mContext.getContentResolver(), name, def, user) != 0; 141 } 142 } 143