blob: 0c2cf60ccea764f6332223596eade5ba8f6fab6d [file] [log] [blame]
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001/*
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
17package com.android.server;
18
Amith Yamasaniaf575b92015-05-29 15:35:26 -070019import android.Manifest;
Sudheer Shankadc589ac2016-11-10 15:30:17 -080020import android.app.ActivityManager;
Dianne Hackborn85e35642017-01-12 15:10:57 -080021import android.app.ActivityManagerInternal;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070022import android.app.AlarmManager;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070023import android.content.BroadcastReceiver;
Adam Lesinski31c05d12015-06-09 17:34:04 -070024import android.content.ContentResolver;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070025import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.pm.ApplicationInfo;
29import android.content.pm.PackageManager;
Amith Yamasaniaf575b92015-05-29 15:35:26 -070030import android.content.pm.PackageManager.NameNotFoundException;
Adam Lesinski31c05d12015-06-09 17:34:04 -070031import android.database.ContentObserver;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070032import android.hardware.Sensor;
33import android.hardware.SensorManager;
Nick Vaccaro20feaea2015-09-17 17:22:44 -070034import android.hardware.SensorEvent;
35import android.hardware.SensorEventListener;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070036import android.hardware.TriggerEvent;
37import android.hardware.TriggerEventListener;
38import android.hardware.display.DisplayManager;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -070039import android.location.LocationRequest;
40import android.location.Location;
41import android.location.LocationListener;
42import android.location.LocationManager;
Dianne Hackborn88c41352016-04-07 15:18:58 -070043import android.net.ConnectivityManager;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070044import android.net.INetworkPolicyManager;
Dianne Hackborn88c41352016-04-07 15:18:58 -070045import android.net.NetworkInfo;
Adam Lesinski31c05d12015-06-09 17:34:04 -070046import android.net.Uri;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -070047import android.os.BatteryStats;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070048import android.os.Binder;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -070049import android.os.Bundle;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070050import android.os.Environment;
51import android.os.FileUtils;
52import android.os.Handler;
53import android.os.IDeviceIdleController;
Yao Chenca5edbb2016-01-13 14:44:36 -080054import android.os.IMaintenanceActivityListener;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070055import android.os.Looper;
56import android.os.Message;
57import android.os.PowerManager;
58import android.os.PowerManagerInternal;
Dianne Hackbornb6683c42015-06-18 17:40:33 -070059import android.os.Process;
Yao Chenca5edbb2016-01-13 14:44:36 -080060import android.os.RemoteCallbackList;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070061import android.os.RemoteException;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -070062import android.os.ResultReceiver;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070063import android.os.ServiceManager;
Dianne Hackborn354736e2016-08-22 17:00:05 -070064import android.os.ShellCallback;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -070065import android.os.ShellCommand;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070066import android.os.SystemClock;
67import android.os.UserHandle;
Adam Lesinski31c05d12015-06-09 17:34:04 -070068import android.provider.Settings;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070069import android.util.ArrayMap;
70import android.util.ArraySet;
Adam Lesinski31c05d12015-06-09 17:34:04 -070071import android.util.KeyValueListParser;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -070072import android.util.MutableLong;
73import android.util.Pair;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070074import android.util.Slog;
Dianne Hackbornfd854ee2015-07-13 18:00:37 -070075import android.util.SparseArray;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070076import android.util.SparseBooleanArray;
77import android.util.TimeUtils;
78import android.util.Xml;
79import android.view.Display;
Amith Yamasani520d8f22015-05-08 16:36:21 -070080
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070081import com.android.internal.app.IBatteryStats;
82import com.android.internal.os.AtomicFile;
83import com.android.internal.os.BackgroundThread;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060084import com.android.internal.util.DumpUtils;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070085import com.android.internal.util.FastXmlSerializer;
86import com.android.internal.util.XmlUtils;
87import com.android.server.am.BatteryStatsService;
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -070088import com.android.server.net.NetworkPolicyManagerInternal;
Amith Yamasani520d8f22015-05-08 16:36:21 -070089
Dianne Hackborn0b4daca2015-04-27 09:47:32 -070090import org.xmlpull.v1.XmlPullParser;
91import org.xmlpull.v1.XmlPullParserException;
92import org.xmlpull.v1.XmlSerializer;
93
94import java.io.ByteArrayOutputStream;
95import java.io.File;
96import java.io.FileDescriptor;
97import java.io.FileInputStream;
98import java.io.FileNotFoundException;
99import java.io.FileOutputStream;
100import java.io.IOException;
101import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +0100102import java.nio.charset.StandardCharsets;
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700103import java.util.Arrays;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700104
105/**
106 * Keeps track of device idleness and drives low power mode based on that.
107 */
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700108public class DeviceIdleController extends SystemService
109 implements AnyMotionDetector.DeviceIdleCallback {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700110 private static final String TAG = "DeviceIdleController";
111
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700112 private static final boolean DEBUG = false;
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700113
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700114 private static final boolean COMPRESS_TIME = false;
Amith Yamasani520d8f22015-05-08 16:36:21 -0700115
Dianne Hackborn953fc942016-03-29 15:36:24 -0700116 private static final int EVENT_BUFFER_SIZE = 100;
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800117
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700118 private AlarmManager mAlarmManager;
119 private IBatteryStats mBatteryStats;
Dianne Hackborn85e35642017-01-12 15:10:57 -0800120 private ActivityManagerInternal mLocalActivityManager;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700121 private PowerManagerInternal mLocalPowerManager;
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700122 private PowerManager mPowerManager;
Dianne Hackborn88c41352016-04-07 15:18:58 -0700123 private ConnectivityService mConnectivityService;
Dianne Hackborn262ae5c2016-02-10 16:28:29 -0800124 private AlarmManagerService.LocalService mLocalAlarmManager;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700125 private INetworkPolicyManager mNetworkPolicyManager;
126 private DisplayManager mDisplayManager;
127 private SensorManager mSensorManager;
Nick Vaccaro20feaea2015-09-17 17:22:44 -0700128 private Sensor mMotionSensor;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700129 private LocationManager mLocationManager;
130 private LocationRequest mLocationRequest;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700131 private Intent mIdleIntent;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700132 private Intent mLightIdleIntent;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700133 private Display mCurDisplay;
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700134 private AnyMotionDetector mAnyMotionDetector;
Dianne Hackbornb6843652016-02-22 12:20:13 -0800135 private boolean mLightEnabled;
136 private boolean mDeepEnabled;
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700137 private boolean mForceIdle;
Dianne Hackborn88c41352016-04-07 15:18:58 -0700138 private boolean mNetworkConnected;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700139 private boolean mScreenOn;
140 private boolean mCharging;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700141 private boolean mNotMoving;
142 private boolean mLocating;
143 private boolean mLocated;
Joe LaPenna23d681b2015-08-27 15:12:11 -0700144 private boolean mHasGps;
145 private boolean mHasNetworkLocation;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700146 private Location mLastGenericLocation;
147 private Location mLastGpsLocation;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700148
149 /** Device is currently active. */
150 private static final int STATE_ACTIVE = 0;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700151 /** Device is inactive (screen off, no motion) and we are waiting to for idle. */
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700152 private static final int STATE_INACTIVE = 1;
153 /** Device is past the initial inactive period, and waiting for the next idle period. */
154 private static final int STATE_IDLE_PENDING = 2;
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700155 /** Device is currently sensing motion. */
156 private static final int STATE_SENSING = 3;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700157 /** Device is currently finding location (and may still be sensing). */
158 private static final int STATE_LOCATING = 4;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700159 /** Device is in the idle state, trying to stay asleep as much as possible. */
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700160 private static final int STATE_IDLE = 5;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700161 /** Device is in the idle state, but temporarily out of idle to do regular maintenance. */
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700162 private static final int STATE_IDLE_MAINTENANCE = 6;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700163 private static String stateToString(int state) {
164 switch (state) {
165 case STATE_ACTIVE: return "ACTIVE";
166 case STATE_INACTIVE: return "INACTIVE";
167 case STATE_IDLE_PENDING: return "IDLE_PENDING";
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700168 case STATE_SENSING: return "SENSING";
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700169 case STATE_LOCATING: return "LOCATING";
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700170 case STATE_IDLE: return "IDLE";
171 case STATE_IDLE_MAINTENANCE: return "IDLE_MAINTENANCE";
172 default: return Integer.toString(state);
173 }
174 }
175
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700176 /** Device is currently active. */
177 private static final int LIGHT_STATE_ACTIVE = 0;
178 /** Device is inactive (screen off) and we are waiting to for the first light idle. */
179 private static final int LIGHT_STATE_INACTIVE = 1;
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700180 /** Device is about to go idle for the first time, wait for current work to complete. */
181 private static final int LIGHT_STATE_PRE_IDLE = 3;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700182 /** Device is in the light idle state, trying to stay asleep as much as possible. */
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700183 private static final int LIGHT_STATE_IDLE = 4;
Dianne Hackborn88c41352016-04-07 15:18:58 -0700184 /** Device is in the light idle state, we want to go in to idle maintenance but are
185 * waiting for network connectivity before doing so. */
186 private static final int LIGHT_STATE_WAITING_FOR_NETWORK = 5;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700187 /** Device is in the light idle state, but temporarily out of idle to do regular maintenance. */
Dianne Hackborn88c41352016-04-07 15:18:58 -0700188 private static final int LIGHT_STATE_IDLE_MAINTENANCE = 6;
Dianne Hackbornb6843652016-02-22 12:20:13 -0800189 /** Device light idle state is overriden, now applying deep doze state. */
Dianne Hackborn88c41352016-04-07 15:18:58 -0700190 private static final int LIGHT_STATE_OVERRIDE = 7;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700191 private static String lightStateToString(int state) {
192 switch (state) {
193 case LIGHT_STATE_ACTIVE: return "ACTIVE";
194 case LIGHT_STATE_INACTIVE: return "INACTIVE";
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700195 case LIGHT_STATE_PRE_IDLE: return "PRE_IDLE";
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700196 case LIGHT_STATE_IDLE: return "IDLE";
Dianne Hackborn88c41352016-04-07 15:18:58 -0700197 case LIGHT_STATE_WAITING_FOR_NETWORK: return "WAITING_FOR_NETWORK";
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700198 case LIGHT_STATE_IDLE_MAINTENANCE: return "IDLE_MAINTENANCE";
199 case LIGHT_STATE_OVERRIDE: return "OVERRIDE";
200 default: return Integer.toString(state);
201 }
202 }
203
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700204 private int mState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700205 private int mLightState;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700206
207 private long mInactiveTimeout;
208 private long mNextAlarmTime;
209 private long mNextIdlePendingDelay;
210 private long mNextIdleDelay;
Dianne Hackborn953fc942016-03-29 15:36:24 -0700211 private long mNextLightIdleDelay;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700212 private long mNextLightAlarmTime;
Kevin Gabayan92f15e62016-04-04 17:52:22 -0700213 private long mNextSensingTimeoutAlarmTime;
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800214 private long mCurIdleBudget;
215 private long mMaintenanceStartTime;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700216
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800217 private int mActiveIdleOpCount;
Joe Onorato8f0e9ced2016-12-08 17:48:49 -0800218 private PowerManager.WakeLock mActiveIdleWakeLock; // held when there are operations in progress
219 private PowerManager.WakeLock mGoingIdleWakeLock; // held when we are going idle so hardware
220 // (especially NetworkPolicyManager) can shut
221 // down.
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800222 private boolean mJobsActive;
223 private boolean mAlarmsActive;
Yao Chenca5edbb2016-01-13 14:44:36 -0800224 private boolean mReportedMaintenanceActivity;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800225
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700226 public final AtomicFile mConfigFile;
227
Yao Chenca5edbb2016-01-13 14:44:36 -0800228 private final RemoteCallbackList<IMaintenanceActivityListener> mMaintenanceActivityListeners =
229 new RemoteCallbackList<IMaintenanceActivityListener>();
230
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700231 /**
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700232 * Package names the system has white-listed to opt out of power save restrictions,
233 * except for device idle mode.
234 */
235 private final ArrayMap<String, Integer> mPowerSaveWhitelistAppsExceptIdle = new ArrayMap<>();
236
237 /**
Sudheer Shanka3f4d7702017-04-28 17:38:03 -0700238 * Package names the user has white-listed using commandline option to opt out of
239 * power save restrictions, except for device idle mode.
240 */
241 private final ArraySet<String> mPowerSaveWhitelistUserAppsExceptIdle = new ArraySet<>();
242
243 /**
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700244 * Package names the system has white-listed to opt out of power save restrictions for
245 * all modes.
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700246 */
247 private final ArrayMap<String, Integer> mPowerSaveWhitelistApps = new ArrayMap<>();
248
249 /**
250 * Package names the user has white-listed to opt out of power save restrictions.
251 */
252 private final ArrayMap<String, Integer> mPowerSaveWhitelistUserApps = new ArrayMap<>();
253
254 /**
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700255 * App IDs of built-in system apps that have been white-listed except for idle modes.
256 */
257 private final SparseBooleanArray mPowerSaveWhitelistSystemAppIdsExceptIdle
258 = new SparseBooleanArray();
259
260 /**
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700261 * App IDs of built-in system apps that have been white-listed.
262 */
263 private final SparseBooleanArray mPowerSaveWhitelistSystemAppIds = new SparseBooleanArray();
264
265 /**
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700266 * App IDs that have been white-listed to opt out of power save restrictions, except
267 * for device idle modes.
268 */
269 private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray();
270
271 /**
272 * Current app IDs that are in the complete power save white list, but shouldn't be
273 * excluded from idle modes. This array can be shared with others because it will not be
274 * modified once set.
275 */
276 private int[] mPowerSaveWhitelistExceptIdleAppIdArray = new int[0];
277
278 /**
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700279 * App IDs that have been white-listed to opt out of power save restrictions.
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700280 */
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700281 private final SparseBooleanArray mPowerSaveWhitelistAllAppIds = new SparseBooleanArray();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700282
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700283 /**
284 * Current app IDs that are in the complete power save white list. This array can
285 * be shared with others because it will not be modified once set.
286 */
Dianne Hackborn3b16cf42015-07-01 15:05:04 -0700287 private int[] mPowerSaveWhitelistAllAppIdArray = new int[0];
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -0700288
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700289 /**
Dianne Hackborn262ae5c2016-02-10 16:28:29 -0800290 * App IDs that have been white-listed by the user to opt out of power save restrictions.
291 */
292 private final SparseBooleanArray mPowerSaveWhitelistUserAppIds = new SparseBooleanArray();
293
294 /**
295 * Current app IDs that are in the user power save white list. This array can
296 * be shared with others because it will not be modified once set.
297 */
298 private int[] mPowerSaveWhitelistUserAppIdArray = new int[0];
299
300 /**
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700301 * List of end times for UIDs that are temporarily marked as being allowed to access
302 * the network and acquire wakelocks. Times are in milliseconds.
303 */
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700304 private final SparseArray<Pair<MutableLong, String>> mTempWhitelistAppIdEndTimes
305 = new SparseArray<>();
306
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -0700307 private NetworkPolicyManagerInternal mNetworkPolicyManagerInternal;
Amith Yamasaniaf575b92015-05-29 15:35:26 -0700308
309 /**
310 * Current app IDs of temporarily whitelist apps for high-priority messages.
311 */
312 private int[] mTempWhitelistAppIdArray = new int[0];
313
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800314 private static final int EVENT_NULL = 0;
315 private static final int EVENT_NORMAL = 1;
316 private static final int EVENT_LIGHT_IDLE = 2;
317 private static final int EVENT_LIGHT_MAINTENANCE = 3;
Dianne Hackbornb6843652016-02-22 12:20:13 -0800318 private static final int EVENT_DEEP_IDLE = 4;
319 private static final int EVENT_DEEP_MAINTENANCE = 5;
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800320
Dianne Hackbornef3aa6e2016-04-29 18:18:08 -0700321 private final int[] mEventCmds = new int[EVENT_BUFFER_SIZE];
322 private final long[] mEventTimes = new long[EVENT_BUFFER_SIZE];
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800323
324 private void addEvent(int cmd) {
325 if (mEventCmds[0] != cmd) {
326 System.arraycopy(mEventCmds, 0, mEventCmds, 1, EVENT_BUFFER_SIZE - 1);
327 System.arraycopy(mEventTimes, 0, mEventTimes, 1, EVENT_BUFFER_SIZE - 1);
328 mEventCmds[0] = cmd;
329 mEventTimes[0] = SystemClock.elapsedRealtime();
330 }
331 }
332
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700333 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
334 @Override public void onReceive(Context context, Intent intent) {
Dianne Hackborn88c41352016-04-07 15:18:58 -0700335 switch (intent.getAction()) {
336 case ConnectivityManager.CONNECTIVITY_ACTION: {
Dianne Hackborn4cb96ca2016-05-17 13:55:29 -0700337 updateConnectivityState(intent);
Dianne Hackborn88c41352016-04-07 15:18:58 -0700338 } break;
339 case Intent.ACTION_BATTERY_CHANGED: {
340 synchronized (DeviceIdleController.this) {
341 int plugged = intent.getIntExtra("plugged", 0);
342 updateChargingLocked(plugged != 0);
343 }
344 } break;
345 case Intent.ACTION_PACKAGE_REMOVED: {
346 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
347 Uri data = intent.getData();
348 String ssp;
349 if (data != null && (ssp = data.getSchemeSpecificPart()) != null) {
350 removePowerSaveWhitelistAppInternal(ssp);
351 }
352 }
353 } break;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700354 }
355 }
356 };
357
358 private final AlarmManager.OnAlarmListener mLightAlarmListener
359 = new AlarmManager.OnAlarmListener() {
360 @Override
361 public void onAlarm() {
362 synchronized (DeviceIdleController.this) {
363 stepLightIdleStateLocked("s:alarm");
364 }
365 }
366 };
367
Kevin Gabayan92f15e62016-04-04 17:52:22 -0700368 private final AlarmManager.OnAlarmListener mSensingTimeoutAlarmListener
369 = new AlarmManager.OnAlarmListener() {
370 @Override
371 public void onAlarm() {
372 if (mState == STATE_SENSING) {
373 synchronized (DeviceIdleController.this) {
374 becomeInactiveIfAppropriateLocked();
375 }
376 }
377 }
378 };
379
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700380 private final AlarmManager.OnAlarmListener mDeepAlarmListener
381 = new AlarmManager.OnAlarmListener() {
382 @Override
383 public void onAlarm() {
384 synchronized (DeviceIdleController.this) {
385 stepIdleStateLocked("s:alarm");
386 }
387 }
388 };
389
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800390 private final BroadcastReceiver mIdleStartedDoneReceiver = new BroadcastReceiver() {
391 @Override public void onReceive(Context context, Intent intent) {
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700392 // When coming out of a deep idle, we will add in some delay before we allow
393 // the system to settle down and finish the maintenance window. This is
394 // to give a chance for any pending work to be scheduled.
395 if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())) {
396 mHandler.sendEmptyMessageDelayed(MSG_FINISH_IDLE_OP,
397 mConstants.MIN_DEEP_MAINTENANCE_TIME);
398 } else {
399 mHandler.sendEmptyMessageDelayed(MSG_FINISH_IDLE_OP,
400 mConstants.MIN_LIGHT_MAINTENANCE_TIME);
401 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800402 }
403 };
404
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700405 private final DisplayManager.DisplayListener mDisplayListener
406 = new DisplayManager.DisplayListener() {
407 @Override public void onDisplayAdded(int displayId) {
408 }
409
410 @Override public void onDisplayRemoved(int displayId) {
411 }
412
413 @Override public void onDisplayChanged(int displayId) {
414 if (displayId == Display.DEFAULT_DISPLAY) {
415 synchronized (DeviceIdleController.this) {
416 updateDisplayLocked();
417 }
418 }
419 }
420 };
421
Nick Vaccaro20feaea2015-09-17 17:22:44 -0700422 private final class MotionListener extends TriggerEventListener
423 implements SensorEventListener {
424
425 boolean active = false;
426
427 @Override
428 public void onTrigger(TriggerEvent event) {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700429 synchronized (DeviceIdleController.this) {
Nick Vaccaro20feaea2015-09-17 17:22:44 -0700430 active = false;
431 motionLocked();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700432 }
433 }
Nick Vaccaro20feaea2015-09-17 17:22:44 -0700434
435 @Override
436 public void onSensorChanged(SensorEvent event) {
437 synchronized (DeviceIdleController.this) {
438 mSensorManager.unregisterListener(this, mMotionSensor);
439 active = false;
440 motionLocked();
441 }
442 }
443
444 @Override
445 public void onAccuracyChanged(Sensor sensor, int accuracy) {}
446
447 public boolean registerLocked() {
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700448 boolean success;
Nick Vaccaro20feaea2015-09-17 17:22:44 -0700449 if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) {
450 success = mSensorManager.requestTriggerSensor(mMotionListener, mMotionSensor);
451 } else {
452 success = mSensorManager.registerListener(
453 mMotionListener, mMotionSensor, SensorManager.SENSOR_DELAY_NORMAL);
454 }
455 if (success) {
456 active = true;
457 } else {
458 Slog.e(TAG, "Unable to register for " + mMotionSensor);
459 }
460 return success;
461 }
462
463 public void unregisterLocked() {
464 if (mMotionSensor.getReportingMode() == Sensor.REPORTING_MODE_ONE_SHOT) {
465 mSensorManager.cancelTriggerSensor(mMotionListener, mMotionSensor);
466 } else {
467 mSensorManager.unregisterListener(mMotionListener);
468 }
469 active = false;
470 }
471 }
472 private final MotionListener mMotionListener = new MotionListener();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -0700473
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700474 private final LocationListener mGenericLocationListener = new LocationListener() {
475 @Override
476 public void onLocationChanged(Location location) {
477 synchronized (DeviceIdleController.this) {
478 receivedGenericLocationLocked(location);
479 }
480 }
481
482 @Override
483 public void onStatusChanged(String provider, int status, Bundle extras) {
484 }
485
486 @Override
487 public void onProviderEnabled(String provider) {
488 }
489
490 @Override
491 public void onProviderDisabled(String provider) {
492 }
493 };
494
495 private final LocationListener mGpsLocationListener = new LocationListener() {
496 @Override
497 public void onLocationChanged(Location location) {
498 synchronized (DeviceIdleController.this) {
499 receivedGpsLocationLocked(location);
500 }
501 }
502
503 @Override
504 public void onStatusChanged(String provider, int status, Bundle extras) {
505 }
506
507 @Override
508 public void onProviderEnabled(String provider) {
509 }
510
511 @Override
512 public void onProviderDisabled(String provider) {
513 }
514 };
515
Adam Lesinski31c05d12015-06-09 17:34:04 -0700516 /**
517 * All times are in milliseconds. These constants are kept synchronized with the system
518 * global Settings. Any access to this class or its fields should be done while
519 * holding the DeviceIdleController lock.
520 */
Dianne Hackborna750a632015-06-16 17:18:23 -0700521 private final class Constants extends ContentObserver {
Adam Lesinski31c05d12015-06-09 17:34:04 -0700522 // Key names stored in the settings value.
Dianne Hackborn953fc942016-03-29 15:36:24 -0700523 private static final String KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT
524 = "light_after_inactive_to";
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700525 private static final String KEY_LIGHT_PRE_IDLE_TIMEOUT = "light_pre_idle_to";
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700526 private static final String KEY_LIGHT_IDLE_TIMEOUT = "light_idle_to";
Dianne Hackborn953fc942016-03-29 15:36:24 -0700527 private static final String KEY_LIGHT_IDLE_FACTOR = "light_idle_factor";
528 private static final String KEY_LIGHT_MAX_IDLE_TIMEOUT = "light_max_idle_to";
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800529 private static final String KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET
530 = "light_idle_maintenance_min_budget";
531 private static final String KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET
532 = "light_idle_maintenance_max_budget";
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700533 private static final String KEY_MIN_LIGHT_MAINTENANCE_TIME = "min_light_maintenance_time";
534 private static final String KEY_MIN_DEEP_MAINTENANCE_TIME = "min_deep_maintenance_time";
Adam Lesinski31c05d12015-06-09 17:34:04 -0700535 private static final String KEY_INACTIVE_TIMEOUT = "inactive_to";
536 private static final String KEY_SENSING_TIMEOUT = "sensing_to";
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700537 private static final String KEY_LOCATING_TIMEOUT = "locating_to";
538 private static final String KEY_LOCATION_ACCURACY = "location_accuracy";
Adam Lesinski31c05d12015-06-09 17:34:04 -0700539 private static final String KEY_MOTION_INACTIVE_TIMEOUT = "motion_inactive_to";
540 private static final String KEY_IDLE_AFTER_INACTIVE_TIMEOUT = "idle_after_inactive_to";
541 private static final String KEY_IDLE_PENDING_TIMEOUT = "idle_pending_to";
542 private static final String KEY_MAX_IDLE_PENDING_TIMEOUT = "max_idle_pending_to";
543 private static final String KEY_IDLE_PENDING_FACTOR = "idle_pending_factor";
544 private static final String KEY_IDLE_TIMEOUT = "idle_to";
545 private static final String KEY_MAX_IDLE_TIMEOUT = "max_idle_to";
546 private static final String KEY_IDLE_FACTOR = "idle_factor";
547 private static final String KEY_MIN_TIME_TO_ALARM = "min_time_to_alarm";
548 private static final String KEY_MAX_TEMP_APP_WHITELIST_DURATION =
549 "max_temp_app_whitelist_duration";
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700550 private static final String KEY_MMS_TEMP_APP_WHITELIST_DURATION =
551 "mms_temp_app_whitelist_duration";
Dianne Hackborn451c3462015-07-21 17:39:46 -0700552 private static final String KEY_SMS_TEMP_APP_WHITELIST_DURATION =
553 "sms_temp_app_whitelist_duration";
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700554 private static final String KEY_NOTIFICATION_WHITELIST_DURATION =
555 "notification_whitelist_duration";
Adam Lesinski31c05d12015-06-09 17:34:04 -0700556
557 /**
Dianne Hackborn953fc942016-03-29 15:36:24 -0700558 * This is the time, after becoming inactive, that we go in to the first
559 * light-weight idle mode.
560 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
561 * @see #KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT
562 */
563 public long LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT;
564
565 /**
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700566 * This is amount of time we will wait from the point where we decide we would
567 * like to go idle until we actually do, while waiting for jobs and other current
568 * activity to finish.
569 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
570 * @see #KEY_LIGHT_PRE_IDLE_TIMEOUT
571 */
572 public long LIGHT_PRE_IDLE_TIMEOUT;
573
574 /**
Dianne Hackborn953fc942016-03-29 15:36:24 -0700575 * This is the initial time that we will run in idle maintenance mode.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700576 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
577 * @see #KEY_LIGHT_IDLE_TIMEOUT
578 */
579 public long LIGHT_IDLE_TIMEOUT;
580
581 /**
Dianne Hackborn953fc942016-03-29 15:36:24 -0700582 * Scaling factor to apply to the light idle mode time each time we complete a cycle.
583 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
584 * @see #KEY_LIGHT_IDLE_FACTOR
585 */
586 public float LIGHT_IDLE_FACTOR;
587
588 /**
589 * This is the maximum time we will run in idle maintenence mode.
590 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
591 * @see #KEY_LIGHT_MAX_IDLE_TIMEOUT
592 */
593 public long LIGHT_MAX_IDLE_TIMEOUT;
594
595 /**
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800596 * This is the minimum amount of time we want to make available for maintenance mode
597 * when lightly idling. That is, we will always have at least this amount of time
598 * available maintenance before timing out and cutting off maintenance mode.
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700599 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800600 * @see #KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700601 */
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800602 public long LIGHT_IDLE_MAINTENANCE_MIN_BUDGET;
603
604 /**
605 * This is the maximum amount of time we want to make available for maintenance mode
606 * when lightly idling. That is, if the system isn't using up its minimum maintenance
607 * budget and this time is being added to the budget reserve, this is the maximum
608 * reserve size we will allow to grow and thus the maximum amount of time we will
609 * allow for the maintenance window.
610 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
611 * @see #KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET
612 */
613 public long LIGHT_IDLE_MAINTENANCE_MAX_BUDGET;
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700614
615 /**
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700616 * This is the minimum amount of time that we will stay in maintenance mode after
617 * a light doze. We have this minimum to allow various things to respond to switching
618 * in to maintenance mode and scheduling their work -- otherwise we may
Dianne Hackborn7ab40252016-06-15 17:30:24 -0700619 * see there is nothing to do (no jobs pending) and go out of maintenance
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700620 * mode immediately.
621 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
622 * @see #KEY_MIN_LIGHT_MAINTENANCE_TIME
623 */
624 public long MIN_LIGHT_MAINTENANCE_TIME;
625
626 /**
627 * This is the minimum amount of time that we will stay in maintenance mode after
628 * a full doze. We have this minimum to allow various things to respond to switching
629 * in to maintenance mode and scheduling their work -- otherwise we may
Dianne Hackborn7ab40252016-06-15 17:30:24 -0700630 * see there is nothing to do (no jobs pending) and go out of maintenance
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700631 * mode immediately.
632 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
633 * @see #KEY_MIN_DEEP_MAINTENANCE_TIME
634 */
635 public long MIN_DEEP_MAINTENANCE_TIME;
636
637 /**
Adam Lesinski31c05d12015-06-09 17:34:04 -0700638 * This is the time, after becoming inactive, at which we start looking at the
639 * motion sensor to determine if the device is being left alone. We don't do this
640 * immediately after going inactive just because we don't want to be continually running
Nick Vaccaro20feaea2015-09-17 17:22:44 -0700641 * the motion sensor whenever the screen is off.
Adam Lesinski31c05d12015-06-09 17:34:04 -0700642 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
643 * @see #KEY_INACTIVE_TIMEOUT
644 */
645 public long INACTIVE_TIMEOUT;
646
647 /**
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700648 * If we don't receive a callback from AnyMotion in this amount of time +
649 * {@link #LOCATING_TIMEOUT}, we will change from
Adam Lesinski31c05d12015-06-09 17:34:04 -0700650 * STATE_SENSING to STATE_INACTIVE, and any AnyMotion callbacks while not in STATE_SENSING
651 * will be ignored.
652 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
653 * @see #KEY_SENSING_TIMEOUT
654 */
655 public long SENSING_TIMEOUT;
656
657 /**
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700658 * This is how long we will wait to try to get a good location fix before going in to
659 * idle mode.
660 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
661 * @see #KEY_LOCATING_TIMEOUT
662 */
663 public long LOCATING_TIMEOUT;
664
665 /**
666 * The desired maximum accuracy (in meters) we consider the location to be good enough to go
667 * on to idle. We will be trying to get an accuracy fix at least this good or until
668 * {@link #LOCATING_TIMEOUT} expires.
669 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
670 * @see #KEY_LOCATION_ACCURACY
671 */
672 public float LOCATION_ACCURACY;
673
674 /**
Adam Lesinski31c05d12015-06-09 17:34:04 -0700675 * This is the time, after seeing motion, that we wait after becoming inactive from
676 * that until we start looking for motion again.
677 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
678 * @see #KEY_MOTION_INACTIVE_TIMEOUT
679 */
680 public long MOTION_INACTIVE_TIMEOUT;
681
682 /**
683 * This is the time, after the inactive timeout elapses, that we will wait looking
Nick Vaccaro20feaea2015-09-17 17:22:44 -0700684 * for motion until we truly consider the device to be idle.
Adam Lesinski31c05d12015-06-09 17:34:04 -0700685 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
686 * @see #KEY_IDLE_AFTER_INACTIVE_TIMEOUT
687 */
688 public long IDLE_AFTER_INACTIVE_TIMEOUT;
689
690 /**
691 * This is the initial time, after being idle, that we will allow ourself to be back
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700692 * in the IDLE_MAINTENANCE state allowing the system to run normally until we return to
693 * idle.
Adam Lesinski31c05d12015-06-09 17:34:04 -0700694 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
695 * @see #KEY_IDLE_PENDING_TIMEOUT
696 */
697 public long IDLE_PENDING_TIMEOUT;
698
699 /**
700 * Maximum pending idle timeout (time spent running) we will be allowed to use.
701 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
702 * @see #KEY_MAX_IDLE_PENDING_TIMEOUT
703 */
704 public long MAX_IDLE_PENDING_TIMEOUT;
705
706 /**
707 * Scaling factor to apply to current pending idle timeout each time we cycle through
708 * that state.
709 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
710 * @see #KEY_IDLE_PENDING_FACTOR
711 */
712 public float IDLE_PENDING_FACTOR;
713
714 /**
715 * This is the initial time that we want to sit in the idle state before waking up
716 * again to return to pending idle and allowing normal work to run.
717 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
718 * @see #KEY_IDLE_TIMEOUT
719 */
720 public long IDLE_TIMEOUT;
721
722 /**
723 * Maximum idle duration we will be allowed to use.
724 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
725 * @see #KEY_MAX_IDLE_TIMEOUT
726 */
727 public long MAX_IDLE_TIMEOUT;
728
729 /**
730 * Scaling factor to apply to current idle timeout each time we cycle through that state.
731 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
732 * @see #KEY_IDLE_FACTOR
733 */
734 public float IDLE_FACTOR;
735
736 /**
737 * This is the minimum time we will allow until the next upcoming alarm for us to
738 * actually go in to idle mode.
739 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
740 * @see #KEY_MIN_TIME_TO_ALARM
741 */
742 public long MIN_TIME_TO_ALARM;
743
744 /**
745 * Max amount of time to temporarily whitelist an app when it receives a high priority
746 * tickle.
747 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
748 * @see #KEY_MAX_TEMP_APP_WHITELIST_DURATION
749 */
750 public long MAX_TEMP_APP_WHITELIST_DURATION;
751
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700752 /**
753 * Amount of time we would like to whitelist an app that is receiving an MMS.
754 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
755 * @see #KEY_MMS_TEMP_APP_WHITELIST_DURATION
756 */
757 public long MMS_TEMP_APP_WHITELIST_DURATION;
758
Dianne Hackborn451c3462015-07-21 17:39:46 -0700759 /**
760 * Amount of time we would like to whitelist an app that is receiving an SMS.
761 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
762 * @see #KEY_SMS_TEMP_APP_WHITELIST_DURATION
763 */
764 public long SMS_TEMP_APP_WHITELIST_DURATION;
765
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700766 /**
767 * Amount of time we would like to whitelist an app that is handling a
768 * {@link android.app.PendingIntent} triggered by a {@link android.app.Notification}.
769 * @see Settings.Global#DEVICE_IDLE_CONSTANTS
770 * @see #KEY_NOTIFICATION_WHITELIST_DURATION
771 */
772 public long NOTIFICATION_WHITELIST_DURATION;
773
Adam Lesinski31c05d12015-06-09 17:34:04 -0700774 private final ContentResolver mResolver;
Joe LaPennaf33b5bf2016-03-23 15:19:47 -0700775 private final boolean mHasWatch;
Adam Lesinski31c05d12015-06-09 17:34:04 -0700776 private final KeyValueListParser mParser = new KeyValueListParser(',');
777
778 public Constants(Handler handler, ContentResolver resolver) {
779 super(handler);
780 mResolver = resolver;
Joe LaPennaf33b5bf2016-03-23 15:19:47 -0700781 mHasWatch = getContext().getPackageManager().hasSystemFeature(
782 PackageManager.FEATURE_WATCH);
783 mResolver.registerContentObserver(Settings.Global.getUriFor(
784 mHasWatch ? Settings.Global.DEVICE_IDLE_CONSTANTS_WATCH
785 : Settings.Global.DEVICE_IDLE_CONSTANTS),
786 false, this);
Adam Lesinski31c05d12015-06-09 17:34:04 -0700787 updateConstants();
788 }
789
790 @Override
791 public void onChange(boolean selfChange, Uri uri) {
792 updateConstants();
793 }
794
795 private void updateConstants() {
796 synchronized (DeviceIdleController.this) {
797 try {
798 mParser.setString(Settings.Global.getString(mResolver,
Joe LaPennaf33b5bf2016-03-23 15:19:47 -0700799 mHasWatch ? Settings.Global.DEVICE_IDLE_CONSTANTS_WATCH
800 : Settings.Global.DEVICE_IDLE_CONSTANTS));
Adam Lesinski31c05d12015-06-09 17:34:04 -0700801 } catch (IllegalArgumentException e) {
802 // Failed to parse the settings string, log this and move on
803 // with defaults.
804 Slog.e(TAG, "Bad device idle settings", e);
805 }
806
Dianne Hackborn953fc942016-03-29 15:36:24 -0700807 LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT = mParser.getLong(
808 KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT,
809 !COMPRESS_TIME ? 5 * 60 * 1000L : 15 * 1000L);
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700810 LIGHT_PRE_IDLE_TIMEOUT = mParser.getLong(KEY_LIGHT_PRE_IDLE_TIMEOUT,
811 !COMPRESS_TIME ? 10 * 60 * 1000L : 30 * 1000L);
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700812 LIGHT_IDLE_TIMEOUT = mParser.getLong(KEY_LIGHT_IDLE_TIMEOUT,
Dianne Hackborn953fc942016-03-29 15:36:24 -0700813 !COMPRESS_TIME ? 5 * 60 * 1000L : 15 * 1000L);
814 LIGHT_IDLE_FACTOR = mParser.getFloat(KEY_LIGHT_IDLE_FACTOR,
815 2f);
816 LIGHT_MAX_IDLE_TIMEOUT = mParser.getLong(KEY_LIGHT_MAX_IDLE_TIMEOUT,
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700817 !COMPRESS_TIME ? 15 * 60 * 1000L : 60 * 1000L);
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800818 LIGHT_IDLE_MAINTENANCE_MIN_BUDGET = mParser.getLong(
819 KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET,
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700820 !COMPRESS_TIME ? 1 * 60 * 1000L : 15 * 1000L);
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800821 LIGHT_IDLE_MAINTENANCE_MAX_BUDGET = mParser.getLong(
822 KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET,
823 !COMPRESS_TIME ? 5 * 60 * 1000L : 30 * 1000L);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700824 MIN_LIGHT_MAINTENANCE_TIME = mParser.getLong(
825 KEY_MIN_LIGHT_MAINTENANCE_TIME,
826 !COMPRESS_TIME ? 5 * 1000L : 1 * 1000L);
827 MIN_DEEP_MAINTENANCE_TIME = mParser.getLong(
828 KEY_MIN_DEEP_MAINTENANCE_TIME,
829 !COMPRESS_TIME ? 30 * 1000L : 5 * 1000L);
Joe LaPenna0c5d3e92016-03-24 13:29:56 -0700830 long inactiveTimeoutDefault = (mHasWatch ? 15 : 30) * 60 * 1000L;
Adam Lesinski31c05d12015-06-09 17:34:04 -0700831 INACTIVE_TIMEOUT = mParser.getLong(KEY_INACTIVE_TIMEOUT,
Joe LaPenna0c5d3e92016-03-24 13:29:56 -0700832 !COMPRESS_TIME ? inactiveTimeoutDefault : (inactiveTimeoutDefault / 10));
Adam Lesinski31c05d12015-06-09 17:34:04 -0700833 SENSING_TIMEOUT = mParser.getLong(KEY_SENSING_TIMEOUT,
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700834 !DEBUG ? 4 * 60 * 1000L : 60 * 1000L);
835 LOCATING_TIMEOUT = mParser.getLong(KEY_LOCATING_TIMEOUT,
836 !DEBUG ? 30 * 1000L : 15 * 1000L);
837 LOCATION_ACCURACY = mParser.getFloat(KEY_LOCATION_ACCURACY, 20);
Adam Lesinski31c05d12015-06-09 17:34:04 -0700838 MOTION_INACTIVE_TIMEOUT = mParser.getLong(KEY_MOTION_INACTIVE_TIMEOUT,
839 !COMPRESS_TIME ? 10 * 60 * 1000L : 60 * 1000L);
Joe LaPenna0c5d3e92016-03-24 13:29:56 -0700840 long idleAfterInactiveTimeout = (mHasWatch ? 15 : 30) * 60 * 1000L;
Adam Lesinski31c05d12015-06-09 17:34:04 -0700841 IDLE_AFTER_INACTIVE_TIMEOUT = mParser.getLong(KEY_IDLE_AFTER_INACTIVE_TIMEOUT,
Joe LaPenna0c5d3e92016-03-24 13:29:56 -0700842 !COMPRESS_TIME ? idleAfterInactiveTimeout
843 : (idleAfterInactiveTimeout / 10));
Adam Lesinski31c05d12015-06-09 17:34:04 -0700844 IDLE_PENDING_TIMEOUT = mParser.getLong(KEY_IDLE_PENDING_TIMEOUT,
845 !COMPRESS_TIME ? 5 * 60 * 1000L : 30 * 1000L);
846 MAX_IDLE_PENDING_TIMEOUT = mParser.getLong(KEY_MAX_IDLE_PENDING_TIMEOUT,
847 !COMPRESS_TIME ? 10 * 60 * 1000L : 60 * 1000L);
848 IDLE_PENDING_FACTOR = mParser.getFloat(KEY_IDLE_PENDING_FACTOR,
849 2f);
850 IDLE_TIMEOUT = mParser.getLong(KEY_IDLE_TIMEOUT,
851 !COMPRESS_TIME ? 60 * 60 * 1000L : 6 * 60 * 1000L);
852 MAX_IDLE_TIMEOUT = mParser.getLong(KEY_MAX_IDLE_TIMEOUT,
853 !COMPRESS_TIME ? 6 * 60 * 60 * 1000L : 30 * 60 * 1000L);
854 IDLE_FACTOR = mParser.getFloat(KEY_IDLE_FACTOR,
855 2f);
856 MIN_TIME_TO_ALARM = mParser.getLong(KEY_MIN_TIME_TO_ALARM,
857 !COMPRESS_TIME ? 60 * 60 * 1000L : 6 * 60 * 1000L);
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700858 MAX_TEMP_APP_WHITELIST_DURATION = mParser.getLong(
859 KEY_MAX_TEMP_APP_WHITELIST_DURATION, 5 * 60 * 1000L);
860 MMS_TEMP_APP_WHITELIST_DURATION = mParser.getLong(
Dianne Hackborn0b6134b2015-07-14 18:48:07 -0700861 KEY_MMS_TEMP_APP_WHITELIST_DURATION, 60 * 1000L);
Dianne Hackborn451c3462015-07-21 17:39:46 -0700862 SMS_TEMP_APP_WHITELIST_DURATION = mParser.getLong(
863 KEY_SMS_TEMP_APP_WHITELIST_DURATION, 20 * 1000L);
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700864 NOTIFICATION_WHITELIST_DURATION = mParser.getLong(
865 KEY_NOTIFICATION_WHITELIST_DURATION, 30 * 1000L);
Adam Lesinski31c05d12015-06-09 17:34:04 -0700866 }
867 }
868
869 void dump(PrintWriter pw) {
870 pw.println(" Settings:");
871
Dianne Hackborn953fc942016-03-29 15:36:24 -0700872 pw.print(" "); pw.print(KEY_LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT); pw.print("=");
873 TimeUtils.formatDuration(LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT, pw);
874 pw.println();
875
Dianne Hackborn945c9c92016-03-30 14:55:00 -0700876 pw.print(" "); pw.print(KEY_LIGHT_PRE_IDLE_TIMEOUT); pw.print("=");
877 TimeUtils.formatDuration(LIGHT_PRE_IDLE_TIMEOUT, pw);
878 pw.println();
879
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700880 pw.print(" "); pw.print(KEY_LIGHT_IDLE_TIMEOUT); pw.print("=");
881 TimeUtils.formatDuration(LIGHT_IDLE_TIMEOUT, pw);
882 pw.println();
883
Dianne Hackborn953fc942016-03-29 15:36:24 -0700884 pw.print(" "); pw.print(KEY_LIGHT_IDLE_FACTOR); pw.print("=");
885 pw.print(LIGHT_IDLE_FACTOR);
886 pw.println();
887
888 pw.print(" "); pw.print(KEY_LIGHT_MAX_IDLE_TIMEOUT); pw.print("=");
889 TimeUtils.formatDuration(LIGHT_MAX_IDLE_TIMEOUT, pw);
890 pw.println();
891
Dianne Hackborn8ed2b972015-11-18 14:52:04 -0800892 pw.print(" "); pw.print(KEY_LIGHT_IDLE_MAINTENANCE_MIN_BUDGET); pw.print("=");
893 TimeUtils.formatDuration(LIGHT_IDLE_MAINTENANCE_MIN_BUDGET, pw);
894 pw.println();
895
896 pw.print(" "); pw.print(KEY_LIGHT_IDLE_MAINTENANCE_MAX_BUDGET); pw.print("=");
897 TimeUtils.formatDuration(LIGHT_IDLE_MAINTENANCE_MAX_BUDGET, pw);
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700898 pw.println();
899
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -0700900 pw.print(" "); pw.print(KEY_MIN_LIGHT_MAINTENANCE_TIME); pw.print("=");
901 TimeUtils.formatDuration(MIN_LIGHT_MAINTENANCE_TIME, pw);
902 pw.println();
903
904 pw.print(" "); pw.print(KEY_MIN_DEEP_MAINTENANCE_TIME); pw.print("=");
905 TimeUtils.formatDuration(MIN_DEEP_MAINTENANCE_TIME, pw);
906 pw.println();
907
Dianne Hackborna750a632015-06-16 17:18:23 -0700908 pw.print(" "); pw.print(KEY_INACTIVE_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700909 TimeUtils.formatDuration(INACTIVE_TIMEOUT, pw);
910 pw.println();
911
Dianne Hackborna750a632015-06-16 17:18:23 -0700912 pw.print(" "); pw.print(KEY_SENSING_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700913 TimeUtils.formatDuration(SENSING_TIMEOUT, pw);
914 pw.println();
915
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700916 pw.print(" "); pw.print(KEY_LOCATING_TIMEOUT); pw.print("=");
917 TimeUtils.formatDuration(LOCATING_TIMEOUT, pw);
918 pw.println();
919
920 pw.print(" "); pw.print(KEY_LOCATION_ACCURACY); pw.print("=");
921 pw.print(LOCATION_ACCURACY); pw.print("m");
922 pw.println();
923
Dianne Hackborna750a632015-06-16 17:18:23 -0700924 pw.print(" "); pw.print(KEY_MOTION_INACTIVE_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700925 TimeUtils.formatDuration(MOTION_INACTIVE_TIMEOUT, pw);
926 pw.println();
927
Dianne Hackborna750a632015-06-16 17:18:23 -0700928 pw.print(" "); pw.print(KEY_IDLE_AFTER_INACTIVE_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700929 TimeUtils.formatDuration(IDLE_AFTER_INACTIVE_TIMEOUT, pw);
930 pw.println();
931
Dianne Hackborna750a632015-06-16 17:18:23 -0700932 pw.print(" "); pw.print(KEY_IDLE_PENDING_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700933 TimeUtils.formatDuration(IDLE_PENDING_TIMEOUT, pw);
934 pw.println();
935
Dianne Hackborna750a632015-06-16 17:18:23 -0700936 pw.print(" "); pw.print(KEY_MAX_IDLE_PENDING_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700937 TimeUtils.formatDuration(MAX_IDLE_PENDING_TIMEOUT, pw);
938 pw.println();
939
Dianne Hackborna750a632015-06-16 17:18:23 -0700940 pw.print(" "); pw.print(KEY_IDLE_PENDING_FACTOR); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700941 pw.println(IDLE_PENDING_FACTOR);
942
Dianne Hackborna750a632015-06-16 17:18:23 -0700943 pw.print(" "); pw.print(KEY_IDLE_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700944 TimeUtils.formatDuration(IDLE_TIMEOUT, pw);
945 pw.println();
946
Dianne Hackborna750a632015-06-16 17:18:23 -0700947 pw.print(" "); pw.print(KEY_MAX_IDLE_TIMEOUT); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700948 TimeUtils.formatDuration(MAX_IDLE_TIMEOUT, pw);
949 pw.println();
950
Dianne Hackborna750a632015-06-16 17:18:23 -0700951 pw.print(" "); pw.print(KEY_IDLE_FACTOR); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700952 pw.println(IDLE_FACTOR);
953
Dianne Hackborna750a632015-06-16 17:18:23 -0700954 pw.print(" "); pw.print(KEY_MIN_TIME_TO_ALARM); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700955 TimeUtils.formatDuration(MIN_TIME_TO_ALARM, pw);
956 pw.println();
957
Dianne Hackborna750a632015-06-16 17:18:23 -0700958 pw.print(" "); pw.print(KEY_MAX_TEMP_APP_WHITELIST_DURATION); pw.print("=");
Adam Lesinski31c05d12015-06-09 17:34:04 -0700959 TimeUtils.formatDuration(MAX_TEMP_APP_WHITELIST_DURATION, pw);
960 pw.println();
Dianne Hackbornfd854ee2015-07-13 18:00:37 -0700961
962 pw.print(" "); pw.print(KEY_MMS_TEMP_APP_WHITELIST_DURATION); pw.print("=");
963 TimeUtils.formatDuration(MMS_TEMP_APP_WHITELIST_DURATION, pw);
964 pw.println();
Dianne Hackborn451c3462015-07-21 17:39:46 -0700965
966 pw.print(" "); pw.print(KEY_SMS_TEMP_APP_WHITELIST_DURATION); pw.print("=");
967 TimeUtils.formatDuration(SMS_TEMP_APP_WHITELIST_DURATION, pw);
968 pw.println();
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700969
970 pw.print(" "); pw.print(KEY_NOTIFICATION_WHITELIST_DURATION); pw.print("=");
971 TimeUtils.formatDuration(NOTIFICATION_WHITELIST_DURATION, pw);
972 pw.println();
Adam Lesinski31c05d12015-06-09 17:34:04 -0700973 }
974 }
975
976 private Constants mConstants;
977
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700978 @Override
979 public void onAnyMotionResult(int result) {
980 if (DEBUG) Slog.d(TAG, "onAnyMotionResult(" + result + ")");
Kevin Gabayan92f15e62016-04-04 17:52:22 -0700981 if (result != AnyMotionDetector.RESULT_UNKNOWN) {
982 synchronized (this) {
983 cancelSensingTimeoutAlarmLocked();
984 }
985 }
Kevin Gabayandcf47012016-07-08 10:41:24 -0700986 if ((result == AnyMotionDetector.RESULT_MOVED) ||
987 (result == AnyMotionDetector.RESULT_UNKNOWN)) {
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700988 synchronized (this) {
Kevin Gabayandcf47012016-07-08 10:41:24 -0700989 handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "non_stationary");
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700990 }
991 } else if (result == AnyMotionDetector.RESULT_STATIONARY) {
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700992 if (mState == STATE_SENSING) {
993 // If we are currently sensing, it is time to move to locating.
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700994 synchronized (this) {
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700995 mNotMoving = true;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800996 stepIdleStateLocked("s:stationary");
Kevin Gabayan89ecf822015-05-18 12:10:07 -0700997 }
Dianne Hackborn42df4fb2015-08-14 16:43:14 -0700998 } else if (mState == STATE_LOCATING) {
999 // If we are currently locating, note that we are not moving and step
1000 // if we have located the position.
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001001 synchronized (this) {
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001002 mNotMoving = true;
1003 if (mLocated) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001004 stepIdleStateLocked("s:stationary");
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001005 }
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001006 }
1007 }
1008 }
1009 }
1010
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001011 private static final int MSG_WRITE_CONFIG = 1;
1012 private static final int MSG_REPORT_IDLE_ON = 2;
1013 private static final int MSG_REPORT_IDLE_ON_LIGHT = 3;
1014 private static final int MSG_REPORT_IDLE_OFF = 4;
1015 private static final int MSG_REPORT_ACTIVE = 5;
1016 private static final int MSG_TEMP_APP_WHITELIST_TIMEOUT = 6;
1017 private static final int MSG_REPORT_MAINTENANCE_ACTIVITY = 7;
1018 private static final int MSG_FINISH_IDLE_OP = 8;
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -07001019 private static final int MSG_REPORT_TEMP_APP_WHITELIST_CHANGED = 9;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001020
1021 final class MyHandler extends Handler {
1022 MyHandler(Looper looper) {
1023 super(looper);
1024 }
1025
1026 @Override public void handleMessage(Message msg) {
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001027 if (DEBUG) Slog.d(TAG, "handleMessage(" + msg.what + ")");
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001028 switch (msg.what) {
1029 case MSG_WRITE_CONFIG: {
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001030 // Does not hold a wakelock. Just let this happen whenever.
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001031 handleWriteConfigFile();
1032 } break;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001033 case MSG_REPORT_IDLE_ON:
1034 case MSG_REPORT_IDLE_ON_LIGHT: {
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001035 // mGoingIdleWakeLock is held at this point
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001036 EventLogTags.writeDeviceIdleOnStart();
Dianne Hackbornb6843652016-02-22 12:20:13 -08001037 final boolean deepChanged;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001038 final boolean lightChanged;
1039 if (msg.what == MSG_REPORT_IDLE_ON) {
Dianne Hackbornb6843652016-02-22 12:20:13 -08001040 deepChanged = mLocalPowerManager.setDeviceIdleMode(true);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001041 lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false);
1042 } else {
Dianne Hackbornb6843652016-02-22 12:20:13 -08001043 deepChanged = mLocalPowerManager.setDeviceIdleMode(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001044 lightChanged = mLocalPowerManager.setLightDeviceIdleMode(true);
1045 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001046 try {
1047 mNetworkPolicyManager.setDeviceIdleMode(true);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001048 mBatteryStats.noteDeviceIdleMode(msg.what == MSG_REPORT_IDLE_ON
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07001049 ? BatteryStats.DEVICE_IDLE_MODE_DEEP
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001050 : BatteryStats.DEVICE_IDLE_MODE_LIGHT, null, Process.myUid());
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001051 } catch (RemoteException e) {
1052 }
Dianne Hackbornb6843652016-02-22 12:20:13 -08001053 if (deepChanged) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001054 getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL);
1055 }
1056 if (lightChanged) {
1057 getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL);
1058 }
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001059 EventLogTags.writeDeviceIdleOnComplete();
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001060 mGoingIdleWakeLock.release();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001061 } break;
1062 case MSG_REPORT_IDLE_OFF: {
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001063 // mActiveIdleWakeLock is held at this point
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001064 EventLogTags.writeDeviceIdleOffStart("unknown");
Dianne Hackbornb6843652016-02-22 12:20:13 -08001065 final boolean deepChanged = mLocalPowerManager.setDeviceIdleMode(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001066 final boolean lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001067 try {
1068 mNetworkPolicyManager.setDeviceIdleMode(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001069 mBatteryStats.noteDeviceIdleMode(BatteryStats.DEVICE_IDLE_MODE_OFF,
1070 null, Process.myUid());
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001071 } catch (RemoteException e) {
1072 }
Dianne Hackbornb6843652016-02-22 12:20:13 -08001073 if (deepChanged) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001074 incActiveIdleOps();
1075 getContext().sendOrderedBroadcastAsUser(mIdleIntent, UserHandle.ALL,
1076 null, mIdleStartedDoneReceiver, null, 0, null, null);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001077 }
1078 if (lightChanged) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001079 incActiveIdleOps();
1080 getContext().sendOrderedBroadcastAsUser(mLightIdleIntent, UserHandle.ALL,
1081 null, mIdleStartedDoneReceiver, null, 0, null, null);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001082 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001083 // Always start with one active op for the message being sent here.
Amith Yamasanicb926fc2016-03-14 17:15:20 -07001084 // Now we are done!
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001085 decActiveIdleOps();
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001086 EventLogTags.writeDeviceIdleOffComplete();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001087 } break;
1088 case MSG_REPORT_ACTIVE: {
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001089 // The device is awake at this point, so no wakelock necessary.
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001090 String activeReason = (String)msg.obj;
1091 int activeUid = msg.arg1;
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001092 EventLogTags.writeDeviceIdleOffStart(
1093 activeReason != null ? activeReason : "unknown");
Dianne Hackbornb6843652016-02-22 12:20:13 -08001094 final boolean deepChanged = mLocalPowerManager.setDeviceIdleMode(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001095 final boolean lightChanged = mLocalPowerManager.setLightDeviceIdleMode(false);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001096 try {
1097 mNetworkPolicyManager.setDeviceIdleMode(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001098 mBatteryStats.noteDeviceIdleMode(BatteryStats.DEVICE_IDLE_MODE_OFF,
1099 activeReason, activeUid);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001100 } catch (RemoteException e) {
1101 }
Dianne Hackbornb6843652016-02-22 12:20:13 -08001102 if (deepChanged) {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001103 getContext().sendBroadcastAsUser(mIdleIntent, UserHandle.ALL);
1104 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001105 if (lightChanged) {
1106 getContext().sendBroadcastAsUser(mLightIdleIntent, UserHandle.ALL);
1107 }
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001108 EventLogTags.writeDeviceIdleOffComplete();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001109 } break;
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001110 case MSG_TEMP_APP_WHITELIST_TIMEOUT: {
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001111 // TODO: What is keeping the device awake at this point? Does it need to be?
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001112 int uid = msg.arg1;
1113 checkTempAppWhitelistTimeout(uid);
1114 } break;
Yao Chenca5edbb2016-01-13 14:44:36 -08001115 case MSG_REPORT_MAINTENANCE_ACTIVITY: {
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001116 // TODO: What is keeping the device awake at this point? Does it need to be?
Yao Chenca5edbb2016-01-13 14:44:36 -08001117 boolean active = (msg.arg1 == 1);
1118 final int size = mMaintenanceActivityListeners.beginBroadcast();
1119 try {
1120 for (int i = 0; i < size; i++) {
1121 try {
1122 mMaintenanceActivityListeners.getBroadcastItem(i)
1123 .onMaintenanceActivityChanged(active);
1124 } catch (RemoteException ignored) {
1125 }
1126 }
1127 } finally {
1128 mMaintenanceActivityListeners.finishBroadcast();
1129 }
1130 } break;
Dianne Hackborn945c9c92016-03-30 14:55:00 -07001131 case MSG_FINISH_IDLE_OP: {
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001132 // mActiveIdleWakeLock is held at this point
Dianne Hackborn945c9c92016-03-30 14:55:00 -07001133 decActiveIdleOps();
1134 } break;
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -07001135 case MSG_REPORT_TEMP_APP_WHITELIST_CHANGED: {
1136 final int appId = msg.arg1;
1137 final boolean added = (msg.arg2 == 1);
1138 mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, added);
1139 } break;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001140 }
1141 }
1142 }
1143
1144 final MyHandler mHandler;
1145
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001146 BinderService mBinderService;
1147
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001148 private final class BinderService extends IDeviceIdleController.Stub {
1149 @Override public void addPowerSaveWhitelistApp(String name) {
Eugene Susla6a7006a2017-03-13 12:57:58 -07001150 if (DEBUG) {
1151 Slog.i(TAG, "addPowerSaveWhitelistApp(name = " + name + ")");
1152 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001153 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
1154 null);
Dianne Hackbornc9b2cd12016-01-20 10:54:29 -08001155 long ident = Binder.clearCallingIdentity();
1156 try {
1157 addPowerSaveWhitelistAppInternal(name);
1158 } finally {
1159 Binder.restoreCallingIdentity(ident);
1160 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001161 }
1162
1163 @Override public void removePowerSaveWhitelistApp(String name) {
Eugene Susla6a7006a2017-03-13 12:57:58 -07001164 if (DEBUG) {
1165 Slog.i(TAG, "removePowerSaveWhitelistApp(name = " + name + ")");
1166 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001167 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
1168 null);
Dianne Hackbornc9b2cd12016-01-20 10:54:29 -08001169 long ident = Binder.clearCallingIdentity();
1170 try {
1171 removePowerSaveWhitelistAppInternal(name);
1172 } finally {
1173 Binder.restoreCallingIdentity(ident);
1174 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001175 }
1176
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001177 @Override public String[] getSystemPowerWhitelistExceptIdle() {
1178 return getSystemPowerWhitelistExceptIdleInternal();
1179 }
1180
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001181 @Override public String[] getSystemPowerWhitelist() {
1182 return getSystemPowerWhitelistInternal();
1183 }
1184
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08001185 @Override public String[] getUserPowerWhitelist() {
1186 return getUserPowerWhitelistInternal();
1187 }
1188
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001189 @Override public String[] getFullPowerWhitelistExceptIdle() {
1190 return getFullPowerWhitelistExceptIdleInternal();
1191 }
1192
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001193 @Override public String[] getFullPowerWhitelist() {
1194 return getFullPowerWhitelistInternal();
1195 }
1196
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001197 @Override public int[] getAppIdWhitelistExceptIdle() {
1198 return getAppIdWhitelistExceptIdleInternal();
1199 }
1200
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001201 @Override public int[] getAppIdWhitelist() {
1202 return getAppIdWhitelistInternal();
1203 }
1204
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08001205 @Override public int[] getAppIdUserWhitelist() {
1206 return getAppIdUserWhitelistInternal();
1207 }
1208
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001209 @Override public int[] getAppIdTempWhitelist() {
1210 return getAppIdTempWhitelistInternal();
1211 }
1212
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001213 @Override public boolean isPowerSaveWhitelistExceptIdleApp(String name) {
1214 return isPowerSaveWhitelistExceptIdleAppInternal(name);
1215 }
1216
Amith Yamasani06bf8242015-05-08 16:36:21 -07001217 @Override public boolean isPowerSaveWhitelistApp(String name) {
1218 return isPowerSaveWhitelistAppInternal(name);
1219 }
1220
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001221 @Override public void addPowerSaveTempWhitelistApp(String packageName, long duration,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001222 int userId, String reason) throws RemoteException {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001223 addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason);
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001224 }
1225
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001226 @Override public long addPowerSaveTempWhitelistAppForMms(String packageName,
1227 int userId, String reason) throws RemoteException {
1228 long duration = mConstants.MMS_TEMP_APP_WHITELIST_DURATION;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001229 addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason);
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001230 return duration;
1231 }
1232
Dianne Hackborn451c3462015-07-21 17:39:46 -07001233 @Override public long addPowerSaveTempWhitelistAppForSms(String packageName,
1234 int userId, String reason) throws RemoteException {
1235 long duration = mConstants.SMS_TEMP_APP_WHITELIST_DURATION;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001236 addPowerSaveTempWhitelistAppChecked(packageName, duration, userId, reason);
Dianne Hackborn451c3462015-07-21 17:39:46 -07001237 return duration;
1238 }
1239
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001240 @Override public void exitIdle(String reason) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001241 getContext().enforceCallingOrSelfPermission(Manifest.permission.DEVICE_POWER,
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001242 null);
Dianne Hackbornc9b2cd12016-01-20 10:54:29 -08001243 long ident = Binder.clearCallingIdentity();
1244 try {
1245 exitIdleInternal(reason);
1246 } finally {
1247 Binder.restoreCallingIdentity(ident);
1248 }
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001249 }
1250
Yao Chenca5edbb2016-01-13 14:44:36 -08001251 @Override public boolean registerMaintenanceActivityListener(
1252 IMaintenanceActivityListener listener) {
1253 return DeviceIdleController.this.registerMaintenanceActivityListener(listener);
1254 }
1255
1256 @Override public void unregisterMaintenanceActivityListener(
1257 IMaintenanceActivityListener listener) {
1258 DeviceIdleController.this.unregisterMaintenanceActivityListener(listener);
1259 }
1260
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001261 @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1262 DeviceIdleController.this.dump(fd, pw, args);
1263 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001264
1265 @Override public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -07001266 FileDescriptor err, String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
1267 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001268 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001269 }
1270
Felipe Lemeef134662016-08-10 14:46:39 -07001271 public class LocalService {
Christopher Tatee0be7e82017-02-08 17:38:20 -08001272 // duration in milliseconds
1273 public void addPowerSaveTempWhitelistApp(int callingUid, String packageName,
1274 long duration, int userId, boolean sync, String reason) {
1275 addPowerSaveTempWhitelistAppInternal(callingUid, packageName, duration,
1276 userId, sync, reason);
1277 }
1278
1279 // duration in milliseconds
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001280 public void addPowerSaveTempWhitelistAppDirect(int appId, long duration, boolean sync,
1281 String reason) {
1282 addPowerSaveTempWhitelistAppDirectInternal(0, appId, duration, sync, reason);
1283 }
1284
Christopher Tatee0be7e82017-02-08 17:38:20 -08001285 // duration in milliseconds
Felipe Lemea1b79bf2016-05-24 13:06:54 -07001286 public long getNotificationWhitelistDuration() {
1287 return mConstants.NOTIFICATION_WHITELIST_DURATION;
1288 }
1289
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001290 public void setJobsActive(boolean active) {
1291 DeviceIdleController.this.setJobsActive(active);
1292 }
1293
1294 // Up-call from alarm manager.
1295 public void setAlarmsActive(boolean active) {
1296 DeviceIdleController.this.setAlarmsActive(active);
1297 }
Amith Yamasanicb926fc2016-03-14 17:15:20 -07001298
Christopher Tate42a386b2016-11-07 12:21:21 -08001299 /** Is the app on any of the power save whitelists, whether system or user? */
1300 public boolean isAppOnWhitelist(int appid) {
1301 return DeviceIdleController.this.isAppOnWhitelistInternal(appid);
1302 }
1303
Amith Yamasanicb926fc2016-03-14 17:15:20 -07001304 /**
1305 * Returns the array of app ids whitelisted by user. Take care not to
1306 * modify this, as it is a reference to the original copy. But the reference
1307 * can change when the list changes, so it needs to be re-acquired when
1308 * {@link PowerManager#ACTION_POWER_SAVE_WHITELIST_CHANGED} is sent.
1309 */
1310 public int[] getPowerSaveWhitelistUserAppIds() {
1311 return DeviceIdleController.this.getPowerSaveWhitelistUserAppIds();
1312 }
Dianne Hackborna750a632015-06-16 17:18:23 -07001313 }
1314
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001315 public DeviceIdleController(Context context) {
1316 super(context);
1317 mConfigFile = new AtomicFile(new File(getSystemDir(), "deviceidle.xml"));
1318 mHandler = new MyHandler(BackgroundThread.getHandler().getLooper());
1319 }
1320
Christopher Tate42a386b2016-11-07 12:21:21 -08001321 boolean isAppOnWhitelistInternal(int appid) {
1322 synchronized (this) {
1323 return Arrays.binarySearch(mPowerSaveWhitelistAllAppIdArray, appid) >= 0;
1324 }
1325 }
1326
Amith Yamasanicb926fc2016-03-14 17:15:20 -07001327 int[] getPowerSaveWhitelistUserAppIds() {
1328 synchronized (this) {
1329 return mPowerSaveWhitelistUserAppIdArray;
1330 }
1331 }
1332
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001333 private static File getSystemDir() {
1334 return new File(Environment.getDataDirectory(), "system");
1335 }
1336
1337 @Override
1338 public void onStart() {
1339 final PackageManager pm = getContext().getPackageManager();
1340
1341 synchronized (this) {
Dianne Hackbornb6843652016-02-22 12:20:13 -08001342 mLightEnabled = mDeepEnabled = getContext().getResources().getBoolean(
Dianne Hackborn92617032015-06-19 15:32:19 -07001343 com.android.internal.R.bool.config_enableAutoPowerModes);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001344 SystemConfig sysConfig = SystemConfig.getInstance();
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001345 ArraySet<String> allowPowerExceptIdle = sysConfig.getAllowInPowerSaveExceptIdle();
1346 for (int i=0; i<allowPowerExceptIdle.size(); i++) {
1347 String pkg = allowPowerExceptIdle.valueAt(i);
1348 try {
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001349 ApplicationInfo ai = pm.getApplicationInfo(pkg,
1350 PackageManager.MATCH_SYSTEM_ONLY);
1351 int appid = UserHandle.getAppId(ai.uid);
1352 mPowerSaveWhitelistAppsExceptIdle.put(ai.packageName, appid);
1353 mPowerSaveWhitelistSystemAppIdsExceptIdle.put(appid, true);
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001354 } catch (PackageManager.NameNotFoundException e) {
1355 }
1356 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001357 ArraySet<String> allowPower = sysConfig.getAllowInPowerSave();
1358 for (int i=0; i<allowPower.size(); i++) {
1359 String pkg = allowPower.valueAt(i);
1360 try {
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001361 ApplicationInfo ai = pm.getApplicationInfo(pkg,
1362 PackageManager.MATCH_SYSTEM_ONLY);
1363 int appid = UserHandle.getAppId(ai.uid);
1364 // These apps are on both the whitelist-except-idle as well
1365 // as the full whitelist, so they apply in all cases.
1366 mPowerSaveWhitelistAppsExceptIdle.put(ai.packageName, appid);
1367 mPowerSaveWhitelistSystemAppIdsExceptIdle.put(appid, true);
1368 mPowerSaveWhitelistApps.put(ai.packageName, appid);
1369 mPowerSaveWhitelistSystemAppIds.put(appid, true);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001370 } catch (PackageManager.NameNotFoundException e) {
1371 }
1372 }
1373
Adam Lesinski31c05d12015-06-09 17:34:04 -07001374 mConstants = new Constants(mHandler, getContext().getContentResolver());
1375
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001376 readConfigFileLocked();
1377 updateWhitelistAppIdsLocked();
1378
Dianne Hackborn88c41352016-04-07 15:18:58 -07001379 mNetworkConnected = true;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001380 mScreenOn = true;
1381 // Start out assuming we are charging. If we aren't, we will at least get
1382 // a battery update the next time the level drops.
1383 mCharging = true;
1384 mState = STATE_ACTIVE;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001385 mLightState = LIGHT_STATE_ACTIVE;
Adam Lesinski31c05d12015-06-09 17:34:04 -07001386 mInactiveTimeout = mConstants.INACTIVE_TIMEOUT;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001387 }
1388
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001389 mBinderService = new BinderService();
1390 publishBinderService(Context.DEVICE_IDLE_CONTROLLER, mBinderService);
Dianne Hackborna750a632015-06-16 17:18:23 -07001391 publishLocalService(LocalService.class, new LocalService());
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001392 }
1393
1394 @Override
1395 public void onBootPhase(int phase) {
1396 if (phase == PHASE_SYSTEM_SERVICES_READY) {
1397 synchronized (this) {
1398 mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
1399 mBatteryStats = BatteryStatsService.getService();
Dianne Hackborn85e35642017-01-12 15:10:57 -08001400 mLocalActivityManager = getLocalService(ActivityManagerInternal.class);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001401 mLocalPowerManager = getLocalService(PowerManagerInternal.class);
Dianne Hackborn945c9c92016-03-30 14:55:00 -07001402 mPowerManager = getContext().getSystemService(PowerManager.class);
1403 mActiveIdleWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
1404 "deviceidle_maint");
1405 mActiveIdleWakeLock.setReferenceCounted(false);
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001406 mGoingIdleWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
1407 "deviceidle_going_idle");
1408 mGoingIdleWakeLock.setReferenceCounted(true);
Dianne Hackborn88c41352016-04-07 15:18:58 -07001409 mConnectivityService = (ConnectivityService)ServiceManager.getService(
1410 Context.CONNECTIVITY_SERVICE);
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08001411 mLocalAlarmManager = getLocalService(AlarmManagerService.LocalService.class);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001412 mNetworkPolicyManager = INetworkPolicyManager.Stub.asInterface(
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001413 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -07001414 mNetworkPolicyManagerInternal = getLocalService(NetworkPolicyManagerInternal.class);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001415 mDisplayManager = (DisplayManager) getContext().getSystemService(
1416 Context.DISPLAY_SERVICE);
1417 mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
Joe LaPenna23d681b2015-08-27 15:12:11 -07001418 int sigMotionSensorId = getContext().getResources().getInteger(
1419 com.android.internal.R.integer.config_autoPowerModeAnyMotionSensor);
1420 if (sigMotionSensorId > 0) {
Nick Vaccaro20feaea2015-09-17 17:22:44 -07001421 mMotionSensor = mSensorManager.getDefaultSensor(sigMotionSensorId, true);
Joe LaPenna23d681b2015-08-27 15:12:11 -07001422 }
Nick Vaccaro20feaea2015-09-17 17:22:44 -07001423 if (mMotionSensor == null && getContext().getResources().getBoolean(
Joe LaPenna23d681b2015-08-27 15:12:11 -07001424 com.android.internal.R.bool.config_autoPowerModePreferWristTilt)) {
Nick Vaccaro20feaea2015-09-17 17:22:44 -07001425 mMotionSensor = mSensorManager.getDefaultSensor(
1426 Sensor.TYPE_WRIST_TILT_GESTURE, true);
Joe LaPenna23d681b2015-08-27 15:12:11 -07001427 }
Nick Vaccaro20feaea2015-09-17 17:22:44 -07001428 if (mMotionSensor == null) {
Joe LaPenna23d681b2015-08-27 15:12:11 -07001429 // As a last ditch, fall back to SMD.
Nick Vaccaro20feaea2015-09-17 17:22:44 -07001430 mMotionSensor = mSensorManager.getDefaultSensor(
1431 Sensor.TYPE_SIGNIFICANT_MOTION, true);
Joe LaPenna23d681b2015-08-27 15:12:11 -07001432 }
Nick Vaccaro20feaea2015-09-17 17:22:44 -07001433
Joe LaPenna23d681b2015-08-27 15:12:11 -07001434 if (getContext().getResources().getBoolean(
1435 com.android.internal.R.bool.config_autoPowerModePrefetchLocation)) {
1436 mLocationManager = (LocationManager) getContext().getSystemService(
1437 Context.LOCATION_SERVICE);
1438 mLocationRequest = new LocationRequest()
1439 .setQuality(LocationRequest.ACCURACY_FINE)
1440 .setInterval(0)
1441 .setFastestInterval(0)
1442 .setNumUpdates(1);
1443 }
1444
1445 float angleThreshold = getContext().getResources().getInteger(
1446 com.android.internal.R.integer.config_autoPowerModeThresholdAngle) / 100f;
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001447 mAnyMotionDetector = new AnyMotionDetector(
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001448 (PowerManager) getContext().getSystemService(Context.POWER_SERVICE),
Joe LaPenna23d681b2015-08-27 15:12:11 -07001449 mHandler, mSensorManager, this, angleThreshold);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001450
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001451 mIdleIntent = new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001452 mIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
1453 | Intent.FLAG_RECEIVER_FOREGROUND);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001454 mLightIdleIntent = new Intent(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED);
1455 mLightIdleIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
1456 | Intent.FLAG_RECEIVER_FOREGROUND);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001457
1458 IntentFilter filter = new IntentFilter();
1459 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001460 getContext().registerReceiver(mReceiver, filter);
Amith Yamasaniac59f75e2016-05-05 12:38:17 -07001461
Dianne Hackborn1b79ad72015-10-12 10:59:47 -07001462 filter = new IntentFilter();
1463 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
1464 filter.addDataScheme("package");
Amith Yamasaniac59f75e2016-05-05 12:38:17 -07001465 getContext().registerReceiver(mReceiver, filter);
1466
Dianne Hackborn88c41352016-04-07 15:18:58 -07001467 filter = new IntentFilter();
1468 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
Dianne Hackborn1b79ad72015-10-12 10:59:47 -07001469 getContext().registerReceiver(mReceiver, filter);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001470
Dianne Hackborn85e35642017-01-12 15:10:57 -08001471 mLocalActivityManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray);
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001472 mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray);
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08001473 mLocalAlarmManager.setDeviceIdleUserWhitelist(mPowerSaveWhitelistUserAppIdArray);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001474 mDisplayManager.registerDisplayListener(mDisplayListener, null);
1475 updateDisplayLocked();
1476 }
Dianne Hackborn4cb96ca2016-05-17 13:55:29 -07001477 updateConnectivityState(null);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001478 }
1479 }
1480
1481 public boolean addPowerSaveWhitelistAppInternal(String name) {
1482 synchronized (this) {
1483 try {
Dianne Hackborn1b79ad72015-10-12 10:59:47 -07001484 ApplicationInfo ai = getContext().getPackageManager().getApplicationInfo(name,
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07001485 PackageManager.MATCH_ANY_USER);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001486 if (mPowerSaveWhitelistUserApps.put(name, UserHandle.getAppId(ai.uid)) == null) {
1487 reportPowerSaveWhitelistChangedLocked();
1488 updateWhitelistAppIdsLocked();
1489 writeConfigFileLocked();
1490 }
1491 return true;
1492 } catch (PackageManager.NameNotFoundException e) {
1493 return false;
1494 }
1495 }
1496 }
1497
1498 public boolean removePowerSaveWhitelistAppInternal(String name) {
1499 synchronized (this) {
1500 if (mPowerSaveWhitelistUserApps.remove(name) != null) {
1501 reportPowerSaveWhitelistChangedLocked();
1502 updateWhitelistAppIdsLocked();
1503 writeConfigFileLocked();
1504 return true;
1505 }
1506 }
1507 return false;
1508 }
1509
Felipe Lemef8a46232016-02-10 13:51:54 -08001510 public boolean getPowerSaveWhitelistAppInternal(String name) {
1511 synchronized (this) {
1512 return mPowerSaveWhitelistUserApps.containsKey(name);
1513 }
1514 }
1515
Sudheer Shanka3f4d7702017-04-28 17:38:03 -07001516 public boolean addPowerSaveWhitelistExceptIdleInternal(String name) {
1517 synchronized (this) {
1518 try {
1519 final ApplicationInfo ai = getContext().getPackageManager().getApplicationInfo(name,
1520 PackageManager.MATCH_ANY_USER);
1521 if (mPowerSaveWhitelistAppsExceptIdle.put(name, UserHandle.getAppId(ai.uid))
1522 == null) {
1523 mPowerSaveWhitelistUserAppsExceptIdle.add(name);
1524 reportPowerSaveWhitelistChangedLocked();
1525 mPowerSaveWhitelistExceptIdleAppIdArray = buildAppIdArray(
1526 mPowerSaveWhitelistAppsExceptIdle, mPowerSaveWhitelistUserApps,
1527 mPowerSaveWhitelistExceptIdleAppIds);
1528 }
1529 return true;
1530 } catch (PackageManager.NameNotFoundException e) {
1531 return false;
1532 }
1533 }
1534 }
1535
1536 public void resetPowerSaveWhitelistExceptIdleInternal() {
1537 synchronized (this) {
1538 if (mPowerSaveWhitelistAppsExceptIdle.removeAll(
1539 mPowerSaveWhitelistUserAppsExceptIdle)) {
1540 reportPowerSaveWhitelistChangedLocked();
1541 mPowerSaveWhitelistExceptIdleAppIdArray = buildAppIdArray(
1542 mPowerSaveWhitelistAppsExceptIdle, mPowerSaveWhitelistUserApps,
1543 mPowerSaveWhitelistExceptIdleAppIds);
1544 mPowerSaveWhitelistUserAppsExceptIdle.clear();
1545 }
1546 }
1547 }
1548
1549 public boolean getPowerSaveWhitelistExceptIdleInternal(String name) {
1550 synchronized (this) {
1551 return mPowerSaveWhitelistAppsExceptIdle.containsKey(name);
1552 }
1553 }
1554
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001555 public String[] getSystemPowerWhitelistExceptIdleInternal() {
1556 synchronized (this) {
1557 int size = mPowerSaveWhitelistAppsExceptIdle.size();
1558 String[] apps = new String[size];
1559 for (int i = 0; i < size; i++) {
1560 apps[i] = mPowerSaveWhitelistAppsExceptIdle.keyAt(i);
1561 }
1562 return apps;
1563 }
1564 }
1565
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001566 public String[] getSystemPowerWhitelistInternal() {
1567 synchronized (this) {
1568 int size = mPowerSaveWhitelistApps.size();
1569 String[] apps = new String[size];
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001570 for (int i = 0; i < size; i++) {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001571 apps[i] = mPowerSaveWhitelistApps.keyAt(i);
1572 }
1573 return apps;
1574 }
1575 }
1576
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08001577 public String[] getUserPowerWhitelistInternal() {
1578 synchronized (this) {
1579 int size = mPowerSaveWhitelistUserApps.size();
1580 String[] apps = new String[size];
1581 for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) {
1582 apps[i] = mPowerSaveWhitelistUserApps.keyAt(i);
1583 }
1584 return apps;
1585 }
1586 }
1587
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001588 public String[] getFullPowerWhitelistExceptIdleInternal() {
1589 synchronized (this) {
1590 int size = mPowerSaveWhitelistAppsExceptIdle.size() + mPowerSaveWhitelistUserApps.size();
1591 String[] apps = new String[size];
1592 int cur = 0;
1593 for (int i = 0; i < mPowerSaveWhitelistAppsExceptIdle.size(); i++) {
1594 apps[cur] = mPowerSaveWhitelistAppsExceptIdle.keyAt(i);
1595 cur++;
1596 }
1597 for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) {
1598 apps[cur] = mPowerSaveWhitelistUserApps.keyAt(i);
1599 cur++;
1600 }
1601 return apps;
1602 }
1603 }
1604
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001605 public String[] getFullPowerWhitelistInternal() {
1606 synchronized (this) {
1607 int size = mPowerSaveWhitelistApps.size() + mPowerSaveWhitelistUserApps.size();
1608 String[] apps = new String[size];
1609 int cur = 0;
1610 for (int i = 0; i < mPowerSaveWhitelistApps.size(); i++) {
1611 apps[cur] = mPowerSaveWhitelistApps.keyAt(i);
1612 cur++;
1613 }
1614 for (int i = 0; i < mPowerSaveWhitelistUserApps.size(); i++) {
1615 apps[cur] = mPowerSaveWhitelistUserApps.keyAt(i);
1616 cur++;
1617 }
1618 return apps;
1619 }
1620 }
1621
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001622 public boolean isPowerSaveWhitelistExceptIdleAppInternal(String packageName) {
1623 synchronized (this) {
1624 return mPowerSaveWhitelistAppsExceptIdle.containsKey(packageName)
1625 || mPowerSaveWhitelistUserApps.containsKey(packageName);
1626 }
1627 }
1628
Amith Yamasani06bf8242015-05-08 16:36:21 -07001629 public boolean isPowerSaveWhitelistAppInternal(String packageName) {
1630 synchronized (this) {
1631 return mPowerSaveWhitelistApps.containsKey(packageName)
1632 || mPowerSaveWhitelistUserApps.containsKey(packageName);
1633 }
1634 }
1635
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001636 public int[] getAppIdWhitelistExceptIdleInternal() {
1637 synchronized (this) {
1638 return mPowerSaveWhitelistExceptIdleAppIdArray;
1639 }
1640 }
1641
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001642 public int[] getAppIdWhitelistInternal() {
1643 synchronized (this) {
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001644 return mPowerSaveWhitelistAllAppIdArray;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001645 }
1646 }
1647
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08001648 public int[] getAppIdUserWhitelistInternal() {
1649 synchronized (this) {
1650 return mPowerSaveWhitelistUserAppIdArray;
1651 }
1652 }
1653
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001654 public int[] getAppIdTempWhitelistInternal() {
1655 synchronized (this) {
1656 return mTempWhitelistAppIdArray;
1657 }
1658 }
1659
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001660 void addPowerSaveTempWhitelistAppChecked(String packageName, long duration,
1661 int userId, String reason) throws RemoteException {
1662 getContext().enforceCallingPermission(
1663 Manifest.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST,
1664 "No permission to change device idle whitelist");
1665 final int callingUid = Binder.getCallingUid();
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001666 userId = ActivityManager.getService().handleIncomingUser(
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001667 Binder.getCallingPid(),
1668 callingUid,
1669 userId,
1670 /*allowAll=*/ false,
1671 /*requireFull=*/ false,
1672 "addPowerSaveTempWhitelistApp", null);
1673 final long token = Binder.clearCallingIdentity();
1674 try {
1675 addPowerSaveTempWhitelistAppInternal(callingUid,
1676 packageName, duration, userId, true, reason);
1677 } finally {
1678 Binder.restoreCallingIdentity(token);
1679 }
1680 }
1681
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001682 /**
1683 * Adds an app to the temporary whitelist and resets the endTime for granting the
1684 * app an exemption to access network and acquire wakelocks.
1685 */
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001686 void addPowerSaveTempWhitelistAppInternal(int callingUid, String packageName,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001687 long duration, int userId, boolean sync, String reason) {
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001688 try {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001689 int uid = getContext().getPackageManager().getPackageUidAsUser(packageName, userId);
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001690 int appId = UserHandle.getAppId(uid);
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001691 addPowerSaveTempWhitelistAppDirectInternal(callingUid, appId, duration, sync, reason);
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001692 } catch (NameNotFoundException e) {
1693 }
1694 }
1695
Dianne Hackborna750a632015-06-16 17:18:23 -07001696 /**
1697 * Adds an app to the temporary whitelist and resets the endTime for granting the
1698 * app an exemption to access network and acquire wakelocks.
1699 */
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07001700 void addPowerSaveTempWhitelistAppDirectInternal(int callingUid, int appId,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001701 long duration, boolean sync, String reason) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001702 final long timeNow = SystemClock.elapsedRealtime();
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -07001703 boolean informWhitelistChanged = false;
Dianne Hackborna750a632015-06-16 17:18:23 -07001704 synchronized (this) {
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07001705 int callingAppId = UserHandle.getAppId(callingUid);
1706 if (callingAppId >= Process.FIRST_APPLICATION_UID) {
1707 if (!mPowerSaveWhitelistSystemAppIds.get(callingAppId)) {
1708 throw new SecurityException("Calling app " + UserHandle.formatUid(callingUid)
1709 + " is not on whitelist");
1710 }
1711 }
Dianne Hackborna750a632015-06-16 17:18:23 -07001712 duration = Math.min(duration, mConstants.MAX_TEMP_APP_WHITELIST_DURATION);
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001713 Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.get(appId);
1714 final boolean newEntry = entry == null;
Dianne Hackborna750a632015-06-16 17:18:23 -07001715 // Set the new end time
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001716 if (newEntry) {
1717 entry = new Pair<>(new MutableLong(0), reason);
1718 mTempWhitelistAppIdEndTimes.put(appId, entry);
1719 }
1720 entry.first.value = timeNow + duration;
Dianne Hackborna750a632015-06-16 17:18:23 -07001721 if (DEBUG) {
Felipe Lemea1b79bf2016-05-24 13:06:54 -07001722 Slog.d(TAG, "Adding AppId " + appId + " to temp whitelist. New entry: " + newEntry);
Dianne Hackborna750a632015-06-16 17:18:23 -07001723 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001724 if (newEntry) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001725 // No pending timeout for the app id, post a delayed message
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001726 try {
1727 mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_START,
1728 reason, appId);
1729 } catch (RemoteException e) {
1730 }
Dianne Hackborna750a632015-06-16 17:18:23 -07001731 postTempActiveTimeoutMessage(appId, duration);
Dianne Hackborn85e35642017-01-12 15:10:57 -08001732 updateTempWhitelistAppIdsLocked(appId, true);
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -07001733 if (sync) {
1734 informWhitelistChanged = true;
1735 } else {
1736 mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_CHANGED, appId, 1)
1737 .sendToTarget();
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001738 }
Dianne Hackborna750a632015-06-16 17:18:23 -07001739 reportTempWhitelistChangedLocked();
1740 }
1741 }
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -07001742 if (informWhitelistChanged) {
1743 mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, true);
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001744 }
Dianne Hackborna750a632015-06-16 17:18:23 -07001745 }
1746
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001747 private void postTempActiveTimeoutMessage(int uid, long delay) {
Felipe Lemea1b79bf2016-05-24 13:06:54 -07001748 if (DEBUG) {
1749 Slog.d(TAG, "postTempActiveTimeoutMessage: uid=" + uid + ", delay=" + delay);
1750 }
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001751 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_TEMP_APP_WHITELIST_TIMEOUT, uid, 0),
1752 delay);
1753 }
1754
1755 void checkTempAppWhitelistTimeout(int uid) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001756 final long timeNow = SystemClock.elapsedRealtime();
Felipe Lemea1b79bf2016-05-24 13:06:54 -07001757 if (DEBUG) {
1758 Slog.d(TAG, "checkTempAppWhitelistTimeout: uid=" + uid + ", timeNow=" + timeNow);
1759 }
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001760 synchronized (this) {
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001761 Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.get(uid);
1762 if (entry == null) {
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001763 // Nothing to do
1764 return;
1765 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001766 if (timeNow >= entry.first.value) {
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001767 mTempWhitelistAppIdEndTimes.delete(uid);
1768 if (DEBUG) {
1769 Slog.d(TAG, "Removing UID " + uid + " from temp whitelist");
1770 }
Dianne Hackborn85e35642017-01-12 15:10:57 -08001771 updateTempWhitelistAppIdsLocked(uid, false);
Sudheer Shankaf34f3ec2017-08-03 11:02:56 -07001772 mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_CHANGED, uid, 0)
1773 .sendToTarget();
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001774 reportTempWhitelistChangedLocked();
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001775 try {
1776 mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_FINISH,
1777 entry.second, uid);
1778 } catch (RemoteException e) {
1779 }
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001780 } else {
1781 // Need more time
Felipe Lemea1b79bf2016-05-24 13:06:54 -07001782 if (DEBUG) {
1783 Slog.d(TAG, "Time to remove UID " + uid + ": " + entry.first.value);
1784 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001785 postTempActiveTimeoutMessage(uid, entry.first.value - timeNow);
Amith Yamasaniaf575b92015-05-29 15:35:26 -07001786 }
1787 }
1788 }
1789
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001790 public void exitIdleInternal(String reason) {
1791 synchronized (this) {
1792 becomeActiveLocked(reason, Binder.getCallingUid());
1793 }
1794 }
1795
Dianne Hackborn4cb96ca2016-05-17 13:55:29 -07001796 void updateConnectivityState(Intent connIntent) {
1797 ConnectivityService cm;
1798 synchronized (this) {
1799 cm = mConnectivityService;
1800 }
1801 if (cm == null) {
1802 return;
1803 }
1804 // Note: can't call out to ConnectivityService with our lock held.
1805 NetworkInfo ni = cm.getActiveNetworkInfo();
1806 synchronized (this) {
Dianne Hackborn88c41352016-04-07 15:18:58 -07001807 boolean conn;
1808 if (ni == null) {
1809 conn = false;
1810 } else {
1811 if (connIntent == null) {
1812 conn = ni.isConnected();
1813 } else {
1814 final int networkType =
1815 connIntent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE,
1816 ConnectivityManager.TYPE_NONE);
1817 if (ni.getType() != networkType) {
1818 return;
1819 }
1820 conn = !connIntent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,
1821 false);
1822 }
1823 }
1824 if (conn != mNetworkConnected) {
1825 mNetworkConnected = conn;
1826 if (conn && mLightState == LIGHT_STATE_WAITING_FOR_NETWORK) {
1827 stepLightIdleStateLocked("network");
1828 }
1829 }
1830 }
1831 }
1832
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001833 void updateDisplayLocked() {
1834 mCurDisplay = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
1835 // We consider any situation where the display is showing something to be it on,
1836 // because if there is anything shown we are going to be updating it at some
1837 // frequency so can't be allowed to go into deep sleeps.
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001838 boolean screenOn = mCurDisplay.getState() == Display.STATE_ON;
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001839 if (DEBUG) Slog.d(TAG, "updateDisplayLocked: screenOn=" + screenOn);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001840 if (!screenOn && mScreenOn) {
1841 mScreenOn = false;
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001842 if (!mForceIdle) {
1843 becomeInactiveIfAppropriateLocked();
1844 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001845 } else if (screenOn) {
1846 mScreenOn = true;
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001847 if (!mForceIdle) {
1848 becomeActiveLocked("screen", Process.myUid());
1849 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001850 }
1851 }
1852
1853 void updateChargingLocked(boolean charging) {
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001854 if (DEBUG) Slog.i(TAG, "updateChargingLocked: charging=" + charging);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001855 if (!charging && mCharging) {
1856 mCharging = false;
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001857 if (!mForceIdle) {
1858 becomeInactiveIfAppropriateLocked();
1859 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001860 } else if (charging) {
1861 mCharging = charging;
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001862 if (!mForceIdle) {
1863 becomeActiveLocked("charging", Process.myUid());
1864 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001865 }
1866 }
1867
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001868 void scheduleReportActiveLocked(String activeReason, int activeUid) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001869 Message msg = mHandler.obtainMessage(MSG_REPORT_ACTIVE, activeUid, 0, activeReason);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001870 mHandler.sendMessage(msg);
1871 }
1872
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001873 void becomeActiveLocked(String activeReason, int activeUid) {
1874 if (DEBUG) Slog.i(TAG, "becomeActiveLocked, reason = " + activeReason);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001875 if (mState != STATE_ACTIVE || mLightState != STATE_ACTIVE) {
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001876 EventLogTags.writeDeviceIdle(STATE_ACTIVE, activeReason);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001877 EventLogTags.writeDeviceIdleLight(LIGHT_STATE_ACTIVE, activeReason);
Dianne Hackbornb6683c42015-06-18 17:40:33 -07001878 scheduleReportActiveLocked(activeReason, activeUid);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001879 mState = STATE_ACTIVE;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001880 mLightState = LIGHT_STATE_ACTIVE;
Adam Lesinski31c05d12015-06-09 17:34:04 -07001881 mInactiveTimeout = mConstants.INACTIVE_TIMEOUT;
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08001882 mCurIdleBudget = 0;
1883 mMaintenanceStartTime = 0;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001884 resetIdleManagementLocked();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001885 resetLightIdleManagementLocked();
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08001886 addEvent(EVENT_NORMAL);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001887 }
1888 }
1889
1890 void becomeInactiveIfAppropriateLocked() {
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001891 if (DEBUG) Slog.d(TAG, "becomeInactiveIfAppropriateLocked()");
Dianne Hackbornb6843652016-02-22 12:20:13 -08001892 if ((!mScreenOn && !mCharging) || mForceIdle) {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001893 // Screen has turned off; we are now going to become inactive and start
1894 // waiting to see if we will ultimately go idle.
Dianne Hackbornb6843652016-02-22 12:20:13 -08001895 if (mState == STATE_ACTIVE && mDeepEnabled) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001896 mState = STATE_INACTIVE;
1897 if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE");
1898 resetIdleManagementLocked();
1899 scheduleAlarmLocked(mInactiveTimeout, false);
1900 EventLogTags.writeDeviceIdle(mState, "no activity");
1901 }
Dianne Hackbornb6843652016-02-22 12:20:13 -08001902 if (mLightState == LIGHT_STATE_ACTIVE && mLightEnabled) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001903 mLightState = LIGHT_STATE_INACTIVE;
1904 if (DEBUG) Slog.d(TAG, "Moved from LIGHT_STATE_ACTIVE to LIGHT_STATE_INACTIVE");
1905 resetLightIdleManagementLocked();
Dianne Hackborn953fc942016-03-29 15:36:24 -07001906 scheduleLightAlarmLocked(mConstants.LIGHT_IDLE_AFTER_INACTIVE_TIMEOUT);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001907 EventLogTags.writeDeviceIdleLight(mLightState, "no activity");
1908 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07001909 }
1910 }
1911
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001912 void resetIdleManagementLocked() {
1913 mNextIdlePendingDelay = 0;
1914 mNextIdleDelay = 0;
Dianne Hackborn953fc942016-03-29 15:36:24 -07001915 mNextLightIdleDelay = 0;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001916 cancelAlarmLocked();
Kevin Gabayan92f15e62016-04-04 17:52:22 -07001917 cancelSensingTimeoutAlarmLocked();
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001918 cancelLocatingLocked();
Nick Vaccaro20feaea2015-09-17 17:22:44 -07001919 stopMonitoringMotionLocked();
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07001920 mAnyMotionDetector.stop();
Kevin Gabayan89ecf822015-05-18 12:10:07 -07001921 }
1922
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001923 void resetLightIdleManagementLocked() {
1924 cancelLightAlarmLocked();
1925 }
1926
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001927 void exitForceIdleLocked() {
1928 if (mForceIdle) {
1929 mForceIdle = false;
1930 if (mScreenOn || mCharging) {
Dianne Hackborn88c41352016-04-07 15:18:58 -07001931 becomeActiveLocked("exit-force", Process.myUid());
Dianne Hackborn4a503b12015-08-06 22:19:06 -07001932 }
1933 }
1934 }
1935
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001936 void stepLightIdleStateLocked(String reason) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001937 if (mLightState == LIGHT_STATE_OVERRIDE) {
Dianne Hackbornb6843652016-02-22 12:20:13 -08001938 // If we are already in deep device idle mode, then
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001939 // there is nothing left to do for light mode.
1940 return;
1941 }
1942
1943 if (DEBUG) Slog.d(TAG, "stepLightIdleStateLocked: mLightState=" + mLightState);
1944 EventLogTags.writeDeviceIdleLightStep();
1945
1946 switch (mLightState) {
1947 case LIGHT_STATE_INACTIVE:
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08001948 mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET;
Dianne Hackborn953fc942016-03-29 15:36:24 -07001949 // Reset the upcoming idle delays.
1950 mNextLightIdleDelay = mConstants.LIGHT_IDLE_TIMEOUT;
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08001951 mMaintenanceStartTime = 0;
Dianne Hackborn945c9c92016-03-30 14:55:00 -07001952 if (!isOpsInactiveLocked()) {
1953 // We have some active ops going on... give them a chance to finish
1954 // before going in to our first idle.
1955 mLightState = LIGHT_STATE_PRE_IDLE;
1956 EventLogTags.writeDeviceIdleLight(mLightState, reason);
1957 scheduleLightAlarmLocked(mConstants.LIGHT_PRE_IDLE_TIMEOUT);
1958 break;
1959 }
1960 // Nothing active, fall through to immediately idle.
1961 case LIGHT_STATE_PRE_IDLE:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001962 case LIGHT_STATE_IDLE_MAINTENANCE:
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08001963 if (mMaintenanceStartTime != 0) {
1964 long duration = SystemClock.elapsedRealtime() - mMaintenanceStartTime;
1965 if (duration < mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET) {
1966 // We didn't use up all of our minimum budget; add this to the reserve.
1967 mCurIdleBudget += (mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET-duration);
1968 } else {
1969 // We used more than our minimum budget; this comes out of the reserve.
1970 mCurIdleBudget -= (duration-mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET);
1971 }
1972 }
1973 mMaintenanceStartTime = 0;
Dianne Hackborn953fc942016-03-29 15:36:24 -07001974 scheduleLightAlarmLocked(mNextLightIdleDelay);
1975 mNextLightIdleDelay = Math.min(mConstants.LIGHT_MAX_IDLE_TIMEOUT,
1976 (long)(mNextLightIdleDelay * mConstants.LIGHT_IDLE_FACTOR));
1977 if (mNextLightIdleDelay < mConstants.LIGHT_IDLE_TIMEOUT) {
1978 mNextLightIdleDelay = mConstants.LIGHT_IDLE_TIMEOUT;
1979 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001980 if (DEBUG) Slog.d(TAG, "Moved to LIGHT_STATE_IDLE.");
1981 mLightState = LIGHT_STATE_IDLE;
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001982 EventLogTags.writeDeviceIdleLight(mLightState, reason);
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08001983 addEvent(EVENT_LIGHT_IDLE);
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08001984 mGoingIdleWakeLock.acquire();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07001985 mHandler.sendEmptyMessage(MSG_REPORT_IDLE_ON_LIGHT);
1986 break;
1987 case LIGHT_STATE_IDLE:
Dianne Hackborn88c41352016-04-07 15:18:58 -07001988 case LIGHT_STATE_WAITING_FOR_NETWORK:
1989 if (mNetworkConnected || mLightState == LIGHT_STATE_WAITING_FOR_NETWORK) {
1990 // We have been idling long enough, now it is time to do some work.
1991 mActiveIdleOpCount = 1;
1992 mActiveIdleWakeLock.acquire();
1993 mMaintenanceStartTime = SystemClock.elapsedRealtime();
1994 if (mCurIdleBudget < mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET) {
1995 mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET;
1996 } else if (mCurIdleBudget > mConstants.LIGHT_IDLE_MAINTENANCE_MAX_BUDGET) {
1997 mCurIdleBudget = mConstants.LIGHT_IDLE_MAINTENANCE_MAX_BUDGET;
1998 }
1999 scheduleLightAlarmLocked(mCurIdleBudget);
2000 if (DEBUG) Slog.d(TAG,
2001 "Moved from LIGHT_STATE_IDLE to LIGHT_STATE_IDLE_MAINTENANCE.");
2002 mLightState = LIGHT_STATE_IDLE_MAINTENANCE;
2003 EventLogTags.writeDeviceIdleLight(mLightState, reason);
2004 addEvent(EVENT_LIGHT_MAINTENANCE);
2005 mHandler.sendEmptyMessage(MSG_REPORT_IDLE_OFF);
2006 } else {
2007 // We'd like to do maintenance, but currently don't have network
2008 // connectivity... let's try to wait until the network comes back.
2009 // We'll only wait for another full idle period, however, and then give up.
2010 scheduleLightAlarmLocked(mNextLightIdleDelay);
2011 if (DEBUG) Slog.d(TAG, "Moved to LIGHT_WAITING_FOR_NETWORK.");
2012 mLightState = LIGHT_STATE_WAITING_FOR_NETWORK;
2013 EventLogTags.writeDeviceIdleLight(mLightState, reason);
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08002014 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002015 break;
2016 }
2017 }
2018
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002019 void stepIdleStateLocked(String reason) {
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002020 if (DEBUG) Slog.d(TAG, "stepIdleStateLocked: mState=" + mState);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002021 EventLogTags.writeDeviceIdleStep();
2022
2023 final long now = SystemClock.elapsedRealtime();
Adam Lesinski31c05d12015-06-09 17:34:04 -07002024 if ((now+mConstants.MIN_TIME_TO_ALARM) > mAlarmManager.getNextWakeFromIdleTime()) {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002025 // Whoops, there is an upcoming alarm. We don't actually want to go idle.
2026 if (mState != STATE_ACTIVE) {
Dianne Hackbornb6683c42015-06-18 17:40:33 -07002027 becomeActiveLocked("alarm", Process.myUid());
Koji Fukui27b33302015-12-16 19:43:01 +09002028 becomeInactiveIfAppropriateLocked();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002029 }
2030 return;
2031 }
2032
2033 switch (mState) {
2034 case STATE_INACTIVE:
2035 // We have now been inactive long enough, it is time to start looking
Nick Vaccaro20feaea2015-09-17 17:22:44 -07002036 // for motion and sleep some more while doing so.
2037 startMonitoringMotionLocked();
Adam Lesinski31c05d12015-06-09 17:34:04 -07002038 scheduleAlarmLocked(mConstants.IDLE_AFTER_INACTIVE_TIMEOUT, false);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002039 // Reset the upcoming idle delays.
Adam Lesinski31c05d12015-06-09 17:34:04 -07002040 mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT;
2041 mNextIdleDelay = mConstants.IDLE_TIMEOUT;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002042 mState = STATE_IDLE_PENDING;
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002043 if (DEBUG) Slog.d(TAG, "Moved from STATE_INACTIVE to STATE_IDLE_PENDING.");
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002044 EventLogTags.writeDeviceIdle(mState, reason);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002045 break;
2046 case STATE_IDLE_PENDING:
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002047 mState = STATE_SENSING;
2048 if (DEBUG) Slog.d(TAG, "Moved from STATE_IDLE_PENDING to STATE_SENSING.");
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002049 EventLogTags.writeDeviceIdle(mState, reason);
Kevin Gabayan92f15e62016-04-04 17:52:22 -07002050 scheduleSensingTimeoutAlarmLocked(mConstants.SENSING_TIMEOUT);
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002051 cancelLocatingLocked();
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002052 mNotMoving = false;
2053 mLocated = false;
2054 mLastGenericLocation = null;
2055 mLastGpsLocation = null;
Kevin Gabayan92f15e62016-04-04 17:52:22 -07002056 mAnyMotionDetector.checkForAnyMotion();
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002057 break;
2058 case STATE_SENSING:
Kevin Gabayan92f15e62016-04-04 17:52:22 -07002059 cancelSensingTimeoutAlarmLocked();
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002060 mState = STATE_LOCATING;
2061 if (DEBUG) Slog.d(TAG, "Moved from STATE_SENSING to STATE_LOCATING.");
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002062 EventLogTags.writeDeviceIdle(mState, reason);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002063 scheduleAlarmLocked(mConstants.LOCATING_TIMEOUT, false);
Joe LaPenna23d681b2015-08-27 15:12:11 -07002064 if (mLocationManager != null
2065 && mLocationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
2066 mLocationManager.requestLocationUpdates(mLocationRequest,
2067 mGenericLocationListener, mHandler.getLooper());
2068 mLocating = true;
2069 } else {
2070 mHasNetworkLocation = false;
2071 }
2072 if (mLocationManager != null
2073 && mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
2074 mHasGps = true;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002075 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5,
2076 mGpsLocationListener, mHandler.getLooper());
Joe LaPenna23d681b2015-08-27 15:12:11 -07002077 mLocating = true;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002078 } else {
Joe LaPenna23d681b2015-08-27 15:12:11 -07002079 mHasGps = false;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002080 }
Joe LaPenna23d681b2015-08-27 15:12:11 -07002081 // If we have a location provider, we're all set, the listeners will move state
2082 // forward.
2083 if (mLocating) {
2084 break;
2085 }
2086
2087 // Otherwise, we have to move from locating into idle maintenance.
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002088 case STATE_LOCATING:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002089 cancelAlarmLocked();
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002090 cancelLocatingLocked();
2091 mAnyMotionDetector.stop();
Dianne Hackborn953fc942016-03-29 15:36:24 -07002092
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002093 case STATE_IDLE_MAINTENANCE:
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002094 scheduleAlarmLocked(mNextIdleDelay, true);
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002095 if (DEBUG) Slog.d(TAG, "Moved to STATE_IDLE. Next alarm in " + mNextIdleDelay +
2096 " ms.");
Adam Lesinski31c05d12015-06-09 17:34:04 -07002097 mNextIdleDelay = (long)(mNextIdleDelay * mConstants.IDLE_FACTOR);
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002098 if (DEBUG) Slog.d(TAG, "Setting mNextIdleDelay = " + mNextIdleDelay);
Adam Lesinski31c05d12015-06-09 17:34:04 -07002099 mNextIdleDelay = Math.min(mNextIdleDelay, mConstants.MAX_IDLE_TIMEOUT);
Dianne Hackborn953fc942016-03-29 15:36:24 -07002100 if (mNextIdleDelay < mConstants.IDLE_TIMEOUT) {
2101 mNextIdleDelay = mConstants.IDLE_TIMEOUT;
2102 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002103 mState = STATE_IDLE;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002104 if (mLightState != LIGHT_STATE_OVERRIDE) {
2105 mLightState = LIGHT_STATE_OVERRIDE;
2106 cancelLightAlarmLocked();
2107 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002108 EventLogTags.writeDeviceIdle(mState, reason);
Dianne Hackbornb6843652016-02-22 12:20:13 -08002109 addEvent(EVENT_DEEP_IDLE);
Joe Onorato8f0e9ced2016-12-08 17:48:49 -08002110 mGoingIdleWakeLock.acquire();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002111 mHandler.sendEmptyMessage(MSG_REPORT_IDLE_ON);
2112 break;
2113 case STATE_IDLE:
2114 // We have been idling long enough, now it is time to do some work.
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002115 mActiveIdleOpCount = 1;
Dianne Hackborn945c9c92016-03-30 14:55:00 -07002116 mActiveIdleWakeLock.acquire();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002117 scheduleAlarmLocked(mNextIdlePendingDelay, false);
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002118 if (DEBUG) Slog.d(TAG, "Moved from STATE_IDLE to STATE_IDLE_MAINTENANCE. " +
2119 "Next alarm in " + mNextIdlePendingDelay + " ms.");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002120 mMaintenanceStartTime = SystemClock.elapsedRealtime();
Adam Lesinski31c05d12015-06-09 17:34:04 -07002121 mNextIdlePendingDelay = Math.min(mConstants.MAX_IDLE_PENDING_TIMEOUT,
2122 (long)(mNextIdlePendingDelay * mConstants.IDLE_PENDING_FACTOR));
Dianne Hackborn953fc942016-03-29 15:36:24 -07002123 if (mNextIdlePendingDelay < mConstants.IDLE_PENDING_TIMEOUT) {
2124 mNextIdlePendingDelay = mConstants.IDLE_PENDING_TIMEOUT;
2125 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002126 mState = STATE_IDLE_MAINTENANCE;
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002127 EventLogTags.writeDeviceIdle(mState, reason);
Dianne Hackbornb6843652016-02-22 12:20:13 -08002128 addEvent(EVENT_DEEP_MAINTENANCE);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002129 mHandler.sendEmptyMessage(MSG_REPORT_IDLE_OFF);
2130 break;
2131 }
2132 }
2133
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002134 void incActiveIdleOps() {
2135 synchronized (this) {
2136 mActiveIdleOpCount++;
2137 }
2138 }
2139
2140 void decActiveIdleOps() {
2141 synchronized (this) {
2142 mActiveIdleOpCount--;
2143 if (mActiveIdleOpCount <= 0) {
2144 exitMaintenanceEarlyIfNeededLocked();
Dianne Hackborn945c9c92016-03-30 14:55:00 -07002145 mActiveIdleWakeLock.release();
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002146 }
2147 }
2148 }
2149
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002150 void setJobsActive(boolean active) {
2151 synchronized (this) {
2152 mJobsActive = active;
Yao Chenca5edbb2016-01-13 14:44:36 -08002153 reportMaintenanceActivityIfNeededLocked();
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002154 if (!active) {
2155 exitMaintenanceEarlyIfNeededLocked();
2156 }
2157 }
2158 }
2159
2160 void setAlarmsActive(boolean active) {
2161 synchronized (this) {
2162 mAlarmsActive = active;
2163 if (!active) {
2164 exitMaintenanceEarlyIfNeededLocked();
2165 }
2166 }
2167 }
2168
Yao Chenca5edbb2016-01-13 14:44:36 -08002169 boolean registerMaintenanceActivityListener(IMaintenanceActivityListener listener) {
2170 synchronized (this) {
2171 mMaintenanceActivityListeners.register(listener);
2172 return mReportedMaintenanceActivity;
2173 }
2174 }
2175
2176 void unregisterMaintenanceActivityListener(IMaintenanceActivityListener listener) {
2177 synchronized (this) {
2178 mMaintenanceActivityListeners.unregister(listener);
2179 }
2180 }
2181
2182 void reportMaintenanceActivityIfNeededLocked() {
Dianne Hackborn7ab40252016-06-15 17:30:24 -07002183 boolean active = mJobsActive;
Yao Chenca5edbb2016-01-13 14:44:36 -08002184 if (active == mReportedMaintenanceActivity) {
2185 return;
2186 }
2187 mReportedMaintenanceActivity = active;
2188 Message msg = mHandler.obtainMessage(MSG_REPORT_MAINTENANCE_ACTIVITY,
2189 mReportedMaintenanceActivity ? 1 : 0, 0);
2190 mHandler.sendMessage(msg);
2191 }
2192
Dianne Hackborn945c9c92016-03-30 14:55:00 -07002193 boolean isOpsInactiveLocked() {
Dianne Hackborn7ab40252016-06-15 17:30:24 -07002194 return mActiveIdleOpCount <= 0 && !mJobsActive && !mAlarmsActive;
Dianne Hackborn945c9c92016-03-30 14:55:00 -07002195 }
2196
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002197 void exitMaintenanceEarlyIfNeededLocked() {
Dianne Hackborn945c9c92016-03-30 14:55:00 -07002198 if (mState == STATE_IDLE_MAINTENANCE || mLightState == LIGHT_STATE_IDLE_MAINTENANCE
2199 || mLightState == LIGHT_STATE_PRE_IDLE) {
2200 if (isOpsInactiveLocked()) {
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002201 final long now = SystemClock.elapsedRealtime();
2202 if (DEBUG) {
2203 StringBuilder sb = new StringBuilder();
2204 sb.append("Exit: start=");
2205 TimeUtils.formatDuration(mMaintenanceStartTime, sb);
2206 sb.append(" now=");
2207 TimeUtils.formatDuration(now, sb);
2208 Slog.d(TAG, sb.toString());
2209 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002210 if (mState == STATE_IDLE_MAINTENANCE) {
Dianne Hackborn945c9c92016-03-30 14:55:00 -07002211 stepIdleStateLocked("s:early");
2212 } else if (mLightState == LIGHT_STATE_PRE_IDLE) {
2213 stepLightIdleStateLocked("s:predone");
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002214 } else {
Dianne Hackborn945c9c92016-03-30 14:55:00 -07002215 stepLightIdleStateLocked("s:early");
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002216 }
2217 }
2218 }
2219 }
2220
Nick Vaccaro20feaea2015-09-17 17:22:44 -07002221 void motionLocked() {
2222 if (DEBUG) Slog.d(TAG, "motionLocked()");
2223 // The motion sensor will have been disabled at this point
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002224 handleMotionDetectedLocked(mConstants.MOTION_INACTIVE_TIMEOUT, "motion");
2225 }
2226
2227 void handleMotionDetectedLocked(long timeout, String type) {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002228 // The device is not yet active, so we want to go back to the pending idle
Nick Vaccaro20feaea2015-09-17 17:22:44 -07002229 // state to wait again for no motion. Note that we only monitor for motion
2230 // after moving out of the inactive state, so no need to worry about that.
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002231 boolean becomeInactive = false;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002232 if (mState != STATE_ACTIVE) {
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002233 scheduleReportActiveLocked(type, Process.myUid());
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002234 mState = STATE_ACTIVE;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002235 mInactiveTimeout = timeout;
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08002236 mCurIdleBudget = 0;
2237 mMaintenanceStartTime = 0;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002238 EventLogTags.writeDeviceIdle(mState, type);
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08002239 addEvent(EVENT_NORMAL);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002240 becomeInactive = true;
2241 }
2242 if (mLightState == LIGHT_STATE_OVERRIDE) {
Dianne Hackbornb6843652016-02-22 12:20:13 -08002243 // We went out of light idle mode because we had started deep idle mode... let's
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002244 // now go back and reset things so we resume light idling if appropriate.
2245 mLightState = STATE_ACTIVE;
2246 EventLogTags.writeDeviceIdleLight(mLightState, type);
2247 becomeInactive = true;
2248 }
2249 if (becomeInactive) {
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002250 becomeInactiveIfAppropriateLocked();
2251 }
2252 }
2253
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002254 void receivedGenericLocationLocked(Location location) {
2255 if (mState != STATE_LOCATING) {
2256 cancelLocatingLocked();
2257 return;
2258 }
2259 if (DEBUG) Slog.d(TAG, "Generic location: " + location);
2260 mLastGenericLocation = new Location(location);
Joe LaPenna23d681b2015-08-27 15:12:11 -07002261 if (location.getAccuracy() > mConstants.LOCATION_ACCURACY && mHasGps) {
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002262 return;
2263 }
2264 mLocated = true;
2265 if (mNotMoving) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002266 stepIdleStateLocked("s:location");
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002267 }
2268 }
2269
2270 void receivedGpsLocationLocked(Location location) {
2271 if (mState != STATE_LOCATING) {
2272 cancelLocatingLocked();
2273 return;
2274 }
2275 if (DEBUG) Slog.d(TAG, "GPS location: " + location);
2276 mLastGpsLocation = new Location(location);
2277 if (location.getAccuracy() > mConstants.LOCATION_ACCURACY) {
2278 return;
2279 }
2280 mLocated = true;
2281 if (mNotMoving) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002282 stepIdleStateLocked("s:gps");
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002283 }
2284 }
2285
Nick Vaccaro20feaea2015-09-17 17:22:44 -07002286 void startMonitoringMotionLocked() {
2287 if (DEBUG) Slog.d(TAG, "startMonitoringMotionLocked()");
2288 if (mMotionSensor != null && !mMotionListener.active) {
2289 mMotionListener.registerLocked();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002290 }
2291 }
2292
Nick Vaccaro20feaea2015-09-17 17:22:44 -07002293 void stopMonitoringMotionLocked() {
2294 if (DEBUG) Slog.d(TAG, "stopMonitoringMotionLocked()");
2295 if (mMotionSensor != null && mMotionListener.active) {
2296 mMotionListener.unregisterLocked();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002297 }
2298 }
2299
2300 void cancelAlarmLocked() {
2301 if (mNextAlarmTime != 0) {
2302 mNextAlarmTime = 0;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002303 mAlarmManager.cancel(mDeepAlarmListener);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002304 }
2305 }
2306
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002307 void cancelLightAlarmLocked() {
2308 if (mNextLightAlarmTime != 0) {
2309 mNextLightAlarmTime = 0;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002310 mAlarmManager.cancel(mLightAlarmListener);
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002311 }
2312 }
2313
2314 void cancelLocatingLocked() {
2315 if (mLocating) {
2316 mLocationManager.removeUpdates(mGenericLocationListener);
2317 mLocationManager.removeUpdates(mGpsLocationListener);
2318 mLocating = false;
2319 }
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002320 }
2321
Kevin Gabayan92f15e62016-04-04 17:52:22 -07002322 void cancelSensingTimeoutAlarmLocked() {
2323 if (mNextSensingTimeoutAlarmTime != 0) {
2324 mNextSensingTimeoutAlarmTime = 0;
2325 mAlarmManager.cancel(mSensingTimeoutAlarmListener);
2326 }
2327 }
2328
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002329 void scheduleAlarmLocked(long delay, boolean idleUntil) {
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002330 if (DEBUG) Slog.d(TAG, "scheduleAlarmLocked(" + delay + ", " + idleUntil + ")");
Nick Vaccaro20feaea2015-09-17 17:22:44 -07002331 if (mMotionSensor == null) {
Joe LaPenna23d681b2015-08-27 15:12:11 -07002332 // If there is no motion sensor on this device, then we won't schedule
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002333 // alarms, because we can't determine if the device is not moving. This effectively
Joe LaPenna23d681b2015-08-27 15:12:11 -07002334 // turns off normal execution of device idling, although it is still possible to
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002335 // manually poke it by pretending like the alarm is going off.
2336 return;
2337 }
2338 mNextAlarmTime = SystemClock.elapsedRealtime() + delay;
2339 if (idleUntil) {
2340 mAlarmManager.setIdleUntil(AlarmManager.ELAPSED_REALTIME_WAKEUP,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002341 mNextAlarmTime, "DeviceIdleController.deep", mDeepAlarmListener, mHandler);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002342 } else {
2343 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002344 mNextAlarmTime, "DeviceIdleController.deep", mDeepAlarmListener, mHandler);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002345 }
2346 }
2347
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002348 void scheduleLightAlarmLocked(long delay) {
2349 if (DEBUG) Slog.d(TAG, "scheduleLightAlarmLocked(" + delay + ")");
Dianne Hackborn08c47a52015-10-15 12:38:14 -07002350 mNextLightAlarmTime = SystemClock.elapsedRealtime() + delay;
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07002351 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002352 mNextLightAlarmTime, "DeviceIdleController.light", mLightAlarmListener, mHandler);
Kevin Gabayan89ecf822015-05-18 12:10:07 -07002353 }
2354
Kevin Gabayan92f15e62016-04-04 17:52:22 -07002355 void scheduleSensingTimeoutAlarmLocked(long delay) {
2356 if (DEBUG) Slog.d(TAG, "scheduleSensingAlarmLocked(" + delay + ")");
2357 mNextSensingTimeoutAlarmTime = SystemClock.elapsedRealtime() + delay;
2358 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, mNextSensingTimeoutAlarmTime,
2359 "DeviceIdleController.sensing", mSensingTimeoutAlarmListener, mHandler);
2360 }
2361
Dianne Hackborn4a503b12015-08-06 22:19:06 -07002362 private static int[] buildAppIdArray(ArrayMap<String, Integer> systemApps,
2363 ArrayMap<String, Integer> userApps, SparseBooleanArray outAppIds) {
2364 outAppIds.clear();
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08002365 if (systemApps != null) {
2366 for (int i = 0; i < systemApps.size(); i++) {
2367 outAppIds.put(systemApps.valueAt(i), true);
2368 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002369 }
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08002370 if (userApps != null) {
2371 for (int i = 0; i < userApps.size(); i++) {
2372 outAppIds.put(userApps.valueAt(i), true);
2373 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002374 }
Dianne Hackborn4a503b12015-08-06 22:19:06 -07002375 int size = outAppIds.size();
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002376 int[] appids = new int[size];
2377 for (int i = 0; i < size; i++) {
Dianne Hackborn4a503b12015-08-06 22:19:06 -07002378 appids[i] = outAppIds.keyAt(i);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002379 }
Dianne Hackborn4a503b12015-08-06 22:19:06 -07002380 return appids;
2381 }
2382
2383 private void updateWhitelistAppIdsLocked() {
2384 mPowerSaveWhitelistExceptIdleAppIdArray = buildAppIdArray(mPowerSaveWhitelistAppsExceptIdle,
2385 mPowerSaveWhitelistUserApps, mPowerSaveWhitelistExceptIdleAppIds);
2386 mPowerSaveWhitelistAllAppIdArray = buildAppIdArray(mPowerSaveWhitelistApps,
2387 mPowerSaveWhitelistUserApps, mPowerSaveWhitelistAllAppIds);
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08002388 mPowerSaveWhitelistUserAppIdArray = buildAppIdArray(null,
2389 mPowerSaveWhitelistUserApps, mPowerSaveWhitelistUserAppIds);
Dianne Hackborn85e35642017-01-12 15:10:57 -08002390 if (mLocalActivityManager != null) {
2391 if (DEBUG) {
2392 Slog.d(TAG, "Setting activity manager whitelist to "
2393 + Arrays.toString(mPowerSaveWhitelistAllAppIdArray));
2394 }
2395 mLocalActivityManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray);
2396 }
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002397 if (mLocalPowerManager != null) {
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002398 if (DEBUG) {
2399 Slog.d(TAG, "Setting wakelock whitelist to "
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07002400 + Arrays.toString(mPowerSaveWhitelistAllAppIdArray));
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002401 }
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07002402 mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray);
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002403 }
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08002404 if (mLocalAlarmManager != null) {
2405 if (DEBUG) {
2406 Slog.d(TAG, "Setting alarm whitelist to "
2407 + Arrays.toString(mPowerSaveWhitelistUserAppIdArray));
2408 }
2409 mLocalAlarmManager.setDeviceIdleUserWhitelist(mPowerSaveWhitelistUserAppIdArray);
2410 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002411 }
2412
Dianne Hackborn85e35642017-01-12 15:10:57 -08002413 private void updateTempWhitelistAppIdsLocked(int appId, boolean adding) {
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002414 final int size = mTempWhitelistAppIdEndTimes.size();
2415 if (mTempWhitelistAppIdArray.length != size) {
2416 mTempWhitelistAppIdArray = new int[size];
2417 }
2418 for (int i = 0; i < size; i++) {
2419 mTempWhitelistAppIdArray[i] = mTempWhitelistAppIdEndTimes.keyAt(i);
2420 }
Dianne Hackborn85e35642017-01-12 15:10:57 -08002421 if (mLocalActivityManager != null) {
2422 if (DEBUG) {
2423 Slog.d(TAG, "Setting activity manager temp whitelist to "
2424 + Arrays.toString(mTempWhitelistAppIdArray));
2425 }
2426 mLocalActivityManager.updateDeviceIdleTempWhitelist(mTempWhitelistAppIdArray, appId,
2427 adding);
2428 }
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002429 if (mLocalPowerManager != null) {
2430 if (DEBUG) {
2431 Slog.d(TAG, "Setting wakelock temp whitelist to "
2432 + Arrays.toString(mTempWhitelistAppIdArray));
2433 }
2434 mLocalPowerManager.setDeviceIdleTempWhitelist(mTempWhitelistAppIdArray);
2435 }
2436 }
2437
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002438 private void reportPowerSaveWhitelistChangedLocked() {
2439 Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED);
2440 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Xiaohui Chene4de5a02015-09-22 15:33:31 -07002441 getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002442 }
2443
2444 private void reportTempWhitelistChangedLocked() {
2445 Intent intent = new Intent(PowerManager.ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED);
2446 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Xiaohui Chene4de5a02015-09-22 15:33:31 -07002447 getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002448 }
2449
2450 void readConfigFileLocked() {
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07002451 if (DEBUG) Slog.d(TAG, "Reading config from " + mConfigFile.getBaseFile());
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002452 mPowerSaveWhitelistUserApps.clear();
2453 FileInputStream stream;
2454 try {
2455 stream = mConfigFile.openRead();
2456 } catch (FileNotFoundException e) {
2457 return;
2458 }
2459 try {
2460 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002461 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002462 readConfigFileLocked(parser);
2463 } catch (XmlPullParserException e) {
2464 } finally {
2465 try {
2466 stream.close();
2467 } catch (IOException e) {
2468 }
2469 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002470 }
2471
2472 private void readConfigFileLocked(XmlPullParser parser) {
2473 final PackageManager pm = getContext().getPackageManager();
2474
2475 try {
2476 int type;
2477 while ((type = parser.next()) != XmlPullParser.START_TAG
2478 && type != XmlPullParser.END_DOCUMENT) {
2479 ;
2480 }
2481
2482 if (type != XmlPullParser.START_TAG) {
2483 throw new IllegalStateException("no start tag found");
2484 }
2485
2486 int outerDepth = parser.getDepth();
2487 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2488 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2489 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2490 continue;
2491 }
2492
2493 String tagName = parser.getName();
2494 if (tagName.equals("wl")) {
2495 String name = parser.getAttributeValue(null, "n");
2496 if (name != null) {
2497 try {
Dianne Hackborn1b79ad72015-10-12 10:59:47 -07002498 ApplicationInfo ai = pm.getApplicationInfo(name,
Amith Yamasani0d1fd8d2016-10-12 14:21:51 -07002499 PackageManager.MATCH_ANY_USER);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002500 mPowerSaveWhitelistUserApps.put(ai.packageName,
2501 UserHandle.getAppId(ai.uid));
2502 } catch (PackageManager.NameNotFoundException e) {
2503 }
2504 }
2505 } else {
2506 Slog.w(TAG, "Unknown element under <config>: "
2507 + parser.getName());
2508 XmlUtils.skipCurrentTag(parser);
2509 }
2510 }
2511
2512 } catch (IllegalStateException e) {
2513 Slog.w(TAG, "Failed parsing config " + e);
2514 } catch (NullPointerException e) {
2515 Slog.w(TAG, "Failed parsing config " + e);
2516 } catch (NumberFormatException e) {
2517 Slog.w(TAG, "Failed parsing config " + e);
2518 } catch (XmlPullParserException e) {
2519 Slog.w(TAG, "Failed parsing config " + e);
2520 } catch (IOException e) {
2521 Slog.w(TAG, "Failed parsing config " + e);
2522 } catch (IndexOutOfBoundsException e) {
2523 Slog.w(TAG, "Failed parsing config " + e);
2524 }
2525 }
2526
2527 void writeConfigFileLocked() {
2528 mHandler.removeMessages(MSG_WRITE_CONFIG);
2529 mHandler.sendEmptyMessageDelayed(MSG_WRITE_CONFIG, 5000);
2530 }
2531
2532 void handleWriteConfigFile() {
2533 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
2534
2535 try {
2536 synchronized (this) {
2537 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002538 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002539 writeConfigFileLocked(out);
2540 }
2541 } catch (IOException e) {
2542 }
2543
2544 synchronized (mConfigFile) {
2545 FileOutputStream stream = null;
2546 try {
2547 stream = mConfigFile.startWrite();
2548 memStream.writeTo(stream);
2549 stream.flush();
2550 FileUtils.sync(stream);
2551 stream.close();
2552 mConfigFile.finishWrite(stream);
2553 } catch (IOException e) {
2554 Slog.w(TAG, "Error writing config file", e);
2555 mConfigFile.failWrite(stream);
2556 }
2557 }
2558 }
2559
2560 void writeConfigFileLocked(XmlSerializer out) throws IOException {
2561 out.startDocument(null, true);
2562 out.startTag(null, "config");
2563 for (int i=0; i<mPowerSaveWhitelistUserApps.size(); i++) {
2564 String name = mPowerSaveWhitelistUserApps.keyAt(i);
2565 out.startTag(null, "wl");
2566 out.attribute(null, "n", name);
2567 out.endTag(null, "wl");
2568 }
2569 out.endTag(null, "config");
2570 out.endDocument();
2571 }
2572
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002573 static void dumpHelp(PrintWriter pw) {
2574 pw.println("Device idle controller (deviceidle) commands:");
2575 pw.println(" help");
2576 pw.println(" Print this help text.");
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002577 pw.println(" step [light|deep]");
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002578 pw.println(" Immediately step to next state, without waiting for alarm.");
Dianne Hackborn88c41352016-04-07 15:18:58 -07002579 pw.println(" force-idle [light|deep]");
Dianne Hackborn4a503b12015-08-06 22:19:06 -07002580 pw.println(" Force directly into idle mode, regardless of other device state.");
Dianne Hackborn88c41352016-04-07 15:18:58 -07002581 pw.println(" force-inactive");
2582 pw.println(" Force to be inactive, ready to freely step idle states.");
2583 pw.println(" unforce");
2584 pw.println(" Resume normal functioning after force-idle or force-inactive.");
2585 pw.println(" get [light|deep|force|screen|charging|network]");
2586 pw.println(" Retrieve the current given state.");
Dianne Hackbornb6843652016-02-22 12:20:13 -08002587 pw.println(" disable [light|deep|all]");
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002588 pw.println(" Completely disable device idle mode.");
Dianne Hackbornb6843652016-02-22 12:20:13 -08002589 pw.println(" enable [light|deep|all]");
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002590 pw.println(" Re-enable device idle mode after it had previously been disabled.");
Dianne Hackbornb6843652016-02-22 12:20:13 -08002591 pw.println(" enabled [light|deep|all]");
Dianne Hackborn92617032015-06-19 15:32:19 -07002592 pw.println(" Print 1 if device idle mode is currently enabled, else 0.");
Dianne Hackborn1b139682015-07-06 15:13:37 -07002593 pw.println(" whitelist");
2594 pw.println(" Print currently whitelisted apps.");
Dianne Hackborn92617032015-06-19 15:32:19 -07002595 pw.println(" whitelist [package ...]");
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002596 pw.println(" Add (prefix with +) or remove (prefix with -) packages.");
Sudheer Shanka3f4d7702017-04-28 17:38:03 -07002597 pw.println(" except-idle-whitelist [package ...|reset]");
2598 pw.println(" Prefix the package with '+' to add it to whitelist or "
2599 + "'=' to check if it is already whitelisted");
2600 pw.println(" [reset] will reset the whitelist to it's original state");
2601 pw.println(" Note that unlike <whitelist> cmd, "
2602 + "changes made using this won't be persisted across boots");
Felipe Lemea1b79bf2016-05-24 13:06:54 -07002603 pw.println(" tempwhitelist");
2604 pw.println(" Print packages that are temporarily whitelisted.");
Dianne Hackborn85e35642017-01-12 15:10:57 -08002605 pw.println(" tempwhitelist [-u USER] [-d DURATION] [package ..]");
2606 pw.println(" Temporarily place packages in whitelist for DURATION milliseconds.");
2607 pw.println(" If no DURATION is specified, 10 seconds is used");
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002608 }
2609
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002610 class Shell extends ShellCommand {
2611 int userId = UserHandle.USER_SYSTEM;
2612
2613 @Override
2614 public int onCommand(String cmd) {
2615 return onShellCommand(this, cmd);
2616 }
2617
2618 @Override
2619 public void onHelp() {
2620 PrintWriter pw = getOutPrintWriter();
2621 dumpHelp(pw);
2622 }
2623 }
2624
2625 int onShellCommand(Shell shell, String cmd) {
2626 PrintWriter pw = shell.getOutPrintWriter();
2627 if ("step".equals(cmd)) {
2628 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
2629 null);
2630 synchronized (this) {
2631 long token = Binder.clearCallingIdentity();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002632 String arg = shell.getNextArg();
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002633 try {
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002634 if (arg == null || "deep".equals(arg)) {
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002635 stepIdleStateLocked("s:shell");
2636 pw.print("Stepped to deep: ");
2637 pw.println(stateToString(mState));
2638 } else if ("light".equals(arg)) {
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07002639 stepLightIdleStateLocked("s:shell");
2640 pw.print("Stepped to light: "); pw.println(lightStateToString(mLightState));
2641 } else {
2642 pw.println("Unknown idle mode: " + arg);
2643 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002644 } finally {
2645 Binder.restoreCallingIdentity(token);
2646 }
2647 }
2648 } else if ("force-idle".equals(cmd)) {
2649 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
2650 null);
2651 synchronized (this) {
2652 long token = Binder.clearCallingIdentity();
Dianne Hackborn88c41352016-04-07 15:18:58 -07002653 String arg = shell.getNextArg();
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002654 try {
Dianne Hackborn88c41352016-04-07 15:18:58 -07002655 if (arg == null || "deep".equals(arg)) {
2656 if (!mDeepEnabled) {
2657 pw.println("Unable to go deep idle; not enabled");
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002658 return -1;
2659 }
Dianne Hackborn88c41352016-04-07 15:18:58 -07002660 mForceIdle = true;
2661 becomeInactiveIfAppropriateLocked();
2662 int curState = mState;
2663 while (curState != STATE_IDLE) {
2664 stepIdleStateLocked("s:shell");
2665 if (curState == mState) {
2666 pw.print("Unable to go deep idle; stopped at ");
2667 pw.println(stateToString(mState));
2668 exitForceIdleLocked();
2669 return -1;
2670 }
2671 curState = mState;
2672 }
2673 pw.println("Now forced in to deep idle mode");
2674 } else if ("light".equals(arg)) {
2675 mForceIdle = true;
2676 becomeInactiveIfAppropriateLocked();
2677 int curLightState = mLightState;
2678 while (curLightState != LIGHT_STATE_IDLE) {
2679 stepIdleStateLocked("s:shell");
2680 if (curLightState == mLightState) {
2681 pw.print("Unable to go light idle; stopped at ");
2682 pw.println(lightStateToString(mLightState));
2683 exitForceIdleLocked();
2684 return -1;
2685 }
2686 curLightState = mLightState;
2687 }
2688 pw.println("Now forced in to light idle mode");
2689 } else {
2690 pw.println("Unknown idle mode: " + arg);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002691 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002692 } finally {
2693 Binder.restoreCallingIdentity(token);
2694 }
2695 }
Dianne Hackborn88c41352016-04-07 15:18:58 -07002696 } else if ("force-inactive".equals(cmd)) {
2697 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
2698 null);
2699 synchronized (this) {
2700 long token = Binder.clearCallingIdentity();
2701 try {
2702 mForceIdle = true;
2703 becomeInactiveIfAppropriateLocked();
2704 pw.print("Light state: ");
2705 pw.print(lightStateToString(mLightState));
2706 pw.print(", deep state: ");
2707 pw.println(stateToString(mState));
2708 } finally {
2709 Binder.restoreCallingIdentity(token);
2710 }
2711 }
2712 } else if ("unforce".equals(cmd)) {
2713 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
2714 null);
2715 synchronized (this) {
2716 long token = Binder.clearCallingIdentity();
2717 try {
2718 exitForceIdleLocked();
2719 pw.print("Light state: ");
2720 pw.print(lightStateToString(mLightState));
2721 pw.print(", deep state: ");
2722 pw.println(stateToString(mState));
2723 } finally {
2724 Binder.restoreCallingIdentity(token);
2725 }
2726 }
2727 } else if ("get".equals(cmd)) {
2728 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
2729 null);
2730 synchronized (this) {
2731 String arg = shell.getNextArg();
2732 if (arg != null) {
2733 long token = Binder.clearCallingIdentity();
2734 try {
2735 switch (arg) {
2736 case "light": pw.println(lightStateToString(mLightState)); break;
2737 case "deep": pw.println(stateToString(mState)); break;
2738 case "force": pw.println(mForceIdle); break;
2739 case "screen": pw.println(mScreenOn); break;
2740 case "charging": pw.println(mCharging); break;
2741 case "network": pw.println(mNetworkConnected); break;
2742 default: pw.println("Unknown get option: " + arg); break;
2743 }
2744 } finally {
2745 Binder.restoreCallingIdentity(token);
2746 }
2747 } else {
2748 pw.println("Argument required");
2749 }
2750 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002751 } else if ("disable".equals(cmd)) {
2752 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
2753 null);
2754 synchronized (this) {
2755 long token = Binder.clearCallingIdentity();
Dianne Hackbornb6843652016-02-22 12:20:13 -08002756 String arg = shell.getNextArg();
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002757 try {
Dianne Hackbornb6843652016-02-22 12:20:13 -08002758 boolean becomeActive = false;
2759 boolean valid = false;
2760 if (arg == null || "deep".equals(arg) || "all".equals(arg)) {
2761 valid = true;
2762 if (mDeepEnabled) {
2763 mDeepEnabled = false;
2764 becomeActive = true;
2765 pw.println("Deep idle mode disabled");
2766 }
2767 }
2768 if (arg == null || "light".equals(arg) || "all".equals(arg)) {
2769 valid = true;
2770 if (mLightEnabled) {
2771 mLightEnabled = false;
2772 becomeActive = true;
2773 pw.println("Light idle mode disabled");
2774 }
2775 }
2776 if (becomeActive) {
2777 becomeActiveLocked((arg == null ? "all" : arg) + "-disabled",
2778 Process.myUid());
2779 }
2780 if (!valid) {
2781 pw.println("Unknown idle mode: " + arg);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002782 }
2783 } finally {
2784 Binder.restoreCallingIdentity(token);
2785 }
2786 }
2787 } else if ("enable".equals(cmd)) {
2788 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER,
2789 null);
2790 synchronized (this) {
2791 long token = Binder.clearCallingIdentity();
Dianne Hackbornb6843652016-02-22 12:20:13 -08002792 String arg = shell.getNextArg();
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002793 try {
Dianne Hackbornb6843652016-02-22 12:20:13 -08002794 boolean becomeInactive = false;
2795 boolean valid = false;
2796 if (arg == null || "deep".equals(arg) || "all".equals(arg)) {
2797 valid = true;
2798 if (!mDeepEnabled) {
2799 mDeepEnabled = true;
2800 becomeInactive = true;
2801 pw.println("Deep idle mode enabled");
2802 }
2803 }
2804 if (arg == null || "light".equals(arg) || "all".equals(arg)) {
2805 valid = true;
2806 if (!mLightEnabled) {
2807 mLightEnabled = true;
2808 becomeInactive = true;
2809 pw.println("Light idle mode enable");
2810 }
2811 }
2812 if (becomeInactive) {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002813 becomeInactiveIfAppropriateLocked();
Dianne Hackbornb6843652016-02-22 12:20:13 -08002814 }
2815 if (!valid) {
2816 pw.println("Unknown idle mode: " + arg);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002817 }
2818 } finally {
2819 Binder.restoreCallingIdentity(token);
2820 }
2821 }
2822 } else if ("enabled".equals(cmd)) {
2823 synchronized (this) {
Dianne Hackbornb6843652016-02-22 12:20:13 -08002824 String arg = shell.getNextArg();
2825 if (arg == null || "all".equals(arg)) {
2826 pw.println(mDeepEnabled && mLightEnabled ? "1" : 0);
2827 } else if ("deep".equals(arg)) {
2828 pw.println(mDeepEnabled ? "1" : 0);
2829 } else if ("light".equals(arg)) {
2830 pw.println(mLightEnabled ? "1" : 0);
2831 } else {
2832 pw.println("Unknown idle mode: " + arg);
2833 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002834 }
2835 } else if ("whitelist".equals(cmd)) {
Dianne Hackborneb909e32016-09-29 14:35:15 -07002836 String arg = shell.getNextArg();
2837 if (arg != null) {
2838 getContext().enforceCallingOrSelfPermission(
2839 android.Manifest.permission.DEVICE_POWER, null);
2840 long token = Binder.clearCallingIdentity();
2841 try {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002842 do {
2843 if (arg.length() < 1 || (arg.charAt(0) != '-'
Felipe Lemef8a46232016-02-10 13:51:54 -08002844 && arg.charAt(0) != '+' && arg.charAt(0) != '=')) {
2845 pw.println("Package must be prefixed with +, -, or =: " + arg);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002846 return -1;
2847 }
2848 char op = arg.charAt(0);
2849 String pkg = arg.substring(1);
2850 if (op == '+') {
2851 if (addPowerSaveWhitelistAppInternal(pkg)) {
2852 pw.println("Added: " + pkg);
2853 } else {
2854 pw.println("Unknown package: " + pkg);
2855 }
Felipe Lemef8a46232016-02-10 13:51:54 -08002856 } else if (op == '-') {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002857 if (removePowerSaveWhitelistAppInternal(pkg)) {
2858 pw.println("Removed: " + pkg);
2859 }
Felipe Lemef8a46232016-02-10 13:51:54 -08002860 } else {
2861 pw.println(getPowerSaveWhitelistAppInternal(pkg));
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002862 }
2863 } while ((arg=shell.getNextArg()) != null);
Dianne Hackborneb909e32016-09-29 14:35:15 -07002864 } finally {
2865 Binder.restoreCallingIdentity(token);
2866 }
2867 } else {
2868 synchronized (this) {
2869 for (int j=0; j<mPowerSaveWhitelistAppsExceptIdle.size(); j++) {
2870 pw.print("system-excidle,");
2871 pw.print(mPowerSaveWhitelistAppsExceptIdle.keyAt(j));
2872 pw.print(",");
2873 pw.println(mPowerSaveWhitelistAppsExceptIdle.valueAt(j));
2874 }
2875 for (int j=0; j<mPowerSaveWhitelistApps.size(); j++) {
2876 pw.print("system,");
2877 pw.print(mPowerSaveWhitelistApps.keyAt(j));
2878 pw.print(",");
2879 pw.println(mPowerSaveWhitelistApps.valueAt(j));
2880 }
2881 for (int j=0; j<mPowerSaveWhitelistUserApps.size(); j++) {
2882 pw.print("user,");
2883 pw.print(mPowerSaveWhitelistUserApps.keyAt(j));
2884 pw.print(",");
2885 pw.println(mPowerSaveWhitelistUserApps.valueAt(j));
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002886 }
2887 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002888 }
2889 } else if ("tempwhitelist".equals(cmd)) {
Dianne Hackborn85e35642017-01-12 15:10:57 -08002890 long duration = 10000;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002891 String opt;
2892 while ((opt=shell.getNextOption()) != null) {
2893 if ("-u".equals(opt)) {
2894 opt = shell.getNextArg();
2895 if (opt == null) {
2896 pw.println("-u requires a user number");
2897 return -1;
2898 }
2899 shell.userId = Integer.parseInt(opt);
Dianne Hackborn85e35642017-01-12 15:10:57 -08002900 } else if ("-d".equals(opt)) {
2901 opt = shell.getNextArg();
2902 if (opt == null) {
2903 pw.println("-d requires a duration");
2904 return -1;
2905 }
2906 duration = Long.parseLong(opt);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002907 }
2908 }
2909 String arg = shell.getNextArg();
2910 if (arg != null) {
2911 try {
Dianne Hackborn85e35642017-01-12 15:10:57 -08002912 addPowerSaveTempWhitelistAppChecked(arg, duration, shell.userId, "shell");
Christopher Tateec3a9f32017-03-21 17:43:47 -07002913 } catch (Exception e) {
2914 pw.println("Failed: " + e);
2915 return -1;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002916 }
2917 } else {
Felipe Lemea1b79bf2016-05-24 13:06:54 -07002918 dumpTempWhitelistSchedule(pw, false);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002919 }
Sudheer Shanka3f4d7702017-04-28 17:38:03 -07002920 } else if ("except-idle-whitelist".equals(cmd)) {
2921 getContext().enforceCallingOrSelfPermission(
2922 android.Manifest.permission.DEVICE_POWER, null);
2923 final long token = Binder.clearCallingIdentity();
2924 try {
2925 String arg = shell.getNextArg();
2926 if (arg == null) {
2927 pw.println("No arguments given");
2928 return -1;
2929 } else if ("reset".equals(arg)) {
2930 resetPowerSaveWhitelistExceptIdleInternal();
2931 } else {
2932 do {
2933 if (arg.length() < 1 || (arg.charAt(0) != '-'
2934 && arg.charAt(0) != '+' && arg.charAt(0) != '=')) {
2935 pw.println("Package must be prefixed with +, -, or =: " + arg);
2936 return -1;
2937 }
2938 char op = arg.charAt(0);
2939 String pkg = arg.substring(1);
2940 if (op == '+') {
2941 if (addPowerSaveWhitelistExceptIdleInternal(pkg)) {
2942 pw.println("Added: " + pkg);
2943 } else {
2944 pw.println("Unknown package: " + pkg);
2945 }
2946 } else if (op == '=') {
2947 pw.println(getPowerSaveWhitelistExceptIdleInternal(pkg));
2948 } else {
2949 pw.println("Unknown argument: " + arg);
2950 return -1;
2951 }
2952 } while ((arg = shell.getNextArg()) != null);
2953 }
2954 } finally {
2955 Binder.restoreCallingIdentity(token);
2956 }
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002957 } else {
2958 return shell.handleDefaultCommands(cmd);
2959 }
2960 return 0;
2961 }
2962
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002963 void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06002964 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002965
2966 if (args != null) {
Xiaohui Chen7c696362015-09-16 09:56:14 -07002967 int userId = UserHandle.USER_SYSTEM;
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002968 for (int i=0; i<args.length; i++) {
2969 String arg = args[i];
2970 if ("-h".equals(arg)) {
2971 dumpHelp(pw);
2972 return;
Amith Yamasaniaf575b92015-05-29 15:35:26 -07002973 } else if ("-u".equals(arg)) {
2974 i++;
2975 if (i < args.length) {
2976 arg = args[i];
2977 userId = Integer.parseInt(arg);
2978 }
Dianne Hackborn8d66b3f2015-05-08 17:21:48 -07002979 } else if ("-a".equals(arg)) {
2980 // Ignore, we always dump all.
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002981 } else if (arg.length() > 0 && arg.charAt(0) == '-'){
2982 pw.println("Unknown option: " + arg);
2983 return;
2984 } else {
Dianne Hackborn9461b6f2015-10-07 17:33:16 -07002985 Shell shell = new Shell();
2986 shell.userId = userId;
2987 String[] newArgs = new String[args.length-i];
2988 System.arraycopy(args, i, newArgs, 0, args.length-i);
Dianne Hackborn354736e2016-08-22 17:00:05 -07002989 shell.exec(mBinderService, null, fd, null, newArgs, null,
2990 new ResultReceiver(null));
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07002991 return;
2992 }
2993 }
2994 }
2995
2996 synchronized (this) {
Dianne Hackborna750a632015-06-16 17:18:23 -07002997 mConstants.dump(pw);
2998
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08002999 if (mEventCmds[0] != EVENT_NULL) {
3000 pw.println(" Idling history:");
3001 long now = SystemClock.elapsedRealtime();
3002 for (int i=EVENT_BUFFER_SIZE-1; i>=0; i--) {
3003 int cmd = mEventCmds[i];
3004 if (cmd == EVENT_NULL) {
3005 continue;
3006 }
3007 String label;
3008 switch (mEventCmds[i]) {
3009 case EVENT_NORMAL: label = " normal"; break;
3010 case EVENT_LIGHT_IDLE: label = " light-idle"; break;
3011 case EVENT_LIGHT_MAINTENANCE: label = "light-maint"; break;
Dianne Hackbornb6843652016-02-22 12:20:13 -08003012 case EVENT_DEEP_IDLE: label = " deep-idle"; break;
3013 case EVENT_DEEP_MAINTENANCE: label = " deep-maint"; break;
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08003014 default: label = " ??"; break;
3015 }
3016 pw.print(" ");
3017 pw.print(label);
3018 pw.print(": ");
3019 TimeUtils.formatDuration(mEventTimes[i], now, pw);;
3020 pw.println();
3021 }
3022 }
3023
Dianne Hackborn4a503b12015-08-06 22:19:06 -07003024 int size = mPowerSaveWhitelistAppsExceptIdle.size();
3025 if (size > 0) {
3026 pw.println(" Whitelist (except idle) system apps:");
3027 for (int i = 0; i < size; i++) {
3028 pw.print(" ");
3029 pw.println(mPowerSaveWhitelistAppsExceptIdle.keyAt(i));
3030 }
3031 }
3032 size = mPowerSaveWhitelistApps.size();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003033 if (size > 0) {
3034 pw.println(" Whitelist system apps:");
3035 for (int i = 0; i < size; i++) {
3036 pw.print(" ");
3037 pw.println(mPowerSaveWhitelistApps.keyAt(i));
3038 }
3039 }
3040 size = mPowerSaveWhitelistUserApps.size();
3041 if (size > 0) {
3042 pw.println(" Whitelist user apps:");
3043 for (int i = 0; i < size; i++) {
3044 pw.print(" ");
3045 pw.println(mPowerSaveWhitelistUserApps.keyAt(i));
3046 }
3047 }
Dianne Hackborn4a503b12015-08-06 22:19:06 -07003048 size = mPowerSaveWhitelistExceptIdleAppIds.size();
3049 if (size > 0) {
3050 pw.println(" Whitelist (except idle) all app ids:");
3051 for (int i = 0; i < size; i++) {
3052 pw.print(" ");
3053 pw.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i));
3054 pw.println();
3055 }
3056 }
Dianne Hackborn262ae5c2016-02-10 16:28:29 -08003057 size = mPowerSaveWhitelistUserAppIds.size();
3058 if (size > 0) {
3059 pw.println(" Whitelist user app ids:");
3060 for (int i = 0; i < size; i++) {
3061 pw.print(" ");
3062 pw.print(mPowerSaveWhitelistUserAppIds.keyAt(i));
3063 pw.println();
3064 }
3065 }
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07003066 size = mPowerSaveWhitelistAllAppIds.size();
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003067 if (size > 0) {
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07003068 pw.println(" Whitelist all app ids:");
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003069 for (int i = 0; i < size; i++) {
Dianne Hackborna750a632015-06-16 17:18:23 -07003070 pw.print(" ");
Dianne Hackborn3b16cf42015-07-01 15:05:04 -07003071 pw.print(mPowerSaveWhitelistAllAppIds.keyAt(i));
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003072 pw.println();
3073 }
3074 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -07003075 dumpTempWhitelistSchedule(pw, true);
3076
Dianne Hackborna750a632015-06-16 17:18:23 -07003077 size = mTempWhitelistAppIdArray != null ? mTempWhitelistAppIdArray.length : 0;
3078 if (size > 0) {
3079 pw.println(" Temp whitelist app ids:");
3080 for (int i = 0; i < size; i++) {
3081 pw.print(" ");
3082 pw.print(mTempWhitelistAppIdArray[i]);
3083 pw.println();
3084 }
3085 }
Adam Lesinski31c05d12015-06-09 17:34:04 -07003086
Dianne Hackbornb6843652016-02-22 12:20:13 -08003087 pw.print(" mLightEnabled="); pw.print(mLightEnabled);
Felipe Lemea1b79bf2016-05-24 13:06:54 -07003088 pw.print(" mDeepEnabled="); pw.println(mDeepEnabled);
Dianne Hackborn4a503b12015-08-06 22:19:06 -07003089 pw.print(" mForceIdle="); pw.println(mForceIdle);
Nick Vaccaro20feaea2015-09-17 17:22:44 -07003090 pw.print(" mMotionSensor="); pw.println(mMotionSensor);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003091 pw.print(" mCurDisplay="); pw.println(mCurDisplay);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003092 pw.print(" mScreenOn="); pw.println(mScreenOn);
Dianne Hackborn88c41352016-04-07 15:18:58 -07003093 pw.print(" mNetworkConnected="); pw.println(mNetworkConnected);
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003094 pw.print(" mCharging="); pw.println(mCharging);
Nick Vaccaro20feaea2015-09-17 17:22:44 -07003095 pw.print(" mMotionActive="); pw.println(mMotionListener.active);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003096 pw.print(" mNotMoving="); pw.println(mNotMoving);
Joe LaPenna23d681b2015-08-27 15:12:11 -07003097 pw.print(" mLocating="); pw.print(mLocating); pw.print(" mHasGps=");
3098 pw.print(mHasGps); pw.print(" mHasNetwork=");
3099 pw.print(mHasNetworkLocation); pw.print(" mLocated="); pw.println(mLocated);
Dianne Hackborn42df4fb2015-08-14 16:43:14 -07003100 if (mLastGenericLocation != null) {
3101 pw.print(" mLastGenericLocation="); pw.println(mLastGenericLocation);
3102 }
3103 if (mLastGpsLocation != null) {
3104 pw.print(" mLastGpsLocation="); pw.println(mLastGpsLocation);
3105 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003106 pw.print(" mState="); pw.print(stateToString(mState));
3107 pw.print(" mLightState=");
3108 pw.println(lightStateToString(mLightState));
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003109 pw.print(" mInactiveTimeout="); TimeUtils.formatDuration(mInactiveTimeout, pw);
3110 pw.println();
Dianne Hackborn627dfa12015-11-11 18:10:30 -08003111 if (mActiveIdleOpCount != 0) {
3112 pw.print(" mActiveIdleOpCount="); pw.println(mActiveIdleOpCount);
3113 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003114 if (mNextAlarmTime != 0) {
3115 pw.print(" mNextAlarmTime=");
3116 TimeUtils.formatDuration(mNextAlarmTime, SystemClock.elapsedRealtime(), pw);
3117 pw.println();
3118 }
3119 if (mNextIdlePendingDelay != 0) {
3120 pw.print(" mNextIdlePendingDelay=");
3121 TimeUtils.formatDuration(mNextIdlePendingDelay, pw);
3122 pw.println();
3123 }
3124 if (mNextIdleDelay != 0) {
3125 pw.print(" mNextIdleDelay=");
3126 TimeUtils.formatDuration(mNextIdleDelay, pw);
3127 pw.println();
3128 }
Dianne Hackborn953fc942016-03-29 15:36:24 -07003129 if (mNextLightIdleDelay != 0) {
3130 pw.print(" mNextIdleDelay=");
3131 TimeUtils.formatDuration(mNextLightIdleDelay, pw);
3132 pw.println();
3133 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003134 if (mNextLightAlarmTime != 0) {
3135 pw.print(" mNextLightAlarmTime=");
3136 TimeUtils.formatDuration(mNextLightAlarmTime, SystemClock.elapsedRealtime(), pw);
3137 pw.println();
3138 }
Dianne Hackborn8ed2b972015-11-18 14:52:04 -08003139 if (mCurIdleBudget != 0) {
3140 pw.print(" mCurIdleBudget=");
3141 TimeUtils.formatDuration(mCurIdleBudget, pw);
3142 pw.println();
3143 }
3144 if (mMaintenanceStartTime != 0) {
3145 pw.print(" mMaintenanceStartTime=");
3146 TimeUtils.formatDuration(mMaintenanceStartTime, SystemClock.elapsedRealtime(), pw);
3147 pw.println();
3148 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08003149 if (mJobsActive) {
3150 pw.print(" mJobsActive="); pw.println(mJobsActive);
3151 }
3152 if (mAlarmsActive) {
3153 pw.print(" mAlarmsActive="); pw.println(mAlarmsActive);
3154 }
Dianne Hackborn0b4daca2015-04-27 09:47:32 -07003155 }
3156 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -07003157
3158 void dumpTempWhitelistSchedule(PrintWriter pw, boolean printTitle) {
3159 final int size = mTempWhitelistAppIdEndTimes.size();
3160 if (size > 0) {
3161 String prefix = "";
3162 if (printTitle) {
3163 pw.println(" Temp whitelist schedule:");
3164 prefix = " ";
3165 }
3166 final long timeNow = SystemClock.elapsedRealtime();
3167 for (int i = 0; i < size; i++) {
3168 pw.print(prefix);
3169 pw.print("UID=");
3170 pw.print(mTempWhitelistAppIdEndTimes.keyAt(i));
3171 pw.print(": ");
3172 Pair<MutableLong, String> entry = mTempWhitelistAppIdEndTimes.valueAt(i);
3173 TimeUtils.formatDuration(entry.first.value, timeNow, pw);
3174 pw.print(" - ");
3175 pw.println(entry.second);
3176 }
3177 }
3178 }
3179 }