Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 21 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 22 | import android.app.AlarmManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
| 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageManager; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 31 | import android.database.ContentObserver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 32 | import android.hardware.Sensor; |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 33 | import android.hardware.SensorEvent; |
| 34 | import android.hardware.SensorEventListener; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 35 | import android.hardware.SensorManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 36 | import android.hardware.TriggerEvent; |
| 37 | import android.hardware.TriggerEventListener; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 38 | import android.location.Location; |
| 39 | import android.location.LocationListener; |
| 40 | import android.location.LocationManager; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 41 | import android.location.LocationRequest; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 42 | import android.net.ConnectivityManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 43 | import android.net.INetworkPolicyManager; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 44 | import android.net.NetworkInfo; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 45 | import android.net.Uri; |
Robin Lee | 204cb22 | 2018-12-07 15:17:44 +0100 | [diff] [blame] | 46 | import android.os.BatteryManager; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 47 | import android.os.BatteryStats; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 48 | import android.os.Binder; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 49 | import android.os.Bundle; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 50 | import android.os.Environment; |
| 51 | import android.os.FileUtils; |
| 52 | import android.os.Handler; |
| 53 | import android.os.IDeviceIdleController; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 54 | import android.os.IMaintenanceActivityListener; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 55 | import android.os.Looper; |
| 56 | import android.os.Message; |
| 57 | import android.os.PowerManager; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 58 | import android.os.PowerManager.ServiceType; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 59 | import android.os.PowerManagerInternal; |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 60 | import android.os.Process; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 61 | import android.os.RemoteCallbackList; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 62 | import android.os.RemoteException; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 63 | import android.os.ResultReceiver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 64 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 65 | import android.os.ShellCallback; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 66 | import android.os.ShellCommand; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 67 | import android.os.SystemClock; |
| 68 | import android.os.UserHandle; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 69 | import android.provider.Settings; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 70 | import android.util.ArrayMap; |
| 71 | import android.util.ArraySet; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 72 | import android.util.KeyValueListParser; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 73 | import android.util.MutableLong; |
| 74 | import android.util.Pair; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 75 | import android.util.Slog; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 76 | import android.util.SparseArray; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 77 | import android.util.SparseBooleanArray; |
| 78 | import android.util.TimeUtils; |
| 79 | import android.util.Xml; |
Amith Yamasani | 520d8f2 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 80 | |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 81 | import com.android.internal.annotations.GuardedBy; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 82 | import com.android.internal.annotations.VisibleForTesting; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 83 | import com.android.internal.app.IBatteryStats; |
| 84 | import com.android.internal.os.AtomicFile; |
| 85 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 86 | import com.android.internal.util.DumpUtils; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 87 | import com.android.internal.util.FastXmlSerializer; |
| 88 | import com.android.internal.util.XmlUtils; |
| 89 | import com.android.server.am.BatteryStatsService; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 90 | import com.android.server.deviceidle.ConstraintController; |
| 91 | import com.android.server.deviceidle.DeviceIdleConstraintTracker; |
| 92 | import com.android.server.deviceidle.IDeviceIdleConstraint; |
| 93 | import com.android.server.deviceidle.TvConstraintController; |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 94 | import com.android.server.net.NetworkPolicyManagerInternal; |
Wale Ogunwale | 9e4f3e0 | 2018-05-17 09:35:39 -0700 | [diff] [blame] | 95 | import com.android.server.wm.ActivityTaskManagerInternal; |
Amith Yamasani | 520d8f2 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 96 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 97 | import org.xmlpull.v1.XmlPullParser; |
| 98 | import org.xmlpull.v1.XmlPullParserException; |
| 99 | import org.xmlpull.v1.XmlSerializer; |
| 100 | |
| 101 | import java.io.ByteArrayOutputStream; |
| 102 | import java.io.File; |
| 103 | import java.io.FileDescriptor; |
| 104 | import java.io.FileInputStream; |
| 105 | import java.io.FileNotFoundException; |
| 106 | import java.io.FileOutputStream; |
| 107 | import java.io.IOException; |
| 108 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 109 | import java.nio.charset.StandardCharsets; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 110 | import java.util.Arrays; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 111 | import java.util.stream.Collectors; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Keeps track of device idleness and drives low power mode based on that. |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 115 | * |
| 116 | * Test: atest com.android.server.DeviceIdleControllerTest |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 117 | * |
Kweku Adams | 3ba574f | 2019-03-19 12:45:55 -0700 | [diff] [blame] | 118 | * Current idling state machine (as of Android Q). This can be visualized using Graphviz: |
| 119 | <pre> |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 120 | |
| 121 | digraph { |
| 122 | subgraph deep { |
| 123 | label="deep"; |
| 124 | |
| 125 | STATE_ACTIVE [label="STATE_ACTIVE\nScreen on OR Charging OR Alarm going off soon"] |
| 126 | STATE_INACTIVE [label="STATE_INACTIVE\nScreen off AND Not charging"] |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 127 | STATE_QUICK_DOZE_DELAY [ |
| 128 | label="STATE_QUICK_DOZE_DELAY\n" |
| 129 | + "Screen off AND Not charging\n" |
| 130 | + "Location, motion detection, and significant motion monitoring turned off" |
| 131 | ] |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 132 | STATE_IDLE_PENDING [ |
| 133 | label="STATE_IDLE_PENDING\nSignificant motion monitoring turned on" |
| 134 | ] |
| 135 | STATE_SENSING [label="STATE_SENSING\nMonitoring for ANY motion"] |
| 136 | STATE_LOCATING [ |
| 137 | label="STATE_LOCATING\nRequesting location, motion monitoring still on" |
| 138 | ] |
| 139 | STATE_IDLE [ |
| 140 | label="STATE_IDLE\nLocation and motion detection turned off\n" |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 141 | + "Significant motion monitoring state unchanged" |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 142 | ] |
| 143 | STATE_IDLE_MAINTENANCE [label="STATE_IDLE_MAINTENANCE\n"] |
| 144 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 145 | STATE_ACTIVE -> STATE_INACTIVE [ |
| 146 | label="becomeInactiveIfAppropriateLocked() AND Quick Doze not enabled" |
| 147 | ] |
| 148 | STATE_ACTIVE -> STATE_QUICK_DOZE_DELAY [ |
| 149 | label="becomeInactiveIfAppropriateLocked() AND Quick Doze enabled" |
| 150 | ] |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 151 | |
| 152 | STATE_INACTIVE -> STATE_ACTIVE [ |
| 153 | label="handleMotionDetectedLocked(), becomeActiveLocked()" |
| 154 | ] |
| 155 | STATE_INACTIVE -> STATE_IDLE_PENDING [label="stepIdleStateLocked()"] |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 156 | STATE_INACTIVE -> STATE_QUICK_DOZE_DELAY [ |
| 157 | label="becomeInactiveIfAppropriateLocked() AND Quick Doze enabled" |
| 158 | ] |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 159 | |
| 160 | STATE_IDLE_PENDING -> STATE_ACTIVE [ |
| 161 | label="handleMotionDetectedLocked(), becomeActiveLocked()" |
| 162 | ] |
| 163 | STATE_IDLE_PENDING -> STATE_SENSING [label="stepIdleStateLocked()"] |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 164 | STATE_IDLE_PENDING -> STATE_QUICK_DOZE_DELAY [ |
| 165 | label="becomeInactiveIfAppropriateLocked() AND Quick Doze enabled" |
| 166 | ] |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 167 | |
| 168 | STATE_SENSING -> STATE_ACTIVE [ |
| 169 | label="handleMotionDetectedLocked(), becomeActiveLocked()" |
| 170 | ] |
| 171 | STATE_SENSING -> STATE_LOCATING [label="stepIdleStateLocked()"] |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 172 | STATE_SENSING -> STATE_QUICK_DOZE_DELAY [ |
| 173 | label="becomeInactiveIfAppropriateLocked() AND Quick Doze enabled" |
| 174 | ] |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 175 | STATE_SENSING -> STATE_IDLE [ |
| 176 | label="stepIdleStateLocked()\n" |
| 177 | + "No Location Manager OR (no Network provider AND no GPS provider)" |
| 178 | ] |
| 179 | |
| 180 | STATE_LOCATING -> STATE_ACTIVE [ |
| 181 | label="handleMotionDetectedLocked(), becomeActiveLocked()" |
| 182 | ] |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 183 | STATE_LOCATING -> STATE_QUICK_DOZE_DELAY [ |
| 184 | label="becomeInactiveIfAppropriateLocked() AND Quick Doze enabled" |
| 185 | ] |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 186 | STATE_LOCATING -> STATE_IDLE [label="stepIdleStateLocked()"] |
| 187 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 188 | STATE_QUICK_DOZE_DELAY -> STATE_ACTIVE [ |
| 189 | label="handleMotionDetectedLocked(), becomeActiveLocked()" |
| 190 | ] |
| 191 | STATE_QUICK_DOZE_DELAY -> STATE_IDLE [label="stepIdleStateLocked()"] |
| 192 | |
Kweku Adams | f596cfc | 2018-10-04 15:15:22 -0700 | [diff] [blame] | 193 | STATE_IDLE -> STATE_ACTIVE [label="handleMotionDetectedLocked(), becomeActiveLocked()"] |
| 194 | STATE_IDLE -> STATE_IDLE_MAINTENANCE [label="stepIdleStateLocked()"] |
| 195 | |
| 196 | STATE_IDLE_MAINTENANCE -> STATE_ACTIVE [ |
| 197 | label="handleMotionDetectedLocked(), becomeActiveLocked()" |
| 198 | ] |
| 199 | STATE_IDLE_MAINTENANCE -> STATE_IDLE [ |
| 200 | label="stepIdleStateLocked(), exitMaintenanceEarlyIfNeededLocked()" |
| 201 | ] |
| 202 | } |
| 203 | |
| 204 | subgraph light { |
| 205 | label="light" |
| 206 | |
| 207 | LIGHT_STATE_ACTIVE [ |
| 208 | label="LIGHT_STATE_ACTIVE\nScreen on OR Charging OR Alarm going off soon" |
| 209 | ] |
| 210 | LIGHT_STATE_INACTIVE [label="LIGHT_STATE_INACTIVE\nScreen off AND Not charging"] |
| 211 | LIGHT_STATE_PRE_IDLE [ |
| 212 | label="LIGHT_STATE_PRE_IDLE\n" |
| 213 | + "Delay going into LIGHT_STATE_IDLE due to some running jobs or alarms" |
| 214 | ] |
| 215 | LIGHT_STATE_IDLE [label="LIGHT_STATE_IDLE\n"] |
| 216 | LIGHT_STATE_WAITING_FOR_NETWORK [ |
| 217 | label="LIGHT_STATE_WAITING_FOR_NETWORK\n" |
| 218 | + "Coming out of LIGHT_STATE_IDLE, waiting for network" |
| 219 | ] |
| 220 | LIGHT_STATE_IDLE_MAINTENANCE [label="LIGHT_STATE_IDLE_MAINTENANCE\n"] |
| 221 | LIGHT_STATE_OVERRIDE [ |
| 222 | label="LIGHT_STATE_OVERRIDE\nDevice in deep doze, light no longer changing states" |
| 223 | ] |
| 224 | |
| 225 | LIGHT_STATE_ACTIVE -> LIGHT_STATE_INACTIVE [ |
| 226 | label="becomeInactiveIfAppropriateLocked()" |
| 227 | ] |
| 228 | LIGHT_STATE_ACTIVE -> LIGHT_STATE_OVERRIDE [label="deep goes to STATE_IDLE"] |
| 229 | |
| 230 | LIGHT_STATE_INACTIVE -> LIGHT_STATE_ACTIVE [label="becomeActiveLocked()"] |
| 231 | LIGHT_STATE_INACTIVE -> LIGHT_STATE_PRE_IDLE [label="active jobs"] |
| 232 | LIGHT_STATE_INACTIVE -> LIGHT_STATE_IDLE [label="no active jobs"] |
| 233 | LIGHT_STATE_INACTIVE -> LIGHT_STATE_OVERRIDE [label="deep goes to STATE_IDLE"] |
| 234 | |
| 235 | LIGHT_STATE_PRE_IDLE -> LIGHT_STATE_ACTIVE [label="becomeActiveLocked()"] |
| 236 | LIGHT_STATE_PRE_IDLE -> LIGHT_STATE_IDLE [ |
| 237 | label="stepLightIdleStateLocked(), exitMaintenanceEarlyIfNeededLocked()" |
| 238 | ] |
| 239 | LIGHT_STATE_PRE_IDLE -> LIGHT_STATE_OVERRIDE [label="deep goes to STATE_IDLE"] |
| 240 | |
| 241 | LIGHT_STATE_IDLE -> LIGHT_STATE_ACTIVE [label="becomeActiveLocked()"] |
| 242 | LIGHT_STATE_IDLE -> LIGHT_STATE_WAITING_FOR_NETWORK [label="no network"] |
| 243 | LIGHT_STATE_IDLE -> LIGHT_STATE_IDLE_MAINTENANCE |
| 244 | LIGHT_STATE_IDLE -> LIGHT_STATE_OVERRIDE [label="deep goes to STATE_IDLE"] |
| 245 | |
| 246 | LIGHT_STATE_WAITING_FOR_NETWORK -> LIGHT_STATE_ACTIVE [label="becomeActiveLocked()"] |
| 247 | LIGHT_STATE_WAITING_FOR_NETWORK -> LIGHT_STATE_IDLE_MAINTENANCE |
| 248 | LIGHT_STATE_WAITING_FOR_NETWORK -> LIGHT_STATE_OVERRIDE [ |
| 249 | label="deep goes to STATE_IDLE" |
| 250 | ] |
| 251 | |
| 252 | LIGHT_STATE_IDLE_MAINTENANCE -> LIGHT_STATE_ACTIVE [label="becomeActiveLocked()"] |
| 253 | LIGHT_STATE_IDLE_MAINTENANCE -> LIGHT_STATE_IDLE [ |
| 254 | label="stepLightIdleStateLocked(), exitMaintenanceEarlyIfNeededLocked()" |
| 255 | ] |
| 256 | LIGHT_STATE_IDLE_MAINTENANCE -> LIGHT_STATE_OVERRIDE [label="deep goes to STATE_IDLE"] |
| 257 | |
| 258 | LIGHT_STATE_OVERRIDE -> LIGHT_STATE_ACTIVE [ |
| 259 | label="handleMotionDetectedLocked(), becomeActiveLocked()" |
| 260 | ] |
| 261 | } |
| 262 | } |
Kweku Adams | 3ba574f | 2019-03-19 12:45:55 -0700 | [diff] [blame] | 263 | </pre> |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 264 | */ |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 265 | public class DeviceIdleController extends SystemService |
| 266 | implements AnyMotionDetector.DeviceIdleCallback { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 267 | private static final String TAG = "DeviceIdleController"; |
| 268 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 269 | private static final boolean DEBUG = false; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 270 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 271 | private static final boolean COMPRESS_TIME = false; |
Amith Yamasani | 520d8f2 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 272 | |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 273 | private static final int EVENT_BUFFER_SIZE = 100; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 274 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 275 | private AlarmManager mAlarmManager; |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 276 | private AlarmManagerInternal mLocalAlarmManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 277 | private IBatteryStats mBatteryStats; |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 278 | private ActivityManagerInternal mLocalActivityManager; |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 279 | private ActivityTaskManagerInternal mLocalActivityTaskManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 280 | private PowerManagerInternal mLocalPowerManager; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 281 | private PowerManager mPowerManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 282 | private INetworkPolicyManager mNetworkPolicyManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 283 | private SensorManager mSensorManager; |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 284 | private final boolean mUseMotionSensor; |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 285 | private Sensor mMotionSensor; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 286 | private LocationRequest mLocationRequest; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 287 | private Intent mIdleIntent; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 288 | private Intent mLightIdleIntent; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 289 | private AnyMotionDetector mAnyMotionDetector; |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 290 | private final AppStateTracker mAppStateTracker; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 291 | private boolean mLightEnabled; |
| 292 | private boolean mDeepEnabled; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 293 | private boolean mQuickDozeActivated; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 294 | private boolean mForceIdle; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 295 | private boolean mNetworkConnected; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 296 | private boolean mScreenOn; |
| 297 | private boolean mCharging; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 298 | private boolean mNotMoving; |
| 299 | private boolean mLocating; |
| 300 | private boolean mLocated; |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 301 | private boolean mHasGps; |
| 302 | private boolean mHasNetworkLocation; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 303 | private Location mLastGenericLocation; |
| 304 | private Location mLastGpsLocation; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 305 | // Current locked state of the screen |
| 306 | private boolean mScreenLocked; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 307 | private int mNumBlockingConstraints = 0; |
| 308 | |
| 309 | /** |
| 310 | * Constraints are the "handbrakes" that stop the device from moving into a lower state until |
| 311 | * every one is released at the same time. |
| 312 | * |
| 313 | * @see #registerDeviceIdleConstraintInternal(IDeviceIdleConstraint, String, int) |
| 314 | */ |
| 315 | private final ArrayMap<IDeviceIdleConstraint, DeviceIdleConstraintTracker> |
| 316 | mConstraints = new ArrayMap<>(); |
| 317 | private ConstraintController mConstraintController; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 318 | |
| 319 | /** Device is currently active. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 320 | @VisibleForTesting |
| 321 | static final int STATE_ACTIVE = 0; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 322 | /** Device is inactive (screen off, no motion) and we are waiting to for idle. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 323 | @VisibleForTesting |
| 324 | static final int STATE_INACTIVE = 1; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 325 | /** Device is past the initial inactive period, and waiting for the next idle period. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 326 | @VisibleForTesting |
| 327 | static final int STATE_IDLE_PENDING = 2; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 328 | /** Device is currently sensing motion. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 329 | @VisibleForTesting |
| 330 | static final int STATE_SENSING = 3; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 331 | /** Device is currently finding location (and may still be sensing). */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 332 | @VisibleForTesting |
| 333 | static final int STATE_LOCATING = 4; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 334 | /** Device is in the idle state, trying to stay asleep as much as possible. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 335 | @VisibleForTesting |
| 336 | static final int STATE_IDLE = 5; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 337 | /** Device is in the idle state, but temporarily out of idle to do regular maintenance. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 338 | @VisibleForTesting |
| 339 | static final int STATE_IDLE_MAINTENANCE = 6; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 340 | /** |
| 341 | * Device is inactive and should go straight into idle (foregoing motion and location |
| 342 | * monitoring), but allow some time for current work to complete first. |
| 343 | */ |
| 344 | @VisibleForTesting |
| 345 | static final int STATE_QUICK_DOZE_DELAY = 7; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 346 | |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 347 | private static final int ACTIVE_REASON_UNKNOWN = 0; |
| 348 | private static final int ACTIVE_REASON_MOTION = 1; |
| 349 | private static final int ACTIVE_REASON_SCREEN = 2; |
| 350 | private static final int ACTIVE_REASON_CHARGING = 3; |
| 351 | private static final int ACTIVE_REASON_UNLOCKED = 4; |
| 352 | private static final int ACTIVE_REASON_FROM_BINDER_CALL = 5; |
| 353 | private static final int ACTIVE_REASON_FORCED = 6; |
| 354 | private static final int ACTIVE_REASON_ALARM = 7; |
| 355 | @VisibleForTesting |
| 356 | static final int SET_IDLE_FACTOR_RESULT_UNINIT = -1; |
| 357 | @VisibleForTesting |
| 358 | static final int SET_IDLE_FACTOR_RESULT_IGNORED = 0; |
| 359 | @VisibleForTesting |
| 360 | static final int SET_IDLE_FACTOR_RESULT_OK = 1; |
| 361 | @VisibleForTesting |
| 362 | static final int SET_IDLE_FACTOR_RESULT_NOT_SUPPORT = 2; |
| 363 | @VisibleForTesting |
| 364 | static final int SET_IDLE_FACTOR_RESULT_INVALID = 3; |
| 365 | @VisibleForTesting |
| 366 | static final long MIN_STATE_STEP_ALARM_CHANGE = 60 * 1000; |
| 367 | @VisibleForTesting |
| 368 | static final float MIN_PRE_IDLE_FACTOR_CHANGE = 0.05f; |
| 369 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 370 | @VisibleForTesting |
| 371 | static String stateToString(int state) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 372 | switch (state) { |
| 373 | case STATE_ACTIVE: return "ACTIVE"; |
| 374 | case STATE_INACTIVE: return "INACTIVE"; |
| 375 | case STATE_IDLE_PENDING: return "IDLE_PENDING"; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 376 | case STATE_SENSING: return "SENSING"; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 377 | case STATE_LOCATING: return "LOCATING"; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 378 | case STATE_IDLE: return "IDLE"; |
| 379 | case STATE_IDLE_MAINTENANCE: return "IDLE_MAINTENANCE"; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 380 | case STATE_QUICK_DOZE_DELAY: return "QUICK_DOZE_DELAY"; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 381 | default: return Integer.toString(state); |
| 382 | } |
| 383 | } |
| 384 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 385 | /** Device is currently active. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 386 | @VisibleForTesting |
| 387 | static final int LIGHT_STATE_ACTIVE = 0; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 388 | /** Device is inactive (screen off) and we are waiting to for the first light idle. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 389 | @VisibleForTesting |
| 390 | static final int LIGHT_STATE_INACTIVE = 1; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 391 | /** Device is about to go idle for the first time, wait for current work to complete. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 392 | @VisibleForTesting |
| 393 | static final int LIGHT_STATE_PRE_IDLE = 3; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 394 | /** Device is in the light idle state, trying to stay asleep as much as possible. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 395 | @VisibleForTesting |
| 396 | static final int LIGHT_STATE_IDLE = 4; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 397 | /** Device is in the light idle state, we want to go in to idle maintenance but are |
| 398 | * waiting for network connectivity before doing so. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 399 | @VisibleForTesting |
| 400 | static final int LIGHT_STATE_WAITING_FOR_NETWORK = 5; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 401 | /** Device is in the light idle state, but temporarily out of idle to do regular maintenance. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 402 | @VisibleForTesting |
| 403 | static final int LIGHT_STATE_IDLE_MAINTENANCE = 6; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 404 | /** Device light idle state is overriden, now applying deep doze state. */ |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 405 | @VisibleForTesting |
| 406 | static final int LIGHT_STATE_OVERRIDE = 7; |
| 407 | |
| 408 | @VisibleForTesting |
| 409 | static String lightStateToString(int state) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 410 | switch (state) { |
| 411 | case LIGHT_STATE_ACTIVE: return "ACTIVE"; |
| 412 | case LIGHT_STATE_INACTIVE: return "INACTIVE"; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 413 | case LIGHT_STATE_PRE_IDLE: return "PRE_IDLE"; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 414 | case LIGHT_STATE_IDLE: return "IDLE"; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 415 | case LIGHT_STATE_WAITING_FOR_NETWORK: return "WAITING_FOR_NETWORK"; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 416 | case LIGHT_STATE_IDLE_MAINTENANCE: return "IDLE_MAINTENANCE"; |
| 417 | case LIGHT_STATE_OVERRIDE: return "OVERRIDE"; |
| 418 | default: return Integer.toString(state); |
| 419 | } |
| 420 | } |
| 421 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 422 | private int mState; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 423 | private int mLightState; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 424 | |
| 425 | private long mInactiveTimeout; |
| 426 | private long mNextAlarmTime; |
| 427 | private long mNextIdlePendingDelay; |
| 428 | private long mNextIdleDelay; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 429 | private long mNextLightIdleDelay; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 430 | private long mNextLightAlarmTime; |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 431 | private long mNextSensingTimeoutAlarmTime; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 432 | private long mCurIdleBudget; |
| 433 | private long mMaintenanceStartTime; |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 434 | private long mIdleStartTime; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 435 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 436 | private int mActiveIdleOpCount; |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 437 | private PowerManager.WakeLock mActiveIdleWakeLock; // held when there are operations in progress |
| 438 | private PowerManager.WakeLock mGoingIdleWakeLock; // held when we are going idle so hardware |
| 439 | // (especially NetworkPolicyManager) can shut |
| 440 | // down. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 441 | private boolean mJobsActive; |
| 442 | private boolean mAlarmsActive; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 443 | private boolean mReportedMaintenanceActivity; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 444 | |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 445 | /* Factor to apply to INACTIVE_TIMEOUT and IDLE_AFTER_INACTIVE_TIMEOUT in order to enter |
| 446 | * STATE_IDLE faster or slower. Don't apply this to SENSING_TIMEOUT or LOCATING_TIMEOUT because: |
| 447 | * - Both of them are shorter |
| 448 | * - Device sensor might take time be to become be stabilized |
| 449 | * Also don't apply the factor if the device is in motion because device motion provides a |
| 450 | * stronger signal than a prediction algorithm. |
| 451 | */ |
| 452 | private float mPreIdleFactor; |
| 453 | private float mLastPreIdleFactor; |
| 454 | private int mActiveReason; |
| 455 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 456 | public final AtomicFile mConfigFile; |
| 457 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 458 | private final RemoteCallbackList<IMaintenanceActivityListener> mMaintenanceActivityListeners = |
| 459 | new RemoteCallbackList<IMaintenanceActivityListener>(); |
| 460 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 461 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 462 | * Package names the system has white-listed to opt out of power save restrictions, |
| 463 | * except for device idle mode. |
| 464 | */ |
| 465 | private final ArrayMap<String, Integer> mPowerSaveWhitelistAppsExceptIdle = new ArrayMap<>(); |
| 466 | |
| 467 | /** |
Sudheer Shanka | 3f4d770 | 2017-04-28 17:38:03 -0700 | [diff] [blame] | 468 | * Package names the user has white-listed using commandline option to opt out of |
| 469 | * power save restrictions, except for device idle mode. |
| 470 | */ |
| 471 | private final ArraySet<String> mPowerSaveWhitelistUserAppsExceptIdle = new ArraySet<>(); |
| 472 | |
| 473 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 474 | * Package names the system has white-listed to opt out of power save restrictions for |
| 475 | * all modes. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 476 | */ |
| 477 | private final ArrayMap<String, Integer> mPowerSaveWhitelistApps = new ArrayMap<>(); |
| 478 | |
| 479 | /** |
| 480 | * Package names the user has white-listed to opt out of power save restrictions. |
| 481 | */ |
| 482 | private final ArrayMap<String, Integer> mPowerSaveWhitelistUserApps = new ArrayMap<>(); |
| 483 | |
| 484 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 485 | * App IDs of built-in system apps that have been white-listed except for idle modes. |
| 486 | */ |
| 487 | private final SparseBooleanArray mPowerSaveWhitelistSystemAppIdsExceptIdle |
| 488 | = new SparseBooleanArray(); |
| 489 | |
| 490 | /** |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 491 | * App IDs of built-in system apps that have been white-listed. |
| 492 | */ |
| 493 | private final SparseBooleanArray mPowerSaveWhitelistSystemAppIds = new SparseBooleanArray(); |
| 494 | |
| 495 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 496 | * App IDs that have been white-listed to opt out of power save restrictions, except |
| 497 | * for device idle modes. |
| 498 | */ |
| 499 | private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray(); |
| 500 | |
| 501 | /** |
| 502 | * Current app IDs that are in the complete power save white list, but shouldn't be |
| 503 | * excluded from idle modes. This array can be shared with others because it will not be |
| 504 | * modified once set. |
| 505 | */ |
| 506 | private int[] mPowerSaveWhitelistExceptIdleAppIdArray = new int[0]; |
| 507 | |
| 508 | /** |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 509 | * App IDs that have been white-listed to opt out of power save restrictions. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 510 | */ |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 511 | private final SparseBooleanArray mPowerSaveWhitelistAllAppIds = new SparseBooleanArray(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 512 | |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 513 | /** |
| 514 | * Current app IDs that are in the complete power save white list. This array can |
| 515 | * be shared with others because it will not be modified once set. |
| 516 | */ |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 517 | private int[] mPowerSaveWhitelistAllAppIdArray = new int[0]; |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 518 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 519 | /** |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 520 | * App IDs that have been white-listed by the user to opt out of power save restrictions. |
| 521 | */ |
| 522 | private final SparseBooleanArray mPowerSaveWhitelistUserAppIds = new SparseBooleanArray(); |
| 523 | |
| 524 | /** |
| 525 | * Current app IDs that are in the user power save white list. This array can |
| 526 | * be shared with others because it will not be modified once set. |
| 527 | */ |
| 528 | private int[] mPowerSaveWhitelistUserAppIdArray = new int[0]; |
| 529 | |
| 530 | /** |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 531 | * List of end times for UIDs that are temporarily marked as being allowed to access |
| 532 | * the network and acquire wakelocks. Times are in milliseconds. |
| 533 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 534 | private final SparseArray<Pair<MutableLong, String>> mTempWhitelistAppIdEndTimes |
| 535 | = new SparseArray<>(); |
| 536 | |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 537 | private NetworkPolicyManagerInternal mNetworkPolicyManagerInternal; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 538 | |
| 539 | /** |
| 540 | * Current app IDs of temporarily whitelist apps for high-priority messages. |
| 541 | */ |
| 542 | private int[] mTempWhitelistAppIdArray = new int[0]; |
| 543 | |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 544 | /** |
| 545 | * Apps in the system whitelist that have been taken out (probably because the user wanted to). |
| 546 | * They can be restored back by calling restoreAppToSystemWhitelist(String). |
| 547 | */ |
| 548 | private ArrayMap<String, Integer> mRemovedFromSystemWhitelistApps = new ArrayMap<>(); |
| 549 | |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 550 | private static final int EVENT_NULL = 0; |
| 551 | private static final int EVENT_NORMAL = 1; |
| 552 | private static final int EVENT_LIGHT_IDLE = 2; |
| 553 | private static final int EVENT_LIGHT_MAINTENANCE = 3; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 554 | private static final int EVENT_DEEP_IDLE = 4; |
| 555 | private static final int EVENT_DEEP_MAINTENANCE = 5; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 556 | |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 557 | private final int[] mEventCmds = new int[EVENT_BUFFER_SIZE]; |
| 558 | private final long[] mEventTimes = new long[EVENT_BUFFER_SIZE]; |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 559 | private final String[] mEventReasons = new String[EVENT_BUFFER_SIZE]; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 560 | |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 561 | private void addEvent(int cmd, String reason) { |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 562 | if (mEventCmds[0] != cmd) { |
| 563 | System.arraycopy(mEventCmds, 0, mEventCmds, 1, EVENT_BUFFER_SIZE - 1); |
| 564 | System.arraycopy(mEventTimes, 0, mEventTimes, 1, EVENT_BUFFER_SIZE - 1); |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 565 | System.arraycopy(mEventReasons, 0, mEventReasons, 1, EVENT_BUFFER_SIZE - 1); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 566 | mEventCmds[0] = cmd; |
| 567 | mEventTimes[0] = SystemClock.elapsedRealtime(); |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 568 | mEventReasons[0] = reason; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 572 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 573 | @Override public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 574 | switch (intent.getAction()) { |
| 575 | case ConnectivityManager.CONNECTIVITY_ACTION: { |
Dianne Hackborn | 4cb96ca | 2016-05-17 13:55:29 -0700 | [diff] [blame] | 576 | updateConnectivityState(intent); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 577 | } break; |
| 578 | case Intent.ACTION_BATTERY_CHANGED: { |
Robin Lee | 204cb22 | 2018-12-07 15:17:44 +0100 | [diff] [blame] | 579 | boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); |
| 580 | boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 581 | synchronized (DeviceIdleController.this) { |
Robin Lee | 204cb22 | 2018-12-07 15:17:44 +0100 | [diff] [blame] | 582 | updateChargingLocked(present && plugged); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 583 | } |
| 584 | } break; |
| 585 | case Intent.ACTION_PACKAGE_REMOVED: { |
| 586 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 587 | Uri data = intent.getData(); |
| 588 | String ssp; |
| 589 | if (data != null && (ssp = data.getSchemeSpecificPart()) != null) { |
| 590 | removePowerSaveWhitelistAppInternal(ssp); |
| 591 | } |
| 592 | } |
| 593 | } break; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | }; |
| 597 | |
| 598 | private final AlarmManager.OnAlarmListener mLightAlarmListener |
| 599 | = new AlarmManager.OnAlarmListener() { |
| 600 | @Override |
| 601 | public void onAlarm() { |
| 602 | synchronized (DeviceIdleController.this) { |
| 603 | stepLightIdleStateLocked("s:alarm"); |
| 604 | } |
| 605 | } |
| 606 | }; |
| 607 | |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 608 | private final AlarmManager.OnAlarmListener mSensingTimeoutAlarmListener |
| 609 | = new AlarmManager.OnAlarmListener() { |
| 610 | @Override |
| 611 | public void onAlarm() { |
| 612 | if (mState == STATE_SENSING) { |
| 613 | synchronized (DeviceIdleController.this) { |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 614 | // Restart the device idle progression in case the device moved but the screen |
| 615 | // didn't turn on. |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 616 | becomeInactiveIfAppropriateLocked(); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | }; |
| 621 | |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 622 | @VisibleForTesting |
| 623 | final AlarmManager.OnAlarmListener mDeepAlarmListener |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 624 | = new AlarmManager.OnAlarmListener() { |
| 625 | @Override |
| 626 | public void onAlarm() { |
| 627 | synchronized (DeviceIdleController.this) { |
| 628 | stepIdleStateLocked("s:alarm"); |
| 629 | } |
| 630 | } |
| 631 | }; |
| 632 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 633 | private final BroadcastReceiver mIdleStartedDoneReceiver = new BroadcastReceiver() { |
| 634 | @Override public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 635 | // When coming out of a deep idle, we will add in some delay before we allow |
| 636 | // the system to settle down and finish the maintenance window. This is |
| 637 | // to give a chance for any pending work to be scheduled. |
| 638 | if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())) { |
| 639 | mHandler.sendEmptyMessageDelayed(MSG_FINISH_IDLE_OP, |
| 640 | mConstants.MIN_DEEP_MAINTENANCE_TIME); |
| 641 | } else { |
| 642 | mHandler.sendEmptyMessageDelayed(MSG_FINISH_IDLE_OP, |
| 643 | mConstants.MIN_LIGHT_MAINTENANCE_TIME); |
| 644 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 645 | } |
| 646 | }; |
| 647 | |
Dianne Hackborn | 3dba8ce | 2017-09-01 17:07:04 -0700 | [diff] [blame] | 648 | private final BroadcastReceiver mInteractivityReceiver = new BroadcastReceiver() { |
| 649 | @Override |
| 650 | public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 9b5ebc9 | 2017-09-08 13:45:35 -0700 | [diff] [blame] | 651 | synchronized (DeviceIdleController.this) { |
| 652 | updateInteractivityLocked(); |
| 653 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 654 | } |
| 655 | }; |
| 656 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 657 | @VisibleForTesting |
| 658 | final class MotionListener extends TriggerEventListener |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 659 | implements SensorEventListener { |
| 660 | |
| 661 | boolean active = false; |
| 662 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 663 | public boolean isActive() { |
| 664 | return active; |
| 665 | } |
| 666 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 667 | @Override |
| 668 | public void onTrigger(TriggerEvent event) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 669 | synchronized (DeviceIdleController.this) { |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 670 | active = false; |
| 671 | motionLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 674 | |
| 675 | @Override |
| 676 | public void onSensorChanged(SensorEvent event) { |
| 677 | synchronized (DeviceIdleController.this) { |
| 678 | mSensorManager.unregisterListener(this, mMotionSensor); |
| 679 | active = false; |
| 680 | motionLocked(); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public void onAccuracyChanged(Sensor sensor, int accuracy) {} |
| 686 | |
| 687 | public boolean registerLocked() { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 688 | boolean success; |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 689 | if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) { |
| 690 | success = mSensorManager.requestTriggerSensor(mMotionListener, mMotionSensor); |
| 691 | } else { |
| 692 | success = mSensorManager.registerListener( |
| 693 | mMotionListener, mMotionSensor, SensorManager.SENSOR_DELAY_NORMAL); |
| 694 | } |
| 695 | if (success) { |
| 696 | active = true; |
| 697 | } else { |
| 698 | Slog.e(TAG, "Unable to register for " + mMotionSensor); |
| 699 | } |
| 700 | return success; |
| 701 | } |
| 702 | |
| 703 | public void unregisterLocked() { |
| 704 | if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) { |
| 705 | mSensorManager.cancelTriggerSensor(mMotionListener, mMotionSensor); |
| 706 | } else { |
| 707 | mSensorManager.unregisterListener(mMotionListener); |
| 708 | } |
| 709 | active = false; |
| 710 | } |
| 711 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 712 | @VisibleForTesting final MotionListener mMotionListener = new MotionListener(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 713 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 714 | private final LocationListener mGenericLocationListener = new LocationListener() { |
| 715 | @Override |
| 716 | public void onLocationChanged(Location location) { |
| 717 | synchronized (DeviceIdleController.this) { |
| 718 | receivedGenericLocationLocked(location); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | @Override |
| 723 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public void onProviderEnabled(String provider) { |
| 728 | } |
| 729 | |
| 730 | @Override |
| 731 | public void onProviderDisabled(String provider) { |
| 732 | } |
| 733 | }; |
| 734 | |
| 735 | private final LocationListener mGpsLocationListener = new LocationListener() { |
| 736 | @Override |
| 737 | public void onLocationChanged(Location location) { |
| 738 | synchronized (DeviceIdleController.this) { |
| 739 | receivedGpsLocationLocked(location); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | @Override |
| 744 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 745 | } |
| 746 | |
| 747 | @Override |
| 748 | public void onProviderEnabled(String provider) { |
| 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public void onProviderDisabled(String provider) { |
| 753 | } |
| 754 | }; |
| 755 | |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 756 | /** |
| 757 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 758 | * global Settings. Any access to this class or its fields should be done while |
| 759 | * holding the DeviceIdleController lock. |
| 760 | */ |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 761 | public final class Constants extends ContentObserver { |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 762 | // Key names stored in the settings value. |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 763 | private static final String KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT |
| 764 | = "light_after_inactive_to"; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 765 | private static final String KEY_LIGHT_PRE_IDLE_TIMEOUT = "light_pre_idle_to"; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 766 | private static final String KEY_LIGHT_IDLE_TIMEOUT = "light_idle_to"; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 767 | private static final String KEY_LIGHT_IDLE_FACTOR = "light_idle_factor"; |
| 768 | private static final String KEY_LIGHT_MAX_IDLE_TIMEOUT = "light_max_idle_to"; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 769 | private static final String KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET |
| 770 | = "light_idle_maintenance_min_budget"; |
| 771 | private static final String KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET |
| 772 | = "light_idle_maintenance_max_budget"; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 773 | private static final String KEY_MIN_LIGHT_MAINTENANCE_TIME = "min_light_maintenance_time"; |
| 774 | private static final String KEY_MIN_DEEP_MAINTENANCE_TIME = "min_deep_maintenance_time"; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 775 | private static final String KEY_INACTIVE_TIMEOUT = "inactive_to"; |
| 776 | private static final String KEY_SENSING_TIMEOUT = "sensing_to"; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 777 | private static final String KEY_LOCATING_TIMEOUT = "locating_to"; |
| 778 | private static final String KEY_LOCATION_ACCURACY = "location_accuracy"; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 779 | private static final String KEY_MOTION_INACTIVE_TIMEOUT = "motion_inactive_to"; |
| 780 | private static final String KEY_IDLE_AFTER_INACTIVE_TIMEOUT = "idle_after_inactive_to"; |
| 781 | private static final String KEY_IDLE_PENDING_TIMEOUT = "idle_pending_to"; |
| 782 | private static final String KEY_MAX_IDLE_PENDING_TIMEOUT = "max_idle_pending_to"; |
| 783 | private static final String KEY_IDLE_PENDING_FACTOR = "idle_pending_factor"; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 784 | private static final String KEY_QUICK_DOZE_DELAY_TIMEOUT = "quick_doze_delay_to"; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 785 | private static final String KEY_IDLE_TIMEOUT = "idle_to"; |
| 786 | private static final String KEY_MAX_IDLE_TIMEOUT = "max_idle_to"; |
| 787 | private static final String KEY_IDLE_FACTOR = "idle_factor"; |
| 788 | private static final String KEY_MIN_TIME_TO_ALARM = "min_time_to_alarm"; |
| 789 | private static final String KEY_MAX_TEMP_APP_WHITELIST_DURATION = |
| 790 | "max_temp_app_whitelist_duration"; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 791 | private static final String KEY_MMS_TEMP_APP_WHITELIST_DURATION = |
| 792 | "mms_temp_app_whitelist_duration"; |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 793 | private static final String KEY_SMS_TEMP_APP_WHITELIST_DURATION = |
| 794 | "sms_temp_app_whitelist_duration"; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 795 | private static final String KEY_NOTIFICATION_WHITELIST_DURATION = |
| 796 | "notification_whitelist_duration"; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 797 | /** |
| 798 | * Whether to wait for the user to unlock the device before causing screen-on to |
| 799 | * exit doze. Default = true |
| 800 | */ |
| 801 | private static final String KEY_WAIT_FOR_UNLOCK = "wait_for_unlock"; |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 802 | private static final String KEY_PRE_IDLE_FACTOR_LONG = |
| 803 | "pre_idle_factor_long"; |
| 804 | private static final String KEY_PRE_IDLE_FACTOR_SHORT = |
| 805 | "pre_idle_factor_short"; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 806 | |
| 807 | /** |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 808 | * This is the time, after becoming inactive, that we go in to the first |
| 809 | * light-weight idle mode. |
| 810 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 811 | * @see #KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT |
| 812 | */ |
| 813 | public long LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT; |
| 814 | |
| 815 | /** |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 816 | * This is amount of time we will wait from the point where we decide we would |
| 817 | * like to go idle until we actually do, while waiting for jobs and other current |
| 818 | * activity to finish. |
| 819 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 820 | * @see #KEY_LIGHT_PRE_IDLE_TIMEOUT |
| 821 | */ |
| 822 | public long LIGHT_PRE_IDLE_TIMEOUT; |
| 823 | |
| 824 | /** |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 825 | * This is the initial time that we will run in idle maintenance mode. |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 826 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 827 | * @see #KEY_LIGHT_IDLE_TIMEOUT |
| 828 | */ |
| 829 | public long LIGHT_IDLE_TIMEOUT; |
| 830 | |
| 831 | /** |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 832 | * Scaling factor to apply to the light idle mode time each time we complete a cycle. |
| 833 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 834 | * @see #KEY_LIGHT_IDLE_FACTOR |
| 835 | */ |
| 836 | public float LIGHT_IDLE_FACTOR; |
| 837 | |
| 838 | /** |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 839 | * This is the maximum time we will run in idle maintenance mode. |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 840 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 841 | * @see #KEY_LIGHT_MAX_IDLE_TIMEOUT |
| 842 | */ |
| 843 | public long LIGHT_MAX_IDLE_TIMEOUT; |
| 844 | |
| 845 | /** |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 846 | * This is the minimum amount of time we want to make available for maintenance mode |
| 847 | * when lightly idling. That is, we will always have at least this amount of time |
| 848 | * available maintenance before timing out and cutting off maintenance mode. |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 849 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 850 | * @see #KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 851 | */ |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 852 | public long LIGHT_IDLE_MAINTENANCE_MIN_BUDGET; |
| 853 | |
| 854 | /** |
| 855 | * This is the maximum amount of time we want to make available for maintenance mode |
| 856 | * when lightly idling. That is, if the system isn't using up its minimum maintenance |
| 857 | * budget and this time is being added to the budget reserve, this is the maximum |
| 858 | * reserve size we will allow to grow and thus the maximum amount of time we will |
| 859 | * allow for the maintenance window. |
| 860 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 861 | * @see #KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET |
| 862 | */ |
| 863 | public long LIGHT_IDLE_MAINTENANCE_MAX_BUDGET; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 864 | |
| 865 | /** |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 866 | * This is the minimum amount of time that we will stay in maintenance mode after |
| 867 | * a light doze. We have this minimum to allow various things to respond to switching |
| 868 | * in to maintenance mode and scheduling their work -- otherwise we may |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 869 | * see there is nothing to do (no jobs pending) and go out of maintenance |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 870 | * mode immediately. |
| 871 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 872 | * @see #KEY_MIN_LIGHT_MAINTENANCE_TIME |
| 873 | */ |
| 874 | public long MIN_LIGHT_MAINTENANCE_TIME; |
| 875 | |
| 876 | /** |
| 877 | * This is the minimum amount of time that we will stay in maintenance mode after |
| 878 | * a full doze. We have this minimum to allow various things to respond to switching |
| 879 | * in to maintenance mode and scheduling their work -- otherwise we may |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 880 | * see there is nothing to do (no jobs pending) and go out of maintenance |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 881 | * mode immediately. |
| 882 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 883 | * @see #KEY_MIN_DEEP_MAINTENANCE_TIME |
| 884 | */ |
| 885 | public long MIN_DEEP_MAINTENANCE_TIME; |
| 886 | |
| 887 | /** |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 888 | * This is the time, after becoming inactive, at which we start looking at the |
| 889 | * motion sensor to determine if the device is being left alone. We don't do this |
| 890 | * immediately after going inactive just because we don't want to be continually running |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 891 | * the motion sensor whenever the screen is off. |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 892 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 893 | * @see #KEY_INACTIVE_TIMEOUT |
| 894 | */ |
| 895 | public long INACTIVE_TIMEOUT; |
| 896 | |
| 897 | /** |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 898 | * If we don't receive a callback from AnyMotion in this amount of time + |
| 899 | * {@link #LOCATING_TIMEOUT}, we will change from |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 900 | * STATE_SENSING to STATE_INACTIVE, and any AnyMotion callbacks while not in STATE_SENSING |
| 901 | * will be ignored. |
| 902 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 903 | * @see #KEY_SENSING_TIMEOUT |
| 904 | */ |
| 905 | public long SENSING_TIMEOUT; |
| 906 | |
| 907 | /** |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 908 | * This is how long we will wait to try to get a good location fix before going in to |
| 909 | * idle mode. |
| 910 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 911 | * @see #KEY_LOCATING_TIMEOUT |
| 912 | */ |
| 913 | public long LOCATING_TIMEOUT; |
| 914 | |
| 915 | /** |
| 916 | * The desired maximum accuracy (in meters) we consider the location to be good enough to go |
| 917 | * on to idle. We will be trying to get an accuracy fix at least this good or until |
| 918 | * {@link #LOCATING_TIMEOUT} expires. |
| 919 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 920 | * @see #KEY_LOCATION_ACCURACY |
| 921 | */ |
| 922 | public float LOCATION_ACCURACY; |
| 923 | |
| 924 | /** |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 925 | * This is the time, after seeing motion, that we wait after becoming inactive from |
| 926 | * that until we start looking for motion again. |
| 927 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 928 | * @see #KEY_MOTION_INACTIVE_TIMEOUT |
| 929 | */ |
| 930 | public long MOTION_INACTIVE_TIMEOUT; |
| 931 | |
| 932 | /** |
| 933 | * This is the time, after the inactive timeout elapses, that we will wait looking |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 934 | * for motion until we truly consider the device to be idle. |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 935 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 936 | * @see #KEY_IDLE_AFTER_INACTIVE_TIMEOUT |
| 937 | */ |
| 938 | public long IDLE_AFTER_INACTIVE_TIMEOUT; |
| 939 | |
| 940 | /** |
| 941 | * This is the initial time, after being idle, that we will allow ourself to be back |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 942 | * in the IDLE_MAINTENANCE state allowing the system to run normally until we return to |
| 943 | * idle. |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 944 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 945 | * @see #KEY_IDLE_PENDING_TIMEOUT |
| 946 | */ |
| 947 | public long IDLE_PENDING_TIMEOUT; |
| 948 | |
| 949 | /** |
| 950 | * Maximum pending idle timeout (time spent running) we will be allowed to use. |
| 951 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 952 | * @see #KEY_MAX_IDLE_PENDING_TIMEOUT |
| 953 | */ |
| 954 | public long MAX_IDLE_PENDING_TIMEOUT; |
| 955 | |
| 956 | /** |
| 957 | * Scaling factor to apply to current pending idle timeout each time we cycle through |
| 958 | * that state. |
| 959 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 960 | * @see #KEY_IDLE_PENDING_FACTOR |
| 961 | */ |
| 962 | public float IDLE_PENDING_FACTOR; |
| 963 | |
| 964 | /** |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 965 | * This is amount of time we will wait from the point where we go into |
| 966 | * STATE_QUICK_DOZE_DELAY until we actually go into STATE_IDLE, while waiting for jobs |
| 967 | * and other current activity to finish. |
| 968 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 969 | * @see #KEY_QUICK_DOZE_DELAY_TIMEOUT |
| 970 | */ |
| 971 | public long QUICK_DOZE_DELAY_TIMEOUT; |
| 972 | |
| 973 | /** |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 974 | * This is the initial time that we want to sit in the idle state before waking up |
| 975 | * again to return to pending idle and allowing normal work to run. |
| 976 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 977 | * @see #KEY_IDLE_TIMEOUT |
| 978 | */ |
| 979 | public long IDLE_TIMEOUT; |
| 980 | |
| 981 | /** |
| 982 | * Maximum idle duration we will be allowed to use. |
| 983 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 984 | * @see #KEY_MAX_IDLE_TIMEOUT |
| 985 | */ |
| 986 | public long MAX_IDLE_TIMEOUT; |
| 987 | |
| 988 | /** |
| 989 | * Scaling factor to apply to current idle timeout each time we cycle through that state. |
| 990 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 991 | * @see #KEY_IDLE_FACTOR |
| 992 | */ |
| 993 | public float IDLE_FACTOR; |
| 994 | |
| 995 | /** |
| 996 | * This is the minimum time we will allow until the next upcoming alarm for us to |
| 997 | * actually go in to idle mode. |
| 998 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 999 | * @see #KEY_MIN_TIME_TO_ALARM |
| 1000 | */ |
| 1001 | public long MIN_TIME_TO_ALARM; |
| 1002 | |
| 1003 | /** |
| 1004 | * Max amount of time to temporarily whitelist an app when it receives a high priority |
| 1005 | * tickle. |
| 1006 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 1007 | * @see #KEY_MAX_TEMP_APP_WHITELIST_DURATION |
| 1008 | */ |
| 1009 | public long MAX_TEMP_APP_WHITELIST_DURATION; |
| 1010 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1011 | /** |
| 1012 | * Amount of time we would like to whitelist an app that is receiving an MMS. |
| 1013 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 1014 | * @see #KEY_MMS_TEMP_APP_WHITELIST_DURATION |
| 1015 | */ |
| 1016 | public long MMS_TEMP_APP_WHITELIST_DURATION; |
| 1017 | |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 1018 | /** |
| 1019 | * Amount of time we would like to whitelist an app that is receiving an SMS. |
| 1020 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 1021 | * @see #KEY_SMS_TEMP_APP_WHITELIST_DURATION |
| 1022 | */ |
| 1023 | public long SMS_TEMP_APP_WHITELIST_DURATION; |
| 1024 | |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1025 | /** |
| 1026 | * Amount of time we would like to whitelist an app that is handling a |
| 1027 | * {@link android.app.PendingIntent} triggered by a {@link android.app.Notification}. |
| 1028 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 1029 | * @see #KEY_NOTIFICATION_WHITELIST_DURATION |
| 1030 | */ |
| 1031 | public long NOTIFICATION_WHITELIST_DURATION; |
| 1032 | |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1033 | /** |
| 1034 | * Pre idle time factor use to make idle delay longer |
| 1035 | */ |
| 1036 | public float PRE_IDLE_FACTOR_LONG; |
| 1037 | |
| 1038 | /** |
| 1039 | * Pre idle time factor use to make idle delay shorter |
| 1040 | */ |
| 1041 | public float PRE_IDLE_FACTOR_SHORT; |
| 1042 | |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1043 | public boolean WAIT_FOR_UNLOCK; |
| 1044 | |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1045 | private final ContentResolver mResolver; |
shreerag | 597da8a | 2017-07-21 14:24:14 -0700 | [diff] [blame] | 1046 | private final boolean mSmallBatteryDevice; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1047 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 1048 | |
| 1049 | public Constants(Handler handler, ContentResolver resolver) { |
| 1050 | super(handler); |
| 1051 | mResolver = resolver; |
shreerag | 597da8a | 2017-07-21 14:24:14 -0700 | [diff] [blame] | 1052 | mSmallBatteryDevice = ActivityManager.isSmallBatteryDevice(); |
| 1053 | mResolver.registerContentObserver( |
| 1054 | Settings.Global.getUriFor(Settings.Global.DEVICE_IDLE_CONSTANTS), |
Joe LaPenna | f33b5bf | 2016-03-23 15:19:47 -0700 | [diff] [blame] | 1055 | false, this); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1056 | updateConstants(); |
| 1057 | } |
| 1058 | |
| 1059 | @Override |
| 1060 | public void onChange(boolean selfChange, Uri uri) { |
| 1061 | updateConstants(); |
| 1062 | } |
| 1063 | |
| 1064 | private void updateConstants() { |
| 1065 | synchronized (DeviceIdleController.this) { |
| 1066 | try { |
| 1067 | mParser.setString(Settings.Global.getString(mResolver, |
shreerag | 597da8a | 2017-07-21 14:24:14 -0700 | [diff] [blame] | 1068 | Settings.Global.DEVICE_IDLE_CONSTANTS)); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1069 | } catch (IllegalArgumentException e) { |
| 1070 | // Failed to parse the settings string, log this and move on |
| 1071 | // with defaults. |
| 1072 | Slog.e(TAG, "Bad device idle settings", e); |
| 1073 | } |
| 1074 | |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1075 | LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT = mParser.getDurationMillis( |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1076 | KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT, |
Suprabh Shukla | 85fff7d | 2018-05-08 17:39:24 -0700 | [diff] [blame] | 1077 | !COMPRESS_TIME ? 3 * 60 * 1000L : 15 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1078 | LIGHT_PRE_IDLE_TIMEOUT = mParser.getDurationMillis(KEY_LIGHT_PRE_IDLE_TIMEOUT, |
Suprabh Shukla | 85fff7d | 2018-05-08 17:39:24 -0700 | [diff] [blame] | 1079 | !COMPRESS_TIME ? 3 * 60 * 1000L : 30 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1080 | LIGHT_IDLE_TIMEOUT = mParser.getDurationMillis(KEY_LIGHT_IDLE_TIMEOUT, |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1081 | !COMPRESS_TIME ? 5 * 60 * 1000L : 15 * 1000L); |
| 1082 | LIGHT_IDLE_FACTOR = mParser.getFloat(KEY_LIGHT_IDLE_FACTOR, |
| 1083 | 2f); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1084 | LIGHT_MAX_IDLE_TIMEOUT = mParser.getDurationMillis(KEY_LIGHT_MAX_IDLE_TIMEOUT, |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1085 | !COMPRESS_TIME ? 15 * 60 * 1000L : 60 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1086 | LIGHT_IDLE_MAINTENANCE_MIN_BUDGET = mParser.getDurationMillis( |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1087 | KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET, |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1088 | !COMPRESS_TIME ? 1 * 60 * 1000L : 15 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1089 | LIGHT_IDLE_MAINTENANCE_MAX_BUDGET = mParser.getDurationMillis( |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1090 | KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET, |
| 1091 | !COMPRESS_TIME ? 5 * 60 * 1000L : 30 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1092 | MIN_LIGHT_MAINTENANCE_TIME = mParser.getDurationMillis( |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 1093 | KEY_MIN_LIGHT_MAINTENANCE_TIME, |
| 1094 | !COMPRESS_TIME ? 5 * 1000L : 1 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1095 | MIN_DEEP_MAINTENANCE_TIME = mParser.getDurationMillis( |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 1096 | KEY_MIN_DEEP_MAINTENANCE_TIME, |
| 1097 | !COMPRESS_TIME ? 30 * 1000L : 5 * 1000L); |
Michael Kwan | 8887146 | 2017-08-21 13:13:37 -0700 | [diff] [blame] | 1098 | long inactiveTimeoutDefault = (mSmallBatteryDevice ? 15 : 30) * 60 * 1000L; |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1099 | INACTIVE_TIMEOUT = mParser.getDurationMillis(KEY_INACTIVE_TIMEOUT, |
Joe LaPenna | 0c5d3e9 | 2016-03-24 13:29:56 -0700 | [diff] [blame] | 1100 | !COMPRESS_TIME ? inactiveTimeoutDefault : (inactiveTimeoutDefault / 10)); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1101 | SENSING_TIMEOUT = mParser.getDurationMillis(KEY_SENSING_TIMEOUT, |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 1102 | !COMPRESS_TIME ? 4 * 60 * 1000L : 60 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1103 | LOCATING_TIMEOUT = mParser.getDurationMillis(KEY_LOCATING_TIMEOUT, |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 1104 | !COMPRESS_TIME ? 30 * 1000L : 15 * 1000L); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1105 | LOCATION_ACCURACY = mParser.getFloat(KEY_LOCATION_ACCURACY, 20); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1106 | MOTION_INACTIVE_TIMEOUT = mParser.getDurationMillis(KEY_MOTION_INACTIVE_TIMEOUT, |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1107 | !COMPRESS_TIME ? 10 * 60 * 1000L : 60 * 1000L); |
Michael Kwan | 8887146 | 2017-08-21 13:13:37 -0700 | [diff] [blame] | 1108 | long idleAfterInactiveTimeout = (mSmallBatteryDevice ? 15 : 30) * 60 * 1000L; |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1109 | IDLE_AFTER_INACTIVE_TIMEOUT = mParser.getDurationMillis( |
| 1110 | KEY_IDLE_AFTER_INACTIVE_TIMEOUT, |
Joe LaPenna | 0c5d3e9 | 2016-03-24 13:29:56 -0700 | [diff] [blame] | 1111 | !COMPRESS_TIME ? idleAfterInactiveTimeout |
| 1112 | : (idleAfterInactiveTimeout / 10)); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1113 | IDLE_PENDING_TIMEOUT = mParser.getDurationMillis(KEY_IDLE_PENDING_TIMEOUT, |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1114 | !COMPRESS_TIME ? 5 * 60 * 1000L : 30 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1115 | MAX_IDLE_PENDING_TIMEOUT = mParser.getDurationMillis(KEY_MAX_IDLE_PENDING_TIMEOUT, |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1116 | !COMPRESS_TIME ? 10 * 60 * 1000L : 60 * 1000L); |
| 1117 | IDLE_PENDING_FACTOR = mParser.getFloat(KEY_IDLE_PENDING_FACTOR, |
| 1118 | 2f); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1119 | QUICK_DOZE_DELAY_TIMEOUT = mParser.getDurationMillis( |
| 1120 | KEY_QUICK_DOZE_DELAY_TIMEOUT, !COMPRESS_TIME ? 60 * 1000L : 15 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1121 | IDLE_TIMEOUT = mParser.getDurationMillis(KEY_IDLE_TIMEOUT, |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1122 | !COMPRESS_TIME ? 60 * 60 * 1000L : 6 * 60 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1123 | MAX_IDLE_TIMEOUT = mParser.getDurationMillis(KEY_MAX_IDLE_TIMEOUT, |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1124 | !COMPRESS_TIME ? 6 * 60 * 60 * 1000L : 30 * 60 * 1000L); |
| 1125 | IDLE_FACTOR = mParser.getFloat(KEY_IDLE_FACTOR, |
| 1126 | 2f); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1127 | MIN_TIME_TO_ALARM = mParser.getDurationMillis(KEY_MIN_TIME_TO_ALARM, |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1128 | !COMPRESS_TIME ? 60 * 60 * 1000L : 6 * 60 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1129 | MAX_TEMP_APP_WHITELIST_DURATION = mParser.getDurationMillis( |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1130 | KEY_MAX_TEMP_APP_WHITELIST_DURATION, 5 * 60 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1131 | MMS_TEMP_APP_WHITELIST_DURATION = mParser.getDurationMillis( |
Dianne Hackborn | 0b6134b | 2015-07-14 18:48:07 -0700 | [diff] [blame] | 1132 | KEY_MMS_TEMP_APP_WHITELIST_DURATION, 60 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1133 | SMS_TEMP_APP_WHITELIST_DURATION = mParser.getDurationMillis( |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 1134 | KEY_SMS_TEMP_APP_WHITELIST_DURATION, 20 * 1000L); |
Amith Yamasani | 761d3ff | 2017-12-14 17:50:03 -0800 | [diff] [blame] | 1135 | NOTIFICATION_WHITELIST_DURATION = mParser.getDurationMillis( |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1136 | KEY_NOTIFICATION_WHITELIST_DURATION, 30 * 1000L); |
Kweku Adams | 43fabba | 2019-03-07 15:47:37 -0800 | [diff] [blame] | 1137 | WAIT_FOR_UNLOCK = mParser.getBoolean(KEY_WAIT_FOR_UNLOCK, true); |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1138 | PRE_IDLE_FACTOR_LONG = mParser.getFloat(KEY_PRE_IDLE_FACTOR_LONG, 1.67f); |
| 1139 | PRE_IDLE_FACTOR_SHORT = mParser.getFloat(KEY_PRE_IDLE_FACTOR_SHORT, 0.33f); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | void dump(PrintWriter pw) { |
| 1144 | pw.println(" Settings:"); |
| 1145 | |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1146 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT); pw.print("="); |
| 1147 | TimeUtils.formatDuration(LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT, pw); |
| 1148 | pw.println(); |
| 1149 | |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1150 | pw.print(" "); pw.print(KEY_LIGHT_PRE_IDLE_TIMEOUT); pw.print("="); |
| 1151 | TimeUtils.formatDuration(LIGHT_PRE_IDLE_TIMEOUT, pw); |
| 1152 | pw.println(); |
| 1153 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1154 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_TIMEOUT); pw.print("="); |
| 1155 | TimeUtils.formatDuration(LIGHT_IDLE_TIMEOUT, pw); |
| 1156 | pw.println(); |
| 1157 | |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1158 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_FACTOR); pw.print("="); |
| 1159 | pw.print(LIGHT_IDLE_FACTOR); |
| 1160 | pw.println(); |
| 1161 | |
| 1162 | pw.print(" "); pw.print(KEY_LIGHT_MAX_IDLE_TIMEOUT); pw.print("="); |
| 1163 | TimeUtils.formatDuration(LIGHT_MAX_IDLE_TIMEOUT, pw); |
| 1164 | pw.println(); |
| 1165 | |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1166 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET); pw.print("="); |
| 1167 | TimeUtils.formatDuration(LIGHT_IDLE_MAINTENANCE_MIN_BUDGET, pw); |
| 1168 | pw.println(); |
| 1169 | |
| 1170 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET); pw.print("="); |
| 1171 | TimeUtils.formatDuration(LIGHT_IDLE_MAINTENANCE_MAX_BUDGET, pw); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1172 | pw.println(); |
| 1173 | |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 1174 | pw.print(" "); pw.print(KEY_MIN_LIGHT_MAINTENANCE_TIME); pw.print("="); |
| 1175 | TimeUtils.formatDuration(MIN_LIGHT_MAINTENANCE_TIME, pw); |
| 1176 | pw.println(); |
| 1177 | |
| 1178 | pw.print(" "); pw.print(KEY_MIN_DEEP_MAINTENANCE_TIME); pw.print("="); |
| 1179 | TimeUtils.formatDuration(MIN_DEEP_MAINTENANCE_TIME, pw); |
| 1180 | pw.println(); |
| 1181 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1182 | pw.print(" "); pw.print(KEY_INACTIVE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1183 | TimeUtils.formatDuration(INACTIVE_TIMEOUT, pw); |
| 1184 | pw.println(); |
| 1185 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1186 | pw.print(" "); pw.print(KEY_SENSING_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1187 | TimeUtils.formatDuration(SENSING_TIMEOUT, pw); |
| 1188 | pw.println(); |
| 1189 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1190 | pw.print(" "); pw.print(KEY_LOCATING_TIMEOUT); pw.print("="); |
| 1191 | TimeUtils.formatDuration(LOCATING_TIMEOUT, pw); |
| 1192 | pw.println(); |
| 1193 | |
| 1194 | pw.print(" "); pw.print(KEY_LOCATION_ACCURACY); pw.print("="); |
| 1195 | pw.print(LOCATION_ACCURACY); pw.print("m"); |
| 1196 | pw.println(); |
| 1197 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1198 | pw.print(" "); pw.print(KEY_MOTION_INACTIVE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1199 | TimeUtils.formatDuration(MOTION_INACTIVE_TIMEOUT, pw); |
| 1200 | pw.println(); |
| 1201 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1202 | pw.print(" "); pw.print(KEY_IDLE_AFTER_INACTIVE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1203 | TimeUtils.formatDuration(IDLE_AFTER_INACTIVE_TIMEOUT, pw); |
| 1204 | pw.println(); |
| 1205 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1206 | pw.print(" "); pw.print(KEY_IDLE_PENDING_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1207 | TimeUtils.formatDuration(IDLE_PENDING_TIMEOUT, pw); |
| 1208 | pw.println(); |
| 1209 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1210 | pw.print(" "); pw.print(KEY_MAX_IDLE_PENDING_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1211 | TimeUtils.formatDuration(MAX_IDLE_PENDING_TIMEOUT, pw); |
| 1212 | pw.println(); |
| 1213 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1214 | pw.print(" "); pw.print(KEY_IDLE_PENDING_FACTOR); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1215 | pw.println(IDLE_PENDING_FACTOR); |
| 1216 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1217 | pw.print(" "); pw.print(KEY_QUICK_DOZE_DELAY_TIMEOUT); pw.print("="); |
| 1218 | TimeUtils.formatDuration(QUICK_DOZE_DELAY_TIMEOUT, pw); |
| 1219 | pw.println(); |
| 1220 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1221 | pw.print(" "); pw.print(KEY_IDLE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1222 | TimeUtils.formatDuration(IDLE_TIMEOUT, pw); |
| 1223 | pw.println(); |
| 1224 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1225 | pw.print(" "); pw.print(KEY_MAX_IDLE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1226 | TimeUtils.formatDuration(MAX_IDLE_TIMEOUT, pw); |
| 1227 | pw.println(); |
| 1228 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1229 | pw.print(" "); pw.print(KEY_IDLE_FACTOR); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1230 | pw.println(IDLE_FACTOR); |
| 1231 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1232 | pw.print(" "); pw.print(KEY_MIN_TIME_TO_ALARM); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1233 | TimeUtils.formatDuration(MIN_TIME_TO_ALARM, pw); |
| 1234 | pw.println(); |
| 1235 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1236 | pw.print(" "); pw.print(KEY_MAX_TEMP_APP_WHITELIST_DURATION); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1237 | TimeUtils.formatDuration(MAX_TEMP_APP_WHITELIST_DURATION, pw); |
| 1238 | pw.println(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1239 | |
| 1240 | pw.print(" "); pw.print(KEY_MMS_TEMP_APP_WHITELIST_DURATION); pw.print("="); |
| 1241 | TimeUtils.formatDuration(MMS_TEMP_APP_WHITELIST_DURATION, pw); |
| 1242 | pw.println(); |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 1243 | |
| 1244 | pw.print(" "); pw.print(KEY_SMS_TEMP_APP_WHITELIST_DURATION); pw.print("="); |
| 1245 | TimeUtils.formatDuration(SMS_TEMP_APP_WHITELIST_DURATION, pw); |
| 1246 | pw.println(); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1247 | |
| 1248 | pw.print(" "); pw.print(KEY_NOTIFICATION_WHITELIST_DURATION); pw.print("="); |
| 1249 | TimeUtils.formatDuration(NOTIFICATION_WHITELIST_DURATION, pw); |
| 1250 | pw.println(); |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1251 | |
| 1252 | pw.print(" "); pw.print(KEY_WAIT_FOR_UNLOCK); pw.print("="); |
| 1253 | pw.println(WAIT_FOR_UNLOCK); |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1254 | |
| 1255 | pw.print(" "); pw.print(KEY_PRE_IDLE_FACTOR_LONG); pw.print("="); |
| 1256 | pw.println(PRE_IDLE_FACTOR_LONG); |
| 1257 | |
| 1258 | pw.print(" "); pw.print(KEY_PRE_IDLE_FACTOR_SHORT); pw.print("="); |
| 1259 | pw.println(PRE_IDLE_FACTOR_SHORT); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | private Constants mConstants; |
| 1264 | |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1265 | @Override |
| 1266 | public void onAnyMotionResult(int result) { |
| 1267 | if (DEBUG) Slog.d(TAG, "onAnyMotionResult(" + result + ")"); |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 1268 | if (result != AnyMotionDetector.RESULT_UNKNOWN) { |
| 1269 | synchronized (this) { |
| 1270 | cancelSensingTimeoutAlarmLocked(); |
| 1271 | } |
| 1272 | } |
Kevin Gabayan | dcf4701 | 2016-07-08 10:41:24 -0700 | [diff] [blame] | 1273 | if ((result == AnyMotionDetector.RESULT_MOVED) || |
| 1274 | (result == AnyMotionDetector.RESULT_UNKNOWN)) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1275 | synchronized (this) { |
Kevin Gabayan | dcf4701 | 2016-07-08 10:41:24 -0700 | [diff] [blame] | 1276 | handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "non_stationary"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1277 | } |
| 1278 | } else if (result == AnyMotionDetector.RESULT_STATIONARY) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1279 | if (mState == STATE_SENSING) { |
| 1280 | // If we are currently sensing, it is time to move to locating. |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1281 | synchronized (this) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1282 | mNotMoving = true; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1283 | stepIdleStateLocked("s:stationary"); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1284 | } |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1285 | } else if (mState == STATE_LOCATING) { |
| 1286 | // If we are currently locating, note that we are not moving and step |
| 1287 | // if we have located the position. |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1288 | synchronized (this) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1289 | mNotMoving = true; |
| 1290 | if (mLocated) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1291 | stepIdleStateLocked("s:stationary"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1292 | } |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1298 | private static final int MSG_WRITE_CONFIG = 1; |
| 1299 | private static final int MSG_REPORT_IDLE_ON = 2; |
| 1300 | private static final int MSG_REPORT_IDLE_ON_LIGHT = 3; |
| 1301 | private static final int MSG_REPORT_IDLE_OFF = 4; |
| 1302 | private static final int MSG_REPORT_ACTIVE = 5; |
| 1303 | private static final int MSG_TEMP_APP_WHITELIST_TIMEOUT = 6; |
| 1304 | private static final int MSG_REPORT_MAINTENANCE_ACTIVITY = 7; |
| 1305 | private static final int MSG_FINISH_IDLE_OP = 8; |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 1306 | private static final int MSG_REPORT_TEMP_APP_WHITELIST_CHANGED = 9; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1307 | private static final int MSG_SEND_CONSTRAINT_MONITORING = 10; |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1308 | private static final int MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR = 11; |
| 1309 | private static final int MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR = 12; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1310 | |
| 1311 | final class MyHandler extends Handler { |
| 1312 | MyHandler(Looper looper) { |
| 1313 | super(looper); |
| 1314 | } |
| 1315 | |
| 1316 | @Override public void handleMessage(Message msg) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1317 | if (DEBUG) Slog.d(TAG, "handleMessage(" + msg.what + ")"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1318 | switch (msg.what) { |
| 1319 | case MSG_WRITE_CONFIG: { |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1320 | // Does not hold a wakelock. Just let this happen whenever. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1321 | handleWriteConfigFile(); |
| 1322 | } break; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1323 | case MSG_REPORT_IDLE_ON: |
| 1324 | case MSG_REPORT_IDLE_ON_LIGHT: { |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1325 | // mGoingIdleWakeLock is held at this point |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1326 | EventLogTags.writeDeviceIdleOnStart(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1327 | final boolean deepChanged; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1328 | final boolean lightChanged; |
| 1329 | if (msg.what == MSG_REPORT_IDLE_ON) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1330 | deepChanged = mLocalPowerManager.setDeviceIdleMode(true); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1331 | lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false); |
| 1332 | } else { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1333 | deepChanged = mLocalPowerManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1334 | lightChanged = mLocalPowerManager.setLightDeviceIdleMode(true); |
| 1335 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1336 | try { |
| 1337 | mNetworkPolicyManager.setDeviceIdleMode(true); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1338 | mBatteryStats.noteDeviceIdleMode(msg.what == MSG_REPORT_IDLE_ON |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 1339 | ? BatteryStats.DEVICE_IDLE_MODE_DEEP |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1340 | : BatteryStats.DEVICE_IDLE_MODE_LIGHT, null, Process.myUid()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1341 | } catch (RemoteException e) { |
| 1342 | } |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1343 | if (deepChanged) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1344 | getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL); |
| 1345 | } |
| 1346 | if (lightChanged) { |
| 1347 | getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL); |
| 1348 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1349 | EventLogTags.writeDeviceIdleOnComplete(); |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1350 | mGoingIdleWakeLock.release(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1351 | } break; |
| 1352 | case MSG_REPORT_IDLE_OFF: { |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1353 | // mActiveIdleWakeLock is held at this point |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1354 | EventLogTags.writeDeviceIdleOffStart("unknown"); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1355 | final boolean deepChanged = mLocalPowerManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1356 | final boolean lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1357 | try { |
| 1358 | mNetworkPolicyManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1359 | mBatteryStats.noteDeviceIdleMode(BatteryStats.DEVICE_IDLE_MODE_OFF, |
| 1360 | null, Process.myUid()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1361 | } catch (RemoteException e) { |
| 1362 | } |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1363 | if (deepChanged) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1364 | incActiveIdleOps(); |
| 1365 | getContext().sendOrderedBroadcastAsUser(mIdleIntent, UserHandle.ALL, |
| 1366 | null, mIdleStartedDoneReceiver, null, 0, null, null); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1367 | } |
| 1368 | if (lightChanged) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1369 | incActiveIdleOps(); |
| 1370 | getContext().sendOrderedBroadcastAsUser(mLightIdleIntent, UserHandle.ALL, |
| 1371 | null, mIdleStartedDoneReceiver, null, 0, null, null); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1372 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1373 | // Always start with one active op for the message being sent here. |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1374 | // Now we are done! |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1375 | decActiveIdleOps(); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1376 | EventLogTags.writeDeviceIdleOffComplete(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1377 | } break; |
| 1378 | case MSG_REPORT_ACTIVE: { |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1379 | // The device is awake at this point, so no wakelock necessary. |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1380 | String activeReason = (String)msg.obj; |
| 1381 | int activeUid = msg.arg1; |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1382 | EventLogTags.writeDeviceIdleOffStart( |
| 1383 | activeReason != null ? activeReason : "unknown"); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1384 | final boolean deepChanged = mLocalPowerManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1385 | final boolean lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1386 | try { |
| 1387 | mNetworkPolicyManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1388 | mBatteryStats.noteDeviceIdleMode(BatteryStats.DEVICE_IDLE_MODE_OFF, |
| 1389 | activeReason, activeUid); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1390 | } catch (RemoteException e) { |
| 1391 | } |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1392 | if (deepChanged) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1393 | getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL); |
| 1394 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1395 | if (lightChanged) { |
| 1396 | getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL); |
| 1397 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1398 | EventLogTags.writeDeviceIdleOffComplete(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1399 | } break; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1400 | case MSG_TEMP_APP_WHITELIST_TIMEOUT: { |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1401 | // TODO: What is keeping the device awake at this point? Does it need to be? |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 1402 | int appId = msg.arg1; |
| 1403 | checkTempAppWhitelistTimeout(appId); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1404 | } break; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 1405 | case MSG_REPORT_MAINTENANCE_ACTIVITY: { |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1406 | // TODO: What is keeping the device awake at this point? Does it need to be? |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 1407 | boolean active = (msg.arg1 == 1); |
| 1408 | final int size = mMaintenanceActivityListeners.beginBroadcast(); |
| 1409 | try { |
| 1410 | for (int i = 0; i < size; i++) { |
| 1411 | try { |
| 1412 | mMaintenanceActivityListeners.getBroadcastItem(i) |
| 1413 | .onMaintenanceActivityChanged(active); |
| 1414 | } catch (RemoteException ignored) { |
| 1415 | } |
| 1416 | } |
| 1417 | } finally { |
| 1418 | mMaintenanceActivityListeners.finishBroadcast(); |
| 1419 | } |
| 1420 | } break; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1421 | case MSG_FINISH_IDLE_OP: { |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1422 | // mActiveIdleWakeLock is held at this point |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1423 | decActiveIdleOps(); |
| 1424 | } break; |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 1425 | case MSG_REPORT_TEMP_APP_WHITELIST_CHANGED: { |
| 1426 | final int appId = msg.arg1; |
| 1427 | final boolean added = (msg.arg2 == 1); |
| 1428 | mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, added); |
| 1429 | } break; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1430 | case MSG_SEND_CONSTRAINT_MONITORING: { |
| 1431 | final IDeviceIdleConstraint constraint = (IDeviceIdleConstraint) msg.obj; |
| 1432 | final boolean monitoring = (msg.arg1 == 1); |
| 1433 | if (monitoring) { |
| 1434 | constraint.startMonitoring(); |
| 1435 | } else { |
| 1436 | constraint.stopMonitoring(); |
| 1437 | } |
| 1438 | } break; |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1439 | case MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR: { |
| 1440 | updatePreIdleFactor(); |
| 1441 | } break; |
| 1442 | case MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR: { |
| 1443 | updatePreIdleFactor(); |
| 1444 | maybeDoImmediateMaintenance(); |
| 1445 | } break; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | final MyHandler mHandler; |
| 1451 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1452 | BinderService mBinderService; |
| 1453 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1454 | private final class BinderService extends IDeviceIdleController.Stub { |
| 1455 | @Override public void addPowerSaveWhitelistApp(String name) { |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 1456 | if (DEBUG) { |
| 1457 | Slog.i(TAG, "addPowerSaveWhitelistApp(name = " + name + ")"); |
| 1458 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1459 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 1460 | null); |
Dianne Hackborn | c9b2cd1 | 2016-01-20 10:54:29 -0800 | [diff] [blame] | 1461 | long ident = Binder.clearCallingIdentity(); |
| 1462 | try { |
| 1463 | addPowerSaveWhitelistAppInternal(name); |
| 1464 | } finally { |
| 1465 | Binder.restoreCallingIdentity(ident); |
| 1466 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | @Override public void removePowerSaveWhitelistApp(String name) { |
Eugene Susla | 6a7006a | 2017-03-13 12:57:58 -0700 | [diff] [blame] | 1470 | if (DEBUG) { |
| 1471 | Slog.i(TAG, "removePowerSaveWhitelistApp(name = " + name + ")"); |
| 1472 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1473 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 1474 | null); |
Dianne Hackborn | c9b2cd1 | 2016-01-20 10:54:29 -0800 | [diff] [blame] | 1475 | long ident = Binder.clearCallingIdentity(); |
| 1476 | try { |
| 1477 | removePowerSaveWhitelistAppInternal(name); |
| 1478 | } finally { |
| 1479 | Binder.restoreCallingIdentity(ident); |
| 1480 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 1483 | @Override public void removeSystemPowerWhitelistApp(String name) { |
| 1484 | if (DEBUG) { |
| 1485 | Slog.d(TAG, "removeAppFromSystemWhitelist(name = " + name + ")"); |
| 1486 | } |
| 1487 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 1488 | null); |
| 1489 | long ident = Binder.clearCallingIdentity(); |
| 1490 | try { |
| 1491 | removeSystemPowerWhitelistAppInternal(name); |
| 1492 | } finally { |
| 1493 | Binder.restoreCallingIdentity(ident); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | @Override public void restoreSystemPowerWhitelistApp(String name) { |
| 1498 | if (DEBUG) { |
| 1499 | Slog.d(TAG, "restoreAppToSystemWhitelist(name = " + name + ")"); |
| 1500 | } |
| 1501 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 1502 | null); |
| 1503 | long ident = Binder.clearCallingIdentity(); |
| 1504 | try { |
| 1505 | restoreSystemPowerWhitelistAppInternal(name); |
| 1506 | } finally { |
| 1507 | Binder.restoreCallingIdentity(ident); |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | public String[] getRemovedSystemPowerWhitelistApps() { |
| 1512 | return getRemovedSystemPowerWhitelistAppsInternal(); |
| 1513 | } |
| 1514 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1515 | @Override public String[] getSystemPowerWhitelistExceptIdle() { |
| 1516 | return getSystemPowerWhitelistExceptIdleInternal(); |
| 1517 | } |
| 1518 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1519 | @Override public String[] getSystemPowerWhitelist() { |
| 1520 | return getSystemPowerWhitelistInternal(); |
| 1521 | } |
| 1522 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1523 | @Override public String[] getUserPowerWhitelist() { |
| 1524 | return getUserPowerWhitelistInternal(); |
| 1525 | } |
| 1526 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1527 | @Override public String[] getFullPowerWhitelistExceptIdle() { |
| 1528 | return getFullPowerWhitelistExceptIdleInternal(); |
| 1529 | } |
| 1530 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1531 | @Override public String[] getFullPowerWhitelist() { |
| 1532 | return getFullPowerWhitelistInternal(); |
| 1533 | } |
| 1534 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1535 | @Override public int[] getAppIdWhitelistExceptIdle() { |
| 1536 | return getAppIdWhitelistExceptIdleInternal(); |
| 1537 | } |
| 1538 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1539 | @Override public int[] getAppIdWhitelist() { |
| 1540 | return getAppIdWhitelistInternal(); |
| 1541 | } |
| 1542 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1543 | @Override public int[] getAppIdUserWhitelist() { |
| 1544 | return getAppIdUserWhitelistInternal(); |
| 1545 | } |
| 1546 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1547 | @Override public int[] getAppIdTempWhitelist() { |
| 1548 | return getAppIdTempWhitelistInternal(); |
| 1549 | } |
| 1550 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1551 | @Override public boolean isPowerSaveWhitelistExceptIdleApp(String name) { |
| 1552 | return isPowerSaveWhitelistExceptIdleAppInternal(name); |
| 1553 | } |
| 1554 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 1555 | @Override public boolean isPowerSaveWhitelistApp(String name) { |
| 1556 | return isPowerSaveWhitelistAppInternal(name); |
| 1557 | } |
| 1558 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1559 | @Override public void addPowerSaveTempWhitelistApp(String packageName, long duration, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1560 | int userId, String reason) throws RemoteException { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1561 | addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1564 | @Override public long addPowerSaveTempWhitelistAppForMms(String packageName, |
| 1565 | int userId, String reason) throws RemoteException { |
| 1566 | long duration = mConstants.MMS_TEMP_APP_WHITELIST_DURATION; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1567 | addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1568 | return duration; |
| 1569 | } |
| 1570 | |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 1571 | @Override public long addPowerSaveTempWhitelistAppForSms(String packageName, |
| 1572 | int userId, String reason) throws RemoteException { |
| 1573 | long duration = mConstants.SMS_TEMP_APP_WHITELIST_DURATION; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1574 | addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason); |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 1575 | return duration; |
| 1576 | } |
| 1577 | |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1578 | @Override public void exitIdle(String reason) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1579 | getContext().enforceCallingOrSelfPermission(Manifest.permission.DEVICE_POWER, |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1580 | null); |
Dianne Hackborn | c9b2cd1 | 2016-01-20 10:54:29 -0800 | [diff] [blame] | 1581 | long ident = Binder.clearCallingIdentity(); |
| 1582 | try { |
| 1583 | exitIdleInternal(reason); |
| 1584 | } finally { |
| 1585 | Binder.restoreCallingIdentity(ident); |
| 1586 | } |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 1589 | @Override public boolean registerMaintenanceActivityListener( |
| 1590 | IMaintenanceActivityListener listener) { |
| 1591 | return DeviceIdleController.this.registerMaintenanceActivityListener(listener); |
| 1592 | } |
| 1593 | |
| 1594 | @Override public void unregisterMaintenanceActivityListener( |
| 1595 | IMaintenanceActivityListener listener) { |
| 1596 | DeviceIdleController.this.unregisterMaintenanceActivityListener(listener); |
| 1597 | } |
| 1598 | |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1599 | @Override public int setPreIdleTimeoutMode(int mode) { |
| 1600 | getContext().enforceCallingOrSelfPermission(Manifest.permission.DEVICE_POWER, |
| 1601 | null); |
| 1602 | long ident = Binder.clearCallingIdentity(); |
| 1603 | try { |
| 1604 | return DeviceIdleController.this.setPreIdleTimeoutMode(mode); |
| 1605 | } finally { |
| 1606 | Binder.restoreCallingIdentity(ident); |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | @Override public void resetPreIdleTimeoutMode() { |
| 1611 | getContext().enforceCallingOrSelfPermission(Manifest.permission.DEVICE_POWER, |
| 1612 | null); |
| 1613 | long ident = Binder.clearCallingIdentity(); |
| 1614 | try { |
| 1615 | DeviceIdleController.this.resetPreIdleTimeoutMode(); |
| 1616 | } finally { |
| 1617 | Binder.restoreCallingIdentity(ident); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1621 | @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1622 | DeviceIdleController.this.dump(fd, pw, args); |
| 1623 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1624 | |
| 1625 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 1626 | FileDescriptor err, String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
| 1627 | (new Shell()).exec(this, in, out, err, args, callback, resultReceiver); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1628 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1631 | public class LocalService { |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1632 | public void onConstraintStateChanged(IDeviceIdleConstraint constraint, boolean active) { |
| 1633 | synchronized (DeviceIdleController.this) { |
| 1634 | onConstraintStateChangedLocked(constraint, active); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | public void registerDeviceIdleConstraint(IDeviceIdleConstraint constraint, String name, |
| 1639 | @IDeviceIdleConstraint.MinimumState int minState) { |
| 1640 | registerDeviceIdleConstraintInternal(constraint, name, minState); |
| 1641 | } |
| 1642 | |
| 1643 | public void unregisterDeviceIdleConstraint(IDeviceIdleConstraint constraint) { |
| 1644 | unregisterDeviceIdleConstraintInternal(constraint); |
| 1645 | } |
| 1646 | |
| 1647 | public void exitIdle(String reason) { |
| 1648 | exitIdleInternal(reason); |
| 1649 | } |
| 1650 | |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 1651 | // duration in milliseconds |
| 1652 | public void addPowerSaveTempWhitelistApp(int callingUid, String packageName, |
| 1653 | long duration, int userId, boolean sync, String reason) { |
| 1654 | addPowerSaveTempWhitelistAppInternal(callingUid, packageName, duration, |
| 1655 | userId, sync, reason); |
| 1656 | } |
| 1657 | |
| 1658 | // duration in milliseconds |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 1659 | public void addPowerSaveTempWhitelistAppDirect(int uid, long duration, boolean sync, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1660 | String reason) { |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 1661 | addPowerSaveTempWhitelistAppDirectInternal(0, uid, duration, sync, reason); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Christopher Tate | e0be7e8 | 2017-02-08 17:38:20 -0800 | [diff] [blame] | 1664 | // duration in milliseconds |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1665 | public long getNotificationWhitelistDuration() { |
| 1666 | return mConstants.NOTIFICATION_WHITELIST_DURATION; |
| 1667 | } |
| 1668 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1669 | public void setJobsActive(boolean active) { |
| 1670 | DeviceIdleController.this.setJobsActive(active); |
| 1671 | } |
| 1672 | |
| 1673 | // Up-call from alarm manager. |
| 1674 | public void setAlarmsActive(boolean active) { |
| 1675 | DeviceIdleController.this.setAlarmsActive(active); |
| 1676 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1677 | |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1678 | /** Is the app on any of the power save whitelists, whether system or user? */ |
| 1679 | public boolean isAppOnWhitelist(int appid) { |
| 1680 | return DeviceIdleController.this.isAppOnWhitelistInternal(appid); |
| 1681 | } |
| 1682 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1683 | /** |
| 1684 | * Returns the array of app ids whitelisted by user. Take care not to |
| 1685 | * modify this, as it is a reference to the original copy. But the reference |
| 1686 | * can change when the list changes, so it needs to be re-acquired when |
| 1687 | * {@link PowerManager#ACTION_POWER_SAVE_WHITELIST_CHANGED} is sent. |
| 1688 | */ |
| 1689 | public int[] getPowerSaveWhitelistUserAppIds() { |
| 1690 | return DeviceIdleController.this.getPowerSaveWhitelistUserAppIds(); |
| 1691 | } |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 1692 | |
| 1693 | public int[] getPowerSaveTempWhitelistAppIds() { |
| 1694 | return DeviceIdleController.this.getAppIdTempWhitelistInternal(); |
| 1695 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1698 | static class Injector { |
| 1699 | private final Context mContext; |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1700 | private ConnectivityService mConnectivityService; |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 1701 | private Constants mConstants; |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1702 | private LocationManager mLocationManager; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1703 | |
| 1704 | Injector(Context ctx) { |
| 1705 | mContext = ctx; |
| 1706 | } |
| 1707 | |
| 1708 | AlarmManager getAlarmManager() { |
| 1709 | return mContext.getSystemService(AlarmManager.class); |
| 1710 | } |
| 1711 | |
| 1712 | AnyMotionDetector getAnyMotionDetector(Handler handler, SensorManager sm, |
| 1713 | AnyMotionDetector.DeviceIdleCallback callback, float angleThreshold) { |
| 1714 | return new AnyMotionDetector(getPowerManager(), handler, sm, callback, angleThreshold); |
| 1715 | } |
| 1716 | |
| 1717 | AppStateTracker getAppStateTracker(Context ctx, Looper looper) { |
| 1718 | return new AppStateTracker(ctx, looper); |
| 1719 | } |
| 1720 | |
| 1721 | ConnectivityService getConnectivityService() { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1722 | if (mConnectivityService == null) { |
| 1723 | mConnectivityService = (ConnectivityService) ServiceManager.getService( |
| 1724 | Context.CONNECTIVITY_SERVICE); |
| 1725 | } |
| 1726 | return mConnectivityService; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1729 | Constants getConstants(DeviceIdleController controller, Handler handler, |
| 1730 | ContentResolver resolver) { |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 1731 | if (mConstants == null) { |
| 1732 | mConstants = controller.new Constants(handler, resolver); |
| 1733 | } |
| 1734 | return mConstants; |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1737 | LocationManager getLocationManager() { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1738 | if (mLocationManager == null) { |
| 1739 | mLocationManager = mContext.getSystemService(LocationManager.class); |
| 1740 | } |
| 1741 | return mLocationManager; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1744 | MyHandler getHandler(DeviceIdleController controller) { |
| 1745 | return controller.new MyHandler(BackgroundThread.getHandler().getLooper()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | PowerManager getPowerManager() { |
| 1749 | return mContext.getSystemService(PowerManager.class); |
| 1750 | } |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1751 | |
| 1752 | SensorManager getSensorManager() { |
| 1753 | return mContext.getSystemService(SensorManager.class); |
| 1754 | } |
| 1755 | |
| 1756 | ConstraintController getConstraintController(Handler handler, LocalService localService) { |
| 1757 | if (mContext.getPackageManager() |
| 1758 | .hasSystemFeature(PackageManager.FEATURE_LEANBACK_ONLY)) { |
| 1759 | return new TvConstraintController(mContext, handler); |
| 1760 | } |
| 1761 | return null; |
| 1762 | } |
| 1763 | |
| 1764 | boolean useMotionSensor() { |
| 1765 | return mContext.getResources().getBoolean( |
| 1766 | com.android.internal.R.bool.config_autoPowerModeUseMotionSensor); |
| 1767 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | private final Injector mInjector; |
| 1771 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 1772 | private ActivityTaskManagerInternal.ScreenObserver mScreenObserver = |
| 1773 | new ActivityTaskManagerInternal.ScreenObserver() { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1774 | @Override |
| 1775 | public void onAwakeStateChanged(boolean isAwake) { } |
| 1776 | |
| 1777 | @Override |
| 1778 | public void onKeyguardStateChanged(boolean isShowing) { |
| 1779 | synchronized (DeviceIdleController.this) { |
| 1780 | DeviceIdleController.this.keyguardShowingLocked(isShowing); |
| 1781 | } |
| 1782 | } |
| 1783 | }; |
| 1784 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1785 | @VisibleForTesting DeviceIdleController(Context context, Injector injector) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1786 | super(context); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1787 | mInjector = injector; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1788 | mConfigFile = new AtomicFile(new File(getSystemDir(), "deviceidle.xml")); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1789 | mHandler = mInjector.getHandler(this); |
| 1790 | mAppStateTracker = mInjector.getAppStateTracker(context, FgThread.get().getLooper()); |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 1791 | LocalServices.addService(AppStateTracker.class, mAppStateTracker); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1792 | mUseMotionSensor = mInjector.useMotionSensor(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1795 | public DeviceIdleController(Context context) { |
| 1796 | this(context, new Injector(context)); |
| 1797 | } |
| 1798 | |
Christopher Tate | 42a386b | 2016-11-07 12:21:21 -0800 | [diff] [blame] | 1799 | boolean isAppOnWhitelistInternal(int appid) { |
| 1800 | synchronized (this) { |
| 1801 | return Arrays.binarySearch(mPowerSaveWhitelistAllAppIdArray, appid) >= 0; |
| 1802 | } |
| 1803 | } |
| 1804 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1805 | int[] getPowerSaveWhitelistUserAppIds() { |
| 1806 | synchronized (this) { |
| 1807 | return mPowerSaveWhitelistUserAppIdArray; |
| 1808 | } |
| 1809 | } |
| 1810 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1811 | private static File getSystemDir() { |
| 1812 | return new File(Environment.getDataDirectory(), "system"); |
| 1813 | } |
| 1814 | |
| 1815 | @Override |
| 1816 | public void onStart() { |
| 1817 | final PackageManager pm = getContext().getPackageManager(); |
| 1818 | |
| 1819 | synchronized (this) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1820 | mLightEnabled = mDeepEnabled = getContext().getResources().getBoolean( |
Dianne Hackborn | 9261703 | 2015-06-19 15:32:19 -0700 | [diff] [blame] | 1821 | com.android.internal.R.bool.config_enableAutoPowerModes); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1822 | SystemConfig sysConfig = SystemConfig.getInstance(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1823 | ArraySet<String> allowPowerExceptIdle = sysConfig.getAllowInPowerSaveExceptIdle(); |
| 1824 | for (int i=0; i<allowPowerExceptIdle.size(); i++) { |
| 1825 | String pkg = allowPowerExceptIdle.valueAt(i); |
| 1826 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 1827 | ApplicationInfo ai = pm.getApplicationInfo(pkg, |
| 1828 | PackageManager.MATCH_SYSTEM_ONLY); |
| 1829 | int appid = UserHandle.getAppId(ai.uid); |
| 1830 | mPowerSaveWhitelistAppsExceptIdle.put(ai.packageName, appid); |
| 1831 | mPowerSaveWhitelistSystemAppIdsExceptIdle.put(appid, true); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1832 | } catch (PackageManager.NameNotFoundException e) { |
| 1833 | } |
| 1834 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1835 | ArraySet<String> allowPower = sysConfig.getAllowInPowerSave(); |
| 1836 | for (int i=0; i<allowPower.size(); i++) { |
| 1837 | String pkg = allowPower.valueAt(i); |
| 1838 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 1839 | ApplicationInfo ai = pm.getApplicationInfo(pkg, |
| 1840 | PackageManager.MATCH_SYSTEM_ONLY); |
| 1841 | int appid = UserHandle.getAppId(ai.uid); |
| 1842 | // These apps are on both the whitelist-except-idle as well |
| 1843 | // as the full whitelist, so they apply in all cases. |
| 1844 | mPowerSaveWhitelistAppsExceptIdle.put(ai.packageName, appid); |
| 1845 | mPowerSaveWhitelistSystemAppIdsExceptIdle.put(appid, true); |
| 1846 | mPowerSaveWhitelistApps.put(ai.packageName, appid); |
| 1847 | mPowerSaveWhitelistSystemAppIds.put(appid, true); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1848 | } catch (PackageManager.NameNotFoundException e) { |
| 1849 | } |
| 1850 | } |
| 1851 | |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1852 | mConstants = mInjector.getConstants(this, mHandler, getContext().getContentResolver()); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1853 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1854 | readConfigFileLocked(); |
| 1855 | updateWhitelistAppIdsLocked(); |
| 1856 | |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1857 | mNetworkConnected = true; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1858 | mScreenOn = true; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1859 | mScreenLocked = false; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1860 | // Start out assuming we are charging. If we aren't, we will at least get |
| 1861 | // a battery update the next time the level drops. |
| 1862 | mCharging = true; |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1863 | mActiveReason = ACTIVE_REASON_UNKNOWN; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1864 | mState = STATE_ACTIVE; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1865 | mLightState = LIGHT_STATE_ACTIVE; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1866 | mInactiveTimeout = mConstants.INACTIVE_TIMEOUT; |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1867 | mPreIdleFactor = 1.0f; |
| 1868 | mLastPreIdleFactor = 1.0f; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1871 | mBinderService = new BinderService(); |
| 1872 | publishBinderService(Context.DEVICE_IDLE_CONTROLLER, mBinderService); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1873 | publishLocalService(LocalService.class, new LocalService()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | @Override |
| 1877 | public void onBootPhase(int phase) { |
| 1878 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
| 1879 | synchronized (this) { |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1880 | mAlarmManager = mInjector.getAlarmManager(); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 1881 | mLocalAlarmManager = getLocalService(AlarmManagerInternal.class); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1882 | mBatteryStats = BatteryStatsService.getService(); |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 1883 | mLocalActivityManager = getLocalService(ActivityManagerInternal.class); |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 1884 | mLocalActivityTaskManager = getLocalService(ActivityTaskManagerInternal.class); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1885 | mLocalPowerManager = getLocalService(PowerManagerInternal.class); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1886 | mPowerManager = mInjector.getPowerManager(); |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1887 | mActiveIdleWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 1888 | "deviceidle_maint"); |
| 1889 | mActiveIdleWakeLock.setReferenceCounted(false); |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 1890 | mGoingIdleWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 1891 | "deviceidle_going_idle"); |
| 1892 | mGoingIdleWakeLock.setReferenceCounted(true); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1893 | mNetworkPolicyManager = INetworkPolicyManager.Stub.asInterface( |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1894 | ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 1895 | mNetworkPolicyManagerInternal = getLocalService(NetworkPolicyManagerInternal.class); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1896 | mSensorManager = mInjector.getSensorManager(); |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 1897 | |
| 1898 | if (mUseMotionSensor) { |
| 1899 | int sigMotionSensorId = getContext().getResources().getInteger( |
| 1900 | com.android.internal.R.integer.config_autoPowerModeAnyMotionSensor); |
| 1901 | if (sigMotionSensorId > 0) { |
| 1902 | mMotionSensor = mSensorManager.getDefaultSensor(sigMotionSensorId, true); |
| 1903 | } |
| 1904 | if (mMotionSensor == null && getContext().getResources().getBoolean( |
| 1905 | com.android.internal.R.bool.config_autoPowerModePreferWristTilt)) { |
| 1906 | mMotionSensor = mSensorManager.getDefaultSensor( |
| 1907 | Sensor.TYPE_WRIST_TILT_GESTURE, true); |
| 1908 | } |
| 1909 | if (mMotionSensor == null) { |
| 1910 | // As a last ditch, fall back to SMD. |
| 1911 | mMotionSensor = mSensorManager.getDefaultSensor( |
| 1912 | Sensor.TYPE_SIGNIFICANT_MOTION, true); |
| 1913 | } |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1914 | } |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1915 | |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1916 | if (getContext().getResources().getBoolean( |
| 1917 | com.android.internal.R.bool.config_autoPowerModePrefetchLocation)) { |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1918 | mLocationRequest = new LocationRequest() |
| 1919 | .setQuality(LocationRequest.ACCURACY_FINE) |
| 1920 | .setInterval(0) |
| 1921 | .setFastestInterval(0) |
| 1922 | .setNumUpdates(1); |
| 1923 | } |
| 1924 | |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1925 | mConstraintController = mInjector.getConstraintController( |
| 1926 | mHandler, getLocalService(LocalService.class)); |
| 1927 | if (mConstraintController != null) { |
| 1928 | mConstraintController.start(); |
| 1929 | } |
| 1930 | |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1931 | float angleThreshold = getContext().getResources().getInteger( |
| 1932 | com.android.internal.R.integer.config_autoPowerModeThresholdAngle) / 100f; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1933 | mAnyMotionDetector = mInjector.getAnyMotionDetector(mHandler, mSensorManager, this, |
| 1934 | angleThreshold); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1935 | |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 1936 | mAppStateTracker.onSystemServicesReady(); |
| 1937 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1938 | mIdleIntent = new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1939 | mIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1940 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1941 | mLightIdleIntent = new Intent(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED); |
| 1942 | mLightIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1943 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1944 | |
| 1945 | IntentFilter filter = new IntentFilter(); |
| 1946 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1947 | getContext().registerReceiver(mReceiver, filter); |
Amith Yamasani | ac59f75e | 2016-05-05 12:38:17 -0700 | [diff] [blame] | 1948 | |
Dianne Hackborn | 1b79ad7 | 2015-10-12 10:59:47 -0700 | [diff] [blame] | 1949 | filter = new IntentFilter(); |
| 1950 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1951 | filter.addDataScheme("package"); |
Amith Yamasani | ac59f75e | 2016-05-05 12:38:17 -0700 | [diff] [blame] | 1952 | getContext().registerReceiver(mReceiver, filter); |
| 1953 | |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1954 | filter = new IntentFilter(); |
| 1955 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
Dianne Hackborn | 1b79ad7 | 2015-10-12 10:59:47 -0700 | [diff] [blame] | 1956 | getContext().registerReceiver(mReceiver, filter); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1957 | |
Dianne Hackborn | 3dba8ce | 2017-09-01 17:07:04 -0700 | [diff] [blame] | 1958 | filter = new IntentFilter(); |
| 1959 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 1960 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1961 | getContext().registerReceiver(mInteractivityReceiver, filter); |
| 1962 | |
Makoto Onuki | af8ff4f | 2018-06-04 14:44:19 -0700 | [diff] [blame] | 1963 | mLocalActivityManager.setDeviceIdleWhitelist( |
| 1964 | mPowerSaveWhitelistAllAppIdArray, mPowerSaveWhitelistExceptIdleAppIdArray); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1965 | mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray); |
Dianne Hackborn | 3dba8ce | 2017-09-01 17:07:04 -0700 | [diff] [blame] | 1966 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1967 | mLocalPowerManager.registerLowPowerModeObserver(ServiceType.QUICK_DOZE, |
| 1968 | state -> { |
| 1969 | synchronized (DeviceIdleController.this) { |
| 1970 | updateQuickDozeFlagLocked(state.batterySaverEnabled); |
| 1971 | } |
| 1972 | }); |
| 1973 | updateQuickDozeFlagLocked( |
| 1974 | mLocalPowerManager.getLowPowerState( |
| 1975 | ServiceType.QUICK_DOZE).batterySaverEnabled); |
| 1976 | |
Wale Ogunwale | 6767eae | 2018-05-03 15:52:51 -0700 | [diff] [blame] | 1977 | mLocalActivityTaskManager.registerScreenObserver(mScreenObserver); |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1978 | |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 1979 | passWhiteListsToForceAppStandbyTrackerLocked(); |
Dianne Hackborn | 3dba8ce | 2017-09-01 17:07:04 -0700 | [diff] [blame] | 1980 | updateInteractivityLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1981 | } |
Dianne Hackborn | 4cb96ca | 2016-05-17 13:55:29 -0700 | [diff] [blame] | 1982 | updateConnectivityState(null); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1983 | } |
| 1984 | } |
| 1985 | |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 1986 | @VisibleForTesting |
| 1987 | boolean hasMotionSensor() { |
| 1988 | return mUseMotionSensor && mMotionSensor != null; |
| 1989 | } |
| 1990 | |
| 1991 | private void registerDeviceIdleConstraintInternal(IDeviceIdleConstraint constraint, |
| 1992 | final String name, final int type) { |
| 1993 | final int minState; |
| 1994 | switch (type) { |
| 1995 | case IDeviceIdleConstraint.ACTIVE: |
| 1996 | minState = STATE_ACTIVE; |
| 1997 | break; |
| 1998 | case IDeviceIdleConstraint.SENSING_OR_ABOVE: |
| 1999 | minState = STATE_SENSING; |
| 2000 | break; |
| 2001 | default: |
| 2002 | Slog.wtf(TAG, "Registering device-idle constraint with invalid type: " + type); |
| 2003 | return; |
| 2004 | } |
| 2005 | synchronized (this) { |
| 2006 | if (mConstraints.containsKey(constraint)) { |
| 2007 | Slog.e(TAG, "Re-registering device-idle constraint: " + constraint + "."); |
| 2008 | return; |
| 2009 | } |
| 2010 | DeviceIdleConstraintTracker tracker = new DeviceIdleConstraintTracker(name, minState); |
| 2011 | mConstraints.put(constraint, tracker); |
| 2012 | updateActiveConstraintsLocked(); |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | private void unregisterDeviceIdleConstraintInternal(IDeviceIdleConstraint constraint) { |
| 2017 | synchronized (this) { |
| 2018 | // Artifically force the constraint to inactive to unblock anything waiting for it. |
| 2019 | onConstraintStateChangedLocked(constraint, /* active= */ false); |
| 2020 | |
| 2021 | // Let the constraint know that we are not listening to it any more. |
| 2022 | setConstraintMonitoringLocked(constraint, /* monitoring= */ false); |
| 2023 | mConstraints.remove(constraint); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | @GuardedBy("this") |
| 2028 | private void onConstraintStateChangedLocked(IDeviceIdleConstraint constraint, boolean active) { |
| 2029 | DeviceIdleConstraintTracker tracker = mConstraints.get(constraint); |
| 2030 | if (tracker == null) { |
| 2031 | Slog.e(TAG, "device-idle constraint " + constraint + " has not been registered."); |
| 2032 | return; |
| 2033 | } |
| 2034 | if (active != tracker.active && tracker.monitoring) { |
| 2035 | tracker.active = active; |
| 2036 | mNumBlockingConstraints += (tracker.active ? +1 : -1); |
| 2037 | if (mNumBlockingConstraints == 0) { |
| 2038 | if (mState == STATE_ACTIVE) { |
| 2039 | becomeInactiveIfAppropriateLocked(); |
| 2040 | } else if (mNextAlarmTime == 0 || mNextAlarmTime < SystemClock.elapsedRealtime()) { |
| 2041 | stepIdleStateLocked("s:" + tracker.name); |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | @GuardedBy("this") |
| 2048 | private void setConstraintMonitoringLocked(IDeviceIdleConstraint constraint, boolean monitor) { |
| 2049 | DeviceIdleConstraintTracker tracker = mConstraints.get(constraint); |
| 2050 | if (tracker.monitoring != monitor) { |
| 2051 | tracker.monitoring = monitor; |
| 2052 | updateActiveConstraintsLocked(); |
| 2053 | // We send the callback on a separate thread instead of just relying on oneway as |
| 2054 | // the client could be in the system server with us and cause re-entry problems. |
| 2055 | mHandler.obtainMessage(MSG_SEND_CONSTRAINT_MONITORING, |
| 2056 | /* monitoring= */ monitor ? 1 : 0, |
| 2057 | /* <not used>= */ -1, |
| 2058 | /* constraint= */ constraint).sendToTarget(); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | @GuardedBy("this") |
| 2063 | private void updateActiveConstraintsLocked() { |
| 2064 | mNumBlockingConstraints = 0; |
| 2065 | for (int i = 0; i < mConstraints.size(); i++) { |
| 2066 | final IDeviceIdleConstraint constraint = mConstraints.keyAt(i); |
| 2067 | final DeviceIdleConstraintTracker tracker = mConstraints.valueAt(i); |
| 2068 | final boolean monitoring = (tracker.minState == mState); |
| 2069 | if (monitoring != tracker.monitoring) { |
| 2070 | setConstraintMonitoringLocked(constraint, monitoring); |
| 2071 | tracker.active = monitoring; |
| 2072 | } |
| 2073 | if (tracker.monitoring && tracker.active) { |
| 2074 | mNumBlockingConstraints++; |
| 2075 | } |
| 2076 | } |
| 2077 | } |
| 2078 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2079 | public boolean addPowerSaveWhitelistAppInternal(String name) { |
| 2080 | synchronized (this) { |
| 2081 | try { |
Dianne Hackborn | 1b79ad7 | 2015-10-12 10:59:47 -0700 | [diff] [blame] | 2082 | ApplicationInfo ai = getContext().getPackageManager().getApplicationInfo(name, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 2083 | PackageManager.MATCH_ANY_USER); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2084 | if (mPowerSaveWhitelistUserApps.put(name, UserHandle.getAppId(ai.uid)) == null) { |
| 2085 | reportPowerSaveWhitelistChangedLocked(); |
| 2086 | updateWhitelistAppIdsLocked(); |
| 2087 | writeConfigFileLocked(); |
| 2088 | } |
| 2089 | return true; |
| 2090 | } catch (PackageManager.NameNotFoundException e) { |
| 2091 | return false; |
| 2092 | } |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | public boolean removePowerSaveWhitelistAppInternal(String name) { |
| 2097 | synchronized (this) { |
| 2098 | if (mPowerSaveWhitelistUserApps.remove(name) != null) { |
| 2099 | reportPowerSaveWhitelistChangedLocked(); |
| 2100 | updateWhitelistAppIdsLocked(); |
| 2101 | writeConfigFileLocked(); |
| 2102 | return true; |
| 2103 | } |
| 2104 | } |
| 2105 | return false; |
| 2106 | } |
| 2107 | |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 2108 | public boolean getPowerSaveWhitelistAppInternal(String name) { |
| 2109 | synchronized (this) { |
| 2110 | return mPowerSaveWhitelistUserApps.containsKey(name); |
| 2111 | } |
| 2112 | } |
| 2113 | |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 2114 | void resetSystemPowerWhitelistInternal() { |
| 2115 | synchronized (this) { |
| 2116 | mPowerSaveWhitelistApps.putAll(mRemovedFromSystemWhitelistApps); |
| 2117 | mRemovedFromSystemWhitelistApps.clear(); |
| 2118 | reportPowerSaveWhitelistChangedLocked(); |
| 2119 | updateWhitelistAppIdsLocked(); |
| 2120 | writeConfigFileLocked(); |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | public boolean restoreSystemPowerWhitelistAppInternal(String name) { |
| 2125 | synchronized (this) { |
| 2126 | if (!mRemovedFromSystemWhitelistApps.containsKey(name)) { |
| 2127 | return false; |
| 2128 | } |
| 2129 | mPowerSaveWhitelistApps.put(name, mRemovedFromSystemWhitelistApps.remove(name)); |
| 2130 | reportPowerSaveWhitelistChangedLocked(); |
| 2131 | updateWhitelistAppIdsLocked(); |
| 2132 | writeConfigFileLocked(); |
| 2133 | return true; |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | public boolean removeSystemPowerWhitelistAppInternal(String name) { |
| 2138 | synchronized (this) { |
| 2139 | if (!mPowerSaveWhitelistApps.containsKey(name)) { |
| 2140 | return false; |
| 2141 | } |
| 2142 | mRemovedFromSystemWhitelistApps.put(name, mPowerSaveWhitelistApps.remove(name)); |
| 2143 | reportPowerSaveWhitelistChangedLocked(); |
| 2144 | updateWhitelistAppIdsLocked(); |
| 2145 | writeConfigFileLocked(); |
| 2146 | return true; |
| 2147 | } |
| 2148 | } |
| 2149 | |
Sudheer Shanka | 3f4d770 | 2017-04-28 17:38:03 -0700 | [diff] [blame] | 2150 | public boolean addPowerSaveWhitelistExceptIdleInternal(String name) { |
| 2151 | synchronized (this) { |
| 2152 | try { |
| 2153 | final ApplicationInfo ai = getContext().getPackageManager().getApplicationInfo(name, |
| 2154 | PackageManager.MATCH_ANY_USER); |
| 2155 | if (mPowerSaveWhitelistAppsExceptIdle.put(name, UserHandle.getAppId(ai.uid)) |
| 2156 | == null) { |
| 2157 | mPowerSaveWhitelistUserAppsExceptIdle.add(name); |
| 2158 | reportPowerSaveWhitelistChangedLocked(); |
| 2159 | mPowerSaveWhitelistExceptIdleAppIdArray = buildAppIdArray( |
| 2160 | mPowerSaveWhitelistAppsExceptIdle, mPowerSaveWhitelistUserApps, |
| 2161 | mPowerSaveWhitelistExceptIdleAppIds); |
Makoto Onuki | 71755c9 | 2018-01-16 14:15:44 -0800 | [diff] [blame] | 2162 | |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 2163 | passWhiteListsToForceAppStandbyTrackerLocked(); |
Sudheer Shanka | 3f4d770 | 2017-04-28 17:38:03 -0700 | [diff] [blame] | 2164 | } |
| 2165 | return true; |
| 2166 | } catch (PackageManager.NameNotFoundException e) { |
| 2167 | return false; |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | public void resetPowerSaveWhitelistExceptIdleInternal() { |
| 2173 | synchronized (this) { |
| 2174 | if (mPowerSaveWhitelistAppsExceptIdle.removeAll( |
| 2175 | mPowerSaveWhitelistUserAppsExceptIdle)) { |
| 2176 | reportPowerSaveWhitelistChangedLocked(); |
| 2177 | mPowerSaveWhitelistExceptIdleAppIdArray = buildAppIdArray( |
| 2178 | mPowerSaveWhitelistAppsExceptIdle, mPowerSaveWhitelistUserApps, |
| 2179 | mPowerSaveWhitelistExceptIdleAppIds); |
| 2180 | mPowerSaveWhitelistUserAppsExceptIdle.clear(); |
Makoto Onuki | 71755c9 | 2018-01-16 14:15:44 -0800 | [diff] [blame] | 2181 | |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 2182 | passWhiteListsToForceAppStandbyTrackerLocked(); |
Sudheer Shanka | 3f4d770 | 2017-04-28 17:38:03 -0700 | [diff] [blame] | 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | public boolean getPowerSaveWhitelistExceptIdleInternal(String name) { |
| 2188 | synchronized (this) { |
| 2189 | return mPowerSaveWhitelistAppsExceptIdle.containsKey(name); |
| 2190 | } |
| 2191 | } |
| 2192 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2193 | public String[] getSystemPowerWhitelistExceptIdleInternal() { |
| 2194 | synchronized (this) { |
| 2195 | int size = mPowerSaveWhitelistAppsExceptIdle.size(); |
| 2196 | String[] apps = new String[size]; |
| 2197 | for (int i = 0; i < size; i++) { |
| 2198 | apps[i] = mPowerSaveWhitelistAppsExceptIdle.keyAt(i); |
| 2199 | } |
| 2200 | return apps; |
| 2201 | } |
| 2202 | } |
| 2203 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2204 | public String[] getSystemPowerWhitelistInternal() { |
| 2205 | synchronized (this) { |
| 2206 | int size = mPowerSaveWhitelistApps.size(); |
| 2207 | String[] apps = new String[size]; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2208 | for (int i = 0; i < size; i++) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2209 | apps[i] = mPowerSaveWhitelistApps.keyAt(i); |
| 2210 | } |
| 2211 | return apps; |
| 2212 | } |
| 2213 | } |
| 2214 | |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 2215 | public String[] getRemovedSystemPowerWhitelistAppsInternal() { |
| 2216 | synchronized (this) { |
| 2217 | int size = mRemovedFromSystemWhitelistApps.size(); |
| 2218 | final String[] apps = new String[size]; |
| 2219 | for (int i = 0; i < size; i++) { |
| 2220 | apps[i] = mRemovedFromSystemWhitelistApps.keyAt(i); |
| 2221 | } |
| 2222 | return apps; |
| 2223 | } |
| 2224 | } |
| 2225 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2226 | public String[] getUserPowerWhitelistInternal() { |
| 2227 | synchronized (this) { |
| 2228 | int size = mPowerSaveWhitelistUserApps.size(); |
| 2229 | String[] apps = new String[size]; |
| 2230 | for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) { |
| 2231 | apps[i] = mPowerSaveWhitelistUserApps.keyAt(i); |
| 2232 | } |
| 2233 | return apps; |
| 2234 | } |
| 2235 | } |
| 2236 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2237 | public String[] getFullPowerWhitelistExceptIdleInternal() { |
| 2238 | synchronized (this) { |
| 2239 | int size = mPowerSaveWhitelistAppsExceptIdle.size() + mPowerSaveWhitelistUserApps.size(); |
| 2240 | String[] apps = new String[size]; |
| 2241 | int cur = 0; |
| 2242 | for (int i = 0; i < mPowerSaveWhitelistAppsExceptIdle.size(); i++) { |
| 2243 | apps[cur] = mPowerSaveWhitelistAppsExceptIdle.keyAt(i); |
| 2244 | cur++; |
| 2245 | } |
| 2246 | for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) { |
| 2247 | apps[cur] = mPowerSaveWhitelistUserApps.keyAt(i); |
| 2248 | cur++; |
| 2249 | } |
| 2250 | return apps; |
| 2251 | } |
| 2252 | } |
| 2253 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2254 | public String[] getFullPowerWhitelistInternal() { |
| 2255 | synchronized (this) { |
| 2256 | int size = mPowerSaveWhitelistApps.size() + mPowerSaveWhitelistUserApps.size(); |
| 2257 | String[] apps = new String[size]; |
| 2258 | int cur = 0; |
| 2259 | for (int i = 0; i < mPowerSaveWhitelistApps.size(); i++) { |
| 2260 | apps[cur] = mPowerSaveWhitelistApps.keyAt(i); |
| 2261 | cur++; |
| 2262 | } |
| 2263 | for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) { |
| 2264 | apps[cur] = mPowerSaveWhitelistUserApps.keyAt(i); |
| 2265 | cur++; |
| 2266 | } |
| 2267 | return apps; |
| 2268 | } |
| 2269 | } |
| 2270 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2271 | public boolean isPowerSaveWhitelistExceptIdleAppInternal(String packageName) { |
| 2272 | synchronized (this) { |
| 2273 | return mPowerSaveWhitelistAppsExceptIdle.containsKey(packageName) |
| 2274 | || mPowerSaveWhitelistUserApps.containsKey(packageName); |
| 2275 | } |
| 2276 | } |
| 2277 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 2278 | public boolean isPowerSaveWhitelistAppInternal(String packageName) { |
| 2279 | synchronized (this) { |
| 2280 | return mPowerSaveWhitelistApps.containsKey(packageName) |
| 2281 | || mPowerSaveWhitelistUserApps.containsKey(packageName); |
| 2282 | } |
| 2283 | } |
| 2284 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2285 | public int[] getAppIdWhitelistExceptIdleInternal() { |
| 2286 | synchronized (this) { |
| 2287 | return mPowerSaveWhitelistExceptIdleAppIdArray; |
| 2288 | } |
| 2289 | } |
| 2290 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2291 | public int[] getAppIdWhitelistInternal() { |
| 2292 | synchronized (this) { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2293 | return mPowerSaveWhitelistAllAppIdArray; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2294 | } |
| 2295 | } |
| 2296 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2297 | public int[] getAppIdUserWhitelistInternal() { |
| 2298 | synchronized (this) { |
| 2299 | return mPowerSaveWhitelistUserAppIdArray; |
| 2300 | } |
| 2301 | } |
| 2302 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2303 | public int[] getAppIdTempWhitelistInternal() { |
| 2304 | synchronized (this) { |
| 2305 | return mTempWhitelistAppIdArray; |
| 2306 | } |
| 2307 | } |
| 2308 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2309 | void addPowerSaveTempWhitelistAppChecked(String packageName, long duration, |
| 2310 | int userId, String reason) throws RemoteException { |
| 2311 | getContext().enforceCallingPermission( |
| 2312 | Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST, |
| 2313 | "No permission to change device idle whitelist"); |
| 2314 | final int callingUid = Binder.getCallingUid(); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 2315 | userId = ActivityManager.getService().handleIncomingUser( |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2316 | Binder.getCallingPid(), |
| 2317 | callingUid, |
| 2318 | userId, |
| 2319 | /*allowAll=*/ false, |
| 2320 | /*requireFull=*/ false, |
| 2321 | "addPowerSaveTempWhitelistApp", null); |
| 2322 | final long token = Binder.clearCallingIdentity(); |
| 2323 | try { |
| 2324 | addPowerSaveTempWhitelistAppInternal(callingUid, |
| 2325 | packageName, duration, userId, true, reason); |
| 2326 | } finally { |
| 2327 | Binder.restoreCallingIdentity(token); |
| 2328 | } |
| 2329 | } |
| 2330 | |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 2331 | void removePowerSaveTempWhitelistAppChecked(String packageName, int userId) |
| 2332 | throws RemoteException { |
| 2333 | getContext().enforceCallingPermission( |
| 2334 | Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST, |
| 2335 | "No permission to change device idle whitelist"); |
| 2336 | final int callingUid = Binder.getCallingUid(); |
| 2337 | userId = ActivityManager.getService().handleIncomingUser( |
| 2338 | Binder.getCallingPid(), |
| 2339 | callingUid, |
| 2340 | userId, |
| 2341 | /*allowAll=*/ false, |
| 2342 | /*requireFull=*/ false, |
| 2343 | "removePowerSaveTempWhitelistApp", null); |
| 2344 | final long token = Binder.clearCallingIdentity(); |
| 2345 | try { |
| 2346 | removePowerSaveTempWhitelistAppInternal(packageName, userId); |
| 2347 | } finally { |
| 2348 | Binder.restoreCallingIdentity(token); |
| 2349 | } |
| 2350 | } |
| 2351 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2352 | /** |
| 2353 | * Adds an app to the temporary whitelist and resets the endTime for granting the |
| 2354 | * app an exemption to access network and acquire wakelocks. |
| 2355 | */ |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2356 | void addPowerSaveTempWhitelistAppInternal(int callingUid, String packageName, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2357 | long duration, int userId, boolean sync, String reason) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2358 | try { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 2359 | int uid = getContext().getPackageManager().getPackageUidAsUser(packageName, userId); |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2360 | addPowerSaveTempWhitelistAppDirectInternal(callingUid, uid, duration, sync, reason); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2361 | } catch (NameNotFoundException e) { |
| 2362 | } |
| 2363 | } |
| 2364 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2365 | /** |
| 2366 | * Adds an app to the temporary whitelist and resets the endTime for granting the |
| 2367 | * app an exemption to access network and acquire wakelocks. |
| 2368 | */ |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2369 | void addPowerSaveTempWhitelistAppDirectInternal(int callingUid, int uid, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2370 | long duration, boolean sync, String reason) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2371 | final long timeNow = SystemClock.elapsedRealtime(); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 2372 | boolean informWhitelistChanged = false; |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2373 | int appId = UserHandle.getAppId(uid); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2374 | synchronized (this) { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2375 | int callingAppId = UserHandle.getAppId(callingUid); |
| 2376 | if (callingAppId >= Process.FIRST_APPLICATION_UID) { |
| 2377 | if (!mPowerSaveWhitelistSystemAppIds.get(callingAppId)) { |
| 2378 | throw new SecurityException("Calling app " + UserHandle.formatUid(callingUid) |
| 2379 | + " is not on whitelist"); |
| 2380 | } |
| 2381 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2382 | duration = Math.min(duration, mConstants.MAX_TEMP_APP_WHITELIST_DURATION); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2383 | Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.get(appId); |
| 2384 | final boolean newEntry = entry == null; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2385 | // Set the new end time |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2386 | if (newEntry) { |
| 2387 | entry = new Pair<>(new MutableLong(0), reason); |
| 2388 | mTempWhitelistAppIdEndTimes.put(appId, entry); |
| 2389 | } |
| 2390 | entry.first.value = timeNow + duration; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2391 | if (DEBUG) { |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2392 | Slog.d(TAG, "Adding AppId " + appId + " to temp whitelist. New entry: " + newEntry); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2393 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2394 | if (newEntry) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2395 | // No pending timeout for the app id, post a delayed message |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2396 | try { |
| 2397 | mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_START, |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2398 | reason, uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2399 | } catch (RemoteException e) { |
| 2400 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2401 | postTempActiveTimeoutMessage(appId, duration); |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 2402 | updateTempWhitelistAppIdsLocked(appId, true); |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 2403 | if (sync) { |
| 2404 | informWhitelistChanged = true; |
| 2405 | } else { |
| 2406 | mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_CHANGED, appId, 1) |
| 2407 | .sendToTarget(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2408 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2409 | reportTempWhitelistChangedLocked(); |
| 2410 | } |
| 2411 | } |
Sudheer Shanka | f34f3ec | 2017-08-03 11:02:56 -0700 | [diff] [blame] | 2412 | if (informWhitelistChanged) { |
| 2413 | mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, true); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2414 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 2417 | /** |
| 2418 | * Removes an app from the temporary whitelist and notifies the observers. |
| 2419 | */ |
| 2420 | private void removePowerSaveTempWhitelistAppInternal(String packageName, int userId) { |
| 2421 | try { |
| 2422 | final int uid = getContext().getPackageManager().getPackageUidAsUser( |
| 2423 | packageName, userId); |
| 2424 | final int appId = UserHandle.getAppId(uid); |
| 2425 | removePowerSaveTempWhitelistAppDirectInternal(appId); |
| 2426 | } catch (NameNotFoundException e) { |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | private void removePowerSaveTempWhitelistAppDirectInternal(int appId) { |
| 2431 | synchronized (this) { |
| 2432 | final int idx = mTempWhitelistAppIdEndTimes.indexOfKey(appId); |
| 2433 | if (idx < 0) { |
| 2434 | // Nothing else to do |
| 2435 | return; |
| 2436 | } |
| 2437 | final String reason = mTempWhitelistAppIdEndTimes.valueAt(idx).second; |
| 2438 | mTempWhitelistAppIdEndTimes.removeAt(idx); |
| 2439 | onAppRemovedFromTempWhitelistLocked(appId, reason); |
| 2440 | } |
| 2441 | } |
| 2442 | |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2443 | private void postTempActiveTimeoutMessage(int appId, long delay) { |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2444 | if (DEBUG) { |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2445 | Slog.d(TAG, "postTempActiveTimeoutMessage: appId=" + appId + ", delay=" + delay); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2446 | } |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2447 | mHandler.sendMessageDelayed( |
| 2448 | mHandler.obtainMessage(MSG_TEMP_APP_WHITELIST_TIMEOUT, appId, 0), delay); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2451 | void checkTempAppWhitelistTimeout(int appId) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2452 | final long timeNow = SystemClock.elapsedRealtime(); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2453 | if (DEBUG) { |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2454 | Slog.d(TAG, "checkTempAppWhitelistTimeout: appId=" + appId + ", timeNow=" + timeNow); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2455 | } |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2456 | synchronized (this) { |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2457 | Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.get(appId); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2458 | if (entry == null) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2459 | // Nothing to do |
| 2460 | return; |
| 2461 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2462 | if (timeNow >= entry.first.value) { |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2463 | mTempWhitelistAppIdEndTimes.delete(appId); |
| 2464 | onAppRemovedFromTempWhitelistLocked(appId, entry.second); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2465 | } else { |
| 2466 | // Need more time |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2467 | if (DEBUG) { |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2468 | Slog.d(TAG, "Time to remove AppId " + appId + ": " + entry.first.value); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2469 | } |
Nicholas Sauer | 7d2287b | 2019-04-11 11:16:34 -0700 | [diff] [blame] | 2470 | postTempActiveTimeoutMessage(appId, entry.first.value - timeNow); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 2475 | @GuardedBy("this") |
| 2476 | private void onAppRemovedFromTempWhitelistLocked(int appId, String reason) { |
| 2477 | if (DEBUG) { |
| 2478 | Slog.d(TAG, "Removing appId " + appId + " from temp whitelist"); |
| 2479 | } |
| 2480 | updateTempWhitelistAppIdsLocked(appId, false); |
| 2481 | mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_CHANGED, appId, 0) |
| 2482 | .sendToTarget(); |
| 2483 | reportTempWhitelistChangedLocked(); |
| 2484 | try { |
| 2485 | mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_FINISH, |
| 2486 | reason, appId); |
| 2487 | } catch (RemoteException e) { |
| 2488 | } |
| 2489 | } |
| 2490 | |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 2491 | public void exitIdleInternal(String reason) { |
| 2492 | synchronized (this) { |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2493 | mActiveReason = ACTIVE_REASON_FROM_BINDER_CALL; |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 2494 | becomeActiveLocked(reason, Binder.getCallingUid()); |
| 2495 | } |
| 2496 | } |
| 2497 | |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2498 | @VisibleForTesting |
| 2499 | boolean isNetworkConnected() { |
| 2500 | synchronized (this) { |
| 2501 | return mNetworkConnected; |
| 2502 | } |
| 2503 | } |
| 2504 | |
Dianne Hackborn | 4cb96ca | 2016-05-17 13:55:29 -0700 | [diff] [blame] | 2505 | void updateConnectivityState(Intent connIntent) { |
| 2506 | ConnectivityService cm; |
| 2507 | synchronized (this) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2508 | cm = mInjector.getConnectivityService(); |
Dianne Hackborn | 4cb96ca | 2016-05-17 13:55:29 -0700 | [diff] [blame] | 2509 | } |
| 2510 | if (cm == null) { |
| 2511 | return; |
| 2512 | } |
| 2513 | // Note: can't call out to ConnectivityService with our lock held. |
| 2514 | NetworkInfo ni = cm.getActiveNetworkInfo(); |
| 2515 | synchronized (this) { |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2516 | boolean conn; |
| 2517 | if (ni == null) { |
| 2518 | conn = false; |
| 2519 | } else { |
| 2520 | if (connIntent == null) { |
| 2521 | conn = ni.isConnected(); |
| 2522 | } else { |
| 2523 | final int networkType = |
| 2524 | connIntent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, |
| 2525 | ConnectivityManager.TYPE_NONE); |
| 2526 | if (ni.getType() != networkType) { |
| 2527 | return; |
| 2528 | } |
| 2529 | conn = !connIntent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, |
| 2530 | false); |
| 2531 | } |
| 2532 | } |
| 2533 | if (conn != mNetworkConnected) { |
| 2534 | mNetworkConnected = conn; |
| 2535 | if (conn && mLightState == LIGHT_STATE_WAITING_FOR_NETWORK) { |
| 2536 | stepLightIdleStateLocked("network"); |
| 2537 | } |
| 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2542 | @VisibleForTesting |
| 2543 | boolean isScreenOn() { |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2544 | synchronized (this) { |
| 2545 | return mScreenOn; |
| 2546 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Dianne Hackborn | 3dba8ce | 2017-09-01 17:07:04 -0700 | [diff] [blame] | 2549 | void updateInteractivityLocked() { |
| 2550 | // The interactivity state from the power manager tells us whether the display is |
| 2551 | // in a state that we need to keep things running so they will update at a normal |
| 2552 | // frequency. |
| 2553 | boolean screenOn = mPowerManager.isInteractive(); |
| 2554 | if (DEBUG) Slog.d(TAG, "updateInteractivityLocked: screenOn=" + screenOn); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2555 | if (!screenOn && mScreenOn) { |
| 2556 | mScreenOn = false; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2557 | if (!mForceIdle) { |
| 2558 | becomeInactiveIfAppropriateLocked(); |
| 2559 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2560 | } else if (screenOn) { |
| 2561 | mScreenOn = true; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 2562 | if (!mForceIdle && (!mScreenLocked || !mConstants.WAIT_FOR_UNLOCK)) { |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2563 | mActiveReason = ACTIVE_REASON_SCREEN; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2564 | becomeActiveLocked("screen", Process.myUid()); |
| 2565 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2569 | @VisibleForTesting |
| 2570 | boolean isCharging() { |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2571 | synchronized (this) { |
| 2572 | return mCharging; |
| 2573 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2576 | void updateChargingLocked(boolean charging) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2577 | if (DEBUG) Slog.i(TAG, "updateChargingLocked: charging=" + charging); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2578 | if (!charging && mCharging) { |
| 2579 | mCharging = false; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2580 | if (!mForceIdle) { |
| 2581 | becomeInactiveIfAppropriateLocked(); |
| 2582 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2583 | } else if (charging) { |
| 2584 | mCharging = charging; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2585 | if (!mForceIdle) { |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2586 | mActiveReason = ACTIVE_REASON_CHARGING; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2587 | becomeActiveLocked("charging", Process.myUid()); |
| 2588 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2592 | @VisibleForTesting |
| 2593 | boolean isQuickDozeEnabled() { |
| 2594 | synchronized (this) { |
| 2595 | return mQuickDozeActivated; |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | /** Updates the quick doze flag and enters deep doze if appropriate. */ |
| 2600 | @VisibleForTesting |
| 2601 | void updateQuickDozeFlagLocked(boolean enabled) { |
| 2602 | if (DEBUG) Slog.i(TAG, "updateQuickDozeFlagLocked: enabled=" + enabled); |
| 2603 | mQuickDozeActivated = enabled; |
| 2604 | if (enabled) { |
| 2605 | // If Quick Doze is enabled, see if we should go straight into it. |
| 2606 | becomeInactiveIfAppropriateLocked(); |
| 2607 | } |
| 2608 | // Going from Deep Doze to Light Idle (if quick doze becomes disabled) is tricky and |
| 2609 | // probably not worth the overhead, so leave in deep doze if that's the case until the |
| 2610 | // next natural time to come out of it. |
| 2611 | } |
| 2612 | |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2613 | |
| 2614 | /** Returns true if the screen is locked. */ |
| 2615 | @VisibleForTesting |
| 2616 | boolean isKeyguardShowing() { |
| 2617 | synchronized (this) { |
| 2618 | return mScreenLocked; |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | @VisibleForTesting |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 2623 | void keyguardShowingLocked(boolean showing) { |
| 2624 | if (DEBUG) Slog.i(TAG, "keyguardShowing=" + showing); |
| 2625 | if (mScreenLocked != showing) { |
| 2626 | mScreenLocked = showing; |
| 2627 | if (mScreenOn && !mForceIdle && !mScreenLocked) { |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2628 | mActiveReason = ACTIVE_REASON_UNLOCKED; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 2629 | becomeActiveLocked("unlocked", Process.myUid()); |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2634 | @VisibleForTesting |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 2635 | void scheduleReportActiveLocked(String activeReason, int activeUid) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2636 | Message msg = mHandler.obtainMessage(MSG_REPORT_ACTIVE, activeUid, 0, activeReason); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2637 | mHandler.sendMessage(msg); |
| 2638 | } |
| 2639 | |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 2640 | void becomeActiveLocked(String activeReason, int activeUid) { |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2641 | becomeActiveLocked(activeReason, activeUid, mConstants.INACTIVE_TIMEOUT, true); |
| 2642 | } |
| 2643 | |
| 2644 | private void becomeActiveLocked(String activeReason, int activeUid, |
| 2645 | long newInactiveTimeout, boolean changeLightIdle) { |
| 2646 | if (DEBUG) { |
| 2647 | Slog.i(TAG, "becomeActiveLocked, reason=" + activeReason |
| 2648 | + ", changeLightIdle=" + changeLightIdle); |
| 2649 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2650 | if (mState != STATE_ACTIVE || mLightState != STATE_ACTIVE) { |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 2651 | EventLogTags.writeDeviceIdle(STATE_ACTIVE, activeReason); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2652 | mState = STATE_ACTIVE; |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2653 | mInactiveTimeout = newInactiveTimeout; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2654 | mCurIdleBudget = 0; |
| 2655 | mMaintenanceStartTime = 0; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2656 | resetIdleManagementLocked(); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2657 | |
| 2658 | if (changeLightIdle) { |
| 2659 | EventLogTags.writeDeviceIdleLight(LIGHT_STATE_ACTIVE, activeReason); |
| 2660 | mLightState = LIGHT_STATE_ACTIVE; |
| 2661 | resetLightIdleManagementLocked(); |
| 2662 | // Only report active if light is also ACTIVE. |
| 2663 | scheduleReportActiveLocked(activeReason, activeUid); |
| 2664 | addEvent(EVENT_NORMAL, activeReason); |
| 2665 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2666 | } |
| 2667 | } |
| 2668 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2669 | /** Must only be used in tests. */ |
| 2670 | @VisibleForTesting |
| 2671 | void setDeepEnabledForTest(boolean enabled) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2672 | synchronized (this) { |
| 2673 | mDeepEnabled = enabled; |
| 2674 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | /** Must only be used in tests. */ |
| 2678 | @VisibleForTesting |
| 2679 | void setLightEnabledForTest(boolean enabled) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2680 | synchronized (this) { |
| 2681 | mLightEnabled = enabled; |
| 2682 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2685 | /** Sanity check to make sure DeviceIdleController and AlarmManager are on the same page. */ |
| 2686 | private void verifyAlarmStateLocked() { |
| 2687 | if (mState == STATE_ACTIVE && mNextAlarmTime != 0) { |
| 2688 | Slog.wtf(TAG, "mState=ACTIVE but mNextAlarmTime=" + mNextAlarmTime); |
| 2689 | } |
| 2690 | if (mState != STATE_IDLE && mLocalAlarmManager.isIdling()) { |
| 2691 | Slog.wtf(TAG, "mState=" + stateToString(mState) + " but AlarmManager is idling"); |
| 2692 | } |
| 2693 | if (mState == STATE_IDLE && !mLocalAlarmManager.isIdling()) { |
| 2694 | Slog.wtf(TAG, "mState=IDLE but AlarmManager is not idling"); |
| 2695 | } |
| 2696 | if (mLightState == LIGHT_STATE_ACTIVE && mNextLightAlarmTime != 0) { |
| 2697 | Slog.wtf(TAG, "mLightState=ACTIVE but mNextLightAlarmTime is " |
| 2698 | + TimeUtils.formatDuration(mNextLightAlarmTime - SystemClock.elapsedRealtime()) |
| 2699 | + " from now"); |
| 2700 | } |
| 2701 | } |
| 2702 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2703 | void becomeInactiveIfAppropriateLocked() { |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2704 | verifyAlarmStateLocked(); |
| 2705 | |
| 2706 | final boolean isScreenBlockingInactive = |
| 2707 | mScreenOn && (!mConstants.WAIT_FOR_UNLOCK || !mScreenLocked); |
| 2708 | if (DEBUG) { |
| 2709 | Slog.d(TAG, "becomeInactiveIfAppropriateLocked():" |
| 2710 | + " isScreenBlockingInactive=" + isScreenBlockingInactive |
| 2711 | + " (mScreenOn=" + mScreenOn |
| 2712 | + ", WAIT_FOR_UNLOCK=" + mConstants.WAIT_FOR_UNLOCK |
| 2713 | + ", mScreenLocked=" + mScreenLocked + ")" |
| 2714 | + " mCharging=" + mCharging |
| 2715 | + " mForceIdle=" + mForceIdle |
| 2716 | ); |
| 2717 | } |
| 2718 | if (!mForceIdle && (mCharging || isScreenBlockingInactive)) { |
| 2719 | return; |
| 2720 | } |
| 2721 | // Become inactive and determine if we will ultimately go idle. |
| 2722 | if (mDeepEnabled) { |
| 2723 | if (mQuickDozeActivated) { |
| 2724 | if (mState == STATE_QUICK_DOZE_DELAY || mState == STATE_IDLE |
| 2725 | || mState == STATE_IDLE_MAINTENANCE) { |
| 2726 | // Already "idling". Don't want to restart the process. |
| 2727 | // mLightState can't be LIGHT_STATE_ACTIVE if mState is any of these 3 |
| 2728 | // values, so returning here is safe. |
| 2729 | return; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2730 | } |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2731 | if (DEBUG) { |
| 2732 | Slog.d(TAG, "Moved from " |
| 2733 | + stateToString(mState) + " to STATE_QUICK_DOZE_DELAY"); |
| 2734 | } |
| 2735 | mState = STATE_QUICK_DOZE_DELAY; |
| 2736 | // Make sure any motion sensing or locating is stopped. |
| 2737 | resetIdleManagementLocked(); |
| 2738 | // Wait a small amount of time in case something (eg: background service from |
| 2739 | // recently closed app) needs to finish running. |
Kweku Adams | bf27786 | 2019-07-19 15:20:44 -0700 | [diff] [blame] | 2740 | // Use a non-wakeup alarm for going into quick doze in case an AlarmClock alarm |
| 2741 | // is scheduled soon. The non-wakeup alarm will be delayed by at most 2 minutes. |
| 2742 | scheduleAlarmLocked(mConstants.QUICK_DOZE_DELAY_TIMEOUT, false, false); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2743 | EventLogTags.writeDeviceIdle(mState, "no activity"); |
| 2744 | } else if (mState == STATE_ACTIVE) { |
| 2745 | mState = STATE_INACTIVE; |
| 2746 | if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE"); |
| 2747 | resetIdleManagementLocked(); |
| 2748 | long delay = mInactiveTimeout; |
| 2749 | if (shouldUseIdleTimeoutFactorLocked()) { |
| 2750 | delay = (long) (mPreIdleFactor * delay); |
| 2751 | } |
| 2752 | scheduleAlarmLocked(delay, false); |
| 2753 | EventLogTags.writeDeviceIdle(mState, "no activity"); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2754 | } |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2755 | } |
| 2756 | if (mLightState == LIGHT_STATE_ACTIVE && mLightEnabled) { |
| 2757 | mLightState = LIGHT_STATE_INACTIVE; |
| 2758 | if (DEBUG) Slog.d(TAG, "Moved from LIGHT_STATE_ACTIVE to LIGHT_STATE_INACTIVE"); |
| 2759 | resetLightIdleManagementLocked(); |
| 2760 | scheduleLightAlarmLocked(mConstants.LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT); |
| 2761 | EventLogTags.writeDeviceIdleLight(mLightState, "no activity"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2762 | } |
| 2763 | } |
| 2764 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2765 | private void resetIdleManagementLocked() { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2766 | mNextIdlePendingDelay = 0; |
| 2767 | mNextIdleDelay = 0; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2768 | mNextLightIdleDelay = 0; |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2769 | mIdleStartTime = 0; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2770 | cancelAlarmLocked(); |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 2771 | cancelSensingTimeoutAlarmLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2772 | cancelLocatingLocked(); |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2773 | stopMonitoringMotionLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2774 | mAnyMotionDetector.stop(); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2775 | updateActiveConstraintsLocked(); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2778 | private void resetLightIdleManagementLocked() { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2779 | cancelLightAlarmLocked(); |
| 2780 | } |
| 2781 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2782 | void exitForceIdleLocked() { |
| 2783 | if (mForceIdle) { |
| 2784 | mForceIdle = false; |
| 2785 | if (mScreenOn || mCharging) { |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2786 | mActiveReason = ACTIVE_REASON_FORCED; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2787 | becomeActiveLocked("exit-force", Process.myUid()); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2788 | } |
| 2789 | } |
| 2790 | } |
| 2791 | |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 2792 | /** |
| 2793 | * Must only be used in tests. |
| 2794 | * |
| 2795 | * This sets the state value directly and thus doesn't trigger any behavioral changes. |
| 2796 | */ |
| 2797 | @VisibleForTesting |
| 2798 | void setLightStateForTest(int lightState) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2799 | synchronized (this) { |
| 2800 | mLightState = lightState; |
| 2801 | } |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 2802 | } |
| 2803 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2804 | @VisibleForTesting |
| 2805 | int getLightState() { |
| 2806 | return mLightState; |
| 2807 | } |
| 2808 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2809 | void stepLightIdleStateLocked(String reason) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2810 | if (mLightState == LIGHT_STATE_OVERRIDE) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2811 | // If we are already in deep device idle mode, then |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2812 | // there is nothing left to do for light mode. |
| 2813 | return; |
| 2814 | } |
| 2815 | |
| 2816 | if (DEBUG) Slog.d(TAG, "stepLightIdleStateLocked: mLightState=" + mLightState); |
| 2817 | EventLogTags.writeDeviceIdleLightStep(); |
| 2818 | |
| 2819 | switch (mLightState) { |
| 2820 | case LIGHT_STATE_INACTIVE: |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2821 | mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2822 | // Reset the upcoming idle delays. |
| 2823 | mNextLightIdleDelay = mConstants.LIGHT_IDLE_TIMEOUT; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2824 | mMaintenanceStartTime = 0; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2825 | if (!isOpsInactiveLocked()) { |
| 2826 | // We have some active ops going on... give them a chance to finish |
| 2827 | // before going in to our first idle. |
| 2828 | mLightState = LIGHT_STATE_PRE_IDLE; |
| 2829 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
| 2830 | scheduleLightAlarmLocked(mConstants.LIGHT_PRE_IDLE_TIMEOUT); |
| 2831 | break; |
| 2832 | } |
| 2833 | // Nothing active, fall through to immediately idle. |
| 2834 | case LIGHT_STATE_PRE_IDLE: |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2835 | case LIGHT_STATE_IDLE_MAINTENANCE: |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2836 | if (mMaintenanceStartTime != 0) { |
| 2837 | long duration = SystemClock.elapsedRealtime() - mMaintenanceStartTime; |
| 2838 | if (duration < mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET) { |
| 2839 | // We didn't use up all of our minimum budget; add this to the reserve. |
| 2840 | mCurIdleBudget += (mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET-duration); |
| 2841 | } else { |
| 2842 | // We used more than our minimum budget; this comes out of the reserve. |
| 2843 | mCurIdleBudget -= (duration-mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET); |
| 2844 | } |
| 2845 | } |
| 2846 | mMaintenanceStartTime = 0; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2847 | scheduleLightAlarmLocked(mNextLightIdleDelay); |
| 2848 | mNextLightIdleDelay = Math.min(mConstants.LIGHT_MAX_IDLE_TIMEOUT, |
| 2849 | (long)(mNextLightIdleDelay * mConstants.LIGHT_IDLE_FACTOR)); |
| 2850 | if (mNextLightIdleDelay < mConstants.LIGHT_IDLE_TIMEOUT) { |
| 2851 | mNextLightIdleDelay = mConstants.LIGHT_IDLE_TIMEOUT; |
| 2852 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2853 | if (DEBUG) Slog.d(TAG, "Moved to LIGHT_STATE_IDLE."); |
| 2854 | mLightState = LIGHT_STATE_IDLE; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2855 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 2856 | addEvent(EVENT_LIGHT_IDLE, null); |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 2857 | mGoingIdleWakeLock.acquire(); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2858 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_ON_LIGHT); |
| 2859 | break; |
| 2860 | case LIGHT_STATE_IDLE: |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2861 | case LIGHT_STATE_WAITING_FOR_NETWORK: |
| 2862 | if (mNetworkConnected || mLightState == LIGHT_STATE_WAITING_FOR_NETWORK) { |
| 2863 | // We have been idling long enough, now it is time to do some work. |
| 2864 | mActiveIdleOpCount = 1; |
| 2865 | mActiveIdleWakeLock.acquire(); |
| 2866 | mMaintenanceStartTime = SystemClock.elapsedRealtime(); |
| 2867 | if (mCurIdleBudget < mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET) { |
| 2868 | mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET; |
| 2869 | } else if (mCurIdleBudget > mConstants.LIGHT_IDLE_MAINTENANCE_MAX_BUDGET) { |
| 2870 | mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MAX_BUDGET; |
| 2871 | } |
| 2872 | scheduleLightAlarmLocked(mCurIdleBudget); |
| 2873 | if (DEBUG) Slog.d(TAG, |
| 2874 | "Moved from LIGHT_STATE_IDLE to LIGHT_STATE_IDLE_MAINTENANCE."); |
| 2875 | mLightState = LIGHT_STATE_IDLE_MAINTENANCE; |
| 2876 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 2877 | addEvent(EVENT_LIGHT_MAINTENANCE, null); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2878 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_OFF); |
| 2879 | } else { |
| 2880 | // We'd like to do maintenance, but currently don't have network |
| 2881 | // connectivity... let's try to wait until the network comes back. |
| 2882 | // We'll only wait for another full idle period, however, and then give up. |
| 2883 | scheduleLightAlarmLocked(mNextLightIdleDelay); |
| 2884 | if (DEBUG) Slog.d(TAG, "Moved to LIGHT_WAITING_FOR_NETWORK."); |
| 2885 | mLightState = LIGHT_STATE_WAITING_FOR_NETWORK; |
| 2886 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2887 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2888 | break; |
| 2889 | } |
| 2890 | } |
| 2891 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2892 | @VisibleForTesting |
| 2893 | int getState() { |
| 2894 | return mState; |
| 2895 | } |
| 2896 | |
| 2897 | @VisibleForTesting |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2898 | void stepIdleStateLocked(String reason) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2899 | if (DEBUG) Slog.d(TAG, "stepIdleStateLocked: mState=" + mState); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2900 | EventLogTags.writeDeviceIdleStep(); |
| 2901 | |
| 2902 | final long now = SystemClock.elapsedRealtime(); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 2903 | if ((now+mConstants.MIN_TIME_TO_ALARM) > mAlarmManager.getNextWakeFromIdleTime()) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2904 | // Whoops, there is an upcoming alarm. We don't actually want to go idle. |
| 2905 | if (mState != STATE_ACTIVE) { |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2906 | mActiveReason = ACTIVE_REASON_ALARM; |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 2907 | becomeActiveLocked("alarm", Process.myUid()); |
Koji Fukui | 27b3330 | 2015-12-16 19:43:01 +0900 | [diff] [blame] | 2908 | becomeInactiveIfAppropriateLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2909 | } |
| 2910 | return; |
| 2911 | } |
| 2912 | |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2913 | if (mNumBlockingConstraints != 0 && !mForceIdle) { |
| 2914 | // We have some constraints from other parts of the system server preventing |
| 2915 | // us from moving to the next state. |
| 2916 | if (DEBUG) { |
| 2917 | Slog.i(TAG, "Cannot step idle state. Blocked by: " + mConstraints.values().stream() |
| 2918 | .filter(x -> x.active) |
| 2919 | .map(x -> x.name) |
| 2920 | .collect(Collectors.joining(","))); |
| 2921 | } |
| 2922 | return; |
| 2923 | } |
| 2924 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2925 | switch (mState) { |
| 2926 | case STATE_INACTIVE: |
| 2927 | // We have now been inactive long enough, it is time to start looking |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2928 | // for motion and sleep some more while doing so. |
| 2929 | startMonitoringMotionLocked(); |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2930 | long delay = mConstants.IDLE_AFTER_INACTIVE_TIMEOUT; |
| 2931 | if (shouldUseIdleTimeoutFactorLocked()) { |
| 2932 | delay = (long) (mPreIdleFactor * delay); |
| 2933 | } |
| 2934 | scheduleAlarmLocked(delay, false); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2935 | moveToStateLocked(STATE_IDLE_PENDING, reason); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2936 | break; |
| 2937 | case STATE_IDLE_PENDING: |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2938 | moveToStateLocked(STATE_SENSING, reason); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2939 | cancelLocatingLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2940 | mLocated = false; |
| 2941 | mLastGenericLocation = null; |
| 2942 | mLastGpsLocation = null; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2943 | updateActiveConstraintsLocked(); |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 2944 | |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2945 | // Wait for open constraints and an accelerometer reading before moving on. |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 2946 | if (mUseMotionSensor && mAnyMotionDetector.hasSensor()) { |
| 2947 | scheduleSensingTimeoutAlarmLocked(mConstants.SENSING_TIMEOUT); |
| 2948 | mNotMoving = false; |
| 2949 | mAnyMotionDetector.checkForAnyMotion(); |
| 2950 | break; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2951 | } else if (mNumBlockingConstraints != 0) { |
| 2952 | cancelAlarmLocked(); |
| 2953 | break; |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | mNotMoving = true; |
| 2957 | // Otherwise, fall through and check this off the list of requirements. |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2958 | case STATE_SENSING: |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 2959 | cancelSensingTimeoutAlarmLocked(); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2960 | moveToStateLocked(STATE_LOCATING, reason); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2961 | scheduleAlarmLocked(mConstants.LOCATING_TIMEOUT, false); |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2962 | LocationManager locationManager = mInjector.getLocationManager(); |
| 2963 | if (locationManager != null |
| 2964 | && locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) { |
| 2965 | locationManager.requestLocationUpdates(mLocationRequest, |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2966 | mGenericLocationListener, mHandler.getLooper()); |
| 2967 | mLocating = true; |
| 2968 | } else { |
| 2969 | mHasNetworkLocation = false; |
| 2970 | } |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2971 | if (locationManager != null |
| 2972 | && locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) { |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2973 | mHasGps = true; |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 2974 | locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2975 | mGpsLocationListener, mHandler.getLooper()); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2976 | mLocating = true; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2977 | } else { |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2978 | mHasGps = false; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2979 | } |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2980 | // If we have a location provider, we're all set, the listeners will move state |
| 2981 | // forward. |
| 2982 | if (mLocating) { |
| 2983 | break; |
| 2984 | } |
| 2985 | |
| 2986 | // Otherwise, we have to move from locating into idle maintenance. |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2987 | case STATE_LOCATING: |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2988 | cancelAlarmLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2989 | cancelLocatingLocked(); |
| 2990 | mAnyMotionDetector.stop(); |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2991 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2992 | // Intentional fallthrough -- time to go into IDLE state. |
| 2993 | case STATE_QUICK_DOZE_DELAY: |
| 2994 | // Reset the upcoming idle delays. |
| 2995 | mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT; |
| 2996 | mNextIdleDelay = mConstants.IDLE_TIMEOUT; |
| 2997 | |
| 2998 | // Everything is in place to go into IDLE state. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2999 | case STATE_IDLE_MAINTENANCE: |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3000 | scheduleAlarmLocked(mNextIdleDelay, true); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 3001 | if (DEBUG) Slog.d(TAG, "Moved to STATE_IDLE. Next alarm in " + mNextIdleDelay + |
| 3002 | " ms."); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 3003 | mNextIdleDelay = (long)(mNextIdleDelay * mConstants.IDLE_FACTOR); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 3004 | if (DEBUG) Slog.d(TAG, "Setting mNextIdleDelay = " + mNextIdleDelay); |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 3005 | mIdleStartTime = SystemClock.elapsedRealtime(); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 3006 | mNextIdleDelay = Math.min(mNextIdleDelay, mConstants.MAX_IDLE_TIMEOUT); |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 3007 | if (mNextIdleDelay < mConstants.IDLE_TIMEOUT) { |
| 3008 | mNextIdleDelay = mConstants.IDLE_TIMEOUT; |
| 3009 | } |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 3010 | moveToStateLocked(STATE_IDLE, reason); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3011 | if (mLightState != LIGHT_STATE_OVERRIDE) { |
| 3012 | mLightState = LIGHT_STATE_OVERRIDE; |
| 3013 | cancelLightAlarmLocked(); |
| 3014 | } |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 3015 | addEvent(EVENT_DEEP_IDLE, null); |
Joe Onorato | 8f0e9ced | 2016-12-08 17:48:49 -0800 | [diff] [blame] | 3016 | mGoingIdleWakeLock.acquire(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3017 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_ON); |
| 3018 | break; |
| 3019 | case STATE_IDLE: |
| 3020 | // We have been idling long enough, now it is time to do some work. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3021 | mActiveIdleOpCount = 1; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 3022 | mActiveIdleWakeLock.acquire(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3023 | scheduleAlarmLocked(mNextIdlePendingDelay, false); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 3024 | if (DEBUG) Slog.d(TAG, "Moved from STATE_IDLE to STATE_IDLE_MAINTENANCE. " + |
| 3025 | "Next alarm in " + mNextIdlePendingDelay + " ms."); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3026 | mMaintenanceStartTime = SystemClock.elapsedRealtime(); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 3027 | mNextIdlePendingDelay = Math.min(mConstants.MAX_IDLE_PENDING_TIMEOUT, |
| 3028 | (long)(mNextIdlePendingDelay * mConstants.IDLE_PENDING_FACTOR)); |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 3029 | if (mNextIdlePendingDelay < mConstants.IDLE_PENDING_TIMEOUT) { |
| 3030 | mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT; |
| 3031 | } |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 3032 | moveToStateLocked(STATE_IDLE_MAINTENANCE, reason); |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 3033 | addEvent(EVENT_DEEP_MAINTENANCE, null); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3034 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_OFF); |
| 3035 | break; |
| 3036 | } |
| 3037 | } |
| 3038 | |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 3039 | private void moveToStateLocked(int state, String reason) { |
| 3040 | final int oldState = mState; |
| 3041 | mState = state; |
| 3042 | if (DEBUG) { |
| 3043 | Slog.d(TAG, String.format("Moved from STATE_%s to STATE_%s.", |
| 3044 | stateToString(oldState), stateToString(mState))); |
| 3045 | } |
| 3046 | EventLogTags.writeDeviceIdle(mState, reason); |
| 3047 | updateActiveConstraintsLocked(); |
| 3048 | } |
| 3049 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3050 | void incActiveIdleOps() { |
| 3051 | synchronized (this) { |
| 3052 | mActiveIdleOpCount++; |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | void decActiveIdleOps() { |
| 3057 | synchronized (this) { |
| 3058 | mActiveIdleOpCount--; |
| 3059 | if (mActiveIdleOpCount <= 0) { |
| 3060 | exitMaintenanceEarlyIfNeededLocked(); |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 3061 | mActiveIdleWakeLock.release(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3062 | } |
| 3063 | } |
| 3064 | } |
| 3065 | |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 3066 | /** Must only be used in tests. */ |
| 3067 | @VisibleForTesting |
| 3068 | void setActiveIdleOpsForTest(int count) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 3069 | synchronized (this) { |
| 3070 | mActiveIdleOpCount = count; |
| 3071 | } |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 3072 | } |
| 3073 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3074 | void setJobsActive(boolean active) { |
| 3075 | synchronized (this) { |
| 3076 | mJobsActive = active; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 3077 | reportMaintenanceActivityIfNeededLocked(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3078 | if (!active) { |
| 3079 | exitMaintenanceEarlyIfNeededLocked(); |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | void setAlarmsActive(boolean active) { |
| 3085 | synchronized (this) { |
| 3086 | mAlarmsActive = active; |
| 3087 | if (!active) { |
| 3088 | exitMaintenanceEarlyIfNeededLocked(); |
| 3089 | } |
| 3090 | } |
| 3091 | } |
| 3092 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 3093 | boolean registerMaintenanceActivityListener(IMaintenanceActivityListener listener) { |
| 3094 | synchronized (this) { |
| 3095 | mMaintenanceActivityListeners.register(listener); |
| 3096 | return mReportedMaintenanceActivity; |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | void unregisterMaintenanceActivityListener(IMaintenanceActivityListener listener) { |
| 3101 | synchronized (this) { |
| 3102 | mMaintenanceActivityListeners.unregister(listener); |
| 3103 | } |
| 3104 | } |
| 3105 | |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 3106 | @VisibleForTesting |
| 3107 | int setPreIdleTimeoutMode(int mode) { |
| 3108 | return setPreIdleTimeoutFactor(getPreIdleTimeoutByMode(mode)); |
| 3109 | } |
| 3110 | |
| 3111 | @VisibleForTesting |
| 3112 | float getPreIdleTimeoutByMode(int mode) { |
| 3113 | switch (mode) { |
| 3114 | case PowerManager.PRE_IDLE_TIMEOUT_MODE_LONG: { |
| 3115 | return mConstants.PRE_IDLE_FACTOR_LONG; |
| 3116 | } |
| 3117 | case PowerManager.PRE_IDLE_TIMEOUT_MODE_SHORT: { |
| 3118 | return mConstants.PRE_IDLE_FACTOR_SHORT; |
| 3119 | } |
| 3120 | case PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL: { |
| 3121 | return 1.0f; |
| 3122 | } |
| 3123 | default: { |
| 3124 | Slog.w(TAG, "Invalid time out factor mode: " + mode); |
| 3125 | return 1.0f; |
| 3126 | } |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | @VisibleForTesting |
| 3131 | float getPreIdleTimeoutFactor() { |
| 3132 | return mPreIdleFactor; |
| 3133 | } |
| 3134 | |
| 3135 | @VisibleForTesting |
| 3136 | int setPreIdleTimeoutFactor(float ratio) { |
| 3137 | if (!mDeepEnabled) { |
| 3138 | if (DEBUG) Slog.d(TAG, "setPreIdleTimeoutFactor: Deep Idle disable"); |
| 3139 | return SET_IDLE_FACTOR_RESULT_NOT_SUPPORT; |
| 3140 | } else if (ratio <= MIN_PRE_IDLE_FACTOR_CHANGE) { |
| 3141 | if (DEBUG) Slog.d(TAG, "setPreIdleTimeoutFactor: Invalid input"); |
| 3142 | return SET_IDLE_FACTOR_RESULT_INVALID; |
| 3143 | } else if (Math.abs(ratio - mPreIdleFactor) < MIN_PRE_IDLE_FACTOR_CHANGE) { |
| 3144 | if (DEBUG) Slog.d(TAG, "setPreIdleTimeoutFactor: New factor same as previous factor"); |
| 3145 | return SET_IDLE_FACTOR_RESULT_IGNORED; |
| 3146 | } |
| 3147 | synchronized (this) { |
| 3148 | mLastPreIdleFactor = mPreIdleFactor; |
| 3149 | mPreIdleFactor = ratio; |
| 3150 | } |
| 3151 | if (DEBUG) Slog.d(TAG, "setPreIdleTimeoutFactor: " + ratio); |
| 3152 | postUpdatePreIdleFactor(); |
| 3153 | return SET_IDLE_FACTOR_RESULT_OK; |
| 3154 | } |
| 3155 | |
| 3156 | @VisibleForTesting |
| 3157 | void resetPreIdleTimeoutMode() { |
| 3158 | synchronized (this) { |
| 3159 | mLastPreIdleFactor = mPreIdleFactor; |
| 3160 | mPreIdleFactor = 1.0f; |
| 3161 | } |
| 3162 | if (DEBUG) Slog.d(TAG, "resetPreIdleTimeoutMode to 1.0"); |
| 3163 | postResetPreIdleTimeoutFactor(); |
| 3164 | } |
| 3165 | |
| 3166 | private void postUpdatePreIdleFactor() { |
| 3167 | mHandler.sendEmptyMessage(MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR); |
| 3168 | } |
| 3169 | |
| 3170 | private void postResetPreIdleTimeoutFactor() { |
| 3171 | mHandler.sendEmptyMessage(MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR); |
| 3172 | } |
| 3173 | |
| 3174 | @VisibleForTesting |
| 3175 | void updatePreIdleFactor() { |
| 3176 | synchronized (this) { |
| 3177 | if (!shouldUseIdleTimeoutFactorLocked()) { |
| 3178 | return; |
| 3179 | } |
| 3180 | if (mState == STATE_INACTIVE || mState == STATE_IDLE_PENDING) { |
| 3181 | if (mNextAlarmTime == 0) { |
| 3182 | return; |
| 3183 | } |
| 3184 | long delay = mNextAlarmTime - SystemClock.elapsedRealtime(); |
| 3185 | if (delay < MIN_STATE_STEP_ALARM_CHANGE) { |
| 3186 | return; |
| 3187 | } |
| 3188 | long newDelay = (long) (delay / mLastPreIdleFactor * mPreIdleFactor); |
| 3189 | if (Math.abs(delay - newDelay) < MIN_STATE_STEP_ALARM_CHANGE) { |
| 3190 | return; |
| 3191 | } |
| 3192 | scheduleAlarmLocked(newDelay, false); |
| 3193 | } |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | @VisibleForTesting |
| 3198 | void maybeDoImmediateMaintenance() { |
| 3199 | synchronized (this) { |
| 3200 | if (mState == STATE_IDLE) { |
| 3201 | long duration = SystemClock.elapsedRealtime() - mIdleStartTime; |
| 3202 | /* Let's trgger a immediate maintenance, |
| 3203 | * if it has been idle for a long time */ |
| 3204 | if (duration > mConstants.IDLE_TIMEOUT) { |
| 3205 | scheduleAlarmLocked(0, false); |
| 3206 | } |
| 3207 | } |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | private boolean shouldUseIdleTimeoutFactorLocked() { |
| 3212 | // exclude ACTIVE_REASON_MOTION, for exclude device in pocket case |
| 3213 | if (mActiveReason == ACTIVE_REASON_MOTION) { |
| 3214 | return false; |
| 3215 | } |
| 3216 | return true; |
| 3217 | } |
| 3218 | |
| 3219 | /** Must only be used in tests. */ |
| 3220 | @VisibleForTesting |
| 3221 | void setIdleStartTimeForTest(long idleStartTime) { |
| 3222 | synchronized (this) { |
| 3223 | mIdleStartTime = idleStartTime; |
| 3224 | } |
| 3225 | } |
| 3226 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 3227 | void reportMaintenanceActivityIfNeededLocked() { |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 3228 | boolean active = mJobsActive; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 3229 | if (active == mReportedMaintenanceActivity) { |
| 3230 | return; |
| 3231 | } |
| 3232 | mReportedMaintenanceActivity = active; |
| 3233 | Message msg = mHandler.obtainMessage(MSG_REPORT_MAINTENANCE_ACTIVITY, |
| 3234 | mReportedMaintenanceActivity ? 1 : 0, 0); |
| 3235 | mHandler.sendMessage(msg); |
| 3236 | } |
| 3237 | |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 3238 | @VisibleForTesting |
| 3239 | long getNextAlarmTime() { |
| 3240 | return mNextAlarmTime; |
| 3241 | } |
| 3242 | |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 3243 | boolean isOpsInactiveLocked() { |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 3244 | return mActiveIdleOpCount <= 0 && !mJobsActive && !mAlarmsActive; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3247 | void exitMaintenanceEarlyIfNeededLocked() { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 3248 | if (mState == STATE_IDLE_MAINTENANCE || mLightState == LIGHT_STATE_IDLE_MAINTENANCE |
| 3249 | || mLightState == LIGHT_STATE_PRE_IDLE) { |
| 3250 | if (isOpsInactiveLocked()) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3251 | final long now = SystemClock.elapsedRealtime(); |
| 3252 | if (DEBUG) { |
| 3253 | StringBuilder sb = new StringBuilder(); |
| 3254 | sb.append("Exit: start="); |
| 3255 | TimeUtils.formatDuration(mMaintenanceStartTime, sb); |
| 3256 | sb.append(" now="); |
| 3257 | TimeUtils.formatDuration(now, sb); |
| 3258 | Slog.d(TAG, sb.toString()); |
| 3259 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3260 | if (mState == STATE_IDLE_MAINTENANCE) { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 3261 | stepIdleStateLocked("s:early"); |
| 3262 | } else if (mLightState == LIGHT_STATE_PRE_IDLE) { |
| 3263 | stepLightIdleStateLocked("s:predone"); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3264 | } else { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 3265 | stepLightIdleStateLocked("s:early"); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3266 | } |
| 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 3271 | void motionLocked() { |
| 3272 | if (DEBUG) Slog.d(TAG, "motionLocked()"); |
| 3273 | // The motion sensor will have been disabled at this point |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3274 | handleMotionDetectedLocked(mConstants.MOTION_INACTIVE_TIMEOUT, "motion"); |
| 3275 | } |
| 3276 | |
| 3277 | void handleMotionDetectedLocked(long timeout, String type) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3278 | // The device is not yet active, so we want to go back to the pending idle |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 3279 | // state to wait again for no motion. Note that we only monitor for motion |
| 3280 | // after moving out of the inactive state, so no need to worry about that. |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 3281 | final boolean becomeInactive = mState != STATE_ACTIVE |
| 3282 | || mLightState == LIGHT_STATE_OVERRIDE; |
| 3283 | // We only want to change the IDLE state if it's OVERRIDE. |
| 3284 | becomeActiveLocked(type, Process.myUid(), timeout, mLightState == LIGHT_STATE_OVERRIDE); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3285 | if (becomeInactive) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3286 | becomeInactiveIfAppropriateLocked(); |
| 3287 | } |
| 3288 | } |
| 3289 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3290 | void receivedGenericLocationLocked(Location location) { |
| 3291 | if (mState != STATE_LOCATING) { |
| 3292 | cancelLocatingLocked(); |
| 3293 | return; |
| 3294 | } |
| 3295 | if (DEBUG) Slog.d(TAG, "Generic location: " + location); |
| 3296 | mLastGenericLocation = new Location(location); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 3297 | if (location.getAccuracy() > mConstants.LOCATION_ACCURACY && mHasGps) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3298 | return; |
| 3299 | } |
| 3300 | mLocated = true; |
| 3301 | if (mNotMoving) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3302 | stepIdleStateLocked("s:location"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | void receivedGpsLocationLocked(Location location) { |
| 3307 | if (mState != STATE_LOCATING) { |
| 3308 | cancelLocatingLocked(); |
| 3309 | return; |
| 3310 | } |
| 3311 | if (DEBUG) Slog.d(TAG, "GPS location: " + location); |
| 3312 | mLastGpsLocation = new Location(location); |
| 3313 | if (location.getAccuracy() > mConstants.LOCATION_ACCURACY) { |
| 3314 | return; |
| 3315 | } |
| 3316 | mLocated = true; |
| 3317 | if (mNotMoving) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3318 | stepIdleStateLocked("s:gps"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3319 | } |
| 3320 | } |
| 3321 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 3322 | void startMonitoringMotionLocked() { |
| 3323 | if (DEBUG) Slog.d(TAG, "startMonitoringMotionLocked()"); |
| 3324 | if (mMotionSensor != null && !mMotionListener.active) { |
| 3325 | mMotionListener.registerLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3326 | } |
| 3327 | } |
| 3328 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 3329 | void stopMonitoringMotionLocked() { |
| 3330 | if (DEBUG) Slog.d(TAG, "stopMonitoringMotionLocked()"); |
| 3331 | if (mMotionSensor != null && mMotionListener.active) { |
| 3332 | mMotionListener.unregisterLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | void cancelAlarmLocked() { |
| 3337 | if (mNextAlarmTime != 0) { |
| 3338 | mNextAlarmTime = 0; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3339 | mAlarmManager.cancel(mDeepAlarmListener); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3340 | } |
| 3341 | } |
| 3342 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3343 | void cancelLightAlarmLocked() { |
| 3344 | if (mNextLightAlarmTime != 0) { |
| 3345 | mNextLightAlarmTime = 0; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3346 | mAlarmManager.cancel(mLightAlarmListener); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | void cancelLocatingLocked() { |
| 3351 | if (mLocating) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 3352 | LocationManager locationManager = mInjector.getLocationManager(); |
| 3353 | locationManager.removeUpdates(mGenericLocationListener); |
| 3354 | locationManager.removeUpdates(mGpsLocationListener); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3355 | mLocating = false; |
| 3356 | } |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 3359 | void cancelSensingTimeoutAlarmLocked() { |
| 3360 | if (mNextSensingTimeoutAlarmTime != 0) { |
| 3361 | mNextSensingTimeoutAlarmTime = 0; |
| 3362 | mAlarmManager.cancel(mSensingTimeoutAlarmListener); |
| 3363 | } |
| 3364 | } |
| 3365 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3366 | void scheduleAlarmLocked(long delay, boolean idleUntil) { |
Kweku Adams | bf27786 | 2019-07-19 15:20:44 -0700 | [diff] [blame] | 3367 | scheduleAlarmLocked(delay, idleUntil, true); |
| 3368 | } |
| 3369 | |
| 3370 | private void scheduleAlarmLocked(long delay, boolean idleUntil, boolean useWakeupAlarm) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 3371 | if (DEBUG) Slog.d(TAG, "scheduleAlarmLocked(" + delay + ", " + idleUntil + ")"); |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 3372 | |
| 3373 | if (mUseMotionSensor && mMotionSensor == null |
| 3374 | && mState != STATE_QUICK_DOZE_DELAY |
| 3375 | && mState != STATE_IDLE |
| 3376 | && mState != STATE_IDLE_MAINTENANCE) { |
| 3377 | // If there is no motion sensor on this device, but we need one, then we won't schedule |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3378 | // alarms, because we can't determine if the device is not moving. This effectively |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 3379 | // turns off normal execution of device idling, although it is still possible to |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3380 | // manually poke it by pretending like the alarm is going off. |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 3381 | // STATE_QUICK_DOZE_DELAY skips the motion sensing so if the state is past the motion |
| 3382 | // sensing stage (ie, is QUICK_DOZE_DELAY, IDLE, or IDLE_MAINTENANCE), then idling |
| 3383 | // can continue until the user interacts with the device. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3384 | return; |
| 3385 | } |
Kweku Adams | bf27786 | 2019-07-19 15:20:44 -0700 | [diff] [blame] | 3386 | final int alarmType = useWakeupAlarm |
| 3387 | ? AlarmManager.ELAPSED_REALTIME_WAKEUP : AlarmManager.ELAPSED_REALTIME; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3388 | mNextAlarmTime = SystemClock.elapsedRealtime() + delay; |
| 3389 | if (idleUntil) { |
Kweku Adams | bf27786 | 2019-07-19 15:20:44 -0700 | [diff] [blame] | 3390 | mAlarmManager.setIdleUntil(alarmType, |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3391 | mNextAlarmTime, "DeviceIdleController.deep", mDeepAlarmListener, mHandler); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3392 | } else { |
Kweku Adams | bf27786 | 2019-07-19 15:20:44 -0700 | [diff] [blame] | 3393 | mAlarmManager.set(alarmType, |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3394 | mNextAlarmTime, "DeviceIdleController.deep", mDeepAlarmListener, mHandler); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3398 | void scheduleLightAlarmLocked(long delay) { |
| 3399 | if (DEBUG) Slog.d(TAG, "scheduleLightAlarmLocked(" + delay + ")"); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 3400 | mNextLightAlarmTime = SystemClock.elapsedRealtime() + delay; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 3401 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3402 | mNextLightAlarmTime, "DeviceIdleController.light", mLightAlarmListener, mHandler); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 3403 | } |
| 3404 | |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 3405 | void scheduleSensingTimeoutAlarmLocked(long delay) { |
| 3406 | if (DEBUG) Slog.d(TAG, "scheduleSensingAlarmLocked(" + delay + ")"); |
| 3407 | mNextSensingTimeoutAlarmTime = SystemClock.elapsedRealtime() + delay; |
| 3408 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, mNextSensingTimeoutAlarmTime, |
| 3409 | "DeviceIdleController.sensing", mSensingTimeoutAlarmListener, mHandler); |
| 3410 | } |
| 3411 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3412 | private static int[] buildAppIdArray(ArrayMap<String, Integer> systemApps, |
| 3413 | ArrayMap<String, Integer> userApps, SparseBooleanArray outAppIds) { |
| 3414 | outAppIds.clear(); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 3415 | if (systemApps != null) { |
| 3416 | for (int i = 0; i < systemApps.size(); i++) { |
| 3417 | outAppIds.put(systemApps.valueAt(i), true); |
| 3418 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3419 | } |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 3420 | if (userApps != null) { |
| 3421 | for (int i = 0; i < userApps.size(); i++) { |
| 3422 | outAppIds.put(userApps.valueAt(i), true); |
| 3423 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3424 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3425 | int size = outAppIds.size(); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3426 | int[] appids = new int[size]; |
| 3427 | for (int i = 0; i < size; i++) { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3428 | appids[i] = outAppIds.keyAt(i); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3429 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3430 | return appids; |
| 3431 | } |
| 3432 | |
| 3433 | private void updateWhitelistAppIdsLocked() { |
| 3434 | mPowerSaveWhitelistExceptIdleAppIdArray = buildAppIdArray(mPowerSaveWhitelistAppsExceptIdle, |
| 3435 | mPowerSaveWhitelistUserApps, mPowerSaveWhitelistExceptIdleAppIds); |
| 3436 | mPowerSaveWhitelistAllAppIdArray = buildAppIdArray(mPowerSaveWhitelistApps, |
| 3437 | mPowerSaveWhitelistUserApps, mPowerSaveWhitelistAllAppIds); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 3438 | mPowerSaveWhitelistUserAppIdArray = buildAppIdArray(null, |
| 3439 | mPowerSaveWhitelistUserApps, mPowerSaveWhitelistUserAppIds); |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 3440 | if (mLocalActivityManager != null) { |
Makoto Onuki | af8ff4f | 2018-06-04 14:44:19 -0700 | [diff] [blame] | 3441 | mLocalActivityManager.setDeviceIdleWhitelist( |
| 3442 | mPowerSaveWhitelistAllAppIdArray, mPowerSaveWhitelistExceptIdleAppIdArray); |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 3443 | } |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3444 | if (mLocalPowerManager != null) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3445 | if (DEBUG) { |
| 3446 | Slog.d(TAG, "Setting wakelock whitelist to " |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 3447 | + Arrays.toString(mPowerSaveWhitelistAllAppIdArray)); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3448 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 3449 | mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3450 | } |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 3451 | passWhiteListsToForceAppStandbyTrackerLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 3454 | private void updateTempWhitelistAppIdsLocked(int appId, boolean adding) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3455 | final int size = mTempWhitelistAppIdEndTimes.size(); |
| 3456 | if (mTempWhitelistAppIdArray.length != size) { |
| 3457 | mTempWhitelistAppIdArray = new int[size]; |
| 3458 | } |
| 3459 | for (int i = 0; i < size; i++) { |
| 3460 | mTempWhitelistAppIdArray[i] = mTempWhitelistAppIdEndTimes.keyAt(i); |
| 3461 | } |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 3462 | if (mLocalActivityManager != null) { |
| 3463 | if (DEBUG) { |
| 3464 | Slog.d(TAG, "Setting activity manager temp whitelist to " |
| 3465 | + Arrays.toString(mTempWhitelistAppIdArray)); |
| 3466 | } |
| 3467 | mLocalActivityManager.updateDeviceIdleTempWhitelist(mTempWhitelistAppIdArray, appId, |
| 3468 | adding); |
| 3469 | } |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3470 | if (mLocalPowerManager != null) { |
| 3471 | if (DEBUG) { |
| 3472 | Slog.d(TAG, "Setting wakelock temp whitelist to " |
| 3473 | + Arrays.toString(mTempWhitelistAppIdArray)); |
| 3474 | } |
| 3475 | mLocalPowerManager.setDeviceIdleTempWhitelist(mTempWhitelistAppIdArray); |
| 3476 | } |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 3477 | passWhiteListsToForceAppStandbyTrackerLocked(); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3480 | private void reportPowerSaveWhitelistChangedLocked() { |
| 3481 | Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
| 3482 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 3483 | getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | private void reportTempWhitelistChangedLocked() { |
| 3487 | Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED); |
| 3488 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 3489 | getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 3492 | private void passWhiteListsToForceAppStandbyTrackerLocked() { |
Makoto Onuki | e491821 | 2018-02-06 11:30:15 -0800 | [diff] [blame] | 3493 | mAppStateTracker.setPowerSaveWhitelistAppIds( |
Makoto Onuki | 71755c9 | 2018-01-16 14:15:44 -0800 | [diff] [blame] | 3494 | mPowerSaveWhitelistExceptIdleAppIdArray, |
Suprabh Shukla | 5bf4981 | 2018-05-24 18:38:50 -0700 | [diff] [blame] | 3495 | mPowerSaveWhitelistUserAppIdArray, |
Makoto Onuki | 2206af3 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 3496 | mTempWhitelistAppIdArray); |
| 3497 | } |
| 3498 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3499 | void readConfigFileLocked() { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 3500 | if (DEBUG) Slog.d(TAG, "Reading config from " + mConfigFile.getBaseFile()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3501 | mPowerSaveWhitelistUserApps.clear(); |
| 3502 | FileInputStream stream; |
| 3503 | try { |
| 3504 | stream = mConfigFile.openRead(); |
| 3505 | } catch (FileNotFoundException e) { |
| 3506 | return; |
| 3507 | } |
| 3508 | try { |
| 3509 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 3510 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3511 | readConfigFileLocked(parser); |
| 3512 | } catch (XmlPullParserException e) { |
| 3513 | } finally { |
| 3514 | try { |
| 3515 | stream.close(); |
| 3516 | } catch (IOException e) { |
| 3517 | } |
| 3518 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
| 3521 | private void readConfigFileLocked(XmlPullParser parser) { |
| 3522 | final PackageManager pm = getContext().getPackageManager(); |
| 3523 | |
| 3524 | try { |
| 3525 | int type; |
| 3526 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 3527 | && type != XmlPullParser.END_DOCUMENT) { |
| 3528 | ; |
| 3529 | } |
| 3530 | |
| 3531 | if (type != XmlPullParser.START_TAG) { |
| 3532 | throw new IllegalStateException("no start tag found"); |
| 3533 | } |
| 3534 | |
| 3535 | int outerDepth = parser.getDepth(); |
| 3536 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 3537 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 3538 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 3539 | continue; |
| 3540 | } |
| 3541 | |
| 3542 | String tagName = parser.getName(); |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 3543 | switch (tagName) { |
| 3544 | case "wl": |
| 3545 | String name = parser.getAttributeValue(null, "n"); |
| 3546 | if (name != null) { |
| 3547 | try { |
| 3548 | ApplicationInfo ai = pm.getApplicationInfo(name, |
| 3549 | PackageManager.MATCH_ANY_USER); |
| 3550 | mPowerSaveWhitelistUserApps.put(ai.packageName, |
| 3551 | UserHandle.getAppId(ai.uid)); |
| 3552 | } catch (PackageManager.NameNotFoundException e) { |
| 3553 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3554 | } |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 3555 | break; |
| 3556 | case "un-wl": |
| 3557 | final String packageName = parser.getAttributeValue(null, "n"); |
| 3558 | if (mPowerSaveWhitelistApps.containsKey(packageName)) { |
| 3559 | mRemovedFromSystemWhitelistApps.put(packageName, |
| 3560 | mPowerSaveWhitelistApps.remove(packageName)); |
| 3561 | } |
| 3562 | break; |
| 3563 | default: |
| 3564 | Slog.w(TAG, "Unknown element under <config>: " |
| 3565 | + parser.getName()); |
| 3566 | XmlUtils.skipCurrentTag(parser); |
| 3567 | break; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | } catch (IllegalStateException e) { |
| 3572 | Slog.w(TAG, "Failed parsing config " + e); |
| 3573 | } catch (NullPointerException e) { |
| 3574 | Slog.w(TAG, "Failed parsing config " + e); |
| 3575 | } catch (NumberFormatException e) { |
| 3576 | Slog.w(TAG, "Failed parsing config " + e); |
| 3577 | } catch (XmlPullParserException e) { |
| 3578 | Slog.w(TAG, "Failed parsing config " + e); |
| 3579 | } catch (IOException e) { |
| 3580 | Slog.w(TAG, "Failed parsing config " + e); |
| 3581 | } catch (IndexOutOfBoundsException e) { |
| 3582 | Slog.w(TAG, "Failed parsing config " + e); |
| 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | void writeConfigFileLocked() { |
| 3587 | mHandler.removeMessages(MSG_WRITE_CONFIG); |
| 3588 | mHandler.sendEmptyMessageDelayed(MSG_WRITE_CONFIG, 5000); |
| 3589 | } |
| 3590 | |
| 3591 | void handleWriteConfigFile() { |
| 3592 | final ByteArrayOutputStream memStream = new ByteArrayOutputStream(); |
| 3593 | |
| 3594 | try { |
| 3595 | synchronized (this) { |
| 3596 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 3597 | out.setOutput(memStream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3598 | writeConfigFileLocked(out); |
| 3599 | } |
| 3600 | } catch (IOException e) { |
| 3601 | } |
| 3602 | |
| 3603 | synchronized (mConfigFile) { |
| 3604 | FileOutputStream stream = null; |
| 3605 | try { |
| 3606 | stream = mConfigFile.startWrite(); |
| 3607 | memStream.writeTo(stream); |
| 3608 | stream.flush(); |
| 3609 | FileUtils.sync(stream); |
| 3610 | stream.close(); |
| 3611 | mConfigFile.finishWrite(stream); |
| 3612 | } catch (IOException e) { |
| 3613 | Slog.w(TAG, "Error writing config file", e); |
| 3614 | mConfigFile.failWrite(stream); |
| 3615 | } |
| 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | void writeConfigFileLocked(XmlSerializer out) throws IOException { |
| 3620 | out.startDocument(null, true); |
| 3621 | out.startTag(null, "config"); |
| 3622 | for (int i=0; i<mPowerSaveWhitelistUserApps.size(); i++) { |
| 3623 | String name = mPowerSaveWhitelistUserApps.keyAt(i); |
| 3624 | out.startTag(null, "wl"); |
| 3625 | out.attribute(null, "n", name); |
| 3626 | out.endTag(null, "wl"); |
| 3627 | } |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 3628 | for (int i = 0; i < mRemovedFromSystemWhitelistApps.size(); i++) { |
| 3629 | out.startTag(null, "un-wl"); |
| 3630 | out.attribute(null, "n", mRemovedFromSystemWhitelistApps.keyAt(i)); |
| 3631 | out.endTag(null, "un-wl"); |
| 3632 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3633 | out.endTag(null, "config"); |
| 3634 | out.endDocument(); |
| 3635 | } |
| 3636 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3637 | static void dumpHelp(PrintWriter pw) { |
| 3638 | pw.println("Device idle controller (deviceidle) commands:"); |
| 3639 | pw.println(" help"); |
| 3640 | pw.println(" Print this help text."); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3641 | pw.println(" step [light|deep]"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3642 | pw.println(" Immediately step to next state, without waiting for alarm."); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3643 | pw.println(" force-idle [light|deep]"); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 3644 | pw.println(" Force directly into idle mode, regardless of other device state."); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3645 | pw.println(" force-inactive"); |
| 3646 | pw.println(" Force to be inactive, ready to freely step idle states."); |
| 3647 | pw.println(" unforce"); |
| 3648 | pw.println(" Resume normal functioning after force-idle or force-inactive."); |
| 3649 | pw.println(" get [light|deep|force|screen|charging|network]"); |
| 3650 | pw.println(" Retrieve the current given state."); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3651 | pw.println(" disable [light|deep|all]"); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3652 | pw.println(" Completely disable device idle mode."); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3653 | pw.println(" enable [light|deep|all]"); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 3654 | pw.println(" Re-enable device idle mode after it had previously been disabled."); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3655 | pw.println(" enabled [light|deep|all]"); |
Dianne Hackborn | 9261703 | 2015-06-19 15:32:19 -0700 | [diff] [blame] | 3656 | pw.println(" Print 1 if device idle mode is currently enabled, else 0."); |
Dianne Hackborn | 1b13968 | 2015-07-06 15:13:37 -0700 | [diff] [blame] | 3657 | pw.println(" whitelist"); |
| 3658 | pw.println(" Print currently whitelisted apps."); |
Dianne Hackborn | 9261703 | 2015-06-19 15:32:19 -0700 | [diff] [blame] | 3659 | pw.println(" whitelist [package ...]"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3660 | pw.println(" Add (prefix with +) or remove (prefix with -) packages."); |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 3661 | pw.println(" sys-whitelist [package ...|reset]"); |
| 3662 | pw.println(" Prefix the package with '-' to remove it from the system whitelist or '+'" |
| 3663 | + " to put it back in the system whitelist."); |
| 3664 | pw.println(" Note that only packages that were" |
| 3665 | + " earlier removed from the system whitelist can be added back."); |
| 3666 | pw.println(" reset will reset the whitelist to the original state"); |
| 3667 | pw.println(" Prints the system whitelist if no arguments are specified"); |
Sudheer Shanka | 3f4d770 | 2017-04-28 17:38:03 -0700 | [diff] [blame] | 3668 | pw.println(" except-idle-whitelist [package ...|reset]"); |
| 3669 | pw.println(" Prefix the package with '+' to add it to whitelist or " |
| 3670 | + "'=' to check if it is already whitelisted"); |
| 3671 | pw.println(" [reset] will reset the whitelist to it's original state"); |
| 3672 | pw.println(" Note that unlike <whitelist> cmd, " |
| 3673 | + "changes made using this won't be persisted across boots"); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3674 | pw.println(" tempwhitelist"); |
| 3675 | pw.println(" Print packages that are temporarily whitelisted."); |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 3676 | pw.println(" tempwhitelist [-u USER] [-d DURATION] [-r] [package]"); |
| 3677 | pw.println(" Temporarily place package in whitelist for DURATION milliseconds."); |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 3678 | pw.println(" If no DURATION is specified, 10 seconds is used"); |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 3679 | pw.println(" If [-r] option is used, then the package is removed from temp whitelist " |
| 3680 | + "and any [-d] is ignored"); |
Amith Yamasani | 4cb4257 | 2018-04-27 10:02:57 -0700 | [diff] [blame] | 3681 | pw.println(" motion"); |
| 3682 | pw.println(" Simulate a motion event to bring the device out of deep doze"); |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 3683 | pw.println(" pre-idle-factor [0|1|2]"); |
| 3684 | pw.println(" Set a new factor to idle time before step to idle" |
| 3685 | + "(inactive_to and idle_after_inactive_to)"); |
| 3686 | pw.println(" reset-pre-idle-factor"); |
| 3687 | pw.println(" Reset factor to idle time to default"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3688 | } |
| 3689 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3690 | class Shell extends ShellCommand { |
| 3691 | int userId = UserHandle.USER_SYSTEM; |
| 3692 | |
| 3693 | @Override |
| 3694 | public int onCommand(String cmd) { |
| 3695 | return onShellCommand(this, cmd); |
| 3696 | } |
| 3697 | |
| 3698 | @Override |
| 3699 | public void onHelp() { |
| 3700 | PrintWriter pw = getOutPrintWriter(); |
| 3701 | dumpHelp(pw); |
| 3702 | } |
| 3703 | } |
| 3704 | |
| 3705 | int onShellCommand(Shell shell, String cmd) { |
| 3706 | PrintWriter pw = shell.getOutPrintWriter(); |
| 3707 | if ("step".equals(cmd)) { |
| 3708 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 3709 | null); |
| 3710 | synchronized (this) { |
| 3711 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3712 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3713 | try { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3714 | if (arg == null || "deep".equals(arg)) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3715 | stepIdleStateLocked("s:shell"); |
| 3716 | pw.print("Stepped to deep: "); |
| 3717 | pw.println(stateToString(mState)); |
| 3718 | } else if ("light".equals(arg)) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 3719 | stepLightIdleStateLocked("s:shell"); |
| 3720 | pw.print("Stepped to light: "); pw.println(lightStateToString(mLightState)); |
| 3721 | } else { |
| 3722 | pw.println("Unknown idle mode: " + arg); |
| 3723 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3724 | } finally { |
| 3725 | Binder.restoreCallingIdentity(token); |
| 3726 | } |
| 3727 | } |
| 3728 | } else if ("force-idle".equals(cmd)) { |
| 3729 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 3730 | null); |
| 3731 | synchronized (this) { |
| 3732 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3733 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3734 | try { |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3735 | if (arg == null || "deep".equals(arg)) { |
| 3736 | if (!mDeepEnabled) { |
| 3737 | pw.println("Unable to go deep idle; not enabled"); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3738 | return -1; |
| 3739 | } |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3740 | mForceIdle = true; |
| 3741 | becomeInactiveIfAppropriateLocked(); |
| 3742 | int curState = mState; |
| 3743 | while (curState != STATE_IDLE) { |
| 3744 | stepIdleStateLocked("s:shell"); |
| 3745 | if (curState == mState) { |
| 3746 | pw.print("Unable to go deep idle; stopped at "); |
| 3747 | pw.println(stateToString(mState)); |
| 3748 | exitForceIdleLocked(); |
| 3749 | return -1; |
| 3750 | } |
| 3751 | curState = mState; |
| 3752 | } |
| 3753 | pw.println("Now forced in to deep idle mode"); |
| 3754 | } else if ("light".equals(arg)) { |
| 3755 | mForceIdle = true; |
| 3756 | becomeInactiveIfAppropriateLocked(); |
| 3757 | int curLightState = mLightState; |
| 3758 | while (curLightState != LIGHT_STATE_IDLE) { |
Tej Singh | 93cf3e3 | 2017-12-07 13:05:38 -0800 | [diff] [blame] | 3759 | stepLightIdleStateLocked("s:shell"); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3760 | if (curLightState == mLightState) { |
| 3761 | pw.print("Unable to go light idle; stopped at "); |
| 3762 | pw.println(lightStateToString(mLightState)); |
| 3763 | exitForceIdleLocked(); |
| 3764 | return -1; |
| 3765 | } |
| 3766 | curLightState = mLightState; |
| 3767 | } |
| 3768 | pw.println("Now forced in to light idle mode"); |
| 3769 | } else { |
| 3770 | pw.println("Unknown idle mode: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3771 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3772 | } finally { |
| 3773 | Binder.restoreCallingIdentity(token); |
| 3774 | } |
| 3775 | } |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3776 | } else if ("force-inactive".equals(cmd)) { |
| 3777 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 3778 | null); |
| 3779 | synchronized (this) { |
| 3780 | long token = Binder.clearCallingIdentity(); |
| 3781 | try { |
| 3782 | mForceIdle = true; |
| 3783 | becomeInactiveIfAppropriateLocked(); |
| 3784 | pw.print("Light state: "); |
| 3785 | pw.print(lightStateToString(mLightState)); |
| 3786 | pw.print(", deep state: "); |
| 3787 | pw.println(stateToString(mState)); |
| 3788 | } finally { |
| 3789 | Binder.restoreCallingIdentity(token); |
| 3790 | } |
| 3791 | } |
| 3792 | } else if ("unforce".equals(cmd)) { |
| 3793 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 3794 | null); |
| 3795 | synchronized (this) { |
| 3796 | long token = Binder.clearCallingIdentity(); |
| 3797 | try { |
| 3798 | exitForceIdleLocked(); |
| 3799 | pw.print("Light state: "); |
| 3800 | pw.print(lightStateToString(mLightState)); |
| 3801 | pw.print(", deep state: "); |
| 3802 | pw.println(stateToString(mState)); |
| 3803 | } finally { |
| 3804 | Binder.restoreCallingIdentity(token); |
| 3805 | } |
| 3806 | } |
| 3807 | } else if ("get".equals(cmd)) { |
| 3808 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 3809 | null); |
| 3810 | synchronized (this) { |
| 3811 | String arg = shell.getNextArg(); |
| 3812 | if (arg != null) { |
| 3813 | long token = Binder.clearCallingIdentity(); |
| 3814 | try { |
| 3815 | switch (arg) { |
| 3816 | case "light": pw.println(lightStateToString(mLightState)); break; |
| 3817 | case "deep": pw.println(stateToString(mState)); break; |
| 3818 | case "force": pw.println(mForceIdle); break; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 3819 | case "quick": pw.println(mQuickDozeActivated); break; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 3820 | case "screen": pw.println(mScreenOn); break; |
| 3821 | case "charging": pw.println(mCharging); break; |
| 3822 | case "network": pw.println(mNetworkConnected); break; |
| 3823 | default: pw.println("Unknown get option: " + arg); break; |
| 3824 | } |
| 3825 | } finally { |
| 3826 | Binder.restoreCallingIdentity(token); |
| 3827 | } |
| 3828 | } else { |
| 3829 | pw.println("Argument required"); |
| 3830 | } |
| 3831 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3832 | } else if ("disable".equals(cmd)) { |
| 3833 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 3834 | null); |
| 3835 | synchronized (this) { |
| 3836 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3837 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3838 | try { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3839 | boolean becomeActive = false; |
| 3840 | boolean valid = false; |
| 3841 | if (arg == null || "deep".equals(arg) || "all".equals(arg)) { |
| 3842 | valid = true; |
| 3843 | if (mDeepEnabled) { |
| 3844 | mDeepEnabled = false; |
| 3845 | becomeActive = true; |
| 3846 | pw.println("Deep idle mode disabled"); |
| 3847 | } |
| 3848 | } |
| 3849 | if (arg == null || "light".equals(arg) || "all".equals(arg)) { |
| 3850 | valid = true; |
| 3851 | if (mLightEnabled) { |
| 3852 | mLightEnabled = false; |
| 3853 | becomeActive = true; |
| 3854 | pw.println("Light idle mode disabled"); |
| 3855 | } |
| 3856 | } |
| 3857 | if (becomeActive) { |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 3858 | mActiveReason = ACTIVE_REASON_FORCED; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3859 | becomeActiveLocked((arg == null ? "all" : arg) + "-disabled", |
| 3860 | Process.myUid()); |
| 3861 | } |
| 3862 | if (!valid) { |
| 3863 | pw.println("Unknown idle mode: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3864 | } |
| 3865 | } finally { |
| 3866 | Binder.restoreCallingIdentity(token); |
| 3867 | } |
| 3868 | } |
| 3869 | } else if ("enable".equals(cmd)) { |
| 3870 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 3871 | null); |
| 3872 | synchronized (this) { |
| 3873 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3874 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3875 | try { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3876 | boolean becomeInactive = false; |
| 3877 | boolean valid = false; |
| 3878 | if (arg == null || "deep".equals(arg) || "all".equals(arg)) { |
| 3879 | valid = true; |
| 3880 | if (!mDeepEnabled) { |
| 3881 | mDeepEnabled = true; |
| 3882 | becomeInactive = true; |
| 3883 | pw.println("Deep idle mode enabled"); |
| 3884 | } |
| 3885 | } |
| 3886 | if (arg == null || "light".equals(arg) || "all".equals(arg)) { |
| 3887 | valid = true; |
| 3888 | if (!mLightEnabled) { |
| 3889 | mLightEnabled = true; |
| 3890 | becomeInactive = true; |
| 3891 | pw.println("Light idle mode enable"); |
| 3892 | } |
| 3893 | } |
| 3894 | if (becomeInactive) { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3895 | becomeInactiveIfAppropriateLocked(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3896 | } |
| 3897 | if (!valid) { |
| 3898 | pw.println("Unknown idle mode: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3899 | } |
| 3900 | } finally { |
| 3901 | Binder.restoreCallingIdentity(token); |
| 3902 | } |
| 3903 | } |
| 3904 | } else if ("enabled".equals(cmd)) { |
| 3905 | synchronized (this) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 3906 | String arg = shell.getNextArg(); |
| 3907 | if (arg == null || "all".equals(arg)) { |
| 3908 | pw.println(mDeepEnabled && mLightEnabled ? "1" : 0); |
| 3909 | } else if ("deep".equals(arg)) { |
| 3910 | pw.println(mDeepEnabled ? "1" : 0); |
| 3911 | } else if ("light".equals(arg)) { |
| 3912 | pw.println(mLightEnabled ? "1" : 0); |
| 3913 | } else { |
| 3914 | pw.println("Unknown idle mode: " + arg); |
| 3915 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3916 | } |
| 3917 | } else if ("whitelist".equals(cmd)) { |
Dianne Hackborn | eb909e3 | 2016-09-29 14:35:15 -0700 | [diff] [blame] | 3918 | String arg = shell.getNextArg(); |
| 3919 | if (arg != null) { |
| 3920 | getContext().enforceCallingOrSelfPermission( |
| 3921 | android.Manifest.permission.DEVICE_POWER, null); |
| 3922 | long token = Binder.clearCallingIdentity(); |
| 3923 | try { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3924 | do { |
| 3925 | if (arg.length() < 1 || (arg.charAt(0) != '-' |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 3926 | && arg.charAt(0) != '+' && arg.charAt(0) != '=')) { |
| 3927 | pw.println("Package must be prefixed with +, -, or =: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3928 | return -1; |
| 3929 | } |
| 3930 | char op = arg.charAt(0); |
| 3931 | String pkg = arg.substring(1); |
| 3932 | if (op == '+') { |
| 3933 | if (addPowerSaveWhitelistAppInternal(pkg)) { |
| 3934 | pw.println("Added: " + pkg); |
| 3935 | } else { |
| 3936 | pw.println("Unknown package: " + pkg); |
| 3937 | } |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 3938 | } else if (op == '-') { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3939 | if (removePowerSaveWhitelistAppInternal(pkg)) { |
| 3940 | pw.println("Removed: " + pkg); |
| 3941 | } |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 3942 | } else { |
| 3943 | pw.println(getPowerSaveWhitelistAppInternal(pkg)); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3944 | } |
| 3945 | } while ((arg=shell.getNextArg()) != null); |
Dianne Hackborn | eb909e3 | 2016-09-29 14:35:15 -0700 | [diff] [blame] | 3946 | } finally { |
| 3947 | Binder.restoreCallingIdentity(token); |
| 3948 | } |
| 3949 | } else { |
| 3950 | synchronized (this) { |
| 3951 | for (int j=0; j<mPowerSaveWhitelistAppsExceptIdle.size(); j++) { |
| 3952 | pw.print("system-excidle,"); |
| 3953 | pw.print(mPowerSaveWhitelistAppsExceptIdle.keyAt(j)); |
| 3954 | pw.print(","); |
| 3955 | pw.println(mPowerSaveWhitelistAppsExceptIdle.valueAt(j)); |
| 3956 | } |
| 3957 | for (int j=0; j<mPowerSaveWhitelistApps.size(); j++) { |
| 3958 | pw.print("system,"); |
| 3959 | pw.print(mPowerSaveWhitelistApps.keyAt(j)); |
| 3960 | pw.print(","); |
| 3961 | pw.println(mPowerSaveWhitelistApps.valueAt(j)); |
| 3962 | } |
| 3963 | for (int j=0; j<mPowerSaveWhitelistUserApps.size(); j++) { |
| 3964 | pw.print("user,"); |
| 3965 | pw.print(mPowerSaveWhitelistUserApps.keyAt(j)); |
| 3966 | pw.print(","); |
| 3967 | pw.println(mPowerSaveWhitelistUserApps.valueAt(j)); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3968 | } |
| 3969 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3970 | } |
| 3971 | } else if ("tempwhitelist".equals(cmd)) { |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 3972 | long duration = 10000; |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 3973 | boolean removePkg = false; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3974 | String opt; |
| 3975 | while ((opt=shell.getNextOption()) != null) { |
| 3976 | if ("-u".equals(opt)) { |
| 3977 | opt = shell.getNextArg(); |
| 3978 | if (opt == null) { |
| 3979 | pw.println("-u requires a user number"); |
| 3980 | return -1; |
| 3981 | } |
| 3982 | shell.userId = Integer.parseInt(opt); |
Dianne Hackborn | 85e3564 | 2017-01-12 15:10:57 -0800 | [diff] [blame] | 3983 | } else if ("-d".equals(opt)) { |
| 3984 | opt = shell.getNextArg(); |
| 3985 | if (opt == null) { |
| 3986 | pw.println("-d requires a duration"); |
| 3987 | return -1; |
| 3988 | } |
| 3989 | duration = Long.parseLong(opt); |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 3990 | } else if ("-r".equals(opt)) { |
| 3991 | removePkg = true; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | String arg = shell.getNextArg(); |
| 3995 | if (arg != null) { |
| 3996 | try { |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 3997 | if (removePkg) { |
| 3998 | removePowerSaveTempWhitelistAppChecked(arg, shell.userId); |
| 3999 | } else { |
| 4000 | addPowerSaveTempWhitelistAppChecked(arg, duration, shell.userId, "shell"); |
| 4001 | } |
Christopher Tate | ec3a9f3 | 2017-03-21 17:43:47 -0700 | [diff] [blame] | 4002 | } catch (Exception e) { |
| 4003 | pw.println("Failed: " + e); |
| 4004 | return -1; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 4005 | } |
Sudheer Shanka | 326b311 | 2017-11-27 14:40:57 -0800 | [diff] [blame] | 4006 | } else if (removePkg) { |
| 4007 | pw.println("[-r] requires a package name"); |
| 4008 | return -1; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 4009 | } else { |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4010 | dumpTempWhitelistSchedule(pw, false); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 4011 | } |
Sudheer Shanka | 3f4d770 | 2017-04-28 17:38:03 -0700 | [diff] [blame] | 4012 | } else if ("except-idle-whitelist".equals(cmd)) { |
| 4013 | getContext().enforceCallingOrSelfPermission( |
| 4014 | android.Manifest.permission.DEVICE_POWER, null); |
| 4015 | final long token = Binder.clearCallingIdentity(); |
| 4016 | try { |
| 4017 | String arg = shell.getNextArg(); |
| 4018 | if (arg == null) { |
| 4019 | pw.println("No arguments given"); |
| 4020 | return -1; |
| 4021 | } else if ("reset".equals(arg)) { |
| 4022 | resetPowerSaveWhitelistExceptIdleInternal(); |
| 4023 | } else { |
| 4024 | do { |
| 4025 | if (arg.length() < 1 || (arg.charAt(0) != '-' |
| 4026 | && arg.charAt(0) != '+' && arg.charAt(0) != '=')) { |
| 4027 | pw.println("Package must be prefixed with +, -, or =: " + arg); |
| 4028 | return -1; |
| 4029 | } |
| 4030 | char op = arg.charAt(0); |
| 4031 | String pkg = arg.substring(1); |
| 4032 | if (op == '+') { |
| 4033 | if (addPowerSaveWhitelistExceptIdleInternal(pkg)) { |
| 4034 | pw.println("Added: " + pkg); |
| 4035 | } else { |
| 4036 | pw.println("Unknown package: " + pkg); |
| 4037 | } |
| 4038 | } else if (op == '=') { |
| 4039 | pw.println(getPowerSaveWhitelistExceptIdleInternal(pkg)); |
| 4040 | } else { |
| 4041 | pw.println("Unknown argument: " + arg); |
| 4042 | return -1; |
| 4043 | } |
| 4044 | } while ((arg = shell.getNextArg()) != null); |
| 4045 | } |
| 4046 | } finally { |
| 4047 | Binder.restoreCallingIdentity(token); |
| 4048 | } |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 4049 | } else if ("sys-whitelist".equals(cmd)) { |
| 4050 | String arg = shell.getNextArg(); |
| 4051 | if (arg != null) { |
| 4052 | getContext().enforceCallingOrSelfPermission( |
| 4053 | android.Manifest.permission.DEVICE_POWER, null); |
| 4054 | final long token = Binder.clearCallingIdentity(); |
| 4055 | try { |
| 4056 | if ("reset".equals(arg)) { |
| 4057 | resetSystemPowerWhitelistInternal(); |
| 4058 | } else { |
| 4059 | do { |
| 4060 | if (arg.length() < 1 |
| 4061 | || (arg.charAt(0) != '-' && arg.charAt(0) != '+')) { |
| 4062 | pw.println("Package must be prefixed with + or - " + arg); |
| 4063 | return -1; |
| 4064 | } |
| 4065 | final char op = arg.charAt(0); |
| 4066 | final String pkg = arg.substring(1); |
| 4067 | switch (op) { |
| 4068 | case '+': |
| 4069 | if (restoreSystemPowerWhitelistAppInternal(pkg)) { |
| 4070 | pw.println("Restored " + pkg); |
| 4071 | } |
| 4072 | break; |
| 4073 | case '-': |
| 4074 | if (removeSystemPowerWhitelistAppInternal(pkg)) { |
| 4075 | pw.println("Removed " + pkg); |
| 4076 | } |
| 4077 | break; |
| 4078 | } |
| 4079 | } while ((arg = shell.getNextArg()) != null); |
| 4080 | } |
| 4081 | } finally { |
| 4082 | Binder.restoreCallingIdentity(token); |
| 4083 | } |
| 4084 | } else { |
| 4085 | synchronized (this) { |
Amith Yamasani | 4cb4257 | 2018-04-27 10:02:57 -0700 | [diff] [blame] | 4086 | for (int j = 0; j < mPowerSaveWhitelistApps.size(); j++) { |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 4087 | pw.print(mPowerSaveWhitelistApps.keyAt(j)); |
| 4088 | pw.print(","); |
| 4089 | pw.println(mPowerSaveWhitelistApps.valueAt(j)); |
| 4090 | } |
| 4091 | } |
| 4092 | } |
Amith Yamasani | 4cb4257 | 2018-04-27 10:02:57 -0700 | [diff] [blame] | 4093 | } else if ("motion".equals(cmd)) { |
| 4094 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 4095 | null); |
| 4096 | synchronized (this) { |
| 4097 | long token = Binder.clearCallingIdentity(); |
| 4098 | try { |
| 4099 | motionLocked(); |
| 4100 | pw.print("Light state: "); |
| 4101 | pw.print(lightStateToString(mLightState)); |
| 4102 | pw.print(", deep state: "); |
| 4103 | pw.println(stateToString(mState)); |
| 4104 | } finally { |
| 4105 | Binder.restoreCallingIdentity(token); |
| 4106 | } |
| 4107 | } |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 4108 | } else if ("pre-idle-factor".equals(cmd)) { |
| 4109 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 4110 | null); |
| 4111 | synchronized (this) { |
| 4112 | long token = Binder.clearCallingIdentity(); |
| 4113 | int ret = SET_IDLE_FACTOR_RESULT_UNINIT; |
| 4114 | try { |
| 4115 | String arg = shell.getNextArg(); |
| 4116 | boolean valid = false; |
| 4117 | int mode = 0; |
| 4118 | if (arg != null) { |
| 4119 | mode = Integer.parseInt(arg); |
| 4120 | ret = setPreIdleTimeoutMode(mode); |
| 4121 | if (ret == SET_IDLE_FACTOR_RESULT_OK) { |
| 4122 | pw.println("pre-idle-factor: " + mode); |
| 4123 | valid = true; |
| 4124 | } else if (ret == SET_IDLE_FACTOR_RESULT_NOT_SUPPORT) { |
| 4125 | valid = true; |
| 4126 | pw.println("Deep idle not supported"); |
| 4127 | } else if (ret == SET_IDLE_FACTOR_RESULT_IGNORED) { |
| 4128 | valid = true; |
| 4129 | pw.println("Idle timeout factor not changed"); |
| 4130 | } |
| 4131 | } |
| 4132 | if (!valid) { |
| 4133 | pw.println("Unknown idle timeout factor: " + arg |
| 4134 | + ",(error code: " + ret + ")"); |
| 4135 | } |
| 4136 | } catch (NumberFormatException e) { |
| 4137 | pw.println("Unknown idle timeout factor" |
| 4138 | + ",(error code: " + ret + ")"); |
| 4139 | } finally { |
| 4140 | Binder.restoreCallingIdentity(token); |
| 4141 | } |
| 4142 | } |
| 4143 | } else if ("reset-pre-idle-factor".equals(cmd)) { |
| 4144 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 4145 | null); |
| 4146 | synchronized (this) { |
| 4147 | long token = Binder.clearCallingIdentity(); |
| 4148 | try { |
| 4149 | resetPreIdleTimeoutMode(); |
| 4150 | } finally { |
| 4151 | Binder.restoreCallingIdentity(token); |
| 4152 | } |
| 4153 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 4154 | } else { |
| 4155 | return shell.handleDefaultCommands(cmd); |
| 4156 | } |
| 4157 | return 0; |
| 4158 | } |
| 4159 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4160 | void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 4161 | if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4162 | |
| 4163 | if (args != null) { |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 4164 | int userId = UserHandle.USER_SYSTEM; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4165 | for (int i=0; i<args.length; i++) { |
| 4166 | String arg = args[i]; |
| 4167 | if ("-h".equals(arg)) { |
| 4168 | dumpHelp(pw); |
| 4169 | return; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 4170 | } else if ("-u".equals(arg)) { |
| 4171 | i++; |
| 4172 | if (i < args.length) { |
| 4173 | arg = args[i]; |
| 4174 | userId = Integer.parseInt(arg); |
| 4175 | } |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 4176 | } else if ("-a".equals(arg)) { |
| 4177 | // Ignore, we always dump all. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4178 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
| 4179 | pw.println("Unknown option: " + arg); |
| 4180 | return; |
| 4181 | } else { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 4182 | Shell shell = new Shell(); |
| 4183 | shell.userId = userId; |
| 4184 | String[] newArgs = new String[args.length-i]; |
| 4185 | System.arraycopy(args, i, newArgs, 0, args.length-i); |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 4186 | shell.exec(mBinderService, null, fd, null, newArgs, null, |
| 4187 | new ResultReceiver(null)); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4188 | return; |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | synchronized (this) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 4194 | mConstants.dump(pw); |
| 4195 | |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 4196 | if (mEventCmds[0] != EVENT_NULL) { |
| 4197 | pw.println(" Idling history:"); |
| 4198 | long now = SystemClock.elapsedRealtime(); |
| 4199 | for (int i=EVENT_BUFFER_SIZE-1; i>=0; i--) { |
| 4200 | int cmd = mEventCmds[i]; |
| 4201 | if (cmd == EVENT_NULL) { |
| 4202 | continue; |
| 4203 | } |
| 4204 | String label; |
| 4205 | switch (mEventCmds[i]) { |
| 4206 | case EVENT_NORMAL: label = " normal"; break; |
| 4207 | case EVENT_LIGHT_IDLE: label = " light-idle"; break; |
| 4208 | case EVENT_LIGHT_MAINTENANCE: label = "light-maint"; break; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 4209 | case EVENT_DEEP_IDLE: label = " deep-idle"; break; |
| 4210 | case EVENT_DEEP_MAINTENANCE: label = " deep-maint"; break; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 4211 | default: label = " ??"; break; |
| 4212 | } |
| 4213 | pw.print(" "); |
| 4214 | pw.print(label); |
| 4215 | pw.print(": "); |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 4216 | TimeUtils.formatDuration(mEventTimes[i], now, pw); |
| 4217 | if (mEventReasons[i] != null) { |
| 4218 | pw.print(" ("); |
| 4219 | pw.print(mEventReasons[i]); |
| 4220 | pw.print(")"); |
| 4221 | } |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 4222 | pw.println(); |
Amith Yamasani | ac6517a | 2018-04-23 12:19:34 -0700 | [diff] [blame] | 4223 | |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 4224 | } |
| 4225 | } |
| 4226 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4227 | int size = mPowerSaveWhitelistAppsExceptIdle.size(); |
| 4228 | if (size > 0) { |
| 4229 | pw.println(" Whitelist (except idle) system apps:"); |
| 4230 | for (int i = 0; i < size; i++) { |
| 4231 | pw.print(" "); |
| 4232 | pw.println(mPowerSaveWhitelistAppsExceptIdle.keyAt(i)); |
| 4233 | } |
| 4234 | } |
| 4235 | size = mPowerSaveWhitelistApps.size(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4236 | if (size > 0) { |
| 4237 | pw.println(" Whitelist system apps:"); |
| 4238 | for (int i = 0; i < size; i++) { |
| 4239 | pw.print(" "); |
| 4240 | pw.println(mPowerSaveWhitelistApps.keyAt(i)); |
| 4241 | } |
| 4242 | } |
Suprabh Shukla | 0810564 | 2017-09-26 14:45:30 -0700 | [diff] [blame] | 4243 | size = mRemovedFromSystemWhitelistApps.size(); |
| 4244 | if (size > 0) { |
| 4245 | pw.println(" Removed from whitelist system apps:"); |
| 4246 | for (int i = 0; i < size; i++) { |
| 4247 | pw.print(" "); |
| 4248 | pw.println(mRemovedFromSystemWhitelistApps.keyAt(i)); |
| 4249 | } |
| 4250 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4251 | size = mPowerSaveWhitelistUserApps.size(); |
| 4252 | if (size > 0) { |
| 4253 | pw.println(" Whitelist user apps:"); |
| 4254 | for (int i = 0; i < size; i++) { |
| 4255 | pw.print(" "); |
| 4256 | pw.println(mPowerSaveWhitelistUserApps.keyAt(i)); |
| 4257 | } |
| 4258 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4259 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 4260 | if (size > 0) { |
| 4261 | pw.println(" Whitelist (except idle) all app ids:"); |
| 4262 | for (int i = 0; i < size; i++) { |
| 4263 | pw.print(" "); |
| 4264 | pw.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 4265 | pw.println(); |
| 4266 | } |
| 4267 | } |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 4268 | size = mPowerSaveWhitelistUserAppIds.size(); |
| 4269 | if (size > 0) { |
| 4270 | pw.println(" Whitelist user app ids:"); |
| 4271 | for (int i = 0; i < size; i++) { |
| 4272 | pw.print(" "); |
| 4273 | pw.print(mPowerSaveWhitelistUserAppIds.keyAt(i)); |
| 4274 | pw.println(); |
| 4275 | } |
| 4276 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 4277 | size = mPowerSaveWhitelistAllAppIds.size(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4278 | if (size > 0) { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 4279 | pw.println(" Whitelist all app ids:"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4280 | for (int i = 0; i < size; i++) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 4281 | pw.print(" "); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 4282 | pw.print(mPowerSaveWhitelistAllAppIds.keyAt(i)); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4283 | pw.println(); |
| 4284 | } |
| 4285 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4286 | dumpTempWhitelistSchedule(pw, true); |
| 4287 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 4288 | size = mTempWhitelistAppIdArray != null ? mTempWhitelistAppIdArray.length : 0; |
| 4289 | if (size > 0) { |
| 4290 | pw.println(" Temp whitelist app ids:"); |
| 4291 | for (int i = 0; i < size; i++) { |
| 4292 | pw.print(" "); |
| 4293 | pw.print(mTempWhitelistAppIdArray[i]); |
| 4294 | pw.println(); |
| 4295 | } |
| 4296 | } |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 4297 | |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 4298 | pw.print(" mLightEnabled="); pw.print(mLightEnabled); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4299 | pw.print(" mDeepEnabled="); pw.println(mDeepEnabled); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 4300 | pw.print(" mForceIdle="); pw.println(mForceIdle); |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 4301 | pw.print(" mUseMotionSensor="); pw.print(mUseMotionSensor); |
| 4302 | if (mUseMotionSensor) { |
| 4303 | pw.print(" mMotionSensor="); pw.println(mMotionSensor); |
| 4304 | } else { |
| 4305 | pw.println(); |
| 4306 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4307 | pw.print(" mScreenOn="); pw.println(mScreenOn); |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 4308 | pw.print(" mScreenLocked="); pw.println(mScreenLocked); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 4309 | pw.print(" mNetworkConnected="); pw.println(mNetworkConnected); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4310 | pw.print(" mCharging="); pw.println(mCharging); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 4311 | if (mConstraints.size() != 0) { |
| 4312 | pw.println(" mConstraints={"); |
| 4313 | for (int i = 0; i < mConstraints.size(); i++) { |
| 4314 | final DeviceIdleConstraintTracker tracker = mConstraints.valueAt(i); |
| 4315 | pw.print(" \""); pw.print(tracker.name); pw.print("\"="); |
| 4316 | if (tracker.minState == mState) { |
| 4317 | pw.println(tracker.active); |
| 4318 | } else { |
| 4319 | pw.print("ignored <mMinState="); pw.print(stateToString(tracker.minState)); |
| 4320 | pw.println(">"); |
| 4321 | } |
| 4322 | } |
| 4323 | pw.println(" }"); |
| 4324 | } |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 4325 | if (mUseMotionSensor) { |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 4326 | pw.print(" mMotionActive="); pw.println(mMotionListener.active); |
Robin Lee | c4d424c | 2018-12-07 15:09:13 +0100 | [diff] [blame] | 4327 | pw.print(" mNotMoving="); pw.println(mNotMoving); |
| 4328 | } |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 4329 | pw.print(" mLocating="); pw.print(mLocating); pw.print(" mHasGps="); |
| 4330 | pw.print(mHasGps); pw.print(" mHasNetwork="); |
| 4331 | pw.print(mHasNetworkLocation); pw.print(" mLocated="); pw.println(mLocated); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 4332 | if (mLastGenericLocation != null) { |
| 4333 | pw.print(" mLastGenericLocation="); pw.println(mLastGenericLocation); |
| 4334 | } |
| 4335 | if (mLastGpsLocation != null) { |
| 4336 | pw.print(" mLastGpsLocation="); pw.println(mLastGpsLocation); |
| 4337 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 4338 | pw.print(" mState="); pw.print(stateToString(mState)); |
| 4339 | pw.print(" mLightState="); |
| 4340 | pw.println(lightStateToString(mLightState)); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4341 | pw.print(" mInactiveTimeout="); TimeUtils.formatDuration(mInactiveTimeout, pw); |
| 4342 | pw.println(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 4343 | if (mActiveIdleOpCount != 0) { |
| 4344 | pw.print(" mActiveIdleOpCount="); pw.println(mActiveIdleOpCount); |
| 4345 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4346 | if (mNextAlarmTime != 0) { |
| 4347 | pw.print(" mNextAlarmTime="); |
| 4348 | TimeUtils.formatDuration(mNextAlarmTime, SystemClock.elapsedRealtime(), pw); |
| 4349 | pw.println(); |
| 4350 | } |
| 4351 | if (mNextIdlePendingDelay != 0) { |
| 4352 | pw.print(" mNextIdlePendingDelay="); |
| 4353 | TimeUtils.formatDuration(mNextIdlePendingDelay, pw); |
| 4354 | pw.println(); |
| 4355 | } |
| 4356 | if (mNextIdleDelay != 0) { |
| 4357 | pw.print(" mNextIdleDelay="); |
| 4358 | TimeUtils.formatDuration(mNextIdleDelay, pw); |
| 4359 | pw.println(); |
| 4360 | } |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 4361 | if (mNextLightIdleDelay != 0) { |
| 4362 | pw.print(" mNextIdleDelay="); |
| 4363 | TimeUtils.formatDuration(mNextLightIdleDelay, pw); |
| 4364 | pw.println(); |
| 4365 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 4366 | if (mNextLightAlarmTime != 0) { |
| 4367 | pw.print(" mNextLightAlarmTime="); |
| 4368 | TimeUtils.formatDuration(mNextLightAlarmTime, SystemClock.elapsedRealtime(), pw); |
| 4369 | pw.println(); |
| 4370 | } |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 4371 | if (mCurIdleBudget != 0) { |
| 4372 | pw.print(" mCurIdleBudget="); |
| 4373 | TimeUtils.formatDuration(mCurIdleBudget, pw); |
| 4374 | pw.println(); |
| 4375 | } |
| 4376 | if (mMaintenanceStartTime != 0) { |
| 4377 | pw.print(" mMaintenanceStartTime="); |
| 4378 | TimeUtils.formatDuration(mMaintenanceStartTime, SystemClock.elapsedRealtime(), pw); |
| 4379 | pw.println(); |
| 4380 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 4381 | if (mJobsActive) { |
| 4382 | pw.print(" mJobsActive="); pw.println(mJobsActive); |
| 4383 | } |
| 4384 | if (mAlarmsActive) { |
| 4385 | pw.print(" mAlarmsActive="); pw.println(mAlarmsActive); |
| 4386 | } |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 4387 | if (Math.abs(mPreIdleFactor - 1.0f) > MIN_PRE_IDLE_FACTOR_CHANGE) { |
| 4388 | pw.print(" mPreIdleFactor="); pw.println(mPreIdleFactor); |
| 4389 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 4390 | } |
| 4391 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4392 | |
| 4393 | void dumpTempWhitelistSchedule(PrintWriter pw, boolean printTitle) { |
| 4394 | final int size = mTempWhitelistAppIdEndTimes.size(); |
| 4395 | if (size > 0) { |
| 4396 | String prefix = ""; |
| 4397 | if (printTitle) { |
| 4398 | pw.println(" Temp whitelist schedule:"); |
| 4399 | prefix = " "; |
| 4400 | } |
| 4401 | final long timeNow = SystemClock.elapsedRealtime(); |
| 4402 | for (int i = 0; i < size; i++) { |
| 4403 | pw.print(prefix); |
| 4404 | pw.print("UID="); |
| 4405 | pw.print(mTempWhitelistAppIdEndTimes.keyAt(i)); |
| 4406 | pw.print(": "); |
| 4407 | Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.valueAt(i); |
| 4408 | TimeUtils.formatDuration(entry.first.value, timeNow, pw); |
| 4409 | pw.print(" - "); |
| 4410 | pw.println(entry.second); |
| 4411 | } |
| 4412 | } |
| 4413 | } |
| 4414 | } |