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