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