The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006-2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 19 | import android.app.AppGlobals; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 22 | import android.content.pm.IPackageManager; |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 23 | import android.content.pm.PackageInfo; |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.os.Binder; |
| 26 | import android.os.IBinder; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 27 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.Process; |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.os.ServiceManager; |
| 32 | import android.os.SystemClock; |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 33 | import android.util.ArrayMap; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 34 | import android.util.AtomicFile; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 35 | import android.util.Slog; |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 36 | import android.util.Xml; |
| 37 | |
| 38 | import com.android.internal.app.IUsageStats; |
| 39 | import com.android.internal.content.PackageMonitor; |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 40 | import com.android.internal.os.PkgUsageStats; |
| 41 | import com.android.internal.util.FastXmlSerializer; |
| 42 | |
| 43 | import org.xmlpull.v1.XmlPullParser; |
| 44 | import org.xmlpull.v1.XmlPullParserException; |
| 45 | import org.xmlpull.v1.XmlSerializer; |
| 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import java.io.File; |
| 48 | import java.io.FileDescriptor; |
| 49 | import java.io.FileInputStream; |
| 50 | import java.io.FileNotFoundException; |
| 51 | import java.io.FileOutputStream; |
| 52 | import java.io.IOException; |
| 53 | import java.io.PrintWriter; |
| 54 | import java.util.ArrayList; |
| 55 | import java.util.Calendar; |
| 56 | import java.util.Collections; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import java.util.HashMap; |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 58 | import java.util.HashSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import java.util.List; |
| 60 | import java.util.Map; |
| 61 | import java.util.Set; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 62 | import java.util.TimeZone; |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 63 | import java.util.concurrent.atomic.AtomicBoolean; |
| 64 | import java.util.concurrent.atomic.AtomicInteger; |
| 65 | import java.util.concurrent.atomic.AtomicLong; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * This service collects the statistics associated with usage |
| 69 | * of various components, like when a particular package is launched or |
| 70 | * paused and aggregates events like number of time a component is launched |
| 71 | * total duration of a component launch. |
| 72 | */ |
| 73 | public final class UsageStatsService extends IUsageStats.Stub { |
| 74 | public static final String SERVICE_NAME = "usagestats"; |
| 75 | private static final boolean localLOGV = false; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 76 | private static final boolean REPORT_UNEXPECTED = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | private static final String TAG = "UsageStats"; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 78 | |
| 79 | // Current on-disk Parcel version |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 80 | private static final int VERSION = 1008; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 81 | |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 82 | private static final int CHECKIN_VERSION = 4; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 83 | |
| 84 | private static final String FILE_PREFIX = "usage-"; |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 85 | |
| 86 | private static final String FILE_HISTORY = FILE_PREFIX + "history.xml"; |
| 87 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 88 | private static final int FILE_WRITE_INTERVAL = 30*60*1000; //ms |
| 89 | |
| 90 | private static final int MAX_NUM_FILES = 5; |
| 91 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 92 | private static final int NUM_LAUNCH_TIME_BINS = 10; |
| 93 | private static final int[] LAUNCH_TIME_BINS = { |
| 94 | 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 5000 |
| 95 | }; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | static IUsageStats sService; |
| 98 | private Context mContext; |
| 99 | // structure used to maintain statistics since the last checkin. |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 100 | final private ArrayMap<String, PkgUsageStatsExtended> mStats; |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 101 | |
| 102 | // Maintains the last time any component was resumed, for all time. |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 103 | final private ArrayMap<String, ArrayMap<String, Long>> mLastResumeTimes; |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 104 | |
| 105 | // To remove last-resume time stats when a pacakge is removed. |
| 106 | private PackageMonitor mPackageMonitor; |
| 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | // Lock to update package stats. Methods suffixed by SLOCK should invoked with |
| 109 | // this lock held |
| 110 | final Object mStatsLock; |
| 111 | // Lock to write to file. Methods suffixed by FLOCK should invoked with |
| 112 | // this lock held. |
| 113 | final Object mFileLock; |
| 114 | // Order of locks is mFileLock followed by mStatsLock to avoid deadlocks |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 115 | private String mLastResumedPkg; |
| 116 | private String mLastResumedComp; |
| 117 | private boolean mIsResumed; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | private File mFile; |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 119 | private AtomicFile mHistoryFile; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 120 | private String mFileLeaf; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 121 | private File mDir; |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 122 | |
| 123 | private Calendar mCal; // guarded by itself |
| 124 | |
| 125 | private final AtomicInteger mLastWriteDay = new AtomicInteger(-1); |
| 126 | private final AtomicLong mLastWriteElapsedTime = new AtomicLong(0); |
| 127 | private final AtomicBoolean mUnforcedDiskWriteRunning = new AtomicBoolean(false); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 128 | |
| 129 | static class TimeStats { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 130 | int count; |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 131 | int[] times = new int[NUM_LAUNCH_TIME_BINS]; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 132 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 133 | TimeStats() { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 136 | void incCount() { |
| 137 | count++; |
| 138 | } |
| 139 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 140 | void add(int val) { |
| 141 | final int[] bins = LAUNCH_TIME_BINS; |
| 142 | for (int i=0; i<NUM_LAUNCH_TIME_BINS-1; i++) { |
| 143 | if (val < bins[i]) { |
| 144 | times[i]++; |
| 145 | return; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 146 | } |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 147 | } |
| 148 | times[NUM_LAUNCH_TIME_BINS-1]++; |
| 149 | } |
| 150 | |
| 151 | TimeStats(Parcel in) { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 152 | count = in.readInt(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 153 | final int[] localTimes = times; |
| 154 | for (int i=0; i<NUM_LAUNCH_TIME_BINS; i++) { |
| 155 | localTimes[i] = in.readInt(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void writeToParcel(Parcel out) { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 160 | out.writeInt(count); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 161 | final int[] localTimes = times; |
| 162 | for (int i=0; i<NUM_LAUNCH_TIME_BINS; i++) { |
| 163 | out.writeInt(localTimes[i]); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
| 168 | private class PkgUsageStatsExtended { |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 169 | final ArrayMap<String, TimeStats> mLaunchTimes |
| 170 | = new ArrayMap<String, TimeStats>(); |
| 171 | final ArrayMap<String, TimeStats> mFullyDrawnTimes |
| 172 | = new ArrayMap<String, TimeStats>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | int mLaunchCount; |
| 174 | long mUsageTime; |
| 175 | long mPausedTime; |
| 176 | long mResumedTime; |
| 177 | |
| 178 | PkgUsageStatsExtended() { |
| 179 | mLaunchCount = 0; |
| 180 | mUsageTime = 0; |
| 181 | } |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 182 | |
| 183 | PkgUsageStatsExtended(Parcel in) { |
| 184 | mLaunchCount = in.readInt(); |
| 185 | mUsageTime = in.readLong(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 186 | if (localLOGV) Slog.v(TAG, "Launch count: " + mLaunchCount |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 187 | + ", Usage time:" + mUsageTime); |
| 188 | |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 189 | final int numLaunchTimeStats = in.readInt(); |
| 190 | if (localLOGV) Slog.v(TAG, "Reading launch times: " + numLaunchTimeStats); |
| 191 | mLaunchTimes.ensureCapacity(numLaunchTimeStats); |
| 192 | for (int i=0; i<numLaunchTimeStats; i++) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 193 | String comp = in.readString(); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 194 | if (localLOGV) Slog.v(TAG, "Component: " + comp); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 195 | TimeStats times = new TimeStats(in); |
| 196 | mLaunchTimes.put(comp, times); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 197 | } |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 198 | |
| 199 | final int numFullyDrawnTimeStats = in.readInt(); |
| 200 | if (localLOGV) Slog.v(TAG, "Reading fully drawn times: " + numFullyDrawnTimeStats); |
| 201 | mFullyDrawnTimes.ensureCapacity(numFullyDrawnTimeStats); |
| 202 | for (int i=0; i<numFullyDrawnTimeStats; i++) { |
| 203 | String comp = in.readString(); |
| 204 | if (localLOGV) Slog.v(TAG, "Component: " + comp); |
| 205 | TimeStats times = new TimeStats(in); |
| 206 | mFullyDrawnTimes.put(comp, times); |
| 207 | } |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 208 | } |
Mark Brophy | 9fc0330 | 2011-07-01 16:56:24 +0100 | [diff] [blame] | 209 | |
| 210 | void updateResume(String comp, boolean launched) { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 211 | if (launched) { |
| 212 | mLaunchCount ++; |
| 213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | mResumedTime = SystemClock.elapsedRealtime(); |
| 215 | } |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | void updatePause() { |
| 218 | mPausedTime = SystemClock.elapsedRealtime(); |
| 219 | mUsageTime += (mPausedTime - mResumedTime); |
| 220 | } |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 221 | |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 222 | void addLaunchCount(String comp) { |
| 223 | TimeStats times = mLaunchTimes.get(comp); |
| 224 | if (times == null) { |
| 225 | times = new TimeStats(); |
| 226 | mLaunchTimes.put(comp, times); |
| 227 | } |
| 228 | times.incCount(); |
| 229 | } |
| 230 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 231 | void addLaunchTime(String comp, int millis) { |
| 232 | TimeStats times = mLaunchTimes.get(comp); |
| 233 | if (times == null) { |
| 234 | times = new TimeStats(); |
| 235 | mLaunchTimes.put(comp, times); |
| 236 | } |
| 237 | times.add(millis); |
| 238 | } |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 239 | |
| 240 | void addFullyDrawnTime(String comp, int millis) { |
| 241 | TimeStats times = mFullyDrawnTimes.get(comp); |
| 242 | if (times == null) { |
| 243 | times = new TimeStats(); |
| 244 | mFullyDrawnTimes.put(comp, times); |
| 245 | } |
| 246 | times.add(millis); |
| 247 | } |
| 248 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 249 | void writeToParcel(Parcel out) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 250 | out.writeInt(mLaunchCount); |
| 251 | out.writeLong(mUsageTime); |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 252 | final int numLaunchTimeStats = mLaunchTimes.size(); |
| 253 | out.writeInt(numLaunchTimeStats); |
| 254 | for (int i=0; i<numLaunchTimeStats; i++) { |
| 255 | out.writeString(mLaunchTimes.keyAt(i)); |
| 256 | mLaunchTimes.valueAt(i).writeToParcel(out); |
| 257 | } |
| 258 | final int numFullyDrawnTimeStats = mFullyDrawnTimes.size(); |
| 259 | out.writeInt(numFullyDrawnTimeStats); |
| 260 | for (int i=0; i<numFullyDrawnTimeStats; i++) { |
| 261 | out.writeString(mFullyDrawnTimes.keyAt(i)); |
| 262 | mFullyDrawnTimes.valueAt(i).writeToParcel(out); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | void clear() { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 267 | mLaunchTimes.clear(); |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 268 | mFullyDrawnTimes.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | mLaunchCount = 0; |
| 270 | mUsageTime = 0; |
| 271 | } |
| 272 | } |
| 273 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 274 | UsageStatsService(String dir) { |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 275 | mStats = new ArrayMap<String, PkgUsageStatsExtended>(); |
| 276 | mLastResumeTimes = new ArrayMap<String, ArrayMap<String, Long>>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | mStatsLock = new Object(); |
| 278 | mFileLock = new Object(); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 279 | mDir = new File(dir); |
| 280 | mCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0")); |
| 281 | |
| 282 | mDir.mkdir(); |
| 283 | |
| 284 | // Remove any old usage files from previous versions. |
| 285 | File parentDir = mDir.getParentFile(); |
| 286 | String fList[] = parentDir.list(); |
| 287 | if (fList != null) { |
| 288 | String prefix = mDir.getName() + "."; |
| 289 | int i = fList.length; |
| 290 | while (i > 0) { |
| 291 | i--; |
| 292 | if (fList[i].startsWith(prefix)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 293 | Slog.i(TAG, "Deleting old usage file: " + fList[i]); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 294 | (new File(parentDir, fList[i])).delete(); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | // Update current stats which are binned by date |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 300 | mFileLeaf = getCurrentDateStr(FILE_PREFIX); |
| 301 | mFile = new File(mDir, mFileLeaf); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 302 | mHistoryFile = new AtomicFile(new File(mDir, FILE_HISTORY)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | readStatsFromFile(); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 304 | readHistoryStatsFromFile(); |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 305 | mLastWriteElapsedTime.set(SystemClock.elapsedRealtime()); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 306 | // mCal was set by getCurrentDateStr(), want to use that same time. |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 307 | mLastWriteDay.set(mCal.get(Calendar.DAY_OF_YEAR)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Utility method to convert date into string. |
| 312 | */ |
| 313 | private String getCurrentDateStr(String prefix) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | StringBuilder sb = new StringBuilder(); |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 315 | synchronized (mCal) { |
| 316 | mCal.setTimeInMillis(System.currentTimeMillis()); |
| 317 | if (prefix != null) { |
| 318 | sb.append(prefix); |
| 319 | } |
| 320 | sb.append(mCal.get(Calendar.YEAR)); |
| 321 | int mm = mCal.get(Calendar.MONTH) - Calendar.JANUARY +1; |
| 322 | if (mm < 10) { |
| 323 | sb.append("0"); |
| 324 | } |
| 325 | sb.append(mm); |
| 326 | int dd = mCal.get(Calendar.DAY_OF_MONTH); |
| 327 | if (dd < 10) { |
| 328 | sb.append("0"); |
| 329 | } |
| 330 | sb.append(dd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | return sb.toString(); |
| 333 | } |
| 334 | |
| 335 | private Parcel getParcelForFile(File file) throws IOException { |
| 336 | FileInputStream stream = new FileInputStream(file); |
| 337 | byte[] raw = readFully(stream); |
| 338 | Parcel in = Parcel.obtain(); |
| 339 | in.unmarshall(raw, 0, raw.length); |
| 340 | in.setDataPosition(0); |
| 341 | stream.close(); |
| 342 | return in; |
| 343 | } |
| 344 | |
| 345 | private void readStatsFromFile() { |
| 346 | File newFile = mFile; |
| 347 | synchronized (mFileLock) { |
| 348 | try { |
| 349 | if (newFile.exists()) { |
| 350 | readStatsFLOCK(newFile); |
| 351 | } else { |
| 352 | // Check for file limit before creating a new file |
| 353 | checkFileLimitFLOCK(); |
| 354 | newFile.createNewFile(); |
| 355 | } |
| 356 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 357 | Slog.w(TAG,"Error : " + e + " reading data from file:" + newFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | private void readStatsFLOCK(File file) throws IOException { |
| 363 | Parcel in = getParcelForFile(file); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 364 | int vers = in.readInt(); |
| 365 | if (vers != VERSION) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 366 | Slog.w(TAG, "Usage stats version changed; dropping"); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | int N = in.readInt(); |
| 370 | while (N > 0) { |
| 371 | N--; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | String pkgName = in.readString(); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 373 | if (pkgName == null) { |
| 374 | break; |
| 375 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 376 | if (localLOGV) Slog.v(TAG, "Reading package #" + N + ": " + pkgName); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 377 | PkgUsageStatsExtended pus = new PkgUsageStatsExtended(in); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | synchronized (mStatsLock) { |
| 379 | mStats.put(pkgName, pus); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 384 | private void readHistoryStatsFromFile() { |
| 385 | synchronized (mFileLock) { |
| 386 | if (mHistoryFile.getBaseFile().exists()) { |
| 387 | readHistoryStatsFLOCK(mHistoryFile); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | private void readHistoryStatsFLOCK(AtomicFile file) { |
| 393 | FileInputStream fis = null; |
| 394 | try { |
| 395 | fis = mHistoryFile.openRead(); |
| 396 | XmlPullParser parser = Xml.newPullParser(); |
| 397 | parser.setInput(fis, null); |
| 398 | int eventType = parser.getEventType(); |
Narayan Kamath | 2ac3cb7 | 2014-01-06 11:31:35 +0000 | [diff] [blame] | 399 | while (eventType != XmlPullParser.START_TAG && |
| 400 | eventType != XmlPullParser.END_DOCUMENT) { |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 401 | eventType = parser.next(); |
| 402 | } |
Narayan Kamath | 2ac3cb7 | 2014-01-06 11:31:35 +0000 | [diff] [blame] | 403 | if (eventType == XmlPullParser.END_DOCUMENT) { |
| 404 | return; |
| 405 | } |
| 406 | |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 407 | String tagName = parser.getName(); |
| 408 | if ("usage-history".equals(tagName)) { |
| 409 | String pkg = null; |
| 410 | do { |
| 411 | eventType = parser.next(); |
| 412 | if (eventType == XmlPullParser.START_TAG) { |
| 413 | tagName = parser.getName(); |
| 414 | int depth = parser.getDepth(); |
| 415 | if ("pkg".equals(tagName) && depth == 2) { |
| 416 | pkg = parser.getAttributeValue(null, "name"); |
| 417 | } else if ("comp".equals(tagName) && depth == 3 && pkg != null) { |
| 418 | String comp = parser.getAttributeValue(null, "name"); |
| 419 | String lastResumeTimeStr = parser.getAttributeValue(null, "lrt"); |
| 420 | if (comp != null && lastResumeTimeStr != null) { |
| 421 | try { |
| 422 | long lastResumeTime = Long.parseLong(lastResumeTimeStr); |
| 423 | synchronized (mStatsLock) { |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 424 | ArrayMap<String, Long> lrt = mLastResumeTimes.get(pkg); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 425 | if (lrt == null) { |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 426 | lrt = new ArrayMap<String, Long>(); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 427 | mLastResumeTimes.put(pkg, lrt); |
| 428 | } |
| 429 | lrt.put(comp, lastResumeTime); |
| 430 | } |
| 431 | } catch (NumberFormatException e) { |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | } else if (eventType == XmlPullParser.END_TAG) { |
| 436 | if ("pkg".equals(parser.getName())) { |
| 437 | pkg = null; |
| 438 | } |
| 439 | } |
| 440 | } while (eventType != XmlPullParser.END_DOCUMENT); |
| 441 | } |
| 442 | } catch (XmlPullParserException e) { |
| 443 | Slog.w(TAG,"Error reading history stats: " + e); |
| 444 | } catch (IOException e) { |
| 445 | Slog.w(TAG,"Error reading history stats: " + e); |
| 446 | } finally { |
| 447 | if (fis != null) { |
| 448 | try { |
| 449 | fis.close(); |
| 450 | } catch (IOException e) { |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | private ArrayList<String> getUsageStatsFileListFLOCK() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | // Check if there are too many files in the system and delete older files |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 458 | String fList[] = mDir.list(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | if (fList == null) { |
| 460 | return null; |
| 461 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | ArrayList<String> fileList = new ArrayList<String>(); |
| 463 | for (String file : fList) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 464 | if (!file.startsWith(FILE_PREFIX)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | if (file.endsWith(".bak")) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 468 | (new File(mDir, file)).delete(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | continue; |
| 470 | } |
| 471 | fileList.add(file); |
| 472 | } |
| 473 | return fileList; |
| 474 | } |
| 475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | private void checkFileLimitFLOCK() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | // Get all usage stats output files |
| 478 | ArrayList<String> fileList = getUsageStatsFileListFLOCK(); |
| 479 | if (fileList == null) { |
| 480 | // Strange but we dont have to delete any thing |
| 481 | return; |
| 482 | } |
| 483 | int count = fileList.size(); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 484 | if (count <= MAX_NUM_FILES) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | return; |
| 486 | } |
| 487 | // Sort files |
| 488 | Collections.sort(fileList); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 489 | count -= MAX_NUM_FILES; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | // Delete older files |
| 491 | for (int i = 0; i < count; i++) { |
| 492 | String fileName = fileList.get(i); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 493 | File file = new File(mDir, fileName); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 494 | Slog.i(TAG, "Deleting usage file : " + fileName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | file.delete(); |
| 496 | } |
| 497 | } |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * Conditionally start up a disk write if it's been awhile, or the |
| 501 | * day has rolled over. |
| 502 | * |
| 503 | * This is called indirectly from user-facing actions (when |
| 504 | * 'force' is false) so it tries to be quick, without writing to |
| 505 | * disk directly or acquiring heavy locks. |
| 506 | * |
| 507 | * @params force do an unconditional, synchronous stats flush |
| 508 | * to disk on the current thread. |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 509 | * @params forceWriteHistoryStats Force writing of historical stats. |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 510 | */ |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 511 | private void writeStatsToFile(final boolean force, final boolean forceWriteHistoryStats) { |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 512 | int curDay; |
| 513 | synchronized (mCal) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 514 | mCal.setTimeInMillis(System.currentTimeMillis()); |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 515 | curDay = mCal.get(Calendar.DAY_OF_YEAR); |
| 516 | } |
| 517 | final boolean dayChanged = curDay != mLastWriteDay.get(); |
| 518 | |
| 519 | // Determine if the day changed... note that this will be wrong |
| 520 | // if the year has changed but we are in the same day of year... |
| 521 | // we can probably live with this. |
| 522 | final long currElapsedTime = SystemClock.elapsedRealtime(); |
| 523 | |
| 524 | // Fast common path, without taking the often-contentious |
| 525 | // mFileLock. |
| 526 | if (!force) { |
| 527 | if (!dayChanged && |
| 528 | (currElapsedTime - mLastWriteElapsedTime.get()) < FILE_WRITE_INTERVAL) { |
| 529 | // wait till the next update |
| 530 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | } |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 532 | if (mUnforcedDiskWriteRunning.compareAndSet(false, true)) { |
| 533 | new Thread("UsageStatsService_DiskWriter") { |
| 534 | public void run() { |
| 535 | try { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 536 | if (localLOGV) Slog.d(TAG, "Disk writer thread starting."); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 537 | writeStatsToFile(true, false); |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 538 | } finally { |
| 539 | mUnforcedDiskWriteRunning.set(false); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 540 | if (localLOGV) Slog.d(TAG, "Disk writer thread ending."); |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | }.start(); |
| 544 | } |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | synchronized (mFileLock) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | // Get the most recent file |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 550 | mFileLeaf = getCurrentDateStr(FILE_PREFIX); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | // Copy current file to back up |
Suchi Amalapurapu | be1c422 | 2009-12-04 12:31:13 -0800 | [diff] [blame] | 552 | File backupFile = null; |
| 553 | if (mFile != null && mFile.exists()) { |
| 554 | backupFile = new File(mFile.getPath() + ".bak"); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 555 | if (!backupFile.exists()) { |
| 556 | if (!mFile.renameTo(backupFile)) { |
| 557 | Slog.w(TAG, "Failed to persist new stats"); |
| 558 | return; |
| 559 | } |
| 560 | } else { |
| 561 | mFile.delete(); |
Suchi Amalapurapu | be1c422 | 2009-12-04 12:31:13 -0800 | [diff] [blame] | 562 | } |
Suchi Amalapurapu | 8550f25 | 2009-09-29 15:20:32 -0700 | [diff] [blame] | 563 | } |
Suchi Amalapurapu | be1c422 | 2009-12-04 12:31:13 -0800 | [diff] [blame] | 564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | // Write mStats to file |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 567 | writeStatsFLOCK(mFile); |
| 568 | mLastWriteElapsedTime.set(currElapsedTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | if (dayChanged) { |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 570 | mLastWriteDay.set(curDay); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | // clear stats |
| 572 | synchronized (mStats) { |
| 573 | mStats.clear(); |
| 574 | } |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 575 | mFile = new File(mDir, mFileLeaf); |
| 576 | checkFileLimitFLOCK(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 578 | |
| 579 | if (dayChanged || forceWriteHistoryStats) { |
| 580 | // Write history stats daily, or when forced (due to shutdown). |
| 581 | writeHistoryStatsFLOCK(mHistoryFile); |
| 582 | } |
| 583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | // Delete the backup file |
| 585 | if (backupFile != null) { |
| 586 | backupFile.delete(); |
| 587 | } |
| 588 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 589 | Slog.w(TAG, "Failed writing stats to file:" + mFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | if (backupFile != null) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 591 | mFile.delete(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | backupFile.renameTo(mFile); |
| 593 | } |
| 594 | } |
| 595 | } |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 596 | if (localLOGV) Slog.d(TAG, "Dumped usage stats."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Brad Fitzpatrick | 389a916 | 2010-08-03 15:41:05 -0700 | [diff] [blame] | 599 | private void writeStatsFLOCK(File file) throws IOException { |
| 600 | FileOutputStream stream = new FileOutputStream(file); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 601 | try { |
| 602 | Parcel out = Parcel.obtain(); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 603 | writeStatsToParcelFLOCK(out); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 604 | stream.write(out.marshall()); |
| 605 | out.recycle(); |
| 606 | stream.flush(); |
| 607 | } finally { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 608 | FileUtils.sync(stream); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 609 | stream.close(); |
| 610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 613 | private void writeStatsToParcelFLOCK(Parcel out) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | synchronized (mStatsLock) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 615 | out.writeInt(VERSION); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | Set<String> keys = mStats.keySet(); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 617 | out.writeInt(keys.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | for (String key : keys) { |
| 619 | PkgUsageStatsExtended pus = mStats.get(key); |
| 620 | out.writeString(key); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 621 | pus.writeToParcel(out); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 626 | /** Filter out stats for any packages which aren't present anymore. */ |
| 627 | private void filterHistoryStats() { |
| 628 | synchronized (mStatsLock) { |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 629 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 630 | for (int i=0; i<mLastResumeTimes.size(); i++) { |
| 631 | String pkg = mLastResumeTimes.keyAt(i); |
| 632 | try { |
| 633 | if (pm.getPackageUid(pkg, 0) < 0) { |
| 634 | mLastResumeTimes.removeAt(i); |
| 635 | i--; |
| 636 | } |
| 637 | } catch (RemoteException e) { |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | private void writeHistoryStatsFLOCK(AtomicFile historyFile) { |
| 644 | FileOutputStream fos = null; |
| 645 | try { |
| 646 | fos = historyFile.startWrite(); |
| 647 | XmlSerializer out = new FastXmlSerializer(); |
| 648 | out.setOutput(fos, "utf-8"); |
| 649 | out.startDocument(null, true); |
| 650 | out.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 651 | out.startTag(null, "usage-history"); |
| 652 | synchronized (mStatsLock) { |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 653 | for (int i=0; i<mLastResumeTimes.size(); i++) { |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 654 | out.startTag(null, "pkg"); |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 655 | out.attribute(null, "name", mLastResumeTimes.keyAt(i)); |
| 656 | ArrayMap<String, Long> comp = mLastResumeTimes.valueAt(i); |
| 657 | for (int j=0; j<comp.size(); j++) { |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 658 | out.startTag(null, "comp"); |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 659 | out.attribute(null, "name", comp.keyAt(j)); |
| 660 | out.attribute(null, "lrt", comp.valueAt(j).toString()); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 661 | out.endTag(null, "comp"); |
| 662 | } |
| 663 | out.endTag(null, "pkg"); |
| 664 | } |
| 665 | } |
| 666 | out.endTag(null, "usage-history"); |
| 667 | out.endDocument(); |
| 668 | |
| 669 | historyFile.finishWrite(fos); |
| 670 | } catch (IOException e) { |
| 671 | Slog.w(TAG,"Error writing history stats" + e); |
| 672 | if (fos != null) { |
| 673 | historyFile.failWrite(fos); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | public void publish(Context context) { |
| 679 | mContext = context; |
| 680 | ServiceManager.addService(SERVICE_NAME, asBinder()); |
| 681 | } |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 682 | |
| 683 | /** |
| 684 | * Start watching packages to remove stats when a package is uninstalled. |
| 685 | * May only be called when the package manager is ready. |
| 686 | */ |
| 687 | public void monitorPackages() { |
| 688 | mPackageMonitor = new PackageMonitor() { |
| 689 | @Override |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 690 | public void onPackageRemovedAllUsers(String packageName, int uid) { |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 691 | synchronized (mStatsLock) { |
| 692 | mLastResumeTimes.remove(packageName); |
| 693 | } |
| 694 | } |
| 695 | }; |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 696 | mPackageMonitor.register(mContext, null, true); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 697 | filterHistoryStats(); |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 698 | } |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 699 | |
| 700 | public void shutdown() { |
| 701 | if (mPackageMonitor != null) { |
| 702 | mPackageMonitor.unregister(); |
| 703 | } |
| 704 | Slog.i(TAG, "Writing usage stats before shutdown..."); |
| 705 | writeStatsToFile(true, true); |
| 706 | } |
| 707 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | public static IUsageStats getService() { |
| 709 | if (sService != null) { |
| 710 | return sService; |
| 711 | } |
| 712 | IBinder b = ServiceManager.getService(SERVICE_NAME); |
| 713 | sService = asInterface(b); |
| 714 | return sService; |
| 715 | } |
| 716 | |
| 717 | public void noteResumeComponent(ComponentName componentName) { |
| 718 | enforceCallingPermission(); |
| 719 | String pkgName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | synchronized (mStatsLock) { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 721 | if ((componentName == null) || |
| 722 | ((pkgName = componentName.getPackageName()) == null)) { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | final boolean samePackage = pkgName.equals(mLastResumedPkg); |
| 727 | if (mIsResumed) { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 728 | if (mLastResumedPkg != null) { |
| 729 | // We last resumed some other package... just pause it now |
| 730 | // to recover. |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 731 | if (REPORT_UNEXPECTED) Slog.i(TAG, "Unexpected resume of " + pkgName |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 732 | + " while already resumed in " + mLastResumedPkg); |
| 733 | PkgUsageStatsExtended pus = mStats.get(mLastResumedPkg); |
| 734 | if (pus != null) { |
| 735 | pus.updatePause(); |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | final boolean sameComp = samePackage |
| 741 | && componentName.getClassName().equals(mLastResumedComp); |
| 742 | |
| 743 | mIsResumed = true; |
| 744 | mLastResumedPkg = pkgName; |
| 745 | mLastResumedComp = componentName.getClassName(); |
| 746 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 747 | if (localLOGV) Slog.i(TAG, "started component:" + pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | PkgUsageStatsExtended pus = mStats.get(pkgName); |
| 749 | if (pus == null) { |
| 750 | pus = new PkgUsageStatsExtended(); |
| 751 | mStats.put(pkgName, pus); |
| 752 | } |
Mark Brophy | 9fc0330 | 2011-07-01 16:56:24 +0100 | [diff] [blame] | 753 | pus.updateResume(mLastResumedComp, !samePackage); |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 754 | if (!sameComp) { |
| 755 | pus.addLaunchCount(mLastResumedComp); |
| 756 | } |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 757 | |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 758 | ArrayMap<String, Long> componentResumeTimes = mLastResumeTimes.get(pkgName); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 759 | if (componentResumeTimes == null) { |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 760 | componentResumeTimes = new ArrayMap<String, Long>(); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 761 | mLastResumeTimes.put(pkgName, componentResumeTimes); |
| 762 | } |
| 763 | componentResumeTimes.put(mLastResumedComp, System.currentTimeMillis()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | public void notePauseComponent(ComponentName componentName) { |
| 768 | enforceCallingPermission(); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 769 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | synchronized (mStatsLock) { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 771 | String pkgName; |
| 772 | if ((componentName == null) || |
| 773 | ((pkgName = componentName.getPackageName()) == null)) { |
| 774 | return; |
| 775 | } |
| 776 | if (!mIsResumed) { |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 777 | if (REPORT_UNEXPECTED) Slog.i(TAG, "Something wrong here, didn't expect " |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 778 | + pkgName + " to be paused"); |
| 779 | return; |
| 780 | } |
| 781 | mIsResumed = false; |
| 782 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 783 | if (localLOGV) Slog.i(TAG, "paused component:"+pkgName); |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 784 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | PkgUsageStatsExtended pus = mStats.get(pkgName); |
| 786 | if (pus == null) { |
| 787 | // Weird some error here |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 788 | Slog.i(TAG, "No package stats for pkg:"+pkgName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | return; |
| 790 | } |
| 791 | pus.updatePause(); |
| 792 | } |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 793 | |
| 794 | // Persist current data to file if needed. |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 795 | writeStatsToFile(false, false); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | public void noteLaunchTime(ComponentName componentName, int millis) { |
| 799 | enforceCallingPermission(); |
| 800 | String pkgName; |
| 801 | if ((componentName == null) || |
| 802 | ((pkgName = componentName.getPackageName()) == null)) { |
| 803 | return; |
| 804 | } |
| 805 | |
| 806 | // Persist current data to file if needed. |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 807 | writeStatsToFile(false, false); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 808 | |
| 809 | synchronized (mStatsLock) { |
| 810 | PkgUsageStatsExtended pus = mStats.get(pkgName); |
| 811 | if (pus != null) { |
| 812 | pus.addLaunchTime(componentName.getClassName(), millis); |
| 813 | } |
| 814 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 817 | public void noteFullyDrawnTime(ComponentName componentName, int millis) { |
| 818 | enforceCallingPermission(); |
| 819 | String pkgName; |
| 820 | if ((componentName == null) || |
| 821 | ((pkgName = componentName.getPackageName()) == null)) { |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | // Persist current data to file if needed. |
| 826 | writeStatsToFile(false, false); |
| 827 | |
| 828 | synchronized (mStatsLock) { |
| 829 | PkgUsageStatsExtended pus = mStats.get(pkgName); |
| 830 | if (pus != null) { |
| 831 | pus.addFullyDrawnTime(componentName.getClassName(), millis); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | public void enforceCallingPermission() { |
| 837 | if (Binder.getCallingPid() == Process.myPid()) { |
| 838 | return; |
| 839 | } |
| 840 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 841 | Binder.getCallingPid(), Binder.getCallingUid(), null); |
| 842 | } |
| 843 | |
| 844 | public PkgUsageStats getPkgUsageStats(ComponentName componentName) { |
| 845 | mContext.enforceCallingOrSelfPermission( |
| 846 | android.Manifest.permission.PACKAGE_USAGE_STATS, null); |
| 847 | String pkgName; |
| 848 | if ((componentName == null) || |
| 849 | ((pkgName = componentName.getPackageName()) == null)) { |
| 850 | return null; |
| 851 | } |
| 852 | synchronized (mStatsLock) { |
| 853 | PkgUsageStatsExtended pus = mStats.get(pkgName); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 854 | Map<String, Long> lastResumeTimes = mLastResumeTimes.get(pkgName); |
| 855 | if (pus == null && lastResumeTimes == null) { |
| 856 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | } |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 858 | int launchCount = pus != null ? pus.mLaunchCount : 0; |
| 859 | long usageTime = pus != null ? pus.mUsageTime : 0; |
| 860 | return new PkgUsageStats(pkgName, launchCount, usageTime, lastResumeTimes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | public PkgUsageStats[] getAllPkgUsageStats() { |
| 865 | mContext.enforceCallingOrSelfPermission( |
| 866 | android.Manifest.permission.PACKAGE_USAGE_STATS, null); |
| 867 | synchronized (mStatsLock) { |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 868 | int size = mLastResumeTimes.size(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | if (size <= 0) { |
| 870 | return null; |
| 871 | } |
| 872 | PkgUsageStats retArr[] = new PkgUsageStats[size]; |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 873 | for (int i=0; i<size; i++) { |
| 874 | String pkg = mLastResumeTimes.keyAt(i); |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 875 | long usageTime = 0; |
| 876 | int launchCount = 0; |
| 877 | |
| 878 | PkgUsageStatsExtended pus = mStats.get(pkg); |
| 879 | if (pus != null) { |
| 880 | usageTime = pus.mUsageTime; |
| 881 | launchCount = pus.mLaunchCount; |
| 882 | } |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 883 | retArr[i] = new PkgUsageStats(pkg, launchCount, usageTime, |
| 884 | mLastResumeTimes.valueAt(i)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | } |
| 886 | return retArr; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | static byte[] readFully(FileInputStream stream) throws java.io.IOException { |
| 891 | int pos = 0; |
| 892 | int avail = stream.available(); |
| 893 | byte[] data = new byte[avail]; |
| 894 | while (true) { |
| 895 | int amt = stream.read(data, pos, data.length-pos); |
| 896 | if (amt <= 0) { |
| 897 | return data; |
| 898 | } |
| 899 | pos += amt; |
| 900 | avail = stream.available(); |
| 901 | if (avail > data.length-pos) { |
| 902 | byte[] newData = new byte[pos+avail]; |
| 903 | System.arraycopy(data, 0, newData, 0, pos); |
| 904 | data = newData; |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 909 | private void collectDumpInfoFLOCK(PrintWriter pw, boolean isCompactOutput, |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 910 | boolean deleteAfterPrint, HashSet<String> packages) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | List<String> fileList = getUsageStatsFileListFLOCK(); |
| 912 | if (fileList == null) { |
| 913 | return; |
| 914 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | Collections.sort(fileList); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | for (String file : fileList) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 917 | if (deleteAfterPrint && file.equalsIgnoreCase(mFileLeaf)) { |
| 918 | // In this mode we don't print the current day's stats, since |
| 919 | // they are incomplete. |
| 920 | continue; |
| 921 | } |
| 922 | File dFile = new File(mDir, file); |
| 923 | String dateStr = file.substring(FILE_PREFIX.length()); |
Dianne Hackborn | 904a857 | 2013-06-28 18:12:31 -0700 | [diff] [blame] | 924 | if (dateStr.length() > 0 && (dateStr.charAt(0) <= '0' || dateStr.charAt(0) >= '9')) { |
| 925 | // If the remainder does not start with a number, it is not a date, |
| 926 | // so we should ignore it for purposes here. |
| 927 | continue; |
| 928 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | try { |
| 930 | Parcel in = getParcelForFile(dFile); |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 931 | collectDumpInfoFromParcelFLOCK(in, pw, dateStr, isCompactOutput, |
| 932 | packages); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 933 | if (deleteAfterPrint) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | // Delete old file after collecting info only for checkin requests |
| 935 | dFile.delete(); |
| 936 | } |
| 937 | } catch (FileNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 938 | Slog.w(TAG, "Failed with "+e+" when collecting dump info from file : " + file); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | return; |
| 940 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 941 | Slog.w(TAG, "Failed with "+e+" when collecting dump info from file : "+file); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | private void collectDumpInfoFromParcelFLOCK(Parcel in, PrintWriter pw, |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 947 | String date, boolean isCompactOutput, HashSet<String> packages) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 948 | StringBuilder sb = new StringBuilder(512); |
| 949 | if (isCompactOutput) { |
| 950 | sb.append("D:"); |
| 951 | sb.append(CHECKIN_VERSION); |
| 952 | sb.append(','); |
| 953 | } else { |
| 954 | sb.append("Date: "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | } |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 956 | |
| 957 | sb.append(date); |
| 958 | |
| 959 | int vers = in.readInt(); |
| 960 | if (vers != VERSION) { |
| 961 | sb.append(" (old data version)"); |
| 962 | pw.println(sb.toString()); |
| 963 | return; |
| 964 | } |
| 965 | |
| 966 | pw.println(sb.toString()); |
| 967 | int N = in.readInt(); |
| 968 | |
| 969 | while (N > 0) { |
| 970 | N--; |
| 971 | String pkgName = in.readString(); |
| 972 | if (pkgName == null) { |
| 973 | break; |
| 974 | } |
| 975 | sb.setLength(0); |
| 976 | PkgUsageStatsExtended pus = new PkgUsageStatsExtended(in); |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 977 | if (packages != null && !packages.contains(pkgName)) { |
| 978 | // This package has not been requested -- don't print |
| 979 | // anything for it. |
| 980 | } else if (isCompactOutput) { |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 981 | sb.append("P:"); |
| 982 | sb.append(pkgName); |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 983 | sb.append(','); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 984 | sb.append(pus.mLaunchCount); |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 985 | sb.append(','); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 986 | sb.append(pus.mUsageTime); |
| 987 | sb.append('\n'); |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 988 | final int NLT = pus.mLaunchTimes.size(); |
| 989 | for (int i=0; i<NLT; i++) { |
| 990 | sb.append("A:"); |
| 991 | String activity = pus.mLaunchTimes.keyAt(i); |
| 992 | sb.append(activity); |
| 993 | TimeStats times = pus.mLaunchTimes.valueAt(i); |
| 994 | sb.append(','); |
| 995 | sb.append(times.count); |
| 996 | for (int j=0; j<NUM_LAUNCH_TIME_BINS; j++) { |
| 997 | sb.append(","); |
| 998 | sb.append(times.times[j]); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 999 | } |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1000 | sb.append('\n'); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1001 | } |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1002 | final int NFDT = pus.mFullyDrawnTimes.size(); |
| 1003 | for (int i=0; i<NFDT; i++) { |
| 1004 | sb.append("A:"); |
| 1005 | String activity = pus.mFullyDrawnTimes.keyAt(i); |
| 1006 | sb.append(activity); |
| 1007 | TimeStats times = pus.mFullyDrawnTimes.valueAt(i); |
| 1008 | for (int j=0; j<NUM_LAUNCH_TIME_BINS; j++) { |
| 1009 | sb.append(","); |
| 1010 | sb.append(times.times[j]); |
| 1011 | } |
| 1012 | sb.append('\n'); |
| 1013 | } |
| 1014 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1015 | } else { |
| 1016 | sb.append(" "); |
| 1017 | sb.append(pkgName); |
| 1018 | sb.append(": "); |
| 1019 | sb.append(pus.mLaunchCount); |
| 1020 | sb.append(" times, "); |
| 1021 | sb.append(pus.mUsageTime); |
| 1022 | sb.append(" ms"); |
| 1023 | sb.append('\n'); |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1024 | final int NLT = pus.mLaunchTimes.size(); |
| 1025 | for (int i=0; i<NLT; i++) { |
| 1026 | sb.append(" "); |
| 1027 | sb.append(pus.mLaunchTimes.keyAt(i)); |
| 1028 | TimeStats times = pus.mLaunchTimes.valueAt(i); |
| 1029 | sb.append(": "); |
| 1030 | sb.append(times.count); |
| 1031 | sb.append(" starts"); |
| 1032 | int lastBin = 0; |
| 1033 | for (int j=0; j<NUM_LAUNCH_TIME_BINS-1; j++) { |
| 1034 | if (times.times[j] != 0) { |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 1035 | sb.append(", "); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 1036 | sb.append(lastBin); |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1037 | sb.append('-'); |
| 1038 | sb.append(LAUNCH_TIME_BINS[j]); |
Dianne Hackborn | 760ec4a | 2009-06-17 20:05:26 -0700 | [diff] [blame] | 1039 | sb.append("ms="); |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1040 | sb.append(times.times[j]); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 1041 | } |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1042 | lastBin = LAUNCH_TIME_BINS[j]; |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1043 | } |
Dianne Hackborn | 2286cdc | 2013-07-01 19:10:06 -0700 | [diff] [blame] | 1044 | if (times.times[NUM_LAUNCH_TIME_BINS-1] != 0) { |
| 1045 | sb.append(", "); |
| 1046 | sb.append(">="); |
| 1047 | sb.append(lastBin); |
| 1048 | sb.append("ms="); |
| 1049 | sb.append(times.times[NUM_LAUNCH_TIME_BINS-1]); |
| 1050 | } |
| 1051 | sb.append('\n'); |
| 1052 | } |
| 1053 | final int NFDT = pus.mFullyDrawnTimes.size(); |
| 1054 | for (int i=0; i<NFDT; i++) { |
| 1055 | sb.append(" "); |
| 1056 | sb.append(pus.mFullyDrawnTimes.keyAt(i)); |
| 1057 | TimeStats times = pus.mFullyDrawnTimes.valueAt(i); |
| 1058 | sb.append(": fully drawn "); |
| 1059 | boolean needComma = false; |
| 1060 | int lastBin = 0; |
| 1061 | for (int j=0; j<NUM_LAUNCH_TIME_BINS-1; j++) { |
| 1062 | if (times.times[j] != 0) { |
| 1063 | if (needComma) { |
| 1064 | sb.append(", "); |
| 1065 | } else { |
| 1066 | needComma = true; |
| 1067 | } |
| 1068 | sb.append(lastBin); |
| 1069 | sb.append('-'); |
| 1070 | sb.append(LAUNCH_TIME_BINS[j]); |
| 1071 | sb.append("ms="); |
| 1072 | sb.append(times.times[j]); |
| 1073 | } |
| 1074 | lastBin = LAUNCH_TIME_BINS[j]; |
| 1075 | } |
| 1076 | if (times.times[NUM_LAUNCH_TIME_BINS-1] != 0) { |
| 1077 | if (needComma) { |
| 1078 | sb.append(", "); |
| 1079 | } |
| 1080 | sb.append(">="); |
| 1081 | sb.append(lastBin); |
| 1082 | sb.append("ms="); |
| 1083 | sb.append(times.times[NUM_LAUNCH_TIME_BINS-1]); |
| 1084 | } |
| 1085 | sb.append('\n'); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | pw.write(sb.toString()); |
| 1090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * Searches array of arguments for the specified string |
| 1095 | * @param args array of argument strings |
| 1096 | * @param value value to search for |
| 1097 | * @return true if the value is contained in the array |
| 1098 | */ |
| 1099 | private static boolean scanArgs(String[] args, String value) { |
| 1100 | if (args != null) { |
| 1101 | for (String arg : args) { |
| 1102 | if (value.equals(arg)) { |
| 1103 | return true; |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | return false; |
| 1108 | } |
| 1109 | |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 1110 | /** |
| 1111 | * Searches array of arguments for the specified string's data |
| 1112 | * @param args array of argument strings |
| 1113 | * @param value value to search for |
| 1114 | * @return the string of data after the arg, or null if there is none |
| 1115 | */ |
| 1116 | private static String scanArgsData(String[] args, String value) { |
| 1117 | if (args != null) { |
| 1118 | final int N = args.length; |
| 1119 | for (int i=0; i<N; i++) { |
| 1120 | if (value.equals(args[i])) { |
| 1121 | i++; |
| 1122 | return i < N ? args[i] : null; |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | return null; |
| 1127 | } |
| 1128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | @Override |
| 1130 | /* |
| 1131 | * The data persisted to file is parsed and the stats are computed. |
| 1132 | */ |
| 1133 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Kenny Root | 3abd75b | 2011-09-29 11:00:41 -0700 | [diff] [blame] | 1134 | if (mContext.checkCallingPermission(android.Manifest.permission.DUMP) |
| 1135 | != PackageManager.PERMISSION_GRANTED) { |
| 1136 | pw.println("Permission Denial: can't dump UsageStats from from pid=" |
| 1137 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 1138 | + " without permission " + android.Manifest.permission.DUMP); |
| 1139 | return; |
| 1140 | } |
| 1141 | |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1142 | final boolean isCheckinRequest = scanArgs(args, "--checkin"); |
| 1143 | final boolean isCompactOutput = isCheckinRequest || scanArgs(args, "-c"); |
| 1144 | final boolean deleteAfterPrint = isCheckinRequest || scanArgs(args, "-d"); |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 1145 | final String rawPackages = scanArgsData(args, "--packages"); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1146 | |
| 1147 | // Make sure the current stats are written to the file. This |
| 1148 | // doesn't need to be done if we are deleting files after printing, |
| 1149 | // since it that case we won't print the current stats. |
| 1150 | if (!deleteAfterPrint) { |
Mark Brophy | c635027 | 2011-08-05 16:16:39 +0100 | [diff] [blame] | 1151 | writeStatsToFile(true, false); |
Dianne Hackborn | 6447ca3 | 2009-04-07 19:50:08 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 1154 | HashSet<String> packages = null; |
| 1155 | if (rawPackages != null) { |
| 1156 | if (!"*".equals(rawPackages)) { |
| 1157 | // A * is a wildcard to show all packages. |
| 1158 | String[] names = rawPackages.split(","); |
| 1159 | for (String n : names) { |
| 1160 | if (packages == null) { |
| 1161 | packages = new HashSet<String>(); |
| 1162 | } |
| 1163 | packages.add(n); |
| 1164 | } |
| 1165 | } |
| 1166 | } else if (isCheckinRequest) { |
| 1167 | // If checkin doesn't specify any packages, then we simply won't |
| 1168 | // show anything. |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1169 | Slog.w(TAG, "Checkin without packages"); |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 1170 | return; |
| 1171 | } |
| 1172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | synchronized (mFileLock) { |
Dianne Hackborn | 9fdbf6a | 2009-07-19 14:18:51 -0700 | [diff] [blame] | 1174 | collectDumpInfoFLOCK(pw, isCompactOutput, deleteAfterPrint, packages); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | } |