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 | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 21 | import android.app.AlarmManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.PackageManager; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager.NameNotFoundException; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 30 | import android.database.ContentObserver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 31 | import android.hardware.Sensor; |
| 32 | import android.hardware.SensorManager; |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 33 | import android.hardware.SensorEvent; |
| 34 | import android.hardware.SensorEventListener; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 35 | import android.hardware.TriggerEvent; |
| 36 | import android.hardware.TriggerEventListener; |
| 37 | import android.hardware.display.DisplayManager; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 38 | import android.location.LocationRequest; |
| 39 | import android.location.Location; |
| 40 | import android.location.LocationListener; |
| 41 | import android.location.LocationManager; |
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; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 46 | import android.os.BatteryStats; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 47 | import android.os.Binder; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 48 | import android.os.Bundle; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 49 | import android.os.Environment; |
| 50 | import android.os.FileUtils; |
| 51 | import android.os.Handler; |
| 52 | import android.os.IDeviceIdleController; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 53 | import android.os.IMaintenanceActivityListener; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 54 | import android.os.Looper; |
| 55 | import android.os.Message; |
| 56 | import android.os.PowerManager; |
| 57 | import android.os.PowerManagerInternal; |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 58 | import android.os.Process; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 59 | import android.os.RemoteCallbackList; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 60 | import android.os.RemoteException; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 61 | import android.os.ResultReceiver; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 62 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 63 | import android.os.ShellCallback; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 64 | import android.os.ShellCommand; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 65 | import android.os.SystemClock; |
| 66 | import android.os.UserHandle; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 67 | import android.provider.Settings; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 68 | import android.util.ArrayMap; |
| 69 | import android.util.ArraySet; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 70 | import android.util.KeyValueListParser; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 71 | import android.util.MutableLong; |
| 72 | import android.util.Pair; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 73 | import android.util.Slog; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 74 | import android.util.SparseArray; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 75 | import android.util.SparseBooleanArray; |
| 76 | import android.util.TimeUtils; |
| 77 | import android.util.Xml; |
| 78 | import android.view.Display; |
Amith Yamasani | 520d8f2 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 79 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 80 | import com.android.internal.app.IBatteryStats; |
| 81 | import com.android.internal.os.AtomicFile; |
| 82 | import com.android.internal.os.BackgroundThread; |
| 83 | import com.android.internal.util.FastXmlSerializer; |
| 84 | import com.android.internal.util.XmlUtils; |
| 85 | import com.android.server.am.BatteryStatsService; |
Amith Yamasani | 520d8f2 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 86 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 87 | import org.xmlpull.v1.XmlPullParser; |
| 88 | import org.xmlpull.v1.XmlPullParserException; |
| 89 | import org.xmlpull.v1.XmlSerializer; |
| 90 | |
| 91 | import java.io.ByteArrayOutputStream; |
| 92 | import java.io.File; |
| 93 | import java.io.FileDescriptor; |
| 94 | import java.io.FileInputStream; |
| 95 | import java.io.FileNotFoundException; |
| 96 | import java.io.FileOutputStream; |
| 97 | import java.io.IOException; |
| 98 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 99 | import java.nio.charset.StandardCharsets; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 100 | import java.util.Arrays; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Keeps track of device idleness and drives low power mode based on that. |
| 104 | */ |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 105 | public class DeviceIdleController extends SystemService |
| 106 | implements AnyMotionDetector.DeviceIdleCallback { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 107 | private static final String TAG = "DeviceIdleController"; |
| 108 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 109 | private static final boolean DEBUG = false; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 110 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 111 | private static final boolean COMPRESS_TIME = false; |
Amith Yamasani | 520d8f2 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 112 | |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 113 | private static final int EVENT_BUFFER_SIZE = 100; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 114 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 115 | private AlarmManager mAlarmManager; |
| 116 | private IBatteryStats mBatteryStats; |
| 117 | private PowerManagerInternal mLocalPowerManager; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 118 | private PowerManager mPowerManager; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 119 | private ConnectivityService mConnectivityService; |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 120 | private AlarmManagerService.LocalService mLocalAlarmManager; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 121 | private INetworkPolicyManager mNetworkPolicyManager; |
| 122 | private DisplayManager mDisplayManager; |
| 123 | private SensorManager mSensorManager; |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 124 | private Sensor mMotionSensor; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 125 | private LocationManager mLocationManager; |
| 126 | private LocationRequest mLocationRequest; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 127 | private Intent mIdleIntent; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 128 | private Intent mLightIdleIntent; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 129 | private Display mCurDisplay; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 130 | private AnyMotionDetector mAnyMotionDetector; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 131 | private boolean mLightEnabled; |
| 132 | private boolean mDeepEnabled; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 133 | private boolean mForceIdle; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 134 | private boolean mNetworkConnected; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 135 | private boolean mScreenOn; |
| 136 | private boolean mCharging; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 137 | private boolean mNotMoving; |
| 138 | private boolean mLocating; |
| 139 | private boolean mLocated; |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 140 | private boolean mHasGps; |
| 141 | private boolean mHasNetworkLocation; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 142 | private Location mLastGenericLocation; |
| 143 | private Location mLastGpsLocation; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 144 | |
| 145 | /** Device is currently active. */ |
| 146 | private static final int STATE_ACTIVE = 0; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 147 | /** Device is inactive (screen off, no motion) and we are waiting to for idle. */ |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 148 | private static final int STATE_INACTIVE = 1; |
| 149 | /** Device is past the initial inactive period, and waiting for the next idle period. */ |
| 150 | private static final int STATE_IDLE_PENDING = 2; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 151 | /** Device is currently sensing motion. */ |
| 152 | private static final int STATE_SENSING = 3; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 153 | /** Device is currently finding location (and may still be sensing). */ |
| 154 | private static final int STATE_LOCATING = 4; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 155 | /** Device is in the idle state, trying to stay asleep as much as possible. */ |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 156 | private static final int STATE_IDLE = 5; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 157 | /** Device is in the idle state, but temporarily out of idle to do regular maintenance. */ |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 158 | private static final int STATE_IDLE_MAINTENANCE = 6; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 159 | private static String stateToString(int state) { |
| 160 | switch (state) { |
| 161 | case STATE_ACTIVE: return "ACTIVE"; |
| 162 | case STATE_INACTIVE: return "INACTIVE"; |
| 163 | case STATE_IDLE_PENDING: return "IDLE_PENDING"; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 164 | case STATE_SENSING: return "SENSING"; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 165 | case STATE_LOCATING: return "LOCATING"; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 166 | case STATE_IDLE: return "IDLE"; |
| 167 | case STATE_IDLE_MAINTENANCE: return "IDLE_MAINTENANCE"; |
| 168 | default: return Integer.toString(state); |
| 169 | } |
| 170 | } |
| 171 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 172 | /** Device is currently active. */ |
| 173 | private static final int LIGHT_STATE_ACTIVE = 0; |
| 174 | /** Device is inactive (screen off) and we are waiting to for the first light idle. */ |
| 175 | private static final int LIGHT_STATE_INACTIVE = 1; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 176 | /** Device is about to go idle for the first time, wait for current work to complete. */ |
| 177 | private static final int LIGHT_STATE_PRE_IDLE = 3; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 178 | /** Device is in the light idle state, trying to stay asleep as much as possible. */ |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 179 | private static final int LIGHT_STATE_IDLE = 4; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 180 | /** Device is in the light idle state, we want to go in to idle maintenance but are |
| 181 | * waiting for network connectivity before doing so. */ |
| 182 | private static final int LIGHT_STATE_WAITING_FOR_NETWORK = 5; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 183 | /** Device is in the light idle state, but temporarily out of idle to do regular maintenance. */ |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 184 | private static final int LIGHT_STATE_IDLE_MAINTENANCE = 6; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 185 | /** Device light idle state is overriden, now applying deep doze state. */ |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 186 | private static final int LIGHT_STATE_OVERRIDE = 7; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 187 | private static String lightStateToString(int state) { |
| 188 | switch (state) { |
| 189 | case LIGHT_STATE_ACTIVE: return "ACTIVE"; |
| 190 | case LIGHT_STATE_INACTIVE: return "INACTIVE"; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 191 | case LIGHT_STATE_PRE_IDLE: return "PRE_IDLE"; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 192 | case LIGHT_STATE_IDLE: return "IDLE"; |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 193 | case LIGHT_STATE_WAITING_FOR_NETWORK: return "WAITING_FOR_NETWORK"; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 194 | case LIGHT_STATE_IDLE_MAINTENANCE: return "IDLE_MAINTENANCE"; |
| 195 | case LIGHT_STATE_OVERRIDE: return "OVERRIDE"; |
| 196 | default: return Integer.toString(state); |
| 197 | } |
| 198 | } |
| 199 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 200 | private int mState; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 201 | private int mLightState; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 202 | |
| 203 | private long mInactiveTimeout; |
| 204 | private long mNextAlarmTime; |
| 205 | private long mNextIdlePendingDelay; |
| 206 | private long mNextIdleDelay; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 207 | private long mNextLightIdleDelay; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 208 | private long mNextLightAlarmTime; |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 209 | private long mNextSensingTimeoutAlarmTime; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 210 | private long mCurIdleBudget; |
| 211 | private long mMaintenanceStartTime; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 212 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 213 | private int mActiveIdleOpCount; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 214 | private PowerManager.WakeLock mActiveIdleWakeLock; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 215 | private boolean mJobsActive; |
| 216 | private boolean mAlarmsActive; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 217 | private boolean mReportedMaintenanceActivity; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 218 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 219 | public final AtomicFile mConfigFile; |
| 220 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 221 | private final RemoteCallbackList<IMaintenanceActivityListener> mMaintenanceActivityListeners = |
| 222 | new RemoteCallbackList<IMaintenanceActivityListener>(); |
| 223 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 224 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 225 | * Package names the system has white-listed to opt out of power save restrictions, |
| 226 | * except for device idle mode. |
| 227 | */ |
| 228 | private final ArrayMap<String, Integer> mPowerSaveWhitelistAppsExceptIdle = new ArrayMap<>(); |
| 229 | |
| 230 | /** |
| 231 | * Package names the system has white-listed to opt out of power save restrictions for |
| 232 | * all modes. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 233 | */ |
| 234 | private final ArrayMap<String, Integer> mPowerSaveWhitelistApps = new ArrayMap<>(); |
| 235 | |
| 236 | /** |
| 237 | * Package names the user has white-listed to opt out of power save restrictions. |
| 238 | */ |
| 239 | private final ArrayMap<String, Integer> mPowerSaveWhitelistUserApps = new ArrayMap<>(); |
| 240 | |
| 241 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 242 | * App IDs of built-in system apps that have been white-listed except for idle modes. |
| 243 | */ |
| 244 | private final SparseBooleanArray mPowerSaveWhitelistSystemAppIdsExceptIdle |
| 245 | = new SparseBooleanArray(); |
| 246 | |
| 247 | /** |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 248 | * App IDs of built-in system apps that have been white-listed. |
| 249 | */ |
| 250 | private final SparseBooleanArray mPowerSaveWhitelistSystemAppIds = new SparseBooleanArray(); |
| 251 | |
| 252 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 253 | * App IDs that have been white-listed to opt out of power save restrictions, except |
| 254 | * for device idle modes. |
| 255 | */ |
| 256 | private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray(); |
| 257 | |
| 258 | /** |
| 259 | * Current app IDs that are in the complete power save white list, but shouldn't be |
| 260 | * excluded from idle modes. This array can be shared with others because it will not be |
| 261 | * modified once set. |
| 262 | */ |
| 263 | private int[] mPowerSaveWhitelistExceptIdleAppIdArray = new int[0]; |
| 264 | |
| 265 | /** |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 266 | * 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] | 267 | */ |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 268 | private final SparseBooleanArray mPowerSaveWhitelistAllAppIds = new SparseBooleanArray(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 269 | |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 270 | /** |
| 271 | * Current app IDs that are in the complete power save white list. This array can |
| 272 | * be shared with others because it will not be modified once set. |
| 273 | */ |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 274 | private int[] mPowerSaveWhitelistAllAppIdArray = new int[0]; |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 275 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 276 | /** |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 277 | * App IDs that have been white-listed by the user to opt out of power save restrictions. |
| 278 | */ |
| 279 | private final SparseBooleanArray mPowerSaveWhitelistUserAppIds = new SparseBooleanArray(); |
| 280 | |
| 281 | /** |
| 282 | * Current app IDs that are in the user power save white list. This array can |
| 283 | * be shared with others because it will not be modified once set. |
| 284 | */ |
| 285 | private int[] mPowerSaveWhitelistUserAppIdArray = new int[0]; |
| 286 | |
| 287 | /** |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 288 | * List of end times for UIDs that are temporarily marked as being allowed to access |
| 289 | * the network and acquire wakelocks. Times are in milliseconds. |
| 290 | */ |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 291 | private final SparseArray<Pair<MutableLong, String>> mTempWhitelistAppIdEndTimes |
| 292 | = new SparseArray<>(); |
| 293 | |
| 294 | /** |
| 295 | * Callback to the NetworkPolicyManagerService to tell it that the temp whitelist has changed. |
| 296 | */ |
| 297 | Runnable mNetworkPolicyTempWhitelistCallback; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * Current app IDs of temporarily whitelist apps for high-priority messages. |
| 301 | */ |
| 302 | private int[] mTempWhitelistAppIdArray = new int[0]; |
| 303 | |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 304 | private static final int EVENT_NULL = 0; |
| 305 | private static final int EVENT_NORMAL = 1; |
| 306 | private static final int EVENT_LIGHT_IDLE = 2; |
| 307 | private static final int EVENT_LIGHT_MAINTENANCE = 3; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 308 | private static final int EVENT_DEEP_IDLE = 4; |
| 309 | private static final int EVENT_DEEP_MAINTENANCE = 5; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 310 | |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 311 | private final int[] mEventCmds = new int[EVENT_BUFFER_SIZE]; |
| 312 | private final long[] mEventTimes = new long[EVENT_BUFFER_SIZE]; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 313 | |
| 314 | private void addEvent(int cmd) { |
| 315 | if (mEventCmds[0] != cmd) { |
| 316 | System.arraycopy(mEventCmds, 0, mEventCmds, 1, EVENT_BUFFER_SIZE - 1); |
| 317 | System.arraycopy(mEventTimes, 0, mEventTimes, 1, EVENT_BUFFER_SIZE - 1); |
| 318 | mEventCmds[0] = cmd; |
| 319 | mEventTimes[0] = SystemClock.elapsedRealtime(); |
| 320 | } |
| 321 | } |
| 322 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 323 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 324 | @Override public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 325 | switch (intent.getAction()) { |
| 326 | case ConnectivityManager.CONNECTIVITY_ACTION: { |
Dianne Hackborn | 4cb96ca | 2016-05-17 13:55:29 -0700 | [diff] [blame] | 327 | updateConnectivityState(intent); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 328 | } break; |
| 329 | case Intent.ACTION_BATTERY_CHANGED: { |
| 330 | synchronized (DeviceIdleController.this) { |
| 331 | int plugged = intent.getIntExtra("plugged", 0); |
| 332 | updateChargingLocked(plugged != 0); |
| 333 | } |
| 334 | } break; |
| 335 | case Intent.ACTION_PACKAGE_REMOVED: { |
| 336 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 337 | Uri data = intent.getData(); |
| 338 | String ssp; |
| 339 | if (data != null && (ssp = data.getSchemeSpecificPart()) != null) { |
| 340 | removePowerSaveWhitelistAppInternal(ssp); |
| 341 | } |
| 342 | } |
| 343 | } break; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | private final AlarmManager.OnAlarmListener mLightAlarmListener |
| 349 | = new AlarmManager.OnAlarmListener() { |
| 350 | @Override |
| 351 | public void onAlarm() { |
| 352 | synchronized (DeviceIdleController.this) { |
| 353 | stepLightIdleStateLocked("s:alarm"); |
| 354 | } |
| 355 | } |
| 356 | }; |
| 357 | |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 358 | private final AlarmManager.OnAlarmListener mSensingTimeoutAlarmListener |
| 359 | = new AlarmManager.OnAlarmListener() { |
| 360 | @Override |
| 361 | public void onAlarm() { |
| 362 | if (mState == STATE_SENSING) { |
| 363 | synchronized (DeviceIdleController.this) { |
| 364 | becomeInactiveIfAppropriateLocked(); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | }; |
| 369 | |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 370 | private final AlarmManager.OnAlarmListener mDeepAlarmListener |
| 371 | = new AlarmManager.OnAlarmListener() { |
| 372 | @Override |
| 373 | public void onAlarm() { |
| 374 | synchronized (DeviceIdleController.this) { |
| 375 | stepIdleStateLocked("s:alarm"); |
| 376 | } |
| 377 | } |
| 378 | }; |
| 379 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 380 | private final BroadcastReceiver mIdleStartedDoneReceiver = new BroadcastReceiver() { |
| 381 | @Override public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 382 | // When coming out of a deep idle, we will add in some delay before we allow |
| 383 | // the system to settle down and finish the maintenance window. This is |
| 384 | // to give a chance for any pending work to be scheduled. |
| 385 | if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())) { |
| 386 | mHandler.sendEmptyMessageDelayed(MSG_FINISH_IDLE_OP, |
| 387 | mConstants.MIN_DEEP_MAINTENANCE_TIME); |
| 388 | } else { |
| 389 | mHandler.sendEmptyMessageDelayed(MSG_FINISH_IDLE_OP, |
| 390 | mConstants.MIN_LIGHT_MAINTENANCE_TIME); |
| 391 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 392 | } |
| 393 | }; |
| 394 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 395 | private final DisplayManager.DisplayListener mDisplayListener |
| 396 | = new DisplayManager.DisplayListener() { |
| 397 | @Override public void onDisplayAdded(int displayId) { |
| 398 | } |
| 399 | |
| 400 | @Override public void onDisplayRemoved(int displayId) { |
| 401 | } |
| 402 | |
| 403 | @Override public void onDisplayChanged(int displayId) { |
| 404 | if (displayId == Display.DEFAULT_DISPLAY) { |
| 405 | synchronized (DeviceIdleController.this) { |
| 406 | updateDisplayLocked(); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | }; |
| 411 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 412 | private final class MotionListener extends TriggerEventListener |
| 413 | implements SensorEventListener { |
| 414 | |
| 415 | boolean active = false; |
| 416 | |
| 417 | @Override |
| 418 | public void onTrigger(TriggerEvent event) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 419 | synchronized (DeviceIdleController.this) { |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 420 | active = false; |
| 421 | motionLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 424 | |
| 425 | @Override |
| 426 | public void onSensorChanged(SensorEvent event) { |
| 427 | synchronized (DeviceIdleController.this) { |
| 428 | mSensorManager.unregisterListener(this, mMotionSensor); |
| 429 | active = false; |
| 430 | motionLocked(); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void onAccuracyChanged(Sensor sensor, int accuracy) {} |
| 436 | |
| 437 | public boolean registerLocked() { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 438 | boolean success; |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 439 | if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) { |
| 440 | success = mSensorManager.requestTriggerSensor(mMotionListener, mMotionSensor); |
| 441 | } else { |
| 442 | success = mSensorManager.registerListener( |
| 443 | mMotionListener, mMotionSensor, SensorManager.SENSOR_DELAY_NORMAL); |
| 444 | } |
| 445 | if (success) { |
| 446 | active = true; |
| 447 | } else { |
| 448 | Slog.e(TAG, "Unable to register for " + mMotionSensor); |
| 449 | } |
| 450 | return success; |
| 451 | } |
| 452 | |
| 453 | public void unregisterLocked() { |
| 454 | if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) { |
| 455 | mSensorManager.cancelTriggerSensor(mMotionListener, mMotionSensor); |
| 456 | } else { |
| 457 | mSensorManager.unregisterListener(mMotionListener); |
| 458 | } |
| 459 | active = false; |
| 460 | } |
| 461 | } |
| 462 | private final MotionListener mMotionListener = new MotionListener(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 463 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 464 | private final LocationListener mGenericLocationListener = new LocationListener() { |
| 465 | @Override |
| 466 | public void onLocationChanged(Location location) { |
| 467 | synchronized (DeviceIdleController.this) { |
| 468 | receivedGenericLocationLocked(location); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void onProviderEnabled(String provider) { |
| 478 | } |
| 479 | |
| 480 | @Override |
| 481 | public void onProviderDisabled(String provider) { |
| 482 | } |
| 483 | }; |
| 484 | |
| 485 | private final LocationListener mGpsLocationListener = new LocationListener() { |
| 486 | @Override |
| 487 | public void onLocationChanged(Location location) { |
| 488 | synchronized (DeviceIdleController.this) { |
| 489 | receivedGpsLocationLocked(location); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void onStatusChanged(String provider, int status, Bundle extras) { |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public void onProviderEnabled(String provider) { |
| 499 | } |
| 500 | |
| 501 | @Override |
| 502 | public void onProviderDisabled(String provider) { |
| 503 | } |
| 504 | }; |
| 505 | |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 506 | /** |
| 507 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 508 | * global Settings. Any access to this class or its fields should be done while |
| 509 | * holding the DeviceIdleController lock. |
| 510 | */ |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 511 | private final class Constants extends ContentObserver { |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 512 | // Key names stored in the settings value. |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 513 | private static final String KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT |
| 514 | = "light_after_inactive_to"; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 515 | 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] | 516 | private static final String KEY_LIGHT_IDLE_TIMEOUT = "light_idle_to"; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 517 | private static final String KEY_LIGHT_IDLE_FACTOR = "light_idle_factor"; |
| 518 | 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] | 519 | private static final String KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET |
| 520 | = "light_idle_maintenance_min_budget"; |
| 521 | private static final String KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET |
| 522 | = "light_idle_maintenance_max_budget"; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 523 | private static final String KEY_MIN_LIGHT_MAINTENANCE_TIME = "min_light_maintenance_time"; |
| 524 | 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] | 525 | private static final String KEY_INACTIVE_TIMEOUT = "inactive_to"; |
| 526 | private static final String KEY_SENSING_TIMEOUT = "sensing_to"; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 527 | private static final String KEY_LOCATING_TIMEOUT = "locating_to"; |
| 528 | private static final String KEY_LOCATION_ACCURACY = "location_accuracy"; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 529 | private static final String KEY_MOTION_INACTIVE_TIMEOUT = "motion_inactive_to"; |
| 530 | private static final String KEY_IDLE_AFTER_INACTIVE_TIMEOUT = "idle_after_inactive_to"; |
| 531 | private static final String KEY_IDLE_PENDING_TIMEOUT = "idle_pending_to"; |
| 532 | private static final String KEY_MAX_IDLE_PENDING_TIMEOUT = "max_idle_pending_to"; |
| 533 | private static final String KEY_IDLE_PENDING_FACTOR = "idle_pending_factor"; |
| 534 | private static final String KEY_IDLE_TIMEOUT = "idle_to"; |
| 535 | private static final String KEY_MAX_IDLE_TIMEOUT = "max_idle_to"; |
| 536 | private static final String KEY_IDLE_FACTOR = "idle_factor"; |
| 537 | private static final String KEY_MIN_TIME_TO_ALARM = "min_time_to_alarm"; |
| 538 | private static final String KEY_MAX_TEMP_APP_WHITELIST_DURATION = |
| 539 | "max_temp_app_whitelist_duration"; |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 540 | private static final String KEY_MMS_TEMP_APP_WHITELIST_DURATION = |
| 541 | "mms_temp_app_whitelist_duration"; |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 542 | private static final String KEY_SMS_TEMP_APP_WHITELIST_DURATION = |
| 543 | "sms_temp_app_whitelist_duration"; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 544 | private static final String KEY_NOTIFICATION_WHITELIST_DURATION = |
| 545 | "notification_whitelist_duration"; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 546 | |
| 547 | /** |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 548 | * This is the time, after becoming inactive, that we go in to the first |
| 549 | * light-weight idle mode. |
| 550 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 551 | * @see #KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT |
| 552 | */ |
| 553 | public long LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT; |
| 554 | |
| 555 | /** |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 556 | * This is amount of time we will wait from the point where we decide we would |
| 557 | * like to go idle until we actually do, while waiting for jobs and other current |
| 558 | * activity to finish. |
| 559 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 560 | * @see #KEY_LIGHT_PRE_IDLE_TIMEOUT |
| 561 | */ |
| 562 | public long LIGHT_PRE_IDLE_TIMEOUT; |
| 563 | |
| 564 | /** |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 565 | * 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] | 566 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 567 | * @see #KEY_LIGHT_IDLE_TIMEOUT |
| 568 | */ |
| 569 | public long LIGHT_IDLE_TIMEOUT; |
| 570 | |
| 571 | /** |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 572 | * Scaling factor to apply to the light idle mode time each time we complete a cycle. |
| 573 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 574 | * @see #KEY_LIGHT_IDLE_FACTOR |
| 575 | */ |
| 576 | public float LIGHT_IDLE_FACTOR; |
| 577 | |
| 578 | /** |
| 579 | * This is the maximum time we will run in idle maintenence mode. |
| 580 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 581 | * @see #KEY_LIGHT_MAX_IDLE_TIMEOUT |
| 582 | */ |
| 583 | public long LIGHT_MAX_IDLE_TIMEOUT; |
| 584 | |
| 585 | /** |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 586 | * This is the minimum amount of time we want to make available for maintenance mode |
| 587 | * when lightly idling. That is, we will always have at least this amount of time |
| 588 | * available maintenance before timing out and cutting off maintenance mode. |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 589 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 590 | * @see #KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 591 | */ |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 592 | public long LIGHT_IDLE_MAINTENANCE_MIN_BUDGET; |
| 593 | |
| 594 | /** |
| 595 | * This is the maximum amount of time we want to make available for maintenance mode |
| 596 | * when lightly idling. That is, if the system isn't using up its minimum maintenance |
| 597 | * budget and this time is being added to the budget reserve, this is the maximum |
| 598 | * reserve size we will allow to grow and thus the maximum amount of time we will |
| 599 | * allow for the maintenance window. |
| 600 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 601 | * @see #KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET |
| 602 | */ |
| 603 | public long LIGHT_IDLE_MAINTENANCE_MAX_BUDGET; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 604 | |
| 605 | /** |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 606 | * This is the minimum amount of time that we will stay in maintenance mode after |
| 607 | * a light doze. We have this minimum to allow various things to respond to switching |
| 608 | * in to maintenance mode and scheduling their work -- otherwise we may |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 609 | * 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] | 610 | * mode immediately. |
| 611 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 612 | * @see #KEY_MIN_LIGHT_MAINTENANCE_TIME |
| 613 | */ |
| 614 | public long MIN_LIGHT_MAINTENANCE_TIME; |
| 615 | |
| 616 | /** |
| 617 | * This is the minimum amount of time that we will stay in maintenance mode after |
| 618 | * a full doze. We have this minimum to allow various things to respond to switching |
| 619 | * in to maintenance mode and scheduling their work -- otherwise we may |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 620 | * 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] | 621 | * mode immediately. |
| 622 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 623 | * @see #KEY_MIN_DEEP_MAINTENANCE_TIME |
| 624 | */ |
| 625 | public long MIN_DEEP_MAINTENANCE_TIME; |
| 626 | |
| 627 | /** |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 628 | * This is the time, after becoming inactive, at which we start looking at the |
| 629 | * motion sensor to determine if the device is being left alone. We don't do this |
| 630 | * 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] | 631 | * the motion sensor whenever the screen is off. |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 632 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 633 | * @see #KEY_INACTIVE_TIMEOUT |
| 634 | */ |
| 635 | public long INACTIVE_TIMEOUT; |
| 636 | |
| 637 | /** |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 638 | * If we don't receive a callback from AnyMotion in this amount of time + |
| 639 | * {@link #LOCATING_TIMEOUT}, we will change from |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 640 | * STATE_SENSING to STATE_INACTIVE, and any AnyMotion callbacks while not in STATE_SENSING |
| 641 | * will be ignored. |
| 642 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 643 | * @see #KEY_SENSING_TIMEOUT |
| 644 | */ |
| 645 | public long SENSING_TIMEOUT; |
| 646 | |
| 647 | /** |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 648 | * This is how long we will wait to try to get a good location fix before going in to |
| 649 | * idle mode. |
| 650 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 651 | * @see #KEY_LOCATING_TIMEOUT |
| 652 | */ |
| 653 | public long LOCATING_TIMEOUT; |
| 654 | |
| 655 | /** |
| 656 | * The desired maximum accuracy (in meters) we consider the location to be good enough to go |
| 657 | * on to idle. We will be trying to get an accuracy fix at least this good or until |
| 658 | * {@link #LOCATING_TIMEOUT} expires. |
| 659 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 660 | * @see #KEY_LOCATION_ACCURACY |
| 661 | */ |
| 662 | public float LOCATION_ACCURACY; |
| 663 | |
| 664 | /** |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 665 | * This is the time, after seeing motion, that we wait after becoming inactive from |
| 666 | * that until we start looking for motion again. |
| 667 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 668 | * @see #KEY_MOTION_INACTIVE_TIMEOUT |
| 669 | */ |
| 670 | public long MOTION_INACTIVE_TIMEOUT; |
| 671 | |
| 672 | /** |
| 673 | * 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] | 674 | * for motion until we truly consider the device to be idle. |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 675 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 676 | * @see #KEY_IDLE_AFTER_INACTIVE_TIMEOUT |
| 677 | */ |
| 678 | public long IDLE_AFTER_INACTIVE_TIMEOUT; |
| 679 | |
| 680 | /** |
| 681 | * 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] | 682 | * in the IDLE_MAINTENANCE state allowing the system to run normally until we return to |
| 683 | * idle. |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 684 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 685 | * @see #KEY_IDLE_PENDING_TIMEOUT |
| 686 | */ |
| 687 | public long IDLE_PENDING_TIMEOUT; |
| 688 | |
| 689 | /** |
| 690 | * Maximum pending idle timeout (time spent running) we will be allowed to use. |
| 691 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 692 | * @see #KEY_MAX_IDLE_PENDING_TIMEOUT |
| 693 | */ |
| 694 | public long MAX_IDLE_PENDING_TIMEOUT; |
| 695 | |
| 696 | /** |
| 697 | * Scaling factor to apply to current pending idle timeout each time we cycle through |
| 698 | * that state. |
| 699 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 700 | * @see #KEY_IDLE_PENDING_FACTOR |
| 701 | */ |
| 702 | public float IDLE_PENDING_FACTOR; |
| 703 | |
| 704 | /** |
| 705 | * This is the initial time that we want to sit in the idle state before waking up |
| 706 | * again to return to pending idle and allowing normal work to run. |
| 707 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 708 | * @see #KEY_IDLE_TIMEOUT |
| 709 | */ |
| 710 | public long IDLE_TIMEOUT; |
| 711 | |
| 712 | /** |
| 713 | * Maximum idle duration we will be allowed to use. |
| 714 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 715 | * @see #KEY_MAX_IDLE_TIMEOUT |
| 716 | */ |
| 717 | public long MAX_IDLE_TIMEOUT; |
| 718 | |
| 719 | /** |
| 720 | * Scaling factor to apply to current idle timeout each time we cycle through that state. |
| 721 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 722 | * @see #KEY_IDLE_FACTOR |
| 723 | */ |
| 724 | public float IDLE_FACTOR; |
| 725 | |
| 726 | /** |
| 727 | * This is the minimum time we will allow until the next upcoming alarm for us to |
| 728 | * actually go in to idle mode. |
| 729 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 730 | * @see #KEY_MIN_TIME_TO_ALARM |
| 731 | */ |
| 732 | public long MIN_TIME_TO_ALARM; |
| 733 | |
| 734 | /** |
| 735 | * Max amount of time to temporarily whitelist an app when it receives a high priority |
| 736 | * tickle. |
| 737 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 738 | * @see #KEY_MAX_TEMP_APP_WHITELIST_DURATION |
| 739 | */ |
| 740 | public long MAX_TEMP_APP_WHITELIST_DURATION; |
| 741 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 742 | /** |
| 743 | * Amount of time we would like to whitelist an app that is receiving an MMS. |
| 744 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 745 | * @see #KEY_MMS_TEMP_APP_WHITELIST_DURATION |
| 746 | */ |
| 747 | public long MMS_TEMP_APP_WHITELIST_DURATION; |
| 748 | |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 749 | /** |
| 750 | * Amount of time we would like to whitelist an app that is receiving an SMS. |
| 751 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 752 | * @see #KEY_SMS_TEMP_APP_WHITELIST_DURATION |
| 753 | */ |
| 754 | public long SMS_TEMP_APP_WHITELIST_DURATION; |
| 755 | |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 756 | /** |
| 757 | * Amount of time we would like to whitelist an app that is handling a |
| 758 | * {@link android.app.PendingIntent} triggered by a {@link android.app.Notification}. |
| 759 | * @see Settings.Global#DEVICE_IDLE_CONSTANTS |
| 760 | * @see #KEY_NOTIFICATION_WHITELIST_DURATION |
| 761 | */ |
| 762 | public long NOTIFICATION_WHITELIST_DURATION; |
| 763 | |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 764 | private final ContentResolver mResolver; |
Joe LaPenna | f33b5bf | 2016-03-23 15:19:47 -0700 | [diff] [blame] | 765 | private final boolean mHasWatch; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 766 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 767 | |
| 768 | public Constants(Handler handler, ContentResolver resolver) { |
| 769 | super(handler); |
| 770 | mResolver = resolver; |
Joe LaPenna | f33b5bf | 2016-03-23 15:19:47 -0700 | [diff] [blame] | 771 | mHasWatch = getContext().getPackageManager().hasSystemFeature( |
| 772 | PackageManager.FEATURE_WATCH); |
| 773 | mResolver.registerContentObserver(Settings.Global.getUriFor( |
| 774 | mHasWatch ? Settings.Global.DEVICE_IDLE_CONSTANTS_WATCH |
| 775 | : Settings.Global.DEVICE_IDLE_CONSTANTS), |
| 776 | false, this); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 777 | updateConstants(); |
| 778 | } |
| 779 | |
| 780 | @Override |
| 781 | public void onChange(boolean selfChange, Uri uri) { |
| 782 | updateConstants(); |
| 783 | } |
| 784 | |
| 785 | private void updateConstants() { |
| 786 | synchronized (DeviceIdleController.this) { |
| 787 | try { |
| 788 | mParser.setString(Settings.Global.getString(mResolver, |
Joe LaPenna | f33b5bf | 2016-03-23 15:19:47 -0700 | [diff] [blame] | 789 | mHasWatch ? Settings.Global.DEVICE_IDLE_CONSTANTS_WATCH |
| 790 | : Settings.Global.DEVICE_IDLE_CONSTANTS)); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 791 | } catch (IllegalArgumentException e) { |
| 792 | // Failed to parse the settings string, log this and move on |
| 793 | // with defaults. |
| 794 | Slog.e(TAG, "Bad device idle settings", e); |
| 795 | } |
| 796 | |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 797 | LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT = mParser.getLong( |
| 798 | KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT, |
| 799 | !COMPRESS_TIME ? 5 * 60 * 1000L : 15 * 1000L); |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 800 | LIGHT_PRE_IDLE_TIMEOUT = mParser.getLong(KEY_LIGHT_PRE_IDLE_TIMEOUT, |
| 801 | !COMPRESS_TIME ? 10 * 60 * 1000L : 30 * 1000L); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 802 | LIGHT_IDLE_TIMEOUT = mParser.getLong(KEY_LIGHT_IDLE_TIMEOUT, |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 803 | !COMPRESS_TIME ? 5 * 60 * 1000L : 15 * 1000L); |
| 804 | LIGHT_IDLE_FACTOR = mParser.getFloat(KEY_LIGHT_IDLE_FACTOR, |
| 805 | 2f); |
| 806 | LIGHT_MAX_IDLE_TIMEOUT = mParser.getLong(KEY_LIGHT_MAX_IDLE_TIMEOUT, |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 807 | !COMPRESS_TIME ? 15 * 60 * 1000L : 60 * 1000L); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 808 | LIGHT_IDLE_MAINTENANCE_MIN_BUDGET = mParser.getLong( |
| 809 | KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET, |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 810 | !COMPRESS_TIME ? 1 * 60 * 1000L : 15 * 1000L); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 811 | LIGHT_IDLE_MAINTENANCE_MAX_BUDGET = mParser.getLong( |
| 812 | KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET, |
| 813 | !COMPRESS_TIME ? 5 * 60 * 1000L : 30 * 1000L); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 814 | MIN_LIGHT_MAINTENANCE_TIME = mParser.getLong( |
| 815 | KEY_MIN_LIGHT_MAINTENANCE_TIME, |
| 816 | !COMPRESS_TIME ? 5 * 1000L : 1 * 1000L); |
| 817 | MIN_DEEP_MAINTENANCE_TIME = mParser.getLong( |
| 818 | KEY_MIN_DEEP_MAINTENANCE_TIME, |
| 819 | !COMPRESS_TIME ? 30 * 1000L : 5 * 1000L); |
Joe LaPenna | 0c5d3e9 | 2016-03-24 13:29:56 -0700 | [diff] [blame] | 820 | long inactiveTimeoutDefault = (mHasWatch ? 15 : 30) * 60 * 1000L; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 821 | INACTIVE_TIMEOUT = mParser.getLong(KEY_INACTIVE_TIMEOUT, |
Joe LaPenna | 0c5d3e9 | 2016-03-24 13:29:56 -0700 | [diff] [blame] | 822 | !COMPRESS_TIME ? inactiveTimeoutDefault : (inactiveTimeoutDefault / 10)); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 823 | SENSING_TIMEOUT = mParser.getLong(KEY_SENSING_TIMEOUT, |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 824 | !DEBUG ? 4 * 60 * 1000L : 60 * 1000L); |
| 825 | LOCATING_TIMEOUT = mParser.getLong(KEY_LOCATING_TIMEOUT, |
| 826 | !DEBUG ? 30 * 1000L : 15 * 1000L); |
| 827 | LOCATION_ACCURACY = mParser.getFloat(KEY_LOCATION_ACCURACY, 20); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 828 | MOTION_INACTIVE_TIMEOUT = mParser.getLong(KEY_MOTION_INACTIVE_TIMEOUT, |
| 829 | !COMPRESS_TIME ? 10 * 60 * 1000L : 60 * 1000L); |
Joe LaPenna | 0c5d3e9 | 2016-03-24 13:29:56 -0700 | [diff] [blame] | 830 | long idleAfterInactiveTimeout = (mHasWatch ? 15 : 30) * 60 * 1000L; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 831 | IDLE_AFTER_INACTIVE_TIMEOUT = mParser.getLong(KEY_IDLE_AFTER_INACTIVE_TIMEOUT, |
Joe LaPenna | 0c5d3e9 | 2016-03-24 13:29:56 -0700 | [diff] [blame] | 832 | !COMPRESS_TIME ? idleAfterInactiveTimeout |
| 833 | : (idleAfterInactiveTimeout / 10)); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 834 | IDLE_PENDING_TIMEOUT = mParser.getLong(KEY_IDLE_PENDING_TIMEOUT, |
| 835 | !COMPRESS_TIME ? 5 * 60 * 1000L : 30 * 1000L); |
| 836 | MAX_IDLE_PENDING_TIMEOUT = mParser.getLong(KEY_MAX_IDLE_PENDING_TIMEOUT, |
| 837 | !COMPRESS_TIME ? 10 * 60 * 1000L : 60 * 1000L); |
| 838 | IDLE_PENDING_FACTOR = mParser.getFloat(KEY_IDLE_PENDING_FACTOR, |
| 839 | 2f); |
| 840 | IDLE_TIMEOUT = mParser.getLong(KEY_IDLE_TIMEOUT, |
| 841 | !COMPRESS_TIME ? 60 * 60 * 1000L : 6 * 60 * 1000L); |
| 842 | MAX_IDLE_TIMEOUT = mParser.getLong(KEY_MAX_IDLE_TIMEOUT, |
| 843 | !COMPRESS_TIME ? 6 * 60 * 60 * 1000L : 30 * 60 * 1000L); |
| 844 | IDLE_FACTOR = mParser.getFloat(KEY_IDLE_FACTOR, |
| 845 | 2f); |
| 846 | MIN_TIME_TO_ALARM = mParser.getLong(KEY_MIN_TIME_TO_ALARM, |
| 847 | !COMPRESS_TIME ? 60 * 60 * 1000L : 6 * 60 * 1000L); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 848 | MAX_TEMP_APP_WHITELIST_DURATION = mParser.getLong( |
| 849 | KEY_MAX_TEMP_APP_WHITELIST_DURATION, 5 * 60 * 1000L); |
| 850 | MMS_TEMP_APP_WHITELIST_DURATION = mParser.getLong( |
Dianne Hackborn | 0b6134b | 2015-07-14 18:48:07 -0700 | [diff] [blame] | 851 | KEY_MMS_TEMP_APP_WHITELIST_DURATION, 60 * 1000L); |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 852 | SMS_TEMP_APP_WHITELIST_DURATION = mParser.getLong( |
| 853 | KEY_SMS_TEMP_APP_WHITELIST_DURATION, 20 * 1000L); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 854 | NOTIFICATION_WHITELIST_DURATION = mParser.getLong( |
| 855 | KEY_NOTIFICATION_WHITELIST_DURATION, 30 * 1000L); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | void dump(PrintWriter pw) { |
| 860 | pw.println(" Settings:"); |
| 861 | |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 862 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT); pw.print("="); |
| 863 | TimeUtils.formatDuration(LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT, pw); |
| 864 | pw.println(); |
| 865 | |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 866 | pw.print(" "); pw.print(KEY_LIGHT_PRE_IDLE_TIMEOUT); pw.print("="); |
| 867 | TimeUtils.formatDuration(LIGHT_PRE_IDLE_TIMEOUT, pw); |
| 868 | pw.println(); |
| 869 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 870 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_TIMEOUT); pw.print("="); |
| 871 | TimeUtils.formatDuration(LIGHT_IDLE_TIMEOUT, pw); |
| 872 | pw.println(); |
| 873 | |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 874 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_FACTOR); pw.print("="); |
| 875 | pw.print(LIGHT_IDLE_FACTOR); |
| 876 | pw.println(); |
| 877 | |
| 878 | pw.print(" "); pw.print(KEY_LIGHT_MAX_IDLE_TIMEOUT); pw.print("="); |
| 879 | TimeUtils.formatDuration(LIGHT_MAX_IDLE_TIMEOUT, pw); |
| 880 | pw.println(); |
| 881 | |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 882 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET); pw.print("="); |
| 883 | TimeUtils.formatDuration(LIGHT_IDLE_MAINTENANCE_MIN_BUDGET, pw); |
| 884 | pw.println(); |
| 885 | |
| 886 | pw.print(" "); pw.print(KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET); pw.print("="); |
| 887 | TimeUtils.formatDuration(LIGHT_IDLE_MAINTENANCE_MAX_BUDGET, pw); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 888 | pw.println(); |
| 889 | |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 890 | pw.print(" "); pw.print(KEY_MIN_LIGHT_MAINTENANCE_TIME); pw.print("="); |
| 891 | TimeUtils.formatDuration(MIN_LIGHT_MAINTENANCE_TIME, pw); |
| 892 | pw.println(); |
| 893 | |
| 894 | pw.print(" "); pw.print(KEY_MIN_DEEP_MAINTENANCE_TIME); pw.print("="); |
| 895 | TimeUtils.formatDuration(MIN_DEEP_MAINTENANCE_TIME, pw); |
| 896 | pw.println(); |
| 897 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 898 | pw.print(" "); pw.print(KEY_INACTIVE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 899 | TimeUtils.formatDuration(INACTIVE_TIMEOUT, pw); |
| 900 | pw.println(); |
| 901 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 902 | pw.print(" "); pw.print(KEY_SENSING_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 903 | TimeUtils.formatDuration(SENSING_TIMEOUT, pw); |
| 904 | pw.println(); |
| 905 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 906 | pw.print(" "); pw.print(KEY_LOCATING_TIMEOUT); pw.print("="); |
| 907 | TimeUtils.formatDuration(LOCATING_TIMEOUT, pw); |
| 908 | pw.println(); |
| 909 | |
| 910 | pw.print(" "); pw.print(KEY_LOCATION_ACCURACY); pw.print("="); |
| 911 | pw.print(LOCATION_ACCURACY); pw.print("m"); |
| 912 | pw.println(); |
| 913 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 914 | pw.print(" "); pw.print(KEY_MOTION_INACTIVE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 915 | TimeUtils.formatDuration(MOTION_INACTIVE_TIMEOUT, pw); |
| 916 | pw.println(); |
| 917 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 918 | pw.print(" "); pw.print(KEY_IDLE_AFTER_INACTIVE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 919 | TimeUtils.formatDuration(IDLE_AFTER_INACTIVE_TIMEOUT, pw); |
| 920 | pw.println(); |
| 921 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 922 | pw.print(" "); pw.print(KEY_IDLE_PENDING_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 923 | TimeUtils.formatDuration(IDLE_PENDING_TIMEOUT, pw); |
| 924 | pw.println(); |
| 925 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 926 | pw.print(" "); pw.print(KEY_MAX_IDLE_PENDING_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 927 | TimeUtils.formatDuration(MAX_IDLE_PENDING_TIMEOUT, pw); |
| 928 | pw.println(); |
| 929 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 930 | pw.print(" "); pw.print(KEY_IDLE_PENDING_FACTOR); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 931 | pw.println(IDLE_PENDING_FACTOR); |
| 932 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 933 | pw.print(" "); pw.print(KEY_IDLE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 934 | TimeUtils.formatDuration(IDLE_TIMEOUT, pw); |
| 935 | pw.println(); |
| 936 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 937 | pw.print(" "); pw.print(KEY_MAX_IDLE_TIMEOUT); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 938 | TimeUtils.formatDuration(MAX_IDLE_TIMEOUT, pw); |
| 939 | pw.println(); |
| 940 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 941 | pw.print(" "); pw.print(KEY_IDLE_FACTOR); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 942 | pw.println(IDLE_FACTOR); |
| 943 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 944 | pw.print(" "); pw.print(KEY_MIN_TIME_TO_ALARM); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 945 | TimeUtils.formatDuration(MIN_TIME_TO_ALARM, pw); |
| 946 | pw.println(); |
| 947 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 948 | pw.print(" "); pw.print(KEY_MAX_TEMP_APP_WHITELIST_DURATION); pw.print("="); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 949 | TimeUtils.formatDuration(MAX_TEMP_APP_WHITELIST_DURATION, pw); |
| 950 | pw.println(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 951 | |
| 952 | pw.print(" "); pw.print(KEY_MMS_TEMP_APP_WHITELIST_DURATION); pw.print("="); |
| 953 | TimeUtils.formatDuration(MMS_TEMP_APP_WHITELIST_DURATION, pw); |
| 954 | pw.println(); |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 955 | |
| 956 | pw.print(" "); pw.print(KEY_SMS_TEMP_APP_WHITELIST_DURATION); pw.print("="); |
| 957 | TimeUtils.formatDuration(SMS_TEMP_APP_WHITELIST_DURATION, pw); |
| 958 | pw.println(); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 959 | |
| 960 | pw.print(" "); pw.print(KEY_NOTIFICATION_WHITELIST_DURATION); pw.print("="); |
| 961 | TimeUtils.formatDuration(NOTIFICATION_WHITELIST_DURATION, pw); |
| 962 | pw.println(); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
| 966 | private Constants mConstants; |
| 967 | |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 968 | @Override |
| 969 | public void onAnyMotionResult(int result) { |
| 970 | if (DEBUG) Slog.d(TAG, "onAnyMotionResult(" + result + ")"); |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 971 | if (result != AnyMotionDetector.RESULT_UNKNOWN) { |
| 972 | synchronized (this) { |
| 973 | cancelSensingTimeoutAlarmLocked(); |
| 974 | } |
| 975 | } |
Kevin Gabayan | dcf4701 | 2016-07-08 10:41:24 -0700 | [diff] [blame] | 976 | if ((result == AnyMotionDetector.RESULT_MOVED) || |
| 977 | (result == AnyMotionDetector.RESULT_UNKNOWN)) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 978 | synchronized (this) { |
Kevin Gabayan | dcf4701 | 2016-07-08 10:41:24 -0700 | [diff] [blame] | 979 | handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "non_stationary"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 980 | } |
| 981 | } else if (result == AnyMotionDetector.RESULT_STATIONARY) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 982 | if (mState == STATE_SENSING) { |
| 983 | // If we are currently sensing, it is time to move to locating. |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 984 | synchronized (this) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 985 | mNotMoving = true; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 986 | stepIdleStateLocked("s:stationary"); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 987 | } |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 988 | } else if (mState == STATE_LOCATING) { |
| 989 | // If we are currently locating, note that we are not moving and step |
| 990 | // if we have located the position. |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 991 | synchronized (this) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 992 | mNotMoving = true; |
| 993 | if (mLocated) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 994 | stepIdleStateLocked("s:stationary"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 995 | } |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1001 | static final int MSG_WRITE_CONFIG = 1; |
| 1002 | static final int MSG_REPORT_IDLE_ON = 2; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1003 | static final int MSG_REPORT_IDLE_ON_LIGHT = 3; |
| 1004 | static final int MSG_REPORT_IDLE_OFF = 4; |
| 1005 | static final int MSG_REPORT_ACTIVE = 5; |
| 1006 | static final int MSG_TEMP_APP_WHITELIST_TIMEOUT = 6; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 1007 | static final int MSG_REPORT_MAINTENANCE_ACTIVITY = 7; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1008 | static final int MSG_FINISH_IDLE_OP = 8; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1009 | |
| 1010 | final class MyHandler extends Handler { |
| 1011 | MyHandler(Looper looper) { |
| 1012 | super(looper); |
| 1013 | } |
| 1014 | |
| 1015 | @Override public void handleMessage(Message msg) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1016 | if (DEBUG) Slog.d(TAG, "handleMessage(" + msg.what + ")"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1017 | switch (msg.what) { |
| 1018 | case MSG_WRITE_CONFIG: { |
| 1019 | handleWriteConfigFile(); |
| 1020 | } break; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1021 | case MSG_REPORT_IDLE_ON: |
| 1022 | case MSG_REPORT_IDLE_ON_LIGHT: { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1023 | EventLogTags.writeDeviceIdleOnStart(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1024 | final boolean deepChanged; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1025 | final boolean lightChanged; |
| 1026 | if (msg.what == MSG_REPORT_IDLE_ON) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1027 | deepChanged = mLocalPowerManager.setDeviceIdleMode(true); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1028 | lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false); |
| 1029 | } else { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1030 | deepChanged = mLocalPowerManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1031 | lightChanged = mLocalPowerManager.setLightDeviceIdleMode(true); |
| 1032 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1033 | try { |
| 1034 | mNetworkPolicyManager.setDeviceIdleMode(true); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1035 | mBatteryStats.noteDeviceIdleMode(msg.what == MSG_REPORT_IDLE_ON |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 1036 | ? BatteryStats.DEVICE_IDLE_MODE_DEEP |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1037 | : BatteryStats.DEVICE_IDLE_MODE_LIGHT, null, Process.myUid()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1038 | } catch (RemoteException e) { |
| 1039 | } |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1040 | if (deepChanged) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1041 | getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL); |
| 1042 | } |
| 1043 | if (lightChanged) { |
| 1044 | getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL); |
| 1045 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1046 | EventLogTags.writeDeviceIdleOnComplete(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1047 | } break; |
| 1048 | case MSG_REPORT_IDLE_OFF: { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1049 | EventLogTags.writeDeviceIdleOffStart("unknown"); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1050 | final boolean deepChanged = mLocalPowerManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1051 | final boolean lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1052 | try { |
| 1053 | mNetworkPolicyManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1054 | mBatteryStats.noteDeviceIdleMode(BatteryStats.DEVICE_IDLE_MODE_OFF, |
| 1055 | null, Process.myUid()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1056 | } catch (RemoteException e) { |
| 1057 | } |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1058 | if (deepChanged) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1059 | incActiveIdleOps(); |
| 1060 | getContext().sendOrderedBroadcastAsUser(mIdleIntent, UserHandle.ALL, |
| 1061 | null, mIdleStartedDoneReceiver, null, 0, null, null); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1062 | } |
| 1063 | if (lightChanged) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1064 | incActiveIdleOps(); |
| 1065 | getContext().sendOrderedBroadcastAsUser(mLightIdleIntent, UserHandle.ALL, |
| 1066 | null, mIdleStartedDoneReceiver, null, 0, null, null); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1067 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1068 | // Always start with one active op for the message being sent here. |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1069 | // Now we are done! |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1070 | decActiveIdleOps(); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1071 | EventLogTags.writeDeviceIdleOffComplete(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1072 | } break; |
| 1073 | case MSG_REPORT_ACTIVE: { |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1074 | String activeReason = (String)msg.obj; |
| 1075 | int activeUid = msg.arg1; |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1076 | EventLogTags.writeDeviceIdleOffStart( |
| 1077 | activeReason != null ? activeReason : "unknown"); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1078 | final boolean deepChanged = mLocalPowerManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1079 | final boolean lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1080 | try { |
| 1081 | mNetworkPolicyManager.setDeviceIdleMode(false); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1082 | mBatteryStats.noteDeviceIdleMode(BatteryStats.DEVICE_IDLE_MODE_OFF, |
| 1083 | activeReason, activeUid); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1084 | } catch (RemoteException e) { |
| 1085 | } |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1086 | if (deepChanged) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1087 | getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL); |
| 1088 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1089 | if (lightChanged) { |
| 1090 | getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL); |
| 1091 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1092 | EventLogTags.writeDeviceIdleOffComplete(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1093 | } break; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1094 | case MSG_TEMP_APP_WHITELIST_TIMEOUT: { |
| 1095 | int uid = msg.arg1; |
| 1096 | checkTempAppWhitelistTimeout(uid); |
| 1097 | } break; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 1098 | case MSG_REPORT_MAINTENANCE_ACTIVITY: { |
| 1099 | boolean active = (msg.arg1 == 1); |
| 1100 | final int size = mMaintenanceActivityListeners.beginBroadcast(); |
| 1101 | try { |
| 1102 | for (int i = 0; i < size; i++) { |
| 1103 | try { |
| 1104 | mMaintenanceActivityListeners.getBroadcastItem(i) |
| 1105 | .onMaintenanceActivityChanged(active); |
| 1106 | } catch (RemoteException ignored) { |
| 1107 | } |
| 1108 | } |
| 1109 | } finally { |
| 1110 | mMaintenanceActivityListeners.finishBroadcast(); |
| 1111 | } |
| 1112 | } break; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1113 | case MSG_FINISH_IDLE_OP: { |
| 1114 | decActiveIdleOps(); |
| 1115 | } break; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | final MyHandler mHandler; |
| 1121 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1122 | BinderService mBinderService; |
| 1123 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1124 | private final class BinderService extends IDeviceIdleController.Stub { |
| 1125 | @Override public void addPowerSaveWhitelistApp(String name) { |
| 1126 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 1127 | null); |
Dianne Hackborn | c9b2cd1 | 2016-01-20 10:54:29 -0800 | [diff] [blame] | 1128 | long ident = Binder.clearCallingIdentity(); |
| 1129 | try { |
| 1130 | addPowerSaveWhitelistAppInternal(name); |
| 1131 | } finally { |
| 1132 | Binder.restoreCallingIdentity(ident); |
| 1133 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | @Override public void removePowerSaveWhitelistApp(String name) { |
| 1137 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 1138 | null); |
Dianne Hackborn | c9b2cd1 | 2016-01-20 10:54:29 -0800 | [diff] [blame] | 1139 | long ident = Binder.clearCallingIdentity(); |
| 1140 | try { |
| 1141 | removePowerSaveWhitelistAppInternal(name); |
| 1142 | } finally { |
| 1143 | Binder.restoreCallingIdentity(ident); |
| 1144 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1147 | @Override public String[] getSystemPowerWhitelistExceptIdle() { |
| 1148 | return getSystemPowerWhitelistExceptIdleInternal(); |
| 1149 | } |
| 1150 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1151 | @Override public String[] getSystemPowerWhitelist() { |
| 1152 | return getSystemPowerWhitelistInternal(); |
| 1153 | } |
| 1154 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1155 | @Override public String[] getUserPowerWhitelist() { |
| 1156 | return getUserPowerWhitelistInternal(); |
| 1157 | } |
| 1158 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1159 | @Override public String[] getFullPowerWhitelistExceptIdle() { |
| 1160 | return getFullPowerWhitelistExceptIdleInternal(); |
| 1161 | } |
| 1162 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1163 | @Override public String[] getFullPowerWhitelist() { |
| 1164 | return getFullPowerWhitelistInternal(); |
| 1165 | } |
| 1166 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1167 | @Override public int[] getAppIdWhitelistExceptIdle() { |
| 1168 | return getAppIdWhitelistExceptIdleInternal(); |
| 1169 | } |
| 1170 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1171 | @Override public int[] getAppIdWhitelist() { |
| 1172 | return getAppIdWhitelistInternal(); |
| 1173 | } |
| 1174 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1175 | @Override public int[] getAppIdUserWhitelist() { |
| 1176 | return getAppIdUserWhitelistInternal(); |
| 1177 | } |
| 1178 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1179 | @Override public int[] getAppIdTempWhitelist() { |
| 1180 | return getAppIdTempWhitelistInternal(); |
| 1181 | } |
| 1182 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1183 | @Override public boolean isPowerSaveWhitelistExceptIdleApp(String name) { |
| 1184 | return isPowerSaveWhitelistExceptIdleAppInternal(name); |
| 1185 | } |
| 1186 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 1187 | @Override public boolean isPowerSaveWhitelistApp(String name) { |
| 1188 | return isPowerSaveWhitelistAppInternal(name); |
| 1189 | } |
| 1190 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1191 | @Override public void addPowerSaveTempWhitelistApp(String packageName, long duration, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1192 | int userId, String reason) throws RemoteException { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1193 | addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1196 | @Override public long addPowerSaveTempWhitelistAppForMms(String packageName, |
| 1197 | int userId, String reason) throws RemoteException { |
| 1198 | long duration = mConstants.MMS_TEMP_APP_WHITELIST_DURATION; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1199 | addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1200 | return duration; |
| 1201 | } |
| 1202 | |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 1203 | @Override public long addPowerSaveTempWhitelistAppForSms(String packageName, |
| 1204 | int userId, String reason) throws RemoteException { |
| 1205 | long duration = mConstants.SMS_TEMP_APP_WHITELIST_DURATION; |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1206 | addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason); |
Dianne Hackborn | 451c346 | 2015-07-21 17:39:46 -0700 | [diff] [blame] | 1207 | return duration; |
| 1208 | } |
| 1209 | |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1210 | @Override public void exitIdle(String reason) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1211 | getContext().enforceCallingOrSelfPermission(Manifest.permission.DEVICE_POWER, |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1212 | null); |
Dianne Hackborn | c9b2cd1 | 2016-01-20 10:54:29 -0800 | [diff] [blame] | 1213 | long ident = Binder.clearCallingIdentity(); |
| 1214 | try { |
| 1215 | exitIdleInternal(reason); |
| 1216 | } finally { |
| 1217 | Binder.restoreCallingIdentity(ident); |
| 1218 | } |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 1221 | @Override public boolean registerMaintenanceActivityListener( |
| 1222 | IMaintenanceActivityListener listener) { |
| 1223 | return DeviceIdleController.this.registerMaintenanceActivityListener(listener); |
| 1224 | } |
| 1225 | |
| 1226 | @Override public void unregisterMaintenanceActivityListener( |
| 1227 | IMaintenanceActivityListener listener) { |
| 1228 | DeviceIdleController.this.unregisterMaintenanceActivityListener(listener); |
| 1229 | } |
| 1230 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1231 | @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1232 | DeviceIdleController.this.dump(fd, pw, args); |
| 1233 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1234 | |
| 1235 | @Override public void onShellCommand(FileDescriptor in, FileDescriptor out, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 1236 | FileDescriptor err, String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
| 1237 | (new Shell()).exec(this, in, out, err, args, callback, resultReceiver); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1238 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Felipe Leme | ef13466 | 2016-08-10 14:46:39 -0700 | [diff] [blame] | 1241 | public class LocalService { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1242 | public void addPowerSaveTempWhitelistAppDirect(int appId, long duration, boolean sync, |
| 1243 | String reason) { |
| 1244 | addPowerSaveTempWhitelistAppDirectInternal(0, appId, duration, sync, reason); |
| 1245 | } |
| 1246 | |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1247 | public long getNotificationWhitelistDuration() { |
| 1248 | return mConstants.NOTIFICATION_WHITELIST_DURATION; |
| 1249 | } |
| 1250 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1251 | public void setNetworkPolicyTempWhitelistCallback(Runnable callback) { |
| 1252 | setNetworkPolicyTempWhitelistCallbackInternal(callback); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1253 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1254 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1255 | public void setJobsActive(boolean active) { |
| 1256 | DeviceIdleController.this.setJobsActive(active); |
| 1257 | } |
| 1258 | |
| 1259 | // Up-call from alarm manager. |
| 1260 | public void setAlarmsActive(boolean active) { |
| 1261 | DeviceIdleController.this.setAlarmsActive(active); |
| 1262 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1263 | |
| 1264 | /** |
| 1265 | * Returns the array of app ids whitelisted by user. Take care not to |
| 1266 | * modify this, as it is a reference to the original copy. But the reference |
| 1267 | * can change when the list changes, so it needs to be re-acquired when |
| 1268 | * {@link PowerManager#ACTION_POWER_SAVE_WHITELIST_CHANGED} is sent. |
| 1269 | */ |
| 1270 | public int[] getPowerSaveWhitelistUserAppIds() { |
| 1271 | return DeviceIdleController.this.getPowerSaveWhitelistUserAppIds(); |
| 1272 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1275 | public DeviceIdleController(Context context) { |
| 1276 | super(context); |
| 1277 | mConfigFile = new AtomicFile(new File(getSystemDir(), "deviceidle.xml")); |
| 1278 | mHandler = new MyHandler(BackgroundThread.getHandler().getLooper()); |
| 1279 | } |
| 1280 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1281 | int[] getPowerSaveWhitelistUserAppIds() { |
| 1282 | synchronized (this) { |
| 1283 | return mPowerSaveWhitelistUserAppIdArray; |
| 1284 | } |
| 1285 | } |
| 1286 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1287 | private static File getSystemDir() { |
| 1288 | return new File(Environment.getDataDirectory(), "system"); |
| 1289 | } |
| 1290 | |
| 1291 | @Override |
| 1292 | public void onStart() { |
| 1293 | final PackageManager pm = getContext().getPackageManager(); |
| 1294 | |
| 1295 | synchronized (this) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1296 | mLightEnabled = mDeepEnabled = getContext().getResources().getBoolean( |
Dianne Hackborn | 9261703 | 2015-06-19 15:32:19 -0700 | [diff] [blame] | 1297 | com.android.internal.R.bool.config_enableAutoPowerModes); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1298 | SystemConfig sysConfig = SystemConfig.getInstance(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1299 | ArraySet<String> allowPowerExceptIdle = sysConfig.getAllowInPowerSaveExceptIdle(); |
| 1300 | for (int i=0; i<allowPowerExceptIdle.size(); i++) { |
| 1301 | String pkg = allowPowerExceptIdle.valueAt(i); |
| 1302 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 1303 | ApplicationInfo ai = pm.getApplicationInfo(pkg, |
| 1304 | PackageManager.MATCH_SYSTEM_ONLY); |
| 1305 | int appid = UserHandle.getAppId(ai.uid); |
| 1306 | mPowerSaveWhitelistAppsExceptIdle.put(ai.packageName, appid); |
| 1307 | mPowerSaveWhitelistSystemAppIdsExceptIdle.put(appid, true); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1308 | } catch (PackageManager.NameNotFoundException e) { |
| 1309 | } |
| 1310 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1311 | ArraySet<String> allowPower = sysConfig.getAllowInPowerSave(); |
| 1312 | for (int i=0; i<allowPower.size(); i++) { |
| 1313 | String pkg = allowPower.valueAt(i); |
| 1314 | try { |
Jeff Sharkey | c5967e9 | 2016-01-07 18:50:29 -0700 | [diff] [blame] | 1315 | ApplicationInfo ai = pm.getApplicationInfo(pkg, |
| 1316 | PackageManager.MATCH_SYSTEM_ONLY); |
| 1317 | int appid = UserHandle.getAppId(ai.uid); |
| 1318 | // These apps are on both the whitelist-except-idle as well |
| 1319 | // as the full whitelist, so they apply in all cases. |
| 1320 | mPowerSaveWhitelistAppsExceptIdle.put(ai.packageName, appid); |
| 1321 | mPowerSaveWhitelistSystemAppIdsExceptIdle.put(appid, true); |
| 1322 | mPowerSaveWhitelistApps.put(ai.packageName, appid); |
| 1323 | mPowerSaveWhitelistSystemAppIds.put(appid, true); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1324 | } catch (PackageManager.NameNotFoundException e) { |
| 1325 | } |
| 1326 | } |
| 1327 | |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1328 | mConstants = new Constants(mHandler, getContext().getContentResolver()); |
| 1329 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1330 | readConfigFileLocked(); |
| 1331 | updateWhitelistAppIdsLocked(); |
| 1332 | |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1333 | mNetworkConnected = true; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1334 | mScreenOn = true; |
| 1335 | // Start out assuming we are charging. If we aren't, we will at least get |
| 1336 | // a battery update the next time the level drops. |
| 1337 | mCharging = true; |
| 1338 | mState = STATE_ACTIVE; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1339 | mLightState = LIGHT_STATE_ACTIVE; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1340 | mInactiveTimeout = mConstants.INACTIVE_TIMEOUT; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1343 | mBinderService = new BinderService(); |
| 1344 | publishBinderService(Context.DEVICE_IDLE_CONTROLLER, mBinderService); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1345 | publishLocalService(LocalService.class, new LocalService()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | @Override |
| 1349 | public void onBootPhase(int phase) { |
| 1350 | if (phase == PHASE_SYSTEM_SERVICES_READY) { |
| 1351 | synchronized (this) { |
| 1352 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
| 1353 | mBatteryStats = BatteryStatsService.getService(); |
| 1354 | mLocalPowerManager = getLocalService(PowerManagerInternal.class); |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1355 | mPowerManager = getContext().getSystemService(PowerManager.class); |
| 1356 | mActiveIdleWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 1357 | "deviceidle_maint"); |
| 1358 | mActiveIdleWakeLock.setReferenceCounted(false); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1359 | mConnectivityService = (ConnectivityService)ServiceManager.getService( |
| 1360 | Context.CONNECTIVITY_SERVICE); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1361 | mLocalAlarmManager = getLocalService(AlarmManagerService.LocalService.class); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1362 | mNetworkPolicyManager = INetworkPolicyManager.Stub.asInterface( |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1363 | ServiceManager.getService(Context.NETWORK_POLICY_SERVICE)); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1364 | mDisplayManager = (DisplayManager) getContext().getSystemService( |
| 1365 | Context.DISPLAY_SERVICE); |
| 1366 | mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1367 | int sigMotionSensorId = getContext().getResources().getInteger( |
| 1368 | com.android.internal.R.integer.config_autoPowerModeAnyMotionSensor); |
| 1369 | if (sigMotionSensorId > 0) { |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1370 | mMotionSensor = mSensorManager.getDefaultSensor(sigMotionSensorId, true); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1371 | } |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1372 | if (mMotionSensor == null && getContext().getResources().getBoolean( |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1373 | com.android.internal.R.bool.config_autoPowerModePreferWristTilt)) { |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1374 | mMotionSensor = mSensorManager.getDefaultSensor( |
| 1375 | Sensor.TYPE_WRIST_TILT_GESTURE, true); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1376 | } |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1377 | if (mMotionSensor == null) { |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1378 | // As a last ditch, fall back to SMD. |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1379 | mMotionSensor = mSensorManager.getDefaultSensor( |
| 1380 | Sensor.TYPE_SIGNIFICANT_MOTION, true); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1381 | } |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1382 | |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1383 | if (getContext().getResources().getBoolean( |
| 1384 | com.android.internal.R.bool.config_autoPowerModePrefetchLocation)) { |
| 1385 | mLocationManager = (LocationManager) getContext().getSystemService( |
| 1386 | Context.LOCATION_SERVICE); |
| 1387 | mLocationRequest = new LocationRequest() |
| 1388 | .setQuality(LocationRequest.ACCURACY_FINE) |
| 1389 | .setInterval(0) |
| 1390 | .setFastestInterval(0) |
| 1391 | .setNumUpdates(1); |
| 1392 | } |
| 1393 | |
| 1394 | float angleThreshold = getContext().getResources().getInteger( |
| 1395 | com.android.internal.R.integer.config_autoPowerModeThresholdAngle) / 100f; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1396 | mAnyMotionDetector = new AnyMotionDetector( |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1397 | (PowerManager) getContext().getSystemService(Context.POWER_SERVICE), |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1398 | mHandler, mSensorManager, this, angleThreshold); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1399 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1400 | mIdleIntent = new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1401 | mIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1402 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1403 | mLightIdleIntent = new Intent(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED); |
| 1404 | mLightIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
| 1405 | | Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1406 | |
| 1407 | IntentFilter filter = new IntentFilter(); |
| 1408 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1409 | getContext().registerReceiver(mReceiver, filter); |
Amith Yamasani | ac59f75e | 2016-05-05 12:38:17 -0700 | [diff] [blame] | 1410 | |
Dianne Hackborn | 1b79ad7 | 2015-10-12 10:59:47 -0700 | [diff] [blame] | 1411 | filter = new IntentFilter(); |
| 1412 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 1413 | filter.addDataScheme("package"); |
Amith Yamasani | ac59f75e | 2016-05-05 12:38:17 -0700 | [diff] [blame] | 1414 | getContext().registerReceiver(mReceiver, filter); |
| 1415 | |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1416 | filter = new IntentFilter(); |
| 1417 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
Dianne Hackborn | 1b79ad7 | 2015-10-12 10:59:47 -0700 | [diff] [blame] | 1418 | getContext().registerReceiver(mReceiver, filter); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1419 | |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1420 | mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1421 | mLocalAlarmManager.setDeviceIdleUserWhitelist(mPowerSaveWhitelistUserAppIdArray); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1422 | mDisplayManager.registerDisplayListener(mDisplayListener, null); |
| 1423 | updateDisplayLocked(); |
| 1424 | } |
Dianne Hackborn | 4cb96ca | 2016-05-17 13:55:29 -0700 | [diff] [blame] | 1425 | updateConnectivityState(null); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | public boolean addPowerSaveWhitelistAppInternal(String name) { |
| 1430 | synchronized (this) { |
| 1431 | try { |
Dianne Hackborn | 1b79ad7 | 2015-10-12 10:59:47 -0700 | [diff] [blame] | 1432 | ApplicationInfo ai = getContext().getPackageManager().getApplicationInfo(name, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 1433 | PackageManager.MATCH_ANY_USER); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1434 | if (mPowerSaveWhitelistUserApps.put(name, UserHandle.getAppId(ai.uid)) == null) { |
| 1435 | reportPowerSaveWhitelistChangedLocked(); |
| 1436 | updateWhitelistAppIdsLocked(); |
| 1437 | writeConfigFileLocked(); |
| 1438 | } |
| 1439 | return true; |
| 1440 | } catch (PackageManager.NameNotFoundException e) { |
| 1441 | return false; |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | public boolean removePowerSaveWhitelistAppInternal(String name) { |
| 1447 | synchronized (this) { |
| 1448 | if (mPowerSaveWhitelistUserApps.remove(name) != null) { |
| 1449 | reportPowerSaveWhitelistChangedLocked(); |
| 1450 | updateWhitelistAppIdsLocked(); |
| 1451 | writeConfigFileLocked(); |
| 1452 | return true; |
| 1453 | } |
| 1454 | } |
| 1455 | return false; |
| 1456 | } |
| 1457 | |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 1458 | public boolean getPowerSaveWhitelistAppInternal(String name) { |
| 1459 | synchronized (this) { |
| 1460 | return mPowerSaveWhitelistUserApps.containsKey(name); |
| 1461 | } |
| 1462 | } |
| 1463 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1464 | public String[] getSystemPowerWhitelistExceptIdleInternal() { |
| 1465 | synchronized (this) { |
| 1466 | int size = mPowerSaveWhitelistAppsExceptIdle.size(); |
| 1467 | String[] apps = new String[size]; |
| 1468 | for (int i = 0; i < size; i++) { |
| 1469 | apps[i] = mPowerSaveWhitelistAppsExceptIdle.keyAt(i); |
| 1470 | } |
| 1471 | return apps; |
| 1472 | } |
| 1473 | } |
| 1474 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1475 | public String[] getSystemPowerWhitelistInternal() { |
| 1476 | synchronized (this) { |
| 1477 | int size = mPowerSaveWhitelistApps.size(); |
| 1478 | String[] apps = new String[size]; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1479 | for (int i = 0; i < size; i++) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1480 | apps[i] = mPowerSaveWhitelistApps.keyAt(i); |
| 1481 | } |
| 1482 | return apps; |
| 1483 | } |
| 1484 | } |
| 1485 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1486 | public String[] getUserPowerWhitelistInternal() { |
| 1487 | synchronized (this) { |
| 1488 | int size = mPowerSaveWhitelistUserApps.size(); |
| 1489 | String[] apps = new String[size]; |
| 1490 | for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) { |
| 1491 | apps[i] = mPowerSaveWhitelistUserApps.keyAt(i); |
| 1492 | } |
| 1493 | return apps; |
| 1494 | } |
| 1495 | } |
| 1496 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1497 | public String[] getFullPowerWhitelistExceptIdleInternal() { |
| 1498 | synchronized (this) { |
| 1499 | int size = mPowerSaveWhitelistAppsExceptIdle.size() + mPowerSaveWhitelistUserApps.size(); |
| 1500 | String[] apps = new String[size]; |
| 1501 | int cur = 0; |
| 1502 | for (int i = 0; i < mPowerSaveWhitelistAppsExceptIdle.size(); i++) { |
| 1503 | apps[cur] = mPowerSaveWhitelistAppsExceptIdle.keyAt(i); |
| 1504 | cur++; |
| 1505 | } |
| 1506 | for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) { |
| 1507 | apps[cur] = mPowerSaveWhitelistUserApps.keyAt(i); |
| 1508 | cur++; |
| 1509 | } |
| 1510 | return apps; |
| 1511 | } |
| 1512 | } |
| 1513 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1514 | public String[] getFullPowerWhitelistInternal() { |
| 1515 | synchronized (this) { |
| 1516 | int size = mPowerSaveWhitelistApps.size() + mPowerSaveWhitelistUserApps.size(); |
| 1517 | String[] apps = new String[size]; |
| 1518 | int cur = 0; |
| 1519 | for (int i = 0; i < mPowerSaveWhitelistApps.size(); i++) { |
| 1520 | apps[cur] = mPowerSaveWhitelistApps.keyAt(i); |
| 1521 | cur++; |
| 1522 | } |
| 1523 | for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) { |
| 1524 | apps[cur] = mPowerSaveWhitelistUserApps.keyAt(i); |
| 1525 | cur++; |
| 1526 | } |
| 1527 | return apps; |
| 1528 | } |
| 1529 | } |
| 1530 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1531 | public boolean isPowerSaveWhitelistExceptIdleAppInternal(String packageName) { |
| 1532 | synchronized (this) { |
| 1533 | return mPowerSaveWhitelistAppsExceptIdle.containsKey(packageName) |
| 1534 | || mPowerSaveWhitelistUserApps.containsKey(packageName); |
| 1535 | } |
| 1536 | } |
| 1537 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 1538 | public boolean isPowerSaveWhitelistAppInternal(String packageName) { |
| 1539 | synchronized (this) { |
| 1540 | return mPowerSaveWhitelistApps.containsKey(packageName) |
| 1541 | || mPowerSaveWhitelistUserApps.containsKey(packageName); |
| 1542 | } |
| 1543 | } |
| 1544 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1545 | public int[] getAppIdWhitelistExceptIdleInternal() { |
| 1546 | synchronized (this) { |
| 1547 | return mPowerSaveWhitelistExceptIdleAppIdArray; |
| 1548 | } |
| 1549 | } |
| 1550 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1551 | public int[] getAppIdWhitelistInternal() { |
| 1552 | synchronized (this) { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1553 | return mPowerSaveWhitelistAllAppIdArray; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 1557 | public int[] getAppIdUserWhitelistInternal() { |
| 1558 | synchronized (this) { |
| 1559 | return mPowerSaveWhitelistUserAppIdArray; |
| 1560 | } |
| 1561 | } |
| 1562 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1563 | public int[] getAppIdTempWhitelistInternal() { |
| 1564 | synchronized (this) { |
| 1565 | return mTempWhitelistAppIdArray; |
| 1566 | } |
| 1567 | } |
| 1568 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1569 | void addPowerSaveTempWhitelistAppChecked(String packageName, long duration, |
| 1570 | int userId, String reason) throws RemoteException { |
| 1571 | getContext().enforceCallingPermission( |
| 1572 | Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST, |
| 1573 | "No permission to change device idle whitelist"); |
| 1574 | final int callingUid = Binder.getCallingUid(); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1575 | userId = ActivityManager.getService().handleIncomingUser( |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1576 | Binder.getCallingPid(), |
| 1577 | callingUid, |
| 1578 | userId, |
| 1579 | /*allowAll=*/ false, |
| 1580 | /*requireFull=*/ false, |
| 1581 | "addPowerSaveTempWhitelistApp", null); |
| 1582 | final long token = Binder.clearCallingIdentity(); |
| 1583 | try { |
| 1584 | addPowerSaveTempWhitelistAppInternal(callingUid, |
| 1585 | packageName, duration, userId, true, reason); |
| 1586 | } finally { |
| 1587 | Binder.restoreCallingIdentity(token); |
| 1588 | } |
| 1589 | } |
| 1590 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1591 | /** |
| 1592 | * Adds an app to the temporary whitelist and resets the endTime for granting the |
| 1593 | * app an exemption to access network and acquire wakelocks. |
| 1594 | */ |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1595 | void addPowerSaveTempWhitelistAppInternal(int callingUid, String packageName, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1596 | long duration, int userId, boolean sync, String reason) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1597 | try { |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 1598 | int uid = getContext().getPackageManager().getPackageUidAsUser(packageName, userId); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1599 | int appId = UserHandle.getAppId(uid); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1600 | addPowerSaveTempWhitelistAppDirectInternal(callingUid, appId, duration, sync, reason); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1601 | } catch (NameNotFoundException e) { |
| 1602 | } |
| 1603 | } |
| 1604 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1605 | /** |
| 1606 | * Adds an app to the temporary whitelist and resets the endTime for granting the |
| 1607 | * app an exemption to access network and acquire wakelocks. |
| 1608 | */ |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 1609 | void addPowerSaveTempWhitelistAppDirectInternal(int callingUid, int appId, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1610 | long duration, boolean sync, String reason) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1611 | final long timeNow = SystemClock.elapsedRealtime(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1612 | Runnable networkPolicyTempWhitelistCallback = null; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1613 | synchronized (this) { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 1614 | int callingAppId = UserHandle.getAppId(callingUid); |
| 1615 | if (callingAppId >= Process.FIRST_APPLICATION_UID) { |
| 1616 | if (!mPowerSaveWhitelistSystemAppIds.get(callingAppId)) { |
| 1617 | throw new SecurityException("Calling app " + UserHandle.formatUid(callingUid) |
| 1618 | + " is not on whitelist"); |
| 1619 | } |
| 1620 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1621 | duration = Math.min(duration, mConstants.MAX_TEMP_APP_WHITELIST_DURATION); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1622 | Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.get(appId); |
| 1623 | final boolean newEntry = entry == null; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1624 | // Set the new end time |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1625 | if (newEntry) { |
| 1626 | entry = new Pair<>(new MutableLong(0), reason); |
| 1627 | mTempWhitelistAppIdEndTimes.put(appId, entry); |
| 1628 | } |
| 1629 | entry.first.value = timeNow + duration; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1630 | if (DEBUG) { |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1631 | Slog.d(TAG, "Adding AppId " + appId + " to temp whitelist. New entry: " + newEntry); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1632 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1633 | if (newEntry) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1634 | // No pending timeout for the app id, post a delayed message |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1635 | try { |
| 1636 | mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_START, |
| 1637 | reason, appId); |
| 1638 | } catch (RemoteException e) { |
| 1639 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1640 | postTempActiveTimeoutMessage(appId, duration); |
| 1641 | updateTempWhitelistAppIdsLocked(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1642 | if (mNetworkPolicyTempWhitelistCallback != null) { |
| 1643 | if (!sync) { |
| 1644 | mHandler.post(mNetworkPolicyTempWhitelistCallback); |
| 1645 | } else { |
| 1646 | networkPolicyTempWhitelistCallback = mNetworkPolicyTempWhitelistCallback; |
| 1647 | } |
| 1648 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1649 | reportTempWhitelistChangedLocked(); |
| 1650 | } |
| 1651 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1652 | if (networkPolicyTempWhitelistCallback != null) { |
| 1653 | networkPolicyTempWhitelistCallback.run(); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | public void setNetworkPolicyTempWhitelistCallbackInternal(Runnable callback) { |
| 1658 | synchronized (this) { |
| 1659 | mNetworkPolicyTempWhitelistCallback = callback; |
| 1660 | } |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1663 | private void postTempActiveTimeoutMessage(int uid, long delay) { |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1664 | if (DEBUG) { |
| 1665 | Slog.d(TAG, "postTempActiveTimeoutMessage: uid=" + uid + ", delay=" + delay); |
| 1666 | } |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1667 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_TEMP_APP_WHITELIST_TIMEOUT, uid, 0), |
| 1668 | delay); |
| 1669 | } |
| 1670 | |
| 1671 | void checkTempAppWhitelistTimeout(int uid) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1672 | final long timeNow = SystemClock.elapsedRealtime(); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1673 | if (DEBUG) { |
| 1674 | Slog.d(TAG, "checkTempAppWhitelistTimeout: uid=" + uid + ", timeNow=" + timeNow); |
| 1675 | } |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1676 | synchronized (this) { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1677 | Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.get(uid); |
| 1678 | if (entry == null) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1679 | // Nothing to do |
| 1680 | return; |
| 1681 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1682 | if (timeNow >= entry.first.value) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1683 | mTempWhitelistAppIdEndTimes.delete(uid); |
| 1684 | if (DEBUG) { |
| 1685 | Slog.d(TAG, "Removing UID " + uid + " from temp whitelist"); |
| 1686 | } |
| 1687 | updateTempWhitelistAppIdsLocked(); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1688 | if (mNetworkPolicyTempWhitelistCallback != null) { |
| 1689 | mHandler.post(mNetworkPolicyTempWhitelistCallback); |
| 1690 | } |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1691 | reportTempWhitelistChangedLocked(); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1692 | try { |
| 1693 | mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_FINISH, |
| 1694 | entry.second, uid); |
| 1695 | } catch (RemoteException e) { |
| 1696 | } |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1697 | } else { |
| 1698 | // Need more time |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 1699 | if (DEBUG) { |
| 1700 | Slog.d(TAG, "Time to remove UID " + uid + ": " + entry.first.value); |
| 1701 | } |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1702 | postTempActiveTimeoutMessage(uid, entry.first.value - timeNow); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1707 | public void exitIdleInternal(String reason) { |
| 1708 | synchronized (this) { |
| 1709 | becomeActiveLocked(reason, Binder.getCallingUid()); |
| 1710 | } |
| 1711 | } |
| 1712 | |
Dianne Hackborn | 4cb96ca | 2016-05-17 13:55:29 -0700 | [diff] [blame] | 1713 | void updateConnectivityState(Intent connIntent) { |
| 1714 | ConnectivityService cm; |
| 1715 | synchronized (this) { |
| 1716 | cm = mConnectivityService; |
| 1717 | } |
| 1718 | if (cm == null) { |
| 1719 | return; |
| 1720 | } |
| 1721 | // Note: can't call out to ConnectivityService with our lock held. |
| 1722 | NetworkInfo ni = cm.getActiveNetworkInfo(); |
| 1723 | synchronized (this) { |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1724 | boolean conn; |
| 1725 | if (ni == null) { |
| 1726 | conn = false; |
| 1727 | } else { |
| 1728 | if (connIntent == null) { |
| 1729 | conn = ni.isConnected(); |
| 1730 | } else { |
| 1731 | final int networkType = |
| 1732 | connIntent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, |
| 1733 | ConnectivityManager.TYPE_NONE); |
| 1734 | if (ni.getType() != networkType) { |
| 1735 | return; |
| 1736 | } |
| 1737 | conn = !connIntent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, |
| 1738 | false); |
| 1739 | } |
| 1740 | } |
| 1741 | if (conn != mNetworkConnected) { |
| 1742 | mNetworkConnected = conn; |
| 1743 | if (conn && mLightState == LIGHT_STATE_WAITING_FOR_NETWORK) { |
| 1744 | stepLightIdleStateLocked("network"); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1750 | void updateDisplayLocked() { |
| 1751 | mCurDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY); |
| 1752 | // We consider any situation where the display is showing something to be it on, |
| 1753 | // because if there is anything shown we are going to be updating it at some |
| 1754 | // frequency so can't be allowed to go into deep sleeps. |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1755 | boolean screenOn = mCurDisplay.getState() == Display.STATE_ON; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1756 | if (DEBUG) Slog.d(TAG, "updateDisplayLocked: screenOn=" + screenOn); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1757 | if (!screenOn && mScreenOn) { |
| 1758 | mScreenOn = false; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1759 | if (!mForceIdle) { |
| 1760 | becomeInactiveIfAppropriateLocked(); |
| 1761 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1762 | } else if (screenOn) { |
| 1763 | mScreenOn = true; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1764 | if (!mForceIdle) { |
| 1765 | becomeActiveLocked("screen", Process.myUid()); |
| 1766 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | void updateChargingLocked(boolean charging) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1771 | if (DEBUG) Slog.i(TAG, "updateChargingLocked: charging=" + charging); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1772 | if (!charging && mCharging) { |
| 1773 | mCharging = false; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1774 | if (!mForceIdle) { |
| 1775 | becomeInactiveIfAppropriateLocked(); |
| 1776 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1777 | } else if (charging) { |
| 1778 | mCharging = charging; |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1779 | if (!mForceIdle) { |
| 1780 | becomeActiveLocked("charging", Process.myUid()); |
| 1781 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1782 | } |
| 1783 | } |
| 1784 | |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1785 | void scheduleReportActiveLocked(String activeReason, int activeUid) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1786 | Message msg = mHandler.obtainMessage(MSG_REPORT_ACTIVE, activeUid, 0, activeReason); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1787 | mHandler.sendMessage(msg); |
| 1788 | } |
| 1789 | |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1790 | void becomeActiveLocked(String activeReason, int activeUid) { |
| 1791 | if (DEBUG) Slog.i(TAG, "becomeActiveLocked, reason = " + activeReason); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1792 | if (mState != STATE_ACTIVE || mLightState != STATE_ACTIVE) { |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1793 | EventLogTags.writeDeviceIdle(STATE_ACTIVE, activeReason); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1794 | EventLogTags.writeDeviceIdleLight(LIGHT_STATE_ACTIVE, activeReason); |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1795 | scheduleReportActiveLocked(activeReason, activeUid); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1796 | mState = STATE_ACTIVE; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1797 | mLightState = LIGHT_STATE_ACTIVE; |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1798 | mInactiveTimeout = mConstants.INACTIVE_TIMEOUT; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1799 | mCurIdleBudget = 0; |
| 1800 | mMaintenanceStartTime = 0; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1801 | resetIdleManagementLocked(); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1802 | resetLightIdleManagementLocked(); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1803 | addEvent(EVENT_NORMAL); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | void becomeInactiveIfAppropriateLocked() { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1808 | if (DEBUG) Slog.d(TAG, "becomeInactiveIfAppropriateLocked()"); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1809 | if ((!mScreenOn && !mCharging) || mForceIdle) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1810 | // Screen has turned off; we are now going to become inactive and start |
| 1811 | // waiting to see if we will ultimately go idle. |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1812 | if (mState == STATE_ACTIVE && mDeepEnabled) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1813 | mState = STATE_INACTIVE; |
| 1814 | if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE"); |
| 1815 | resetIdleManagementLocked(); |
| 1816 | scheduleAlarmLocked(mInactiveTimeout, false); |
| 1817 | EventLogTags.writeDeviceIdle(mState, "no activity"); |
| 1818 | } |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1819 | if (mLightState == LIGHT_STATE_ACTIVE && mLightEnabled) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1820 | mLightState = LIGHT_STATE_INACTIVE; |
| 1821 | if (DEBUG) Slog.d(TAG, "Moved from LIGHT_STATE_ACTIVE to LIGHT_STATE_INACTIVE"); |
| 1822 | resetLightIdleManagementLocked(); |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1823 | scheduleLightAlarmLocked(mConstants.LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1824 | EventLogTags.writeDeviceIdleLight(mLightState, "no activity"); |
| 1825 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1826 | } |
| 1827 | } |
| 1828 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1829 | void resetIdleManagementLocked() { |
| 1830 | mNextIdlePendingDelay = 0; |
| 1831 | mNextIdleDelay = 0; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1832 | mNextLightIdleDelay = 0; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1833 | cancelAlarmLocked(); |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 1834 | cancelSensingTimeoutAlarmLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1835 | cancelLocatingLocked(); |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 1836 | stopMonitoringMotionLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1837 | mAnyMotionDetector.stop(); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1840 | void resetLightIdleManagementLocked() { |
| 1841 | cancelLightAlarmLocked(); |
| 1842 | } |
| 1843 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1844 | void exitForceIdleLocked() { |
| 1845 | if (mForceIdle) { |
| 1846 | mForceIdle = false; |
| 1847 | if (mScreenOn || mCharging) { |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1848 | becomeActiveLocked("exit-force", Process.myUid()); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | } |
| 1852 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1853 | void stepLightIdleStateLocked(String reason) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1854 | if (mLightState == LIGHT_STATE_OVERRIDE) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 1855 | // If we are already in deep device idle mode, then |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1856 | // there is nothing left to do for light mode. |
| 1857 | return; |
| 1858 | } |
| 1859 | |
| 1860 | if (DEBUG) Slog.d(TAG, "stepLightIdleStateLocked: mLightState=" + mLightState); |
| 1861 | EventLogTags.writeDeviceIdleLightStep(); |
| 1862 | |
| 1863 | switch (mLightState) { |
| 1864 | case LIGHT_STATE_INACTIVE: |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1865 | mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1866 | // Reset the upcoming idle delays. |
| 1867 | mNextLightIdleDelay = mConstants.LIGHT_IDLE_TIMEOUT; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1868 | mMaintenanceStartTime = 0; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 1869 | if (!isOpsInactiveLocked()) { |
| 1870 | // We have some active ops going on... give them a chance to finish |
| 1871 | // before going in to our first idle. |
| 1872 | mLightState = LIGHT_STATE_PRE_IDLE; |
| 1873 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
| 1874 | scheduleLightAlarmLocked(mConstants.LIGHT_PRE_IDLE_TIMEOUT); |
| 1875 | break; |
| 1876 | } |
| 1877 | // Nothing active, fall through to immediately idle. |
| 1878 | case LIGHT_STATE_PRE_IDLE: |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1879 | case LIGHT_STATE_IDLE_MAINTENANCE: |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1880 | if (mMaintenanceStartTime != 0) { |
| 1881 | long duration = SystemClock.elapsedRealtime() - mMaintenanceStartTime; |
| 1882 | if (duration < mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET) { |
| 1883 | // We didn't use up all of our minimum budget; add this to the reserve. |
| 1884 | mCurIdleBudget += (mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET-duration); |
| 1885 | } else { |
| 1886 | // We used more than our minimum budget; this comes out of the reserve. |
| 1887 | mCurIdleBudget -= (duration-mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET); |
| 1888 | } |
| 1889 | } |
| 1890 | mMaintenanceStartTime = 0; |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 1891 | scheduleLightAlarmLocked(mNextLightIdleDelay); |
| 1892 | mNextLightIdleDelay = Math.min(mConstants.LIGHT_MAX_IDLE_TIMEOUT, |
| 1893 | (long)(mNextLightIdleDelay * mConstants.LIGHT_IDLE_FACTOR)); |
| 1894 | if (mNextLightIdleDelay < mConstants.LIGHT_IDLE_TIMEOUT) { |
| 1895 | mNextLightIdleDelay = mConstants.LIGHT_IDLE_TIMEOUT; |
| 1896 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1897 | if (DEBUG) Slog.d(TAG, "Moved to LIGHT_STATE_IDLE."); |
| 1898 | mLightState = LIGHT_STATE_IDLE; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1899 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1900 | addEvent(EVENT_LIGHT_IDLE); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1901 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_ON_LIGHT); |
| 1902 | break; |
| 1903 | case LIGHT_STATE_IDLE: |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 1904 | case LIGHT_STATE_WAITING_FOR_NETWORK: |
| 1905 | if (mNetworkConnected || mLightState == LIGHT_STATE_WAITING_FOR_NETWORK) { |
| 1906 | // We have been idling long enough, now it is time to do some work. |
| 1907 | mActiveIdleOpCount = 1; |
| 1908 | mActiveIdleWakeLock.acquire(); |
| 1909 | mMaintenanceStartTime = SystemClock.elapsedRealtime(); |
| 1910 | if (mCurIdleBudget < mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET) { |
| 1911 | mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET; |
| 1912 | } else if (mCurIdleBudget > mConstants.LIGHT_IDLE_MAINTENANCE_MAX_BUDGET) { |
| 1913 | mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MAX_BUDGET; |
| 1914 | } |
| 1915 | scheduleLightAlarmLocked(mCurIdleBudget); |
| 1916 | if (DEBUG) Slog.d(TAG, |
| 1917 | "Moved from LIGHT_STATE_IDLE to LIGHT_STATE_IDLE_MAINTENANCE."); |
| 1918 | mLightState = LIGHT_STATE_IDLE_MAINTENANCE; |
| 1919 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
| 1920 | addEvent(EVENT_LIGHT_MAINTENANCE); |
| 1921 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_OFF); |
| 1922 | } else { |
| 1923 | // We'd like to do maintenance, but currently don't have network |
| 1924 | // connectivity... let's try to wait until the network comes back. |
| 1925 | // We'll only wait for another full idle period, however, and then give up. |
| 1926 | scheduleLightAlarmLocked(mNextLightIdleDelay); |
| 1927 | if (DEBUG) Slog.d(TAG, "Moved to LIGHT_WAITING_FOR_NETWORK."); |
| 1928 | mLightState = LIGHT_STATE_WAITING_FOR_NETWORK; |
| 1929 | EventLogTags.writeDeviceIdleLight(mLightState, reason); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 1930 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1931 | break; |
| 1932 | } |
| 1933 | } |
| 1934 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1935 | void stepIdleStateLocked(String reason) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1936 | if (DEBUG) Slog.d(TAG, "stepIdleStateLocked: mState=" + mState); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1937 | EventLogTags.writeDeviceIdleStep(); |
| 1938 | |
| 1939 | final long now = SystemClock.elapsedRealtime(); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1940 | if ((now+mConstants.MIN_TIME_TO_ALARM) > mAlarmManager.getNextWakeFromIdleTime()) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1941 | // Whoops, there is an upcoming alarm. We don't actually want to go idle. |
| 1942 | if (mState != STATE_ACTIVE) { |
Dianne Hackborn | b6683c4 | 2015-06-18 17:40:33 -0700 | [diff] [blame] | 1943 | becomeActiveLocked("alarm", Process.myUid()); |
Koji Fukui | 27b3330 | 2015-12-16 19:43:01 +0900 | [diff] [blame] | 1944 | becomeInactiveIfAppropriateLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1945 | } |
| 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | switch (mState) { |
| 1950 | case STATE_INACTIVE: |
| 1951 | // 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] | 1952 | // for motion and sleep some more while doing so. |
| 1953 | startMonitoringMotionLocked(); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1954 | scheduleAlarmLocked(mConstants.IDLE_AFTER_INACTIVE_TIMEOUT, false); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1955 | // Reset the upcoming idle delays. |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 1956 | mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT; |
| 1957 | mNextIdleDelay = mConstants.IDLE_TIMEOUT; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1958 | mState = STATE_IDLE_PENDING; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1959 | if (DEBUG) Slog.d(TAG, "Moved from STATE_INACTIVE to STATE_IDLE_PENDING."); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1960 | EventLogTags.writeDeviceIdle(mState, reason); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 1961 | break; |
| 1962 | case STATE_IDLE_PENDING: |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1963 | mState = STATE_SENSING; |
| 1964 | if (DEBUG) Slog.d(TAG, "Moved from STATE_IDLE_PENDING to STATE_SENSING."); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1965 | EventLogTags.writeDeviceIdle(mState, reason); |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 1966 | scheduleSensingTimeoutAlarmLocked(mConstants.SENSING_TIMEOUT); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1967 | cancelLocatingLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1968 | mNotMoving = false; |
| 1969 | mLocated = false; |
| 1970 | mLastGenericLocation = null; |
| 1971 | mLastGpsLocation = null; |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 1972 | mAnyMotionDetector.checkForAnyMotion(); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1973 | break; |
| 1974 | case STATE_SENSING: |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 1975 | cancelSensingTimeoutAlarmLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1976 | mState = STATE_LOCATING; |
| 1977 | if (DEBUG) Slog.d(TAG, "Moved from STATE_SENSING to STATE_LOCATING."); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1978 | EventLogTags.writeDeviceIdle(mState, reason); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 1979 | scheduleAlarmLocked(mConstants.LOCATING_TIMEOUT, false); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1980 | if (mLocationManager != null |
| 1981 | && mLocationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) { |
| 1982 | mLocationManager.requestLocationUpdates(mLocationRequest, |
| 1983 | mGenericLocationListener, mHandler.getLooper()); |
| 1984 | mLocating = true; |
| 1985 | } else { |
| 1986 | mHasNetworkLocation = false; |
| 1987 | } |
| 1988 | if (mLocationManager != null |
| 1989 | && mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) { |
| 1990 | mHasGps = true; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1991 | mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, |
| 1992 | mGpsLocationListener, mHandler.getLooper()); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1993 | mLocating = true; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1994 | } else { |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1995 | mHasGps = false; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 1996 | } |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 1997 | // If we have a location provider, we're all set, the listeners will move state |
| 1998 | // forward. |
| 1999 | if (mLocating) { |
| 2000 | break; |
| 2001 | } |
| 2002 | |
| 2003 | // Otherwise, we have to move from locating into idle maintenance. |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2004 | case STATE_LOCATING: |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2005 | cancelAlarmLocked(); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2006 | cancelLocatingLocked(); |
| 2007 | mAnyMotionDetector.stop(); |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2008 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2009 | case STATE_IDLE_MAINTENANCE: |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2010 | scheduleAlarmLocked(mNextIdleDelay, true); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2011 | if (DEBUG) Slog.d(TAG, "Moved to STATE_IDLE. Next alarm in " + mNextIdleDelay + |
| 2012 | " ms."); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 2013 | mNextIdleDelay = (long)(mNextIdleDelay * mConstants.IDLE_FACTOR); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2014 | if (DEBUG) Slog.d(TAG, "Setting mNextIdleDelay = " + mNextIdleDelay); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 2015 | mNextIdleDelay = Math.min(mNextIdleDelay, mConstants.MAX_IDLE_TIMEOUT); |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2016 | if (mNextIdleDelay < mConstants.IDLE_TIMEOUT) { |
| 2017 | mNextIdleDelay = mConstants.IDLE_TIMEOUT; |
| 2018 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2019 | mState = STATE_IDLE; |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2020 | if (mLightState != LIGHT_STATE_OVERRIDE) { |
| 2021 | mLightState = LIGHT_STATE_OVERRIDE; |
| 2022 | cancelLightAlarmLocked(); |
| 2023 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2024 | EventLogTags.writeDeviceIdle(mState, reason); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2025 | addEvent(EVENT_DEEP_IDLE); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2026 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_ON); |
| 2027 | break; |
| 2028 | case STATE_IDLE: |
| 2029 | // 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] | 2030 | mActiveIdleOpCount = 1; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2031 | mActiveIdleWakeLock.acquire(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2032 | scheduleAlarmLocked(mNextIdlePendingDelay, false); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2033 | if (DEBUG) Slog.d(TAG, "Moved from STATE_IDLE to STATE_IDLE_MAINTENANCE. " + |
| 2034 | "Next alarm in " + mNextIdlePendingDelay + " ms."); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2035 | mMaintenanceStartTime = SystemClock.elapsedRealtime(); |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 2036 | mNextIdlePendingDelay = Math.min(mConstants.MAX_IDLE_PENDING_TIMEOUT, |
| 2037 | (long)(mNextIdlePendingDelay * mConstants.IDLE_PENDING_FACTOR)); |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2038 | if (mNextIdlePendingDelay < mConstants.IDLE_PENDING_TIMEOUT) { |
| 2039 | mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT; |
| 2040 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2041 | mState = STATE_IDLE_MAINTENANCE; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2042 | EventLogTags.writeDeviceIdle(mState, reason); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2043 | addEvent(EVENT_DEEP_MAINTENANCE); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2044 | mHandler.sendEmptyMessage(MSG_REPORT_IDLE_OFF); |
| 2045 | break; |
| 2046 | } |
| 2047 | } |
| 2048 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2049 | void incActiveIdleOps() { |
| 2050 | synchronized (this) { |
| 2051 | mActiveIdleOpCount++; |
| 2052 | } |
| 2053 | } |
| 2054 | |
| 2055 | void decActiveIdleOps() { |
| 2056 | synchronized (this) { |
| 2057 | mActiveIdleOpCount--; |
| 2058 | if (mActiveIdleOpCount <= 0) { |
| 2059 | exitMaintenanceEarlyIfNeededLocked(); |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2060 | mActiveIdleWakeLock.release(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2065 | void setJobsActive(boolean active) { |
| 2066 | synchronized (this) { |
| 2067 | mJobsActive = active; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 2068 | reportMaintenanceActivityIfNeededLocked(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2069 | if (!active) { |
| 2070 | exitMaintenanceEarlyIfNeededLocked(); |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | void setAlarmsActive(boolean active) { |
| 2076 | synchronized (this) { |
| 2077 | mAlarmsActive = active; |
| 2078 | if (!active) { |
| 2079 | exitMaintenanceEarlyIfNeededLocked(); |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 2084 | boolean registerMaintenanceActivityListener(IMaintenanceActivityListener listener) { |
| 2085 | synchronized (this) { |
| 2086 | mMaintenanceActivityListeners.register(listener); |
| 2087 | return mReportedMaintenanceActivity; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | void unregisterMaintenanceActivityListener(IMaintenanceActivityListener listener) { |
| 2092 | synchronized (this) { |
| 2093 | mMaintenanceActivityListeners.unregister(listener); |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | void reportMaintenanceActivityIfNeededLocked() { |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 2098 | boolean active = mJobsActive; |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 2099 | if (active == mReportedMaintenanceActivity) { |
| 2100 | return; |
| 2101 | } |
| 2102 | mReportedMaintenanceActivity = active; |
| 2103 | Message msg = mHandler.obtainMessage(MSG_REPORT_MAINTENANCE_ACTIVITY, |
| 2104 | mReportedMaintenanceActivity ? 1 : 0, 0); |
| 2105 | mHandler.sendMessage(msg); |
| 2106 | } |
| 2107 | |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2108 | boolean isOpsInactiveLocked() { |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 2109 | return mActiveIdleOpCount <= 0 && !mJobsActive && !mAlarmsActive; |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2112 | void exitMaintenanceEarlyIfNeededLocked() { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2113 | if (mState == STATE_IDLE_MAINTENANCE || mLightState == LIGHT_STATE_IDLE_MAINTENANCE |
| 2114 | || mLightState == LIGHT_STATE_PRE_IDLE) { |
| 2115 | if (isOpsInactiveLocked()) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2116 | final long now = SystemClock.elapsedRealtime(); |
| 2117 | if (DEBUG) { |
| 2118 | StringBuilder sb = new StringBuilder(); |
| 2119 | sb.append("Exit: start="); |
| 2120 | TimeUtils.formatDuration(mMaintenanceStartTime, sb); |
| 2121 | sb.append(" now="); |
| 2122 | TimeUtils.formatDuration(now, sb); |
| 2123 | Slog.d(TAG, sb.toString()); |
| 2124 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2125 | if (mState == STATE_IDLE_MAINTENANCE) { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2126 | stepIdleStateLocked("s:early"); |
| 2127 | } else if (mLightState == LIGHT_STATE_PRE_IDLE) { |
| 2128 | stepLightIdleStateLocked("s:predone"); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2129 | } else { |
Dianne Hackborn | 945c9c9 | 2016-03-30 14:55:00 -0700 | [diff] [blame] | 2130 | stepLightIdleStateLocked("s:early"); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2136 | void motionLocked() { |
| 2137 | if (DEBUG) Slog.d(TAG, "motionLocked()"); |
| 2138 | // The motion sensor will have been disabled at this point |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2139 | handleMotionDetectedLocked(mConstants.MOTION_INACTIVE_TIMEOUT, "motion"); |
| 2140 | } |
| 2141 | |
| 2142 | void handleMotionDetectedLocked(long timeout, String type) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2143 | // 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] | 2144 | // state to wait again for no motion. Note that we only monitor for motion |
| 2145 | // after moving out of the inactive state, so no need to worry about that. |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2146 | boolean becomeInactive = false; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2147 | if (mState != STATE_ACTIVE) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2148 | scheduleReportActiveLocked(type, Process.myUid()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2149 | mState = STATE_ACTIVE; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2150 | mInactiveTimeout = timeout; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2151 | mCurIdleBudget = 0; |
| 2152 | mMaintenanceStartTime = 0; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2153 | EventLogTags.writeDeviceIdle(mState, type); |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2154 | addEvent(EVENT_NORMAL); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2155 | becomeInactive = true; |
| 2156 | } |
| 2157 | if (mLightState == LIGHT_STATE_OVERRIDE) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2158 | // We went out of light idle mode because we had started deep idle mode... let's |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2159 | // now go back and reset things so we resume light idling if appropriate. |
| 2160 | mLightState = STATE_ACTIVE; |
| 2161 | EventLogTags.writeDeviceIdleLight(mLightState, type); |
| 2162 | becomeInactive = true; |
| 2163 | } |
| 2164 | if (becomeInactive) { |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2165 | becomeInactiveIfAppropriateLocked(); |
| 2166 | } |
| 2167 | } |
| 2168 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2169 | void receivedGenericLocationLocked(Location location) { |
| 2170 | if (mState != STATE_LOCATING) { |
| 2171 | cancelLocatingLocked(); |
| 2172 | return; |
| 2173 | } |
| 2174 | if (DEBUG) Slog.d(TAG, "Generic location: " + location); |
| 2175 | mLastGenericLocation = new Location(location); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2176 | if (location.getAccuracy() > mConstants.LOCATION_ACCURACY && mHasGps) { |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2177 | return; |
| 2178 | } |
| 2179 | mLocated = true; |
| 2180 | if (mNotMoving) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2181 | stepIdleStateLocked("s:location"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | void receivedGpsLocationLocked(Location location) { |
| 2186 | if (mState != STATE_LOCATING) { |
| 2187 | cancelLocatingLocked(); |
| 2188 | return; |
| 2189 | } |
| 2190 | if (DEBUG) Slog.d(TAG, "GPS location: " + location); |
| 2191 | mLastGpsLocation = new Location(location); |
| 2192 | if (location.getAccuracy() > mConstants.LOCATION_ACCURACY) { |
| 2193 | return; |
| 2194 | } |
| 2195 | mLocated = true; |
| 2196 | if (mNotMoving) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2197 | stepIdleStateLocked("s:gps"); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2201 | void startMonitoringMotionLocked() { |
| 2202 | if (DEBUG) Slog.d(TAG, "startMonitoringMotionLocked()"); |
| 2203 | if (mMotionSensor != null && !mMotionListener.active) { |
| 2204 | mMotionListener.registerLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2205 | } |
| 2206 | } |
| 2207 | |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2208 | void stopMonitoringMotionLocked() { |
| 2209 | if (DEBUG) Slog.d(TAG, "stopMonitoringMotionLocked()"); |
| 2210 | if (mMotionSensor != null && mMotionListener.active) { |
| 2211 | mMotionListener.unregisterLocked(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | |
| 2215 | void cancelAlarmLocked() { |
| 2216 | if (mNextAlarmTime != 0) { |
| 2217 | mNextAlarmTime = 0; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2218 | mAlarmManager.cancel(mDeepAlarmListener); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2222 | void cancelLightAlarmLocked() { |
| 2223 | if (mNextLightAlarmTime != 0) { |
| 2224 | mNextLightAlarmTime = 0; |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2225 | mAlarmManager.cancel(mLightAlarmListener); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | void cancelLocatingLocked() { |
| 2230 | if (mLocating) { |
| 2231 | mLocationManager.removeUpdates(mGenericLocationListener); |
| 2232 | mLocationManager.removeUpdates(mGpsLocationListener); |
| 2233 | mLocating = false; |
| 2234 | } |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 2237 | void cancelSensingTimeoutAlarmLocked() { |
| 2238 | if (mNextSensingTimeoutAlarmTime != 0) { |
| 2239 | mNextSensingTimeoutAlarmTime = 0; |
| 2240 | mAlarmManager.cancel(mSensingTimeoutAlarmListener); |
| 2241 | } |
| 2242 | } |
| 2243 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2244 | void scheduleAlarmLocked(long delay, boolean idleUntil) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2245 | if (DEBUG) Slog.d(TAG, "scheduleAlarmLocked(" + delay + ", " + idleUntil + ")"); |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2246 | if (mMotionSensor == null) { |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2247 | // If there is no motion sensor on this device, then we won't schedule |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2248 | // 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] | 2249 | // 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] | 2250 | // manually poke it by pretending like the alarm is going off. |
| 2251 | return; |
| 2252 | } |
| 2253 | mNextAlarmTime = SystemClock.elapsedRealtime() + delay; |
| 2254 | if (idleUntil) { |
| 2255 | mAlarmManager.setIdleUntil(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2256 | mNextAlarmTime, "DeviceIdleController.deep", mDeepAlarmListener, mHandler); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2257 | } else { |
| 2258 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2259 | mNextAlarmTime, "DeviceIdleController.deep", mDeepAlarmListener, mHandler); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2263 | void scheduleLightAlarmLocked(long delay) { |
| 2264 | if (DEBUG) Slog.d(TAG, "scheduleLightAlarmLocked(" + delay + ")"); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2265 | mNextLightAlarmTime = SystemClock.elapsedRealtime() + delay; |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2266 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2267 | mNextLightAlarmTime, "DeviceIdleController.light", mLightAlarmListener, mHandler); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Kevin Gabayan | 92f15e6 | 2016-04-04 17:52:22 -0700 | [diff] [blame] | 2270 | void scheduleSensingTimeoutAlarmLocked(long delay) { |
| 2271 | if (DEBUG) Slog.d(TAG, "scheduleSensingAlarmLocked(" + delay + ")"); |
| 2272 | mNextSensingTimeoutAlarmTime = SystemClock.elapsedRealtime() + delay; |
| 2273 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, mNextSensingTimeoutAlarmTime, |
| 2274 | "DeviceIdleController.sensing", mSensingTimeoutAlarmListener, mHandler); |
| 2275 | } |
| 2276 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2277 | private static int[] buildAppIdArray(ArrayMap<String, Integer> systemApps, |
| 2278 | ArrayMap<String, Integer> userApps, SparseBooleanArray outAppIds) { |
| 2279 | outAppIds.clear(); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2280 | if (systemApps != null) { |
| 2281 | for (int i = 0; i < systemApps.size(); i++) { |
| 2282 | outAppIds.put(systemApps.valueAt(i), true); |
| 2283 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2284 | } |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2285 | if (userApps != null) { |
| 2286 | for (int i = 0; i < userApps.size(); i++) { |
| 2287 | outAppIds.put(userApps.valueAt(i), true); |
| 2288 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2289 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2290 | int size = outAppIds.size(); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 2291 | int[] appids = new int[size]; |
| 2292 | for (int i = 0; i < size; i++) { |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2293 | appids[i] = outAppIds.keyAt(i); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 2294 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2295 | return appids; |
| 2296 | } |
| 2297 | |
| 2298 | private void updateWhitelistAppIdsLocked() { |
| 2299 | mPowerSaveWhitelistExceptIdleAppIdArray = buildAppIdArray(mPowerSaveWhitelistAppsExceptIdle, |
| 2300 | mPowerSaveWhitelistUserApps, mPowerSaveWhitelistExceptIdleAppIds); |
| 2301 | mPowerSaveWhitelistAllAppIdArray = buildAppIdArray(mPowerSaveWhitelistApps, |
| 2302 | mPowerSaveWhitelistUserApps, mPowerSaveWhitelistAllAppIds); |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2303 | mPowerSaveWhitelistUserAppIdArray = buildAppIdArray(null, |
| 2304 | mPowerSaveWhitelistUserApps, mPowerSaveWhitelistUserAppIds); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 2305 | if (mLocalPowerManager != null) { |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2306 | if (DEBUG) { |
| 2307 | Slog.d(TAG, "Setting wakelock whitelist to " |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2308 | + Arrays.toString(mPowerSaveWhitelistAllAppIdArray)); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2309 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2310 | mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 2311 | } |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2312 | if (mLocalAlarmManager != null) { |
| 2313 | if (DEBUG) { |
| 2314 | Slog.d(TAG, "Setting alarm whitelist to " |
| 2315 | + Arrays.toString(mPowerSaveWhitelistUserAppIdArray)); |
| 2316 | } |
| 2317 | mLocalAlarmManager.setDeviceIdleUserWhitelist(mPowerSaveWhitelistUserAppIdArray); |
| 2318 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2319 | } |
| 2320 | |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2321 | private void updateTempWhitelistAppIdsLocked() { |
| 2322 | final int size = mTempWhitelistAppIdEndTimes.size(); |
| 2323 | if (mTempWhitelistAppIdArray.length != size) { |
| 2324 | mTempWhitelistAppIdArray = new int[size]; |
| 2325 | } |
| 2326 | for (int i = 0; i < size; i++) { |
| 2327 | mTempWhitelistAppIdArray[i] = mTempWhitelistAppIdEndTimes.keyAt(i); |
| 2328 | } |
| 2329 | if (mLocalPowerManager != null) { |
| 2330 | if (DEBUG) { |
| 2331 | Slog.d(TAG, "Setting wakelock temp whitelist to " |
| 2332 | + Arrays.toString(mTempWhitelistAppIdArray)); |
| 2333 | } |
| 2334 | mLocalPowerManager.setDeviceIdleTempWhitelist(mTempWhitelistAppIdArray); |
| 2335 | } |
| 2336 | } |
| 2337 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2338 | private void reportPowerSaveWhitelistChangedLocked() { |
| 2339 | Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
| 2340 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 2341 | getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | private void reportTempWhitelistChangedLocked() { |
| 2345 | Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED); |
| 2346 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 2347 | getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | void readConfigFileLocked() { |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 2351 | if (DEBUG) Slog.d(TAG, "Reading config from " + mConfigFile.getBaseFile()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2352 | mPowerSaveWhitelistUserApps.clear(); |
| 2353 | FileInputStream stream; |
| 2354 | try { |
| 2355 | stream = mConfigFile.openRead(); |
| 2356 | } catch (FileNotFoundException e) { |
| 2357 | return; |
| 2358 | } |
| 2359 | try { |
| 2360 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2361 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2362 | readConfigFileLocked(parser); |
| 2363 | } catch (XmlPullParserException e) { |
| 2364 | } finally { |
| 2365 | try { |
| 2366 | stream.close(); |
| 2367 | } catch (IOException e) { |
| 2368 | } |
| 2369 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | private void readConfigFileLocked(XmlPullParser parser) { |
| 2373 | final PackageManager pm = getContext().getPackageManager(); |
| 2374 | |
| 2375 | try { |
| 2376 | int type; |
| 2377 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 2378 | && type != XmlPullParser.END_DOCUMENT) { |
| 2379 | ; |
| 2380 | } |
| 2381 | |
| 2382 | if (type != XmlPullParser.START_TAG) { |
| 2383 | throw new IllegalStateException("no start tag found"); |
| 2384 | } |
| 2385 | |
| 2386 | int outerDepth = parser.getDepth(); |
| 2387 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 2388 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 2389 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 2390 | continue; |
| 2391 | } |
| 2392 | |
| 2393 | String tagName = parser.getName(); |
| 2394 | if (tagName.equals("wl")) { |
| 2395 | String name = parser.getAttributeValue(null, "n"); |
| 2396 | if (name != null) { |
| 2397 | try { |
Dianne Hackborn | 1b79ad7 | 2015-10-12 10:59:47 -0700 | [diff] [blame] | 2398 | ApplicationInfo ai = pm.getApplicationInfo(name, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 2399 | PackageManager.MATCH_ANY_USER); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2400 | mPowerSaveWhitelistUserApps.put(ai.packageName, |
| 2401 | UserHandle.getAppId(ai.uid)); |
| 2402 | } catch (PackageManager.NameNotFoundException e) { |
| 2403 | } |
| 2404 | } |
| 2405 | } else { |
| 2406 | Slog.w(TAG, "Unknown element under <config>: " |
| 2407 | + parser.getName()); |
| 2408 | XmlUtils.skipCurrentTag(parser); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | } catch (IllegalStateException e) { |
| 2413 | Slog.w(TAG, "Failed parsing config " + e); |
| 2414 | } catch (NullPointerException e) { |
| 2415 | Slog.w(TAG, "Failed parsing config " + e); |
| 2416 | } catch (NumberFormatException e) { |
| 2417 | Slog.w(TAG, "Failed parsing config " + e); |
| 2418 | } catch (XmlPullParserException e) { |
| 2419 | Slog.w(TAG, "Failed parsing config " + e); |
| 2420 | } catch (IOException e) { |
| 2421 | Slog.w(TAG, "Failed parsing config " + e); |
| 2422 | } catch (IndexOutOfBoundsException e) { |
| 2423 | Slog.w(TAG, "Failed parsing config " + e); |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | void writeConfigFileLocked() { |
| 2428 | mHandler.removeMessages(MSG_WRITE_CONFIG); |
| 2429 | mHandler.sendEmptyMessageDelayed(MSG_WRITE_CONFIG, 5000); |
| 2430 | } |
| 2431 | |
| 2432 | void handleWriteConfigFile() { |
| 2433 | final ByteArrayOutputStream memStream = new ByteArrayOutputStream(); |
| 2434 | |
| 2435 | try { |
| 2436 | synchronized (this) { |
| 2437 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2438 | out.setOutput(memStream, StandardCharsets.UTF_8.name()); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2439 | writeConfigFileLocked(out); |
| 2440 | } |
| 2441 | } catch (IOException e) { |
| 2442 | } |
| 2443 | |
| 2444 | synchronized (mConfigFile) { |
| 2445 | FileOutputStream stream = null; |
| 2446 | try { |
| 2447 | stream = mConfigFile.startWrite(); |
| 2448 | memStream.writeTo(stream); |
| 2449 | stream.flush(); |
| 2450 | FileUtils.sync(stream); |
| 2451 | stream.close(); |
| 2452 | mConfigFile.finishWrite(stream); |
| 2453 | } catch (IOException e) { |
| 2454 | Slog.w(TAG, "Error writing config file", e); |
| 2455 | mConfigFile.failWrite(stream); |
| 2456 | } |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | void writeConfigFileLocked(XmlSerializer out) throws IOException { |
| 2461 | out.startDocument(null, true); |
| 2462 | out.startTag(null, "config"); |
| 2463 | for (int i=0; i<mPowerSaveWhitelistUserApps.size(); i++) { |
| 2464 | String name = mPowerSaveWhitelistUserApps.keyAt(i); |
| 2465 | out.startTag(null, "wl"); |
| 2466 | out.attribute(null, "n", name); |
| 2467 | out.endTag(null, "wl"); |
| 2468 | } |
| 2469 | out.endTag(null, "config"); |
| 2470 | out.endDocument(); |
| 2471 | } |
| 2472 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2473 | static void dumpHelp(PrintWriter pw) { |
| 2474 | pw.println("Device idle controller (deviceidle) commands:"); |
| 2475 | pw.println(" help"); |
| 2476 | pw.println(" Print this help text."); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2477 | pw.println(" step [light|deep]"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2478 | pw.println(" Immediately step to next state, without waiting for alarm."); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2479 | pw.println(" force-idle [light|deep]"); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2480 | pw.println(" Force directly into idle mode, regardless of other device state."); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2481 | pw.println(" force-inactive"); |
| 2482 | pw.println(" Force to be inactive, ready to freely step idle states."); |
| 2483 | pw.println(" unforce"); |
| 2484 | pw.println(" Resume normal functioning after force-idle or force-inactive."); |
| 2485 | pw.println(" get [light|deep|force|screen|charging|network]"); |
| 2486 | pw.println(" Retrieve the current given state."); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2487 | pw.println(" disable [light|deep|all]"); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 2488 | pw.println(" Completely disable device idle mode."); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2489 | pw.println(" enable [light|deep|all]"); |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 2490 | 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] | 2491 | pw.println(" enabled [light|deep|all]"); |
Dianne Hackborn | 9261703 | 2015-06-19 15:32:19 -0700 | [diff] [blame] | 2492 | 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] | 2493 | pw.println(" whitelist"); |
| 2494 | pw.println(" Print currently whitelisted apps."); |
Dianne Hackborn | 9261703 | 2015-06-19 15:32:19 -0700 | [diff] [blame] | 2495 | pw.println(" whitelist [package ...]"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2496 | pw.println(" Add (prefix with +) or remove (prefix with -) packages."); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2497 | pw.println(" tempwhitelist"); |
| 2498 | pw.println(" Print packages that are temporarily whitelisted."); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2499 | pw.println(" tempwhitelist [-u] [package ..]"); |
Dianne Hackborn | 9261703 | 2015-06-19 15:32:19 -0700 | [diff] [blame] | 2500 | pw.println(" Temporarily place packages in whitelist for 10 seconds."); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2503 | class Shell extends ShellCommand { |
| 2504 | int userId = UserHandle.USER_SYSTEM; |
| 2505 | |
| 2506 | @Override |
| 2507 | public int onCommand(String cmd) { |
| 2508 | return onShellCommand(this, cmd); |
| 2509 | } |
| 2510 | |
| 2511 | @Override |
| 2512 | public void onHelp() { |
| 2513 | PrintWriter pw = getOutPrintWriter(); |
| 2514 | dumpHelp(pw); |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | int onShellCommand(Shell shell, String cmd) { |
| 2519 | PrintWriter pw = shell.getOutPrintWriter(); |
| 2520 | if ("step".equals(cmd)) { |
| 2521 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 2522 | null); |
| 2523 | synchronized (this) { |
| 2524 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2525 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2526 | try { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2527 | if (arg == null || "deep".equals(arg)) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2528 | stepIdleStateLocked("s:shell"); |
| 2529 | pw.print("Stepped to deep: "); |
| 2530 | pw.println(stateToString(mState)); |
| 2531 | } else if ("light".equals(arg)) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 2532 | stepLightIdleStateLocked("s:shell"); |
| 2533 | pw.print("Stepped to light: "); pw.println(lightStateToString(mLightState)); |
| 2534 | } else { |
| 2535 | pw.println("Unknown idle mode: " + arg); |
| 2536 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2537 | } finally { |
| 2538 | Binder.restoreCallingIdentity(token); |
| 2539 | } |
| 2540 | } |
| 2541 | } else if ("force-idle".equals(cmd)) { |
| 2542 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 2543 | null); |
| 2544 | synchronized (this) { |
| 2545 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2546 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2547 | try { |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2548 | if (arg == null || "deep".equals(arg)) { |
| 2549 | if (!mDeepEnabled) { |
| 2550 | pw.println("Unable to go deep idle; not enabled"); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2551 | return -1; |
| 2552 | } |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2553 | mForceIdle = true; |
| 2554 | becomeInactiveIfAppropriateLocked(); |
| 2555 | int curState = mState; |
| 2556 | while (curState != STATE_IDLE) { |
| 2557 | stepIdleStateLocked("s:shell"); |
| 2558 | if (curState == mState) { |
| 2559 | pw.print("Unable to go deep idle; stopped at "); |
| 2560 | pw.println(stateToString(mState)); |
| 2561 | exitForceIdleLocked(); |
| 2562 | return -1; |
| 2563 | } |
| 2564 | curState = mState; |
| 2565 | } |
| 2566 | pw.println("Now forced in to deep idle mode"); |
| 2567 | } else if ("light".equals(arg)) { |
| 2568 | mForceIdle = true; |
| 2569 | becomeInactiveIfAppropriateLocked(); |
| 2570 | int curLightState = mLightState; |
| 2571 | while (curLightState != LIGHT_STATE_IDLE) { |
| 2572 | stepIdleStateLocked("s:shell"); |
| 2573 | if (curLightState == mLightState) { |
| 2574 | pw.print("Unable to go light idle; stopped at "); |
| 2575 | pw.println(lightStateToString(mLightState)); |
| 2576 | exitForceIdleLocked(); |
| 2577 | return -1; |
| 2578 | } |
| 2579 | curLightState = mLightState; |
| 2580 | } |
| 2581 | pw.println("Now forced in to light idle mode"); |
| 2582 | } else { |
| 2583 | pw.println("Unknown idle mode: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2584 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2585 | } finally { |
| 2586 | Binder.restoreCallingIdentity(token); |
| 2587 | } |
| 2588 | } |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2589 | } else if ("force-inactive".equals(cmd)) { |
| 2590 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 2591 | null); |
| 2592 | synchronized (this) { |
| 2593 | long token = Binder.clearCallingIdentity(); |
| 2594 | try { |
| 2595 | mForceIdle = true; |
| 2596 | becomeInactiveIfAppropriateLocked(); |
| 2597 | pw.print("Light state: "); |
| 2598 | pw.print(lightStateToString(mLightState)); |
| 2599 | pw.print(", deep state: "); |
| 2600 | pw.println(stateToString(mState)); |
| 2601 | } finally { |
| 2602 | Binder.restoreCallingIdentity(token); |
| 2603 | } |
| 2604 | } |
| 2605 | } else if ("unforce".equals(cmd)) { |
| 2606 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 2607 | null); |
| 2608 | synchronized (this) { |
| 2609 | long token = Binder.clearCallingIdentity(); |
| 2610 | try { |
| 2611 | exitForceIdleLocked(); |
| 2612 | pw.print("Light state: "); |
| 2613 | pw.print(lightStateToString(mLightState)); |
| 2614 | pw.print(", deep state: "); |
| 2615 | pw.println(stateToString(mState)); |
| 2616 | } finally { |
| 2617 | Binder.restoreCallingIdentity(token); |
| 2618 | } |
| 2619 | } |
| 2620 | } else if ("get".equals(cmd)) { |
| 2621 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 2622 | null); |
| 2623 | synchronized (this) { |
| 2624 | String arg = shell.getNextArg(); |
| 2625 | if (arg != null) { |
| 2626 | long token = Binder.clearCallingIdentity(); |
| 2627 | try { |
| 2628 | switch (arg) { |
| 2629 | case "light": pw.println(lightStateToString(mLightState)); break; |
| 2630 | case "deep": pw.println(stateToString(mState)); break; |
| 2631 | case "force": pw.println(mForceIdle); break; |
| 2632 | case "screen": pw.println(mScreenOn); break; |
| 2633 | case "charging": pw.println(mCharging); break; |
| 2634 | case "network": pw.println(mNetworkConnected); break; |
| 2635 | default: pw.println("Unknown get option: " + arg); break; |
| 2636 | } |
| 2637 | } finally { |
| 2638 | Binder.restoreCallingIdentity(token); |
| 2639 | } |
| 2640 | } else { |
| 2641 | pw.println("Argument required"); |
| 2642 | } |
| 2643 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2644 | } else if ("disable".equals(cmd)) { |
| 2645 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 2646 | null); |
| 2647 | synchronized (this) { |
| 2648 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2649 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2650 | try { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2651 | boolean becomeActive = false; |
| 2652 | boolean valid = false; |
| 2653 | if (arg == null || "deep".equals(arg) || "all".equals(arg)) { |
| 2654 | valid = true; |
| 2655 | if (mDeepEnabled) { |
| 2656 | mDeepEnabled = false; |
| 2657 | becomeActive = true; |
| 2658 | pw.println("Deep idle mode disabled"); |
| 2659 | } |
| 2660 | } |
| 2661 | if (arg == null || "light".equals(arg) || "all".equals(arg)) { |
| 2662 | valid = true; |
| 2663 | if (mLightEnabled) { |
| 2664 | mLightEnabled = false; |
| 2665 | becomeActive = true; |
| 2666 | pw.println("Light idle mode disabled"); |
| 2667 | } |
| 2668 | } |
| 2669 | if (becomeActive) { |
| 2670 | becomeActiveLocked((arg == null ? "all" : arg) + "-disabled", |
| 2671 | Process.myUid()); |
| 2672 | } |
| 2673 | if (!valid) { |
| 2674 | pw.println("Unknown idle mode: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2675 | } |
| 2676 | } finally { |
| 2677 | Binder.restoreCallingIdentity(token); |
| 2678 | } |
| 2679 | } |
| 2680 | } else if ("enable".equals(cmd)) { |
| 2681 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, |
| 2682 | null); |
| 2683 | synchronized (this) { |
| 2684 | long token = Binder.clearCallingIdentity(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2685 | String arg = shell.getNextArg(); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2686 | try { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2687 | boolean becomeInactive = false; |
| 2688 | boolean valid = false; |
| 2689 | if (arg == null || "deep".equals(arg) || "all".equals(arg)) { |
| 2690 | valid = true; |
| 2691 | if (!mDeepEnabled) { |
| 2692 | mDeepEnabled = true; |
| 2693 | becomeInactive = true; |
| 2694 | pw.println("Deep idle mode enabled"); |
| 2695 | } |
| 2696 | } |
| 2697 | if (arg == null || "light".equals(arg) || "all".equals(arg)) { |
| 2698 | valid = true; |
| 2699 | if (!mLightEnabled) { |
| 2700 | mLightEnabled = true; |
| 2701 | becomeInactive = true; |
| 2702 | pw.println("Light idle mode enable"); |
| 2703 | } |
| 2704 | } |
| 2705 | if (becomeInactive) { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2706 | becomeInactiveIfAppropriateLocked(); |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2707 | } |
| 2708 | if (!valid) { |
| 2709 | pw.println("Unknown idle mode: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2710 | } |
| 2711 | } finally { |
| 2712 | Binder.restoreCallingIdentity(token); |
| 2713 | } |
| 2714 | } |
| 2715 | } else if ("enabled".equals(cmd)) { |
| 2716 | synchronized (this) { |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2717 | String arg = shell.getNextArg(); |
| 2718 | if (arg == null || "all".equals(arg)) { |
| 2719 | pw.println(mDeepEnabled && mLightEnabled ? "1" : 0); |
| 2720 | } else if ("deep".equals(arg)) { |
| 2721 | pw.println(mDeepEnabled ? "1" : 0); |
| 2722 | } else if ("light".equals(arg)) { |
| 2723 | pw.println(mLightEnabled ? "1" : 0); |
| 2724 | } else { |
| 2725 | pw.println("Unknown idle mode: " + arg); |
| 2726 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2727 | } |
| 2728 | } else if ("whitelist".equals(cmd)) { |
Dianne Hackborn | eb909e3 | 2016-09-29 14:35:15 -0700 | [diff] [blame] | 2729 | String arg = shell.getNextArg(); |
| 2730 | if (arg != null) { |
| 2731 | getContext().enforceCallingOrSelfPermission( |
| 2732 | android.Manifest.permission.DEVICE_POWER, null); |
| 2733 | long token = Binder.clearCallingIdentity(); |
| 2734 | try { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2735 | do { |
| 2736 | if (arg.length() < 1 || (arg.charAt(0) != '-' |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 2737 | && arg.charAt(0) != '+' && arg.charAt(0) != '=')) { |
| 2738 | pw.println("Package must be prefixed with +, -, or =: " + arg); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2739 | return -1; |
| 2740 | } |
| 2741 | char op = arg.charAt(0); |
| 2742 | String pkg = arg.substring(1); |
| 2743 | if (op == '+') { |
| 2744 | if (addPowerSaveWhitelistAppInternal(pkg)) { |
| 2745 | pw.println("Added: " + pkg); |
| 2746 | } else { |
| 2747 | pw.println("Unknown package: " + pkg); |
| 2748 | } |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 2749 | } else if (op == '-') { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2750 | if (removePowerSaveWhitelistAppInternal(pkg)) { |
| 2751 | pw.println("Removed: " + pkg); |
| 2752 | } |
Felipe Leme | f8a4623 | 2016-02-10 13:51:54 -0800 | [diff] [blame] | 2753 | } else { |
| 2754 | pw.println(getPowerSaveWhitelistAppInternal(pkg)); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2755 | } |
| 2756 | } while ((arg=shell.getNextArg()) != null); |
Dianne Hackborn | eb909e3 | 2016-09-29 14:35:15 -0700 | [diff] [blame] | 2757 | } finally { |
| 2758 | Binder.restoreCallingIdentity(token); |
| 2759 | } |
| 2760 | } else { |
| 2761 | synchronized (this) { |
| 2762 | for (int j=0; j<mPowerSaveWhitelistAppsExceptIdle.size(); j++) { |
| 2763 | pw.print("system-excidle,"); |
| 2764 | pw.print(mPowerSaveWhitelistAppsExceptIdle.keyAt(j)); |
| 2765 | pw.print(","); |
| 2766 | pw.println(mPowerSaveWhitelistAppsExceptIdle.valueAt(j)); |
| 2767 | } |
| 2768 | for (int j=0; j<mPowerSaveWhitelistApps.size(); j++) { |
| 2769 | pw.print("system,"); |
| 2770 | pw.print(mPowerSaveWhitelistApps.keyAt(j)); |
| 2771 | pw.print(","); |
| 2772 | pw.println(mPowerSaveWhitelistApps.valueAt(j)); |
| 2773 | } |
| 2774 | for (int j=0; j<mPowerSaveWhitelistUserApps.size(); j++) { |
| 2775 | pw.print("user,"); |
| 2776 | pw.print(mPowerSaveWhitelistUserApps.keyAt(j)); |
| 2777 | pw.print(","); |
| 2778 | pw.println(mPowerSaveWhitelistUserApps.valueAt(j)); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2779 | } |
| 2780 | } |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2781 | } |
| 2782 | } else if ("tempwhitelist".equals(cmd)) { |
| 2783 | String opt; |
| 2784 | while ((opt=shell.getNextOption()) != null) { |
| 2785 | if ("-u".equals(opt)) { |
| 2786 | opt = shell.getNextArg(); |
| 2787 | if (opt == null) { |
| 2788 | pw.println("-u requires a user number"); |
| 2789 | return -1; |
| 2790 | } |
| 2791 | shell.userId = Integer.parseInt(opt); |
| 2792 | } |
| 2793 | } |
| 2794 | String arg = shell.getNextArg(); |
| 2795 | if (arg != null) { |
| 2796 | try { |
| 2797 | addPowerSaveTempWhitelistAppChecked(arg, 10000L, shell.userId, "shell"); |
| 2798 | } catch (RemoteException re) { |
| 2799 | pw.println("Failed: " + re); |
| 2800 | } |
| 2801 | } else { |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2802 | dumpTempWhitelistSchedule(pw, false); |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2803 | } |
| 2804 | } else { |
| 2805 | return shell.handleDefaultCommands(cmd); |
| 2806 | } |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2810 | void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2811 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2812 | != PackageManager.PERMISSION_GRANTED) { |
| 2813 | pw.println("Permission Denial: can't dump DeviceIdleController from from pid=" |
| 2814 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 2815 | + " without permission " + android.Manifest.permission.DUMP); |
| 2816 | return; |
| 2817 | } |
| 2818 | |
| 2819 | if (args != null) { |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 2820 | int userId = UserHandle.USER_SYSTEM; |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2821 | for (int i=0; i<args.length; i++) { |
| 2822 | String arg = args[i]; |
| 2823 | if ("-h".equals(arg)) { |
| 2824 | dumpHelp(pw); |
| 2825 | return; |
Amith Yamasani | af575b9 | 2015-05-29 15:35:26 -0700 | [diff] [blame] | 2826 | } else if ("-u".equals(arg)) { |
| 2827 | i++; |
| 2828 | if (i < args.length) { |
| 2829 | arg = args[i]; |
| 2830 | userId = Integer.parseInt(arg); |
| 2831 | } |
Dianne Hackborn | 8d66b3f | 2015-05-08 17:21:48 -0700 | [diff] [blame] | 2832 | } else if ("-a".equals(arg)) { |
| 2833 | // Ignore, we always dump all. |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2834 | } else if (arg.length() > 0 && arg.charAt(0) == '-'){ |
| 2835 | pw.println("Unknown option: " + arg); |
| 2836 | return; |
| 2837 | } else { |
Dianne Hackborn | 9461b6f | 2015-10-07 17:33:16 -0700 | [diff] [blame] | 2838 | Shell shell = new Shell(); |
| 2839 | shell.userId = userId; |
| 2840 | String[] newArgs = new String[args.length-i]; |
| 2841 | System.arraycopy(args, i, newArgs, 0, args.length-i); |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 2842 | shell.exec(mBinderService, null, fd, null, newArgs, null, |
| 2843 | new ResultReceiver(null)); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2844 | return; |
| 2845 | } |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | synchronized (this) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2850 | mConstants.dump(pw); |
| 2851 | |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2852 | if (mEventCmds[0] != EVENT_NULL) { |
| 2853 | pw.println(" Idling history:"); |
| 2854 | long now = SystemClock.elapsedRealtime(); |
| 2855 | for (int i=EVENT_BUFFER_SIZE-1; i>=0; i--) { |
| 2856 | int cmd = mEventCmds[i]; |
| 2857 | if (cmd == EVENT_NULL) { |
| 2858 | continue; |
| 2859 | } |
| 2860 | String label; |
| 2861 | switch (mEventCmds[i]) { |
| 2862 | case EVENT_NORMAL: label = " normal"; break; |
| 2863 | case EVENT_LIGHT_IDLE: label = " light-idle"; break; |
| 2864 | case EVENT_LIGHT_MAINTENANCE: label = "light-maint"; break; |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2865 | case EVENT_DEEP_IDLE: label = " deep-idle"; break; |
| 2866 | case EVENT_DEEP_MAINTENANCE: label = " deep-maint"; break; |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2867 | default: label = " ??"; break; |
| 2868 | } |
| 2869 | pw.print(" "); |
| 2870 | pw.print(label); |
| 2871 | pw.print(": "); |
| 2872 | TimeUtils.formatDuration(mEventTimes[i], now, pw);; |
| 2873 | pw.println(); |
| 2874 | } |
| 2875 | } |
| 2876 | |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2877 | int size = mPowerSaveWhitelistAppsExceptIdle.size(); |
| 2878 | if (size > 0) { |
| 2879 | pw.println(" Whitelist (except idle) system apps:"); |
| 2880 | for (int i = 0; i < size; i++) { |
| 2881 | pw.print(" "); |
| 2882 | pw.println(mPowerSaveWhitelistAppsExceptIdle.keyAt(i)); |
| 2883 | } |
| 2884 | } |
| 2885 | size = mPowerSaveWhitelistApps.size(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2886 | if (size > 0) { |
| 2887 | pw.println(" Whitelist system apps:"); |
| 2888 | for (int i = 0; i < size; i++) { |
| 2889 | pw.print(" "); |
| 2890 | pw.println(mPowerSaveWhitelistApps.keyAt(i)); |
| 2891 | } |
| 2892 | } |
| 2893 | size = mPowerSaveWhitelistUserApps.size(); |
| 2894 | if (size > 0) { |
| 2895 | pw.println(" Whitelist user apps:"); |
| 2896 | for (int i = 0; i < size; i++) { |
| 2897 | pw.print(" "); |
| 2898 | pw.println(mPowerSaveWhitelistUserApps.keyAt(i)); |
| 2899 | } |
| 2900 | } |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2901 | size = mPowerSaveWhitelistExceptIdleAppIds.size(); |
| 2902 | if (size > 0) { |
| 2903 | pw.println(" Whitelist (except idle) all app ids:"); |
| 2904 | for (int i = 0; i < size; i++) { |
| 2905 | pw.print(" "); |
| 2906 | pw.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i)); |
| 2907 | pw.println(); |
| 2908 | } |
| 2909 | } |
Dianne Hackborn | 262ae5c | 2016-02-10 16:28:29 -0800 | [diff] [blame] | 2910 | size = mPowerSaveWhitelistUserAppIds.size(); |
| 2911 | if (size > 0) { |
| 2912 | pw.println(" Whitelist user app ids:"); |
| 2913 | for (int i = 0; i < size; i++) { |
| 2914 | pw.print(" "); |
| 2915 | pw.print(mPowerSaveWhitelistUserAppIds.keyAt(i)); |
| 2916 | pw.println(); |
| 2917 | } |
| 2918 | } |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2919 | size = mPowerSaveWhitelistAllAppIds.size(); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2920 | if (size > 0) { |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2921 | pw.println(" Whitelist all app ids:"); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2922 | for (int i = 0; i < size; i++) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2923 | pw.print(" "); |
Dianne Hackborn | 3b16cf4 | 2015-07-01 15:05:04 -0700 | [diff] [blame] | 2924 | pw.print(mPowerSaveWhitelistAllAppIds.keyAt(i)); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2925 | pw.println(); |
| 2926 | } |
| 2927 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2928 | dumpTempWhitelistSchedule(pw, true); |
| 2929 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 2930 | size = mTempWhitelistAppIdArray != null ? mTempWhitelistAppIdArray.length : 0; |
| 2931 | if (size > 0) { |
| 2932 | pw.println(" Temp whitelist app ids:"); |
| 2933 | for (int i = 0; i < size; i++) { |
| 2934 | pw.print(" "); |
| 2935 | pw.print(mTempWhitelistAppIdArray[i]); |
| 2936 | pw.println(); |
| 2937 | } |
| 2938 | } |
Adam Lesinski | 31c05d1 | 2015-06-09 17:34:04 -0700 | [diff] [blame] | 2939 | |
Dianne Hackborn | b684365 | 2016-02-22 12:20:13 -0800 | [diff] [blame] | 2940 | pw.print(" mLightEnabled="); pw.print(mLightEnabled); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 2941 | pw.print(" mDeepEnabled="); pw.println(mDeepEnabled); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 2942 | pw.print(" mForceIdle="); pw.println(mForceIdle); |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2943 | pw.print(" mMotionSensor="); pw.println(mMotionSensor); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2944 | pw.print(" mCurDisplay="); pw.println(mCurDisplay); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2945 | pw.print(" mScreenOn="); pw.println(mScreenOn); |
Dianne Hackborn | 88c4135 | 2016-04-07 15:18:58 -0700 | [diff] [blame] | 2946 | pw.print(" mNetworkConnected="); pw.println(mNetworkConnected); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2947 | pw.print(" mCharging="); pw.println(mCharging); |
Nick Vaccaro | 20feaea | 2015-09-17 17:22:44 -0700 | [diff] [blame] | 2948 | pw.print(" mMotionActive="); pw.println(mMotionListener.active); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2949 | pw.print(" mNotMoving="); pw.println(mNotMoving); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 2950 | pw.print(" mLocating="); pw.print(mLocating); pw.print(" mHasGps="); |
| 2951 | pw.print(mHasGps); pw.print(" mHasNetwork="); |
| 2952 | pw.print(mHasNetworkLocation); pw.print(" mLocated="); pw.println(mLocated); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 2953 | if (mLastGenericLocation != null) { |
| 2954 | pw.print(" mLastGenericLocation="); pw.println(mLastGenericLocation); |
| 2955 | } |
| 2956 | if (mLastGpsLocation != null) { |
| 2957 | pw.print(" mLastGpsLocation="); pw.println(mLastGpsLocation); |
| 2958 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2959 | pw.print(" mState="); pw.print(stateToString(mState)); |
| 2960 | pw.print(" mLightState="); |
| 2961 | pw.println(lightStateToString(mLightState)); |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2962 | pw.print(" mInactiveTimeout="); TimeUtils.formatDuration(mInactiveTimeout, pw); |
| 2963 | pw.println(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 2964 | if (mActiveIdleOpCount != 0) { |
| 2965 | pw.print(" mActiveIdleOpCount="); pw.println(mActiveIdleOpCount); |
| 2966 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 2967 | if (mNextAlarmTime != 0) { |
| 2968 | pw.print(" mNextAlarmTime="); |
| 2969 | TimeUtils.formatDuration(mNextAlarmTime, SystemClock.elapsedRealtime(), pw); |
| 2970 | pw.println(); |
| 2971 | } |
| 2972 | if (mNextIdlePendingDelay != 0) { |
| 2973 | pw.print(" mNextIdlePendingDelay="); |
| 2974 | TimeUtils.formatDuration(mNextIdlePendingDelay, pw); |
| 2975 | pw.println(); |
| 2976 | } |
| 2977 | if (mNextIdleDelay != 0) { |
| 2978 | pw.print(" mNextIdleDelay="); |
| 2979 | TimeUtils.formatDuration(mNextIdleDelay, pw); |
| 2980 | pw.println(); |
| 2981 | } |
Dianne Hackborn | 953fc94 | 2016-03-29 15:36:24 -0700 | [diff] [blame] | 2982 | if (mNextLightIdleDelay != 0) { |
| 2983 | pw.print(" mNextIdleDelay="); |
| 2984 | TimeUtils.formatDuration(mNextLightIdleDelay, pw); |
| 2985 | pw.println(); |
| 2986 | } |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 2987 | if (mNextLightAlarmTime != 0) { |
| 2988 | pw.print(" mNextLightAlarmTime="); |
| 2989 | TimeUtils.formatDuration(mNextLightAlarmTime, SystemClock.elapsedRealtime(), pw); |
| 2990 | pw.println(); |
| 2991 | } |
Dianne Hackborn | 8ed2b97 | 2015-11-18 14:52:04 -0800 | [diff] [blame] | 2992 | if (mCurIdleBudget != 0) { |
| 2993 | pw.print(" mCurIdleBudget="); |
| 2994 | TimeUtils.formatDuration(mCurIdleBudget, pw); |
| 2995 | pw.println(); |
| 2996 | } |
| 2997 | if (mMaintenanceStartTime != 0) { |
| 2998 | pw.print(" mMaintenanceStartTime="); |
| 2999 | TimeUtils.formatDuration(mMaintenanceStartTime, SystemClock.elapsedRealtime(), pw); |
| 3000 | pw.println(); |
| 3001 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3002 | if (mJobsActive) { |
| 3003 | pw.print(" mJobsActive="); pw.println(mJobsActive); |
| 3004 | } |
| 3005 | if (mAlarmsActive) { |
| 3006 | pw.print(" mAlarmsActive="); pw.println(mAlarmsActive); |
| 3007 | } |
Dianne Hackborn | 0b4daca | 2015-04-27 09:47:32 -0700 | [diff] [blame] | 3008 | } |
| 3009 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3010 | |
| 3011 | void dumpTempWhitelistSchedule(PrintWriter pw, boolean printTitle) { |
| 3012 | final int size = mTempWhitelistAppIdEndTimes.size(); |
| 3013 | if (size > 0) { |
| 3014 | String prefix = ""; |
| 3015 | if (printTitle) { |
| 3016 | pw.println(" Temp whitelist schedule:"); |
| 3017 | prefix = " "; |
| 3018 | } |
| 3019 | final long timeNow = SystemClock.elapsedRealtime(); |
| 3020 | for (int i = 0; i < size; i++) { |
| 3021 | pw.print(prefix); |
| 3022 | pw.print("UID="); |
| 3023 | pw.print(mTempWhitelistAppIdEndTimes.keyAt(i)); |
| 3024 | pw.print(": "); |
| 3025 | Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.valueAt(i); |
| 3026 | TimeUtils.formatDuration(entry.first.value, timeNow, pw); |
| 3027 | pw.print(" - "); |
| 3028 | pw.println(entry.second); |
| 3029 | } |
| 3030 | } |
| 3031 | } |
| 3032 | } |