AppIdleHistory.java revision 119be9a5fc4033eba570ec94b94862401ee84570
10a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani/** 20a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * Copyright (C) 2015 The Android Open Source Project 30a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * 40a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * Licensed under the Apache License, Version 2.0 (the "License"); you may not 50a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * use this file except in compliance with the License. You may obtain a copy 60a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * of the License at 70a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * 80a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * http://www.apache.org/licenses/LICENSE-2.0 90a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * 100a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * Unless required by applicable law or agreed to in writing, software 110a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 120a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 130a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * License for the specific language governing permissions and limitations 140a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * under the License. 150a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani */ 160a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani 170a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasanipackage com.android.server.usage; 180a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani 19119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasaniimport static android.app.usage.UsageStatsManager.REASON_MAIN_DEFAULT; 20119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasaniimport static android.app.usage.UsageStatsManager.REASON_MAIN_FORCED; 21119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasaniimport static android.app.usage.UsageStatsManager.REASON_MAIN_MASK; 22119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasaniimport static android.app.usage.UsageStatsManager.REASON_MAIN_PREDICTED; 23119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasaniimport static android.app.usage.UsageStatsManager.REASON_MAIN_USAGE; 24119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasaniimport static android.app.usage.UsageStatsManager.REASON_SUB_USAGE_USER_INTERACTION; 25172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasaniimport static android.app.usage.UsageStatsManager.STANDBY_BUCKET_ACTIVE; 26172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasaniimport static android.app.usage.UsageStatsManager.STANDBY_BUCKET_NEVER; 27172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasaniimport static android.app.usage.UsageStatsManager.STANDBY_BUCKET_RARE; 28bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasaniimport static android.app.usage.UsageStatsManager.STANDBY_BUCKET_WORKING_SET; 29172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasani 30868bde247c76e309232b1e3e6a873bde725acc22Suprabh Shuklaimport android.app.usage.AppStandbyInfo; 31afbccb7d37647f6da61ebcc52a598c7a9f54bc3fAmith Yamasaniimport android.app.usage.UsageStatsManager; 32a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport android.os.SystemClock; 330a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasaniimport android.util.ArrayMap; 34a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport android.util.AtomicFile; 35a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport android.util.Slog; 360a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasaniimport android.util.SparseArray; 37a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport android.util.TimeUtils; 38a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport android.util.Xml; 390a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani 40a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport com.android.internal.annotations.VisibleForTesting; 41a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport com.android.internal.util.FastXmlSerializer; 420a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasaniimport com.android.internal.util.IndentingPrintWriter; 430a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani 44a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport libcore.io.IoUtils; 45a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 46a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport org.xmlpull.v1.XmlPullParser; 47a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport org.xmlpull.v1.XmlPullParserException; 48a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 49a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.io.BufferedOutputStream; 50a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.io.BufferedReader; 51a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.io.File; 52a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.io.FileInputStream; 53a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.io.FileOutputStream; 54a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.io.FileReader; 55a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.io.IOException; 56a93542f9d341897f3206f775fd5720663b17504fAmith Yamasaniimport java.nio.charset.StandardCharsets; 57868bde247c76e309232b1e3e6a873bde725acc22Suprabh Shuklaimport java.util.ArrayList; 58a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 590a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani/** 600a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * Keeps track of recent active state changes in apps. 610a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani * Access should be guarded by a lock by the caller. 620a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani */ 630a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasanipublic class AppIdleHistory { 640a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani 65a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private static final String TAG = "AppIdleHistory"; 66a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 6717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani private static final boolean DEBUG = AppStandbyController.DEBUG; 6817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 69a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani // History for all users and all packages 7017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani private SparseArray<ArrayMap<String,AppUsageHistory>> mIdleHistory = new SparseArray<>(); 710a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani private static final long ONE_MINUTE = 60 * 1000; 720a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani 73a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani @VisibleForTesting 74a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani static final String APP_IDLE_FILENAME = "app_idle_stats.xml"; 75a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private static final String TAG_PACKAGES = "packages"; 76a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private static final String TAG_PACKAGE = "package"; 77a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private static final String ATTR_NAME = "name"; 78a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani // Screen on timebase time when app was last used 79a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private static final String ATTR_SCREEN_IDLE = "screenIdleTime"; 80a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani // Elapsed timebase time when app was last used 81a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private static final String ATTR_ELAPSED_IDLE = "elapsedIdleTime"; 82bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // Elapsed timebase time when the app bucket was last predicted externally 83bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani private static final String ATTR_LAST_PREDICTED_TIME = "lastPredictedTime"; 84bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // The standby bucket for the app 8517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani private static final String ATTR_CURRENT_BUCKET = "appLimitBucket"; 86bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // The reason the app was put in the above bucket 8717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani private static final String ATTR_BUCKETING_REASON = "bucketReason"; 8853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani // The last time a job was run for this app 8953f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani private static final String ATTR_LAST_RUN_JOB_TIME = "lastJobRunTime"; 9053f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani // The time when the forced active state can be overridden. 91bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani private static final String ATTR_BUCKET_ACTIVE_TIMEOUT_TIME = "activeTimeoutTime"; 92bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani // The time when the forced working_set state can be overridden. 93bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani private static final String ATTR_BUCKET_WORKING_SET_TIMEOUT_TIME = "workingSetTimeoutTime"; 9417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 95a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani // device on time = mElapsedDuration + (timeNow - mElapsedSnapshot) 96a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private long mElapsedSnapshot; // Elapsed time snapshot when last write of mDeviceOnDuration 97a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private long mElapsedDuration; // Total device on duration since device was "born" 98a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 99a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani // screen on time = mScreenOnDuration + (timeNow - mScreenOnSnapshot) 100a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private long mScreenOnSnapshot; // Elapsed time snapshot when last write of mScreenOnDuration 101a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private long mScreenOnDuration; // Total screen on duration since device was "born" 102a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 103a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private final File mStorageDir; 104a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 105a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani private boolean mScreenOn; 106a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 107bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani static class AppUsageHistory { 108bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // Last used time using elapsed timebase 109a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani long lastUsedElapsedTime; 110bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // Last used time using screen_on timebase 111a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani long lastUsedScreenTime; 112bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // Last predicted time using elapsed timebase 113bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani long lastPredictedTime; 114bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // Standby bucket 115afbccb7d37647f6da61ebcc52a598c7a9f54bc3fAmith Yamasani @UsageStatsManager.StandbyBuckets 116afbccb7d37647f6da61ebcc52a598c7a9f54bc3fAmith Yamasani int currentBucket; 117119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani // Reason for setting the standby bucket. The value here is a combination of 118119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani // one of UsageStatsManager.REASON_MAIN_* and one (or none) of 119119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani // UsageStatsManager.REASON_SUB_*. Also see REASON_MAIN_MASK and REASON_SUB_MASK. 120119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani int bucketingReason; 121bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani // In-memory only, last bucket for which the listeners were informed 12284cd7b7a9e5ad6a604c075bc620f6bd9ab6b1486Amith Yamasani int lastInformedBucket; 12353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani // The last time a job was run for this app, using elapsed timebase 12453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani long lastJobRunTime; 125bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani // When should the bucket active state timeout, in elapsed timebase, if greater than 12653f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani // lastUsedElapsedTime. 12753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani // This is used to keep the app in a high bucket regardless of other timeouts and 12853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani // predictions. 129bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani long bucketActiveTimeoutTime; 130bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani // If there's a forced working_set state, this is when it times out. This can be sitting 131bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani // under any active state timeout, so that it becomes applicable after the active state 132bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani // timeout expires. 133bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani long bucketWorkingSetTimeoutTime; 134a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 135a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 136a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani AppIdleHistory(File storageDir, long elapsedRealtime) { 137a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mElapsedSnapshot = elapsedRealtime; 138a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mScreenOnSnapshot = elapsedRealtime; 139a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mStorageDir = storageDir; 14061d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani readScreenOnTime(); 141a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 142a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 14361d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani public void updateDisplay(boolean screenOn, long elapsedRealtime) { 144a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (screenOn == mScreenOn) return; 145a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 146a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mScreenOn = screenOn; 147a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (mScreenOn) { 148a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mScreenOnSnapshot = elapsedRealtime; 149a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } else { 150a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mScreenOnDuration += elapsedRealtime - mScreenOnSnapshot; 151a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mElapsedDuration += elapsedRealtime - mElapsedSnapshot; 152a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mElapsedSnapshot = elapsedRealtime; 153a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 15417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (DEBUG) Slog.d(TAG, "mScreenOnSnapshot=" + mScreenOnSnapshot 15517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani + ", mScreenOnDuration=" + mScreenOnDuration 15617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani + ", mScreenOn=" + mScreenOn); 157a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 158a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 15961d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani public long getScreenOnTime(long elapsedRealtime) { 160a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani long screenOnTime = mScreenOnDuration; 161a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (mScreenOn) { 162a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani screenOnTime += elapsedRealtime - mScreenOnSnapshot; 163a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 164a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani return screenOnTime; 165a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 166a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 167a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani @VisibleForTesting 168a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani File getScreenOnTimeFile() { 169a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani return new File(mStorageDir, "screen_on_time"); 170a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 171a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 17261d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani private void readScreenOnTime() { 173a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani File screenOnTimeFile = getScreenOnTimeFile(); 174a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (screenOnTimeFile.exists()) { 175a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani try { 176a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani BufferedReader reader = new BufferedReader(new FileReader(screenOnTimeFile)); 177a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mScreenOnDuration = Long.parseLong(reader.readLine()); 178a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mElapsedDuration = Long.parseLong(reader.readLine()); 179a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani reader.close(); 180a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } catch (IOException | NumberFormatException e) { 181a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 182a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } else { 18361d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani writeScreenOnTime(); 184a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 185a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 186a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 18761d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani private void writeScreenOnTime() { 188a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani AtomicFile screenOnTimeFile = new AtomicFile(getScreenOnTimeFile()); 189a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani FileOutputStream fos = null; 190a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani try { 191a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani fos = screenOnTimeFile.startWrite(); 192a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani fos.write((Long.toString(mScreenOnDuration) + "\n" 193a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani + Long.toString(mElapsedDuration) + "\n").getBytes()); 194a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani screenOnTimeFile.finishWrite(fos); 195a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } catch (IOException ioe) { 196a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani screenOnTimeFile.failWrite(fos); 197a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 198a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 199a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 200a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani /** 201a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani * To be called periodically to keep track of elapsed time when app idle times are written 202a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani */ 20361d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani public void writeAppIdleDurations() { 204a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final long elapsedRealtime = SystemClock.elapsedRealtime(); 205a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani // Only bump up and snapshot the elapsed time. Don't change screen on duration. 206a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mElapsedDuration += elapsedRealtime - mElapsedSnapshot; 207a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani mElapsedSnapshot = elapsedRealtime; 20861d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani writeScreenOnTime(); 209a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 210a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 21153f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani /** 21253f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * Mark the app as used and update the bucket if necessary. If there is a timeout specified 21353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * that's in the future, then the usage event is temporary and keeps the app in the specified 21453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * bucket at least until the timeout is reached. This can be used to keep the app in an 21553f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * elevated bucket for a while until some important task gets to run. 216d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * @param appUsageHistory the usage record for the app being updated 217d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * @param packageName name of the app being updated, for logging purposes 218d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * @param newBucket the bucket to set the app to 219119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani * @param usageReason the sub-reason for usage, one of REASON_SUB_USAGE_* 22053f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @param elapsedRealtime mark as used time if non-zero 221bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani * @param timeout set the timeout of the specified bucket, if non-zero. Can only be used 222bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani * with bucket values of ACTIVE and WORKING_SET. 22353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @return 22453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani */ 225d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate public AppUsageHistory reportUsage(AppUsageHistory appUsageHistory, String packageName, 226119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani int newBucket, int usageReason, long elapsedRealtime, long timeout) { 227bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani // Set the timeout if applicable 228bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani if (timeout > elapsedRealtime) { 229bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani // Convert to elapsed timebase 230bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani final long timeoutTime = mElapsedDuration + (timeout - mElapsedSnapshot); 231bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani if (newBucket == STANDBY_BUCKET_ACTIVE) { 232bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani appUsageHistory.bucketActiveTimeoutTime = Math.max(timeoutTime, 233bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani appUsageHistory.bucketActiveTimeoutTime); 234bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani } else if (newBucket == STANDBY_BUCKET_WORKING_SET) { 235bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani appUsageHistory.bucketWorkingSetTimeoutTime = Math.max(timeoutTime, 236bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani appUsageHistory.bucketWorkingSetTimeoutTime); 237bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani } else { 238bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani throw new IllegalArgumentException("Cannot set a timeout on bucket=" + 239bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani newBucket); 240bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani } 241bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani } 242bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani 24353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani if (elapsedRealtime != 0) { 24453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani appUsageHistory.lastUsedElapsedTime = mElapsedDuration 24553f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani + (elapsedRealtime - mElapsedSnapshot); 24653f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani appUsageHistory.lastUsedScreenTime = getScreenOnTime(elapsedRealtime); 247803eab6906427517b22c7fd42b21dbf57456eb72Amith Yamasani } 248803eab6906427517b22c7fd42b21dbf57456eb72Amith Yamasani 249d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate if (appUsageHistory.currentBucket > newBucket) { 250d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate appUsageHistory.currentBucket = newBucket; 251803eab6906427517b22c7fd42b21dbf57456eb72Amith Yamasani if (DEBUG) { 25253f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani Slog.d(TAG, "Moved " + packageName + " to bucket=" + appUsageHistory 25353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani .currentBucket 254119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani + ", reason=0x0" + Integer.toHexString(appUsageHistory.bucketingReason)); 255803eab6906427517b22c7fd42b21dbf57456eb72Amith Yamasani } 25617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 257119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani appUsageHistory.bucketingReason = REASON_MAIN_USAGE | usageReason; 258803eab6906427517b22c7fd42b21dbf57456eb72Amith Yamasani 259d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate return appUsageHistory; 260d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate } 261d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate 262d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate /** 263d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * Mark the app as used and update the bucket if necessary. If there is a timeout specified 264d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * that's in the future, then the usage event is temporary and keeps the app in the specified 265d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * bucket at least until the timeout is reached. This can be used to keep the app in an 266d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * elevated bucket for a while until some important task gets to run. 267d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * @param packageName 268d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * @param userId 269d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * @param newBucket the bucket to set the app to 270119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani * @param usageReason sub reason for usage 271119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani * @param nowElapsed mark as used time if non-zero 272bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani * @param timeout set the timeout of the specified bucket, if non-zero. Can only be used 273bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani * with bucket values of ACTIVE and WORKING_SET. 274d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate * @return 275d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate */ 276d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate public AppUsageHistory reportUsage(String packageName, int userId, int newBucket, 277119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani int usageReason, long nowElapsed, long timeout) { 278d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 279d117b293aec3fc2de0f6508cb8975af4dbd6a6a3Christopher Tate AppUsageHistory history = getPackageHistory(userHistory, packageName, nowElapsed, true); 280119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani return reportUsage(history, packageName, newBucket, usageReason, nowElapsed, timeout); 281a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 282a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 28317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani private ArrayMap<String, AppUsageHistory> getUserHistory(int userId) { 28417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = mIdleHistory.get(userId); 2856776849dc5ff851a225745393f082b702754e278Amith Yamasani if (userHistory == null) { 2866776849dc5ff851a225745393f082b702754e278Amith Yamasani userHistory = new ArrayMap<>(); 2876776849dc5ff851a225745393f082b702754e278Amith Yamasani mIdleHistory.put(userId, userHistory); 28861d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani readAppIdleTimes(userId, userHistory); 2896776849dc5ff851a225745393f082b702754e278Amith Yamasani } 2906776849dc5ff851a225745393f082b702754e278Amith Yamasani return userHistory; 2916776849dc5ff851a225745393f082b702754e278Amith Yamasani } 2926776849dc5ff851a225745393f082b702754e278Amith Yamasani 29317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani private AppUsageHistory getPackageHistory(ArrayMap<String, AppUsageHistory> userHistory, 29417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani String packageName, long elapsedRealtime, boolean create) { 29517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = userHistory.get(packageName); 29617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (appUsageHistory == null && create) { 29717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory = new AppUsageHistory(); 29817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory.lastUsedElapsedTime = getElapsedTime(elapsedRealtime); 29917fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory.lastUsedScreenTime = getScreenOnTime(elapsedRealtime); 300bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani appUsageHistory.lastPredictedTime = getElapsedTime(0); 301172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasani appUsageHistory.currentBucket = STANDBY_BUCKET_NEVER; 302119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani appUsageHistory.bucketingReason = REASON_MAIN_DEFAULT; 303afbccb7d37647f6da61ebcc52a598c7a9f54bc3fAmith Yamasani appUsageHistory.lastInformedBucket = -1; 30453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani appUsageHistory.lastJobRunTime = Long.MIN_VALUE; // long long time ago 30517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani userHistory.put(packageName, appUsageHistory); 3066776849dc5ff851a225745393f082b702754e278Amith Yamasani } 30717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani return appUsageHistory; 3086776849dc5ff851a225745393f082b702754e278Amith Yamasani } 3096776849dc5ff851a225745393f082b702754e278Amith Yamasani 310a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani public void onUserRemoved(int userId) { 3116776849dc5ff851a225745393f082b702754e278Amith Yamasani mIdleHistory.remove(userId); 3126776849dc5ff851a225745393f082b702754e278Amith Yamasani } 3136776849dc5ff851a225745393f082b702754e278Amith Yamasani 31461d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani public boolean isIdle(String packageName, int userId, long elapsedRealtime) { 31517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 31617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = 31717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani getPackageHistory(userHistory, packageName, elapsedRealtime, true); 31817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (appUsageHistory == null) { 319a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani return false; // Default to not idle 320a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } else { 321172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasani return appUsageHistory.currentBucket >= STANDBY_BUCKET_RARE; 32217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani // Whether or not it's passed will now be externally calculated and the 32317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani // bucket will be pushed to the history using setAppStandbyBucket() 32417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani //return hasPassedThresholds(appUsageHistory, elapsedRealtime); 32517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 32617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 32717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 328bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani public AppUsageHistory getAppUsageHistory(String packageName, int userId, 329bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani long elapsedRealtime) { 330bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 331bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani AppUsageHistory appUsageHistory = 332bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani getPackageHistory(userHistory, packageName, elapsedRealtime, true); 333bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani return appUsageHistory; 334bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani } 335bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani 33617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani public void setAppStandbyBucket(String packageName, int userId, long elapsedRealtime, 337119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani int bucket, int reason) { 33817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 33917fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = 34017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani getPackageHistory(userHistory, packageName, elapsedRealtime, true); 34117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory.currentBucket = bucket; 34217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory.bucketingReason = reason; 343119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani if ((reason & REASON_MAIN_MASK) == REASON_MAIN_PREDICTED) { 344bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani appUsageHistory.lastPredictedTime = getElapsedTime(elapsedRealtime); 345bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani } 34617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (DEBUG) { 34717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani Slog.d(TAG, "Moved " + packageName + " to bucket=" + appUsageHistory.currentBucket 348119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani + ", reason=0x0" + Integer.toHexString(appUsageHistory.bucketingReason)); 349a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 350a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 351a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 35253f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani /** 35353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * Marks the last time a job was run, with the given elapsedRealtime. The time stored is 35453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * based on the elapsed timebase. 35553f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @param packageName 35653f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @param userId 35753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @param elapsedRealtime 35853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani */ 35953f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani public void setLastJobRunTime(String packageName, int userId, long elapsedRealtime) { 36053f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 36153f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani AppUsageHistory appUsageHistory = 36253f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani getPackageHistory(userHistory, packageName, elapsedRealtime, true); 36353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani appUsageHistory.lastJobRunTime = getElapsedTime(elapsedRealtime); 36453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani } 36553f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani 36653f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani /** 36753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * Returns the time since the last job was run for this app. This can be larger than the 36853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * current elapsedRealtime, in case it happened before boot or a really large value if no jobs 36953f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * were ever run. 37053f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @param packageName 37153f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @param userId 37253f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @param elapsedRealtime 37353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani * @return 37453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani */ 37553f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani public long getTimeSinceLastJobRun(String packageName, int userId, long elapsedRealtime) { 37653f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 37753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani AppUsageHistory appUsageHistory = 37853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani getPackageHistory(userHistory, packageName, elapsedRealtime, true); 37953f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani // Don't adjust the default, else it'll wrap around to a positive value 38053f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani if (appUsageHistory.lastJobRunTime == Long.MIN_VALUE) return Long.MAX_VALUE; 38153f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani return getElapsedTime(elapsedRealtime) - appUsageHistory.lastJobRunTime; 38253f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani } 38353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani 38417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani public int getAppStandbyBucket(String packageName, int userId, long elapsedRealtime) { 38517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 38617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = 38717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani getPackageHistory(userHistory, packageName, elapsedRealtime, true); 38817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani return appUsageHistory.currentBucket; 38917fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 39017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 391868bde247c76e309232b1e3e6a873bde725acc22Suprabh Shukla public ArrayList<AppStandbyInfo> getAppStandbyBuckets(int userId, boolean appIdleEnabled) { 392e878931414e46eaaf1e10e227cd50bcf5435dee8Amith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 393e878931414e46eaaf1e10e227cd50bcf5435dee8Amith Yamasani int size = userHistory.size(); 394868bde247c76e309232b1e3e6a873bde725acc22Suprabh Shukla ArrayList<AppStandbyInfo> buckets = new ArrayList<>(size); 395e878931414e46eaaf1e10e227cd50bcf5435dee8Amith Yamasani for (int i = 0; i < size; i++) { 396868bde247c76e309232b1e3e6a873bde725acc22Suprabh Shukla buckets.add(new AppStandbyInfo(userHistory.keyAt(i), 397868bde247c76e309232b1e3e6a873bde725acc22Suprabh Shukla appIdleEnabled ? userHistory.valueAt(i).currentBucket : STANDBY_BUCKET_ACTIVE)); 398e878931414e46eaaf1e10e227cd50bcf5435dee8Amith Yamasani } 399e878931414e46eaaf1e10e227cd50bcf5435dee8Amith Yamasani return buckets; 400e878931414e46eaaf1e10e227cd50bcf5435dee8Amith Yamasani } 401e878931414e46eaaf1e10e227cd50bcf5435dee8Amith Yamasani 402119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani public int getAppStandbyReason(String packageName, int userId, long elapsedRealtime) { 40317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 40417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = 40517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani getPackageHistory(userHistory, packageName, elapsedRealtime, false); 406119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani return appUsageHistory != null ? appUsageHistory.bucketingReason : 0; 40717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 40817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 409bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani public long getElapsedTime(long elapsedRealtime) { 410a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani return (elapsedRealtime - mElapsedSnapshot + mElapsedDuration); 411a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 412a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 413a732f014c5743af0dbb7eb2e63474a7147576f9dChristopher Tate /* Returns the new standby bucket the app is assigned to */ 414a732f014c5743af0dbb7eb2e63474a7147576f9dChristopher Tate public int setIdle(String packageName, int userId, boolean idle, long elapsedRealtime) { 41517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 41617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = getPackageHistory(userHistory, packageName, 41717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani elapsedRealtime, true); 41817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (idle) { 419172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasani appUsageHistory.currentBucket = STANDBY_BUCKET_RARE; 420119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani appUsageHistory.bucketingReason = REASON_MAIN_FORCED; 42117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } else { 422172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasani appUsageHistory.currentBucket = STANDBY_BUCKET_ACTIVE; 42317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani // This is to pretend that the app was just used, don't freeze the state anymore. 424119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani appUsageHistory.bucketingReason = REASON_MAIN_USAGE | REASON_SUB_USAGE_USER_INTERACTION; 42517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 426a732f014c5743af0dbb7eb2e63474a7147576f9dChristopher Tate return appUsageHistory.currentBucket; 427a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 428a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 42961d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani public void clearUsage(String packageName, int userId) { 43017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 431bdda1e076eb30bf88749a1a349f9bb6f52434ecdAmith Yamasani userHistory.remove(packageName); 432bdda1e076eb30bf88749a1a349f9bb6f52434ecdAmith Yamasani } 433bdda1e076eb30bf88749a1a349f9bb6f52434ecdAmith Yamasani 43417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani boolean shouldInformListeners(String packageName, int userId, 43584cd7b7a9e5ad6a604c075bc620f6bd9ab6b1486Amith Yamasani long elapsedRealtime, int bucket) { 43617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 43717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = getPackageHistory(userHistory, packageName, 43817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani elapsedRealtime, true); 43984cd7b7a9e5ad6a604c075bc620f6bd9ab6b1486Amith Yamasani if (appUsageHistory.lastInformedBucket != bucket) { 44084cd7b7a9e5ad6a604c075bc620f6bd9ab6b1486Amith Yamasani appUsageHistory.lastInformedBucket = bucket; 44117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani return true; 44217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 44317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani return false; 44417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 44517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 44617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani /** 44717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * Returns the index in the arrays of screenTimeThresholds and elapsedTimeThresholds 44817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * that corresponds to how long since the app was used. 44917fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * @param packageName 45017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * @param userId 45117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * @param elapsedRealtime current time 45217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * @param screenTimeThresholds Array of screen times, in ascending order, first one is 0 45317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * @param elapsedTimeThresholds Array of elapsed time, in ascending order, first one is 0 45417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani * @return The index whose values the app's used time exceeds (in both arrays) 45517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani */ 45617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani int getThresholdIndex(String packageName, int userId, long elapsedRealtime, 45717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani long[] screenTimeThresholds, long[] elapsedTimeThresholds) { 45817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId); 45917fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = getPackageHistory(userHistory, packageName, 46017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani elapsedRealtime, false); 46117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani // If we don't have any state for the app, assume never used 46217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (appUsageHistory == null) return screenTimeThresholds.length - 1; 46317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 46417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani long screenOnDelta = getScreenOnTime(elapsedRealtime) - appUsageHistory.lastUsedScreenTime; 46517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani long elapsedDelta = getElapsedTime(elapsedRealtime) - appUsageHistory.lastUsedElapsedTime; 46617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani 46717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (DEBUG) Slog.d(TAG, packageName 46817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani + " lastUsedScreen=" + appUsageHistory.lastUsedScreenTime 46917fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani + " lastUsedElapsed=" + appUsageHistory.lastUsedElapsedTime); 47017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (DEBUG) Slog.d(TAG, packageName + " screenOn=" + screenOnDelta 47117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani + ", elapsed=" + elapsedDelta); 47217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani for (int i = screenTimeThresholds.length - 1; i >= 0; i--) { 47317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani if (screenOnDelta >= screenTimeThresholds[i] 47417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani && elapsedDelta >= elapsedTimeThresholds[i]) { 47517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani return i; 47617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 47717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 47817fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani return 0; 479a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 480a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 48117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani @VisibleForTesting 48217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani File getUserFile(int userId) { 483a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani return new File(new File(new File(mStorageDir, "users"), 484a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Integer.toString(userId)), APP_IDLE_FILENAME); 485a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 486a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 48717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani private void readAppIdleTimes(int userId, ArrayMap<String, AppUsageHistory> userHistory) { 488a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani FileInputStream fis = null; 489a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani try { 490a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani AtomicFile appIdleFile = new AtomicFile(getUserFile(userId)); 491a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani fis = appIdleFile.openRead(); 492a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani XmlPullParser parser = Xml.newPullParser(); 493a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani parser.setInput(fis, StandardCharsets.UTF_8.name()); 494a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 495a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani int type; 496a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani while ((type = parser.next()) != XmlPullParser.START_TAG 497a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani && type != XmlPullParser.END_DOCUMENT) { 498a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani // Skip 499a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 500a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 501a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (type != XmlPullParser.START_TAG) { 502a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Slog.e(TAG, "Unable to read app idle file for user " + userId); 503a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani return; 504a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 505a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (!parser.getName().equals(TAG_PACKAGES)) { 506a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani return; 507a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 508a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { 509a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (type == XmlPullParser.START_TAG) { 510a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final String name = parser.getName(); 511a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (name.equals(TAG_PACKAGE)) { 512a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final String packageName = parser.getAttributeValue(null, ATTR_NAME); 51317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory appUsageHistory = new AppUsageHistory(); 51417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory.lastUsedElapsedTime = 515a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Long.parseLong(parser.getAttributeValue(null, ATTR_ELAPSED_IDLE)); 51617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory.lastUsedScreenTime = 517a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Long.parseLong(parser.getAttributeValue(null, ATTR_SCREEN_IDLE)); 51853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani appUsageHistory.lastPredictedTime = getLongValue(parser, 51953f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani ATTR_LAST_PREDICTED_TIME, 0L); 52017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani String currentBucketString = parser.getAttributeValue(null, 52117fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ATTR_CURRENT_BUCKET); 52217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani appUsageHistory.currentBucket = currentBucketString == null 523172612c8919638ad57ac0b67f2acec3e144fc26cAmith Yamasani ? STANDBY_BUCKET_ACTIVE 52417fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani : Integer.parseInt(currentBucketString); 525119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani String bucketingReason = 52617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani parser.getAttributeValue(null, ATTR_BUCKETING_REASON); 52753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani appUsageHistory.lastJobRunTime = getLongValue(parser, 52853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani ATTR_LAST_RUN_JOB_TIME, Long.MIN_VALUE); 529bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani appUsageHistory.bucketActiveTimeoutTime = getLongValue(parser, 530bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani ATTR_BUCKET_ACTIVE_TIMEOUT_TIME, 0L); 531bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani appUsageHistory.bucketWorkingSetTimeoutTime = getLongValue(parser, 532bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani ATTR_BUCKET_WORKING_SET_TIMEOUT_TIME, 0L); 533119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani appUsageHistory.bucketingReason = REASON_MAIN_DEFAULT; 534119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani if (bucketingReason != null) { 535119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani try { 536119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani appUsageHistory.bucketingReason = 537119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani Integer.parseInt(bucketingReason, 16); 538119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani } catch (NumberFormatException nfe) { 539119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani } 54017fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani } 541bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani appUsageHistory.lastInformedBucket = -1; 54217fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani userHistory.put(packageName, appUsageHistory); 543a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 544a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 545a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 546a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } catch (IOException | XmlPullParserException e) { 547a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Slog.e(TAG, "Unable to read app idle file for user " + userId); 548a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } finally { 549a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani IoUtils.closeQuietly(fis); 550a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 551a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 552a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 55353f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani private long getLongValue(XmlPullParser parser, String attrName, long defValue) { 55453f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani String value = parser.getAttributeValue(null, attrName); 55553f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani if (value == null) return defValue; 55653f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani return Long.parseLong(value); 55753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani } 55853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani 55961d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani public void writeAppIdleTimes(int userId) { 560a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani FileOutputStream fos = null; 561a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani AtomicFile appIdleFile = new AtomicFile(getUserFile(userId)); 562a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani try { 563a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani fos = appIdleFile.startWrite(); 564a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final BufferedOutputStream bos = new BufferedOutputStream(fos); 565a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 566a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani FastXmlSerializer xml = new FastXmlSerializer(); 567a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.setOutput(bos, StandardCharsets.UTF_8.name()); 568a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.startDocument(null, true); 569a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 570a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 571a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.startTag(null, TAG_PACKAGES); 572a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 57317fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String,AppUsageHistory> userHistory = getUserHistory(userId); 574a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final int N = userHistory.size(); 575a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani for (int i = 0; i < N; i++) { 576a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani String packageName = userHistory.keyAt(i); 57717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani AppUsageHistory history = userHistory.valueAt(i); 578a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.startTag(null, TAG_PACKAGE); 579a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.attribute(null, ATTR_NAME, packageName); 580a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.attribute(null, ATTR_ELAPSED_IDLE, 581a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Long.toString(history.lastUsedElapsedTime)); 582a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.attribute(null, ATTR_SCREEN_IDLE, 583a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Long.toString(history.lastUsedScreenTime)); 584bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani xml.attribute(null, ATTR_LAST_PREDICTED_TIME, 585bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani Long.toString(history.lastPredictedTime)); 58617fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani xml.attribute(null, ATTR_CURRENT_BUCKET, 58717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani Integer.toString(history.currentBucket)); 588119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani xml.attribute(null, ATTR_BUCKETING_REASON, 589119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani Integer.toHexString(history.bucketingReason)); 590bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani if (history.bucketActiveTimeoutTime > 0) { 591bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani xml.attribute(null, ATTR_BUCKET_ACTIVE_TIMEOUT_TIME, Long.toString(history 592bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani .bucketActiveTimeoutTime)); 593bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani } 594bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani if (history.bucketWorkingSetTimeoutTime > 0) { 595bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani xml.attribute(null, ATTR_BUCKET_WORKING_SET_TIMEOUT_TIME, Long.toString(history 596bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani .bucketWorkingSetTimeoutTime)); 59753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani } 59853f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani if (history.lastJobRunTime != Long.MIN_VALUE) { 59953f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani xml.attribute(null, ATTR_LAST_RUN_JOB_TIME, Long.toString(history 60053f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani .lastJobRunTime)); 60153f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani } 602a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.endTag(null, TAG_PACKAGE); 603a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 604a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani 605a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.endTag(null, TAG_PACKAGES); 606a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani xml.endDocument(); 607a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani appIdleFile.finishWrite(fos); 608a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } catch (Exception e) { 609a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani appIdleFile.failWrite(fos); 610a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani Slog.e(TAG, "Error writing app idle file for user " + userId); 611a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 6120a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani } 6130a11e69428d4c00dfcb368c1eb4e60ad8e0dc918Amith Yamasani 614c81983a0c3d7bfe8384dbf48909f4bcf300e36d2Dianne Hackborn public void dump(IndentingPrintWriter idpw, int userId, String pkg) { 615119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.println("App Standby States:"); 616a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.increaseIndent(); 61717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani ArrayMap<String, AppUsageHistory> userHistory = mIdleHistory.get(userId); 618a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final long elapsedRealtime = SystemClock.elapsedRealtime(); 61961d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani final long totalElapsedTime = getElapsedTime(elapsedRealtime); 62061d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani final long screenOnTime = getScreenOnTime(elapsedRealtime); 621a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani if (userHistory == null) return; 622a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final int P = userHistory.size(); 623a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani for (int p = 0; p < P; p++) { 624a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani final String packageName = userHistory.keyAt(p); 62517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani final AppUsageHistory appUsageHistory = userHistory.valueAt(p); 626c81983a0c3d7bfe8384dbf48909f4bcf300e36d2Dianne Hackborn if (pkg != null && !pkg.equals(packageName)) { 627c81983a0c3d7bfe8384dbf48909f4bcf300e36d2Dianne Hackborn continue; 628c81983a0c3d7bfe8384dbf48909f4bcf300e36d2Dianne Hackborn } 629a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.print("package=" + packageName); 630119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" u=" + userId); 631119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" bucket=" + appUsageHistory.currentBucket 632119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani + " reason=" 633119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani + UsageStatsManager.reasonToString(appUsageHistory.bucketingReason)); 634119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" used="); 63517fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani TimeUtils.formatDuration(totalElapsedTime - appUsageHistory.lastUsedElapsedTime, idpw); 636119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" usedScr="); 63717fffee4908f11038ba9cc5a672d15cb25be3dfeAmith Yamasani TimeUtils.formatDuration(screenOnTime - appUsageHistory.lastUsedScreenTime, idpw); 638119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" lastPred="); 639bd7b302f91f225f2dd2367cc37db9d2b75aec521Amith Yamasani TimeUtils.formatDuration(totalElapsedTime - appUsageHistory.lastPredictedTime, idpw); 640119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" activeLeft="); 641119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani TimeUtils.formatDuration(appUsageHistory.bucketActiveTimeoutTime - totalElapsedTime, 642bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani idpw); 643119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" wsLeft="); 644119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani TimeUtils.formatDuration(appUsageHistory.bucketWorkingSetTimeoutTime - totalElapsedTime, 645bbbad9cc0fc1def37bcec1fc8626e3c0ab6e3491Amith Yamasani idpw); 646119be9a5fc4033eba570ec94b94862401ee84570Amith Yamasani idpw.print(" lastJob="); 64753f06eae611ffa559f80d9efaddba17544eb7819Amith Yamasani TimeUtils.formatDuration(totalElapsedTime - appUsageHistory.lastJobRunTime, idpw); 64861d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani idpw.print(" idle=" + (isIdle(packageName, userId, elapsedRealtime) ? "y" : "n")); 649a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.println(); 650a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 651a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.println(); 652a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.print("totalElapsedTime="); 65361d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani TimeUtils.formatDuration(getElapsedTime(elapsedRealtime), idpw); 654a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.println(); 655a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.print("totalScreenOnTime="); 65661d5fd7fee3250bdf4b6ddfbccbd6bceae9436c6Amith Yamasani TimeUtils.formatDuration(getScreenOnTime(elapsedRealtime), idpw); 657a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.println(); 658a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani idpw.decreaseIndent(); 659a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani } 660a93542f9d341897f3206f775fd5720663b17504fAmith Yamasani} 661