Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 17 | package com.android.server.usage; |
| 18 | |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 19 | import android.app.usage.ConfigurationStats; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 20 | import android.app.usage.TimeSparseArray; |
| 21 | import android.app.usage.UsageEvents; |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 22 | import android.app.usage.UsageEvents.Event; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 23 | import android.app.usage.UsageStats; |
| 24 | import android.app.usage.UsageStatsManager; |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageInfo; |
| 26 | import android.content.pm.PackageManager; |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 28 | import android.os.SystemClock; |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.text.format.DateUtils; |
| 31 | import android.util.ArrayMap; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 32 | import android.util.ArraySet; |
| 33 | import android.util.Slog; |
| 34 | |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 35 | import com.android.internal.util.IndentingPrintWriter; |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 36 | import com.android.server.usage.UsageStatsDatabase.StatCombiner; |
| 37 | |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 38 | import java.io.File; |
| 39 | import java.io.IOException; |
| 40 | import java.text.SimpleDateFormat; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.Arrays; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 43 | import java.util.List; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * A per-user UsageStatsService. All methods are meant to be called with the main lock held |
| 47 | * in UsageStatsService. |
| 48 | */ |
| 49 | class UserUsageStatsService { |
| 50 | private static final String TAG = "UsageStatsService"; |
| 51 | private static final boolean DEBUG = UsageStatsService.DEBUG; |
| 52 | private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 53 | private static final int sDateFormatFlags = |
| 54 | DateUtils.FORMAT_SHOW_DATE |
| 55 | | DateUtils.FORMAT_SHOW_TIME |
| 56 | | DateUtils.FORMAT_SHOW_YEAR |
| 57 | | DateUtils.FORMAT_NUMERIC_DATE; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 59 | private final Context mContext; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 60 | private final UsageStatsDatabase mDatabase; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 61 | private final IntervalStats[] mCurrentStats; |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 62 | private IntervalStats mAppIdleRollingWindow; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 63 | private boolean mStatsChanged = false; |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 64 | private final UnixCalendar mDailyExpiryDate; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 65 | private final StatsUpdatedListener mListener; |
| 66 | private final String mLogPrefix; |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 67 | private final int mUserId; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 68 | |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 69 | private static final long[] INTERVAL_LENGTH = new long[] { |
| 70 | UnixCalendar.DAY_IN_MILLIS, UnixCalendar.WEEK_IN_MILLIS, |
| 71 | UnixCalendar.MONTH_IN_MILLIS, UnixCalendar.YEAR_IN_MILLIS |
| 72 | }; |
| 73 | |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 74 | interface StatsUpdatedListener { |
| 75 | void onStatsUpdated(); |
Adam Lesinski | f0ef3c1 | 2016-01-13 12:26:07 -0800 | [diff] [blame] | 76 | long getAppIdleRollingWindowDurationMillis(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 79 | UserUsageStatsService(Context context, int userId, File usageStatsDir, |
| 80 | StatsUpdatedListener listener) { |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 81 | mContext = context; |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 82 | mDailyExpiryDate = new UnixCalendar(0); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 83 | mDatabase = new UsageStatsDatabase(usageStatsDir); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 84 | mCurrentStats = new IntervalStats[UsageStatsManager.INTERVAL_COUNT]; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 85 | mListener = listener; |
| 86 | mLogPrefix = "User[" + Integer.toString(userId) + "] "; |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 87 | mUserId = userId; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 90 | void init(final long currentTimeMillis, final long deviceUsageTime) { |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 91 | mDatabase.init(currentTimeMillis); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 92 | |
| 93 | int nullCount = 0; |
| 94 | for (int i = 0; i < mCurrentStats.length; i++) { |
| 95 | mCurrentStats[i] = mDatabase.getLatestUsageStats(i); |
| 96 | if (mCurrentStats[i] == null) { |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 97 | // Find out how many intervals we don't have data for. |
| 98 | // Ideally it should be all or none. |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 99 | nullCount++; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (nullCount > 0) { |
| 104 | if (nullCount != mCurrentStats.length) { |
| 105 | // This is weird, but we shouldn't fail if something like this |
| 106 | // happens. |
| 107 | Slog.w(TAG, mLogPrefix + "Some stats have no latest available"); |
| 108 | } else { |
| 109 | // This must be first boot. |
| 110 | } |
| 111 | |
| 112 | // By calling loadActiveStats, we will |
| 113 | // generate new stats for each bucket. |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 114 | loadActiveStats(currentTimeMillis, /*resetBeginIdleTime=*/ false); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 115 | } else { |
| 116 | // Set up the expiry date to be one day from the latest daily stat. |
| 117 | // This may actually be today and we will rollover on the first event |
| 118 | // that is reported. |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 119 | updateRolloverDeadline(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Now close off any events that were open at the time this was saved. |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 123 | for (IntervalStats stat : mCurrentStats) { |
Adam Lesinski | 37a46b4 | 2014-09-05 15:38:05 -0700 | [diff] [blame] | 124 | final int pkgCount = stat.packageStats.size(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 125 | for (int i = 0; i < pkgCount; i++) { |
Adam Lesinski | 37a46b4 | 2014-09-05 15:38:05 -0700 | [diff] [blame] | 126 | UsageStats pkgStats = stat.packageStats.valueAt(i); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 127 | if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || |
| 128 | pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) { |
| 129 | stat.update(pkgStats.mPackageName, stat.lastTimeSaved, |
| 130 | UsageEvents.Event.END_OF_DAY); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 131 | notifyStatsChanged(); |
| 132 | } |
| 133 | } |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 134 | |
| 135 | stat.updateConfigurationStats(null, stat.lastTimeSaved); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 136 | } |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 137 | |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 138 | refreshAppIdleRollingWindow(currentTimeMillis, deviceUsageTime); |
| 139 | |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 140 | if (mDatabase.isNewUpdate()) { |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 141 | initializeDefaultsForApps(currentTimeMillis, deviceUsageTime, |
| 142 | mDatabase.isFirstUpdate()); |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * If any of the apps don't have a last-used entry, add one now. |
| 148 | * @param currentTimeMillis the current time |
| 149 | * @param firstUpdate if it is the first update, touch all installed apps, otherwise only |
| 150 | * touch the system apps |
| 151 | */ |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 152 | private void initializeDefaultsForApps(long currentTimeMillis, long deviceUsageTime, |
| 153 | boolean firstUpdate) { |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 154 | PackageManager pm = mContext.getPackageManager(); |
Jeff Sharkey | e06b4d1 | 2016-01-06 14:51:50 -0700 | [diff] [blame] | 155 | List<PackageInfo> packages = pm.getInstalledPackagesAsUser(0, mUserId); |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 156 | final int packageCount = packages.size(); |
| 157 | for (int i = 0; i < packageCount; i++) { |
| 158 | final PackageInfo pi = packages.get(i); |
| 159 | String packageName = pi.packageName; |
| 160 | if (pi.applicationInfo != null && (firstUpdate || pi.applicationInfo.isSystemApp()) |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 161 | && getBeginIdleTime(packageName) == -1) { |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 162 | for (IntervalStats stats : mCurrentStats) { |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 163 | stats.update(packageName, currentTimeMillis, Event.SYSTEM_INTERACTION); |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 164 | stats.updateBeginIdleTime(packageName, deviceUsageTime); |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 165 | } |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 166 | |
| 167 | mAppIdleRollingWindow.update(packageName, currentTimeMillis, |
| 168 | Event.SYSTEM_INTERACTION); |
| 169 | mAppIdleRollingWindow.updateBeginIdleTime(packageName, deviceUsageTime); |
| 170 | mStatsChanged = true; |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | // Persist the new OTA-related access stats. |
| 174 | persistActiveStats(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 177 | void onTimeChanged(long oldTime, long newTime, long deviceUsageTime, |
| 178 | boolean resetBeginIdleTime) { |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 179 | persistActiveStats(); |
| 180 | mDatabase.onTimeChanged(newTime - oldTime); |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 181 | loadActiveStats(newTime, resetBeginIdleTime); |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 182 | refreshAppIdleRollingWindow(newTime, deviceUsageTime); |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 185 | void reportEvent(UsageEvents.Event event, long deviceUsageTime) { |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 186 | if (DEBUG) { |
Adam Lesinski | 9d96075 | 2014-08-25 14:48:12 -0700 | [diff] [blame] | 187 | Slog.d(TAG, mLogPrefix + "Got usage event for " + event.mPackage |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 188 | + "[" + event.mTimeStamp + "]: " |
| 189 | + eventToString(event.mEventType)); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 192 | if (event.mTimeStamp >= mDailyExpiryDate.getTimeInMillis()) { |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 193 | // Need to rollover |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 194 | rolloverStats(event.mTimeStamp, deviceUsageTime); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 197 | final IntervalStats currentDailyStats = mCurrentStats[UsageStatsManager.INTERVAL_DAILY]; |
| 198 | |
| 199 | final Configuration newFullConfig = event.mConfiguration; |
| 200 | if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE && |
| 201 | currentDailyStats.activeConfiguration != null) { |
| 202 | // Make the event configuration a delta. |
| 203 | event.mConfiguration = Configuration.generateDelta( |
| 204 | currentDailyStats.activeConfiguration, newFullConfig); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 205 | } |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 206 | |
| 207 | // Add the event to the daily list. |
| 208 | if (currentDailyStats.events == null) { |
| 209 | currentDailyStats.events = new TimeSparseArray<>(); |
| 210 | } |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 211 | if (event.mEventType != UsageEvents.Event.SYSTEM_INTERACTION) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 212 | currentDailyStats.events.put(event.mTimeStamp, event); |
| 213 | } |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 214 | |
| 215 | for (IntervalStats stats : mCurrentStats) { |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 216 | if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) { |
| 217 | stats.updateConfigurationStats(newFullConfig, event.mTimeStamp); |
| 218 | } else { |
| 219 | stats.update(event.mPackage, event.mTimeStamp, event.mEventType); |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 220 | stats.updateBeginIdleTime(event.mPackage, deviceUsageTime); |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 221 | } |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 224 | if (event.mEventType != Event.CONFIGURATION_CHANGE) { |
| 225 | mAppIdleRollingWindow.update(event.mPackage, event.mTimeStamp, event.mEventType); |
| 226 | mAppIdleRollingWindow.updateBeginIdleTime(event.mPackage, deviceUsageTime); |
| 227 | } |
| 228 | |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 229 | notifyStatsChanged(); |
| 230 | } |
| 231 | |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 232 | /** |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 233 | * Sets the beginIdleTime for each of the intervals. |
| 234 | * @param beginIdleTime |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 235 | */ |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 236 | void setBeginIdleTime(String packageName, long beginIdleTime) { |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 237 | for (IntervalStats stats : mCurrentStats) { |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 238 | stats.updateBeginIdleTime(packageName, beginIdleTime); |
| 239 | } |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 240 | mAppIdleRollingWindow.updateBeginIdleTime(packageName, beginIdleTime); |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 241 | notifyStatsChanged(); |
| 242 | } |
| 243 | |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 244 | void setSystemLastUsedTime(String packageName, long lastUsedTime) { |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 245 | for (IntervalStats stats : mCurrentStats) { |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 246 | stats.updateSystemLastUsedTime(packageName, lastUsedTime); |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 247 | } |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 248 | mAppIdleRollingWindow.updateSystemLastUsedTime(packageName, lastUsedTime); |
Amith Yamasani | cf76872 | 2015-04-23 20:36:41 -0700 | [diff] [blame] | 249 | notifyStatsChanged(); |
| 250 | } |
| 251 | |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 252 | private static final StatCombiner<UsageStats> sUsageStatsCombiner = |
| 253 | new StatCombiner<UsageStats>() { |
| 254 | @Override |
| 255 | public void combine(IntervalStats stats, boolean mutable, |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 256 | List<UsageStats> accResult) { |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 257 | if (!mutable) { |
Adam Lesinski | 37a46b4 | 2014-09-05 15:38:05 -0700 | [diff] [blame] | 258 | accResult.addAll(stats.packageStats.values()); |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
Adam Lesinski | 37a46b4 | 2014-09-05 15:38:05 -0700 | [diff] [blame] | 262 | final int statCount = stats.packageStats.size(); |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 263 | for (int i = 0; i < statCount; i++) { |
Adam Lesinski | 37a46b4 | 2014-09-05 15:38:05 -0700 | [diff] [blame] | 264 | accResult.add(new UsageStats(stats.packageStats.valueAt(i))); |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | }; |
| 268 | |
| 269 | private static final StatCombiner<ConfigurationStats> sConfigStatsCombiner = |
| 270 | new StatCombiner<ConfigurationStats>() { |
| 271 | @Override |
| 272 | public void combine(IntervalStats stats, boolean mutable, |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 273 | List<ConfigurationStats> accResult) { |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 274 | if (!mutable) { |
| 275 | accResult.addAll(stats.configurations.values()); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | final int configCount = stats.configurations.size(); |
| 280 | for (int i = 0; i < configCount; i++) { |
| 281 | accResult.add(new ConfigurationStats(stats.configurations.valueAt(i))); |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | /** |
| 287 | * Generic query method that selects the appropriate IntervalStats for the specified time range |
| 288 | * and bucket, then calls the {@link com.android.server.usage.UsageStatsDatabase.StatCombiner} |
| 289 | * provided to select the stats to use from the IntervalStats object. |
| 290 | */ |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 291 | private <T> List<T> queryStats(int intervalType, final long beginTime, final long endTime, |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 292 | StatCombiner<T> combiner) { |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 293 | if (intervalType == UsageStatsManager.INTERVAL_BEST) { |
| 294 | intervalType = mDatabase.findBestFitBucket(beginTime, endTime); |
| 295 | if (intervalType < 0) { |
| 296 | // Nothing saved to disk yet, so every stat is just as equal (no rollover has |
| 297 | // occurred. |
| 298 | intervalType = UsageStatsManager.INTERVAL_DAILY; |
| 299 | } |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 302 | if (intervalType < 0 || intervalType >= mCurrentStats.length) { |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 303 | if (DEBUG) { |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 304 | Slog.d(TAG, mLogPrefix + "Bad intervalType used " + intervalType); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 305 | } |
| 306 | return null; |
| 307 | } |
| 308 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 309 | final IntervalStats currentStats = mCurrentStats[intervalType]; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 310 | |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 311 | if (DEBUG) { |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 312 | Slog.d(TAG, mLogPrefix + "SELECT * FROM " + intervalType + " WHERE beginTime >= " |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 313 | + beginTime + " AND endTime < " + endTime); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 316 | if (beginTime >= currentStats.endTime) { |
| 317 | if (DEBUG) { |
| 318 | Slog.d(TAG, mLogPrefix + "Requesting stats after " + beginTime + " but latest is " |
| 319 | + currentStats.endTime); |
| 320 | } |
| 321 | // Nothing newer available. |
| 322 | return null; |
| 323 | } |
| 324 | |
| 325 | // Truncate the endTime to just before the in-memory stats. Then, we'll append the |
| 326 | // in-memory stats to the results (if necessary) so as to avoid writing to disk too |
| 327 | // often. |
| 328 | final long truncatedEndTime = Math.min(currentStats.beginTime, endTime); |
| 329 | |
| 330 | // Get the stats from disk. |
| 331 | List<T> results = mDatabase.queryUsageStats(intervalType, beginTime, |
| 332 | truncatedEndTime, combiner); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 333 | if (DEBUG) { |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 334 | Slog.d(TAG, "Got " + (results != null ? results.size() : 0) + " results from disk"); |
| 335 | Slog.d(TAG, "Current stats beginTime=" + currentStats.beginTime + |
| 336 | " endTime=" + currentStats.endTime); |
| 337 | } |
| 338 | |
| 339 | // Now check if the in-memory stats match the range and add them if they do. |
| 340 | if (beginTime < currentStats.endTime && endTime > currentStats.beginTime) { |
| 341 | if (DEBUG) { |
| 342 | Slog.d(TAG, mLogPrefix + "Returning in-memory stats"); |
| 343 | } |
| 344 | |
| 345 | if (results == null) { |
| 346 | results = new ArrayList<>(); |
| 347 | } |
| 348 | combiner.combine(currentStats, true, results); |
| 349 | } |
| 350 | |
| 351 | if (DEBUG) { |
| 352 | Slog.d(TAG, mLogPrefix + "Results: " + (results != null ? results.size() : 0)); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 353 | } |
| 354 | return results; |
| 355 | } |
| 356 | |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 357 | List<UsageStats> queryUsageStats(int bucketType, long beginTime, long endTime) { |
| 358 | return queryStats(bucketType, beginTime, endTime, sUsageStatsCombiner); |
| 359 | } |
| 360 | |
| 361 | List<ConfigurationStats> queryConfigurationStats(int bucketType, long beginTime, long endTime) { |
| 362 | return queryStats(bucketType, beginTime, endTime, sConfigStatsCombiner); |
| 363 | } |
| 364 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 365 | UsageEvents queryEvents(final long beginTime, final long endTime) { |
| 366 | final ArraySet<String> names = new ArraySet<>(); |
| 367 | List<UsageEvents.Event> results = queryStats(UsageStatsManager.INTERVAL_DAILY, |
| 368 | beginTime, endTime, new StatCombiner<UsageEvents.Event>() { |
| 369 | @Override |
| 370 | public void combine(IntervalStats stats, boolean mutable, |
| 371 | List<UsageEvents.Event> accumulatedResult) { |
| 372 | if (stats.events == null) { |
| 373 | return; |
| 374 | } |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 375 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 376 | final int startIndex = stats.events.closestIndexOnOrAfter(beginTime); |
| 377 | if (startIndex < 0) { |
| 378 | return; |
| 379 | } |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 380 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 381 | final int size = stats.events.size(); |
| 382 | for (int i = startIndex; i < size; i++) { |
| 383 | if (stats.events.keyAt(i) >= endTime) { |
| 384 | return; |
| 385 | } |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 386 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 387 | final UsageEvents.Event event = stats.events.valueAt(i); |
| 388 | names.add(event.mPackage); |
| 389 | if (event.mClass != null) { |
| 390 | names.add(event.mClass); |
| 391 | } |
| 392 | accumulatedResult.add(event); |
| 393 | } |
| 394 | } |
| 395 | }); |
| 396 | |
| 397 | if (results == null || results.isEmpty()) { |
| 398 | return null; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 401 | String[] table = names.toArray(new String[names.size()]); |
| 402 | Arrays.sort(table); |
| 403 | return new UsageEvents(results, table); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 406 | long getBeginIdleTime(String packageName) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 407 | UsageStats packageUsage; |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 408 | if ((packageUsage = mAppIdleRollingWindow.packageStats.get(packageName)) == null) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 409 | return -1; |
| 410 | } else { |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 411 | return packageUsage.getBeginIdleTime(); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 415 | long getSystemLastUsedTime(String packageName) { |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 416 | UsageStats packageUsage; |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 417 | if ((packageUsage = mAppIdleRollingWindow.packageStats.get(packageName)) == null) { |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 418 | return -1; |
| 419 | } else { |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 420 | return packageUsage.getLastTimeSystemUsed(); |
Amith Yamasani | 547116e | 2015-05-19 16:51:30 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 424 | void persistActiveStats() { |
| 425 | if (mStatsChanged) { |
| 426 | Slog.i(TAG, mLogPrefix + "Flushing usage stats to disk"); |
| 427 | try { |
| 428 | for (int i = 0; i < mCurrentStats.length; i++) { |
| 429 | mDatabase.putUsageStats(i, mCurrentStats[i]); |
| 430 | } |
| 431 | mStatsChanged = false; |
| 432 | } catch (IOException e) { |
| 433 | Slog.e(TAG, mLogPrefix + "Failed to persist active stats", e); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 438 | private void rolloverStats(final long currentTimeMillis, final long deviceUsageTime) { |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 439 | final long startTime = SystemClock.elapsedRealtime(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 440 | Slog.i(TAG, mLogPrefix + "Rolling over usage stats"); |
| 441 | |
| 442 | // Finish any ongoing events with an END_OF_DAY event. Make a note of which components |
| 443 | // need a new CONTINUE_PREVIOUS_DAY entry. |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 444 | final Configuration previousConfig = |
| 445 | mCurrentStats[UsageStatsManager.INTERVAL_DAILY].activeConfiguration; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 446 | ArraySet<String> continuePreviousDay = new ArraySet<>(); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 447 | for (IntervalStats stat : mCurrentStats) { |
Adam Lesinski | 37a46b4 | 2014-09-05 15:38:05 -0700 | [diff] [blame] | 448 | final int pkgCount = stat.packageStats.size(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 449 | for (int i = 0; i < pkgCount; i++) { |
Adam Lesinski | 37a46b4 | 2014-09-05 15:38:05 -0700 | [diff] [blame] | 450 | UsageStats pkgStats = stat.packageStats.valueAt(i); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 451 | if (pkgStats.mLastEvent == UsageEvents.Event.MOVE_TO_FOREGROUND || |
| 452 | pkgStats.mLastEvent == UsageEvents.Event.CONTINUE_PREVIOUS_DAY) { |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 453 | continuePreviousDay.add(pkgStats.mPackageName); |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 454 | stat.update(pkgStats.mPackageName, mDailyExpiryDate.getTimeInMillis() - 1, |
| 455 | UsageEvents.Event.END_OF_DAY); |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 456 | notifyStatsChanged(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 459 | |
| 460 | stat.updateConfigurationStats(null, mDailyExpiryDate.getTimeInMillis() - 1); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | persistActiveStats(); |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 464 | mDatabase.prune(currentTimeMillis); |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 465 | loadActiveStats(currentTimeMillis, /*resetBeginIdleTime=*/ false); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 466 | |
| 467 | final int continueCount = continuePreviousDay.size(); |
| 468 | for (int i = 0; i < continueCount; i++) { |
| 469 | String name = continuePreviousDay.valueAt(i); |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 470 | final long beginTime = mCurrentStats[UsageStatsManager.INTERVAL_DAILY].beginTime; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 471 | for (IntervalStats stat : mCurrentStats) { |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 472 | stat.update(name, beginTime, UsageEvents.Event.CONTINUE_PREVIOUS_DAY); |
| 473 | stat.updateConfigurationStats(previousConfig, beginTime); |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 474 | notifyStatsChanged(); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | persistActiveStats(); |
| 478 | |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 479 | refreshAppIdleRollingWindow(currentTimeMillis, deviceUsageTime); |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 480 | |
Adam Lesinski | 66143fa | 2014-09-11 08:31:05 -0700 | [diff] [blame] | 481 | final long totalTime = SystemClock.elapsedRealtime() - startTime; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 482 | Slog.i(TAG, mLogPrefix + "Rolling over usage stats complete. Took " + totalTime |
| 483 | + " milliseconds"); |
| 484 | } |
| 485 | |
| 486 | private void notifyStatsChanged() { |
| 487 | if (!mStatsChanged) { |
| 488 | mStatsChanged = true; |
| 489 | mListener.onStatsUpdated(); |
| 490 | } |
| 491 | } |
| 492 | |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 493 | private void loadActiveStats(final long currentTimeMillis, boolean resetBeginIdleTime) { |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 494 | for (int intervalType = 0; intervalType < mCurrentStats.length; intervalType++) { |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 495 | final IntervalStats stats = mDatabase.getLatestUsageStats(intervalType); |
| 496 | if (stats != null && currentTimeMillis - 500 >= stats.endTime && |
| 497 | currentTimeMillis < stats.beginTime + INTERVAL_LENGTH[intervalType]) { |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 498 | if (DEBUG) { |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 499 | Slog.d(TAG, mLogPrefix + "Loading existing stats @ " + |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 500 | sDateFormat.format(stats.beginTime) + "(" + stats.beginTime + |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 501 | ") for interval " + intervalType); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 502 | } |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 503 | mCurrentStats[intervalType] = stats; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 504 | } else { |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 505 | // No good fit remains. |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 506 | if (DEBUG) { |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 507 | Slog.d(TAG, "Creating new stats @ " + |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 508 | sDateFormat.format(currentTimeMillis) + "(" + |
| 509 | currentTimeMillis + ") for interval " + intervalType); |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 510 | } |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 511 | |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 512 | mCurrentStats[intervalType] = new IntervalStats(); |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 513 | mCurrentStats[intervalType].beginTime = currentTimeMillis; |
| 514 | mCurrentStats[intervalType].endTime = currentTimeMillis + 1; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 515 | } |
Amith Yamasani | 520d8f2 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 516 | |
| 517 | if (resetBeginIdleTime) { |
| 518 | for (UsageStats usageStats : mCurrentStats[intervalType].packageStats.values()) { |
| 519 | usageStats.mBeginIdleTime = 0; |
| 520 | } |
| 521 | } |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 522 | } |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 523 | |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 524 | mStatsChanged = false; |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 525 | updateRolloverDeadline(); |
| 526 | } |
| 527 | |
| 528 | private void updateRolloverDeadline() { |
| 529 | mDailyExpiryDate.setTimeInMillis( |
| 530 | mCurrentStats[UsageStatsManager.INTERVAL_DAILY].beginTime); |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 531 | mDailyExpiryDate.addDays(1); |
Adam Lesinski | d26bea3 | 2014-09-03 16:49:59 -0700 | [diff] [blame] | 532 | Slog.i(TAG, mLogPrefix + "Rollover scheduled @ " + |
| 533 | sDateFormat.format(mDailyExpiryDate.getTimeInMillis()) + "(" + |
Adam Lesinski | 7cba1d4 | 2015-08-04 16:17:37 -0700 | [diff] [blame] | 534 | mDailyExpiryDate.getTimeInMillis() + ")"); |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 537 | private static void mergePackageStats(IntervalStats dst, IntervalStats src, |
| 538 | final long deviceUsageTime) { |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 539 | dst.endTime = Math.max(dst.endTime, src.endTime); |
| 540 | |
| 541 | final int srcPackageCount = src.packageStats.size(); |
| 542 | for (int i = 0; i < srcPackageCount; i++) { |
| 543 | final String packageName = src.packageStats.keyAt(i); |
| 544 | final UsageStats srcStats = src.packageStats.valueAt(i); |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 545 | UsageStats dstStats = dst.packageStats.get(packageName); |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 546 | if (dstStats == null) { |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 547 | dstStats = new UsageStats(srcStats); |
| 548 | dst.packageStats.put(packageName, dstStats); |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 549 | } else { |
| 550 | dstStats.add(src.packageStats.valueAt(i)); |
| 551 | } |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 552 | |
| 553 | // App idle times can not begin in the future. This happens if we had a time change. |
| 554 | if (dstStats.mBeginIdleTime > deviceUsageTime) { |
| 555 | dstStats.mBeginIdleTime = deviceUsageTime; |
| 556 | } |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 561 | * App idle operates on a rolling window of time. When we roll over time, we end up with a |
| 562 | * period of time where in-memory stats are empty and we don't hit the disk for older stats |
| 563 | * for performance reasons. Suddenly all apps will become idle. |
| 564 | * |
| 565 | * Instead, at times we do a deep query to find all the apps that have run in the past few |
| 566 | * days and keep the cached data up to date. |
| 567 | * |
| 568 | * @param currentTimeMillis |
| 569 | */ |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 570 | void refreshAppIdleRollingWindow(final long currentTimeMillis, final long deviceUsageTime) { |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 571 | // Start the rolling window for AppIdle requests. |
Adam Lesinski | f0ef3c1 | 2016-01-13 12:26:07 -0800 | [diff] [blame] | 572 | final long startRangeMillis = currentTimeMillis - |
| 573 | mListener.getAppIdleRollingWindowDurationMillis(); |
| 574 | |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 575 | List<IntervalStats> stats = mDatabase.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, |
Adam Lesinski | f0ef3c1 | 2016-01-13 12:26:07 -0800 | [diff] [blame] | 576 | startRangeMillis, currentTimeMillis, new StatCombiner<IntervalStats>() { |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 577 | @Override |
| 578 | public void combine(IntervalStats stats, boolean mutable, |
| 579 | List<IntervalStats> accumulatedResult) { |
| 580 | IntervalStats accum; |
| 581 | if (accumulatedResult.isEmpty()) { |
| 582 | accum = new IntervalStats(); |
| 583 | accum.beginTime = stats.beginTime; |
| 584 | accumulatedResult.add(accum); |
| 585 | } else { |
| 586 | accum = accumulatedResult.get(0); |
| 587 | } |
| 588 | |
| 589 | mergePackageStats(accum, stats, deviceUsageTime); |
| 590 | } |
| 591 | }); |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 592 | |
| 593 | if (stats == null || stats.isEmpty()) { |
| 594 | mAppIdleRollingWindow = new IntervalStats(); |
| 595 | mergePackageStats(mAppIdleRollingWindow, |
Adam Lesinski | 7856f3c | 2016-01-13 11:03:35 -0800 | [diff] [blame] | 596 | mCurrentStats[UsageStatsManager.INTERVAL_YEARLY], deviceUsageTime); |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 597 | } else { |
| 598 | mAppIdleRollingWindow = stats.get(0); |
| 599 | } |
| 600 | } |
| 601 | |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 602 | // |
| 603 | // -- DUMP related methods -- |
| 604 | // |
| 605 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 606 | void checkin(final IndentingPrintWriter pw, final long screenOnTime) { |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 607 | mDatabase.checkinDailyFiles(new UsageStatsDatabase.CheckinAction() { |
| 608 | @Override |
| 609 | public boolean checkin(IntervalStats stats) { |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 610 | printIntervalStats(pw, stats, screenOnTime, false); |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 611 | return true; |
| 612 | } |
| 613 | }); |
| 614 | } |
| 615 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 616 | void dump(IndentingPrintWriter pw, final long screenOnTime) { |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 617 | // This is not a check-in, only dump in-memory stats. |
| 618 | for (int interval = 0; interval < mCurrentStats.length; interval++) { |
| 619 | pw.print("In-memory "); |
| 620 | pw.print(intervalToString(interval)); |
| 621 | pw.println(" stats"); |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 622 | printIntervalStats(pw, mCurrentStats[interval], screenOnTime, true); |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 623 | } |
Adam Lesinski | da4a377 | 2016-01-07 18:24:53 -0800 | [diff] [blame] | 624 | |
| 625 | pw.println("AppIdleRollingWindow cache"); |
| 626 | printIntervalStats(pw, mAppIdleRollingWindow, screenOnTime, true); |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | private String formatDateTime(long dateTime, boolean pretty) { |
| 630 | if (pretty) { |
| 631 | return "\"" + DateUtils.formatDateTime(mContext, dateTime, sDateFormatFlags) + "\""; |
| 632 | } |
| 633 | return Long.toString(dateTime); |
| 634 | } |
| 635 | |
| 636 | private String formatElapsedTime(long elapsedTime, boolean pretty) { |
| 637 | if (pretty) { |
| 638 | return "\"" + DateUtils.formatElapsedTime(elapsedTime / 1000) + "\""; |
| 639 | } |
| 640 | return Long.toString(elapsedTime); |
| 641 | } |
| 642 | |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 643 | void printIntervalStats(IndentingPrintWriter pw, IntervalStats stats, long screenOnTime, |
| 644 | boolean prettyDates) { |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 645 | if (prettyDates) { |
| 646 | pw.printPair("timeRange", "\"" + DateUtils.formatDateRange(mContext, |
| 647 | stats.beginTime, stats.endTime, sDateFormatFlags) + "\""); |
| 648 | } else { |
| 649 | pw.printPair("beginTime", stats.beginTime); |
| 650 | pw.printPair("endTime", stats.endTime); |
| 651 | } |
| 652 | pw.println(); |
| 653 | pw.increaseIndent(); |
| 654 | pw.println("packages"); |
| 655 | pw.increaseIndent(); |
| 656 | final ArrayMap<String, UsageStats> pkgStats = stats.packageStats; |
| 657 | final int pkgCount = pkgStats.size(); |
| 658 | for (int i = 0; i < pkgCount; i++) { |
| 659 | final UsageStats usageStats = pkgStats.valueAt(i); |
| 660 | pw.printPair("package", usageStats.mPackageName); |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 661 | pw.printPair("totalTime", |
| 662 | formatElapsedTime(usageStats.mTotalTimeInForeground, prettyDates)); |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 663 | pw.printPair("lastTime", formatDateTime(usageStats.mLastTimeUsed, prettyDates)); |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 664 | pw.printPair("lastTimeSystem", |
| 665 | formatDateTime(usageStats.mLastTimeSystemUsed, prettyDates)); |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 666 | pw.printPair("inactiveTime", |
| 667 | formatElapsedTime(screenOnTime - usageStats.mBeginIdleTime, prettyDates)); |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 668 | pw.println(); |
| 669 | } |
| 670 | pw.decreaseIndent(); |
| 671 | |
| 672 | pw.println("configurations"); |
| 673 | pw.increaseIndent(); |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 674 | final ArrayMap<Configuration, ConfigurationStats> configStats = stats.configurations; |
Adam Lesinski | 1bb18c4 | 2014-08-18 12:21:34 -0700 | [diff] [blame] | 675 | final int configCount = configStats.size(); |
| 676 | for (int i = 0; i < configCount; i++) { |
| 677 | final ConfigurationStats config = configStats.valueAt(i); |
| 678 | pw.printPair("config", Configuration.resourceQualifierString(config.mConfiguration)); |
| 679 | pw.printPair("totalTime", formatElapsedTime(config.mTotalTimeActive, prettyDates)); |
| 680 | pw.printPair("lastTime", formatDateTime(config.mLastTimeActive, prettyDates)); |
| 681 | pw.printPair("count", config.mActivationCount); |
| 682 | pw.println(); |
| 683 | } |
| 684 | pw.decreaseIndent(); |
| 685 | |
| 686 | pw.println("events"); |
| 687 | pw.increaseIndent(); |
| 688 | final TimeSparseArray<UsageEvents.Event> events = stats.events; |
| 689 | final int eventCount = events != null ? events.size() : 0; |
| 690 | for (int i = 0; i < eventCount; i++) { |
| 691 | final UsageEvents.Event event = events.valueAt(i); |
| 692 | pw.printPair("time", formatDateTime(event.mTimeStamp, prettyDates)); |
| 693 | pw.printPair("type", eventToString(event.mEventType)); |
| 694 | pw.printPair("package", event.mPackage); |
| 695 | if (event.mClass != null) { |
| 696 | pw.printPair("class", event.mClass); |
| 697 | } |
| 698 | if (event.mConfiguration != null) { |
| 699 | pw.printPair("config", Configuration.resourceQualifierString(event.mConfiguration)); |
| 700 | } |
| 701 | pw.println(); |
| 702 | } |
| 703 | pw.decreaseIndent(); |
| 704 | pw.decreaseIndent(); |
| 705 | } |
| 706 | |
| 707 | private static String intervalToString(int interval) { |
| 708 | switch (interval) { |
| 709 | case UsageStatsManager.INTERVAL_DAILY: |
| 710 | return "daily"; |
| 711 | case UsageStatsManager.INTERVAL_WEEKLY: |
| 712 | return "weekly"; |
| 713 | case UsageStatsManager.INTERVAL_MONTHLY: |
| 714 | return "monthly"; |
| 715 | case UsageStatsManager.INTERVAL_YEARLY: |
| 716 | return "yearly"; |
| 717 | default: |
| 718 | return "?"; |
| 719 | } |
| 720 | } |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 721 | |
| 722 | private static String eventToString(int eventType) { |
| 723 | switch (eventType) { |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 724 | case UsageEvents.Event.NONE: |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 725 | return "NONE"; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 726 | case UsageEvents.Event.MOVE_TO_BACKGROUND: |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 727 | return "MOVE_TO_BACKGROUND"; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 728 | case UsageEvents.Event.MOVE_TO_FOREGROUND: |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 729 | return "MOVE_TO_FOREGROUND"; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 730 | case UsageEvents.Event.END_OF_DAY: |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 731 | return "END_OF_DAY"; |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 732 | case UsageEvents.Event.CONTINUE_PREVIOUS_DAY: |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 733 | return "CONTINUE_PREVIOUS_DAY"; |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 734 | case UsageEvents.Event.CONFIGURATION_CHANGE: |
| 735 | return "CONFIGURATION_CHANGE"; |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 736 | case UsageEvents.Event.SYSTEM_INTERACTION: |
| 737 | return "SYSTEM_INTERACTION"; |
| 738 | case UsageEvents.Event.USER_INTERACTION: |
| 739 | return "USER_INTERACTION"; |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 740 | default: |
| 741 | return "UNKNOWN"; |
| 742 | } |
| 743 | } |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame^] | 744 | |
| 745 | byte[] getBackupPayload(String key){ |
| 746 | return mDatabase.getBackupPayload(key); |
| 747 | } |
| 748 | |
| 749 | void applyRestoredPayload(String key, byte[] payload){ |
| 750 | mDatabase.applyRestoredPayload(key, payload); |
| 751 | } |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 752 | } |