The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 17 | package com.android.server.power; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Beverly | ae79ab9 | 2017-12-11 09:20:02 -0500 | [diff] [blame] | 19 | import static android.os.PowerManagerInternal.WAKEFULNESS_ASLEEP; |
| 20 | import static android.os.PowerManagerInternal.WAKEFULNESS_AWAKE; |
| 21 | import static android.os.PowerManagerInternal.WAKEFULNESS_DOZING; |
| 22 | import static android.os.PowerManagerInternal.WAKEFULNESS_DREAMING; |
| 23 | |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 24 | import android.annotation.IntDef; |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 25 | import android.annotation.NonNull; |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 26 | import android.annotation.UserIdInt; |
Jason Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 27 | import android.app.ActivityManager; |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 28 | import android.app.SynchronousUserSwitchObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
| 34 | import android.content.pm.PackageManager; |
Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 35 | import android.content.res.Resources; |
Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 36 | import android.database.ContentObserver; |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 37 | import android.hardware.SensorManager; |
| 38 | import android.hardware.SystemSensorManager; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 39 | import android.hardware.display.AmbientDisplayConfiguration; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 40 | import android.hardware.display.DisplayManagerInternal; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 41 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Ruchi Kandoi | 0d43404 | 2016-10-03 09:12:02 -0700 | [diff] [blame] | 42 | import android.hardware.power.V1_0.PowerHint; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 43 | import android.net.Uri; |
Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 44 | import android.os.BatteryManager; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 45 | import android.os.BatteryManagerInternal; |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 46 | import android.os.BatterySaverPolicyConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.Binder; |
| 48 | import android.os.Handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.os.IBinder; |
| 50 | import android.os.IPowerManager; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 51 | import android.os.Looper; |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 52 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.PowerManager; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 54 | import android.os.PowerManager.ServiceType; |
Santos Cordon | 623526b | 2019-04-09 17:02:38 +0100 | [diff] [blame] | 55 | import android.os.PowerManager.WakeData; |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 56 | import android.os.PowerManager.WakeReason; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 57 | import android.os.PowerManagerInternal; |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 58 | import android.os.PowerSaveState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.os.Process; |
| 60 | import android.os.RemoteException; |
Jocelyn Dang | f2c38c1 | 2017-03-31 14:03:37 -0700 | [diff] [blame] | 61 | import android.os.ResultReceiver; |
| 62 | import android.os.ShellCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.os.SystemClock; |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 64 | import android.os.SystemProperties; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 65 | import android.os.Trace; |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 66 | import android.os.UserHandle; |
Christine Franks | 732c043 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 67 | import android.os.UserManager; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 68 | import android.os.WorkSource; |
Narayan Kamath | 2f916ed | 2017-12-29 13:02:15 +0000 | [diff] [blame] | 69 | import android.os.WorkSource.WorkChain; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.provider.Settings; |
Daichi Hirono | 82ab980 | 2016-03-02 13:23:29 +0900 | [diff] [blame] | 71 | import android.provider.Settings.SettingNotFoundException; |
Jeff Brown | 567f7ca | 2014-01-30 23:38:03 -0800 | [diff] [blame] | 72 | import android.service.dreams.DreamManagerInternal; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 73 | import android.service.vr.IVrManager; |
| 74 | import android.service.vr.IVrStateCallbacks; |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 75 | import android.util.KeyValueListParser; |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 76 | import android.util.PrintWriterPrinter; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 77 | import android.util.Slog; |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 78 | import android.util.SparseArray; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 79 | import android.util.TimeUtils; |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 80 | import android.util.proto.ProtoOutputStream; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 81 | import android.view.Display; |
Makoto Onuki | aae8953 | 2017-11-08 14:32:03 -0800 | [diff] [blame] | 82 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 83 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 84 | import com.android.internal.app.IAppOpsService; |
| 85 | import com.android.internal.app.IBatteryStats; |
| 86 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 87 | import com.android.internal.util.DumpUtils; |
Jason Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 88 | import com.android.server.EventLogTags; |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 89 | import com.android.server.LockGuard; |
Jeff Sharkey | fe6f85c | 2017-01-20 10:42:57 -0700 | [diff] [blame] | 90 | import com.android.server.RescueParty; |
Jason Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 91 | import com.android.server.ServiceThread; |
| 92 | import com.android.server.SystemService; |
Michael Wright | 64c820d | 2017-03-21 12:36:55 +0000 | [diff] [blame] | 93 | import com.android.server.UiThread; |
Jason Monk | afae4bd | 2015-12-15 14:20:06 -0500 | [diff] [blame] | 94 | import com.android.server.Watchdog; |
| 95 | import com.android.server.am.BatteryStatsService; |
| 96 | import com.android.server.lights.Light; |
| 97 | import com.android.server.lights.LightsManager; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 98 | import com.android.server.policy.WindowManagerPolicy; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 99 | import com.android.server.power.batterysaver.BatterySaverController; |
Kweku Adams | 7fb72a4 | 2019-01-08 16:08:49 -0800 | [diff] [blame] | 100 | import com.android.server.power.batterysaver.BatterySaverPolicy; |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 101 | import com.android.server.power.batterysaver.BatterySaverStateMachine; |
Makoto Onuki | bd7a625 | 2018-05-10 13:41:39 -0700 | [diff] [blame] | 102 | import com.android.server.power.batterysaver.BatterySavingStats; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import java.io.FileDescriptor; |
| 105 | import java.io.PrintWriter; |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 106 | import java.lang.annotation.Retention; |
| 107 | import java.lang.annotation.RetentionPolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import java.util.ArrayList; |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 109 | import java.util.Arrays; |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 110 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 112 | /** |
| 113 | * The power manager service is responsible for coordinating power management |
| 114 | * functions on the device. |
| 115 | */ |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 116 | public final class PowerManagerService extends SystemService |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 117 | implements Watchdog.Monitor { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | private static final String TAG = "PowerManagerService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | |
Santos Cordon | 4ddb801 | 2017-09-20 15:25:44 -0700 | [diff] [blame] | 120 | private static final boolean DEBUG = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 121 | private static final boolean DEBUG_SPEW = DEBUG && true; |
Jeff Brown | 88c997a | 2012-06-22 13:57:45 -0700 | [diff] [blame] | 122 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 123 | // Message: Sent when a user activity timeout occurs to update the power state. |
| 124 | private static final int MSG_USER_ACTIVITY_TIMEOUT = 1; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 125 | // Message: Sent when the device enters or exits a dreaming or dozing state. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 126 | private static final int MSG_SANDMAN = 2; |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 127 | // Message: Sent when the screen brightness boost expires. |
| 128 | private static final int MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 3; |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 129 | // Message: Polling to look for long held wake locks. |
| 130 | private static final int MSG_CHECK_FOR_LONG_WAKELOCKS = 4; |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 131 | // Message: Sent when an attentive timeout occurs to update the power state. |
| 132 | private static final int MSG_ATTENTIVE_TIMEOUT = 5; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 133 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 134 | // Dirty bit: mWakeLocks changed |
| 135 | private static final int DIRTY_WAKE_LOCKS = 1 << 0; |
| 136 | // Dirty bit: mWakefulness changed |
| 137 | private static final int DIRTY_WAKEFULNESS = 1 << 1; |
| 138 | // Dirty bit: user activity was poked or may have timed out |
| 139 | private static final int DIRTY_USER_ACTIVITY = 1 << 2; |
| 140 | // Dirty bit: actual display power state was updated asynchronously |
| 141 | private static final int DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED = 1 << 3; |
| 142 | // Dirty bit: mBootCompleted changed |
| 143 | private static final int DIRTY_BOOT_COMPLETED = 1 << 4; |
| 144 | // Dirty bit: settings changed |
| 145 | private static final int DIRTY_SETTINGS = 1 << 5; |
| 146 | // Dirty bit: mIsPowered changed |
| 147 | private static final int DIRTY_IS_POWERED = 1 << 6; |
| 148 | // Dirty bit: mStayOn changed |
| 149 | private static final int DIRTY_STAY_ON = 1 << 7; |
| 150 | // Dirty bit: battery state changed |
| 151 | private static final int DIRTY_BATTERY_STATE = 1 << 8; |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 152 | // Dirty bit: proximity state changed |
| 153 | private static final int DIRTY_PROXIMITY_POSITIVE = 1 << 9; |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 154 | // Dirty bit: dock state changed |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 155 | private static final int DIRTY_DOCK_STATE = 1 << 10; |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 156 | // Dirty bit: brightness boost changed |
| 157 | private static final int DIRTY_SCREEN_BRIGHTNESS_BOOST = 1 << 11; |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 158 | // Dirty bit: sQuiescent changed |
| 159 | private static final int DIRTY_QUIESCENT = 1 << 12; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 160 | // Dirty bit: VR Mode enabled changed |
| 161 | private static final int DIRTY_VR_MODE_CHANGED = 1 << 13; |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 162 | // Dirty bit: attentive timer may have timed out |
| 163 | private static final int DIRTY_ATTENTIVE = 1 << 14; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 164 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 165 | // Summarizes the state of all active wakelocks. |
| 166 | private static final int WAKE_LOCK_CPU = 1 << 0; |
| 167 | private static final int WAKE_LOCK_SCREEN_BRIGHT = 1 << 1; |
| 168 | private static final int WAKE_LOCK_SCREEN_DIM = 1 << 2; |
| 169 | private static final int WAKE_LOCK_BUTTON_BRIGHT = 1 << 3; |
| 170 | private static final int WAKE_LOCK_PROXIMITY_SCREEN_OFF = 1 << 4; |
Jeff Brown | 1042874 | 2012-10-09 15:47:30 -0700 | [diff] [blame] | 171 | private static final int WAKE_LOCK_STAY_AWAKE = 1 << 5; // only set if already awake |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 172 | private static final int WAKE_LOCK_DOZE = 1 << 6; |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 173 | private static final int WAKE_LOCK_DRAW = 1 << 7; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 175 | // Summarizes the user activity state. |
| 176 | private static final int USER_ACTIVITY_SCREEN_BRIGHT = 1 << 0; |
| 177 | private static final int USER_ACTIVITY_SCREEN_DIM = 1 << 1; |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 178 | private static final int USER_ACTIVITY_SCREEN_DREAM = 1 << 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 180 | // Default timeout in milliseconds. This is only used until the settings |
| 181 | // provider populates the actual default value (R.integer.def_screen_off_timeout). |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 182 | private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000; |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 183 | private static final int DEFAULT_SLEEP_TIMEOUT = -1; |
Jeff Brown | ff53254 | 2012-10-02 21:18:04 -0700 | [diff] [blame] | 184 | |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 185 | // Screen brightness boost timeout. |
| 186 | // Hardcoded for now until we decide what the right policy should be. |
| 187 | // This should perhaps be a setting. |
| 188 | private static final int SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 5 * 1000; |
| 189 | |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 190 | // How long a partial wake lock must be held until we consider it a long wake lock. |
| 191 | static final long MIN_LONG_WAKE_CHECK_INTERVAL = 60*1000; |
| 192 | |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 193 | // Power features defined in hardware/libhardware/include/hardware/power.h. |
| 194 | private static final int POWER_FEATURE_DOUBLE_TAP_TO_WAKE = 1; |
| 195 | |
| 196 | // Default setting for double tap to wake. |
| 197 | private static final int DEFAULT_DOUBLE_TAP_TO_WAKE = 0; |
| 198 | |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 199 | // System property indicating that the screen should remain off until an explicit user action |
| 200 | private static final String SYSTEM_PROPERTY_QUIESCENT = "ro.boot.quiescent"; |
| 201 | |
Christine Franks | 732c043 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 202 | // System Property indicating that retail demo mode is currently enabled. |
| 203 | private static final String SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED = "sys.retaildemo.enabled"; |
| 204 | |
Wei Wang | 72b77cc | 2019-02-07 14:10:23 -0800 | [diff] [blame] | 205 | // Possible reasons for shutting down or reboot for use in REBOOT_PROPERTY(sys.boot.reason) |
| 206 | // which is set by bootstat |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 207 | private static final String REASON_SHUTDOWN = "shutdown"; |
| 208 | private static final String REASON_REBOOT = "reboot"; |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 209 | private static final String REASON_USERREQUESTED = "shutdown,userrequested"; |
| 210 | private static final String REASON_THERMAL_SHUTDOWN = "shutdown,thermal"; |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 211 | private static final String REASON_LOW_BATTERY = "shutdown,battery"; |
| 212 | private static final String REASON_BATTERY_THERMAL_STATE = "shutdown,thermal,battery"; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 213 | |
Makoto Onuki | 5ab5e1d | 2017-04-14 17:04:26 -0700 | [diff] [blame] | 214 | private static final String TRACE_SCREEN_ON = "Screen turning on"; |
| 215 | |
| 216 | /** If turning screen on takes more than this long, we show a warning on logcat. */ |
| 217 | private static final int SCREEN_ON_LATENCY_WARNING_MS = 200; |
| 218 | |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 219 | /** Constants for {@link #shutdownOrRebootInternal} */ |
| 220 | @Retention(RetentionPolicy.SOURCE) |
| 221 | @IntDef({HALT_MODE_SHUTDOWN, HALT_MODE_REBOOT, HALT_MODE_REBOOT_SAFE_MODE}) |
| 222 | public @interface HaltMode {} |
| 223 | private static final int HALT_MODE_SHUTDOWN = 0; |
| 224 | private static final int HALT_MODE_REBOOT = 1; |
| 225 | private static final int HALT_MODE_REBOOT_SAFE_MODE = 2; |
| 226 | |
Mark Salyzyn | 74ce8b3 | 2018-06-05 09:06:54 -0700 | [diff] [blame] | 227 | // property for last reboot reason |
| 228 | private static final String REBOOT_PROPERTY = "sys.boot.reason"; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 229 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 230 | private final Context mContext; |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 231 | private final ServiceThread mHandlerThread; |
| 232 | private final PowerManagerHandler mHandler; |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 233 | private final AmbientDisplayConfiguration mAmbientDisplayConfiguration; |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 234 | private final BatterySaverPolicy mBatterySaverPolicy; |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 235 | private final BatterySaverController mBatterySaverController; |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 236 | private final BatterySaverStateMachine mBatterySaverStateMachine; |
Makoto Onuki | bd7a625 | 2018-05-10 13:41:39 -0700 | [diff] [blame] | 237 | private final BatterySavingStats mBatterySavingStats; |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 238 | private final AttentionDetector mAttentionDetector; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 239 | private final BinderService mBinderService; |
| 240 | private final LocalService mLocalService; |
| 241 | private final NativeWrapper mNativeWrapper; |
| 242 | private final Injector mInjector; |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 243 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 244 | private LightsManager mLightsManager; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 245 | private BatteryManagerInternal mBatteryManagerInternal; |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 246 | private DisplayManagerInternal mDisplayManagerInternal; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 247 | private IBatteryStats mBatteryStats; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 248 | private IAppOpsService mAppOps; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 249 | private WindowManagerPolicy mPolicy; |
| 250 | private Notifier mNotifier; |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 251 | private WirelessChargerDetector mWirelessChargerDetector; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 252 | private SettingsObserver mSettingsObserver; |
Jeff Brown | 567f7ca | 2014-01-30 23:38:03 -0800 | [diff] [blame] | 253 | private DreamManagerInternal mDreamManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 254 | private Light mAttentionLight; |
Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 255 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 256 | private InattentiveSleepWarningController mInattentiveSleepWarningOverlayController; |
| 257 | |
Jeff Sharkey | 5f3e934 | 2017-03-13 14:53:11 -0600 | [diff] [blame] | 258 | private final Object mLock = LockGuard.installNewLock(LockGuard.INDEX_POWER); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 259 | |
| 260 | // A bitfield that indicates what parts of the power state have |
| 261 | // changed and need to be recalculated. |
| 262 | private int mDirty; |
| 263 | |
| 264 | // Indicates whether the device is awake or asleep or somewhere in between. |
| 265 | // This is distinct from the screen power state, which is managed separately. |
| 266 | private int mWakefulness; |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 267 | private boolean mWakefulnessChanging; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 268 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 269 | // True if the sandman has just been summoned for the first time since entering the |
| 270 | // dreaming or dozing state. Indicates whether a new dream should begin. |
| 271 | private boolean mSandmanSummoned; |
| 272 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 273 | // True if MSG_SANDMAN has been scheduled. |
| 274 | private boolean mSandmanScheduled; |
| 275 | |
| 276 | // Table of all suspend blockers. |
| 277 | // There should only be a few of these. |
| 278 | private final ArrayList<SuspendBlocker> mSuspendBlockers = new ArrayList<SuspendBlocker>(); |
| 279 | |
| 280 | // Table of all wake locks acquired by applications. |
| 281 | private final ArrayList<WakeLock> mWakeLocks = new ArrayList<WakeLock>(); |
| 282 | |
| 283 | // A bitfield that summarizes the state of all active wakelocks. |
| 284 | private int mWakeLockSummary; |
| 285 | |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 286 | // Have we scheduled a message to check for long wake locks? This is when we will check. |
| 287 | private long mNotifyLongScheduled; |
| 288 | |
| 289 | // Last time we checked for long wake locks. |
| 290 | private long mNotifyLongDispatched; |
| 291 | |
| 292 | // The time we decided to do next long check. |
| 293 | private long mNotifyLongNextCheck; |
| 294 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 295 | // If true, instructs the display controller to wait for the proximity sensor to |
| 296 | // go negative before turning the screen on. |
| 297 | private boolean mRequestWaitForNegativeProximity; |
| 298 | |
| 299 | // Timestamp of the last time the device was awoken or put to sleep. |
| 300 | private long mLastWakeTime; |
| 301 | private long mLastSleepTime; |
| 302 | |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 303 | // Last reason the device went to sleep. |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 304 | private @WakeReason int mLastWakeReason; |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 305 | private int mLastSleepReason; |
| 306 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 307 | // Timestamp of the last call to user activity. |
| 308 | private long mLastUserActivityTime; |
| 309 | private long mLastUserActivityTimeNoChangeLights; |
| 310 | |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 311 | // Timestamp of last interactive power hint. |
| 312 | private long mLastInteractivePowerHintTime; |
| 313 | |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 314 | // Timestamp of the last screen brightness boost. |
| 315 | private long mLastScreenBrightnessBoostTime; |
| 316 | private boolean mScreenBrightnessBoostInProgress; |
| 317 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 318 | // A bitfield that summarizes the effect of the user activity timer. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 319 | private int mUserActivitySummary; |
| 320 | |
| 321 | // The desired display power state. The actual state may lag behind the |
| 322 | // requested because it is updated asynchronously by the display power controller. |
| 323 | private final DisplayPowerRequest mDisplayPowerRequest = new DisplayPowerRequest(); |
| 324 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 325 | // True if the display power state has been fully applied, which means the display |
| 326 | // is actually on or actually off or whatever was requested. |
| 327 | private boolean mDisplayReady; |
| 328 | |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 329 | // The suspend blocker used to keep the CPU alive when an application has acquired |
| 330 | // a wake lock. |
| 331 | private final SuspendBlocker mWakeLockSuspendBlocker; |
| 332 | |
| 333 | // True if the wake lock suspend blocker has been acquired. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 334 | private boolean mHoldingWakeLockSuspendBlocker; |
| 335 | |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 336 | // The suspend blocker used to keep the CPU alive when the display is on, the |
| 337 | // display is getting ready or there is user activity (in which case the display |
| 338 | // must be on). |
| 339 | private final SuspendBlocker mDisplaySuspendBlocker; |
| 340 | |
| 341 | // True if the display suspend blocker has been acquired. |
| 342 | private boolean mHoldingDisplaySuspendBlocker; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 343 | |
| 344 | // True if systemReady() has been called. |
| 345 | private boolean mSystemReady; |
| 346 | |
| 347 | // True if boot completed occurred. We keep the screen on until this happens. |
| 348 | private boolean mBootCompleted; |
| 349 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 350 | // True if auto-suspend mode is enabled. |
| 351 | // Refer to autosuspend.h. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 352 | private boolean mHalAutoSuspendModeEnabled; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 353 | |
| 354 | // True if interactive mode is enabled. |
| 355 | // Refer to power.h. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 356 | private boolean mHalInteractiveModeEnabled; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 357 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 358 | // True if the device is plugged into a power source. |
| 359 | private boolean mIsPowered; |
| 360 | |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 361 | // The current plug type, such as BatteryManager.BATTERY_PLUGGED_WIRELESS. |
| 362 | private int mPlugType; |
| 363 | |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 364 | // The current battery level percentage. |
| 365 | private int mBatteryLevel; |
| 366 | |
| 367 | // The battery level percentage at the time the dream started. |
| 368 | // This is used to terminate a dream and go to sleep if the battery is |
| 369 | // draining faster than it is charging and the user activity timeout has expired. |
| 370 | private int mBatteryLevelWhenDreamStarted; |
| 371 | |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 372 | // The current dock state. |
| 373 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 374 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 375 | // True to decouple auto-suspend mode from the display state. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 376 | private boolean mDecoupleHalAutoSuspendModeFromDisplayConfig; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 377 | |
| 378 | // True to decouple interactive mode from the display state. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 379 | private boolean mDecoupleHalInteractiveModeFromDisplayConfig; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 380 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 381 | // True if the device should wake up when plugged or unplugged. |
| 382 | private boolean mWakeUpWhenPluggedOrUnpluggedConfig; |
| 383 | |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 384 | // True if the device should wake up when plugged or unplugged in theater mode. |
| 385 | private boolean mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig; |
| 386 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 387 | // True if the device should suspend when the screen is off due to proximity. |
| 388 | private boolean mSuspendWhenScreenOffDueToProximityConfig; |
| 389 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 390 | // Default value for attentive timeout. |
| 391 | private int mAttentiveTimeoutConfig; |
| 392 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 393 | // True if dreams are supported on this device. |
| 394 | private boolean mDreamsSupportedConfig; |
| 395 | |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 396 | // Default value for dreams enabled |
| 397 | private boolean mDreamsEnabledByDefaultConfig; |
| 398 | |
| 399 | // Default value for dreams activate-on-sleep |
| 400 | private boolean mDreamsActivatedOnSleepByDefaultConfig; |
| 401 | |
| 402 | // Default value for dreams activate-on-dock |
| 403 | private boolean mDreamsActivatedOnDockByDefaultConfig; |
| 404 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 405 | // True if dreams can run while not plugged in. |
| 406 | private boolean mDreamsEnabledOnBatteryConfig; |
| 407 | |
| 408 | // Minimum battery level to allow dreaming when powered. |
| 409 | // Use -1 to disable this safety feature. |
| 410 | private int mDreamsBatteryLevelMinimumWhenPoweredConfig; |
| 411 | |
| 412 | // Minimum battery level to allow dreaming when not powered. |
| 413 | // Use -1 to disable this safety feature. |
| 414 | private int mDreamsBatteryLevelMinimumWhenNotPoweredConfig; |
| 415 | |
| 416 | // If the battery level drops by this percentage and the user activity timeout |
| 417 | // has expired, then assume the device is receiving insufficient current to charge |
| 418 | // effectively and terminate the dream. Use -1 to disable this safety feature. |
| 419 | private int mDreamsBatteryLevelDrainCutoffConfig; |
| 420 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 421 | // True if dreams are enabled by the user. |
| 422 | private boolean mDreamsEnabledSetting; |
| 423 | |
John Spurlock | 1a868b7 | 2012-08-22 09:56:51 -0400 | [diff] [blame] | 424 | // True if dreams should be activated on sleep. |
| 425 | private boolean mDreamsActivateOnSleepSetting; |
| 426 | |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 427 | // True if dreams should be activated on dock. |
| 428 | private boolean mDreamsActivateOnDockSetting; |
| 429 | |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 430 | // True if doze should not be started until after the screen off transition. |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 431 | private boolean mDozeAfterScreenOff; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 432 | |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 433 | // The minimum screen off timeout, in milliseconds. |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 434 | private long mMinimumScreenOffTimeoutConfig; |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 435 | |
| 436 | // The screen dim duration, in milliseconds. |
| 437 | // This is subtracted from the end of the screen off timeout so the |
| 438 | // minimum screen off timeout should be longer than this. |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 439 | private long mMaximumScreenDimDurationConfig; |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 440 | |
| 441 | // The maximum screen dim time expressed as a ratio relative to the screen |
| 442 | // off timeout. If the screen off timeout is very short then we want the |
| 443 | // dim timeout to also be quite short so that most of the time is spent on. |
| 444 | // Otherwise the user won't get much screen on time before dimming occurs. |
| 445 | private float mMaximumScreenDimRatioConfig; |
| 446 | |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 447 | // Whether device supports double tap to wake. |
| 448 | private boolean mSupportsDoubleTapWakeConfig; |
| 449 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 450 | // The screen off timeout setting value in milliseconds. |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 451 | private long mScreenOffTimeoutSetting; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 452 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 453 | // Default for attentive warning duration. |
| 454 | private long mAttentiveWarningDurationConfig; |
| 455 | |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 456 | // The sleep timeout setting value in milliseconds. |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 457 | private long mSleepTimeoutSetting; |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 458 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 459 | // How long to show a warning message to user before the device goes to sleep |
| 460 | // after long user inactivity, even if wakelocks are held. |
| 461 | private long mAttentiveTimeoutSetting; |
| 462 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 463 | // The maximum allowable screen off timeout according to the device |
| 464 | // administration policy. Overrides other settings. |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 465 | private long mMaximumScreenOffTimeoutFromDeviceAdmin = Long.MAX_VALUE; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 466 | |
| 467 | // The stay on while plugged in setting. |
| 468 | // A bitfield of battery conditions under which to make the screen stay on. |
| 469 | private int mStayOnWhilePluggedInSetting; |
| 470 | |
| 471 | // True if the device should stay on. |
| 472 | private boolean mStayOn; |
| 473 | |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 474 | // True if the lights should stay off until an explicit user action. |
| 475 | private static boolean sQuiescent; |
| 476 | |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 477 | // True if the proximity sensor reads a positive result. |
| 478 | private boolean mProximityPositive; |
| 479 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 480 | // Screen brightness setting limits. |
| 481 | private int mScreenBrightnessSettingMinimum; |
| 482 | private int mScreenBrightnessSettingMaximum; |
| 483 | private int mScreenBrightnessSettingDefault; |
| 484 | |
| 485 | // The screen brightness setting, from 0 to 255. |
| 486 | // Use -1 if no value has been set. |
| 487 | private int mScreenBrightnessSetting; |
| 488 | |
| 489 | // The screen brightness mode. |
| 490 | // One of the Settings.System.SCREEN_BRIGHTNESS_MODE_* constants. |
| 491 | private int mScreenBrightnessModeSetting; |
| 492 | |
| 493 | // The screen brightness setting override from the window manager |
| 494 | // to allow the current foreground activity to override the brightness. |
| 495 | // Use -1 to disable. |
| 496 | private int mScreenBrightnessOverrideFromWindowManager = -1; |
| 497 | |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 498 | // The window manager has determined the user to be inactive via other means. |
| 499 | // Set this to false to disable. |
| 500 | private boolean mUserInactiveOverrideFromWindowManager; |
| 501 | |
Nick Armstrong-Crews | 024872e | 2015-09-30 16:49:19 -0700 | [diff] [blame] | 502 | // The next possible user activity timeout after being explicitly told the user is inactive. |
| 503 | // Set to -1 when not told the user is inactive since the last period spent dozing or asleep. |
| 504 | private long mOverriddenTimeout = -1; |
| 505 | |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 506 | // The user activity timeout override from the window manager |
| 507 | // to allow the current foreground activity to override the user activity timeout. |
| 508 | // Use -1 to disable. |
| 509 | private long mUserActivityTimeoutOverrideFromWindowManager = -1; |
| 510 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 511 | // The screen state to use while dozing. |
| 512 | private int mDozeScreenStateOverrideFromDreamManager = Display.STATE_UNKNOWN; |
| 513 | |
| 514 | // The screen brightness to use while dozing. |
| 515 | private int mDozeScreenBrightnessOverrideFromDreamManager = PowerManager.BRIGHTNESS_DEFAULT; |
| 516 | |
Ivan Podogov | 56bc6d0 | 2018-02-26 16:04:24 +0000 | [diff] [blame] | 517 | // Keep display state when dozing. |
| 518 | private boolean mDrawWakeLockOverrideFromSidekick; |
| 519 | |
Jeff Brown | 9ba8d78 | 2012-10-01 16:38:23 -0700 | [diff] [blame] | 520 | // Time when we last logged a warning about calling userActivity() without permission. |
| 521 | private long mLastWarningAboutUserActivityPermission = Long.MIN_VALUE; |
| 522 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 523 | // True if the battery level is currently considered low. |
| 524 | private boolean mBatteryLevelLow; |
| 525 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 526 | // True if we are currently in device idle mode. |
| 527 | private boolean mDeviceIdleMode; |
| 528 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 529 | // True if we are currently in light device idle mode. |
| 530 | private boolean mLightDeviceIdleMode; |
| 531 | |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 532 | // Set of app ids that we will always respect the wake locks for. |
| 533 | int[] mDeviceIdleWhitelist = new int[0]; |
| 534 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 535 | // Set of app ids that are temporarily allowed to acquire wakelocks due to high-pri message |
| 536 | int[] mDeviceIdleTempWhitelist = new int[0]; |
| 537 | |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 538 | private final SparseArray<UidState> mUidState = new SparseArray<>(); |
| 539 | |
| 540 | // We are currently in the middle of a batch change of uids. |
| 541 | private boolean mUidsChanging; |
| 542 | |
| 543 | // Some uids have actually changed while mUidsChanging was true. |
| 544 | private boolean mUidsChanged; |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 545 | |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 546 | // True if theater mode is enabled |
| 547 | private boolean mTheaterModeEnabled; |
| 548 | |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 549 | // True if always on display is enabled |
| 550 | private boolean mAlwaysOnEnabled; |
| 551 | |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 552 | // True if double tap to wake is enabled |
| 553 | private boolean mDoubleTapWakeEnabled; |
| 554 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 555 | // True if we are currently in VR Mode. |
| 556 | private boolean mIsVrModeEnabled; |
| 557 | |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 558 | // True if we in the process of performing a forceSuspend |
| 559 | private boolean mForceSuspendActive; |
| 560 | |
Santos Cordon | 0304438 | 2019-09-19 16:59:01 +0100 | [diff] [blame] | 561 | // Transition to Doze is in progress. We have transitioned to WAKEFULNESS_DOZING, |
| 562 | // but the DreamService has not yet been told to start (it's an async process). |
| 563 | private boolean mDozeStartInProgress; |
| 564 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 565 | private final class ForegroundProfileObserver extends SynchronousUserSwitchObserver { |
| 566 | @Override |
Beverly | 478722e | 2019-10-14 11:16:57 -0400 | [diff] [blame] | 567 | public void onUserSwitching(@UserIdInt int newUserId) throws RemoteException { |
| 568 | synchronized (mLock) { |
| 569 | mUserId = newUserId; |
| 570 | } |
| 571 | } |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 572 | |
| 573 | @Override |
| 574 | public void onForegroundProfileSwitch(@UserIdInt int newProfileId) throws RemoteException { |
| 575 | final long now = SystemClock.uptimeMillis(); |
Beverly | 478722e | 2019-10-14 11:16:57 -0400 | [diff] [blame] | 576 | synchronized (mLock) { |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 577 | mForegroundProfile = newProfileId; |
| 578 | maybeUpdateForegroundProfileLastActivityLocked(now); |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | // User id corresponding to activity the user is currently interacting with. |
| 584 | private @UserIdInt int mForegroundProfile; |
Beverly | 478722e | 2019-10-14 11:16:57 -0400 | [diff] [blame] | 585 | // User id of main profile for the current user (doesn't include managed profiles) |
| 586 | private @UserIdInt int mUserId; |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 587 | |
| 588 | // Per-profile state to track when a profile should be locked. |
| 589 | private final SparseArray<ProfilePowerState> mProfilePowerState = new SparseArray<>(); |
| 590 | |
| 591 | private static final class ProfilePowerState { |
| 592 | // Profile user id. |
| 593 | final @UserIdInt int mUserId; |
| 594 | // Maximum time to lock set by admin. |
| 595 | long mScreenOffTimeout; |
| 596 | // Like top-level mWakeLockSummary, but only for wake locks that affect current profile. |
| 597 | int mWakeLockSummary; |
| 598 | // Last user activity that happened in an app running in the profile. |
| 599 | long mLastUserActivityTime; |
| 600 | // Whether profile has been locked last time it timed out. |
| 601 | boolean mLockingNotified; |
| 602 | |
| 603 | public ProfilePowerState(@UserIdInt int userId, long screenOffTimeout) { |
| 604 | mUserId = userId; |
| 605 | mScreenOffTimeout = screenOffTimeout; |
| 606 | // Not accurate but at least won't cause immediate locking of the profile. |
| 607 | mLastUserActivityTime = SystemClock.uptimeMillis(); |
| 608 | } |
| 609 | } |
| 610 | |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 611 | /** |
| 612 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 613 | * global Settings. Any access to this class or its fields should be done while |
| 614 | * holding the PowerManagerService.mLock lock. |
| 615 | */ |
| 616 | private final class Constants extends ContentObserver { |
| 617 | // Key names stored in the settings value. |
| 618 | private static final String KEY_NO_CACHED_WAKE_LOCKS = "no_cached_wake_locks"; |
| 619 | |
| 620 | private static final boolean DEFAULT_NO_CACHED_WAKE_LOCKS = true; |
| 621 | |
| 622 | // Prevent processes that are cached from holding wake locks? |
| 623 | public boolean NO_CACHED_WAKE_LOCKS = DEFAULT_NO_CACHED_WAKE_LOCKS; |
| 624 | |
| 625 | private ContentResolver mResolver; |
| 626 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 627 | |
| 628 | public Constants(Handler handler) { |
| 629 | super(handler); |
| 630 | } |
| 631 | |
| 632 | public void start(ContentResolver resolver) { |
| 633 | mResolver = resolver; |
| 634 | mResolver.registerContentObserver(Settings.Global.getUriFor( |
| 635 | Settings.Global.POWER_MANAGER_CONSTANTS), false, this); |
| 636 | updateConstants(); |
| 637 | } |
| 638 | |
| 639 | @Override |
| 640 | public void onChange(boolean selfChange, Uri uri) { |
| 641 | updateConstants(); |
| 642 | } |
| 643 | |
| 644 | private void updateConstants() { |
| 645 | synchronized (mLock) { |
| 646 | try { |
| 647 | mParser.setString(Settings.Global.getString(mResolver, |
| 648 | Settings.Global.POWER_MANAGER_CONSTANTS)); |
| 649 | } catch (IllegalArgumentException e) { |
| 650 | // Failed to parse the settings string, log this and move on |
| 651 | // with defaults. |
| 652 | Slog.e(TAG, "Bad alarm manager settings", e); |
| 653 | } |
| 654 | |
| 655 | NO_CACHED_WAKE_LOCKS = mParser.getBoolean(KEY_NO_CACHED_WAKE_LOCKS, |
| 656 | DEFAULT_NO_CACHED_WAKE_LOCKS); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | void dump(PrintWriter pw) { |
| 661 | pw.println(" Settings " + Settings.Global.POWER_MANAGER_CONSTANTS + ":"); |
| 662 | |
| 663 | pw.print(" "); pw.print(KEY_NO_CACHED_WAKE_LOCKS); pw.print("="); |
| 664 | pw.println(NO_CACHED_WAKE_LOCKS); |
| 665 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 666 | |
| 667 | void dumpProto(ProtoOutputStream proto) { |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 668 | final long constantsToken = proto.start(PowerManagerServiceDumpProto.CONSTANTS); |
| 669 | proto.write(PowerManagerServiceDumpProto.ConstantsProto.IS_NO_CACHED_WAKE_LOCKS, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 670 | NO_CACHED_WAKE_LOCKS); |
| 671 | proto.end(constantsToken); |
| 672 | } |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 675 | /** |
| 676 | * Wrapper around the static-native methods of PowerManagerService. |
| 677 | * |
| 678 | * This class exists to allow us to mock static native methods in our tests. If mocking static |
| 679 | * methods becomes easier than this in the future, we can delete this class. |
| 680 | */ |
| 681 | @VisibleForTesting |
| 682 | public static class NativeWrapper { |
| 683 | /** Wrapper for PowerManager.nativeInit */ |
| 684 | public void nativeInit(PowerManagerService service) { |
| 685 | service.nativeInit(); |
| 686 | } |
| 687 | |
| 688 | /** Wrapper for PowerManager.nativeAcquireSuspectBlocker */ |
| 689 | public void nativeAcquireSuspendBlocker(String name) { |
| 690 | PowerManagerService.nativeAcquireSuspendBlocker(name); |
| 691 | } |
| 692 | |
| 693 | /** Wrapper for PowerManager.nativeReleaseSuspendBlocker */ |
| 694 | public void nativeReleaseSuspendBlocker(String name) { |
| 695 | PowerManagerService.nativeReleaseSuspendBlocker(name); |
| 696 | } |
| 697 | |
| 698 | /** Wrapper for PowerManager.nativeSetInteractive */ |
| 699 | public void nativeSetInteractive(boolean enable) { |
| 700 | PowerManagerService.nativeSetInteractive(enable); |
| 701 | } |
| 702 | |
| 703 | /** Wrapper for PowerManager.nativeSetAutoSuspend */ |
| 704 | public void nativeSetAutoSuspend(boolean enable) { |
| 705 | PowerManagerService.nativeSetAutoSuspend(enable); |
| 706 | } |
| 707 | |
| 708 | /** Wrapper for PowerManager.nativeSendPowerHint */ |
| 709 | public void nativeSendPowerHint(int hintId, int data) { |
| 710 | PowerManagerService.nativeSendPowerHint(hintId, data); |
| 711 | } |
| 712 | |
| 713 | /** Wrapper for PowerManager.nativeSetFeature */ |
| 714 | public void nativeSetFeature(int featureId, int data) { |
| 715 | PowerManagerService.nativeSetFeature(featureId, data); |
| 716 | } |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 717 | |
| 718 | /** Wrapper for PowerManager.nativeForceSuspend */ |
| 719 | public boolean nativeForceSuspend() { |
| 720 | return PowerManagerService.nativeForceSuspend(); |
| 721 | } |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | @VisibleForTesting |
| 725 | static class Injector { |
| 726 | Notifier createNotifier(Looper looper, Context context, IBatteryStats batteryStats, |
| 727 | SuspendBlocker suspendBlocker, WindowManagerPolicy policy) { |
| 728 | return new Notifier(looper, context, batteryStats, suspendBlocker, policy); |
| 729 | } |
| 730 | |
| 731 | SuspendBlocker createSuspendBlocker(PowerManagerService service, String name) { |
| 732 | SuspendBlocker suspendBlocker = service.new SuspendBlockerImpl(name); |
| 733 | service.mSuspendBlockers.add(suspendBlocker); |
| 734 | return suspendBlocker; |
| 735 | } |
| 736 | |
| 737 | BatterySaverPolicy createBatterySaverPolicy( |
| 738 | Object lock, Context context, BatterySavingStats batterySavingStats) { |
| 739 | return new BatterySaverPolicy(lock, context, batterySavingStats); |
| 740 | } |
| 741 | |
| 742 | NativeWrapper createNativeWrapper() { |
| 743 | return new NativeWrapper(); |
| 744 | } |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 745 | |
| 746 | WirelessChargerDetector createWirelessChargerDetector( |
| 747 | SensorManager sensorManager, SuspendBlocker suspendBlocker, Handler handler) { |
| 748 | return new WirelessChargerDetector(sensorManager, suspendBlocker, handler); |
| 749 | } |
| 750 | |
| 751 | AmbientDisplayConfiguration createAmbientDisplayConfiguration(Context context) { |
| 752 | return new AmbientDisplayConfiguration(context); |
| 753 | } |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 754 | |
| 755 | InattentiveSleepWarningController createInattentiveSleepWarningController() { |
| 756 | return new InattentiveSleepWarningController(); |
| 757 | } |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 758 | } |
| 759 | |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 760 | final Constants mConstants; |
| 761 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 762 | private native void nativeInit(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 763 | private static native void nativeAcquireSuspendBlocker(String name); |
| 764 | private static native void nativeReleaseSuspendBlocker(String name); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 765 | private static native void nativeSetInteractive(boolean enable); |
| 766 | private static native void nativeSetAutoSuspend(boolean enable); |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 767 | private static native void nativeSendPowerHint(int hintId, int data); |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 768 | private static native void nativeSetFeature(int featureId, int data); |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 769 | private static native boolean nativeForceSuspend(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 771 | public PowerManagerService(Context context) { |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 772 | this(context, new Injector()); |
| 773 | } |
| 774 | |
| 775 | @VisibleForTesting |
| 776 | PowerManagerService(Context context, Injector injector) { |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 777 | super(context); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 778 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 779 | mContext = context; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 780 | mBinderService = new BinderService(); |
| 781 | mLocalService = new LocalService(); |
| 782 | mNativeWrapper = injector.createNativeWrapper(); |
| 783 | mInjector = injector; |
| 784 | |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 785 | mHandlerThread = new ServiceThread(TAG, |
| 786 | Process.THREAD_PRIORITY_DISPLAY, false /*allowIo*/); |
| 787 | mHandlerThread.start(); |
| 788 | mHandler = new PowerManagerHandler(mHandlerThread.getLooper()); |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 789 | mConstants = new Constants(mHandler); |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 790 | mAmbientDisplayConfiguration = mInjector.createAmbientDisplayConfiguration(context); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 791 | mAttentionDetector = new AttentionDetector(this::onUserAttention, mLock); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 792 | |
Makoto Onuki | bd7a625 | 2018-05-10 13:41:39 -0700 | [diff] [blame] | 793 | mBatterySavingStats = new BatterySavingStats(mLock); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 794 | mBatterySaverPolicy = |
| 795 | mInjector.createBatterySaverPolicy(mLock, mContext, mBatterySavingStats); |
Makoto Onuki | bd7a625 | 2018-05-10 13:41:39 -0700 | [diff] [blame] | 796 | mBatterySaverController = new BatterySaverController(mLock, mContext, |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 797 | BackgroundThread.get().getLooper(), mBatterySaverPolicy, |
| 798 | mBatterySavingStats); |
Makoto Onuki | bd7a625 | 2018-05-10 13:41:39 -0700 | [diff] [blame] | 799 | mBatterySaverStateMachine = new BatterySaverStateMachine( |
| 800 | mLock, mContext, mBatterySaverController); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 801 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 802 | mInattentiveSleepWarningOverlayController = |
| 803 | mInjector.createInattentiveSleepWarningController(); |
| 804 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 805 | synchronized (mLock) { |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 806 | mWakeLockSuspendBlocker = |
| 807 | mInjector.createSuspendBlocker(this, "PowerManagerService.WakeLocks"); |
| 808 | mDisplaySuspendBlocker = |
| 809 | mInjector.createSuspendBlocker(this, "PowerManagerService.Display"); |
| 810 | if (mDisplaySuspendBlocker != null) { |
| 811 | mDisplaySuspendBlocker.acquire(); |
| 812 | mHoldingDisplaySuspendBlocker = true; |
| 813 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 814 | mHalAutoSuspendModeEnabled = false; |
| 815 | mHalInteractiveModeEnabled = true; |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 816 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 817 | mWakefulness = WAKEFULNESS_AWAKE; |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 818 | sQuiescent = SystemProperties.get(SYSTEM_PROPERTY_QUIESCENT, "0").equals("1"); |
| 819 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 820 | mNativeWrapper.nativeInit(this); |
| 821 | mNativeWrapper.nativeSetAutoSuspend(false); |
| 822 | mNativeWrapper.nativeSetInteractive(true); |
| 823 | mNativeWrapper.nativeSetFeature(POWER_FEATURE_DOUBLE_TAP_TO_WAKE, 0); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 824 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | } |
| 826 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 827 | @Override |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 828 | public void onStart() { |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 829 | publishBinderService(Context.POWER_SERVICE, mBinderService); |
| 830 | publishLocalService(PowerManagerInternal.class, mLocalService); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 831 | |
| 832 | Watchdog.getInstance().addMonitor(this); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 833 | Watchdog.getInstance().addThread(mHandler); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 834 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 835 | |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 836 | @Override |
| 837 | public void onBootPhase(int phase) { |
Craig Mautner | 6e2f395 | 2014-09-09 14:26:41 -0700 | [diff] [blame] | 838 | synchronized (mLock) { |
Jeff Sharkey | b5e89c6 | 2016-04-01 23:20:31 -0600 | [diff] [blame] | 839 | if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 840 | incrementBootCount(); |
| 841 | |
| 842 | } else if (phase == PHASE_BOOT_COMPLETED) { |
Craig Mautner | 6e2f395 | 2014-09-09 14:26:41 -0700 | [diff] [blame] | 843 | final long now = SystemClock.uptimeMillis(); |
| 844 | mBootCompleted = true; |
| 845 | mDirty |= DIRTY_BOOT_COMPLETED; |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 846 | |
| 847 | mBatterySaverStateMachine.onBootCompleted(); |
Craig Mautner | 6e2f395 | 2014-09-09 14:26:41 -0700 | [diff] [blame] | 848 | userActivityNoUpdateLocked( |
| 849 | now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); |
| 850 | updatePowerStateLocked(); |
| 851 | } |
Jeff Brown | 6d2a949 | 2014-08-07 19:06:49 -0700 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 855 | public void systemReady(IAppOpsService appOps) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 856 | synchronized (mLock) { |
| 857 | mSystemReady = true; |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 858 | mAppOps = appOps; |
| 859 | mDreamManager = getLocalService(DreamManagerInternal.class); |
| 860 | mDisplayManagerInternal = getLocalService(DisplayManagerInternal.class); |
| 861 | mPolicy = getLocalService(WindowManagerPolicy.class); |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 862 | mBatteryManagerInternal = getLocalService(BatteryManagerInternal.class); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 863 | mAttentionDetector.systemReady(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 865 | PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 866 | mScreenBrightnessSettingMinimum = pm.getMinimumScreenBrightnessSetting(); |
| 867 | mScreenBrightnessSettingMaximum = pm.getMaximumScreenBrightnessSetting(); |
| 868 | mScreenBrightnessSettingDefault = pm.getDefaultScreenBrightnessSetting(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | |
Jaikumar Ganesh | 6d0c1d78 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 870 | SensorManager sensorManager = new SystemSensorManager(mContext, mHandler.getLooper()); |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 871 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 872 | // The notifier runs on the system server's main looper so as not to interfere |
| 873 | // with the animations and other critical functions of the power manager. |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 874 | mBatteryStats = BatteryStatsService.getService(); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 875 | mNotifier = mInjector.createNotifier(Looper.getMainLooper(), mContext, mBatteryStats, |
| 876 | mInjector.createSuspendBlocker(this, "PowerManagerService.Broadcasts"), |
| 877 | mPolicy); |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 878 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 879 | mWirelessChargerDetector = mInjector.createWirelessChargerDetector(sensorManager, |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 880 | mInjector.createSuspendBlocker( |
| 881 | this, "PowerManagerService.WirelessChargerDetector"), |
Jeff Brown | dbcc8a2 | 2013-10-01 16:16:44 -0700 | [diff] [blame] | 882 | mHandler); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 883 | mSettingsObserver = new SettingsObserver(mHandler); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 884 | |
| 885 | mLightsManager = getLocalService(LightsManager.class); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 886 | mAttentionLight = mLightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 887 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 888 | // Initialize display power management. |
| 889 | mDisplayManagerInternal.initPowerManagement( |
| 890 | mDisplayPowerCallbacks, mHandler, sensorManager); |
| 891 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 892 | try { |
| 893 | final ForegroundProfileObserver observer = new ForegroundProfileObserver(); |
| 894 | ActivityManager.getService().registerUserSwitchObserver(observer, TAG); |
| 895 | } catch (RemoteException e) { |
| 896 | // Shouldn't happen since in-process. |
| 897 | } |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 898 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 899 | // Go. |
| 900 | readConfigurationLocked(); |
| 901 | updateSettingsLocked(); |
| 902 | mDirty |= DIRTY_BATTERY_STATE; |
| 903 | updatePowerStateLocked(); |
| 904 | } |
Shibin George | 43f5de0 | 2016-07-06 02:12:10 +0530 | [diff] [blame] | 905 | |
Jeff Sharkey | 6e54461 | 2017-02-20 11:02:26 -0700 | [diff] [blame] | 906 | final ContentResolver resolver = mContext.getContentResolver(); |
| 907 | mConstants.start(resolver); |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 908 | |
| 909 | mBatterySaverController.systemReady(); |
Makoto Onuki | bd7a625 | 2018-05-10 13:41:39 -0700 | [diff] [blame] | 910 | mBatterySaverPolicy.systemReady(); |
Jeff Sharkey | 6e54461 | 2017-02-20 11:02:26 -0700 | [diff] [blame] | 911 | |
| 912 | // Register for settings changes. |
| 913 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 914 | Settings.Secure.SCREENSAVER_ENABLED), |
| 915 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 916 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 917 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP), |
| 918 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 919 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 920 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK), |
| 921 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 922 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 923 | Settings.System.SCREEN_OFF_TIMEOUT), |
| 924 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 925 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 926 | Settings.Secure.SLEEP_TIMEOUT), |
| 927 | false, mSettingsObserver, UserHandle.USER_ALL); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 928 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 929 | Settings.Secure.ATTENTIVE_TIMEOUT), |
| 930 | false, mSettingsObserver, UserHandle.USER_ALL); |
Jeff Sharkey | 6e54461 | 2017-02-20 11:02:26 -0700 | [diff] [blame] | 931 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 932 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN), |
| 933 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 934 | resolver.registerContentObserver(Settings.System.getUriFor( |
Jeff Sharkey | 6e54461 | 2017-02-20 11:02:26 -0700 | [diff] [blame] | 935 | Settings.System.SCREEN_BRIGHTNESS_MODE), |
| 936 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 937 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 938 | Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ), |
| 939 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 940 | resolver.registerContentObserver(Settings.Global.getUriFor( |
Jeff Sharkey | 6e54461 | 2017-02-20 11:02:26 -0700 | [diff] [blame] | 941 | Settings.Global.THEATER_MODE_ON), |
| 942 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 943 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 944 | Settings.Secure.DOZE_ALWAYS_ON), |
| 945 | false, mSettingsObserver, UserHandle.USER_ALL); |
| 946 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
Jeff Sharkey | 6e54461 | 2017-02-20 11:02:26 -0700 | [diff] [blame] | 947 | Settings.Secure.DOUBLE_TAP_TO_WAKE), |
| 948 | false, mSettingsObserver, UserHandle.USER_ALL); |
Christine Franks | 732c043 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 949 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 950 | Settings.Global.DEVICE_DEMO_MODE), |
| 951 | false, mSettingsObserver, UserHandle.USER_SYSTEM); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 952 | IVrManager vrManager = IVrManager.Stub.asInterface(getBinderService(Context.VR_SERVICE)); |
Jeff Sharkey | 6e54461 | 2017-02-20 11:02:26 -0700 | [diff] [blame] | 953 | if (vrManager != null) { |
| 954 | try { |
| 955 | vrManager.registerListener(mVrStateCallbacks); |
| 956 | } catch (RemoteException e) { |
| 957 | Slog.e(TAG, "Failed to register VR mode state listener: " + e); |
| 958 | } |
| 959 | } |
| 960 | |
Shibin George | 43f5de0 | 2016-07-06 02:12:10 +0530 | [diff] [blame] | 961 | // Register for broadcasts from other components of the system. |
| 962 | IntentFilter filter = new IntentFilter(); |
| 963 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 964 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 965 | mContext.registerReceiver(new BatteryReceiver(), filter, null, mHandler); |
| 966 | |
| 967 | filter = new IntentFilter(); |
| 968 | filter.addAction(Intent.ACTION_DREAMING_STARTED); |
| 969 | filter.addAction(Intent.ACTION_DREAMING_STOPPED); |
| 970 | mContext.registerReceiver(new DreamReceiver(), filter, null, mHandler); |
| 971 | |
| 972 | filter = new IntentFilter(); |
| 973 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
| 974 | mContext.registerReceiver(new UserSwitchedReceiver(), filter, null, mHandler); |
| 975 | |
| 976 | filter = new IntentFilter(); |
| 977 | filter.addAction(Intent.ACTION_DOCK_EVENT); |
| 978 | mContext.registerReceiver(new DockReceiver(), filter, null, mHandler); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 981 | @VisibleForTesting |
| 982 | void readConfigurationLocked() { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 983 | final Resources resources = mContext.getResources(); |
| 984 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 985 | mDecoupleHalAutoSuspendModeFromDisplayConfig = resources.getBoolean( |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 986 | com.android.internal.R.bool.config_powerDecoupleAutoSuspendModeFromDisplay); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 987 | mDecoupleHalInteractiveModeFromDisplayConfig = resources.getBoolean( |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 988 | com.android.internal.R.bool.config_powerDecoupleInteractiveModeFromDisplay); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 989 | mWakeUpWhenPluggedOrUnpluggedConfig = resources.getBoolean( |
Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 990 | com.android.internal.R.bool.config_unplugTurnsOnScreen); |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 991 | mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig = resources.getBoolean( |
| 992 | com.android.internal.R.bool.config_allowTheaterModeWakeFromUnplug); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 993 | mSuspendWhenScreenOffDueToProximityConfig = resources.getBoolean( |
| 994 | com.android.internal.R.bool.config_suspendWhenScreenOffDueToProximity); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 995 | mAttentiveTimeoutConfig = resources.getInteger( |
| 996 | com.android.internal.R.integer.config_attentiveTimeout); |
| 997 | mAttentiveWarningDurationConfig = resources.getInteger( |
| 998 | com.android.internal.R.integer.config_attentiveWarningDuration); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 999 | mDreamsSupportedConfig = resources.getBoolean( |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 1000 | com.android.internal.R.bool.config_dreamsSupported); |
| 1001 | mDreamsEnabledByDefaultConfig = resources.getBoolean( |
| 1002 | com.android.internal.R.bool.config_dreamsEnabledByDefault); |
| 1003 | mDreamsActivatedOnSleepByDefaultConfig = resources.getBoolean( |
| 1004 | com.android.internal.R.bool.config_dreamsActivatedOnSleepByDefault); |
| 1005 | mDreamsActivatedOnDockByDefaultConfig = resources.getBoolean( |
| 1006 | com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1007 | mDreamsEnabledOnBatteryConfig = resources.getBoolean( |
| 1008 | com.android.internal.R.bool.config_dreamsEnabledOnBattery); |
| 1009 | mDreamsBatteryLevelMinimumWhenPoweredConfig = resources.getInteger( |
| 1010 | com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenPowered); |
| 1011 | mDreamsBatteryLevelMinimumWhenNotPoweredConfig = resources.getInteger( |
| 1012 | com.android.internal.R.integer.config_dreamsBatteryLevelMinimumWhenNotPowered); |
| 1013 | mDreamsBatteryLevelDrainCutoffConfig = resources.getInteger( |
| 1014 | com.android.internal.R.integer.config_dreamsBatteryLevelDrainCutoff); |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 1015 | mDozeAfterScreenOff = resources.getBoolean( |
Lucas Dupin | 5354cc0 | 2018-03-20 16:09:34 -0700 | [diff] [blame] | 1016 | com.android.internal.R.bool.config_dozeAfterScreenOffByDefault); |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 1017 | mMinimumScreenOffTimeoutConfig = resources.getInteger( |
| 1018 | com.android.internal.R.integer.config_minimumScreenOffTimeout); |
| 1019 | mMaximumScreenDimDurationConfig = resources.getInteger( |
| 1020 | com.android.internal.R.integer.config_maximumScreenDimDuration); |
| 1021 | mMaximumScreenDimRatioConfig = resources.getFraction( |
| 1022 | com.android.internal.R.fraction.config_maximumScreenDimRatio, 1, 1); |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 1023 | mSupportsDoubleTapWakeConfig = resources.getBoolean( |
| 1024 | com.android.internal.R.bool.config_supportDoubleTapWake); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1027 | private void updateSettingsLocked() { |
| 1028 | final ContentResolver resolver = mContext.getContentResolver(); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 1029 | |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 1030 | mDreamsEnabledSetting = (Settings.Secure.getIntForUser(resolver, |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 1031 | Settings.Secure.SCREENSAVER_ENABLED, |
| 1032 | mDreamsEnabledByDefaultConfig ? 1 : 0, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 1033 | UserHandle.USER_CURRENT) != 0); |
| 1034 | mDreamsActivateOnSleepSetting = (Settings.Secure.getIntForUser(resolver, |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 1035 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP, |
| 1036 | mDreamsActivatedOnSleepByDefaultConfig ? 1 : 0, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 1037 | UserHandle.USER_CURRENT) != 0); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 1038 | mDreamsActivateOnDockSetting = (Settings.Secure.getIntForUser(resolver, |
John Spurlock | ed108f3 | 2012-10-18 16:49:24 -0400 | [diff] [blame] | 1039 | Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK, |
| 1040 | mDreamsActivatedOnDockByDefaultConfig ? 1 : 0, |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 1041 | UserHandle.USER_CURRENT) != 0); |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 1042 | mScreenOffTimeoutSetting = Settings.System.getIntForUser(resolver, |
| 1043 | Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT, |
| 1044 | UserHandle.USER_CURRENT); |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 1045 | mSleepTimeoutSetting = Settings.Secure.getIntForUser(resolver, |
| 1046 | Settings.Secure.SLEEP_TIMEOUT, DEFAULT_SLEEP_TIMEOUT, |
| 1047 | UserHandle.USER_CURRENT); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 1048 | mAttentiveTimeoutSetting = Settings.Secure.getIntForUser(resolver, |
| 1049 | Settings.Secure.ATTENTIVE_TIMEOUT, mAttentiveTimeoutConfig, |
| 1050 | UserHandle.USER_CURRENT); |
Christopher Tate | ad73532 | 2012-09-07 14:19:43 -0700 | [diff] [blame] | 1051 | mStayOnWhilePluggedInSetting = Settings.Global.getInt(resolver, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 1052 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, BatteryManager.BATTERY_PLUGGED_AC); |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 1053 | mTheaterModeEnabled = Settings.Global.getInt(mContext.getContentResolver(), |
| 1054 | Settings.Global.THEATER_MODE_ON, 0) == 1; |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 1055 | mAlwaysOnEnabled = mAmbientDisplayConfiguration.alwaysOnEnabled(UserHandle.USER_CURRENT); |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 1056 | |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 1057 | if (mSupportsDoubleTapWakeConfig) { |
| 1058 | boolean doubleTapWakeEnabled = Settings.Secure.getIntForUser(resolver, |
| 1059 | Settings.Secure.DOUBLE_TAP_TO_WAKE, DEFAULT_DOUBLE_TAP_TO_WAKE, |
| 1060 | UserHandle.USER_CURRENT) != 0; |
| 1061 | if (doubleTapWakeEnabled != mDoubleTapWakeEnabled) { |
| 1062 | mDoubleTapWakeEnabled = doubleTapWakeEnabled; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 1063 | mNativeWrapper.nativeSetFeature( |
| 1064 | POWER_FEATURE_DOUBLE_TAP_TO_WAKE, mDoubleTapWakeEnabled ? 1 : 0); |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
Christine Franks | 732c043 | 2017-06-23 18:12:46 -0700 | [diff] [blame] | 1068 | final String retailDemoValue = UserManager.isDeviceInDemoMode(mContext) ? "1" : "0"; |
| 1069 | if (!retailDemoValue.equals(SystemProperties.get(SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED))) { |
| 1070 | SystemProperties.set(SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED, retailDemoValue); |
| 1071 | } |
| 1072 | |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 1073 | mScreenBrightnessModeSetting = Settings.System.getIntForUser(resolver, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1074 | Settings.System.SCREEN_BRIGHTNESS_MODE, |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 1075 | Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL, UserHandle.USER_CURRENT); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1076 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1077 | mDirty |= DIRTY_SETTINGS; |
| 1078 | } |
| 1079 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1080 | private void handleSettingsChangedLocked() { |
| 1081 | updateSettingsLocked(); |
| 1082 | updatePowerStateLocked(); |
| 1083 | } |
| 1084 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1085 | private void acquireWakeLockInternal(IBinder lock, int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1086 | WorkSource ws, String historyTag, int uid, int pid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1087 | synchronized (mLock) { |
| 1088 | if (DEBUG_SPEW) { |
| 1089 | Slog.d(TAG, "acquireWakeLockInternal: lock=" + Objects.hashCode(lock) |
| 1090 | + ", flags=0x" + Integer.toHexString(flags) |
| 1091 | + ", tag=\"" + tag + "\", ws=" + ws + ", uid=" + uid + ", pid=" + pid); |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1092 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1094 | WakeLock wakeLock; |
| 1095 | int index = findWakeLockIndexLocked(lock); |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 1096 | boolean notifyAcquire; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1097 | if (index >= 0) { |
| 1098 | wakeLock = mWakeLocks.get(index); |
| 1099 | if (!wakeLock.hasSameProperties(flags, tag, ws, uid, pid)) { |
| 1100 | // Update existing wake lock. This shouldn't happen but is harmless. |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1101 | notifyWakeLockChangingLocked(wakeLock, flags, tag, packageName, |
| 1102 | uid, pid, ws, historyTag); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1103 | wakeLock.updateProperties(flags, tag, packageName, ws, historyTag, uid, pid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | } |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 1105 | notifyAcquire = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | } else { |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 1107 | UidState state = mUidState.get(uid); |
| 1108 | if (state == null) { |
| 1109 | state = new UidState(uid); |
| 1110 | state.mProcState = ActivityManager.PROCESS_STATE_NONEXISTENT; |
| 1111 | mUidState.put(uid, state); |
| 1112 | } |
| 1113 | state.mNumWakeLocks++; |
| 1114 | wakeLock = new WakeLock(lock, flags, tag, packageName, ws, historyTag, uid, pid, |
| 1115 | state); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1116 | try { |
| 1117 | lock.linkToDeath(wakeLock, 0); |
| 1118 | } catch (RemoteException ex) { |
| 1119 | throw new IllegalArgumentException("Wake lock is already dead."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1121 | mWakeLocks.add(wakeLock); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 1122 | setWakeLockDisabledStateLocked(wakeLock); |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 1123 | notifyAcquire = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1126 | applyWakeLockFlagsOnAcquireLocked(wakeLock, uid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1127 | mDirty |= DIRTY_WAKE_LOCKS; |
| 1128 | updatePowerStateLocked(); |
Dianne Hackborn | 0d192a9 | 2014-07-15 16:39:47 -0700 | [diff] [blame] | 1129 | if (notifyAcquire) { |
| 1130 | // This needs to be done last so we are sure we have acquired the |
| 1131 | // kernel wake lock. Otherwise we have a race where the system may |
| 1132 | // go to sleep between the time we start the accounting in battery |
| 1133 | // stats and when we actually get around to telling the kernel to |
| 1134 | // stay awake. |
| 1135 | notifyWakeLockAcquiredLocked(wakeLock); |
| 1136 | } |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1140 | @SuppressWarnings("deprecation") |
Craig Mautner | 6edb6db | 2012-11-20 18:21:12 -0800 | [diff] [blame] | 1141 | private static boolean isScreenLock(final WakeLock wakeLock) { |
| 1142 | switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 1143 | case PowerManager.FULL_WAKE_LOCK: |
| 1144 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 1145 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 1146 | return true; |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Artem Iglikov | c474bfd0 | 2018-09-25 11:56:09 -0700 | [diff] [blame] | 1151 | private static WorkChain getFirstNonEmptyWorkChain(WorkSource workSource) { |
| 1152 | if (workSource.getWorkChains() == null) { |
| 1153 | return null; |
| 1154 | } |
| 1155 | |
| 1156 | for (WorkChain workChain: workSource.getWorkChains()) { |
| 1157 | if (workChain.getSize() > 0) { |
| 1158 | return workChain; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | return null; |
| 1163 | } |
| 1164 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1165 | private void applyWakeLockFlagsOnAcquireLocked(WakeLock wakeLock, int uid) { |
Jeff Brown | 6eade79 | 2013-09-10 18:45:25 -0700 | [diff] [blame] | 1166 | if ((wakeLock.mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0 |
| 1167 | && isScreenLock(wakeLock)) { |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1168 | String opPackageName; |
| 1169 | int opUid; |
Artem Iglikov | c474bfd0 | 2018-09-25 11:56:09 -0700 | [diff] [blame] | 1170 | if (wakeLock.mWorkSource != null && !wakeLock.mWorkSource.isEmpty()) { |
| 1171 | WorkSource workSource = wakeLock.mWorkSource; |
| 1172 | WorkChain workChain = getFirstNonEmptyWorkChain(workSource); |
| 1173 | if (workChain != null) { |
| 1174 | opPackageName = workChain.getAttributionTag(); |
| 1175 | opUid = workChain.getAttributionUid(); |
| 1176 | } else { |
| 1177 | opPackageName = workSource.getName(0) != null |
| 1178 | ? workSource.getName(0) : wakeLock.mPackageName; |
| 1179 | opUid = workSource.get(0); |
| 1180 | } |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1181 | } else { |
| 1182 | opPackageName = wakeLock.mPackageName; |
Artem Iglikov | c474bfd0 | 2018-09-25 11:56:09 -0700 | [diff] [blame] | 1183 | opUid = wakeLock.mOwnerUid; |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1184 | } |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1185 | wakeUpNoUpdateLocked(SystemClock.uptimeMillis(), |
| 1186 | PowerManager.WAKE_REASON_APPLICATION, wakeLock.mTag, |
| 1187 | opUid, opPackageName, opUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1191 | private void releaseWakeLockInternal(IBinder lock, int flags) { |
| 1192 | synchronized (mLock) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1193 | int index = findWakeLockIndexLocked(lock); |
| 1194 | if (index < 0) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 1195 | if (DEBUG_SPEW) { |
| 1196 | Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock) |
| 1197 | + " [not found], flags=0x" + Integer.toHexString(flags)); |
| 1198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | return; |
| 1200 | } |
Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 1201 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1202 | WakeLock wakeLock = mWakeLocks.get(index); |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 1203 | if (DEBUG_SPEW) { |
| 1204 | Slog.d(TAG, "releaseWakeLockInternal: lock=" + Objects.hashCode(lock) |
| 1205 | + " [" + wakeLock.mTag + "], flags=0x" + Integer.toHexString(flags)); |
| 1206 | } |
| 1207 | |
Michael Wright | 1208e27 | 2014-09-08 19:57:50 -0700 | [diff] [blame] | 1208 | if ((flags & PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY) != 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1209 | mRequestWaitForNegativeProximity = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1212 | wakeLock.mLock.unlinkToDeath(wakeLock, 0); |
| 1213 | removeWakeLockLocked(wakeLock, index); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | } |
| 1215 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1216 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1217 | private void handleWakeLockDeath(WakeLock wakeLock) { |
| 1218 | synchronized (mLock) { |
| 1219 | if (DEBUG_SPEW) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 1220 | Slog.d(TAG, "handleWakeLockDeath: lock=" + Objects.hashCode(wakeLock.mLock) |
| 1221 | + " [" + wakeLock.mTag + "]"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | int index = mWakeLocks.indexOf(wakeLock); |
| 1225 | if (index < 0) { |
| 1226 | return; |
| 1227 | } |
| 1228 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1229 | removeWakeLockLocked(wakeLock, index); |
Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1230 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1231 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1232 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1233 | private void removeWakeLockLocked(WakeLock wakeLock, int index) { |
| 1234 | mWakeLocks.remove(index); |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 1235 | UidState state = wakeLock.mUidState; |
| 1236 | state.mNumWakeLocks--; |
| 1237 | if (state.mNumWakeLocks <= 0 && |
| 1238 | state.mProcState == ActivityManager.PROCESS_STATE_NONEXISTENT) { |
| 1239 | mUidState.remove(state.mUid); |
| 1240 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1241 | notifyWakeLockReleasedLocked(wakeLock); |
| 1242 | |
| 1243 | applyWakeLockFlagsOnReleaseLocked(wakeLock); |
| 1244 | mDirty |= DIRTY_WAKE_LOCKS; |
| 1245 | updatePowerStateLocked(); |
| 1246 | } |
| 1247 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1248 | private void applyWakeLockFlagsOnReleaseLocked(WakeLock wakeLock) { |
Jeff Brown | 6eade79 | 2013-09-10 18:45:25 -0700 | [diff] [blame] | 1249 | if ((wakeLock.mFlags & PowerManager.ON_AFTER_RELEASE) != 0 |
| 1250 | && isScreenLock(wakeLock)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1251 | userActivityNoUpdateLocked(SystemClock.uptimeMillis(), |
| 1252 | PowerManager.USER_ACTIVITY_EVENT_OTHER, |
| 1253 | PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS, |
| 1254 | wakeLock.mOwnerUid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 1258 | private void updateWakeLockWorkSourceInternal(IBinder lock, WorkSource ws, String historyTag, |
| 1259 | int callingUid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1260 | synchronized (mLock) { |
| 1261 | int index = findWakeLockIndexLocked(lock); |
| 1262 | if (index < 0) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 1263 | if (DEBUG_SPEW) { |
| 1264 | Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock) |
| 1265 | + " [not found], ws=" + ws); |
| 1266 | } |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 1267 | throw new IllegalArgumentException("Wake lock not active: " + lock |
| 1268 | + " from uid " + callingUid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | WakeLock wakeLock = mWakeLocks.get(index); |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 1272 | if (DEBUG_SPEW) { |
| 1273 | Slog.d(TAG, "updateWakeLockWorkSourceInternal: lock=" + Objects.hashCode(lock) |
| 1274 | + " [" + wakeLock.mTag + "], ws=" + ws); |
| 1275 | } |
| 1276 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1277 | if (!wakeLock.hasSameWorkSource(ws)) { |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1278 | notifyWakeLockChangingLocked(wakeLock, wakeLock.mFlags, wakeLock.mTag, |
| 1279 | wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid, |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 1280 | ws, historyTag); |
| 1281 | wakeLock.mHistoryTag = historyTag; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1282 | wakeLock.updateWorkSource(ws); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | private int findWakeLockIndexLocked(IBinder lock) { |
| 1288 | final int count = mWakeLocks.size(); |
| 1289 | for (int i = 0; i < count; i++) { |
| 1290 | if (mWakeLocks.get(i).mLock == lock) { |
| 1291 | return i; |
| 1292 | } |
| 1293 | } |
| 1294 | return -1; |
| 1295 | } |
| 1296 | |
| 1297 | private void notifyWakeLockAcquiredLocked(WakeLock wakeLock) { |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 1298 | if (mSystemReady && !wakeLock.mDisabled) { |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1299 | wakeLock.mNotifiedAcquired = true; |
| 1300 | mNotifier.onWakeLockAcquired(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1301 | wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource, |
| 1302 | wakeLock.mHistoryTag); |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 1303 | restartNofifyLongTimerLocked(wakeLock); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | private void enqueueNotifyLongMsgLocked(long time) { |
| 1308 | mNotifyLongScheduled = time; |
| 1309 | Message msg = mHandler.obtainMessage(MSG_CHECK_FOR_LONG_WAKELOCKS); |
| 1310 | msg.setAsynchronous(true); |
| 1311 | mHandler.sendMessageAtTime(msg, time); |
| 1312 | } |
| 1313 | |
| 1314 | private void restartNofifyLongTimerLocked(WakeLock wakeLock) { |
| 1315 | wakeLock.mAcquireTime = SystemClock.uptimeMillis(); |
| 1316 | if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) |
| 1317 | == PowerManager.PARTIAL_WAKE_LOCK && mNotifyLongScheduled == 0) { |
| 1318 | enqueueNotifyLongMsgLocked(wakeLock.mAcquireTime + MIN_LONG_WAKE_CHECK_INTERVAL); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | private void notifyWakeLockLongStartedLocked(WakeLock wakeLock) { |
| 1323 | if (mSystemReady && !wakeLock.mDisabled) { |
| 1324 | wakeLock.mNotifiedLong = true; |
| 1325 | mNotifier.onLongPartialWakeLockStart(wakeLock.mTag, wakeLock.mOwnerUid, |
| 1326 | wakeLock.mWorkSource, wakeLock.mHistoryTag); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | private void notifyWakeLockLongFinishedLocked(WakeLock wakeLock) { |
| 1331 | if (wakeLock.mNotifiedLong) { |
| 1332 | wakeLock.mNotifiedLong = false; |
| 1333 | mNotifier.onLongPartialWakeLockFinish(wakeLock.mTag, wakeLock.mOwnerUid, |
| 1334 | wakeLock.mWorkSource, wakeLock.mHistoryTag); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1338 | private void notifyWakeLockChangingLocked(WakeLock wakeLock, int flags, String tag, |
| 1339 | String packageName, int uid, int pid, WorkSource ws, String historyTag) { |
| 1340 | if (mSystemReady && wakeLock.mNotifiedAcquired) { |
| 1341 | mNotifier.onWakeLockChanging(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName, |
| 1342 | wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource, |
| 1343 | wakeLock.mHistoryTag, flags, tag, packageName, uid, pid, ws, historyTag); |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 1344 | notifyWakeLockLongFinishedLocked(wakeLock); |
| 1345 | // Changing the wake lock will count as releasing the old wake lock(s) and |
| 1346 | // acquiring the new ones... we do this because otherwise once a wakelock |
| 1347 | // becomes long, if we just continued to treat it as long we can get in to |
| 1348 | // situations where we spam battery stats with every following change to it. |
| 1349 | restartNofifyLongTimerLocked(wakeLock); |
Dianne Hackborn | e5167ca | 2014-03-08 14:39:10 -0800 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1353 | private void notifyWakeLockReleasedLocked(WakeLock wakeLock) { |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 1354 | if (mSystemReady && wakeLock.mNotifiedAcquired) { |
| 1355 | wakeLock.mNotifiedAcquired = false; |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 1356 | wakeLock.mAcquireTime = 0; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 1357 | mNotifier.onWakeLockReleased(wakeLock.mFlags, wakeLock.mTag, |
| 1358 | wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid, |
| 1359 | wakeLock.mWorkSource, wakeLock.mHistoryTag); |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 1360 | notifyWakeLockLongFinishedLocked(wakeLock); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1364 | @SuppressWarnings("deprecation") |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1365 | private boolean isWakeLockLevelSupportedInternal(int level) { |
| 1366 | synchronized (mLock) { |
| 1367 | switch (level) { |
| 1368 | case PowerManager.PARTIAL_WAKE_LOCK: |
| 1369 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 1370 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 1371 | case PowerManager.FULL_WAKE_LOCK: |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1372 | case PowerManager.DOZE_WAKE_LOCK: |
Jeff Brown | c4bd42c | 2015-06-12 18:00:11 -0700 | [diff] [blame] | 1373 | case PowerManager.DRAW_WAKE_LOCK: |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1374 | return true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1375 | |
| 1376 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1377 | return mSystemReady && mDisplayManagerInternal.isProximitySensorAvailable(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1378 | |
| 1379 | default: |
| 1380 | return false; |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1385 | // Called from native code. |
| 1386 | private void userActivityFromNative(long eventTime, int event, int flags) { |
| 1387 | userActivityInternal(eventTime, event, flags, Process.SYSTEM_UID); |
| 1388 | } |
| 1389 | |
| 1390 | private void userActivityInternal(long eventTime, int event, int flags, int uid) { |
| 1391 | synchronized (mLock) { |
| 1392 | if (userActivityNoUpdateLocked(eventTime, event, flags, uid)) { |
| 1393 | updatePowerStateLocked(); |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 1398 | private void onUserAttention() { |
| 1399 | synchronized (mLock) { |
| 1400 | if (userActivityNoUpdateLocked(SystemClock.uptimeMillis(), |
| 1401 | PowerManager.USER_ACTIVITY_EVENT_ATTENTION, 0 /* flags */, |
| 1402 | Process.SYSTEM_UID)) { |
| 1403 | updatePowerStateLocked(); |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1408 | private boolean userActivityNoUpdateLocked(long eventTime, int event, int flags, int uid) { |
| 1409 | if (DEBUG_SPEW) { |
| 1410 | Slog.d(TAG, "userActivityNoUpdateLocked: eventTime=" + eventTime |
| 1411 | + ", event=" + event + ", flags=0x" + Integer.toHexString(flags) |
| 1412 | + ", uid=" + uid); |
| 1413 | } |
| 1414 | |
| 1415 | if (eventTime < mLastSleepTime || eventTime < mLastWakeTime |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1416 | || !mBootCompleted || !mSystemReady) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1417 | return false; |
| 1418 | } |
| 1419 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1420 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "userActivity"); |
| 1421 | try { |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1422 | if (eventTime > mLastInteractivePowerHintTime) { |
Ruchi Kandoi | 0d43404 | 2016-10-03 09:12:02 -0700 | [diff] [blame] | 1423 | powerHintInternal(PowerHint.INTERACTION, 0); |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1424 | mLastInteractivePowerHintTime = eventTime; |
| 1425 | } |
| 1426 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1427 | mNotifier.onUserActivity(event, uid); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 1428 | mAttentionDetector.onUserActivity(eventTime, event); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1429 | |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 1430 | if (mUserInactiveOverrideFromWindowManager) { |
| 1431 | mUserInactiveOverrideFromWindowManager = false; |
Nick Armstrong-Crews | 024872e | 2015-09-30 16:49:19 -0700 | [diff] [blame] | 1432 | mOverriddenTimeout = -1; |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 1435 | if (mWakefulness == WAKEFULNESS_ASLEEP |
| 1436 | || mWakefulness == WAKEFULNESS_DOZING |
| 1437 | || (flags & PowerManager.USER_ACTIVITY_FLAG_INDIRECT) != 0) { |
| 1438 | return false; |
| 1439 | } |
| 1440 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1441 | maybeUpdateForegroundProfileLastActivityLocked(eventTime); |
| 1442 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1443 | if ((flags & PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS) != 0) { |
| 1444 | if (eventTime > mLastUserActivityTimeNoChangeLights |
| 1445 | && eventTime > mLastUserActivityTime) { |
| 1446 | mLastUserActivityTimeNoChangeLights = eventTime; |
| 1447 | mDirty |= DIRTY_USER_ACTIVITY; |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 1448 | if (event == PowerManager.USER_ACTIVITY_EVENT_BUTTON) { |
| 1449 | mDirty |= DIRTY_QUIESCENT; |
| 1450 | } |
| 1451 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1452 | return true; |
| 1453 | } |
| 1454 | } else { |
| 1455 | if (eventTime > mLastUserActivityTime) { |
| 1456 | mLastUserActivityTime = eventTime; |
| 1457 | mDirty |= DIRTY_USER_ACTIVITY; |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 1458 | if (event == PowerManager.USER_ACTIVITY_EVENT_BUTTON) { |
| 1459 | mDirty |= DIRTY_QUIESCENT; |
| 1460 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1461 | return true; |
| 1462 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1463 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1464 | } finally { |
| 1465 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1466 | } |
| 1467 | return false; |
| 1468 | } |
| 1469 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1470 | private void maybeUpdateForegroundProfileLastActivityLocked(long eventTime) { |
| 1471 | final ProfilePowerState profile = mProfilePowerState.get(mForegroundProfile); |
| 1472 | if (profile != null && eventTime > profile.mLastUserActivityTime) { |
| 1473 | profile.mLastUserActivityTime = eventTime; |
| 1474 | } |
| 1475 | } |
| 1476 | |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1477 | private void wakeUpInternal(long eventTime, @WakeReason int reason, String details, int uid, |
| 1478 | String opPackageName, int opUid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1479 | synchronized (mLock) { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1480 | if (wakeUpNoUpdateLocked(eventTime, reason, details, uid, opPackageName, opUid)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1481 | updatePowerStateLocked(); |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1482 | } |
| 1483 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1484 | } |
Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1485 | |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1486 | private boolean wakeUpNoUpdateLocked(long eventTime, @WakeReason int reason, String details, |
| 1487 | int reasonUid, String opPackageName, int opUid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1488 | if (DEBUG_SPEW) { |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1489 | Slog.d(TAG, "wakeUpNoUpdateLocked: eventTime=" + eventTime + ", uid=" + reasonUid); |
Joe Onorato | 60607a9 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 1490 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1491 | |
| 1492 | if (eventTime < mLastSleepTime || mWakefulness == WAKEFULNESS_AWAKE |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 1493 | || !mBootCompleted || !mSystemReady || mForceSuspendActive) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1494 | return false; |
| 1495 | } |
| 1496 | |
Makoto Onuki | 5ab5e1d | 2017-04-14 17:04:26 -0700 | [diff] [blame] | 1497 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, TRACE_SCREEN_ON, 0); |
| 1498 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1499 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "wakeUp"); |
| 1500 | try { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1501 | Slog.i(TAG, "Waking up from " |
| 1502 | + PowerManagerInternal.wakefulnessToString(mWakefulness) |
| 1503 | + " (uid=" + reasonUid |
| 1504 | + ", reason=" + PowerManager.wakeReasonToString(reason) |
| 1505 | + ", details=" + details |
| 1506 | + ")..."); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1507 | |
| 1508 | mLastWakeTime = eventTime; |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1509 | mLastWakeReason = reason; |
| 1510 | setWakefulnessLocked(WAKEFULNESS_AWAKE, reason, eventTime); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1511 | |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1512 | mNotifier.onWakeUp(reason, details, reasonUid, opPackageName, opUid); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1513 | userActivityNoUpdateLocked( |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1514 | eventTime, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, reasonUid); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1515 | } finally { |
| 1516 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1517 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1518 | return true; |
| 1519 | } |
| 1520 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1521 | private void goToSleepInternal(long eventTime, int reason, int flags, int uid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1522 | synchronized (mLock) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1523 | if (goToSleepNoUpdateLocked(eventTime, reason, flags, uid)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1524 | updatePowerStateLocked(); |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 1529 | /** |
| 1530 | * Puts the system in doze. |
| 1531 | * |
| 1532 | * This method is called goToSleep for historical reasons but actually attempts to DOZE, |
| 1533 | * and only tucks itself in to SLEEP if requested with the flag |
| 1534 | * {@link PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE}. |
| 1535 | */ |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1536 | @SuppressWarnings("deprecation") |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1537 | private boolean goToSleepNoUpdateLocked(long eventTime, int reason, int flags, int uid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1538 | if (DEBUG_SPEW) { |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1539 | Slog.d(TAG, "goToSleepNoUpdateLocked: eventTime=" + eventTime |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1540 | + ", reason=" + reason + ", flags=" + flags + ", uid=" + uid); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1543 | if (eventTime < mLastWakeTime |
| 1544 | || mWakefulness == WAKEFULNESS_ASLEEP |
| 1545 | || mWakefulness == WAKEFULNESS_DOZING |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1546 | || !mBootCompleted || !mSystemReady) { |
| 1547 | return false; |
| 1548 | } |
| 1549 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1550 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "goToSleep"); |
| 1551 | try { |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 1552 | reason = Math.min(PowerManager.GO_TO_SLEEP_REASON_MAX, |
| 1553 | Math.max(reason, PowerManager.GO_TO_SLEEP_REASON_MIN)); |
| 1554 | Slog.i(TAG, "Going to sleep due to " + PowerManager.sleepReasonToString(reason) |
| 1555 | + " (uid " + uid + ")..."); |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1556 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1557 | mLastSleepTime = eventTime; |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 1558 | mLastSleepReason = reason; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1559 | mSandmanSummoned = true; |
Santos Cordon | 0304438 | 2019-09-19 16:59:01 +0100 | [diff] [blame] | 1560 | mDozeStartInProgress = true; |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1561 | setWakefulnessLocked(WAKEFULNESS_DOZING, reason, eventTime); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1562 | |
| 1563 | // Report the number of wake locks that will be cleared by going to sleep. |
| 1564 | int numWakeLocksCleared = 0; |
| 1565 | final int numWakeLocks = mWakeLocks.size(); |
| 1566 | for (int i = 0; i < numWakeLocks; i++) { |
| 1567 | final WakeLock wakeLock = mWakeLocks.get(i); |
| 1568 | switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 1569 | case PowerManager.FULL_WAKE_LOCK: |
| 1570 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 1571 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 1572 | numWakeLocksCleared += 1; |
| 1573 | break; |
| 1574 | } |
| 1575 | } |
Michael Wright | b55e3a1 | 2018-03-06 15:14:06 +0000 | [diff] [blame] | 1576 | EventLogTags.writePowerSleepRequested(numWakeLocksCleared); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1577 | |
| 1578 | // Skip dozing if requested. |
| 1579 | if ((flags & PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE) != 0) { |
| 1580 | reallyGoToSleepNoUpdateLocked(eventTime, uid); |
| 1581 | } |
| 1582 | } finally { |
| 1583 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 1584 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1585 | return true; |
| 1586 | } |
| 1587 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1588 | private void napInternal(long eventTime, int uid) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1589 | synchronized (mLock) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1590 | if (napNoUpdateLocked(eventTime, uid)) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1591 | updatePowerStateLocked(); |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1596 | private boolean napNoUpdateLocked(long eventTime, int uid) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1597 | if (DEBUG_SPEW) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1598 | Slog.d(TAG, "napNoUpdateLocked: eventTime=" + eventTime + ", uid=" + uid); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | if (eventTime < mLastWakeTime || mWakefulness != WAKEFULNESS_AWAKE |
| 1602 | || !mBootCompleted || !mSystemReady) { |
| 1603 | return false; |
| 1604 | } |
| 1605 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1606 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "nap"); |
| 1607 | try { |
| 1608 | Slog.i(TAG, "Nap time (uid " + uid +")..."); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1609 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1610 | mSandmanSummoned = true; |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1611 | setWakefulnessLocked(WAKEFULNESS_DREAMING, 0, eventTime); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1612 | } finally { |
| 1613 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 1614 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1615 | return true; |
| 1616 | } |
| 1617 | |
| 1618 | // Done dozing, drop everything and go to sleep. |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1619 | private boolean reallyGoToSleepNoUpdateLocked(long eventTime, int uid) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1620 | if (DEBUG_SPEW) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 1621 | Slog.d(TAG, "reallyGoToSleepNoUpdateLocked: eventTime=" + eventTime |
| 1622 | + ", uid=" + uid); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | if (eventTime < mLastWakeTime || mWakefulness == WAKEFULNESS_ASLEEP |
| 1626 | || !mBootCompleted || !mSystemReady) { |
| 1627 | return false; |
| 1628 | } |
| 1629 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1630 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "reallyGoToSleep"); |
| 1631 | try { |
| 1632 | Slog.i(TAG, "Sleeping (uid " + uid +")..."); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1633 | |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1634 | setWakefulnessLocked(WAKEFULNESS_ASLEEP, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, |
| 1635 | eventTime); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1636 | } finally { |
| 1637 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 1638 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 1639 | return true; |
| 1640 | } |
| 1641 | |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 1642 | @VisibleForTesting |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1643 | void setWakefulnessLocked(int wakefulness, int reason, long eventTime) { |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 1644 | if (mWakefulness != wakefulness) { |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 1645 | mWakefulness = wakefulness; |
| 1646 | mWakefulnessChanging = true; |
| 1647 | mDirty |= DIRTY_WAKEFULNESS; |
Santos Cordon | 0304438 | 2019-09-19 16:59:01 +0100 | [diff] [blame] | 1648 | |
| 1649 | // This is only valid while we are in wakefulness dozing. Set to false otherwise. |
| 1650 | mDozeStartInProgress &= (mWakefulness == WAKEFULNESS_DOZING); |
| 1651 | |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 1652 | if (mNotifier != null) { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1653 | mNotifier.onWakefulnessChangeStarted(wakefulness, reason, eventTime); |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 1654 | } |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 1655 | mAttentionDetector.onWakefulnessChangeStarted(wakefulness); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1656 | } |
| 1657 | } |
| 1658 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 1659 | @VisibleForTesting |
| 1660 | int getWakefulness() { |
| 1661 | return mWakefulness; |
| 1662 | } |
| 1663 | |
Nick Armstrong-Crews | 024872e | 2015-09-30 16:49:19 -0700 | [diff] [blame] | 1664 | /** |
| 1665 | * Logs the time the device would have spent awake before user activity timeout, |
| 1666 | * had the system not been told the user was inactive. |
| 1667 | */ |
| 1668 | private void logSleepTimeoutRecapturedLocked() { |
| 1669 | final long now = SystemClock.uptimeMillis(); |
| 1670 | final long savedWakeTimeMs = mOverriddenTimeout - now; |
| 1671 | if (savedWakeTimeMs >= 0) { |
Michael Wright | b55e3a1 | 2018-03-06 15:14:06 +0000 | [diff] [blame] | 1672 | EventLogTags.writePowerSoftSleepRequested(savedWakeTimeMs); |
Nick Armstrong-Crews | 024872e | 2015-09-30 16:49:19 -0700 | [diff] [blame] | 1673 | mOverriddenTimeout = -1; |
| 1674 | } |
| 1675 | } |
| 1676 | |
Jeff Brown | 416c49c | 2015-05-26 19:50:18 -0700 | [diff] [blame] | 1677 | private void finishWakefulnessChangeIfNeededLocked() { |
| 1678 | if (mWakefulnessChanging && mDisplayReady) { |
| 1679 | if (mWakefulness == WAKEFULNESS_DOZING |
| 1680 | && (mWakeLockSummary & WAKE_LOCK_DOZE) == 0) { |
| 1681 | return; // wait until dream has enabled dozing |
Santos Cordon | 0304438 | 2019-09-19 16:59:01 +0100 | [diff] [blame] | 1682 | } else { |
| 1683 | // Doze wakelock acquired (doze started) or device is no longer dozing. |
| 1684 | mDozeStartInProgress = false; |
Jeff Brown | 416c49c | 2015-05-26 19:50:18 -0700 | [diff] [blame] | 1685 | } |
Nick Armstrong-Crews | 024872e | 2015-09-30 16:49:19 -0700 | [diff] [blame] | 1686 | if (mWakefulness == WAKEFULNESS_DOZING || mWakefulness == WAKEFULNESS_ASLEEP) { |
| 1687 | logSleepTimeoutRecapturedLocked(); |
| 1688 | } |
Makoto Onuki | 5ab5e1d | 2017-04-14 17:04:26 -0700 | [diff] [blame] | 1689 | if (mWakefulness == WAKEFULNESS_AWAKE) { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1690 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, TRACE_SCREEN_ON, 0); |
| 1691 | final int latencyMs = (int) (SystemClock.uptimeMillis() - mLastWakeTime); |
| 1692 | if (latencyMs >= SCREEN_ON_LATENCY_WARNING_MS) { |
| 1693 | Slog.w(TAG, "Screen on took " + latencyMs + " ms"); |
| 1694 | } |
Makoto Onuki | 5ab5e1d | 2017-04-14 17:04:26 -0700 | [diff] [blame] | 1695 | } |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 1696 | mWakefulnessChanging = false; |
Jeff Brown | 416c49c | 2015-05-26 19:50:18 -0700 | [diff] [blame] | 1697 | mNotifier.onWakefulnessChangeFinished(); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1701 | /** |
| 1702 | * Updates the global power state based on dirty bits recorded in mDirty. |
| 1703 | * |
| 1704 | * This is the main function that performs power state transitions. |
| 1705 | * We centralize them here so that we can recompute the power state completely |
| 1706 | * each time something important changes, and ensure that we do it the same |
| 1707 | * way each time. The point is to gather all of the transition logic here. |
| 1708 | */ |
| 1709 | private void updatePowerStateLocked() { |
| 1710 | if (!mSystemReady || mDirty == 0) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1711 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | } |
Jeff Brown | 2d8a390 | 2014-03-11 23:02:35 -0700 | [diff] [blame] | 1713 | if (!Thread.holdsLock(mLock)) { |
| 1714 | Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked"); |
| 1715 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1716 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1717 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "updatePowerState"); |
| 1718 | try { |
| 1719 | // Phase 0: Basic state updates. |
| 1720 | updateIsPoweredLocked(mDirty); |
| 1721 | updateStayOnLocked(mDirty); |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 1722 | updateScreenBrightnessBoostLocked(mDirty); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1724 | // Phase 1: Update wakefulness. |
| 1725 | // Loop because the wake lock and user activity computations are influenced |
| 1726 | // by changes in wakefulness. |
| 1727 | final long now = SystemClock.uptimeMillis(); |
| 1728 | int dirtyPhase2 = 0; |
| 1729 | for (;;) { |
| 1730 | int dirtyPhase1 = mDirty; |
| 1731 | dirtyPhase2 |= dirtyPhase1; |
| 1732 | mDirty = 0; |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1733 | |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1734 | updateWakeLockSummaryLocked(dirtyPhase1); |
| 1735 | updateUserActivitySummaryLocked(now, dirtyPhase1); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 1736 | updateAttentiveStateLocked(now, dirtyPhase1); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1737 | if (!updateWakefulnessLocked(dirtyPhase1)) { |
| 1738 | break; |
| 1739 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1741 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1742 | // Phase 2: Lock profiles that became inactive/not kept awake. |
| 1743 | updateProfilesLocked(now); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1744 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1745 | // Phase 3: Update display power state. |
| 1746 | final boolean displayBecameReady = updateDisplayPowerStateLocked(dirtyPhase2); |
| 1747 | |
| 1748 | // Phase 4: Update dream state (depends on display ready signal). |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1749 | updateDreamLocked(dirtyPhase2, displayBecameReady); |
| 1750 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1751 | // Phase 5: Send notifications, if needed. |
Jeff Brown | 416c49c | 2015-05-26 19:50:18 -0700 | [diff] [blame] | 1752 | finishWakefulnessChangeIfNeededLocked(); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1753 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1754 | // Phase 6: Update suspend blocker. |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1755 | // Because we might release the last suspend blocker here, we need to make sure |
| 1756 | // we finished everything else first! |
| 1757 | updateSuspendBlockerLocked(); |
| 1758 | } finally { |
| 1759 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1761 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1762 | |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1763 | /** |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1764 | * Check profile timeouts and notify profiles that should be locked. |
| 1765 | */ |
| 1766 | private void updateProfilesLocked(long now) { |
| 1767 | final int numProfiles = mProfilePowerState.size(); |
| 1768 | for (int i = 0; i < numProfiles; i++) { |
| 1769 | final ProfilePowerState profile = mProfilePowerState.valueAt(i); |
| 1770 | if (isProfileBeingKeptAwakeLocked(profile, now)) { |
| 1771 | profile.mLockingNotified = false; |
| 1772 | } else if (!profile.mLockingNotified) { |
| 1773 | profile.mLockingNotified = true; |
| 1774 | mNotifier.onProfileTimeout(profile.mUserId); |
| 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | private boolean isProfileBeingKeptAwakeLocked(ProfilePowerState profile, long now) { |
| 1780 | return (profile.mLastUserActivityTime + profile.mScreenOffTimeout > now) |
| 1781 | || (profile.mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0 |
| 1782 | || (mProximityPositive && |
| 1783 | (profile.mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0); |
| 1784 | } |
| 1785 | |
| 1786 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1787 | * Updates the value of mIsPowered. |
| 1788 | * Sets DIRTY_IS_POWERED if a change occurred. |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1789 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1790 | private void updateIsPoweredLocked(int dirty) { |
| 1791 | if ((dirty & DIRTY_BATTERY_STATE) != 0) { |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1792 | final boolean wasPowered = mIsPowered; |
| 1793 | final int oldPlugType = mPlugType; |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1794 | final boolean oldLevelLow = mBatteryLevelLow; |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1795 | mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY); |
| 1796 | mPlugType = mBatteryManagerInternal.getPlugType(); |
| 1797 | mBatteryLevel = mBatteryManagerInternal.getBatteryLevel(); |
| 1798 | mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow(); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 1799 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1800 | if (DEBUG_SPEW) { |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 1801 | Slog.d(TAG, "updateIsPoweredLocked: wasPowered=" + wasPowered |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1802 | + ", mIsPowered=" + mIsPowered |
| 1803 | + ", oldPlugType=" + oldPlugType |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 1804 | + ", mPlugType=" + mPlugType |
| 1805 | + ", mBatteryLevel=" + mBatteryLevel); |
Jeff Brown | a4d8204 | 2012-10-02 19:11:19 -0700 | [diff] [blame] | 1806 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 1807 | |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1808 | if (wasPowered != mIsPowered || oldPlugType != mPlugType) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1809 | mDirty |= DIRTY_IS_POWERED; |
| 1810 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1811 | // Update wireless dock detection state. |
| 1812 | final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update( |
Beverly | fcaab29 | 2018-03-19 10:42:06 -0400 | [diff] [blame] | 1813 | mIsPowered, mPlugType); |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1814 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1815 | // Treat plugging and unplugging the devices as a user activity. |
| 1816 | // Users find it disconcerting when they plug or unplug the device |
| 1817 | // and it shuts off right away. |
| 1818 | // Some devices also wake the device when plugged or unplugged because |
| 1819 | // they don't have a charging LED. |
| 1820 | final long now = SystemClock.uptimeMillis(); |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1821 | if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType, |
| 1822 | dockedOnWirelessCharger)) { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 1823 | wakeUpNoUpdateLocked(now, PowerManager.WAKE_REASON_PLUGGED_IN, |
| 1824 | "android.server.power:PLUGGED:" + mIsPowered, Process.SYSTEM_UID, |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 1825 | mContext.getOpPackageName(), Process.SYSTEM_UID); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1826 | } |
| 1827 | userActivityNoUpdateLocked( |
| 1828 | now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 1829 | |
Beverly | ac32c9a | 2018-01-31 16:10:41 -0500 | [diff] [blame] | 1830 | // only play charging sounds if boot is completed so charging sounds don't play |
| 1831 | // with potential notification sounds |
| 1832 | if (mBootCompleted) { |
| 1833 | if (mIsPowered && !BatteryManager.isPlugWired(oldPlugType) |
| 1834 | && BatteryManager.isPlugWired(mPlugType)) { |
Beverly | 478722e | 2019-10-14 11:16:57 -0400 | [diff] [blame] | 1835 | mNotifier.onWiredChargingStarted(mUserId); |
Beverly | ac32c9a | 2018-01-31 16:10:41 -0500 | [diff] [blame] | 1836 | } else if (dockedOnWirelessCharger) { |
Beverly | 478722e | 2019-10-14 11:16:57 -0400 | [diff] [blame] | 1837 | mNotifier.onWirelessChargingStarted(mBatteryLevel, mUserId); |
Beverly | ac32c9a | 2018-01-31 16:10:41 -0500 | [diff] [blame] | 1838 | } |
Jeff Brown | 84e2756 | 2012-12-07 13:56:34 -0800 | [diff] [blame] | 1839 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1840 | } |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 1841 | |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 1842 | mBatterySaverStateMachine.setBatteryStatus(mIsPowered, mBatteryLevel, mBatteryLevelLow); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1846 | private boolean shouldWakeUpWhenPluggedOrUnpluggedLocked( |
| 1847 | boolean wasPowered, int oldPlugType, boolean dockedOnWirelessCharger) { |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1848 | // Don't wake when powered unless configured to do so. |
| 1849 | if (!mWakeUpWhenPluggedOrUnpluggedConfig) { |
| 1850 | return false; |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1851 | } |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1852 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1853 | // Don't wake when undocked from wireless charger. |
| 1854 | // See WirelessChargerDetector for justification. |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1855 | if (wasPowered && !mIsPowered |
| 1856 | && oldPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) { |
| 1857 | return false; |
| 1858 | } |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1859 | |
| 1860 | // Don't wake when docked on wireless charger unless we are certain of it. |
| 1861 | // See WirelessChargerDetector for justification. |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1862 | if (!wasPowered && mIsPowered |
| 1863 | && mPlugType == BatteryManager.BATTERY_PLUGGED_WIRELESS |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 1864 | && !dockedOnWirelessCharger) { |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1865 | return false; |
| 1866 | } |
| 1867 | |
| 1868 | // If already dreaming and becoming powered, then don't wake. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1869 | if (mIsPowered && mWakefulness == WAKEFULNESS_DREAMING) { |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1870 | return false; |
| 1871 | } |
| 1872 | |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 1873 | // Don't wake while theater mode is enabled. |
| 1874 | if (mTheaterModeEnabled && !mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig) { |
| 1875 | return false; |
| 1876 | } |
| 1877 | |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 1878 | // On Always On Display, SystemUI shows the charging indicator |
| 1879 | if (mAlwaysOnEnabled && mWakefulness == WAKEFULNESS_DOZING) { |
| 1880 | return false; |
| 1881 | } |
| 1882 | |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 1883 | // Otherwise wake up! |
| 1884 | return true; |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1887 | /** |
| 1888 | * Updates the value of mStayOn. |
| 1889 | * Sets DIRTY_STAY_ON if a change occurred. |
| 1890 | */ |
| 1891 | private void updateStayOnLocked(int dirty) { |
| 1892 | if ((dirty & (DIRTY_BATTERY_STATE | DIRTY_SETTINGS)) != 0) { |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1893 | final boolean wasStayOn = mStayOn; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1894 | if (mStayOnWhilePluggedInSetting != 0 |
| 1895 | && !isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) { |
Jeff Brown | 2139276 | 2014-06-13 19:00:36 -0700 | [diff] [blame] | 1896 | mStayOn = mBatteryManagerInternal.isPowered(mStayOnWhilePluggedInSetting); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1897 | } else { |
| 1898 | mStayOn = false; |
| 1899 | } |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1900 | |
| 1901 | if (mStayOn != wasStayOn) { |
| 1902 | mDirty |= DIRTY_STAY_ON; |
| 1903 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * Updates the value of mWakeLockSummary to summarize the state of all active wake locks. |
| 1909 | * Note that most wake-locks are ignored when the system is asleep. |
| 1910 | * |
| 1911 | * This function must have no other side-effects. |
| 1912 | */ |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1913 | @SuppressWarnings("deprecation") |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1914 | private void updateWakeLockSummaryLocked(int dirty) { |
| 1915 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_WAKEFULNESS)) != 0) { |
| 1916 | mWakeLockSummary = 0; |
| 1917 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1918 | final int numProfiles = mProfilePowerState.size(); |
| 1919 | for (int i = 0; i < numProfiles; i++) { |
| 1920 | mProfilePowerState.valueAt(i).mWakeLockSummary = 0; |
| 1921 | } |
| 1922 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1923 | final int numWakeLocks = mWakeLocks.size(); |
| 1924 | for (int i = 0; i < numWakeLocks; i++) { |
| 1925 | final WakeLock wakeLock = mWakeLocks.get(i); |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1926 | final int wakeLockFlags = getWakeLockSummaryFlags(wakeLock); |
| 1927 | mWakeLockSummary |= wakeLockFlags; |
| 1928 | for (int j = 0; j < numProfiles; j++) { |
| 1929 | final ProfilePowerState profile = mProfilePowerState.valueAt(j); |
| 1930 | if (wakeLockAffectsUser(wakeLock, profile.mUserId)) { |
| 1931 | profile.mWakeLockSummary |= wakeLockFlags; |
| 1932 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1936 | mWakeLockSummary = adjustWakeLockSummaryLocked(mWakeLockSummary); |
| 1937 | for (int i = 0; i < numProfiles; i++) { |
| 1938 | final ProfilePowerState profile = mProfilePowerState.valueAt(i); |
| 1939 | profile.mWakeLockSummary = adjustWakeLockSummaryLocked(profile.mWakeLockSummary); |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 1940 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1941 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1942 | if (DEBUG_SPEW) { |
| 1943 | Slog.d(TAG, "updateWakeLockSummaryLocked: mWakefulness=" |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 1944 | + PowerManagerInternal.wakefulnessToString(mWakefulness) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1945 | + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary)); |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 1950 | private int adjustWakeLockSummaryLocked(int wakeLockSummary) { |
| 1951 | // Cancel wake locks that make no sense based on the current state. |
| 1952 | if (mWakefulness != WAKEFULNESS_DOZING) { |
| 1953 | wakeLockSummary &= ~(WAKE_LOCK_DOZE | WAKE_LOCK_DRAW); |
| 1954 | } |
| 1955 | if (mWakefulness == WAKEFULNESS_ASLEEP |
| 1956 | || (wakeLockSummary & WAKE_LOCK_DOZE) != 0) { |
| 1957 | wakeLockSummary &= ~(WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM |
| 1958 | | WAKE_LOCK_BUTTON_BRIGHT); |
| 1959 | if (mWakefulness == WAKEFULNESS_ASLEEP) { |
| 1960 | wakeLockSummary &= ~WAKE_LOCK_PROXIMITY_SCREEN_OFF; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | // Infer implied wake locks where necessary based on the current state. |
| 1965 | if ((wakeLockSummary & (WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_SCREEN_DIM)) != 0) { |
| 1966 | if (mWakefulness == WAKEFULNESS_AWAKE) { |
| 1967 | wakeLockSummary |= WAKE_LOCK_CPU | WAKE_LOCK_STAY_AWAKE; |
| 1968 | } else if (mWakefulness == WAKEFULNESS_DREAMING) { |
| 1969 | wakeLockSummary |= WAKE_LOCK_CPU; |
| 1970 | } |
| 1971 | } |
| 1972 | if ((wakeLockSummary & WAKE_LOCK_DRAW) != 0) { |
| 1973 | wakeLockSummary |= WAKE_LOCK_CPU; |
| 1974 | } |
| 1975 | |
| 1976 | return wakeLockSummary; |
| 1977 | } |
| 1978 | |
| 1979 | /** Get wake lock summary flags that correspond to the given wake lock. */ |
| 1980 | private int getWakeLockSummaryFlags(WakeLock wakeLock) { |
| 1981 | switch (wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 1982 | case PowerManager.PARTIAL_WAKE_LOCK: |
| 1983 | if (!wakeLock.mDisabled) { |
| 1984 | // We only respect this if the wake lock is not disabled. |
| 1985 | return WAKE_LOCK_CPU; |
| 1986 | } |
| 1987 | break; |
| 1988 | case PowerManager.FULL_WAKE_LOCK: |
| 1989 | return WAKE_LOCK_SCREEN_BRIGHT | WAKE_LOCK_BUTTON_BRIGHT; |
| 1990 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 1991 | return WAKE_LOCK_SCREEN_BRIGHT; |
| 1992 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 1993 | return WAKE_LOCK_SCREEN_DIM; |
| 1994 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| 1995 | return WAKE_LOCK_PROXIMITY_SCREEN_OFF; |
| 1996 | case PowerManager.DOZE_WAKE_LOCK: |
| 1997 | return WAKE_LOCK_DOZE; |
| 1998 | case PowerManager.DRAW_WAKE_LOCK: |
| 1999 | return WAKE_LOCK_DRAW; |
| 2000 | } |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | private boolean wakeLockAffectsUser(WakeLock wakeLock, @UserIdInt int userId) { |
| 2005 | if (wakeLock.mWorkSource != null) { |
| 2006 | for (int k = 0; k < wakeLock.mWorkSource.size(); k++) { |
| 2007 | final int uid = wakeLock.mWorkSource.get(k); |
| 2008 | if (userId == UserHandle.getUserId(uid)) { |
| 2009 | return true; |
| 2010 | } |
| 2011 | } |
Narayan Kamath | 2f916ed | 2017-12-29 13:02:15 +0000 | [diff] [blame] | 2012 | |
| 2013 | final ArrayList<WorkChain> workChains = wakeLock.mWorkSource.getWorkChains(); |
| 2014 | if (workChains != null) { |
| 2015 | for (int k = 0; k < workChains.size(); k++) { |
| 2016 | final int uid = workChains.get(k).getAttributionUid(); |
| 2017 | if (userId == UserHandle.getUserId(uid)) { |
| 2018 | return true; |
| 2019 | } |
| 2020 | } |
| 2021 | } |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2022 | } |
| 2023 | return userId == UserHandle.getUserId(wakeLock.mOwnerUid); |
| 2024 | } |
| 2025 | |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 2026 | void checkForLongWakeLocks() { |
| 2027 | synchronized (mLock) { |
| 2028 | final long now = SystemClock.uptimeMillis(); |
| 2029 | mNotifyLongDispatched = now; |
| 2030 | final long when = now - MIN_LONG_WAKE_CHECK_INTERVAL; |
| 2031 | long nextCheckTime = Long.MAX_VALUE; |
| 2032 | final int numWakeLocks = mWakeLocks.size(); |
| 2033 | for (int i = 0; i < numWakeLocks; i++) { |
| 2034 | final WakeLock wakeLock = mWakeLocks.get(i); |
| 2035 | if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) |
| 2036 | == PowerManager.PARTIAL_WAKE_LOCK) { |
| 2037 | if (wakeLock.mNotifiedAcquired && !wakeLock.mNotifiedLong) { |
| 2038 | if (wakeLock.mAcquireTime < when) { |
| 2039 | // This wake lock has exceeded the long acquire time, report! |
| 2040 | notifyWakeLockLongStartedLocked(wakeLock); |
| 2041 | } else { |
| 2042 | // This wake lock could still become a long one, at this time. |
| 2043 | long checkTime = wakeLock.mAcquireTime + MIN_LONG_WAKE_CHECK_INTERVAL; |
| 2044 | if (checkTime < nextCheckTime) { |
| 2045 | nextCheckTime = checkTime; |
| 2046 | } |
| 2047 | } |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | mNotifyLongScheduled = 0; |
| 2052 | mHandler.removeMessages(MSG_CHECK_FOR_LONG_WAKELOCKS); |
| 2053 | if (nextCheckTime != Long.MAX_VALUE) { |
| 2054 | mNotifyLongNextCheck = nextCheckTime; |
| 2055 | enqueueNotifyLongMsgLocked(nextCheckTime); |
| 2056 | } else { |
| 2057 | mNotifyLongNextCheck = 0; |
| 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2062 | /** |
| 2063 | * Updates the value of mUserActivitySummary to summarize the user requested |
| 2064 | * state of the system such as whether the screen should be bright or dim. |
| 2065 | * Note that user activity is ignored when the system is asleep. |
| 2066 | * |
| 2067 | * This function must have no other side-effects. |
| 2068 | */ |
| 2069 | private void updateUserActivitySummaryLocked(long now, int dirty) { |
| 2070 | // Update the status of the user activity timeout timer. |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2071 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY |
Jeff Brown | a191aa9 | 2014-11-12 23:01:12 -0800 | [diff] [blame] | 2072 | | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2073 | mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT); |
| 2074 | |
| 2075 | long nextTimeout = 0; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2076 | if (mWakefulness == WAKEFULNESS_AWAKE |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2077 | || mWakefulness == WAKEFULNESS_DREAMING |
| 2078 | || mWakefulness == WAKEFULNESS_DOZING) { |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2079 | final long attentiveTimeout = getAttentiveTimeoutLocked(); |
| 2080 | final long sleepTimeout = getSleepTimeoutLocked(attentiveTimeout); |
| 2081 | final long screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout, |
| 2082 | attentiveTimeout); |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2083 | final long screenDimDuration = getScreenDimDurationLocked(screenOffTimeout); |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 2084 | final boolean userInactiveOverride = mUserInactiveOverrideFromWindowManager; |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2085 | final long nextProfileTimeout = getNextProfileTimeoutLocked(now); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2086 | |
| 2087 | mUserActivitySummary = 0; |
| 2088 | if (mLastUserActivityTime >= mLastWakeTime) { |
| 2089 | nextTimeout = mLastUserActivityTime |
| 2090 | + screenOffTimeout - screenDimDuration; |
| 2091 | if (now < nextTimeout) { |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2092 | mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2093 | } else { |
| 2094 | nextTimeout = mLastUserActivityTime + screenOffTimeout; |
| 2095 | if (now < nextTimeout) { |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2096 | mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | } |
| 2100 | if (mUserActivitySummary == 0 |
| 2101 | && mLastUserActivityTimeNoChangeLights >= mLastWakeTime) { |
| 2102 | nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2103 | if (now < nextTimeout) { |
Andrii Kulian | 9407a6b | 2017-05-24 12:24:09 -0700 | [diff] [blame] | 2104 | if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_BRIGHT |
| 2105 | || mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_VR) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2106 | mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT; |
| 2107 | } else if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) { |
| 2108 | mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM; |
| 2109 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2110 | } |
| 2111 | } |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 2112 | |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2113 | if (mUserActivitySummary == 0) { |
| 2114 | if (sleepTimeout >= 0) { |
| 2115 | final long anyUserActivity = Math.max(mLastUserActivityTime, |
| 2116 | mLastUserActivityTimeNoChangeLights); |
| 2117 | if (anyUserActivity >= mLastWakeTime) { |
| 2118 | nextTimeout = anyUserActivity + sleepTimeout; |
| 2119 | if (now < nextTimeout) { |
| 2120 | mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM; |
| 2121 | } |
| 2122 | } |
| 2123 | } else { |
| 2124 | mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM; |
| 2125 | nextTimeout = -1; |
| 2126 | } |
| 2127 | } |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 2128 | |
| 2129 | if (mUserActivitySummary != USER_ACTIVITY_SCREEN_DREAM && userInactiveOverride) { |
Mojtaba | 2d80edc | 2015-09-18 16:35:24 -0700 | [diff] [blame] | 2130 | if ((mUserActivitySummary & |
| 2131 | (USER_ACTIVITY_SCREEN_BRIGHT | USER_ACTIVITY_SCREEN_DIM)) != 0) { |
Nick Armstrong-Crews | 024872e | 2015-09-30 16:49:19 -0700 | [diff] [blame] | 2132 | // Device is being kept awake by recent user activity |
| 2133 | if (nextTimeout >= now && mOverriddenTimeout == -1) { |
| 2134 | // Save when the next timeout would have occurred |
| 2135 | mOverriddenTimeout = nextTimeout; |
| 2136 | } |
Mojtaba | 2d80edc | 2015-09-18 16:35:24 -0700 | [diff] [blame] | 2137 | } |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 2138 | mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM; |
| 2139 | nextTimeout = -1; |
| 2140 | } |
| 2141 | |
Alex Salo | 9e24223 | 2019-05-14 19:27:17 -0700 | [diff] [blame] | 2142 | if ((mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0 |
| 2143 | && (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) == 0) { |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 2144 | nextTimeout = mAttentionDetector.updateUserActivity(nextTimeout); |
| 2145 | } |
| 2146 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2147 | if (nextProfileTimeout > 0) { |
| 2148 | nextTimeout = Math.min(nextTimeout, nextProfileTimeout); |
| 2149 | } |
| 2150 | |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2151 | if (mUserActivitySummary != 0 && nextTimeout >= 0) { |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2152 | scheduleUserInactivityTimeout(nextTimeout); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2153 | } |
| 2154 | } else { |
| 2155 | mUserActivitySummary = 0; |
| 2156 | } |
| 2157 | |
| 2158 | if (DEBUG_SPEW) { |
| 2159 | Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness=" |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 2160 | + PowerManagerInternal.wakefulnessToString(mWakefulness) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2161 | + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary) |
| 2162 | + ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout)); |
| 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2167 | private void scheduleUserInactivityTimeout(long timeMs) { |
| 2168 | final Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT); |
| 2169 | msg.setAsynchronous(true); |
| 2170 | mHandler.sendMessageAtTime(msg, timeMs); |
| 2171 | } |
| 2172 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2173 | private void scheduleAttentiveTimeout(long timeMs) { |
| 2174 | final Message msg = mHandler.obtainMessage(MSG_ATTENTIVE_TIMEOUT); |
| 2175 | msg.setAsynchronous(true); |
| 2176 | mHandler.sendMessageAtTime(msg, timeMs); |
| 2177 | } |
| 2178 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2179 | /** |
| 2180 | * Finds the next profile timeout time or returns -1 if there are no profiles to be locked. |
| 2181 | */ |
| 2182 | private long getNextProfileTimeoutLocked(long now) { |
| 2183 | long nextTimeout = -1; |
| 2184 | final int numProfiles = mProfilePowerState.size(); |
| 2185 | for (int i = 0; i < numProfiles; i++) { |
| 2186 | final ProfilePowerState profile = mProfilePowerState.valueAt(i); |
| 2187 | final long timeout = profile.mLastUserActivityTime + profile.mScreenOffTimeout; |
| 2188 | if (timeout > now && (nextTimeout == -1 || timeout < nextTimeout)) { |
| 2189 | nextTimeout = timeout; |
| 2190 | } |
| 2191 | } |
| 2192 | return nextTimeout; |
| 2193 | } |
| 2194 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2195 | private void updateAttentiveStateLocked(long now, int dirty) { |
| 2196 | long attentiveTimeout = getAttentiveTimeoutLocked(); |
| 2197 | long goToSleepTime = mLastUserActivityTime + attentiveTimeout; |
| 2198 | long showWarningTime = goToSleepTime - mAttentiveWarningDurationConfig; |
| 2199 | |
| 2200 | boolean warningDismissed = maybeHideInattentiveSleepWarningLocked(now, showWarningTime); |
| 2201 | |
| 2202 | if (attentiveTimeout >= 0 && (warningDismissed |
| 2203 | || (dirty & (DIRTY_ATTENTIVE | DIRTY_STAY_ON | DIRTY_SCREEN_BRIGHTNESS_BOOST |
| 2204 | | DIRTY_PROXIMITY_POSITIVE | DIRTY_WAKEFULNESS | DIRTY_BOOT_COMPLETED |
| 2205 | | DIRTY_SETTINGS)) != 0)) { |
| 2206 | if (DEBUG_SPEW) { |
| 2207 | Slog.d(TAG, "Updating attentive state"); |
| 2208 | } |
| 2209 | |
| 2210 | mHandler.removeMessages(MSG_ATTENTIVE_TIMEOUT); |
| 2211 | |
| 2212 | if (isBeingKeptFromShowingInattentiveSleepWarningLocked()) { |
| 2213 | return; |
| 2214 | } |
| 2215 | |
| 2216 | long nextTimeout = -1; |
| 2217 | |
| 2218 | if (now < showWarningTime) { |
| 2219 | nextTimeout = showWarningTime; |
| 2220 | } else if (now < goToSleepTime) { |
| 2221 | if (DEBUG) { |
| 2222 | long timeToSleep = goToSleepTime - now; |
| 2223 | Slog.d(TAG, "Going to sleep in " + timeToSleep |
| 2224 | + "ms if there is no user activity"); |
| 2225 | } |
| 2226 | mInattentiveSleepWarningOverlayController.show(); |
| 2227 | nextTimeout = goToSleepTime; |
| 2228 | } else { |
| 2229 | if (DEBUG && mWakefulness != WAKEFULNESS_ASLEEP) { |
| 2230 | Slog.i(TAG, "Going to sleep now due to long user inactivity"); |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | if (nextTimeout >= 0) { |
| 2235 | scheduleAttentiveTimeout(nextTimeout); |
| 2236 | } |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | private boolean maybeHideInattentiveSleepWarningLocked(long now, long showWarningTime) { |
| 2241 | long attentiveTimeout = getAttentiveTimeoutLocked(); |
| 2242 | |
Robert Horvath | 60e51fe | 2019-08-20 13:10:34 +0200 | [diff] [blame] | 2243 | if (!mInattentiveSleepWarningOverlayController.isShown()) { |
| 2244 | return false; |
| 2245 | } |
| 2246 | |
| 2247 | if (mWakefulness != WAKEFULNESS_AWAKE) { |
| 2248 | mInattentiveSleepWarningOverlayController.dismiss(false); |
| 2249 | return true; |
| 2250 | } else if (attentiveTimeout < 0 || isBeingKeptFromShowingInattentiveSleepWarningLocked() |
| 2251 | || now < showWarningTime) { |
| 2252 | mInattentiveSleepWarningOverlayController.dismiss(true); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2253 | return true; |
| 2254 | } |
| 2255 | |
| 2256 | return false; |
| 2257 | } |
| 2258 | |
| 2259 | private boolean isAttentiveTimeoutExpired(long now) { |
| 2260 | long attentiveTimeout = getAttentiveTimeoutLocked(); |
| 2261 | return attentiveTimeout >= 0 && now > mLastUserActivityTime + attentiveTimeout; |
| 2262 | } |
| 2263 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2264 | /** |
| 2265 | * Called when a user activity timeout has occurred. |
| 2266 | * Simply indicates that something about user activity has changed so that the new |
| 2267 | * state can be recomputed when the power state is updated. |
| 2268 | * |
| 2269 | * This function must have no other side-effects besides setting the dirty |
| 2270 | * bit and calling update power state. Wakefulness transitions are handled elsewhere. |
| 2271 | */ |
| 2272 | private void handleUserActivityTimeout() { // runs on handler thread |
| 2273 | synchronized (mLock) { |
| 2274 | if (DEBUG_SPEW) { |
| 2275 | Slog.d(TAG, "handleUserActivityTimeout"); |
| 2276 | } |
| 2277 | |
| 2278 | mDirty |= DIRTY_USER_ACTIVITY; |
| 2279 | updatePowerStateLocked(); |
| 2280 | } |
| 2281 | } |
| 2282 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2283 | private void handleAttentiveTimeout() { // runs on handler thread |
| 2284 | synchronized (mLock) { |
| 2285 | if (DEBUG_SPEW) { |
| 2286 | Slog.d(TAG, "handleAttentiveTimeout"); |
| 2287 | } |
| 2288 | |
| 2289 | mDirty |= DIRTY_ATTENTIVE; |
| 2290 | updatePowerStateLocked(); |
| 2291 | } |
| 2292 | } |
| 2293 | |
| 2294 | private long getAttentiveTimeoutLocked() { |
| 2295 | long timeout = mAttentiveTimeoutSetting; |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2296 | if (timeout <= 0) { |
| 2297 | return -1; |
| 2298 | } |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2299 | |
| 2300 | return Math.max(timeout, mMinimumScreenOffTimeoutConfig); |
| 2301 | } |
| 2302 | |
| 2303 | private long getSleepTimeoutLocked(long attentiveTimeout) { |
| 2304 | long timeout = mSleepTimeoutSetting; |
| 2305 | if (timeout <= 0) { |
| 2306 | return -1; |
| 2307 | } |
| 2308 | if (attentiveTimeout >= 0) { |
| 2309 | timeout = Math.min(timeout, attentiveTimeout); |
| 2310 | } |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2311 | return Math.max(timeout, mMinimumScreenOffTimeoutConfig); |
| 2312 | } |
| 2313 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2314 | private long getScreenOffTimeoutLocked(long sleepTimeout, long attentiveTimeout) { |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2315 | long timeout = mScreenOffTimeoutSetting; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2316 | if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) { |
| 2317 | timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin); |
| 2318 | } |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 2319 | if (mUserActivityTimeoutOverrideFromWindowManager >= 0) { |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2320 | timeout = Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager); |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 2321 | } |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2322 | if (sleepTimeout >= 0) { |
| 2323 | timeout = Math.min(timeout, sleepTimeout); |
| 2324 | } |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2325 | if (attentiveTimeout >= 0) { |
| 2326 | timeout = Math.min(timeout, attentiveTimeout); |
| 2327 | } |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 2328 | return Math.max(timeout, mMinimumScreenOffTimeoutConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2331 | private long getScreenDimDurationLocked(long screenOffTimeout) { |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 2332 | return Math.min(mMaximumScreenDimDurationConfig, |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 2333 | (long)(screenOffTimeout * mMaximumScreenDimRatioConfig)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | /** |
| 2337 | * Updates the wakefulness of the device. |
| 2338 | * |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2339 | * This is the function that decides whether the device should start dreaming |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2340 | * based on the current wake locks and user activity state. It may modify mDirty |
| 2341 | * if the wakefulness changes. |
| 2342 | * |
| 2343 | * Returns true if the wakefulness changed and we need to restart power state calculation. |
| 2344 | */ |
| 2345 | private boolean updateWakefulnessLocked(int dirty) { |
| 2346 | boolean changed = false; |
| 2347 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 2348 | | DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2349 | | DIRTY_DOCK_STATE | DIRTY_ATTENTIVE)) != 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2350 | if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) { |
| 2351 | if (DEBUG_SPEW) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 2352 | Slog.d(TAG, "updateWakefulnessLocked: Bed time..."); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2353 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 2354 | final long time = SystemClock.uptimeMillis(); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2355 | if (isAttentiveTimeoutExpired(time)) { |
| 2356 | changed = goToSleepNoUpdateLocked(time, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, |
| 2357 | PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE, Process.SYSTEM_UID); |
| 2358 | } else if (shouldNapAtBedTimeLocked()) { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2359 | changed = napNoUpdateLocked(time, Process.SYSTEM_UID); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 2360 | } else { |
| 2361 | changed = goToSleepNoUpdateLocked(time, |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 2362 | PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 2363 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | return changed; |
| 2367 | } |
| 2368 | |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2369 | /** |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 2370 | * Returns true if the device should automatically nap and start dreaming when the user |
| 2371 | * activity timeout has expired and it's bedtime. |
| 2372 | */ |
| 2373 | private boolean shouldNapAtBedTimeLocked() { |
| 2374 | return mDreamsActivateOnSleepSetting |
| 2375 | || (mDreamsActivateOnDockSetting |
| 2376 | && mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 2377 | } |
| 2378 | |
| 2379 | /** |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2380 | * Returns true if the device should go to sleep now. |
| 2381 | * Also used when exiting a dream to determine whether we should go back |
| 2382 | * to being fully awake or else go to sleep for good. |
| 2383 | */ |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2384 | private boolean isItBedTimeYetLocked() { |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2385 | if (!mBootCompleted) { |
| 2386 | return false; |
| 2387 | } |
| 2388 | |
| 2389 | long now = SystemClock.uptimeMillis(); |
| 2390 | if (isAttentiveTimeoutExpired(now)) { |
| 2391 | return !isBeingKeptFromInattentiveSleepLocked(); |
| 2392 | } else { |
| 2393 | return !isBeingKeptAwakeLocked(); |
| 2394 | } |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | /** |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 2398 | * Returns true if the device is being kept awake by a wake lock, user activity |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2399 | * or the stay on while powered setting. We also keep the phone awake when |
| 2400 | * the proximity sensor returns a positive result so that the device does not |
| 2401 | * lock while in a phone call. This function only controls whether the device |
| 2402 | * will go to sleep or dream which is independent of whether it will be allowed |
| 2403 | * to suspend. |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2404 | */ |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 2405 | private boolean isBeingKeptAwakeLocked() { |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2406 | return mStayOn |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 2407 | || mProximityPositive |
Jeff Brown | 1042874 | 2012-10-09 15:47:30 -0700 | [diff] [blame] | 2408 | || (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0 |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2409 | || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2410 | | USER_ACTIVITY_SCREEN_DIM)) != 0 |
| 2411 | || mScreenBrightnessBoostInProgress; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | /** |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2415 | * Returns true if the device is prevented from going into inattentive sleep by the stay on |
| 2416 | * while powered setting. We also keep the device awake when the proximity sensor returns a |
| 2417 | * positive result so that the device does not lock while in a phone call. This function only |
| 2418 | * controls whether the device will go to sleep which is independent of whether it will be |
| 2419 | * allowed to suspend. |
| 2420 | */ |
| 2421 | private boolean isBeingKeptFromInattentiveSleepLocked() { |
| 2422 | return mStayOn || mScreenBrightnessBoostInProgress || mProximityPositive |
| 2423 | || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT |
| 2424 | | USER_ACTIVITY_SCREEN_DIM)) != 0; |
| 2425 | } |
| 2426 | |
| 2427 | private boolean isBeingKeptFromShowingInattentiveSleepWarningLocked() { |
| 2428 | return mStayOn || mScreenBrightnessBoostInProgress || mProximityPositive || !mBootCompleted; |
| 2429 | } |
| 2430 | |
| 2431 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2432 | * Determines whether to post a message to the sandman to update the dream state. |
| 2433 | */ |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2434 | private void updateDreamLocked(int dirty, boolean displayBecameReady) { |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 2435 | if ((dirty & (DIRTY_WAKEFULNESS |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2436 | | DIRTY_USER_ACTIVITY |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2437 | | DIRTY_ATTENTIVE |
Jeff Brown | 645832d | 2012-10-03 14:57:03 -0700 | [diff] [blame] | 2438 | | DIRTY_WAKE_LOCKS |
| 2439 | | DIRTY_BOOT_COMPLETED |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 2440 | | DIRTY_SETTINGS |
| 2441 | | DIRTY_IS_POWERED |
| 2442 | | DIRTY_STAY_ON |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 2443 | | DIRTY_PROXIMITY_POSITIVE |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2444 | | DIRTY_BATTERY_STATE)) != 0 || displayBecameReady) { |
| 2445 | if (mDisplayReady) { |
| 2446 | scheduleSandmanLocked(); |
| 2447 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | private void scheduleSandmanLocked() { |
| 2452 | if (!mSandmanScheduled) { |
| 2453 | mSandmanScheduled = true; |
| 2454 | Message msg = mHandler.obtainMessage(MSG_SANDMAN); |
| 2455 | msg.setAsynchronous(true); |
| 2456 | mHandler.sendMessage(msg); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | /** |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2461 | * Called when the device enters or exits a dreaming or dozing state. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2462 | * |
| 2463 | * We do this asynchronously because we must call out of the power manager to start |
| 2464 | * the dream and we don't want to hold our lock while doing so. There is a risk that |
| 2465 | * the device will wake or go to sleep in the meantime so we have to handle that case. |
| 2466 | */ |
| 2467 | private void handleSandman() { // runs on handler thread |
| 2468 | // Handle preconditions. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2469 | final boolean startDreaming; |
| 2470 | final int wakefulness; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2471 | synchronized (mLock) { |
| 2472 | mSandmanScheduled = false; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2473 | wakefulness = mWakefulness; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2474 | if (mSandmanSummoned && mDisplayReady) { |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2475 | startDreaming = canDreamLocked() || canDozeLocked(); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2476 | mSandmanSummoned = false; |
| 2477 | } else { |
| 2478 | startDreaming = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2482 | // Start dreaming if needed. |
| 2483 | // We only control the dream on the handler thread, so we don't need to worry about |
| 2484 | // concurrent attempts to start or stop the dream. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2485 | final boolean isDreaming; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2486 | if (mDreamManager != null) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2487 | // Restart the dream whenever the sandman is summoned. |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 2488 | if (startDreaming) { |
Jeff Brown | f6d4668 | 2014-07-17 22:44:20 -0700 | [diff] [blame] | 2489 | mDreamManager.stopDream(false /*immediate*/); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2490 | mDreamManager.startDream(wakefulness == WAKEFULNESS_DOZING); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2491 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 2492 | isDreaming = mDreamManager.isDreaming(); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2493 | } else { |
| 2494 | isDreaming = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
Santos Cordon | 0304438 | 2019-09-19 16:59:01 +0100 | [diff] [blame] | 2497 | // At this point, we either attempted to start the dream or no attempt will be made, |
| 2498 | // so stop holding the display suspend blocker for Doze. |
| 2499 | mDozeStartInProgress = false; |
| 2500 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2501 | // Update dream state. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2502 | synchronized (mLock) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2503 | // Remember the initial battery level when the dream started. |
| 2504 | if (startDreaming && isDreaming) { |
| 2505 | mBatteryLevelWhenDreamStarted = mBatteryLevel; |
| 2506 | if (wakefulness == WAKEFULNESS_DOZING) { |
| 2507 | Slog.i(TAG, "Dozing..."); |
| 2508 | } else { |
| 2509 | Slog.i(TAG, "Dreaming..."); |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | // If preconditions changed, wait for the next iteration to determine |
| 2514 | // whether the dream should continue (or be restarted). |
| 2515 | if (mSandmanSummoned || mWakefulness != wakefulness) { |
| 2516 | return; // wait for next cycle |
| 2517 | } |
| 2518 | |
| 2519 | // Determine whether the dream should continue. |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2520 | long now = SystemClock.uptimeMillis(); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2521 | if (wakefulness == WAKEFULNESS_DREAMING) { |
| 2522 | if (isDreaming && canDreamLocked()) { |
| 2523 | if (mDreamsBatteryLevelDrainCutoffConfig >= 0 |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 2524 | && mBatteryLevel < mBatteryLevelWhenDreamStarted |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2525 | - mDreamsBatteryLevelDrainCutoffConfig |
| 2526 | && !isBeingKeptAwakeLocked()) { |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 2527 | // If the user activity timeout expired and the battery appears |
| 2528 | // to be draining faster than it is charging then stop dreaming |
| 2529 | // and go to sleep. |
| 2530 | Slog.i(TAG, "Stopping dream because the battery appears to " |
| 2531 | + "be draining faster than it is charging. " |
| 2532 | + "Battery level when dream started: " |
| 2533 | + mBatteryLevelWhenDreamStarted + "%. " |
| 2534 | + "Battery level now: " + mBatteryLevel + "%."); |
| 2535 | } else { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2536 | return; // continue dreaming |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 2537 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2538 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2539 | |
| 2540 | // Dream has ended or will be stopped. Update the power state. |
| 2541 | if (isItBedTimeYetLocked()) { |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2542 | int flags = 0; |
| 2543 | if (isAttentiveTimeoutExpired(now)) { |
| 2544 | flags |= PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE; |
| 2545 | } |
| 2546 | goToSleepNoUpdateLocked(now, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, flags, |
| 2547 | Process.SYSTEM_UID); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2548 | updatePowerStateLocked(); |
| 2549 | } else { |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2550 | wakeUpNoUpdateLocked(now, |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 2551 | PowerManager.WAKE_REASON_UNKNOWN, |
| 2552 | "android.server.power:DREAM_FINISHED", Process.SYSTEM_UID, |
| 2553 | mContext.getOpPackageName(), Process.SYSTEM_UID); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2554 | updatePowerStateLocked(); |
| 2555 | } |
| 2556 | } else if (wakefulness == WAKEFULNESS_DOZING) { |
| 2557 | if (isDreaming) { |
| 2558 | return; // continue dozing |
| 2559 | } |
| 2560 | |
| 2561 | // Doze has ended or will be stopped. Update the power state. |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 2562 | reallyGoToSleepNoUpdateLocked(now, Process.SYSTEM_UID); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2563 | updatePowerStateLocked(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2564 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2565 | } |
| 2566 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2567 | // Stop dream. |
| 2568 | if (isDreaming) { |
Jeff Brown | f6d4668 | 2014-07-17 22:44:20 -0700 | [diff] [blame] | 2569 | mDreamManager.stopDream(false /*immediate*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | /** |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2574 | * Returns true if the device is allowed to dream in its current state. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2575 | */ |
| 2576 | private boolean canDreamLocked() { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2577 | if (mWakefulness != WAKEFULNESS_DREAMING |
| 2578 | || !mDreamsSupportedConfig |
| 2579 | || !mDreamsEnabledSetting |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2580 | || !mDisplayPowerRequest.isBrightOrDim() |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 2581 | || mDisplayPowerRequest.isVr() |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2582 | || (mUserActivitySummary & (USER_ACTIVITY_SCREEN_BRIGHT |
| 2583 | | USER_ACTIVITY_SCREEN_DIM | USER_ACTIVITY_SCREEN_DREAM)) == 0 |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2584 | || !mBootCompleted) { |
| 2585 | return false; |
| 2586 | } |
| 2587 | if (!isBeingKeptAwakeLocked()) { |
Jeff Brown | 966604f | 2014-02-24 16:19:51 -0800 | [diff] [blame] | 2588 | if (!mIsPowered && !mDreamsEnabledOnBatteryConfig) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2589 | return false; |
| 2590 | } |
| 2591 | if (!mIsPowered |
| 2592 | && mDreamsBatteryLevelMinimumWhenNotPoweredConfig >= 0 |
| 2593 | && mBatteryLevel < mDreamsBatteryLevelMinimumWhenNotPoweredConfig) { |
| 2594 | return false; |
| 2595 | } |
| 2596 | if (mIsPowered |
| 2597 | && mDreamsBatteryLevelMinimumWhenPoweredConfig >= 0 |
| 2598 | && mBatteryLevel < mDreamsBatteryLevelMinimumWhenPoweredConfig) { |
| 2599 | return false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2600 | } |
| 2601 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2602 | return true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2605 | /** |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 2606 | * Returns true if the device is allowed to doze in its current state. |
| 2607 | */ |
| 2608 | private boolean canDozeLocked() { |
| 2609 | return mWakefulness == WAKEFULNESS_DOZING; |
| 2610 | } |
| 2611 | |
| 2612 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2613 | * Updates the display power state asynchronously. |
| 2614 | * When the update is finished, mDisplayReady will be set to true. The display |
| 2615 | * controller posts a message to tell us when the actual display power state |
| 2616 | * has been updated so we come back here to double-check and finish up. |
| 2617 | * |
| 2618 | * This function recalculates the display power state each time. |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2619 | * |
| 2620 | * @return True if the display became ready. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2621 | */ |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2622 | private boolean updateDisplayPowerStateLocked(int dirty) { |
| 2623 | final boolean oldDisplayReady = mDisplayReady; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2624 | if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS |
| 2625 | | DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED | DIRTY_BOOT_COMPLETED |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 2626 | | DIRTY_SETTINGS | DIRTY_SCREEN_BRIGHTNESS_BOOST | DIRTY_VR_MODE_CHANGED | |
| 2627 | DIRTY_QUIESCENT)) != 0) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2628 | mDisplayPowerRequest.policy = getDesiredScreenPolicyLocked(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2629 | |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 2630 | // Determine appropriate screen brightness and auto-brightness adjustments. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 2631 | final boolean autoBrightness; |
| 2632 | final int screenBrightnessOverride; |
Michael Wright | 9f818ea | 2016-07-29 19:59:39 +0100 | [diff] [blame] | 2633 | if (!mBootCompleted) { |
| 2634 | // Keep the brightness steady during boot. This requires the |
| 2635 | // bootloader brightness and the default brightness to be identical. |
| 2636 | autoBrightness = false; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 2637 | screenBrightnessOverride = mScreenBrightnessSettingDefault; |
Michael Wright | 9f818ea | 2016-07-29 19:59:39 +0100 | [diff] [blame] | 2638 | } else if (isValidBrightness(mScreenBrightnessOverrideFromWindowManager)) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2639 | autoBrightness = false; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 2640 | screenBrightnessOverride = mScreenBrightnessOverrideFromWindowManager; |
| 2641 | } else { |
| 2642 | autoBrightness = (mScreenBrightnessModeSetting == |
| 2643 | Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); |
| 2644 | screenBrightnessOverride = -1; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2645 | } |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 2646 | |
| 2647 | // Update display power request. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 2648 | mDisplayPowerRequest.screenBrightnessOverride = screenBrightnessOverride; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2649 | mDisplayPowerRequest.useAutoBrightness = autoBrightness; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2650 | mDisplayPowerRequest.useProximitySensor = shouldUseProximitySensorLocked(); |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 2651 | mDisplayPowerRequest.boostScreenBrightness = shouldBoostScreenBrightness(); |
Ruchi Kandoi | f974cc8 | 2014-05-01 11:25:10 -0700 | [diff] [blame] | 2652 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 2653 | updatePowerRequestFromBatterySaverPolicy(mDisplayPowerRequest); |
| 2654 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2655 | if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE) { |
| 2656 | mDisplayPowerRequest.dozeScreenState = mDozeScreenStateOverrideFromDreamManager; |
Ivan Podogov | 56bc6d0 | 2018-02-26 16:04:24 +0000 | [diff] [blame] | 2657 | if ((mWakeLockSummary & WAKE_LOCK_DRAW) != 0 |
| 2658 | && !mDrawWakeLockOverrideFromSidekick) { |
Chris Phoenix | 10a4a64 | 2017-09-25 13:21:00 -0700 | [diff] [blame] | 2659 | if (mDisplayPowerRequest.dozeScreenState == Display.STATE_DOZE_SUSPEND) { |
| 2660 | mDisplayPowerRequest.dozeScreenState = Display.STATE_DOZE; |
| 2661 | } |
| 2662 | if (mDisplayPowerRequest.dozeScreenState == Display.STATE_ON_SUSPEND) { |
| 2663 | mDisplayPowerRequest.dozeScreenState = Display.STATE_ON; |
| 2664 | } |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 2665 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2666 | mDisplayPowerRequest.dozeScreenBrightness = |
| 2667 | mDozeScreenBrightnessOverrideFromDreamManager; |
| 2668 | } else { |
| 2669 | mDisplayPowerRequest.dozeScreenState = Display.STATE_UNKNOWN; |
| 2670 | mDisplayPowerRequest.dozeScreenBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
| 2671 | } |
| 2672 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2673 | mDisplayReady = mDisplayManagerInternal.requestPowerState(mDisplayPowerRequest, |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2674 | mRequestWaitForNegativeProximity); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2675 | mRequestWaitForNegativeProximity = false; |
| 2676 | |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 2677 | if ((dirty & DIRTY_QUIESCENT) != 0) { |
| 2678 | sQuiescent = false; |
| 2679 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2680 | if (DEBUG_SPEW) { |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 2681 | Slog.d(TAG, "updateDisplayPowerStateLocked: mDisplayReady=" + mDisplayReady |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2682 | + ", policy=" + mDisplayPowerRequest.policy |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2683 | + ", mWakefulness=" + mWakefulness |
| 2684 | + ", mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary) |
| 2685 | + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary) |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 2686 | + ", mBootCompleted=" + mBootCompleted |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 2687 | + ", screenBrightnessOverride=" + screenBrightnessOverride |
| 2688 | + ", useAutoBrightness=" + autoBrightness |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 2689 | + ", mScreenBrightnessBoostInProgress=" + mScreenBrightnessBoostInProgress |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 2690 | + ", mIsVrModeEnabled= " + mIsVrModeEnabled |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 2691 | + ", sQuiescent=" + sQuiescent); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2692 | } |
| 2693 | } |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2694 | return mDisplayReady && !oldDisplayReady; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2695 | } |
| 2696 | |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2697 | private void updateScreenBrightnessBoostLocked(int dirty) { |
| 2698 | if ((dirty & DIRTY_SCREEN_BRIGHTNESS_BOOST) != 0) { |
| 2699 | if (mScreenBrightnessBoostInProgress) { |
| 2700 | final long now = SystemClock.uptimeMillis(); |
| 2701 | mHandler.removeMessages(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT); |
| 2702 | if (mLastScreenBrightnessBoostTime > mLastSleepTime) { |
| 2703 | final long boostTimeout = mLastScreenBrightnessBoostTime + |
| 2704 | SCREEN_BRIGHTNESS_BOOST_TIMEOUT; |
| 2705 | if (boostTimeout > now) { |
| 2706 | Message msg = mHandler.obtainMessage(MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT); |
| 2707 | msg.setAsynchronous(true); |
| 2708 | mHandler.sendMessageAtTime(msg, boostTimeout); |
| 2709 | return; |
| 2710 | } |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 2711 | } |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2712 | mScreenBrightnessBoostInProgress = false; |
| 2713 | userActivityNoUpdateLocked(now, |
| 2714 | PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 2715 | } |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 2716 | } |
| 2717 | } |
| 2718 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 2719 | private boolean shouldBoostScreenBrightness() { |
| 2720 | return !mIsVrModeEnabled && mScreenBrightnessBoostInProgress; |
| 2721 | } |
| 2722 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2723 | private static boolean isValidBrightness(int value) { |
| 2724 | return value >= 0 && value <= 255; |
| 2725 | } |
| 2726 | |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 2727 | @VisibleForTesting |
| 2728 | int getDesiredScreenPolicyLocked() { |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 2729 | if (mWakefulness == WAKEFULNESS_ASLEEP || sQuiescent) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2730 | return DisplayPowerRequest.POLICY_OFF; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2733 | if (mWakefulness == WAKEFULNESS_DOZING) { |
| 2734 | if ((mWakeLockSummary & WAKE_LOCK_DOZE) != 0) { |
| 2735 | return DisplayPowerRequest.POLICY_DOZE; |
| 2736 | } |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 2737 | if (mDozeAfterScreenOff) { |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 2738 | return DisplayPowerRequest.POLICY_OFF; |
| 2739 | } |
| 2740 | // Fall through and preserve the current screen policy if not configured to |
| 2741 | // doze after screen off. This causes the screen off transition to be skipped. |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2742 | } |
| 2743 | |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 2744 | // It is important that POLICY_VR check happens after the wakefulness checks above so |
| 2745 | // that VR-mode does not prevent displays from transitioning to the correct state when |
| 2746 | // dozing or sleeping. |
| 2747 | if (mIsVrModeEnabled) { |
| 2748 | return DisplayPowerRequest.POLICY_VR; |
| 2749 | } |
| 2750 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2751 | if ((mWakeLockSummary & WAKE_LOCK_SCREEN_BRIGHT) != 0 |
| 2752 | || (mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0 |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2753 | || !mBootCompleted |
| 2754 | || mScreenBrightnessBoostInProgress) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2755 | return DisplayPowerRequest.POLICY_BRIGHT; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2756 | } |
| 2757 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2758 | return DisplayPowerRequest.POLICY_DIM; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2761 | private final DisplayManagerInternal.DisplayPowerCallbacks mDisplayPowerCallbacks = |
| 2762 | new DisplayManagerInternal.DisplayPowerCallbacks() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2763 | private int mDisplayState = Display.STATE_UNKNOWN; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2764 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2765 | @Override |
| 2766 | public void onStateChanged() { |
Jeff Brown | d91e417 | 2013-07-16 15:18:19 -0700 | [diff] [blame] | 2767 | synchronized (mLock) { |
| 2768 | mDirty |= DIRTY_ACTUAL_DISPLAY_POWER_STATE_UPDATED; |
| 2769 | updatePowerStateLocked(); |
| 2770 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | @Override |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 2774 | public void onProximityPositive() { |
Jeff Brown | d91e417 | 2013-07-16 15:18:19 -0700 | [diff] [blame] | 2775 | synchronized (mLock) { |
| 2776 | mProximityPositive = true; |
| 2777 | mDirty |= DIRTY_PROXIMITY_POSITIVE; |
| 2778 | updatePowerStateLocked(); |
| 2779 | } |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2783 | public void onProximityNegative() { |
Jeff Brown | d91e417 | 2013-07-16 15:18:19 -0700 | [diff] [blame] | 2784 | synchronized (mLock) { |
| 2785 | mProximityPositive = false; |
| 2786 | mDirty |= DIRTY_PROXIMITY_POSITIVE; |
| 2787 | userActivityNoUpdateLocked(SystemClock.uptimeMillis(), |
| 2788 | PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); |
| 2789 | updatePowerStateLocked(); |
| 2790 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2791 | } |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2792 | |
| 2793 | @Override |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2794 | public void onDisplayStateChange(int state) { |
| 2795 | // This method is only needed to support legacy display blanking behavior |
| 2796 | // where the display's power state is coupled to suspend or to the power HAL. |
| 2797 | // The order of operations matters here. |
| 2798 | synchronized (mLock) { |
| 2799 | if (mDisplayState != state) { |
| 2800 | mDisplayState = state; |
| 2801 | if (state == Display.STATE_OFF) { |
| 2802 | if (!mDecoupleHalInteractiveModeFromDisplayConfig) { |
| 2803 | setHalInteractiveModeLocked(false); |
| 2804 | } |
| 2805 | if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 2806 | setHalAutoSuspendModeLocked(true); |
| 2807 | } |
| 2808 | } else { |
| 2809 | if (!mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 2810 | setHalAutoSuspendModeLocked(false); |
| 2811 | } |
| 2812 | if (!mDecoupleHalInteractiveModeFromDisplayConfig) { |
| 2813 | setHalInteractiveModeLocked(true); |
| 2814 | } |
| 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | @Override |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2821 | public void acquireSuspendBlocker() { |
| 2822 | mDisplaySuspendBlocker.acquire(); |
| 2823 | } |
| 2824 | |
| 2825 | @Override |
| 2826 | public void releaseSuspendBlocker() { |
| 2827 | mDisplaySuspendBlocker.release(); |
| 2828 | } |
| 2829 | |
| 2830 | @Override |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2831 | public String toString() { |
| 2832 | synchronized (this) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2833 | return "state=" + Display.stateToString(mDisplayState); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 2834 | } |
| 2835 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2836 | }; |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 2837 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2838 | private boolean shouldUseProximitySensorLocked() { |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 2839 | return !mIsVrModeEnabled && (mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2840 | } |
Jim Miller | 92e66dd | 2012-02-21 18:57:12 -0800 | [diff] [blame] | 2841 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2842 | /** |
| 2843 | * Updates the suspend blocker that keeps the CPU alive. |
| 2844 | * |
| 2845 | * This function must have no other side-effects. |
| 2846 | */ |
| 2847 | private void updateSuspendBlockerLocked() { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2848 | final boolean needWakeLockSuspendBlocker = ((mWakeLockSummary & WAKE_LOCK_CPU) != 0); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2849 | final boolean needDisplaySuspendBlocker = needDisplaySuspendBlockerLocked(); |
| 2850 | final boolean autoSuspend = !needDisplaySuspendBlocker; |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2851 | final boolean interactive = mDisplayPowerRequest.isBrightOrDim(); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2852 | |
| 2853 | // Disable auto-suspend if needed. |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2854 | // FIXME We should consider just leaving auto-suspend enabled forever since |
| 2855 | // we already hold the necessary wakelocks. |
| 2856 | if (!autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 2857 | setHalAutoSuspendModeLocked(false); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2858 | } |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 2859 | |
| 2860 | // First acquire suspend blockers if needed. |
| 2861 | if (needWakeLockSuspendBlocker && !mHoldingWakeLockSuspendBlocker) { |
| 2862 | mWakeLockSuspendBlocker.acquire(); |
| 2863 | mHoldingWakeLockSuspendBlocker = true; |
| 2864 | } |
| 2865 | if (needDisplaySuspendBlocker && !mHoldingDisplaySuspendBlocker) { |
| 2866 | mDisplaySuspendBlocker.acquire(); |
| 2867 | mHoldingDisplaySuspendBlocker = true; |
| 2868 | } |
| 2869 | |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2870 | // Inform the power HAL about interactive mode. |
| 2871 | // Although we could set interactive strictly based on the wakefulness |
| 2872 | // as reported by isInteractive(), it is actually more desirable to track |
| 2873 | // the display policy state instead so that the interactive state observed |
| 2874 | // by the HAL more accurately tracks transitions between AWAKE and DOZING. |
| 2875 | // Refer to getDesiredScreenPolicyLocked() for details. |
| 2876 | if (mDecoupleHalInteractiveModeFromDisplayConfig) { |
| 2877 | // When becoming non-interactive, we want to defer sending this signal |
| 2878 | // until the display is actually ready so that all transitions have |
| 2879 | // completed. This is probably a good sign that things have gotten |
| 2880 | // too tangled over here... |
| 2881 | if (interactive || mDisplayReady) { |
| 2882 | setHalInteractiveModeLocked(interactive); |
| 2883 | } |
| 2884 | } |
| 2885 | |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 2886 | // Then release suspend blockers if needed. |
| 2887 | if (!needWakeLockSuspendBlocker && mHoldingWakeLockSuspendBlocker) { |
| 2888 | mWakeLockSuspendBlocker.release(); |
| 2889 | mHoldingWakeLockSuspendBlocker = false; |
| 2890 | } |
| 2891 | if (!needDisplaySuspendBlocker && mHoldingDisplaySuspendBlocker) { |
| 2892 | mDisplaySuspendBlocker.release(); |
| 2893 | mHoldingDisplaySuspendBlocker = false; |
| 2894 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2895 | |
| 2896 | // Enable auto-suspend if needed. |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2897 | if (autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) { |
| 2898 | setHalAutoSuspendModeLocked(true); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2899 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2900 | } |
| 2901 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2902 | /** |
| 2903 | * Return true if we must keep a suspend blocker active on behalf of the display. |
| 2904 | * We do so if the screen is on or is in transition between states. |
| 2905 | */ |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2906 | private boolean needDisplaySuspendBlockerLocked() { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2907 | if (!mDisplayReady) { |
| 2908 | return true; |
| 2909 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 2910 | if (mDisplayPowerRequest.isBrightOrDim()) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2911 | // If we asked for the screen to be on but it is off due to the proximity |
| 2912 | // sensor then we may suspend but only if the configuration allows it. |
| 2913 | // On some hardware it may not be safe to suspend because the proximity |
| 2914 | // sensor may not be correctly configured as a wake-up source. |
| 2915 | if (!mDisplayPowerRequest.useProximitySensor || !mProximityPositive |
| 2916 | || !mSuspendWhenScreenOffDueToProximityConfig) { |
| 2917 | return true; |
| 2918 | } |
| 2919 | } |
Santos Cordon | d9701ab | 2019-08-01 18:33:20 +0100 | [diff] [blame] | 2920 | |
| 2921 | if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DOZE |
| 2922 | && mDisplayPowerRequest.dozeScreenState == Display.STATE_ON) { |
| 2923 | // Although we are in DOZE and would normally allow the device to suspend, |
| 2924 | // the doze service has explicitly requested the display to remain in the ON |
| 2925 | // state which means we should hold the display suspend blocker. |
| 2926 | return true; |
| 2927 | } |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 2928 | if (mScreenBrightnessBoostInProgress) { |
| 2929 | return true; |
| 2930 | } |
Santos Cordon | 0304438 | 2019-09-19 16:59:01 +0100 | [diff] [blame] | 2931 | |
| 2932 | // When we transition to DOZING, we have to keep the display suspend blocker |
| 2933 | // up until the Doze service has a change to acquire the DOZE wakelock. |
| 2934 | // Here we wait for mWakefulnessChanging to become false since the wakefulness |
| 2935 | // transition to DOZING isn't considered "changed" until the doze wake lock is |
| 2936 | // acquired. |
| 2937 | if (mWakefulness == WAKEFULNESS_DOZING && mDozeStartInProgress) { |
| 2938 | return true; |
| 2939 | } |
| 2940 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2941 | // Let the system suspend if the screen is off or dozing. |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 2942 | return false; |
| 2943 | } |
| 2944 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2945 | private void setHalAutoSuspendModeLocked(boolean enable) { |
| 2946 | if (enable != mHalAutoSuspendModeEnabled) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2947 | if (DEBUG) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2948 | Slog.d(TAG, "Setting HAL auto-suspend mode to " + enable); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2949 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2950 | mHalAutoSuspendModeEnabled = enable; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2951 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalAutoSuspend(" + enable + ")"); |
| 2952 | try { |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 2953 | mNativeWrapper.nativeSetAutoSuspend(enable); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2954 | } finally { |
| 2955 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 2956 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2957 | } |
| 2958 | } |
| 2959 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2960 | private void setHalInteractiveModeLocked(boolean enable) { |
| 2961 | if (enable != mHalInteractiveModeEnabled) { |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2962 | if (DEBUG) { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2963 | Slog.d(TAG, "Setting HAL interactive mode to " + enable); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2964 | } |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2965 | mHalInteractiveModeEnabled = enable; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2966 | Trace.traceBegin(Trace.TRACE_TAG_POWER, "setHalInteractive(" + enable + ")"); |
| 2967 | try { |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 2968 | mNativeWrapper.nativeSetInteractive(enable); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 2969 | } finally { |
| 2970 | Trace.traceEnd(Trace.TRACE_TAG_POWER); |
| 2971 | } |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 2972 | } |
| 2973 | } |
| 2974 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 2975 | private boolean isInteractiveInternal() { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 2976 | synchronized (mLock) { |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 2977 | return PowerManagerInternal.isInteractive(mWakefulness); |
Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 2981 | private boolean setLowPowerModeInternal(boolean enabled) { |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2982 | synchronized (mLock) { |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 2983 | if (DEBUG) { |
| 2984 | Slog.d(TAG, "setLowPowerModeInternal " + enabled + " mIsPowered=" + mIsPowered); |
| 2985 | } |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2986 | if (mIsPowered) { |
| 2987 | return false; |
| 2988 | } |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2989 | |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 2990 | mBatterySaverStateMachine.setBatterySaverEnabledManually(enabled); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2991 | |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 2992 | return true; |
| 2993 | } |
| 2994 | } |
| 2995 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2996 | boolean isDeviceIdleModeInternal() { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 2997 | synchronized (mLock) { |
| 2998 | return mDeviceIdleMode; |
| 2999 | } |
| 3000 | } |
| 3001 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3002 | boolean isLightDeviceIdleModeInternal() { |
| 3003 | synchronized (mLock) { |
| 3004 | return mLightDeviceIdleMode; |
| 3005 | } |
| 3006 | } |
| 3007 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3008 | private void handleBatteryStateChangedLocked() { |
| 3009 | mDirty |= DIRTY_BATTERY_STATE; |
| 3010 | updatePowerStateLocked(); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3011 | } |
| 3012 | |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 3013 | private void shutdownOrRebootInternal(final @HaltMode int haltMode, final boolean confirm, |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 3014 | final String reason, boolean wait) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3015 | if (mHandler == null || !mSystemReady) { |
Jeff Sharkey | fe6f85c | 2017-01-20 10:42:57 -0700 | [diff] [blame] | 3016 | if (RescueParty.isAttemptingFactoryReset()) { |
| 3017 | // If we're stuck in a really low-level reboot loop, and a |
| 3018 | // rescue party is trying to prompt the user for a factory data |
| 3019 | // reset, we must GET TO DA CHOPPA! |
| 3020 | PowerManagerService.lowLevelReboot(reason); |
| 3021 | } else { |
| 3022 | throw new IllegalStateException("Too early to call shutdown() or reboot()"); |
| 3023 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 3024 | } |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 3025 | |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 3026 | Runnable runnable = new Runnable() { |
Jeff Brown | ab887a0 | 2012-10-15 16:00:40 -0700 | [diff] [blame] | 3027 | @Override |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 3028 | public void run() { |
| 3029 | synchronized (this) { |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 3030 | if (haltMode == HALT_MODE_REBOOT_SAFE_MODE) { |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 3031 | ShutdownThread.rebootSafeMode(getUiContext(), confirm); |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 3032 | } else if (haltMode == HALT_MODE_REBOOT) { |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 3033 | ShutdownThread.reboot(getUiContext(), reason, confirm); |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 3034 | } else { |
Adam Lesinski | a82b626 | 2017-03-21 16:56:17 -0700 | [diff] [blame] | 3035 | ShutdownThread.shutdown(getUiContext(), reason, confirm); |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 3036 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 3037 | } |
San Mehat | 1e51279 | 2010-01-07 10:40:29 -0800 | [diff] [blame] | 3038 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 3039 | }; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3040 | |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 3041 | // ShutdownThread must run on a looper capable of displaying the UI. |
Michael Wright | 64c820d | 2017-03-21 12:36:55 +0000 | [diff] [blame] | 3042 | Message msg = Message.obtain(UiThread.getHandler(), runnable); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3043 | msg.setAsynchronous(true); |
Michael Wright | 64c820d | 2017-03-21 12:36:55 +0000 | [diff] [blame] | 3044 | UiThread.getHandler().sendMessage(msg); |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 3045 | |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 3046 | // PowerManager.reboot() is documented not to return so just wait for the inevitable. |
Dianne Hackborn | c428aae | 2012-10-03 16:38:22 -0700 | [diff] [blame] | 3047 | if (wait) { |
| 3048 | synchronized (runnable) { |
| 3049 | while (true) { |
| 3050 | try { |
| 3051 | runnable.wait(); |
| 3052 | } catch (InterruptedException e) { |
| 3053 | } |
Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 3054 | } |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 3055 | } |
Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 3056 | } |
| 3057 | } |
| 3058 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3059 | private void crashInternal(final String message) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 3060 | Thread t = new Thread("PowerManagerService.crash()") { |
Jeff Brown | ab887a0 | 2012-10-15 16:00:40 -0700 | [diff] [blame] | 3061 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3062 | public void run() { |
| 3063 | throw new RuntimeException(message); |
| 3064 | } |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 3065 | }; |
| 3066 | try { |
| 3067 | t.start(); |
| 3068 | t.join(); |
| 3069 | } catch (InterruptedException e) { |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 3070 | Slog.wtf(TAG, e); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 3074 | @VisibleForTesting |
| 3075 | void updatePowerRequestFromBatterySaverPolicy(DisplayPowerRequest displayPowerRequest) { |
| 3076 | PowerSaveState state = mBatterySaverPolicy. |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 3077 | getBatterySaverPolicy(ServiceType.SCREEN_BRIGHTNESS); |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 3078 | displayPowerRequest.lowPowerMode = state.batterySaverEnabled; |
| 3079 | displayPowerRequest.screenLowPowerBrightnessFactor = state.brightnessFactor; |
| 3080 | } |
| 3081 | |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3082 | void setStayOnSettingInternal(int val) { |
Christopher Tate | ad73532 | 2012-09-07 14:19:43 -0700 | [diff] [blame] | 3083 | Settings.Global.putInt(mContext.getContentResolver(), |
| 3084 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3085 | } |
| 3086 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3087 | void setMaximumScreenOffTimeoutFromDeviceAdminInternal(@UserIdInt int userId, long timeMs) { |
| 3088 | if (userId < 0) { |
| 3089 | Slog.wtf(TAG, "Attempt to set screen off timeout for invalid user: " + userId); |
| 3090 | return; |
| 3091 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3092 | synchronized (mLock) { |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3093 | // System-wide timeout |
| 3094 | if (userId == UserHandle.USER_SYSTEM) { |
| 3095 | mMaximumScreenOffTimeoutFromDeviceAdmin = timeMs; |
| 3096 | } else if (timeMs == Long.MAX_VALUE || timeMs == 0) { |
| 3097 | mProfilePowerState.delete(userId); |
| 3098 | } else { |
| 3099 | final ProfilePowerState profile = mProfilePowerState.get(userId); |
| 3100 | if (profile != null) { |
| 3101 | profile.mScreenOffTimeout = timeMs; |
| 3102 | } else { |
| 3103 | mProfilePowerState.put(userId, new ProfilePowerState(userId, timeMs)); |
| 3104 | // We need to recalculate wake locks for the new profile state. |
| 3105 | mDirty |= DIRTY_WAKE_LOCKS; |
| 3106 | } |
| 3107 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3108 | mDirty |= DIRTY_SETTINGS; |
| 3109 | updatePowerStateLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3113 | boolean setDeviceIdleModeInternal(boolean enabled) { |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3114 | synchronized (mLock) { |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 3115 | if (mDeviceIdleMode == enabled) { |
| 3116 | return false; |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3117 | } |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 3118 | mDeviceIdleMode = enabled; |
| 3119 | updateWakeLockDisabledStatesLocked(); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3120 | } |
Felipe Leme | ea01439 | 2016-09-06 13:59:54 -0700 | [diff] [blame] | 3121 | if (enabled) { |
| 3122 | EventLogTags.writeDeviceIdleOnPhase("power"); |
| 3123 | } else { |
| 3124 | EventLogTags.writeDeviceIdleOffPhase("power"); |
| 3125 | } |
| 3126 | return true; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3127 | } |
| 3128 | |
| 3129 | boolean setLightDeviceIdleModeInternal(boolean enabled) { |
| 3130 | synchronized (mLock) { |
| 3131 | if (mLightDeviceIdleMode != enabled) { |
| 3132 | mLightDeviceIdleMode = enabled; |
| 3133 | return true; |
| 3134 | } |
| 3135 | return false; |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | void setDeviceIdleWhitelistInternal(int[] appids) { |
| 3140 | synchronized (mLock) { |
| 3141 | mDeviceIdleWhitelist = appids; |
| 3142 | if (mDeviceIdleMode) { |
| 3143 | updateWakeLockDisabledStatesLocked(); |
| 3144 | } |
| 3145 | } |
| 3146 | } |
| 3147 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3148 | void setDeviceIdleTempWhitelistInternal(int[] appids) { |
| 3149 | synchronized (mLock) { |
| 3150 | mDeviceIdleTempWhitelist = appids; |
| 3151 | if (mDeviceIdleMode) { |
| 3152 | updateWakeLockDisabledStatesLocked(); |
| 3153 | } |
| 3154 | } |
| 3155 | } |
| 3156 | |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3157 | void startUidChangesInternal() { |
| 3158 | synchronized (mLock) { |
| 3159 | mUidsChanging = true; |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | void finishUidChangesInternal() { |
| 3164 | synchronized (mLock) { |
| 3165 | mUidsChanging = false; |
| 3166 | if (mUidsChanged) { |
| 3167 | updateWakeLockDisabledStatesLocked(); |
| 3168 | mUidsChanged = false; |
| 3169 | } |
| 3170 | } |
| 3171 | } |
| 3172 | |
| 3173 | private void handleUidStateChangeLocked() { |
| 3174 | if (mUidsChanging) { |
| 3175 | mUidsChanged = true; |
| 3176 | } else { |
| 3177 | updateWakeLockDisabledStatesLocked(); |
| 3178 | } |
| 3179 | } |
| 3180 | |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3181 | void updateUidProcStateInternal(int uid, int procState) { |
| 3182 | synchronized (mLock) { |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3183 | UidState state = mUidState.get(uid); |
| 3184 | if (state == null) { |
| 3185 | state = new UidState(uid); |
| 3186 | mUidState.put(uid, state); |
| 3187 | } |
Dianne Hackborn | 951ea69 | 2016-11-02 10:41:53 -0700 | [diff] [blame] | 3188 | final boolean oldShouldAllow = state.mProcState |
| 3189 | <= ActivityManager.PROCESS_STATE_RECEIVER; |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3190 | state.mProcState = procState; |
Dianne Hackborn | 951ea69 | 2016-11-02 10:41:53 -0700 | [diff] [blame] | 3191 | if (state.mNumWakeLocks > 0) { |
| 3192 | if (mDeviceIdleMode) { |
| 3193 | handleUidStateChangeLocked(); |
| 3194 | } else if (!state.mActive && oldShouldAllow != |
| 3195 | (procState <= ActivityManager.PROCESS_STATE_RECEIVER)) { |
| 3196 | // If this uid is not active, but the process state has changed such |
| 3197 | // that we may still want to allow it to hold a wake lock, then take care of it. |
| 3198 | handleUidStateChangeLocked(); |
| 3199 | } |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3200 | } |
| 3201 | } |
| 3202 | } |
| 3203 | |
| 3204 | void uidGoneInternal(int uid) { |
| 3205 | synchronized (mLock) { |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3206 | final int index = mUidState.indexOfKey(uid); |
| 3207 | if (index >= 0) { |
| 3208 | UidState state = mUidState.valueAt(index); |
| 3209 | state.mProcState = ActivityManager.PROCESS_STATE_NONEXISTENT; |
| 3210 | state.mActive = false; |
| 3211 | mUidState.removeAt(index); |
| 3212 | if (mDeviceIdleMode && state.mNumWakeLocks > 0) { |
| 3213 | handleUidStateChangeLocked(); |
| 3214 | } |
| 3215 | } |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | void uidActiveInternal(int uid) { |
| 3220 | synchronized (mLock) { |
| 3221 | UidState state = mUidState.get(uid); |
| 3222 | if (state == null) { |
| 3223 | state = new UidState(uid); |
| 3224 | state.mProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY; |
| 3225 | mUidState.put(uid, state); |
| 3226 | } |
| 3227 | state.mActive = true; |
| 3228 | if (state.mNumWakeLocks > 0) { |
| 3229 | handleUidStateChangeLocked(); |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | void uidIdleInternal(int uid) { |
| 3235 | synchronized (mLock) { |
| 3236 | UidState state = mUidState.get(uid); |
| 3237 | if (state != null) { |
| 3238 | state.mActive = false; |
| 3239 | if (state.mNumWakeLocks > 0) { |
| 3240 | handleUidStateChangeLocked(); |
| 3241 | } |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3246 | private void updateWakeLockDisabledStatesLocked() { |
| 3247 | boolean changed = false; |
| 3248 | final int numWakeLocks = mWakeLocks.size(); |
| 3249 | for (int i = 0; i < numWakeLocks; i++) { |
| 3250 | final WakeLock wakeLock = mWakeLocks.get(i); |
| 3251 | if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) |
Ruchi Kandoi | 9c36c02 | 2016-04-20 13:42:01 -0700 | [diff] [blame] | 3252 | == PowerManager.PARTIAL_WAKE_LOCK) { |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3253 | if (setWakeLockDisabledStateLocked(wakeLock)) { |
| 3254 | changed = true; |
| 3255 | if (wakeLock.mDisabled) { |
| 3256 | // This wake lock is no longer being respected. |
| 3257 | notifyWakeLockReleasedLocked(wakeLock); |
| 3258 | } else { |
| 3259 | notifyWakeLockAcquiredLocked(wakeLock); |
| 3260 | } |
| 3261 | } |
| 3262 | } |
| 3263 | } |
| 3264 | if (changed) { |
| 3265 | mDirty |= DIRTY_WAKE_LOCKS; |
| 3266 | updatePowerStateLocked(); |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | private boolean setWakeLockDisabledStateLocked(WakeLock wakeLock) { |
| 3271 | if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) |
| 3272 | == PowerManager.PARTIAL_WAKE_LOCK) { |
Ruchi Kandoi | 9c36c02 | 2016-04-20 13:42:01 -0700 | [diff] [blame] | 3273 | boolean disabled = false; |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3274 | final int appid = UserHandle.getAppId(wakeLock.mOwnerUid); |
| 3275 | if (appid >= Process.FIRST_APPLICATION_UID) { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 3276 | // Cached inactive processes are never allowed to hold wake locks. |
| 3277 | if (mConstants.NO_CACHED_WAKE_LOCKS) { |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 3278 | disabled = mForceSuspendActive |
| 3279 | || (!wakeLock.mUidState.mActive && wakeLock.mUidState.mProcState |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 3280 | != ActivityManager.PROCESS_STATE_NONEXISTENT && |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 3281 | wakeLock.mUidState.mProcState > ActivityManager.PROCESS_STATE_RECEIVER); |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 3282 | } |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3283 | if (mDeviceIdleMode) { |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 3284 | // If we are in idle mode, we will also ignore all partial wake locks that are |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3285 | // for application uids that are not whitelisted. |
| 3286 | final UidState state = wakeLock.mUidState; |
| 3287 | if (Arrays.binarySearch(mDeviceIdleWhitelist, appid) < 0 && |
| 3288 | Arrays.binarySearch(mDeviceIdleTempWhitelist, appid) < 0 && |
| 3289 | state.mProcState != ActivityManager.PROCESS_STATE_NONEXISTENT && |
Dianne Hackborn | 10fc4fd | 2017-12-19 17:23:13 -0800 | [diff] [blame] | 3290 | state.mProcState > |
| 3291 | ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE) { |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3292 | disabled = true; |
| 3293 | } |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3294 | } |
| 3295 | } |
Ruchi Kandoi | 9c36c02 | 2016-04-20 13:42:01 -0700 | [diff] [blame] | 3296 | if (wakeLock.mDisabled != disabled) { |
| 3297 | wakeLock.mDisabled = disabled; |
| 3298 | return true; |
| 3299 | } |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3300 | } |
| 3301 | return false; |
| 3302 | } |
| 3303 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3304 | private boolean isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() { |
| 3305 | return mMaximumScreenOffTimeoutFromDeviceAdmin >= 0 |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3306 | && mMaximumScreenOffTimeoutFromDeviceAdmin < Long.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3307 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3308 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3309 | private void setAttentionLightInternal(boolean on, int color) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3310 | Light light; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3311 | synchronized (mLock) { |
| 3312 | if (!mSystemReady) { |
| 3313 | return; |
| 3314 | } |
| 3315 | light = mAttentionLight; |
Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 3316 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3317 | |
| 3318 | // Control light outside of lock. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3319 | light.setFlashing(color, Light.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 3322 | private void setDozeAfterScreenOffInternal(boolean on) { |
| 3323 | synchronized (mLock) { |
| 3324 | mDozeAfterScreenOff = on; |
| 3325 | } |
| 3326 | } |
| 3327 | |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 3328 | private void boostScreenBrightnessInternal(long eventTime, int uid) { |
| 3329 | synchronized (mLock) { |
| 3330 | if (!mSystemReady || mWakefulness == WAKEFULNESS_ASLEEP |
| 3331 | || eventTime < mLastScreenBrightnessBoostTime) { |
| 3332 | return; |
| 3333 | } |
| 3334 | |
| 3335 | Slog.i(TAG, "Brightness boost activated (uid " + uid +")..."); |
| 3336 | mLastScreenBrightnessBoostTime = eventTime; |
Kweku Adams | f94be8a | 2019-06-14 13:39:14 -0700 | [diff] [blame] | 3337 | mScreenBrightnessBoostInProgress = true; |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 3338 | mDirty |= DIRTY_SCREEN_BRIGHTNESS_BOOST; |
| 3339 | |
| 3340 | userActivityNoUpdateLocked(eventTime, |
| 3341 | PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, uid); |
| 3342 | updatePowerStateLocked(); |
| 3343 | } |
| 3344 | } |
| 3345 | |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 3346 | private boolean isScreenBrightnessBoostedInternal() { |
| 3347 | synchronized (mLock) { |
| 3348 | return mScreenBrightnessBoostInProgress; |
| 3349 | } |
| 3350 | } |
| 3351 | |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 3352 | /** |
| 3353 | * Called when a screen brightness boost timeout has occurred. |
| 3354 | * |
| 3355 | * This function must have no other side-effects besides setting the dirty |
| 3356 | * bit and calling update power state. |
| 3357 | */ |
| 3358 | private void handleScreenBrightnessBoostTimeout() { // runs on handler thread |
| 3359 | synchronized (mLock) { |
| 3360 | if (DEBUG_SPEW) { |
| 3361 | Slog.d(TAG, "handleScreenBrightnessBoostTimeout"); |
| 3362 | } |
| 3363 | |
| 3364 | mDirty |= DIRTY_SCREEN_BRIGHTNESS_BOOST; |
| 3365 | updatePowerStateLocked(); |
| 3366 | } |
| 3367 | } |
| 3368 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3369 | private void setScreenBrightnessOverrideFromWindowManagerInternal(int brightness) { |
| 3370 | synchronized (mLock) { |
| 3371 | if (mScreenBrightnessOverrideFromWindowManager != brightness) { |
| 3372 | mScreenBrightnessOverrideFromWindowManager = brightness; |
| 3373 | mDirty |= DIRTY_SETTINGS; |
| 3374 | updatePowerStateLocked(); |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3375 | } |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3376 | } |
Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 3377 | } |
| 3378 | |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 3379 | private void setUserInactiveOverrideFromWindowManagerInternal() { |
| 3380 | synchronized (mLock) { |
| 3381 | mUserInactiveOverrideFromWindowManager = true; |
| 3382 | mDirty |= DIRTY_USER_ACTIVITY; |
| 3383 | updatePowerStateLocked(); |
| 3384 | } |
| 3385 | } |
| 3386 | |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 3387 | private void setUserActivityTimeoutOverrideFromWindowManagerInternal(long timeoutMillis) { |
| 3388 | synchronized (mLock) { |
| 3389 | if (mUserActivityTimeoutOverrideFromWindowManager != timeoutMillis) { |
| 3390 | mUserActivityTimeoutOverrideFromWindowManager = timeoutMillis; |
Michael Wright | b55e3a1 | 2018-03-06 15:14:06 +0000 | [diff] [blame] | 3391 | EventLogTags.writeUserActivityTimeoutOverride(timeoutMillis); |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 3392 | mDirty |= DIRTY_SETTINGS; |
| 3393 | updatePowerStateLocked(); |
| 3394 | } |
| 3395 | } |
| 3396 | } |
| 3397 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3398 | private void setDozeOverrideFromDreamManagerInternal( |
| 3399 | int screenState, int screenBrightness) { |
| 3400 | synchronized (mLock) { |
| 3401 | if (mDozeScreenStateOverrideFromDreamManager != screenState |
| 3402 | || mDozeScreenBrightnessOverrideFromDreamManager != screenBrightness) { |
| 3403 | mDozeScreenStateOverrideFromDreamManager = screenState; |
| 3404 | mDozeScreenBrightnessOverrideFromDreamManager = screenBrightness; |
| 3405 | mDirty |= DIRTY_SETTINGS; |
| 3406 | updatePowerStateLocked(); |
| 3407 | } |
| 3408 | } |
| 3409 | } |
| 3410 | |
Ivan Podogov | 56bc6d0 | 2018-02-26 16:04:24 +0000 | [diff] [blame] | 3411 | private void setDrawWakeLockOverrideFromSidekickInternal(boolean keepState) { |
| 3412 | synchronized (mLock) { |
| 3413 | if (mDrawWakeLockOverrideFromSidekick != keepState) { |
| 3414 | mDrawWakeLockOverrideFromSidekick = keepState; |
| 3415 | mDirty |= DIRTY_SETTINGS; |
| 3416 | updatePowerStateLocked(); |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 3421 | @VisibleForTesting |
| 3422 | void setVrModeEnabled(boolean enabled) { |
| 3423 | mIsVrModeEnabled = enabled; |
| 3424 | } |
| 3425 | |
Makoto Onuki | a7d8c4d | 2017-11-20 15:20:16 -0800 | [diff] [blame] | 3426 | private void powerHintInternal(int hintId, int data) { |
| 3427 | // Maybe filter the event. |
| 3428 | switch (hintId) { |
| 3429 | case PowerHint.LAUNCH: // 1: activate launch boost 0: deactivate. |
| 3430 | if (data == 1 && mBatterySaverController.isLaunchBoostDisabled()) { |
| 3431 | return; |
| 3432 | } |
| 3433 | break; |
| 3434 | } |
| 3435 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 3436 | mNativeWrapper.nativeSendPowerHint(hintId, data); |
Ruchi Kandoi | 15aedf5 | 2014-05-09 19:57:51 -0700 | [diff] [blame] | 3437 | } |
| 3438 | |
Alex Kershaw | 2418ea9 | 2018-10-19 17:17:49 +0100 | [diff] [blame] | 3439 | @VisibleForTesting |
| 3440 | boolean wasDeviceIdleForInternal(long ms) { |
| 3441 | synchronized (mLock) { |
| 3442 | return mLastUserActivityTime + ms < SystemClock.uptimeMillis(); |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | @VisibleForTesting |
| 3447 | void onUserActivity() { |
| 3448 | synchronized (mLock) { |
| 3449 | mLastUserActivityTime = SystemClock.uptimeMillis(); |
| 3450 | } |
| 3451 | } |
| 3452 | |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 3453 | private boolean forceSuspendInternal(int uid) { |
| 3454 | try { |
| 3455 | synchronized (mLock) { |
| 3456 | mForceSuspendActive = true; |
| 3457 | // Place the system in an non-interactive state |
| 3458 | goToSleepInternal(SystemClock.uptimeMillis(), |
| 3459 | PowerManager.GO_TO_SLEEP_REASON_FORCE_SUSPEND, |
| 3460 | PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE, uid); |
| 3461 | |
| 3462 | // Disable all the partial wake locks as well |
| 3463 | updateWakeLockDisabledStatesLocked(); |
| 3464 | } |
| 3465 | |
| 3466 | Slog.i(TAG, "Force-Suspending (uid " + uid + ")..."); |
| 3467 | boolean success = mNativeWrapper.nativeForceSuspend(); |
| 3468 | if (!success) { |
| 3469 | Slog.i(TAG, "Force-Suspending failed in native."); |
| 3470 | } |
| 3471 | return success; |
| 3472 | } finally { |
| 3473 | synchronized (mLock) { |
| 3474 | mForceSuspendActive = false; |
| 3475 | // Re-enable wake locks once again. |
| 3476 | updateWakeLockDisabledStatesLocked(); |
| 3477 | } |
| 3478 | } |
| 3479 | } |
| 3480 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3481 | /** |
| 3482 | * Low-level function turn the device off immediately, without trying |
| 3483 | * to be clean. Most people should use {@link ShutdownThread} for a clean shutdown. |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 3484 | * |
| 3485 | * @param reason code to pass to android_reboot() (e.g. "userrequested"), or null. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3486 | */ |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 3487 | public static void lowLevelShutdown(String reason) { |
| 3488 | if (reason == null) { |
| 3489 | reason = ""; |
| 3490 | } |
| 3491 | SystemProperties.set("sys.powerctl", "shutdown," + reason); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | /** |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 3495 | * Low-level function to reboot the device. On success, this |
| 3496 | * function doesn't return. If more than 20 seconds passes from |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 3497 | * the time a reboot is requested, this method returns. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3498 | * |
| 3499 | * @param reason code to pass to the kernel (e.g. "recovery"), or null. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3500 | */ |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 3501 | public static void lowLevelReboot(String reason) { |
| 3502 | if (reason == null) { |
| 3503 | reason = ""; |
| 3504 | } |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 3505 | |
| 3506 | // If the reason is "quiescent", it means that the boot process should proceed |
| 3507 | // without turning on the screen/lights. |
| 3508 | // The "quiescent" property is sticky, meaning that any number |
| 3509 | // of subsequent reboots should honor the property until it is reset. |
| 3510 | if (reason.equals(PowerManager.REBOOT_QUIESCENT)) { |
| 3511 | sQuiescent = true; |
| 3512 | reason = ""; |
Dmitri Plotnikov | 690c6bd | 2017-05-10 16:26:38 -0700 | [diff] [blame] | 3513 | } else if (reason.endsWith("," + PowerManager.REBOOT_QUIESCENT)) { |
| 3514 | sQuiescent = true; |
| 3515 | reason = reason.substring(0, |
| 3516 | reason.length() - PowerManager.REBOOT_QUIESCENT.length() - 1); |
| 3517 | } |
| 3518 | |
| 3519 | if (reason.equals(PowerManager.REBOOT_RECOVERY) |
| 3520 | || reason.equals(PowerManager.REBOOT_RECOVERY_UPDATE)) { |
| 3521 | reason = "recovery"; |
Dmitri Plotnikov | a8d2c64 | 2016-12-08 10:49:24 -0800 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | if (sQuiescent) { |
| 3525 | // Pass the optional "quiescent" argument to the bootloader to let it know |
| 3526 | // that it should not turn the screen/lights on. |
| 3527 | reason = reason + ",quiescent"; |
| 3528 | } |
| 3529 | |
| 3530 | SystemProperties.set("sys.powerctl", "reboot," + reason); |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 3531 | try { |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 3532 | Thread.sleep(20 * 1000L); |
Nick Kralevich | dbcf2d7 | 2013-04-18 14:41:40 -0700 | [diff] [blame] | 3533 | } catch (InterruptedException e) { |
| 3534 | Thread.currentThread().interrupt(); |
| 3535 | } |
Tao Bao | 90237f7 | 2015-05-21 16:25:19 -0700 | [diff] [blame] | 3536 | Slog.wtf(TAG, "Unexpected return from lowLevelReboot!"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | @Override // Watchdog.Monitor implementation |
| 3540 | public void monitor() { |
| 3541 | // Grab and release lock for watchdog monitor to detect deadlocks. |
| 3542 | synchronized (mLock) { |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3543 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3544 | } |
| 3545 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 3546 | private void dumpInternal(PrintWriter pw) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3547 | pw.println("POWER MANAGER (dumpsys power)\n"); |
| 3548 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 3549 | final WirelessChargerDetector wcd; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3550 | synchronized (mLock) { |
| 3551 | pw.println("Power Manager State:"); |
Dianne Hackborn | 0ef403e | 2017-01-24 18:22:15 -0800 | [diff] [blame] | 3552 | mConstants.dump(pw); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3553 | pw.println(" mDirty=0x" + Integer.toHexString(mDirty)); |
Jeff Brown | fbe9670 | 2014-11-19 18:30:58 -0800 | [diff] [blame] | 3554 | pw.println(" mWakefulness=" + PowerManagerInternal.wakefulnessToString(mWakefulness)); |
| 3555 | pw.println(" mWakefulnessChanging=" + mWakefulnessChanging); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3556 | pw.println(" mIsPowered=" + mIsPowered); |
Jeff Brown | f3fb895 | 2012-10-02 20:57:05 -0700 | [diff] [blame] | 3557 | pw.println(" mPlugType=" + mPlugType); |
Jeff Brown | 016ff14 | 2012-10-15 16:47:22 -0700 | [diff] [blame] | 3558 | pw.println(" mBatteryLevel=" + mBatteryLevel); |
| 3559 | pw.println(" mBatteryLevelWhenDreamStarted=" + mBatteryLevelWhenDreamStarted); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 3560 | pw.println(" mDockState=" + mDockState); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3561 | pw.println(" mStayOn=" + mStayOn); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 3562 | pw.println(" mProximityPositive=" + mProximityPositive); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3563 | pw.println(" mBootCompleted=" + mBootCompleted); |
| 3564 | pw.println(" mSystemReady=" + mSystemReady); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 3565 | pw.println(" mHalAutoSuspendModeEnabled=" + mHalAutoSuspendModeEnabled); |
| 3566 | pw.println(" mHalInteractiveModeEnabled=" + mHalInteractiveModeEnabled); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3567 | pw.println(" mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary)); |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 3568 | pw.print(" mNotifyLongScheduled="); |
| 3569 | if (mNotifyLongScheduled == 0) { |
| 3570 | pw.print("(none)"); |
| 3571 | } else { |
| 3572 | TimeUtils.formatDuration(mNotifyLongScheduled, SystemClock.uptimeMillis(), pw); |
| 3573 | } |
| 3574 | pw.println(); |
| 3575 | pw.print(" mNotifyLongDispatched="); |
| 3576 | if (mNotifyLongDispatched == 0) { |
| 3577 | pw.print("(none)"); |
| 3578 | } else { |
| 3579 | TimeUtils.formatDuration(mNotifyLongDispatched, SystemClock.uptimeMillis(), pw); |
| 3580 | } |
| 3581 | pw.println(); |
| 3582 | pw.print(" mNotifyLongNextCheck="); |
| 3583 | if (mNotifyLongNextCheck == 0) { |
| 3584 | pw.print("(none)"); |
| 3585 | } else { |
| 3586 | TimeUtils.formatDuration(mNotifyLongNextCheck, SystemClock.uptimeMillis(), pw); |
| 3587 | } |
| 3588 | pw.println(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3589 | pw.println(" mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)); |
| 3590 | pw.println(" mRequestWaitForNegativeProximity=" + mRequestWaitForNegativeProximity); |
| 3591 | pw.println(" mSandmanScheduled=" + mSandmanScheduled); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 3592 | pw.println(" mSandmanSummoned=" + mSandmanSummoned); |
Dianne Hackborn | 1427230 | 2014-06-10 23:13:02 -0700 | [diff] [blame] | 3593 | pw.println(" mBatteryLevelLow=" + mBatteryLevelLow); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3594 | pw.println(" mLightDeviceIdleMode=" + mLightDeviceIdleMode); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3595 | pw.println(" mDeviceIdleMode=" + mDeviceIdleMode); |
| 3596 | pw.println(" mDeviceIdleWhitelist=" + Arrays.toString(mDeviceIdleWhitelist)); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3597 | pw.println(" mDeviceIdleTempWhitelist=" + Arrays.toString(mDeviceIdleTempWhitelist)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3598 | pw.println(" mLastWakeTime=" + TimeUtils.formatUptime(mLastWakeTime)); |
| 3599 | pw.println(" mLastSleepTime=" + TimeUtils.formatUptime(mLastSleepTime)); |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 3600 | pw.println(" mLastSleepReason=" + PowerManager.sleepReasonToString(mLastSleepReason)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3601 | pw.println(" mLastUserActivityTime=" + TimeUtils.formatUptime(mLastUserActivityTime)); |
| 3602 | pw.println(" mLastUserActivityTimeNoChangeLights=" |
| 3603 | + TimeUtils.formatUptime(mLastUserActivityTimeNoChangeLights)); |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 3604 | pw.println(" mLastInteractivePowerHintTime=" |
| 3605 | + TimeUtils.formatUptime(mLastInteractivePowerHintTime)); |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 3606 | pw.println(" mLastScreenBrightnessBoostTime=" |
| 3607 | + TimeUtils.formatUptime(mLastScreenBrightnessBoostTime)); |
| 3608 | pw.println(" mScreenBrightnessBoostInProgress=" |
| 3609 | + mScreenBrightnessBoostInProgress); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3610 | pw.println(" mDisplayReady=" + mDisplayReady); |
| 3611 | pw.println(" mHoldingWakeLockSuspendBlocker=" + mHoldingWakeLockSuspendBlocker); |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 3612 | pw.println(" mHoldingDisplaySuspendBlocker=" + mHoldingDisplaySuspendBlocker); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3613 | |
| 3614 | pw.println(); |
| 3615 | pw.println("Settings and Configuration:"); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 3616 | pw.println(" mDecoupleHalAutoSuspendModeFromDisplayConfig=" |
| 3617 | + mDecoupleHalAutoSuspendModeFromDisplayConfig); |
| 3618 | pw.println(" mDecoupleHalInteractiveModeFromDisplayConfig=" |
| 3619 | + mDecoupleHalInteractiveModeFromDisplayConfig); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 3620 | pw.println(" mWakeUpWhenPluggedOrUnpluggedConfig=" |
| 3621 | + mWakeUpWhenPluggedOrUnpluggedConfig); |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 3622 | pw.println(" mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig=" |
| 3623 | + mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig); |
| 3624 | pw.println(" mTheaterModeEnabled=" |
| 3625 | + mTheaterModeEnabled); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 3626 | pw.println(" mSuspendWhenScreenOffDueToProximityConfig=" |
| 3627 | + mSuspendWhenScreenOffDueToProximityConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3628 | pw.println(" mDreamsSupportedConfig=" + mDreamsSupportedConfig); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 3629 | pw.println(" mDreamsEnabledByDefaultConfig=" + mDreamsEnabledByDefaultConfig); |
| 3630 | pw.println(" mDreamsActivatedOnSleepByDefaultConfig=" |
| 3631 | + mDreamsActivatedOnSleepByDefaultConfig); |
| 3632 | pw.println(" mDreamsActivatedOnDockByDefaultConfig=" |
| 3633 | + mDreamsActivatedOnDockByDefaultConfig); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 3634 | pw.println(" mDreamsEnabledOnBatteryConfig=" |
| 3635 | + mDreamsEnabledOnBatteryConfig); |
| 3636 | pw.println(" mDreamsBatteryLevelMinimumWhenPoweredConfig=" |
| 3637 | + mDreamsBatteryLevelMinimumWhenPoweredConfig); |
| 3638 | pw.println(" mDreamsBatteryLevelMinimumWhenNotPoweredConfig=" |
| 3639 | + mDreamsBatteryLevelMinimumWhenNotPoweredConfig); |
| 3640 | pw.println(" mDreamsBatteryLevelDrainCutoffConfig=" |
| 3641 | + mDreamsBatteryLevelDrainCutoffConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3642 | pw.println(" mDreamsEnabledSetting=" + mDreamsEnabledSetting); |
John Spurlock | 1a868b7 | 2012-08-22 09:56:51 -0400 | [diff] [blame] | 3643 | pw.println(" mDreamsActivateOnSleepSetting=" + mDreamsActivateOnSleepSetting); |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 3644 | pw.println(" mDreamsActivateOnDockSetting=" + mDreamsActivateOnDockSetting); |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 3645 | pw.println(" mDozeAfterScreenOff=" + mDozeAfterScreenOff); |
Jeff Brown | 27736f5 | 2014-05-20 17:17:10 -0700 | [diff] [blame] | 3646 | pw.println(" mMinimumScreenOffTimeoutConfig=" + mMinimumScreenOffTimeoutConfig); |
| 3647 | pw.println(" mMaximumScreenDimDurationConfig=" + mMaximumScreenDimDurationConfig); |
| 3648 | pw.println(" mMaximumScreenDimRatioConfig=" + mMaximumScreenDimRatioConfig); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 3649 | pw.println(" mAttentiveTimeoutConfig=" + mAttentiveTimeoutConfig); |
| 3650 | pw.println(" mAttentiveTimeoutSetting=" + mAttentiveTimeoutSetting); |
| 3651 | pw.println(" mAttentiveWarningDurationConfig=" + mAttentiveWarningDurationConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3652 | pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting); |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 3653 | pw.println(" mSleepTimeoutSetting=" + mSleepTimeoutSetting); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3654 | pw.println(" mMaximumScreenOffTimeoutFromDeviceAdmin=" |
| 3655 | + mMaximumScreenOffTimeoutFromDeviceAdmin + " (enforced=" |
| 3656 | + isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked() + ")"); |
| 3657 | pw.println(" mStayOnWhilePluggedInSetting=" + mStayOnWhilePluggedInSetting); |
| 3658 | pw.println(" mScreenBrightnessSetting=" + mScreenBrightnessSetting); |
| 3659 | pw.println(" mScreenBrightnessModeSetting=" + mScreenBrightnessModeSetting); |
| 3660 | pw.println(" mScreenBrightnessOverrideFromWindowManager=" |
| 3661 | + mScreenBrightnessOverrideFromWindowManager); |
Jeff Brown | 1e3b98d | 2012-09-30 18:58:59 -0700 | [diff] [blame] | 3662 | pw.println(" mUserActivityTimeoutOverrideFromWindowManager=" |
| 3663 | + mUserActivityTimeoutOverrideFromWindowManager); |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 3664 | pw.println(" mUserInactiveOverrideFromWindowManager=" |
| 3665 | + mUserInactiveOverrideFromWindowManager); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3666 | pw.println(" mDozeScreenStateOverrideFromDreamManager=" |
| 3667 | + mDozeScreenStateOverrideFromDreamManager); |
Ivan Podogov | 56bc6d0 | 2018-02-26 16:04:24 +0000 | [diff] [blame] | 3668 | pw.println(" mDrawWakeLockOverrideFromSidekick=" + mDrawWakeLockOverrideFromSidekick); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 3669 | pw.println(" mDozeScreenBrightnessOverrideFromDreamManager=" |
| 3670 | + mDozeScreenBrightnessOverrideFromDreamManager); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3671 | pw.println(" mScreenBrightnessSettingMinimum=" + mScreenBrightnessSettingMinimum); |
| 3672 | pw.println(" mScreenBrightnessSettingMaximum=" + mScreenBrightnessSettingMaximum); |
| 3673 | pw.println(" mScreenBrightnessSettingDefault=" + mScreenBrightnessSettingDefault); |
Jason Monk | 27bbb2d | 2015-03-31 16:46:39 -0400 | [diff] [blame] | 3674 | pw.println(" mDoubleTapWakeEnabled=" + mDoubleTapWakeEnabled); |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 3675 | pw.println(" mIsVrModeEnabled=" + mIsVrModeEnabled); |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3676 | pw.println(" mForegroundProfile=" + mForegroundProfile); |
Beverly | 478722e | 2019-10-14 11:16:57 -0400 | [diff] [blame] | 3677 | pw.println(" mUserId=" + mUserId); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3678 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 3679 | final long attentiveTimeout = getAttentiveTimeoutLocked(); |
| 3680 | final long sleepTimeout = getSleepTimeoutLocked(attentiveTimeout); |
| 3681 | final long screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout, attentiveTimeout); |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3682 | final long screenDimDuration = getScreenDimDurationLocked(screenOffTimeout); |
Jeff Brown | ff53254 | 2012-10-02 21:18:04 -0700 | [diff] [blame] | 3683 | pw.println(); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 3684 | pw.println("Attentive timeout: " + attentiveTimeout + " ms"); |
Jeff Brown | 05af6ad | 2014-09-30 20:54:30 -0700 | [diff] [blame] | 3685 | pw.println("Sleep timeout: " + sleepTimeout + " ms"); |
Jeff Brown | ff53254 | 2012-10-02 21:18:04 -0700 | [diff] [blame] | 3686 | pw.println("Screen off timeout: " + screenOffTimeout + " ms"); |
| 3687 | pw.println("Screen dim duration: " + screenDimDuration + " ms"); |
| 3688 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3689 | pw.println(); |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3690 | pw.print("UID states (changing="); |
| 3691 | pw.print(mUidsChanging); |
| 3692 | pw.print(" changed="); |
| 3693 | pw.print(mUidsChanged); |
| 3694 | pw.println("):"); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3695 | for (int i=0; i<mUidState.size(); i++) { |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3696 | final UidState state = mUidState.valueAt(i); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3697 | pw.print(" UID "); UserHandle.formatUid(pw, mUidState.keyAt(i)); |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 3698 | pw.print(": "); |
| 3699 | if (state.mActive) pw.print(" ACTIVE "); |
| 3700 | else pw.print("INACTIVE "); |
| 3701 | pw.print(" count="); |
| 3702 | pw.print(state.mNumWakeLocks); |
| 3703 | pw.print(" state="); |
| 3704 | pw.println(state.mProcState); |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | pw.println(); |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 3708 | pw.println("Looper state:"); |
| 3709 | mHandler.getLooper().dump(new PrintWriterPrinter(pw), " "); |
| 3710 | |
| 3711 | pw.println(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3712 | pw.println("Wake Locks: size=" + mWakeLocks.size()); |
| 3713 | for (WakeLock wl : mWakeLocks) { |
| 3714 | pw.println(" " + wl); |
Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 3715 | } |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3716 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3717 | pw.println(); |
| 3718 | pw.println("Suspend Blockers: size=" + mSuspendBlockers.size()); |
| 3719 | for (SuspendBlocker sb : mSuspendBlockers) { |
| 3720 | pw.println(" " + sb); |
| 3721 | } |
| 3722 | |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 3723 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 3724 | pw.println("Display Power: " + mDisplayPowerCallbacks); |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 3725 | |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 3726 | mBatterySaverPolicy.dump(pw); |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 3727 | mBatterySaverStateMachine.dump(pw); |
Lucas Dupin | 0a5d797 | 2019-01-16 18:52:30 -0800 | [diff] [blame] | 3728 | mAttentionDetector.dump(pw); |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 3729 | |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3730 | pw.println(); |
| 3731 | final int numProfiles = mProfilePowerState.size(); |
| 3732 | pw.println("Profile power states: size=" + numProfiles); |
| 3733 | for (int i = 0; i < numProfiles; i++) { |
| 3734 | final ProfilePowerState profile = mProfilePowerState.valueAt(i); |
| 3735 | pw.print(" mUserId="); |
| 3736 | pw.print(profile.mUserId); |
| 3737 | pw.print(" mScreenOffTimeout="); |
| 3738 | pw.print(profile.mScreenOffTimeout); |
| 3739 | pw.print(" mWakeLockSummary="); |
| 3740 | pw.print(profile.mWakeLockSummary); |
| 3741 | pw.print(" mLastUserActivityTime="); |
| 3742 | pw.print(profile.mLastUserActivityTime); |
| 3743 | pw.print(" mLockingNotified="); |
| 3744 | pw.println(profile.mLockingNotified); |
| 3745 | } |
| 3746 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 3747 | wcd = mWirelessChargerDetector; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 3750 | if (wcd != null) { |
| 3751 | wcd.dump(pw); |
| 3752 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3755 | private void dumpProto(FileDescriptor fd) { |
| 3756 | final WirelessChargerDetector wcd; |
| 3757 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3758 | |
| 3759 | synchronized (mLock) { |
| 3760 | mConstants.dumpProto(proto); |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3761 | proto.write(PowerManagerServiceDumpProto.DIRTY, mDirty); |
| 3762 | proto.write(PowerManagerServiceDumpProto.WAKEFULNESS, mWakefulness); |
| 3763 | proto.write(PowerManagerServiceDumpProto.IS_WAKEFULNESS_CHANGING, mWakefulnessChanging); |
| 3764 | proto.write(PowerManagerServiceDumpProto.IS_POWERED, mIsPowered); |
| 3765 | proto.write(PowerManagerServiceDumpProto.PLUG_TYPE, mPlugType); |
| 3766 | proto.write(PowerManagerServiceDumpProto.BATTERY_LEVEL, mBatteryLevel); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3767 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3768 | PowerManagerServiceDumpProto.BATTERY_LEVEL_WHEN_DREAM_STARTED, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3769 | mBatteryLevelWhenDreamStarted); |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3770 | proto.write(PowerManagerServiceDumpProto.DOCK_STATE, mDockState); |
| 3771 | proto.write(PowerManagerServiceDumpProto.IS_STAY_ON, mStayOn); |
| 3772 | proto.write(PowerManagerServiceDumpProto.IS_PROXIMITY_POSITIVE, mProximityPositive); |
| 3773 | proto.write(PowerManagerServiceDumpProto.IS_BOOT_COMPLETED, mBootCompleted); |
| 3774 | proto.write(PowerManagerServiceDumpProto.IS_SYSTEM_READY, mSystemReady); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3775 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3776 | PowerManagerServiceDumpProto.IS_HAL_AUTO_SUSPEND_MODE_ENABLED, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3777 | mHalAutoSuspendModeEnabled); |
| 3778 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3779 | PowerManagerServiceDumpProto.IS_HAL_AUTO_INTERACTIVE_MODE_ENABLED, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3780 | mHalInteractiveModeEnabled); |
| 3781 | |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3782 | final long activeWakeLocksToken = proto.start(PowerManagerServiceDumpProto.ACTIVE_WAKE_LOCKS); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3783 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3784 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_CPU, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3785 | (mWakeLockSummary & WAKE_LOCK_CPU) != 0); |
| 3786 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3787 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_SCREEN_BRIGHT, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3788 | (mWakeLockSummary & WAKE_LOCK_SCREEN_BRIGHT) != 0); |
| 3789 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3790 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_SCREEN_DIM, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3791 | (mWakeLockSummary & WAKE_LOCK_SCREEN_DIM) != 0); |
| 3792 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3793 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_BUTTON_BRIGHT, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3794 | (mWakeLockSummary & WAKE_LOCK_BUTTON_BRIGHT) != 0); |
| 3795 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3796 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_PROXIMITY_SCREEN_OFF, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3797 | (mWakeLockSummary & WAKE_LOCK_PROXIMITY_SCREEN_OFF) != 0); |
| 3798 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3799 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_STAY_AWAKE, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3800 | (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) != 0); |
| 3801 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3802 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_DOZE, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3803 | (mWakeLockSummary & WAKE_LOCK_DOZE) != 0); |
| 3804 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3805 | PowerManagerServiceDumpProto.ActiveWakeLocksProto.IS_DRAW, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3806 | (mWakeLockSummary & WAKE_LOCK_DRAW) != 0); |
| 3807 | proto.end(activeWakeLocksToken); |
| 3808 | |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3809 | proto.write(PowerManagerServiceDumpProto.NOTIFY_LONG_SCHEDULED_MS, mNotifyLongScheduled); |
| 3810 | proto.write(PowerManagerServiceDumpProto.NOTIFY_LONG_DISPATCHED_MS, mNotifyLongDispatched); |
| 3811 | proto.write(PowerManagerServiceDumpProto.NOTIFY_LONG_NEXT_CHECK_MS, mNotifyLongNextCheck); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3812 | |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3813 | final long userActivityToken = proto.start(PowerManagerServiceDumpProto.USER_ACTIVITY); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3814 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3815 | PowerManagerServiceDumpProto.UserActivityProto.IS_SCREEN_BRIGHT, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3816 | (mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0); |
| 3817 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3818 | PowerManagerServiceDumpProto.UserActivityProto.IS_SCREEN_DIM, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3819 | (mUserActivitySummary & USER_ACTIVITY_SCREEN_DIM) != 0); |
| 3820 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3821 | PowerManagerServiceDumpProto.UserActivityProto.IS_SCREEN_DREAM, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3822 | (mUserActivitySummary & USER_ACTIVITY_SCREEN_DREAM) != 0); |
| 3823 | proto.end(userActivityToken); |
| 3824 | |
| 3825 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3826 | PowerManagerServiceDumpProto.IS_REQUEST_WAIT_FOR_NEGATIVE_PROXIMITY, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3827 | mRequestWaitForNegativeProximity); |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3828 | proto.write(PowerManagerServiceDumpProto.IS_SANDMAN_SCHEDULED, mSandmanScheduled); |
| 3829 | proto.write(PowerManagerServiceDumpProto.IS_SANDMAN_SUMMONED, mSandmanSummoned); |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3830 | proto.write(PowerManagerServiceDumpProto.IS_BATTERY_LEVEL_LOW, mBatteryLevelLow); |
| 3831 | proto.write(PowerManagerServiceDumpProto.IS_LIGHT_DEVICE_IDLE_MODE, mLightDeviceIdleMode); |
| 3832 | proto.write(PowerManagerServiceDumpProto.IS_DEVICE_IDLE_MODE, mDeviceIdleMode); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3833 | |
| 3834 | for (int id : mDeviceIdleWhitelist) { |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3835 | proto.write(PowerManagerServiceDumpProto.DEVICE_IDLE_WHITELIST, id); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3836 | } |
| 3837 | for (int id : mDeviceIdleTempWhitelist) { |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3838 | proto.write(PowerManagerServiceDumpProto.DEVICE_IDLE_TEMP_WHITELIST, id); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3839 | } |
| 3840 | |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3841 | proto.write(PowerManagerServiceDumpProto.LAST_WAKE_TIME_MS, mLastWakeTime); |
| 3842 | proto.write(PowerManagerServiceDumpProto.LAST_SLEEP_TIME_MS, mLastSleepTime); |
| 3843 | proto.write(PowerManagerServiceDumpProto.LAST_USER_ACTIVITY_TIME_MS, mLastUserActivityTime); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3844 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3845 | PowerManagerServiceDumpProto.LAST_USER_ACTIVITY_TIME_NO_CHANGE_LIGHTS_MS, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3846 | mLastUserActivityTimeNoChangeLights); |
| 3847 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3848 | PowerManagerServiceDumpProto.LAST_INTERACTIVE_POWER_HINT_TIME_MS, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3849 | mLastInteractivePowerHintTime); |
| 3850 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3851 | PowerManagerServiceDumpProto.LAST_SCREEN_BRIGHTNESS_BOOST_TIME_MS, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3852 | mLastScreenBrightnessBoostTime); |
| 3853 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3854 | PowerManagerServiceDumpProto.IS_SCREEN_BRIGHTNESS_BOOST_IN_PROGRESS, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3855 | mScreenBrightnessBoostInProgress); |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3856 | proto.write(PowerManagerServiceDumpProto.IS_DISPLAY_READY, mDisplayReady); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3857 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3858 | PowerManagerServiceDumpProto.IS_HOLDING_WAKE_LOCK_SUSPEND_BLOCKER, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3859 | mHoldingWakeLockSuspendBlocker); |
| 3860 | proto.write( |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3861 | PowerManagerServiceDumpProto.IS_HOLDING_DISPLAY_SUSPEND_BLOCKER, |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3862 | mHoldingDisplaySuspendBlocker); |
| 3863 | |
| 3864 | final long settingsAndConfigurationToken = |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 3865 | proto.start(PowerManagerServiceDumpProto.SETTINGS_AND_CONFIGURATION); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3866 | proto.write( |
| 3867 | PowerServiceSettingsAndConfigurationDumpProto |
| 3868 | .IS_DECOUPLE_HAL_AUTO_SUSPEND_MODE_FROM_DISPLAY_CONFIG, |
| 3869 | mDecoupleHalAutoSuspendModeFromDisplayConfig); |
| 3870 | proto.write( |
| 3871 | PowerServiceSettingsAndConfigurationDumpProto |
| 3872 | .IS_DECOUPLE_HAL_INTERACTIVE_MODE_FROM_DISPLAY_CONFIG, |
| 3873 | mDecoupleHalInteractiveModeFromDisplayConfig); |
| 3874 | proto.write( |
| 3875 | PowerServiceSettingsAndConfigurationDumpProto |
| 3876 | .IS_WAKE_UP_WHEN_PLUGGED_OR_UNPLUGGED_CONFIG, |
| 3877 | mWakeUpWhenPluggedOrUnpluggedConfig); |
| 3878 | proto.write( |
| 3879 | PowerServiceSettingsAndConfigurationDumpProto |
| 3880 | .IS_WAKE_UP_WHEN_PLUGGED_OR_UNPLUGGED_IN_THEATER_MODE_CONFIG, |
| 3881 | mWakeUpWhenPluggedOrUnpluggedInTheaterModeConfig); |
| 3882 | proto.write( |
| 3883 | PowerServiceSettingsAndConfigurationDumpProto.IS_THEATER_MODE_ENABLED, |
| 3884 | mTheaterModeEnabled); |
| 3885 | proto.write( |
| 3886 | PowerServiceSettingsAndConfigurationDumpProto |
| 3887 | .IS_SUSPEND_WHEN_SCREEN_OFF_DUE_TO_PROXIMITY_CONFIG, |
| 3888 | mSuspendWhenScreenOffDueToProximityConfig); |
| 3889 | proto.write( |
| 3890 | PowerServiceSettingsAndConfigurationDumpProto.ARE_DREAMS_SUPPORTED_CONFIG, |
| 3891 | mDreamsSupportedConfig); |
| 3892 | proto.write( |
| 3893 | PowerServiceSettingsAndConfigurationDumpProto |
| 3894 | .ARE_DREAMS_ENABLED_BY_DEFAULT_CONFIG, |
| 3895 | mDreamsEnabledByDefaultConfig); |
| 3896 | proto.write( |
| 3897 | PowerServiceSettingsAndConfigurationDumpProto |
| 3898 | .ARE_DREAMS_ACTIVATED_ON_SLEEP_BY_DEFAULT_CONFIG, |
| 3899 | mDreamsActivatedOnSleepByDefaultConfig); |
| 3900 | proto.write( |
| 3901 | PowerServiceSettingsAndConfigurationDumpProto |
| 3902 | .ARE_DREAMS_ACTIVATED_ON_DOCK_BY_DEFAULT_CONFIG, |
| 3903 | mDreamsActivatedOnDockByDefaultConfig); |
| 3904 | proto.write( |
| 3905 | PowerServiceSettingsAndConfigurationDumpProto |
| 3906 | .ARE_DREAMS_ENABLED_ON_BATTERY_CONFIG, |
| 3907 | mDreamsEnabledOnBatteryConfig); |
| 3908 | proto.write( |
| 3909 | PowerServiceSettingsAndConfigurationDumpProto |
| 3910 | .DREAMS_BATTERY_LEVEL_MINIMUM_WHEN_POWERED_CONFIG, |
| 3911 | mDreamsBatteryLevelMinimumWhenPoweredConfig); |
| 3912 | proto.write( |
| 3913 | PowerServiceSettingsAndConfigurationDumpProto |
| 3914 | .DREAMS_BATTERY_LEVEL_MINIMUM_WHEN_NOT_POWERED_CONFIG, |
| 3915 | mDreamsBatteryLevelMinimumWhenNotPoweredConfig); |
| 3916 | proto.write( |
| 3917 | PowerServiceSettingsAndConfigurationDumpProto |
| 3918 | .DREAMS_BATTERY_LEVEL_DRAIN_CUTOFF_CONFIG, |
| 3919 | mDreamsBatteryLevelDrainCutoffConfig); |
| 3920 | proto.write( |
| 3921 | PowerServiceSettingsAndConfigurationDumpProto.ARE_DREAMS_ENABLED_SETTING, |
| 3922 | mDreamsEnabledSetting); |
| 3923 | proto.write( |
| 3924 | PowerServiceSettingsAndConfigurationDumpProto |
| 3925 | .ARE_DREAMS_ACTIVATE_ON_SLEEP_SETTING, |
| 3926 | mDreamsActivateOnSleepSetting); |
| 3927 | proto.write( |
| 3928 | PowerServiceSettingsAndConfigurationDumpProto |
| 3929 | .ARE_DREAMS_ACTIVATE_ON_DOCK_SETTING, |
| 3930 | mDreamsActivateOnDockSetting); |
| 3931 | proto.write( |
| 3932 | PowerServiceSettingsAndConfigurationDumpProto.IS_DOZE_AFTER_SCREEN_OFF_CONFIG, |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 3933 | mDozeAfterScreenOff); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3934 | proto.write( |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3935 | PowerServiceSettingsAndConfigurationDumpProto |
| 3936 | .MINIMUM_SCREEN_OFF_TIMEOUT_CONFIG_MS, |
| 3937 | mMinimumScreenOffTimeoutConfig); |
| 3938 | proto.write( |
| 3939 | PowerServiceSettingsAndConfigurationDumpProto |
| 3940 | .MAXIMUM_SCREEN_DIM_DURATION_CONFIG_MS, |
| 3941 | mMaximumScreenDimDurationConfig); |
| 3942 | proto.write( |
| 3943 | PowerServiceSettingsAndConfigurationDumpProto.MAXIMUM_SCREEN_DIM_RATIO_CONFIG, |
| 3944 | mMaximumScreenDimRatioConfig); |
| 3945 | proto.write( |
| 3946 | PowerServiceSettingsAndConfigurationDumpProto.SCREEN_OFF_TIMEOUT_SETTING_MS, |
| 3947 | mScreenOffTimeoutSetting); |
| 3948 | proto.write( |
| 3949 | PowerServiceSettingsAndConfigurationDumpProto.SLEEP_TIMEOUT_SETTING_MS, |
| 3950 | mSleepTimeoutSetting); |
| 3951 | proto.write( |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 3952 | PowerServiceSettingsAndConfigurationDumpProto.ATTENTIVE_TIMEOUT_SETTING_MS, |
| 3953 | mAttentiveTimeoutSetting); |
| 3954 | proto.write( |
| 3955 | PowerServiceSettingsAndConfigurationDumpProto.ATTENTIVE_TIMEOUT_CONFIG_MS, |
| 3956 | mAttentiveTimeoutConfig); |
| 3957 | proto.write( |
| 3958 | PowerServiceSettingsAndConfigurationDumpProto |
| 3959 | .ATTENTIVE_WARNING_DURATION_CONFIG_MS, |
| 3960 | mAttentiveWarningDurationConfig); |
| 3961 | proto.write( |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3962 | PowerServiceSettingsAndConfigurationDumpProto |
| 3963 | .MAXIMUM_SCREEN_OFF_TIMEOUT_FROM_DEVICE_ADMIN_MS, |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 3964 | // Clamp to int32 |
| 3965 | Math.min(mMaximumScreenOffTimeoutFromDeviceAdmin, Integer.MAX_VALUE)); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3966 | proto.write( |
| 3967 | PowerServiceSettingsAndConfigurationDumpProto |
| 3968 | .IS_MAXIMUM_SCREEN_OFF_TIMEOUT_FROM_DEVICE_ADMIN_ENFORCED_LOCKED, |
| 3969 | isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()); |
| 3970 | |
| 3971 | final long stayOnWhilePluggedInToken = |
| 3972 | proto.start( |
| 3973 | PowerServiceSettingsAndConfigurationDumpProto.STAY_ON_WHILE_PLUGGED_IN); |
| 3974 | proto.write( |
| 3975 | PowerServiceSettingsAndConfigurationDumpProto.StayOnWhilePluggedInProto |
| 3976 | .IS_STAY_ON_WHILE_PLUGGED_IN_AC, |
| 3977 | ((mStayOnWhilePluggedInSetting & BatteryManager.BATTERY_PLUGGED_AC) != 0)); |
| 3978 | proto.write( |
| 3979 | PowerServiceSettingsAndConfigurationDumpProto.StayOnWhilePluggedInProto |
| 3980 | .IS_STAY_ON_WHILE_PLUGGED_IN_USB, |
| 3981 | ((mStayOnWhilePluggedInSetting & BatteryManager.BATTERY_PLUGGED_USB) != 0)); |
| 3982 | proto.write( |
| 3983 | PowerServiceSettingsAndConfigurationDumpProto.StayOnWhilePluggedInProto |
| 3984 | .IS_STAY_ON_WHILE_PLUGGED_IN_WIRELESS, |
| 3985 | ((mStayOnWhilePluggedInSetting & BatteryManager.BATTERY_PLUGGED_WIRELESS) |
| 3986 | != 0)); |
| 3987 | proto.end(stayOnWhilePluggedInToken); |
| 3988 | |
| 3989 | proto.write( |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 3990 | PowerServiceSettingsAndConfigurationDumpProto.SCREEN_BRIGHTNESS_MODE_SETTING, |
| 3991 | mScreenBrightnessModeSetting); |
| 3992 | proto.write( |
| 3993 | PowerServiceSettingsAndConfigurationDumpProto |
| 3994 | .SCREEN_BRIGHTNESS_OVERRIDE_FROM_WINDOW_MANAGER, |
| 3995 | mScreenBrightnessOverrideFromWindowManager); |
| 3996 | proto.write( |
| 3997 | PowerServiceSettingsAndConfigurationDumpProto |
| 3998 | .USER_ACTIVITY_TIMEOUT_OVERRIDE_FROM_WINDOW_MANAGER_MS, |
| 3999 | mUserActivityTimeoutOverrideFromWindowManager); |
| 4000 | proto.write( |
| 4001 | PowerServiceSettingsAndConfigurationDumpProto |
| 4002 | .IS_USER_INACTIVE_OVERRIDE_FROM_WINDOW_MANAGER, |
| 4003 | mUserInactiveOverrideFromWindowManager); |
| 4004 | proto.write( |
| 4005 | PowerServiceSettingsAndConfigurationDumpProto |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4006 | .DOZE_SCREEN_STATE_OVERRIDE_FROM_DREAM_MANAGER, |
| 4007 | mDozeScreenStateOverrideFromDreamManager); |
| 4008 | proto.write( |
| 4009 | PowerServiceSettingsAndConfigurationDumpProto |
Ivan Podogov | 56bc6d0 | 2018-02-26 16:04:24 +0000 | [diff] [blame] | 4010 | .DRAW_WAKE_LOCK_OVERRIDE_FROM_SIDEKICK, |
| 4011 | mDrawWakeLockOverrideFromSidekick); |
| 4012 | proto.write( |
| 4013 | PowerServiceSettingsAndConfigurationDumpProto |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4014 | .DOZED_SCREEN_BRIGHTNESS_OVERRIDE_FROM_DREAM_MANAGER, |
| 4015 | mDozeScreenBrightnessOverrideFromDreamManager); |
| 4016 | |
| 4017 | final long screenBrightnessSettingLimitsToken = |
| 4018 | proto.start( |
| 4019 | PowerServiceSettingsAndConfigurationDumpProto |
| 4020 | .SCREEN_BRIGHTNESS_SETTING_LIMITS); |
| 4021 | proto.write( |
| 4022 | PowerServiceSettingsAndConfigurationDumpProto.ScreenBrightnessSettingLimitsProto |
| 4023 | .SETTING_MINIMUM, |
| 4024 | mScreenBrightnessSettingMinimum); |
| 4025 | proto.write( |
| 4026 | PowerServiceSettingsAndConfigurationDumpProto.ScreenBrightnessSettingLimitsProto |
| 4027 | .SETTING_MAXIMUM, |
| 4028 | mScreenBrightnessSettingMaximum); |
| 4029 | proto.write( |
| 4030 | PowerServiceSettingsAndConfigurationDumpProto.ScreenBrightnessSettingLimitsProto |
| 4031 | .SETTING_DEFAULT, |
| 4032 | mScreenBrightnessSettingDefault); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4033 | proto.end(screenBrightnessSettingLimitsToken); |
| 4034 | |
| 4035 | proto.write( |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4036 | PowerServiceSettingsAndConfigurationDumpProto.IS_DOUBLE_TAP_WAKE_ENABLED, |
| 4037 | mDoubleTapWakeEnabled); |
| 4038 | proto.write( |
| 4039 | PowerServiceSettingsAndConfigurationDumpProto.IS_VR_MODE_ENABLED, |
| 4040 | mIsVrModeEnabled); |
| 4041 | proto.end(settingsAndConfigurationToken); |
| 4042 | |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 4043 | final long attentiveTimeout = getAttentiveTimeoutLocked(); |
| 4044 | final long sleepTimeout = getSleepTimeoutLocked(attentiveTimeout); |
| 4045 | final long screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout, attentiveTimeout); |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 4046 | final long screenDimDuration = getScreenDimDurationLocked(screenOffTimeout); |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 4047 | proto.write(PowerManagerServiceDumpProto.ATTENTIVE_TIMEOUT_MS, attentiveTimeout); |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 4048 | proto.write(PowerManagerServiceDumpProto.SLEEP_TIMEOUT_MS, sleepTimeout); |
| 4049 | proto.write(PowerManagerServiceDumpProto.SCREEN_OFF_TIMEOUT_MS, screenOffTimeout); |
| 4050 | proto.write(PowerManagerServiceDumpProto.SCREEN_DIM_DURATION_MS, screenDimDuration); |
| 4051 | proto.write(PowerManagerServiceDumpProto.ARE_UIDS_CHANGING, mUidsChanging); |
| 4052 | proto.write(PowerManagerServiceDumpProto.ARE_UIDS_CHANGED, mUidsChanged); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4053 | |
| 4054 | for (int i = 0; i < mUidState.size(); i++) { |
| 4055 | final UidState state = mUidState.valueAt(i); |
Kweku Adams | 3219852 | 2017-10-25 15:12:34 -0700 | [diff] [blame] | 4056 | final long uIDToken = proto.start(PowerManagerServiceDumpProto.UID_STATES); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4057 | final int uid = mUidState.keyAt(i); |
Kweku Adams | 3219852 | 2017-10-25 15:12:34 -0700 | [diff] [blame] | 4058 | proto.write(PowerManagerServiceDumpProto.UidStateProto.UID, uid); |
| 4059 | proto.write(PowerManagerServiceDumpProto.UidStateProto.UID_STRING, UserHandle.formatUid(uid)); |
| 4060 | proto.write(PowerManagerServiceDumpProto.UidStateProto.IS_ACTIVE, state.mActive); |
| 4061 | proto.write(PowerManagerServiceDumpProto.UidStateProto.NUM_WAKE_LOCKS, state.mNumWakeLocks); |
Bookatz | db026a2 | 2018-01-10 19:01:56 -0800 | [diff] [blame] | 4062 | proto.write(PowerManagerServiceDumpProto.UidStateProto.PROCESS_STATE, |
| 4063 | ActivityManager.processStateAmToProto(state.mProcState)); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4064 | proto.end(uIDToken); |
| 4065 | } |
| 4066 | |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 4067 | mBatterySaverStateMachine.dumpProto(proto, |
| 4068 | PowerManagerServiceDumpProto.BATTERY_SAVER_STATE_MACHINE); |
| 4069 | |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 4070 | mHandler.getLooper().writeToProto(proto, PowerManagerServiceDumpProto.LOOPER); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4071 | |
| 4072 | for (WakeLock wl : mWakeLocks) { |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 4073 | wl.writeToProto(proto, PowerManagerServiceDumpProto.WAKE_LOCKS); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | for (SuspendBlocker sb : mSuspendBlockers) { |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 4077 | sb.writeToProto(proto, PowerManagerServiceDumpProto.SUSPEND_BLOCKERS); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4078 | } |
| 4079 | wcd = mWirelessChargerDetector; |
| 4080 | } |
| 4081 | |
| 4082 | if (wcd != null) { |
Kweku Adams | e6b00c2 | 2017-10-23 16:46:45 -0700 | [diff] [blame] | 4083 | wcd.writeToProto(proto, PowerManagerServiceDumpProto.WIRELESS_CHARGER_DETECTOR); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4084 | } |
| 4085 | proto.flush(); |
| 4086 | } |
| 4087 | |
Daichi Hirono | 82ab980 | 2016-03-02 13:23:29 +0900 | [diff] [blame] | 4088 | private void incrementBootCount() { |
| 4089 | synchronized (mLock) { |
| 4090 | int count; |
| 4091 | try { |
| 4092 | count = Settings.Global.getInt( |
| 4093 | getContext().getContentResolver(), Settings.Global.BOOT_COUNT); |
| 4094 | } catch (SettingNotFoundException e) { |
| 4095 | count = 0; |
| 4096 | } |
| 4097 | Settings.Global.putInt( |
| 4098 | getContext().getContentResolver(), Settings.Global.BOOT_COUNT, count + 1); |
| 4099 | } |
| 4100 | } |
| 4101 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4102 | private static WorkSource copyWorkSource(WorkSource workSource) { |
| 4103 | return workSource != null ? new WorkSource(workSource) : null; |
| 4104 | } |
| 4105 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 4106 | @VisibleForTesting |
| 4107 | final class BatteryReceiver extends BroadcastReceiver { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4108 | @Override |
| 4109 | public void onReceive(Context context, Intent intent) { |
| 4110 | synchronized (mLock) { |
| 4111 | handleBatteryStateChangedLocked(); |
Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 4112 | } |
Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 4113 | } |
| 4114 | } |
| 4115 | |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 4116 | private final class DreamReceiver extends BroadcastReceiver { |
| 4117 | @Override |
| 4118 | public void onReceive(Context context, Intent intent) { |
| 4119 | synchronized (mLock) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 4120 | scheduleSandmanLocked(); |
John Spurlock | f4f6b4c | 2012-08-25 12:08:03 -0400 | [diff] [blame] | 4121 | } |
| 4122 | } |
| 4123 | } |
| 4124 | |
Santos Cordon | 9b510a2 | 2018-08-24 16:42:54 +0100 | [diff] [blame] | 4125 | @VisibleForTesting |
| 4126 | final class UserSwitchedReceiver extends BroadcastReceiver { |
Jeff Brown | d493596 | 2012-09-25 13:27:20 -0700 | [diff] [blame] | 4127 | @Override |
| 4128 | public void onReceive(Context context, Intent intent) { |
| 4129 | synchronized (mLock) { |
| 4130 | handleSettingsChangedLocked(); |
| 4131 | } |
| 4132 | } |
| 4133 | } |
| 4134 | |
Jeff Brown | ec6aa59 | 2012-10-17 20:30:25 -0700 | [diff] [blame] | 4135 | private final class DockReceiver extends BroadcastReceiver { |
| 4136 | @Override |
| 4137 | public void onReceive(Context context, Intent intent) { |
| 4138 | synchronized (mLock) { |
| 4139 | int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 4140 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 4141 | if (mDockState != dockState) { |
| 4142 | mDockState = dockState; |
| 4143 | mDirty |= DIRTY_DOCK_STATE; |
| 4144 | updatePowerStateLocked(); |
| 4145 | } |
| 4146 | } |
| 4147 | } |
| 4148 | } |
| 4149 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4150 | private final class SettingsObserver extends ContentObserver { |
| 4151 | public SettingsObserver(Handler handler) { |
| 4152 | super(handler); |
| 4153 | } |
| 4154 | |
| 4155 | @Override |
| 4156 | public void onChange(boolean selfChange, Uri uri) { |
| 4157 | synchronized (mLock) { |
| 4158 | handleSettingsChangedLocked(); |
| 4159 | } |
| 4160 | } |
| 4161 | } |
| 4162 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 4163 | private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() { |
Ruchi Kandoi | 03a0428 | 2016-03-14 17:09:17 -0700 | [diff] [blame] | 4164 | @Override |
| 4165 | public void onVrStateChanged(boolean enabled) { |
Ruchi Kandoi | 0d43404 | 2016-10-03 09:12:02 -0700 | [diff] [blame] | 4166 | powerHintInternal(PowerHint.VR_MODE, enabled ? 1 : 0); |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 4167 | |
| 4168 | synchronized (mLock) { |
| 4169 | if (mIsVrModeEnabled != enabled) { |
Santos Cordon | 21e9f2b | 2017-09-13 11:59:39 -0700 | [diff] [blame] | 4170 | setVrModeEnabled(enabled); |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 4171 | mDirty |= DIRTY_VR_MODE_CHANGED; |
| 4172 | updatePowerStateLocked(); |
| 4173 | } |
| 4174 | } |
Ruchi Kandoi | 03a0428 | 2016-03-14 17:09:17 -0700 | [diff] [blame] | 4175 | } |
| 4176 | }; |
| 4177 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4178 | /** |
| 4179 | * Handler for asynchronous operations performed by the power manager. |
| 4180 | */ |
| 4181 | private final class PowerManagerHandler extends Handler { |
| 4182 | public PowerManagerHandler(Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 4183 | super(looper, null, true /*async*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | @Override |
| 4187 | public void handleMessage(Message msg) { |
| 4188 | switch (msg.what) { |
| 4189 | case MSG_USER_ACTIVITY_TIMEOUT: |
| 4190 | handleUserActivityTimeout(); |
| 4191 | break; |
| 4192 | case MSG_SANDMAN: |
| 4193 | handleSandman(); |
| 4194 | break; |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 4195 | case MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT: |
| 4196 | handleScreenBrightnessBoostTimeout(); |
| 4197 | break; |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 4198 | case MSG_CHECK_FOR_LONG_WAKELOCKS: |
| 4199 | checkForLongWakeLocks(); |
| 4200 | break; |
Robert Horvath | 5560f38 | 2019-07-10 10:46:38 +0200 | [diff] [blame] | 4201 | case MSG_ATTENTIVE_TIMEOUT: |
| 4202 | handleAttentiveTimeout(); |
| 4203 | break; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4204 | } |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | /** |
| 4209 | * Represents a wake lock that has been acquired by an application. |
| 4210 | */ |
| 4211 | private final class WakeLock implements IBinder.DeathRecipient { |
| 4212 | public final IBinder mLock; |
| 4213 | public int mFlags; |
| 4214 | public String mTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 4215 | public final String mPackageName; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4216 | public WorkSource mWorkSource; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 4217 | public String mHistoryTag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 4218 | public final int mOwnerUid; |
| 4219 | public final int mOwnerPid; |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 4220 | public final UidState mUidState; |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 4221 | public long mAcquireTime; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 4222 | public boolean mNotifiedAcquired; |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 4223 | public boolean mNotifiedLong; |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 4224 | public boolean mDisabled; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4225 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 4226 | public WakeLock(IBinder lock, int flags, String tag, String packageName, |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 4227 | WorkSource workSource, String historyTag, int ownerUid, int ownerPid, |
| 4228 | UidState uidState) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4229 | mLock = lock; |
| 4230 | mFlags = flags; |
| 4231 | mTag = tag; |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 4232 | mPackageName = packageName; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4233 | mWorkSource = copyWorkSource(workSource); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 4234 | mHistoryTag = historyTag; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4235 | mOwnerUid = ownerUid; |
| 4236 | mOwnerPid = ownerPid; |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 4237 | mUidState = uidState; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | @Override |
| 4241 | public void binderDied() { |
| 4242 | PowerManagerService.this.handleWakeLockDeath(this); |
| 4243 | } |
| 4244 | |
| 4245 | public boolean hasSameProperties(int flags, String tag, WorkSource workSource, |
| 4246 | int ownerUid, int ownerPid) { |
| 4247 | return mFlags == flags |
| 4248 | && mTag.equals(tag) |
| 4249 | && hasSameWorkSource(workSource) |
| 4250 | && mOwnerUid == ownerUid |
| 4251 | && mOwnerPid == ownerPid; |
| 4252 | } |
| 4253 | |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 4254 | public void updateProperties(int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 4255 | WorkSource workSource, String historyTag, int ownerUid, int ownerPid) { |
Dianne Hackborn | 713df15 | 2013-05-17 11:27:57 -0700 | [diff] [blame] | 4256 | if (!mPackageName.equals(packageName)) { |
| 4257 | throw new IllegalStateException("Existing wake lock package name changed: " |
| 4258 | + mPackageName + " to " + packageName); |
| 4259 | } |
| 4260 | if (mOwnerUid != ownerUid) { |
| 4261 | throw new IllegalStateException("Existing wake lock uid changed: " |
| 4262 | + mOwnerUid + " to " + ownerUid); |
| 4263 | } |
| 4264 | if (mOwnerPid != ownerPid) { |
| 4265 | throw new IllegalStateException("Existing wake lock pid changed: " |
| 4266 | + mOwnerPid + " to " + ownerPid); |
| 4267 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4268 | mFlags = flags; |
| 4269 | mTag = tag; |
| 4270 | updateWorkSource(workSource); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 4271 | mHistoryTag = historyTag; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | public boolean hasSameWorkSource(WorkSource workSource) { |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 4275 | return Objects.equals(mWorkSource, workSource); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | public void updateWorkSource(WorkSource workSource) { |
| 4279 | mWorkSource = copyWorkSource(workSource); |
| 4280 | } |
| 4281 | |
| 4282 | @Override |
| 4283 | public String toString() { |
Dianne Hackborn | d0db6f0 | 2016-07-18 14:14:20 -0700 | [diff] [blame] | 4284 | StringBuilder sb = new StringBuilder(); |
| 4285 | sb.append(getLockLevelString()); |
| 4286 | sb.append(" '"); |
| 4287 | sb.append(mTag); |
| 4288 | sb.append("'"); |
| 4289 | sb.append(getLockFlagsString()); |
| 4290 | if (mDisabled) { |
| 4291 | sb.append(" DISABLED"); |
| 4292 | } |
| 4293 | if (mNotifiedAcquired) { |
| 4294 | sb.append(" ACQ="); |
| 4295 | TimeUtils.formatDuration(mAcquireTime-SystemClock.uptimeMillis(), sb); |
| 4296 | } |
| 4297 | if (mNotifiedLong) { |
| 4298 | sb.append(" LONG"); |
| 4299 | } |
| 4300 | sb.append(" (uid="); |
| 4301 | sb.append(mOwnerUid); |
| 4302 | if (mOwnerPid != 0) { |
| 4303 | sb.append(" pid="); |
| 4304 | sb.append(mOwnerPid); |
| 4305 | } |
| 4306 | if (mWorkSource != null) { |
| 4307 | sb.append(" ws="); |
| 4308 | sb.append(mWorkSource); |
| 4309 | } |
| 4310 | sb.append(")"); |
| 4311 | return sb.toString(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4312 | } |
| 4313 | |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4314 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 4315 | final long wakeLockToken = proto.start(fieldId); |
| 4316 | proto.write(WakeLockProto.LOCK_LEVEL, (mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)); |
| 4317 | proto.write(WakeLockProto.TAG, mTag); |
| 4318 | |
| 4319 | final long wakeLockFlagsToken = proto.start(WakeLockProto.FLAGS); |
| 4320 | proto.write(WakeLockProto.WakeLockFlagsProto.IS_ACQUIRE_CAUSES_WAKEUP, |
| 4321 | (mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP)!=0); |
| 4322 | proto.write(WakeLockProto.WakeLockFlagsProto.IS_ON_AFTER_RELEASE, |
| 4323 | (mFlags & PowerManager.ON_AFTER_RELEASE)!=0); |
| 4324 | proto.end(wakeLockFlagsToken); |
| 4325 | |
| 4326 | proto.write(WakeLockProto.IS_DISABLED, mDisabled); |
| 4327 | if (mNotifiedAcquired) { |
| 4328 | proto.write(WakeLockProto.ACQ_MS, mAcquireTime); |
| 4329 | } |
| 4330 | proto.write(WakeLockProto.IS_NOTIFIED_LONG, mNotifiedLong); |
| 4331 | proto.write(WakeLockProto.UID, mOwnerUid); |
| 4332 | proto.write(WakeLockProto.PID, mOwnerPid); |
| 4333 | |
| 4334 | if (mWorkSource != null) { |
| 4335 | mWorkSource.writeToProto(proto, WakeLockProto.WORK_SOURCE); |
| 4336 | } |
| 4337 | proto.end(wakeLockToken); |
| 4338 | } |
| 4339 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 4340 | @SuppressWarnings("deprecation") |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4341 | private String getLockLevelString() { |
| 4342 | switch (mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK) { |
| 4343 | case PowerManager.FULL_WAKE_LOCK: |
| 4344 | return "FULL_WAKE_LOCK "; |
| 4345 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 4346 | return "SCREEN_BRIGHT_WAKE_LOCK "; |
| 4347 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 4348 | return "SCREEN_DIM_WAKE_LOCK "; |
| 4349 | case PowerManager.PARTIAL_WAKE_LOCK: |
| 4350 | return "PARTIAL_WAKE_LOCK "; |
| 4351 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| 4352 | return "PROXIMITY_SCREEN_OFF_WAKE_LOCK"; |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 4353 | case PowerManager.DOZE_WAKE_LOCK: |
| 4354 | return "DOZE_WAKE_LOCK "; |
Jeff Brown | c2932a1 | 2014-11-20 18:04:05 -0800 | [diff] [blame] | 4355 | case PowerManager.DRAW_WAKE_LOCK: |
| 4356 | return "DRAW_WAKE_LOCK "; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4357 | default: |
| 4358 | return "??? "; |
| 4359 | } |
| 4360 | } |
| 4361 | |
| 4362 | private String getLockFlagsString() { |
| 4363 | String result = ""; |
| 4364 | if ((mFlags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 4365 | result += " ACQUIRE_CAUSES_WAKEUP"; |
| 4366 | } |
| 4367 | if ((mFlags & PowerManager.ON_AFTER_RELEASE) != 0) { |
| 4368 | result += " ON_AFTER_RELEASE"; |
| 4369 | } |
| 4370 | return result; |
| 4371 | } |
| 4372 | } |
| 4373 | |
| 4374 | private final class SuspendBlockerImpl implements SuspendBlocker { |
| 4375 | private final String mName; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 4376 | private final String mTraceName; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4377 | private int mReferenceCount; |
| 4378 | |
| 4379 | public SuspendBlockerImpl(String name) { |
| 4380 | mName = name; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 4381 | mTraceName = "SuspendBlocker (" + name + ")"; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4382 | } |
| 4383 | |
| 4384 | @Override |
| 4385 | protected void finalize() throws Throwable { |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 4386 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4387 | if (mReferenceCount != 0) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 4388 | Slog.wtf(TAG, "Suspend blocker \"" + mName |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4389 | + "\" was finalized without being released!"); |
| 4390 | mReferenceCount = 0; |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 4391 | mNativeWrapper.nativeReleaseSuspendBlocker(mName); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 4392 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 4393 | } |
| 4394 | } finally { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4395 | super.finalize(); |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 4396 | } |
| 4397 | } |
| 4398 | |
Craig Mautner | 75fc9de | 2012-06-18 16:53:27 -0700 | [diff] [blame] | 4399 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4400 | public void acquire() { |
| 4401 | synchronized (this) { |
| 4402 | mReferenceCount += 1; |
| 4403 | if (mReferenceCount == 1) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 4404 | if (DEBUG_SPEW) { |
| 4405 | Slog.d(TAG, "Acquiring suspend blocker \"" + mName + "\"."); |
| 4406 | } |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 4407 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 4408 | mNativeWrapper.nativeAcquireSuspendBlocker(mName); |
Craig Mautner | 3793368 | 2012-06-06 14:13:39 -0700 | [diff] [blame] | 4409 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | |
Craig Mautner | 75fc9de | 2012-06-18 16:53:27 -0700 | [diff] [blame] | 4413 | @Override |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4414 | public void release() { |
| 4415 | synchronized (this) { |
| 4416 | mReferenceCount -= 1; |
| 4417 | if (mReferenceCount == 0) { |
Jeff Brown | 27f7a86 | 2012-12-12 15:43:31 -0800 | [diff] [blame] | 4418 | if (DEBUG_SPEW) { |
| 4419 | Slog.d(TAG, "Releasing suspend blocker \"" + mName + "\"."); |
| 4420 | } |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 4421 | mNativeWrapper.nativeReleaseSuspendBlocker(mName); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 4422 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, mTraceName, 0); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4423 | } else if (mReferenceCount < 0) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 4424 | Slog.wtf(TAG, "Suspend blocker \"" + mName |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4425 | + "\" was released without being acquired!", new Throwable()); |
| 4426 | mReferenceCount = 0; |
| 4427 | } |
| 4428 | } |
Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 4429 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4430 | |
| 4431 | @Override |
| 4432 | public String toString() { |
| 4433 | synchronized (this) { |
| 4434 | return mName + ": ref count=" + mReferenceCount; |
| 4435 | } |
| 4436 | } |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 4437 | |
| 4438 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 4439 | final long sbToken = proto.start(fieldId); |
| 4440 | synchronized (this) { |
| 4441 | proto.write(SuspendBlockerProto.NAME, mName); |
| 4442 | proto.write(SuspendBlockerProto.REFERENCE_COUNT, mReferenceCount); |
| 4443 | } |
| 4444 | proto.end(sbToken); |
| 4445 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 4446 | } |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 4447 | |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 4448 | static final class UidState { |
| 4449 | final int mUid; |
| 4450 | int mNumWakeLocks; |
| 4451 | int mProcState; |
| 4452 | boolean mActive; |
| 4453 | |
| 4454 | UidState(int uid) { |
| 4455 | mUid = uid; |
| 4456 | } |
| 4457 | } |
| 4458 | |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 4459 | @VisibleForTesting |
| 4460 | final class BinderService extends IPowerManager.Stub { |
Jocelyn Dang | f2c38c1 | 2017-03-31 14:03:37 -0700 | [diff] [blame] | 4461 | @Override |
| 4462 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 4463 | FileDescriptor err, String[] args, ShellCallback callback, |
| 4464 | ResultReceiver resultReceiver) { |
| 4465 | (new PowerManagerShellCommand(this)).exec( |
| 4466 | this, in, out, err, args, callback, resultReceiver); |
| 4467 | } |
| 4468 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4469 | @Override // Binder call |
| 4470 | public void acquireWakeLockWithUid(IBinder lock, int flags, String tag, |
| 4471 | String packageName, int uid) { |
Dianne Hackborn | ef640cd | 2014-03-25 14:41:05 -0700 | [diff] [blame] | 4472 | if (uid < 0) { |
| 4473 | uid = Binder.getCallingUid(); |
| 4474 | } |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 4475 | acquireWakeLock(lock, flags, tag, packageName, new WorkSource(uid), null); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | @Override // Binder call |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 4479 | public void powerHint(int hintId, int data) { |
Dianne Hackborn | ddef7e7 | 2014-07-09 16:39:14 -0700 | [diff] [blame] | 4480 | if (!mSystemReady) { |
| 4481 | // Service not ready yet, so who the heck cares about power hints, bah. |
| 4482 | return; |
| 4483 | } |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 4484 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
Ruchi Kandoi | 15aedf5 | 2014-05-09 19:57:51 -0700 | [diff] [blame] | 4485 | powerHintInternal(hintId, data); |
Ruchi Kandoi | f20a5eb | 2014-04-01 17:39:20 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | @Override // Binder call |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4489 | public void acquireWakeLock(IBinder lock, int flags, String tag, String packageName, |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 4490 | WorkSource ws, String historyTag) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4491 | if (lock == null) { |
| 4492 | throw new IllegalArgumentException("lock must not be null"); |
| 4493 | } |
| 4494 | if (packageName == null) { |
| 4495 | throw new IllegalArgumentException("packageName must not be null"); |
| 4496 | } |
| 4497 | PowerManager.validateWakeLockParameters(flags, tag); |
| 4498 | |
| 4499 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
Jeff Brown | 72671fb | 2014-08-21 21:41:09 -0700 | [diff] [blame] | 4500 | if ((flags & PowerManager.DOZE_WAKE_LOCK) != 0) { |
| 4501 | mContext.enforceCallingOrSelfPermission( |
| 4502 | android.Manifest.permission.DEVICE_POWER, null); |
| 4503 | } |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 4504 | if (ws != null && !ws.isEmpty()) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4505 | mContext.enforceCallingOrSelfPermission( |
| 4506 | android.Manifest.permission.UPDATE_DEVICE_STATS, null); |
| 4507 | } else { |
| 4508 | ws = null; |
| 4509 | } |
| 4510 | |
| 4511 | final int uid = Binder.getCallingUid(); |
| 4512 | final int pid = Binder.getCallingPid(); |
| 4513 | final long ident = Binder.clearCallingIdentity(); |
| 4514 | try { |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 4515 | acquireWakeLockInternal(lock, flags, tag, packageName, ws, historyTag, uid, pid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4516 | } finally { |
| 4517 | Binder.restoreCallingIdentity(ident); |
| 4518 | } |
| 4519 | } |
| 4520 | |
| 4521 | @Override // Binder call |
| 4522 | public void releaseWakeLock(IBinder lock, int flags) { |
| 4523 | if (lock == null) { |
| 4524 | throw new IllegalArgumentException("lock must not be null"); |
| 4525 | } |
| 4526 | |
| 4527 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 4528 | |
| 4529 | final long ident = Binder.clearCallingIdentity(); |
| 4530 | try { |
| 4531 | releaseWakeLockInternal(lock, flags); |
| 4532 | } finally { |
| 4533 | Binder.restoreCallingIdentity(ident); |
| 4534 | } |
| 4535 | } |
| 4536 | |
| 4537 | @Override // Binder call |
| 4538 | public void updateWakeLockUids(IBinder lock, int[] uids) { |
| 4539 | WorkSource ws = null; |
| 4540 | |
| 4541 | if (uids != null) { |
| 4542 | ws = new WorkSource(); |
| 4543 | // XXX should WorkSource have a way to set uids as an int[] instead of adding them |
| 4544 | // one at a time? |
| 4545 | for (int i = 0; i < uids.length; i++) { |
| 4546 | ws.add(uids[i]); |
| 4547 | } |
| 4548 | } |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 4549 | updateWakeLockWorkSource(lock, ws, null); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | @Override // Binder call |
Dianne Hackborn | 4590e52 | 2014-03-24 13:36:46 -0700 | [diff] [blame] | 4553 | public void updateWakeLockWorkSource(IBinder lock, WorkSource ws, String historyTag) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4554 | if (lock == null) { |
| 4555 | throw new IllegalArgumentException("lock must not be null"); |
| 4556 | } |
| 4557 | |
| 4558 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
Narayan Kamath | 8182202 | 2017-12-08 11:56:01 +0000 | [diff] [blame] | 4559 | if (ws != null && !ws.isEmpty()) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4560 | mContext.enforceCallingOrSelfPermission( |
| 4561 | android.Manifest.permission.UPDATE_DEVICE_STATS, null); |
| 4562 | } else { |
| 4563 | ws = null; |
| 4564 | } |
| 4565 | |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 4566 | final int callingUid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4567 | final long ident = Binder.clearCallingIdentity(); |
| 4568 | try { |
Dianne Hackborn | d953c53 | 2014-08-16 18:17:38 -0700 | [diff] [blame] | 4569 | updateWakeLockWorkSourceInternal(lock, ws, historyTag, callingUid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4570 | } finally { |
| 4571 | Binder.restoreCallingIdentity(ident); |
| 4572 | } |
| 4573 | } |
| 4574 | |
| 4575 | @Override // Binder call |
| 4576 | public boolean isWakeLockLevelSupported(int level) { |
| 4577 | final long ident = Binder.clearCallingIdentity(); |
| 4578 | try { |
| 4579 | return isWakeLockLevelSupportedInternal(level); |
| 4580 | } finally { |
| 4581 | Binder.restoreCallingIdentity(ident); |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | @Override // Binder call |
| 4586 | public void userActivity(long eventTime, int event, int flags) { |
| 4587 | final long now = SystemClock.uptimeMillis(); |
| 4588 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER) |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 4589 | != PackageManager.PERMISSION_GRANTED |
| 4590 | && mContext.checkCallingOrSelfPermission( |
| 4591 | android.Manifest.permission.USER_ACTIVITY) |
| 4592 | != PackageManager.PERMISSION_GRANTED) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4593 | // Once upon a time applications could call userActivity(). |
| 4594 | // Now we require the DEVICE_POWER permission. Log a warning and ignore the |
| 4595 | // request instead of throwing a SecurityException so we don't break old apps. |
| 4596 | synchronized (mLock) { |
| 4597 | if (now >= mLastWarningAboutUserActivityPermission + (5 * 60 * 1000)) { |
| 4598 | mLastWarningAboutUserActivityPermission = now; |
| 4599 | Slog.w(TAG, "Ignoring call to PowerManager.userActivity() because the " |
Jeff Brown | 0a57112 | 2014-08-21 21:50:43 -0700 | [diff] [blame] | 4600 | + "caller does not have DEVICE_POWER or USER_ACTIVITY " |
| 4601 | + "permission. Please fix your app! " |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4602 | + " pid=" + Binder.getCallingPid() |
| 4603 | + " uid=" + Binder.getCallingUid()); |
| 4604 | } |
| 4605 | } |
| 4606 | return; |
| 4607 | } |
| 4608 | |
Jim Miller | dca15d2 | 2015-06-16 20:55:13 -0700 | [diff] [blame] | 4609 | if (eventTime > now) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4610 | throw new IllegalArgumentException("event time must not be in the future"); |
| 4611 | } |
| 4612 | |
| 4613 | final int uid = Binder.getCallingUid(); |
| 4614 | final long ident = Binder.clearCallingIdentity(); |
| 4615 | try { |
| 4616 | userActivityInternal(eventTime, event, flags, uid); |
| 4617 | } finally { |
| 4618 | Binder.restoreCallingIdentity(ident); |
| 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | @Override // Binder call |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 4623 | public void wakeUp(long eventTime, @WakeReason int reason, String details, |
| 4624 | String opPackageName) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4625 | if (eventTime > SystemClock.uptimeMillis()) { |
| 4626 | throw new IllegalArgumentException("event time must not be in the future"); |
| 4627 | } |
| 4628 | |
| 4629 | mContext.enforceCallingOrSelfPermission( |
| 4630 | android.Manifest.permission.DEVICE_POWER, null); |
| 4631 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 4632 | final int uid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4633 | final long ident = Binder.clearCallingIdentity(); |
| 4634 | try { |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 4635 | wakeUpInternal(eventTime, reason, details, uid, opPackageName, uid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4636 | } finally { |
| 4637 | Binder.restoreCallingIdentity(ident); |
| 4638 | } |
| 4639 | } |
| 4640 | |
| 4641 | @Override // Binder call |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 4642 | public void goToSleep(long eventTime, int reason, int flags) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4643 | if (eventTime > SystemClock.uptimeMillis()) { |
| 4644 | throw new IllegalArgumentException("event time must not be in the future"); |
| 4645 | } |
| 4646 | |
| 4647 | mContext.enforceCallingOrSelfPermission( |
| 4648 | android.Manifest.permission.DEVICE_POWER, null); |
| 4649 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 4650 | final int uid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4651 | final long ident = Binder.clearCallingIdentity(); |
| 4652 | try { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 4653 | goToSleepInternal(eventTime, reason, flags, uid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4654 | } finally { |
| 4655 | Binder.restoreCallingIdentity(ident); |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | @Override // Binder call |
| 4660 | public void nap(long eventTime) { |
| 4661 | if (eventTime > SystemClock.uptimeMillis()) { |
| 4662 | throw new IllegalArgumentException("event time must not be in the future"); |
| 4663 | } |
| 4664 | |
| 4665 | mContext.enforceCallingOrSelfPermission( |
| 4666 | android.Manifest.permission.DEVICE_POWER, null); |
| 4667 | |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 4668 | final int uid = Binder.getCallingUid(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4669 | final long ident = Binder.clearCallingIdentity(); |
| 4670 | try { |
Jeff Brown | c12035c | 2014-08-13 18:52:25 -0700 | [diff] [blame] | 4671 | napInternal(eventTime, uid); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4672 | } finally { |
| 4673 | Binder.restoreCallingIdentity(ident); |
| 4674 | } |
| 4675 | } |
| 4676 | |
| 4677 | @Override // Binder call |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 4678 | public boolean isInteractive() { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4679 | final long ident = Binder.clearCallingIdentity(); |
| 4680 | try { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 4681 | return isInteractiveInternal(); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4682 | } finally { |
| 4683 | Binder.restoreCallingIdentity(ident); |
| 4684 | } |
| 4685 | } |
| 4686 | |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 4687 | @Override // Binder call |
| 4688 | public boolean isPowerSaveMode() { |
| 4689 | final long ident = Binder.clearCallingIdentity(); |
| 4690 | try { |
Makoto Onuki | bd7a625 | 2018-05-10 13:41:39 -0700 | [diff] [blame] | 4691 | return mBatterySaverController.isEnabled(); |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 4692 | } finally { |
| 4693 | Binder.restoreCallingIdentity(ident); |
| 4694 | } |
| 4695 | } |
| 4696 | |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 4697 | // Binder call |
| 4698 | public PowerSaveState getPowerSaveState(@ServiceType int serviceType) { |
| 4699 | final long ident = Binder.clearCallingIdentity(); |
| 4700 | try { |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 4701 | return mBatterySaverPolicy.getBatterySaverPolicy(serviceType); |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 4702 | } finally { |
| 4703 | Binder.restoreCallingIdentity(ident); |
| 4704 | } |
| 4705 | } |
| 4706 | |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 4707 | @Override // Binder call |
Salvador Martinez | c8c4c5d | 2019-03-11 11:11:37 -0700 | [diff] [blame] | 4708 | public boolean setPowerSaveModeEnabled(boolean enabled) { |
Salvador Martinez | a80bbab | 2018-09-24 10:36:11 -0700 | [diff] [blame] | 4709 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER) |
| 4710 | != PackageManager.PERMISSION_GRANTED) { |
| 4711 | mContext.enforceCallingOrSelfPermission( |
| 4712 | android.Manifest.permission.DEVICE_POWER, null); |
| 4713 | } |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 4714 | final long ident = Binder.clearCallingIdentity(); |
| 4715 | try { |
Makoto Onuki | a3cd7b9 | 2018-03-19 14:47:05 -0700 | [diff] [blame] | 4716 | return setLowPowerModeInternal(enabled); |
John Spurlock | 8d4e6cb | 2014-09-14 11:10:22 -0400 | [diff] [blame] | 4717 | } finally { |
| 4718 | Binder.restoreCallingIdentity(ident); |
| 4719 | } |
| 4720 | } |
| 4721 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4722 | @Override // Binder call |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 4723 | public boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold) { |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 4724 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER, |
| 4725 | "updateDynamicPowerSavings"); |
| 4726 | final long ident = Binder.clearCallingIdentity(); |
| 4727 | try { |
| 4728 | final ContentResolver resolver = mContext.getContentResolver(); |
| 4729 | boolean success = Settings.Global.putInt(resolver, |
| 4730 | Settings.Global.DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD, |
| 4731 | disableThreshold); |
| 4732 | if (success) { |
| 4733 | // abort updating if we weren't able to succeed on the threshold |
| 4734 | success &= Settings.Global.putInt(resolver, |
| 4735 | Settings.Global.DYNAMIC_POWER_SAVINGS_ENABLED, |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 4736 | powerSaveHint ? 1 : 0); |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 4737 | } |
| 4738 | return success; |
| 4739 | } finally { |
| 4740 | Binder.restoreCallingIdentity(ident); |
| 4741 | } |
| 4742 | } |
| 4743 | |
| 4744 | @Override // Binder call |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 4745 | public boolean setAdaptivePowerSavePolicy(@NonNull BatterySaverPolicyConfig config) { |
| 4746 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER) |
| 4747 | != PackageManager.PERMISSION_GRANTED) { |
| 4748 | mContext.enforceCallingOrSelfPermission( |
| 4749 | android.Manifest.permission.DEVICE_POWER, "setAdaptivePowerSavePolicy"); |
| 4750 | } |
| 4751 | final long ident = Binder.clearCallingIdentity(); |
| 4752 | try { |
| 4753 | return mBatterySaverStateMachine.setAdaptiveBatterySaverPolicy(config); |
| 4754 | } finally { |
| 4755 | Binder.restoreCallingIdentity(ident); |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | @Override // Binder call |
| 4760 | public boolean setAdaptivePowerSaveEnabled(boolean enabled) { |
| 4761 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER) |
| 4762 | != PackageManager.PERMISSION_GRANTED) { |
| 4763 | mContext.enforceCallingOrSelfPermission( |
| 4764 | android.Manifest.permission.DEVICE_POWER, "setAdaptivePowerSaveEnabled"); |
| 4765 | } |
| 4766 | final long ident = Binder.clearCallingIdentity(); |
| 4767 | try { |
| 4768 | return mBatterySaverStateMachine.setAdaptiveBatterySaverEnabled(enabled); |
| 4769 | } finally { |
| 4770 | Binder.restoreCallingIdentity(ident); |
| 4771 | } |
| 4772 | } |
| 4773 | |
| 4774 | @Override // Binder call |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 4775 | public int getPowerSaveModeTrigger() { |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 4776 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER, null); |
| 4777 | final long ident = Binder.clearCallingIdentity(); |
| 4778 | try { |
| 4779 | return Settings.Global.getInt(mContext.getContentResolver(), |
Salvador Martinez | b85a9f8 | 2019-03-20 16:21:27 -0700 | [diff] [blame] | 4780 | Settings.Global.AUTOMATIC_POWER_SAVE_MODE, |
| 4781 | PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE); |
Salvador Martinez | 812ea75 | 2018-10-19 13:03:20 -0700 | [diff] [blame] | 4782 | } finally { |
| 4783 | Binder.restoreCallingIdentity(ident); |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | @Override // Binder call |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 4788 | public boolean isDeviceIdleMode() { |
| 4789 | final long ident = Binder.clearCallingIdentity(); |
| 4790 | try { |
| 4791 | return isDeviceIdleModeInternal(); |
| 4792 | } finally { |
| 4793 | Binder.restoreCallingIdentity(ident); |
| 4794 | } |
| 4795 | } |
| 4796 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 4797 | @Override // Binder call |
| 4798 | public boolean isLightDeviceIdleMode() { |
| 4799 | final long ident = Binder.clearCallingIdentity(); |
| 4800 | try { |
| 4801 | return isLightDeviceIdleModeInternal(); |
| 4802 | } finally { |
| 4803 | Binder.restoreCallingIdentity(ident); |
| 4804 | } |
| 4805 | } |
| 4806 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4807 | /** |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 4808 | * Gets the reason for the last time the phone had to reboot. |
| 4809 | * |
| 4810 | * @return The reason the phone last shut down as an int or |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 4811 | * {@link PowerManager#SHUTDOWN_REASON_UNKNOWN} if the file could not be opened. |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 4812 | */ |
| 4813 | @Override // Binder call |
| 4814 | public int getLastShutdownReason() { |
| 4815 | mContext.enforceCallingOrSelfPermission( |
| 4816 | android.Manifest.permission.DEVICE_POWER, null); |
| 4817 | |
| 4818 | final long ident = Binder.clearCallingIdentity(); |
| 4819 | try { |
Mark Salyzyn | 74ce8b3 | 2018-06-05 09:06:54 -0700 | [diff] [blame] | 4820 | return getLastShutdownReasonInternal(REBOOT_PROPERTY); |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 4821 | } finally { |
| 4822 | Binder.restoreCallingIdentity(ident); |
| 4823 | } |
| 4824 | } |
| 4825 | |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 4826 | @Override // Binder call |
| 4827 | public int getLastSleepReason() { |
| 4828 | mContext.enforceCallingOrSelfPermission( |
| 4829 | android.Manifest.permission.DEVICE_POWER, null); |
| 4830 | |
| 4831 | final long ident = Binder.clearCallingIdentity(); |
| 4832 | try { |
| 4833 | return getLastSleepReasonInternal(); |
| 4834 | } finally { |
| 4835 | Binder.restoreCallingIdentity(ident); |
| 4836 | } |
| 4837 | } |
| 4838 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 4839 | /** |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4840 | * Reboots the device. |
| 4841 | * |
| 4842 | * @param confirm If true, shows a reboot confirmation dialog. |
| 4843 | * @param reason The reason for the reboot, or null if none. |
| 4844 | * @param wait If true, this call waits for the reboot to complete and does not return. |
| 4845 | */ |
| 4846 | @Override // Binder call |
| 4847 | public void reboot(boolean confirm, String reason, boolean wait) { |
| 4848 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
Tao Bao | e8a403d | 2015-12-31 07:44:55 -0800 | [diff] [blame] | 4849 | if (PowerManager.REBOOT_RECOVERY.equals(reason) |
| 4850 | || PowerManager.REBOOT_RECOVERY_UPDATE.equals(reason)) { |
Doug Zongker | 3b0218b | 2014-01-14 12:29:06 -0800 | [diff] [blame] | 4851 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.RECOVERY, null); |
| 4852 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4853 | |
| 4854 | final long ident = Binder.clearCallingIdentity(); |
| 4855 | try { |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 4856 | shutdownOrRebootInternal(HALT_MODE_REBOOT, confirm, reason, wait); |
| 4857 | } finally { |
| 4858 | Binder.restoreCallingIdentity(ident); |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | /** |
| 4863 | * Reboots the device into safe mode |
| 4864 | * |
| 4865 | * @param confirm If true, shows a reboot confirmation dialog. |
| 4866 | * @param wait If true, this call waits for the reboot to complete and does not return. |
| 4867 | */ |
| 4868 | @Override // Binder call |
| 4869 | public void rebootSafeMode(boolean confirm, boolean wait) { |
| 4870 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 4871 | |
| 4872 | final long ident = Binder.clearCallingIdentity(); |
| 4873 | try { |
| 4874 | shutdownOrRebootInternal(HALT_MODE_REBOOT_SAFE_MODE, confirm, |
| 4875 | PowerManager.REBOOT_SAFE_MODE, wait); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4876 | } finally { |
| 4877 | Binder.restoreCallingIdentity(ident); |
| 4878 | } |
| 4879 | } |
| 4880 | |
| 4881 | /** |
| 4882 | * Shuts down the device. |
| 4883 | * |
| 4884 | * @param confirm If true, shows a shutdown confirmation dialog. |
| 4885 | * @param wait If true, this call waits for the shutdown to complete and does not return. |
| 4886 | */ |
| 4887 | @Override // Binder call |
Yusuke Sato | 705ffd1 | 2015-07-21 15:52:11 -0700 | [diff] [blame] | 4888 | public void shutdown(boolean confirm, String reason, boolean wait) { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4889 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 4890 | |
| 4891 | final long ident = Binder.clearCallingIdentity(); |
| 4892 | try { |
Tony Mantler | b8009fd | 2016-03-14 15:55:35 -0700 | [diff] [blame] | 4893 | shutdownOrRebootInternal(HALT_MODE_SHUTDOWN, confirm, reason, wait); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4894 | } finally { |
| 4895 | Binder.restoreCallingIdentity(ident); |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | /** |
| 4900 | * Crash the runtime (causing a complete restart of the Android framework). |
| 4901 | * Requires REBOOT permission. Mostly for testing. Should not return. |
| 4902 | */ |
| 4903 | @Override // Binder call |
| 4904 | public void crash(String message) { |
| 4905 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 4906 | |
| 4907 | final long ident = Binder.clearCallingIdentity(); |
| 4908 | try { |
| 4909 | crashInternal(message); |
| 4910 | } finally { |
| 4911 | Binder.restoreCallingIdentity(ident); |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | /** |
| 4916 | * Set the setting that determines whether the device stays on when plugged in. |
| 4917 | * The argument is a bit string, with each bit specifying a power source that, |
| 4918 | * when the device is connected to that source, causes the device to stay on. |
| 4919 | * See {@link android.os.BatteryManager} for the list of power sources that |
| 4920 | * can be specified. Current values include |
| 4921 | * {@link android.os.BatteryManager#BATTERY_PLUGGED_AC} |
| 4922 | * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB} |
| 4923 | * |
| 4924 | * Used by "adb shell svc power stayon ..." |
| 4925 | * |
| 4926 | * @param val an {@code int} containing the bits that specify which power sources |
| 4927 | * should cause the device to stay on. |
| 4928 | */ |
| 4929 | @Override // Binder call |
| 4930 | public void setStayOnSetting(int val) { |
Billy Lau | 6ad2d66 | 2015-07-18 00:26:58 +0100 | [diff] [blame] | 4931 | int uid = Binder.getCallingUid(); |
| 4932 | // if uid is of root's, we permit this operation straight away |
| 4933 | if (uid != Process.ROOT_UID) { |
| 4934 | if (!Settings.checkAndNoteWriteSettingsOperation(mContext, uid, |
| 4935 | Settings.getPackageNameForUid(mContext, uid), true)) { |
| 4936 | return; |
| 4937 | } |
| 4938 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4939 | |
| 4940 | final long ident = Binder.clearCallingIdentity(); |
| 4941 | try { |
| 4942 | setStayOnSettingInternal(val); |
| 4943 | } finally { |
| 4944 | Binder.restoreCallingIdentity(ident); |
| 4945 | } |
| 4946 | } |
| 4947 | |
| 4948 | /** |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 4949 | * Used by the phone application to make the attention LED flash when ringing. |
| 4950 | */ |
| 4951 | @Override // Binder call |
| 4952 | public void setAttentionLight(boolean on, int color) { |
| 4953 | mContext.enforceCallingOrSelfPermission( |
| 4954 | android.Manifest.permission.DEVICE_POWER, null); |
| 4955 | |
| 4956 | final long ident = Binder.clearCallingIdentity(); |
| 4957 | try { |
| 4958 | setAttentionLightInternal(on, color); |
| 4959 | } finally { |
| 4960 | Binder.restoreCallingIdentity(ident); |
| 4961 | } |
| 4962 | } |
| 4963 | |
| 4964 | @Override // Binder call |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 4965 | public void setDozeAfterScreenOff(boolean on) { |
| 4966 | mContext.enforceCallingOrSelfPermission( |
| 4967 | android.Manifest.permission.DEVICE_POWER, null); |
| 4968 | |
| 4969 | final long ident = Binder.clearCallingIdentity(); |
| 4970 | try { |
| 4971 | setDozeAfterScreenOffInternal(on); |
| 4972 | } finally { |
| 4973 | Binder.restoreCallingIdentity(ident); |
| 4974 | } |
| 4975 | } |
| 4976 | |
| 4977 | @Override // Binder call |
Jeff Brown | e333e67 | 2014-10-28 13:48:55 -0700 | [diff] [blame] | 4978 | public void boostScreenBrightness(long eventTime) { |
| 4979 | if (eventTime > SystemClock.uptimeMillis()) { |
| 4980 | throw new IllegalArgumentException("event time must not be in the future"); |
| 4981 | } |
| 4982 | |
| 4983 | mContext.enforceCallingOrSelfPermission( |
| 4984 | android.Manifest.permission.DEVICE_POWER, null); |
| 4985 | |
| 4986 | final int uid = Binder.getCallingUid(); |
| 4987 | final long ident = Binder.clearCallingIdentity(); |
| 4988 | try { |
| 4989 | boostScreenBrightnessInternal(eventTime, uid); |
| 4990 | } finally { |
| 4991 | Binder.restoreCallingIdentity(ident); |
| 4992 | } |
| 4993 | } |
| 4994 | |
| 4995 | @Override // Binder call |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 4996 | public boolean isScreenBrightnessBoosted() { |
| 4997 | final long ident = Binder.clearCallingIdentity(); |
| 4998 | try { |
| 4999 | return isScreenBrightnessBoostedInternal(); |
| 5000 | } finally { |
| 5001 | Binder.restoreCallingIdentity(ident); |
| 5002 | } |
| 5003 | } |
| 5004 | |
Santos Cordon | 12f92eb | 2019-02-01 21:28:47 +0000 | [diff] [blame] | 5005 | @Override // binder call |
| 5006 | public boolean forceSuspend() { |
| 5007 | mContext.enforceCallingOrSelfPermission( |
| 5008 | android.Manifest.permission.DEVICE_POWER, null); |
| 5009 | |
| 5010 | final int uid = Binder.getCallingUid(); |
| 5011 | final long ident = Binder.clearCallingIdentity(); |
| 5012 | try { |
| 5013 | return forceSuspendInternal(uid); |
| 5014 | } finally { |
| 5015 | Binder.restoreCallingIdentity(ident); |
| 5016 | } |
| 5017 | } |
| 5018 | |
Bryce Lee | 84d6c0f | 2015-03-17 10:43:08 -0700 | [diff] [blame] | 5019 | @Override // Binder call |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5020 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 5021 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5022 | |
| 5023 | final long ident = Binder.clearCallingIdentity(); |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 5024 | |
| 5025 | boolean isDumpProto = false; |
| 5026 | for (String arg : args) { |
| 5027 | if (arg.equals("--proto")) { |
| 5028 | isDumpProto = true; |
| 5029 | } |
| 5030 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5031 | try { |
Netta P | 958d0a5 | 2017-02-07 11:20:55 -0800 | [diff] [blame] | 5032 | if (isDumpProto) { |
| 5033 | dumpProto(fd); |
| 5034 | } else { |
| 5035 | dumpInternal(pw); |
| 5036 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5037 | } finally { |
| 5038 | Binder.restoreCallingIdentity(ident); |
| 5039 | } |
| 5040 | } |
| 5041 | } |
| 5042 | |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 5043 | @VisibleForTesting |
Santos Cordon | 64a6e61 | 2018-08-22 19:27:04 +0100 | [diff] [blame] | 5044 | BinderService getBinderServiceInstance() { |
| 5045 | return mBinderService; |
| 5046 | } |
| 5047 | |
| 5048 | @VisibleForTesting |
| 5049 | LocalService getLocalServiceInstance() { |
| 5050 | return mLocalService; |
| 5051 | } |
| 5052 | |
| 5053 | @VisibleForTesting |
Mark Salyzyn | e65c0c6 | 2017-08-15 07:53:47 -0700 | [diff] [blame] | 5054 | // lastRebootReasonProperty argument to permit testing |
| 5055 | int getLastShutdownReasonInternal(String lastRebootReasonProperty) { |
| 5056 | String line = SystemProperties.get(lastRebootReasonProperty); |
Michael Wright | 9199d0c | 2017-05-10 21:35:13 +0100 | [diff] [blame] | 5057 | if (line == null) { |
| 5058 | return PowerManager.SHUTDOWN_REASON_UNKNOWN; |
| 5059 | } |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 5060 | switch (line) { |
| 5061 | case REASON_SHUTDOWN: |
| 5062 | return PowerManager.SHUTDOWN_REASON_SHUTDOWN; |
| 5063 | case REASON_REBOOT: |
| 5064 | return PowerManager.SHUTDOWN_REASON_REBOOT; |
| 5065 | case REASON_USERREQUESTED: |
| 5066 | return PowerManager.SHUTDOWN_REASON_USER_REQUESTED; |
| 5067 | case REASON_THERMAL_SHUTDOWN: |
| 5068 | return PowerManager.SHUTDOWN_REASON_THERMAL_SHUTDOWN; |
Sudheer Shanka | 292637f | 2017-09-25 10:36:23 -0700 | [diff] [blame] | 5069 | case REASON_LOW_BATTERY: |
| 5070 | return PowerManager.SHUTDOWN_REASON_LOW_BATTERY; |
| 5071 | case REASON_BATTERY_THERMAL_STATE: |
| 5072 | return PowerManager.SHUTDOWN_REASON_BATTERY_THERMAL; |
Salvador Martinez | a6f7b25 | 2017-04-10 10:46:15 -0700 | [diff] [blame] | 5073 | default: |
| 5074 | return PowerManager.SHUTDOWN_REASON_UNKNOWN; |
| 5075 | } |
| 5076 | } |
| 5077 | |
Calin Tataru | a380572 | 2018-08-09 16:41:28 +0200 | [diff] [blame] | 5078 | private int getLastSleepReasonInternal() { |
| 5079 | synchronized (mLock) { |
| 5080 | return mLastSleepReason; |
| 5081 | } |
| 5082 | } |
| 5083 | |
Santos Cordon | 623526b | 2019-04-09 17:02:38 +0100 | [diff] [blame] | 5084 | private PowerManager.WakeData getLastWakeupInternal() { |
| 5085 | synchronized (mLock) { |
| 5086 | return new WakeData(mLastWakeTime, mLastWakeReason); |
| 5087 | } |
| 5088 | } |
| 5089 | |
Santos Cordon | d9701ab | 2019-08-01 18:33:20 +0100 | [diff] [blame] | 5090 | @VisibleForTesting |
| 5091 | final class LocalService extends PowerManagerInternal { |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5092 | @Override |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 5093 | public void setScreenBrightnessOverrideFromWindowManager(int screenBrightness) { |
| 5094 | if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT |
| 5095 | || screenBrightness > PowerManager.BRIGHTNESS_ON) { |
| 5096 | screenBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5097 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 5098 | setScreenBrightnessOverrideFromWindowManagerInternal(screenBrightness); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5099 | } |
| 5100 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5101 | @Override |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 5102 | public void setDozeOverrideFromDreamManager(int screenState, int screenBrightness) { |
| 5103 | switch (screenState) { |
| 5104 | case Display.STATE_UNKNOWN: |
| 5105 | case Display.STATE_OFF: |
| 5106 | case Display.STATE_DOZE: |
| 5107 | case Display.STATE_DOZE_SUSPEND: |
Chris Phoenix | 10a4a64 | 2017-09-25 13:21:00 -0700 | [diff] [blame] | 5108 | case Display.STATE_ON_SUSPEND: |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 5109 | case Display.STATE_ON: |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 5110 | case Display.STATE_VR: |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 5111 | break; |
| 5112 | default: |
| 5113 | screenState = Display.STATE_UNKNOWN; |
| 5114 | break; |
| 5115 | } |
| 5116 | if (screenBrightness < PowerManager.BRIGHTNESS_DEFAULT |
| 5117 | || screenBrightness > PowerManager.BRIGHTNESS_ON) { |
| 5118 | screenBrightness = PowerManager.BRIGHTNESS_DEFAULT; |
| 5119 | } |
| 5120 | setDozeOverrideFromDreamManagerInternal(screenState, screenBrightness); |
| 5121 | } |
| 5122 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5123 | @Override |
Nick Armstrong-Crews | 56ecfcc | 2015-09-07 21:46:50 -0700 | [diff] [blame] | 5124 | public void setUserInactiveOverrideFromWindowManager() { |
| 5125 | setUserInactiveOverrideFromWindowManagerInternal(); |
| 5126 | } |
| 5127 | |
| 5128 | @Override |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5129 | public void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 5130 | setUserActivityTimeoutOverrideFromWindowManagerInternal(timeoutMillis); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | @Override |
Ivan Podogov | 56bc6d0 | 2018-02-26 16:04:24 +0000 | [diff] [blame] | 5134 | public void setDrawWakeLockOverrideFromSidekick(boolean keepState) { |
| 5135 | setDrawWakeLockOverrideFromSidekickInternal(keepState); |
| 5136 | } |
| 5137 | |
| 5138 | @Override |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 5139 | public void setMaximumScreenOffTimeoutFromDeviceAdmin(@UserIdInt int userId, long timeMs) { |
| 5140 | setMaximumScreenOffTimeoutFromDeviceAdminInternal(userId, timeMs); |
Jeff Brown | 5ce1cb2 | 2014-11-06 19:05:33 -0800 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | @Override |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 5144 | public PowerSaveState getLowPowerState(@ServiceType int serviceType) { |
Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 5145 | return mBatterySaverPolicy.getBatterySaverPolicy(serviceType); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | @Override |
| 5149 | public void registerLowPowerModeObserver(LowPowerModeListener listener) { |
Makoto Onuki | 66a7812 | 2017-11-14 15:03:21 -0800 | [diff] [blame] | 5150 | mBatterySaverController.addListener(listener); |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 5151 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 5152 | |
| 5153 | @Override |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 5154 | public boolean setDeviceIdleMode(boolean enabled) { |
| 5155 | return setDeviceIdleModeInternal(enabled); |
| 5156 | } |
| 5157 | |
| 5158 | @Override |
| 5159 | public boolean setLightDeviceIdleMode(boolean enabled) { |
| 5160 | return setLightDeviceIdleModeInternal(enabled); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 5161 | } |
| 5162 | |
| 5163 | @Override |
| 5164 | public void setDeviceIdleWhitelist(int[] appids) { |
| 5165 | setDeviceIdleWhitelistInternal(appids); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 5166 | } |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 5167 | |
| 5168 | @Override |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 5169 | public void setDeviceIdleTempWhitelist(int[] appids) { |
| 5170 | setDeviceIdleTempWhitelistInternal(appids); |
| 5171 | } |
| 5172 | |
| 5173 | @Override |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 5174 | public void startUidChanges() { |
| 5175 | startUidChangesInternal(); |
| 5176 | } |
| 5177 | |
| 5178 | @Override |
| 5179 | public void finishUidChanges() { |
| 5180 | finishUidChangesInternal(); |
| 5181 | } |
| 5182 | |
| 5183 | @Override |
Dianne Hackborn | d23e0d6 | 2015-05-15 16:36:12 -0700 | [diff] [blame] | 5184 | public void updateUidProcState(int uid, int procState) { |
| 5185 | updateUidProcStateInternal(uid, procState); |
| 5186 | } |
| 5187 | |
| 5188 | @Override |
| 5189 | public void uidGone(int uid) { |
| 5190 | uidGoneInternal(uid); |
| 5191 | } |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 5192 | |
| 5193 | @Override |
Dianne Hackborn | d33c7cd | 2016-10-25 17:13:24 -0700 | [diff] [blame] | 5194 | public void uidActive(int uid) { |
| 5195 | uidActiveInternal(uid); |
| 5196 | } |
| 5197 | |
| 5198 | @Override |
| 5199 | public void uidIdle(int uid) { |
| 5200 | uidIdleInternal(uid); |
| 5201 | } |
| 5202 | |
| 5203 | @Override |
Michael Wright | a4d22d7 | 2015-09-16 23:19:26 +0100 | [diff] [blame] | 5204 | public void powerHint(int hintId, int data) { |
| 5205 | powerHintInternal(hintId, data); |
| 5206 | } |
Alex Kershaw | 2418ea9 | 2018-10-19 17:17:49 +0100 | [diff] [blame] | 5207 | |
| 5208 | @Override |
| 5209 | public boolean wasDeviceIdleFor(long ms) { |
| 5210 | return wasDeviceIdleForInternal(ms); |
| 5211 | } |
Santos Cordon | 623526b | 2019-04-09 17:02:38 +0100 | [diff] [blame] | 5212 | |
| 5213 | @Override |
| 5214 | public WakeData getLastWakeup() { |
| 5215 | return getLastWakeupInternal(); |
| 5216 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 5217 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5218 | } |