blob: dfba2086cd16c121adaaa9bebcce3326d5313fc0 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2008 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017package android.os;
18
19import java.io.PrintWriter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080020import java.util.ArrayList;
Dianne Hackborn81038902012-11-26 17:04:09 -080021import java.util.Collections;
22import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Formatter;
Dianne Hackborne4a59512010-12-07 11:08:07 -080024import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import java.util.Map;
26
Dianne Hackborna7c837f2014-01-15 16:20:44 -080027import android.content.Context;
Dianne Hackborne4a59512010-12-07 11:08:07 -080028import android.content.pm.ApplicationInfo;
Wink Saville52840902011-02-18 12:40:47 -080029import android.telephony.SignalStrength;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080030import android.text.format.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.Printer;
32import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070033import android.util.TimeUtils;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080034import com.android.internal.os.BatterySipper;
35import com.android.internal.os.BatteryStatsHelper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37/**
38 * A class providing access to battery usage statistics, including information on
39 * wakelocks, processes, packages, and services. All times are represented in microseconds
40 * except where indicated otherwise.
41 * @hide
42 */
43public abstract class BatteryStats implements Parcelable {
44
45 private static final boolean LOCAL_LOGV = false;
Dianne Hackborn91268cf2013-06-13 19:06:50 -070046
47 /** @hide */
48 public static final String SERVICE_NAME = "batterystats";
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
51 * A constant indicating a partial wake lock timer.
52 */
53 public static final int WAKE_TYPE_PARTIAL = 0;
54
55 /**
56 * A constant indicating a full wake lock timer.
57 */
58 public static final int WAKE_TYPE_FULL = 1;
59
60 /**
61 * A constant indicating a window wake lock timer.
62 */
63 public static final int WAKE_TYPE_WINDOW = 2;
64
65 /**
66 * A constant indicating a sensor timer.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
68 public static final int SENSOR = 3;
The Android Open Source Project10592532009-03-18 17:39:46 -070069
70 /**
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070071 * A constant indicating a a wifi running timer
Dianne Hackborn617f8772009-03-31 15:04:46 -070072 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070073 public static final int WIFI_RUNNING = 4;
Dianne Hackborn617f8772009-03-31 15:04:46 -070074
75 /**
The Android Open Source Project10592532009-03-18 17:39:46 -070076 * A constant indicating a full wifi lock timer
The Android Open Source Project10592532009-03-18 17:39:46 -070077 */
Dianne Hackborn617f8772009-03-31 15:04:46 -070078 public static final int FULL_WIFI_LOCK = 5;
The Android Open Source Project10592532009-03-18 17:39:46 -070079
80 /**
Nick Pelly6ccaa542012-06-15 15:22:47 -070081 * A constant indicating a wifi scan
The Android Open Source Project10592532009-03-18 17:39:46 -070082 */
Nick Pelly6ccaa542012-06-15 15:22:47 -070083 public static final int WIFI_SCAN = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Robert Greenwalt5347bd42009-05-13 15:10:16 -070085 /**
86 * A constant indicating a wifi multicast timer
Robert Greenwalt5347bd42009-05-13 15:10:16 -070087 */
88 public static final int WIFI_MULTICAST_ENABLED = 7;
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -070091 * A constant indicating an audio turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070092 */
93 public static final int AUDIO_TURNED_ON = 7;
94
95 /**
96 * A constant indicating a video turn on timer
Amith Yamasani244fa5c2009-05-22 14:36:07 -070097 */
98 public static final int VIDEO_TURNED_ON = 8;
99
100 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800101 * A constant indicating a vibrator on timer
102 */
103 public static final int VIBRATOR_ON = 9;
104
105 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700106 * A constant indicating a foreground activity timer
107 */
108 public static final int FOREGROUND_ACTIVITY = 10;
109
110 /**
Robert Greenwalta029ea12013-09-25 16:38:12 -0700111 * A constant indicating a wifi batched scan is active
112 */
113 public static final int WIFI_BATCHED_SCAN = 11;
114
115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * Include all of the data in the stats, including previously saved data.
117 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700118 public static final int STATS_SINCE_CHARGED = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 /**
121 * Include only the last run in the stats.
122 */
123 public static final int STATS_LAST = 1;
124
125 /**
126 * Include only the current run in the stats.
127 */
128 public static final int STATS_CURRENT = 2;
129
130 /**
131 * Include only the run since the last time the device was unplugged in the stats.
132 */
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700133 public static final int STATS_SINCE_UNPLUGGED = 3;
Evan Millare84de8d2009-04-02 22:16:12 -0700134
135 // NOTE: Update this list if you add/change any stats above.
136 // These characters are supposed to represent "total", "last", "current",
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700137 // and "unplugged". They were shortened for efficiency sake.
Evan Millare84de8d2009-04-02 22:16:12 -0700138 private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
140 /**
141 * Bump the version on this if the checkin format changes.
142 */
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700143 private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
Evan Millar22ac0432009-03-31 11:33:18 -0700144
145 private static final long BYTES_PER_KB = 1024;
146 private static final long BYTES_PER_MB = 1048576; // 1024^2
147 private static final long BYTES_PER_GB = 1073741824; //1024^3
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Dianne Hackborne4a59512010-12-07 11:08:07 -0800150 private static final String UID_DATA = "uid";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 private static final String APK_DATA = "apk";
Evan Millare84de8d2009-04-02 22:16:12 -0700152 private static final String PROCESS_DATA = "pr";
153 private static final String SENSOR_DATA = "sr";
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800154 private static final String VIBRATOR_DATA = "vib";
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700155 private static final String FOREGROUND_DATA = "fg";
Evan Millare84de8d2009-04-02 22:16:12 -0700156 private static final String WAKELOCK_DATA = "wl";
Evan Millarc64edde2009-04-18 12:26:32 -0700157 private static final String KERNEL_WAKELOCK_DATA = "kwl";
Evan Millare84de8d2009-04-02 22:16:12 -0700158 private static final String NETWORK_DATA = "nt";
159 private static final String USER_ACTIVITY_DATA = "ua";
160 private static final String BATTERY_DATA = "bt";
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800161 private static final String BATTERY_DISCHARGE_DATA = "dc";
Evan Millare84de8d2009-04-02 22:16:12 -0700162 private static final String BATTERY_LEVEL_DATA = "lv";
Nick Pelly6ccaa542012-06-15 15:22:47 -0700163 private static final String WIFI_DATA = "wfl";
Evan Millare84de8d2009-04-02 22:16:12 -0700164 private static final String MISC_DATA = "m";
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800165 private static final String GLOBAL_NETWORK_DATA = "gn";
Dianne Hackborn099bc622014-01-22 13:39:16 -0800166 private static final String HISTORY_STRING_POOL = "hsp";
Dianne Hackborn8a0de582013-08-07 15:22:07 -0700167 private static final String HISTORY_DATA = "h";
Evan Millare84de8d2009-04-02 22:16:12 -0700168 private static final String SCREEN_BRIGHTNESS_DATA = "br";
169 private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
Amith Yamasanif37447b2009-10-08 18:28:01 -0700170 private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
Evan Millare84de8d2009-04-02 22:16:12 -0700171 private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
172 private static final String DATA_CONNECTION_TIME_DATA = "dct";
173 private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800174 private static final String WIFI_STATE_TIME_DATA = "wst";
175 private static final String WIFI_STATE_COUNT_DATA = "wsc";
176 private static final String BLUETOOTH_STATE_TIME_DATA = "bst";
177 private static final String BLUETOOTH_STATE_COUNT_DATA = "bsc";
Dianne Hackborna7c837f2014-01-15 16:20:44 -0800178 private static final String POWER_USE_SUMMARY_DATA = "pws";
179 private static final String POWER_USE_ITEM_DATA = "pwi";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700181 private final StringBuilder mFormatBuilder = new StringBuilder(32);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private final Formatter mFormatter = new Formatter(mFormatBuilder);
183
184 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700185 * State for keeping track of counting information.
186 */
187 public static abstract class Counter {
188
189 /**
190 * Returns the count associated with this Counter for the
191 * selected type of statistics.
192 *
193 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
194 */
Evan Millarc64edde2009-04-18 12:26:32 -0700195 public abstract int getCountLocked(int which);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700196
197 /**
198 * Temporary for debugging.
199 */
200 public abstract void logState(Printer pw, String prefix);
201 }
202
203 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 * State for keeping track of timing information.
205 */
206 public static abstract class Timer {
207
208 /**
209 * Returns the count associated with this Timer for the
210 * selected type of statistics.
211 *
212 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
213 */
Evan Millarc64edde2009-04-18 12:26:32 -0700214 public abstract int getCountLocked(int which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
216 /**
217 * Returns the total time in microseconds associated with this Timer for the
218 * selected type of statistics.
219 *
220 * @param batteryRealtime system realtime on battery in microseconds
221 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
222 * @return a time in microseconds
223 */
Evan Millarc64edde2009-04-18 12:26:32 -0700224 public abstract long getTotalTimeLocked(long batteryRealtime, int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
227 * Temporary for debugging.
228 */
Dianne Hackborn627bba72009-03-24 22:32:56 -0700229 public abstract void logState(Printer pw, String prefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
231
232 /**
233 * The statistics associated with a particular uid.
234 */
235 public static abstract class Uid {
236
237 /**
238 * Returns a mapping containing wakelock statistics.
239 *
240 * @return a Map from Strings to Uid.Wakelock objects.
241 */
242 public abstract Map<String, ? extends Wakelock> getWakelockStats();
243
244 /**
245 * The statistics associated with a particular wake lock.
246 */
247 public static abstract class Wakelock {
248 public abstract Timer getWakeTime(int type);
249 }
250
251 /**
252 * Returns a mapping containing sensor statistics.
253 *
254 * @return a Map from Integer sensor ids to Uid.Sensor objects.
255 */
256 public abstract Map<Integer, ? extends Sensor> getSensorStats();
257
258 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700259 * Returns a mapping containing active process data.
260 */
261 public abstract SparseArray<? extends Pid> getPidStats();
262
263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 * Returns a mapping containing process statistics.
265 *
266 * @return a Map from Strings to Uid.Proc objects.
267 */
268 public abstract Map<String, ? extends Proc> getProcessStats();
269
270 /**
271 * Returns a mapping containing package statistics.
272 *
273 * @return a Map from Strings to Uid.Pkg objects.
274 */
275 public abstract Map<String, ? extends Pkg> getPackageStats();
276
277 /**
278 * {@hide}
279 */
280 public abstract int getUid();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700281
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800282 public abstract void noteWifiRunningLocked(long elapsedRealtime);
283 public abstract void noteWifiStoppedLocked(long elapsedRealtime);
284 public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
285 public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
286 public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
287 public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
288 public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
289 public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
290 public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
291 public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
292 public abstract void noteAudioTurnedOnLocked(long elapsedRealtime);
293 public abstract void noteAudioTurnedOffLocked(long elapsedRealtime);
294 public abstract void noteVideoTurnedOnLocked(long elapsedRealtime);
295 public abstract void noteVideoTurnedOffLocked(long elapsedRealtime);
296 public abstract void noteActivityResumedLocked(long elapsedRealtime);
297 public abstract void noteActivityPausedLocked(long elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700298 public abstract long getWifiRunningTime(long batteryRealtime, int which);
The Android Open Source Project10592532009-03-18 17:39:46 -0700299 public abstract long getFullWifiLockTime(long batteryRealtime, int which);
Nick Pelly6ccaa542012-06-15 15:22:47 -0700300 public abstract long getWifiScanTime(long batteryRealtime, int which);
Robert Greenwalta029ea12013-09-25 16:38:12 -0700301 public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -0700302 public abstract long getWifiMulticastTime(long batteryRealtime,
303 int which);
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700304 public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
305 public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -0700306 public abstract Timer getForegroundActivityTimer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800307 public abstract Timer getVibratorOnTimer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Robert Greenwalta029ea12013-09-25 16:38:12 -0700309 public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
310
Dianne Hackborn617f8772009-03-31 15:04:46 -0700311 /**
Jeff Browndf693de2012-07-27 12:03:38 -0700312 * Note that these must match the constants in android.os.PowerManager.
313 * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
314 * also be bumped.
Dianne Hackborn617f8772009-03-31 15:04:46 -0700315 */
316 static final String[] USER_ACTIVITY_TYPES = {
Jeff Browndf693de2012-07-27 12:03:38 -0700317 "other", "button", "touch"
Dianne Hackborn617f8772009-03-31 15:04:46 -0700318 };
319
Jeff Browndf693de2012-07-27 12:03:38 -0700320 public static final int NUM_USER_ACTIVITY_TYPES = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700321
Dianne Hackborn617f8772009-03-31 15:04:46 -0700322 public abstract void noteUserActivityLocked(int type);
323 public abstract boolean hasUserActivity();
324 public abstract int getUserActivityCount(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700325
326 public abstract boolean hasNetworkActivity();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800327 public abstract long getNetworkActivityBytes(int type, int which);
328 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 public static abstract class Sensor {
Mathias Agopian7f84c062013-02-04 19:22:47 -0800331 /*
332 * FIXME: it's not correct to use this magic value because it
333 * could clash with a sensor handle (which are defined by
334 * the sensor HAL, and therefore out of our control
335 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 // Magic sensor number for the GPS.
337 public static final int GPS = -10000;
338
339 public abstract int getHandle();
340
341 public abstract Timer getSensorTime();
342 }
343
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700344 public class Pid {
345 public long mWakeSum;
346 public long mWakeStart;
347 }
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 /**
350 * The statistics associated with a particular process.
351 */
352 public static abstract class Proc {
353
Dianne Hackborn287952c2010-09-22 22:34:31 -0700354 public static class ExcessivePower {
355 public static final int TYPE_WAKE = 1;
356 public static final int TYPE_CPU = 2;
357
358 public int type;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700359 public long overTime;
360 public long usedTime;
361 }
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800364 * Returns true if this process is still active in the battery stats.
365 */
366 public abstract boolean isActive();
367
368 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 * Returns the total time (in 1/100 sec) spent executing in user code.
370 *
371 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
372 */
373 public abstract long getUserTime(int which);
374
375 /**
376 * Returns the total time (in 1/100 sec) spent executing in system code.
377 *
378 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
379 */
380 public abstract long getSystemTime(int which);
381
382 /**
383 * Returns the number of times the process has been started.
384 *
385 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
386 */
387 public abstract int getStarts(int which);
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700388
389 /**
390 * Returns the cpu time spent in microseconds while the process was in the foreground.
391 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
392 * @return foreground cpu time in microseconds
393 */
394 public abstract long getForegroundTime(int which);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700395
396 /**
397 * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
398 * @param speedStep the index of the CPU speed. This is not the actual speed of the
399 * CPU.
400 * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
401 * @see BatteryStats#getCpuSpeedSteps()
402 */
403 public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700404
Dianne Hackborn287952c2010-09-22 22:34:31 -0700405 public abstract int countExcessivePowers();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700406
Dianne Hackborn287952c2010-09-22 22:34:31 -0700407 public abstract ExcessivePower getExcessivePower(int i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409
410 /**
411 * The statistics associated with a particular package.
412 */
413 public static abstract class Pkg {
414
415 /**
416 * Returns the number of times this package has done something that could wake up the
417 * device from sleep.
418 *
419 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
420 */
421 public abstract int getWakeups(int which);
422
423 /**
424 * Returns a mapping containing service statistics.
425 */
426 public abstract Map<String, ? extends Serv> getServiceStats();
427
428 /**
429 * The statistics associated with a particular service.
430 */
431 public abstract class Serv {
432
433 /**
434 * Returns the amount of time spent started.
435 *
436 * @param batteryUptime elapsed uptime on battery in microseconds.
437 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
438 * @return
439 */
440 public abstract long getStartTime(long batteryUptime, int which);
441
442 /**
443 * Returns the total number of times startService() has been called.
444 *
445 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
446 */
447 public abstract int getStarts(int which);
448
449 /**
450 * Returns the total number times the service has been launched.
451 *
452 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
453 */
454 public abstract int getLaunches(int which);
455 }
456 }
457 }
458
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800459 public final static class HistoryTag {
460 public String string;
461 public int uid;
462
463 public int poolIdx;
464
465 public void setTo(HistoryTag o) {
466 string = o.string;
467 uid = o.uid;
468 poolIdx = o.poolIdx;
469 }
470
471 public void setTo(String _string, int _uid) {
472 string = _string;
473 uid = _uid;
474 poolIdx = -1;
475 }
476
477 public void writeToParcel(Parcel dest, int flags) {
478 dest.writeString(string);
479 dest.writeInt(uid);
480 }
481
482 public void readFromParcel(Parcel src) {
483 string = src.readString();
484 uid = src.readInt();
485 poolIdx = -1;
486 }
487
488 @Override
489 public boolean equals(Object o) {
490 if (this == o) return true;
491 if (o == null || getClass() != o.getClass()) return false;
492
493 HistoryTag that = (HistoryTag) o;
494
495 if (uid != that.uid) return false;
496 if (!string.equals(that.string)) return false;
497
498 return true;
499 }
500
501 @Override
502 public int hashCode() {
503 int result = string.hashCode();
504 result = 31 * result + uid;
505 return result;
506 }
507 }
508
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700509 public final static class HistoryItem implements Parcelable {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700510 public HistoryItem next;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700511
512 public long time;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800513
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800514 public static final byte CMD_UPDATE = 0; // These can be written as deltas
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800515 public static final byte CMD_NULL = -1;
516 public static final byte CMD_START = 4;
517 public static final byte CMD_OVERFLOW = 5;
518
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700519 public byte cmd = CMD_NULL;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700520
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800521 /**
522 * Return whether the command code is a delta data update.
523 */
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800524 public boolean isDeltaData() {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800525 return cmd == CMD_UPDATE;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800526 }
527
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700528 public byte batteryLevel;
529 public byte batteryStatus;
530 public byte batteryHealth;
531 public byte batteryPlugType;
532
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900533 public short batteryTemperature;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700534 public char batteryVoltage;
535
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700536 // Constants from SCREEN_BRIGHTNESS_*
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700537 public static final int STATE_BRIGHTNESS_SHIFT = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800538 public static final int STATE_BRIGHTNESS_MASK = 0x7;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700539 // Constants from SIGNAL_STRENGTH_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800540 public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
541 public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700542 // Constants from ServiceState.STATE_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800543 public static final int STATE_PHONE_STATE_SHIFT = 6;
544 public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700545 // Constants from DATA_CONNECTION_*
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800546 public static final int STATE_DATA_CONNECTION_SHIFT = 9;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800547 public static final int STATE_DATA_CONNECTION_MASK = 0x1f << STATE_DATA_CONNECTION_SHIFT;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800548
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700549 // These states always appear directly in the first int token
550 // of a delta change; they should be ones that change relatively
551 // frequently.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800552 public static final int STATE_WAKE_LOCK_FLAG = 1<<31;
553 public static final int STATE_SENSOR_ON_FLAG = 1<<30;
554 public static final int STATE_GPS_ON_FLAG = 1<<29;
555 public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<28;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800556 public static final int STATE_WIFI_SCAN_FLAG = 1<<27;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800557 public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<26;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800558 public static final int STATE_MOBILE_RADIO_ACTIVE_FLAG = 1<<25;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800559 public static final int STATE_WIFI_RUNNING_FLAG = 1<<24;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700560 // These are on the lower bits used for the command; if they change
561 // we need to write another int of data.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800562 public static final int STATE_PHONE_SCANNING_FLAG = 1<<23;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700563 public static final int STATE_AUDIO_ON_FLAG = 1<<22;
564 public static final int STATE_VIDEO_ON_FLAG = 1<<21;
565 public static final int STATE_SCREEN_ON_FLAG = 1<<20;
566 public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
567 public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
568 public static final int STATE_WIFI_ON_FLAG = 1<<17;
569 public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700570
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700571 public static final int MOST_INTERESTING_STATES =
572 STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
573 | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
574
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700575 public int states;
576
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800577 // The wake lock that was acquired at this point.
578 public HistoryTag wakelockTag;
579
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800580 public static final int EVENT_FLAG_START = 0x8000;
581 public static final int EVENT_FLAG_FINISH = 0x4000;
582
583 // No event in this item.
584 public static final int EVENT_NONE = 0x0000;
585 // Event is about a process that is running.
586 public static final int EVENT_PROC = 0x0001;
587 // Event is about an application package that is in the foreground.
588 public static final int EVENT_FOREGROUND = 0x0002;
589 // Event is about an application package that is at the top of the screen.
590 public static final int EVENT_TOP = 0x0003;
591 // Number of event types.
592 public static final int EVENT_COUNT = 0x0004;
593
594 public static final int EVENT_PROC_START = EVENT_PROC | EVENT_FLAG_START;
595 public static final int EVENT_PROC_FINISH = EVENT_PROC | EVENT_FLAG_FINISH;
596 public static final int EVENT_FOREGROUND_START = EVENT_FOREGROUND | EVENT_FLAG_START;
597 public static final int EVENT_FOREGROUND_FINISH = EVENT_FOREGROUND | EVENT_FLAG_FINISH;
598 public static final int EVENT_TOP_START = EVENT_TOP | EVENT_FLAG_START;
599 public static final int EVENT_TOP_FINISH = EVENT_TOP | EVENT_FLAG_FINISH;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800600
601 // For CMD_EVENT.
602 public int eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800603 public HistoryTag eventTag;
604
605 // Meta-data when reading.
606 public int numReadInts;
607
608 // Pre-allocated objects.
609 public final HistoryTag localWakelockTag = new HistoryTag();
610 public final HistoryTag localEventTag = new HistoryTag();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800611
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700612 public HistoryItem() {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700613 }
614
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700615 public HistoryItem(long time, Parcel src) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700616 this.time = time;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800617 numReadInts = 2;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700618 readFromParcel(src);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700619 }
620
621 public int describeContents() {
622 return 0;
623 }
624
625 public void writeToParcel(Parcel dest, int flags) {
626 dest.writeLong(time);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700627 int bat = (((int)cmd)&0xff)
628 | ((((int)batteryLevel)<<8)&0xff00)
629 | ((((int)batteryStatus)<<16)&0xf0000)
630 | ((((int)batteryHealth)<<20)&0xf00000)
631 | ((((int)batteryPlugType)<<24)&0xf000000);
632 dest.writeInt(bat);
633 bat = (((int)batteryTemperature)&0xffff)
634 | ((((int)batteryVoltage)<<16)&0xffff0000);
635 dest.writeInt(bat);
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700636 dest.writeInt(states);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800637 if (wakelockTag != null) {
638 dest.writeInt(1);
639 wakelockTag.writeToParcel(dest, flags);
640 } else {
641 dest.writeInt(0);
642 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800643 dest.writeInt(eventCode);
644 if (eventCode != EVENT_NONE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800645 dest.writeInt(eventCode);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800646 eventTag.writeToParcel(dest, flags);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800647 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700648 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700649
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800650 public void readFromParcel(Parcel src) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800651 int start = src.dataPosition();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700652 int bat = src.readInt();
653 cmd = (byte)(bat&0xff);
654 batteryLevel = (byte)((bat>>8)&0xff);
655 batteryStatus = (byte)((bat>>16)&0xf);
656 batteryHealth = (byte)((bat>>20)&0xf);
657 batteryPlugType = (byte)((bat>>24)&0xf);
658 bat = src.readInt();
Sungmin Choic7e9e8b2013-01-16 12:57:36 +0900659 batteryTemperature = (short)(bat&0xffff);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700660 batteryVoltage = (char)((bat>>16)&0xffff);
661 states = src.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800662 if (src.readInt() != 0) {
663 wakelockTag = localWakelockTag;
664 wakelockTag.readFromParcel(src);
665 } else {
666 wakelockTag = null;
667 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800668 eventCode = src.readInt();
669 if (eventCode != EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800670 eventTag = localEventTag;
671 eventTag.readFromParcel(src);
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700672 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800673 numReadInts += (src.dataPosition()-start)/4;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700674 }
675
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700676 public void clear() {
677 time = 0;
678 cmd = CMD_NULL;
679 batteryLevel = 0;
680 batteryStatus = 0;
681 batteryHealth = 0;
682 batteryPlugType = 0;
683 batteryTemperature = 0;
684 batteryVoltage = 0;
685 states = 0;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800686 wakelockTag = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800687 eventCode = EVENT_NONE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800688 eventTag = null;
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700689 }
690
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700691 public void setTo(HistoryItem o) {
692 time = o.time;
693 cmd = o.cmd;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800694 setToCommon(o);
695 }
696
697 public void setTo(long time, byte cmd, HistoryItem o) {
698 this.time = time;
699 this.cmd = cmd;
700 setToCommon(o);
701 }
702
703 private void setToCommon(HistoryItem o) {
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700704 batteryLevel = o.batteryLevel;
705 batteryStatus = o.batteryStatus;
706 batteryHealth = o.batteryHealth;
707 batteryPlugType = o.batteryPlugType;
708 batteryTemperature = o.batteryTemperature;
709 batteryVoltage = o.batteryVoltage;
710 states = o.states;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800711 if (o.wakelockTag != null) {
712 wakelockTag = localWakelockTag;
713 wakelockTag.setTo(o.wakelockTag);
714 } else {
715 wakelockTag = null;
716 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800717 eventCode = o.eventCode;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800718 if (o.eventTag != null) {
719 eventTag = localEventTag;
720 eventTag.setTo(o.eventTag);
721 } else {
722 eventTag = null;
723 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700724 }
725
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800726 public boolean sameNonEvent(HistoryItem o) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700727 return batteryLevel == o.batteryLevel
728 && batteryStatus == o.batteryStatus
729 && batteryHealth == o.batteryHealth
730 && batteryPlugType == o.batteryPlugType
731 && batteryTemperature == o.batteryTemperature
732 && batteryVoltage == o.batteryVoltage
733 && states == o.states;
734 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800735
736 public boolean same(HistoryItem o) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800737 if (!sameNonEvent(o) || eventCode != o.eventCode) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800738 return false;
739 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800740 if (wakelockTag != o.wakelockTag) {
741 if (wakelockTag == null || o.wakelockTag == null) {
742 return false;
743 }
744 if (!wakelockTag.equals(o.wakelockTag)) {
745 return false;
746 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800747 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800748 if (eventTag != o.eventTag) {
749 if (eventTag == null || o.eventTag == null) {
750 return false;
751 }
752 if (!eventTag.equals(o.eventTag)) {
753 return false;
754 }
755 }
756 return true;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800757 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700758 }
759
760 public static final class BitDescription {
761 public final int mask;
762 public final int shift;
763 public final String name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800764 public final String shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700765 public final String[] values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800766 public final String[] shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700767
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800768 public BitDescription(int mask, String name, String shortName) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700769 this.mask = mask;
770 this.shift = -1;
771 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800772 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700773 this.values = null;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800774 this.shortValues = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700775 }
776
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800777 public BitDescription(int mask, int shift, String name, String shortName,
778 String[] values, String[] shortValues) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700779 this.mask = mask;
780 this.shift = shift;
781 this.name = name;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800782 this.shortName = shortName;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700783 this.values = values;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800784 this.shortValues = shortValues;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700785 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700786 }
787
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800788 public abstract int getHistoryTotalSize();
789
790 public abstract int getHistoryUsedSize();
791
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700792 public abstract boolean startIteratingHistoryLocked();
793
Dianne Hackborn099bc622014-01-22 13:39:16 -0800794 public abstract int getHistoryStringPoolSize();
795
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800796 public abstract int getHistoryStringPoolBytes();
797
798 public abstract String getHistoryTagPoolString(int index);
799
800 public abstract int getHistoryTagPoolUid(int index);
Dianne Hackborn099bc622014-01-22 13:39:16 -0800801
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700802 public abstract boolean getNextHistoryLocked(HistoryItem out);
803
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700804 public abstract void finishIteratingHistoryLocked();
805
806 public abstract boolean startIteratingOldHistoryLocked();
807
808 public abstract boolean getNextOldHistoryLocked(HistoryItem out);
809
810 public abstract void finishIteratingOldHistoryLocked();
811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 /**
Dianne Hackbornb5e31652010-09-07 12:13:55 -0700813 * Return the base time offset for the battery history.
814 */
815 public abstract long getHistoryBaseTime();
816
817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 * Returns the number of times the device has been started.
819 */
820 public abstract int getStartCount();
821
822 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700823 * Returns the time in microseconds that the screen has been on while the device was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 * running on battery.
825 *
826 * {@hide}
827 */
828 public abstract long getScreenOnTime(long batteryRealtime, int which);
829
Dianne Hackborn617f8772009-03-31 15:04:46 -0700830 public static final int SCREEN_BRIGHTNESS_DARK = 0;
831 public static final int SCREEN_BRIGHTNESS_DIM = 1;
832 public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
833 public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
834 public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
835
836 static final String[] SCREEN_BRIGHTNESS_NAMES = {
837 "dark", "dim", "medium", "light", "bright"
838 };
839
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800840 static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
841 "0", "1", "2", "3", "4"
842 };
843
Dianne Hackborn617f8772009-03-31 15:04:46 -0700844 public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
845
846 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700847 * Returns the time in microseconds that the screen has been on with
Dianne Hackborn617f8772009-03-31 15:04:46 -0700848 * the given brightness
849 *
850 * {@hide}
851 */
852 public abstract long getScreenBrightnessTime(int brightnessBin,
853 long batteryRealtime, int which);
854
855 public abstract int getInputEventCount(int which);
856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700858 * Returns the time in microseconds that the phone has been on while the device was
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 * running on battery.
860 *
861 * {@hide}
862 */
863 public abstract long getPhoneOnTime(long batteryRealtime, int which);
Dianne Hackborn627bba72009-03-24 22:32:56 -0700864
865 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700866 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700867 * the given signal strength.
868 *
869 * {@hide}
870 */
871 public abstract long getPhoneSignalStrengthTime(int strengthBin,
872 long batteryRealtime, int which);
873
Dianne Hackborn617f8772009-03-31 15:04:46 -0700874 /**
Amith Yamasanif37447b2009-10-08 18:28:01 -0700875 * Returns the time in microseconds that the phone has been trying to
876 * acquire a signal.
877 *
878 * {@hide}
879 */
880 public abstract long getPhoneSignalScanningTime(
881 long batteryRealtime, int which);
882
883 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700884 * Returns the number of times the phone has entered the given signal strength.
885 *
886 * {@hide}
887 */
888 public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
889
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800890 /**
891 * Returns the time in microseconds that the mobile network has been active
892 * (in a high power state).
893 *
894 * {@hide}
895 */
896 public abstract long getMobileRadioActiveTime(long batteryRealtime, int which);
897
898
Dianne Hackborn627bba72009-03-24 22:32:56 -0700899 public static final int DATA_CONNECTION_NONE = 0;
900 public static final int DATA_CONNECTION_GPRS = 1;
901 public static final int DATA_CONNECTION_EDGE = 2;
902 public static final int DATA_CONNECTION_UMTS = 3;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700903 public static final int DATA_CONNECTION_CDMA = 4;
904 public static final int DATA_CONNECTION_EVDO_0 = 5;
905 public static final int DATA_CONNECTION_EVDO_A = 6;
906 public static final int DATA_CONNECTION_1xRTT = 7;
907 public static final int DATA_CONNECTION_HSDPA = 8;
908 public static final int DATA_CONNECTION_HSUPA = 9;
909 public static final int DATA_CONNECTION_HSPA = 10;
910 public static final int DATA_CONNECTION_IDEN = 11;
911 public static final int DATA_CONNECTION_EVDO_B = 12;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700912 public static final int DATA_CONNECTION_LTE = 13;
913 public static final int DATA_CONNECTION_EHRPD = 14;
Patrick Tjinb71703c2013-11-06 09:27:03 -0800914 public static final int DATA_CONNECTION_HSPAP = 15;
915 public static final int DATA_CONNECTION_OTHER = 16;
Robert Greenwalt962a9902010-11-02 11:10:25 -0700916
Dianne Hackborn627bba72009-03-24 22:32:56 -0700917 static final String[] DATA_CONNECTION_NAMES = {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700918 "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
Robert Greenwalt962a9902010-11-02 11:10:25 -0700919 "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
Patrick Tjinb71703c2013-11-06 09:27:03 -0800920 "ehrpd", "hspap", "other"
Dianne Hackborn627bba72009-03-24 22:32:56 -0700921 };
922
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700923 public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
Dianne Hackborn627bba72009-03-24 22:32:56 -0700924
925 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700926 * Returns the time in microseconds that the phone has been running with
Dianne Hackborn627bba72009-03-24 22:32:56 -0700927 * the given data connection.
928 *
929 * {@hide}
930 */
931 public abstract long getPhoneDataConnectionTime(int dataType,
932 long batteryRealtime, int which);
933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700935 * Returns the number of times the phone has entered the given data
936 * connection type.
937 *
938 * {@hide}
939 */
940 public abstract int getPhoneDataConnectionCount(int dataType, int which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700941
942 public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
943 = new BitDescription[] {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800944 new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
945 new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800946 new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800947 new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
948 new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
949 new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800950 new BitDescription(HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG, "mobile_radio", "Pr"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800951 new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
952 new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800953 new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
954 new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800955 new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
956 new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
957 new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
958 new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
959 new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
960 new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
961 HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
962 DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
963 new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
964 HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
965 new String[] {"in", "out", "emergency", "off"},
966 new String[] {"in", "out", "em", "off"}),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700967 new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800968 HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
969 SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
970 "0", "1", "2", "3", "4"
971 }),
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800972 new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
973 HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
974 SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700975 };
Dianne Hackborn617f8772009-03-31 15:04:46 -0700976
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800977 public static final String[] HISTORY_EVENT_NAMES = new String[] {
978 "null", "proc", "fg", "top"
979 };
980
981 public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
982 "Nl", "Pr", "Fg", "Tp"
983 };
984
Dianne Hackborn617f8772009-03-31 15:04:46 -0700985 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700986 * Returns the time in microseconds that wifi has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -0700987 * running on battery.
988 *
989 * {@hide}
990 */
991 public abstract long getWifiOnTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700992
993 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -0700994 * Returns the time in microseconds that wifi has been on and the driver has
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700995 * been in the running state while the device was running on battery.
996 *
997 * {@hide}
998 */
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700999 public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001000
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001001 public static final int WIFI_STATE_OFF = 0;
1002 public static final int WIFI_STATE_OFF_SCANNING = 1;
1003 public static final int WIFI_STATE_ON_NO_NETWORKS = 2;
1004 public static final int WIFI_STATE_ON_DISCONNECTED = 3;
1005 public static final int WIFI_STATE_ON_CONNECTED_STA = 4;
1006 public static final int WIFI_STATE_ON_CONNECTED_P2P = 5;
1007 public static final int WIFI_STATE_ON_CONNECTED_STA_P2P = 6;
1008 public static final int WIFI_STATE_SOFT_AP = 7;
1009
1010 static final String[] WIFI_STATE_NAMES = {
1011 "off", "scanning", "no_net", "disconn",
1012 "sta", "p2p", "sta_p2p", "soft_ap"
1013 };
1014
1015 public static final int NUM_WIFI_STATES = WIFI_STATE_SOFT_AP+1;
1016
1017 /**
1018 * Returns the time in microseconds that WiFi has been running in the given state.
1019 *
1020 * {@hide}
1021 */
1022 public abstract long getWifiStateTime(int wifiState,
1023 long batteryRealtime, int which);
1024
1025 /**
1026 * Returns the number of times that WiFi has entered the given state.
1027 *
1028 * {@hide}
1029 */
1030 public abstract int getWifiStateCount(int wifiState, int which);
1031
The Android Open Source Project10592532009-03-18 17:39:46 -07001032 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001033 * Returns the time in microseconds that bluetooth has been on while the device was
The Android Open Source Project10592532009-03-18 17:39:46 -07001034 * running on battery.
1035 *
1036 * {@hide}
1037 */
1038 public abstract long getBluetoothOnTime(long batteryRealtime, int which);
1039
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001040 public abstract int getBluetoothPingCount();
1041
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001042 public static final int BLUETOOTH_STATE_INACTIVE = 0;
1043 public static final int BLUETOOTH_STATE_LOW = 1;
1044 public static final int BLUETOOTH_STATE_MEDIUM = 2;
1045 public static final int BLUETOOTH_STATE_HIGH = 3;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001046
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001047 static final String[] BLUETOOTH_STATE_NAMES = {
1048 "inactive", "low", "med", "high"
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001049 };
1050
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001051 public static final int NUM_BLUETOOTH_STATES = BLUETOOTH_STATE_HIGH +1;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001052
1053 /**
1054 * Returns the time in microseconds that Bluetooth has been running in the
1055 * given active state.
1056 *
1057 * {@hide}
1058 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001059 public abstract long getBluetoothStateTime(int bluetoothState,
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001060 long batteryRealtime, int which);
1061
1062 /**
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001063 * Returns the number of times that Bluetooth has entered the given active state.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001064 *
1065 * {@hide}
1066 */
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001067 public abstract int getBluetoothStateCount(int bluetoothState, int which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001068
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001069 public static final int NETWORK_MOBILE_RX_DATA = 0;
1070 public static final int NETWORK_MOBILE_TX_DATA = 1;
1071 public static final int NETWORK_WIFI_RX_DATA = 2;
1072 public static final int NETWORK_WIFI_TX_DATA = 3;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001073
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001074 public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001075
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001076 public abstract long getNetworkActivityBytes(int type, int which);
1077 public abstract long getNetworkActivityPackets(int type, int which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001078
The Android Open Source Project10592532009-03-18 17:39:46 -07001079 /**
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001080 * Return the wall clock time when battery stats data collection started.
1081 */
1082 public abstract long getStartClockTime();
1083
1084 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 * Return whether we are currently running on battery.
1086 */
1087 public abstract boolean getIsOnBattery();
1088
1089 /**
1090 * Returns a SparseArray containing the statistics for each uid.
1091 */
1092 public abstract SparseArray<? extends Uid> getUidStats();
1093
1094 /**
1095 * Returns the current battery uptime in microseconds.
1096 *
1097 * @param curTime the amount of elapsed realtime in microseconds.
1098 */
1099 public abstract long getBatteryUptime(long curTime);
1100
1101 /**
1102 * Returns the current battery realtime in microseconds.
1103 *
1104 * @param curTime the amount of elapsed realtime in microseconds.
1105 */
1106 public abstract long getBatteryRealtime(long curTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001107
1108 /**
Evan Millar633a1742009-04-02 16:36:33 -07001109 * Returns the battery percentage level at the last time the device was unplugged from power, or
1110 * the last time it booted on battery power.
The Android Open Source Project10592532009-03-18 17:39:46 -07001111 */
Evan Millar633a1742009-04-02 16:36:33 -07001112 public abstract int getDischargeStartLevel();
The Android Open Source Project10592532009-03-18 17:39:46 -07001113
1114 /**
Evan Millar633a1742009-04-02 16:36:33 -07001115 * Returns the current battery percentage level if we are in a discharge cycle, otherwise
1116 * returns the level at the last plug event.
The Android Open Source Project10592532009-03-18 17:39:46 -07001117 */
Evan Millar633a1742009-04-02 16:36:33 -07001118 public abstract int getDischargeCurrentLevel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119
1120 /**
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001121 * Get the amount the battery has discharged since the stats were
1122 * last reset after charging, as a lower-end approximation.
1123 */
1124 public abstract int getLowDischargeAmountSinceCharge();
1125
1126 /**
1127 * Get the amount the battery has discharged since the stats were
1128 * last reset after charging, as an upper-end approximation.
1129 */
1130 public abstract int getHighDischargeAmountSinceCharge();
1131
1132 /**
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001133 * Get the amount the battery has discharged while the screen was on,
1134 * since the last time power was unplugged.
1135 */
1136 public abstract int getDischargeAmountScreenOn();
1137
1138 /**
1139 * Get the amount the battery has discharged while the screen was on,
1140 * since the last time the device was charged.
1141 */
1142 public abstract int getDischargeAmountScreenOnSinceCharge();
1143
1144 /**
1145 * Get the amount the battery has discharged while the screen was off,
1146 * since the last time power was unplugged.
1147 */
1148 public abstract int getDischargeAmountScreenOff();
1149
1150 /**
1151 * Get the amount the battery has discharged while the screen was off,
1152 * since the last time the device was charged.
1153 */
1154 public abstract int getDischargeAmountScreenOffSinceCharge();
1155
1156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 * Returns the total, last, or current battery uptime in microseconds.
1158 *
1159 * @param curTime the elapsed realtime in microseconds.
1160 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1161 */
1162 public abstract long computeBatteryUptime(long curTime, int which);
1163
1164 /**
1165 * Returns the total, last, or current battery realtime in microseconds.
1166 *
1167 * @param curTime the current elapsed realtime in microseconds.
1168 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1169 */
1170 public abstract long computeBatteryRealtime(long curTime, int which);
1171
1172 /**
1173 * Returns the total, last, or current uptime in microseconds.
1174 *
1175 * @param curTime the current elapsed realtime in microseconds.
1176 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1177 */
1178 public abstract long computeUptime(long curTime, int which);
1179
1180 /**
1181 * Returns the total, last, or current realtime in microseconds.
1182 * *
1183 * @param curTime the current elapsed realtime in microseconds.
1184 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1185 */
1186 public abstract long computeRealtime(long curTime, int which);
Evan Millarc64edde2009-04-18 12:26:32 -07001187
1188 public abstract Map<String, ? extends Timer> getKernelWakelockStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189
Amith Yamasanie43530a2009-08-21 13:11:37 -07001190 /** Returns the number of different speeds that the CPU can run at */
1191 public abstract int getCpuSpeedSteps();
1192
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001193 public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1194
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001195 private final static void formatTimeRaw(StringBuilder out, long seconds) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 long days = seconds / (60 * 60 * 24);
1197 if (days != 0) {
1198 out.append(days);
1199 out.append("d ");
1200 }
1201 long used = days * 60 * 60 * 24;
1202
1203 long hours = (seconds - used) / (60 * 60);
1204 if (hours != 0 || used != 0) {
1205 out.append(hours);
1206 out.append("h ");
1207 }
1208 used += hours * 60 * 60;
1209
1210 long mins = (seconds-used) / 60;
1211 if (mins != 0 || used != 0) {
1212 out.append(mins);
1213 out.append("m ");
1214 }
1215 used += mins * 60;
1216
1217 if (seconds != 0 || used != 0) {
1218 out.append(seconds-used);
1219 out.append("s ");
1220 }
1221 }
1222
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001223 private final static void formatTime(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 long sec = time / 100;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001225 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 sb.append((time - (sec * 100)) * 10);
1227 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001230 private final static void formatTimeMs(StringBuilder sb, long time) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 long sec = time / 1000;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001232 formatTimeRaw(sb, sec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 sb.append(time - (sec * 1000));
1234 sb.append("ms ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236
1237 private final String formatRatioLocked(long num, long den) {
1238 if (den == 0L) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001239 return "--%";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 }
1241 float perc = ((float)num) / ((float)den) * 100;
1242 mFormatBuilder.setLength(0);
1243 mFormatter.format("%.1f%%", perc);
1244 return mFormatBuilder.toString();
1245 }
1246
Evan Millar22ac0432009-03-31 11:33:18 -07001247 private final String formatBytesLocked(long bytes) {
1248 mFormatBuilder.setLength(0);
1249
1250 if (bytes < BYTES_PER_KB) {
1251 return bytes + "B";
1252 } else if (bytes < BYTES_PER_MB) {
1253 mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1254 return mFormatBuilder.toString();
1255 } else if (bytes < BYTES_PER_GB){
1256 mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1257 return mFormatBuilder.toString();
1258 } else {
1259 mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1260 return mFormatBuilder.toString();
1261 }
1262 }
1263
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001264 private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1265 if (timer != null) {
1266 // Convert from microseconds to milliseconds with rounding
1267 long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1268 long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1269 return totalTimeMillis;
1270 }
1271 return 0;
1272 }
1273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 *
1276 * @param sb a StringBuilder object.
1277 * @param timer a Timer object contining the wakelock times.
1278 * @param batteryRealtime the current on-battery time in microseconds.
1279 * @param name the name of the wakelock.
1280 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1281 * @param linePrefix a String to be prepended to each line of output.
1282 * @return the line prefix
1283 */
1284 private static final String printWakeLock(StringBuilder sb, Timer timer,
1285 long batteryRealtime, String name, int which, String linePrefix) {
1286
1287 if (timer != null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001288 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289
Evan Millarc64edde2009-04-18 12:26:32 -07001290 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 if (totalTimeMillis != 0) {
1292 sb.append(linePrefix);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001293 formatTimeMs(sb, totalTimeMillis);
Dianne Hackborn81038902012-11-26 17:04:09 -08001294 if (name != null) {
1295 sb.append(name);
1296 sb.append(' ');
1297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 sb.append('(');
1299 sb.append(count);
1300 sb.append(" times)");
1301 return ", ";
1302 }
1303 }
1304 return linePrefix;
1305 }
1306
1307 /**
1308 * Checkin version of wakelock printer. Prints simple comma-separated list.
1309 *
1310 * @param sb a StringBuilder object.
1311 * @param timer a Timer object contining the wakelock times.
1312 * @param now the current time in microseconds.
1313 * @param name the name of the wakelock.
1314 * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1315 * @param linePrefix a String to be prepended to each line of output.
1316 * @return the line prefix
1317 */
1318 private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
Evan Millarc64edde2009-04-18 12:26:32 -07001319 String name, int which, String linePrefix) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 long totalTimeMicros = 0;
1321 int count = 0;
1322 if (timer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001323 totalTimeMicros = timer.getTotalTimeLocked(now, which);
1324 count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 }
1326 sb.append(linePrefix);
1327 sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1328 sb.append(',');
Evan Millarc64edde2009-04-18 12:26:32 -07001329 sb.append(name != null ? name + "," : "");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 sb.append(count);
1331 return ",";
1332 }
1333
1334 /**
1335 * Dump a comma-separated line of values for terse checkin mode.
1336 *
1337 * @param pw the PageWriter to dump log to
1338 * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1339 * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" , "process", "network")
1340 * @param args type-dependent data arguments
1341 */
1342 private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1343 Object... args ) {
1344 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1345 pw.print(uid); pw.print(',');
1346 pw.print(category); pw.print(',');
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001347 pw.print(type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348
1349 for (Object arg : args) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001350 pw.print(',');
1351 pw.print(arg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001353 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355
1356 /**
1357 * Checkin server version of dump to produce more compact, computer-readable log.
1358 *
1359 * NOTE: all times are expressed in 'ms'.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 */
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001361 public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1363 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1364 final long batteryUptime = getBatteryUptime(rawUptime);
1365 final long batteryRealtime = getBatteryRealtime(rawRealtime);
1366 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1367 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1368 final long totalRealtime = computeRealtime(rawRealtime, which);
1369 final long totalUptime = computeUptime(rawUptime, which);
1370 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1371 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001372 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001373 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001374 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375
1376 StringBuilder sb = new StringBuilder(128);
1377
Evan Millar22ac0432009-03-31 11:33:18 -07001378 SparseArray<? extends Uid> uidStats = getUidStats();
1379 final int NU = uidStats.size();
1380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 String category = STAT_NAMES[which];
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 // Dump "battery" stat
1384 dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001385 which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
Dianne Hackborn617f8772009-03-31 15:04:46 -07001386 whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001387 totalRealtime / 1000, totalUptime / 1000,
1388 getStartClockTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001390 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001391 long fullWakeLockTimeTotal = 0;
1392 long partialWakeLockTimeTotal = 0;
1393
1394 for (int iu = 0; iu < NU; iu++) {
1395 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001396
Evan Millar22ac0432009-03-31 11:33:18 -07001397 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1398 if (wakelocks.size() > 0) {
1399 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1400 : wakelocks.entrySet()) {
1401 Uid.Wakelock wl = ent.getValue();
1402
1403 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1404 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001405 fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
Evan Millar22ac0432009-03-31 11:33:18 -07001406 }
1407
1408 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1409 if (partialWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001410 partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001411 batteryRealtime, which);
1412 }
1413 }
1414 }
1415 }
1416
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001417 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1418 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1419 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1420 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1421 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1422 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1423 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1424 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1425
1426 // Dump network stats
1427 dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1428 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1429 mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 // Dump misc stats
1432 dumpLine(pw, 0 /* uid */, category, MISC_DATA,
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001433 screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001434 wifiRunningTime / 1000, bluetoothOnTime / 1000,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001435 mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
Dianne Hackborn617f8772009-03-31 15:04:46 -07001436 fullWakeLockTimeTotal, partialWakeLockTimeTotal,
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001437 getInputEventCount(which), getMobileRadioActiveTime(batteryRealtime, which));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001438
1439 // Dump screen brightness stats
1440 Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1441 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1442 args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1443 }
1444 dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
The Android Open Source Project10592532009-03-18 17:39:46 -07001445
Dianne Hackborn627bba72009-03-24 22:32:56 -07001446 // Dump signal strength stats
Wink Saville52840902011-02-18 12:40:47 -08001447 args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1448 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001449 args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1450 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001451 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
Amith Yamasanif37447b2009-10-08 18:28:01 -07001452 dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1453 getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
Wink Saville52840902011-02-18 12:40:47 -08001454 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07001455 args[i] = getPhoneSignalStrengthCount(i, which);
1456 }
1457 dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001458
Dianne Hackborn627bba72009-03-24 22:32:56 -07001459 // Dump network type stats
1460 args = new Object[NUM_DATA_CONNECTION_TYPES];
1461 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1462 args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1463 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001464 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1465 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1466 args[i] = getPhoneDataConnectionCount(i, which);
1467 }
1468 dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08001469
1470 // Dump wifi state stats
1471 args = new Object[NUM_WIFI_STATES];
1472 for (int i=0; i<NUM_WIFI_STATES; i++) {
1473 args[i] = getWifiStateTime(i, batteryRealtime, which) / 1000;
1474 }
1475 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_TIME_DATA, args);
1476 for (int i=0; i<NUM_WIFI_STATES; i++) {
1477 args[i] = getWifiStateCount(i, which);
1478 }
1479 dumpLine(pw, 0 /* uid */, category, WIFI_STATE_COUNT_DATA, args);
1480
1481 // Dump bluetooth state stats
1482 args = new Object[NUM_BLUETOOTH_STATES];
1483 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1484 args[i] = getBluetoothStateTime(i, batteryRealtime, which) / 1000;
1485 }
1486 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_TIME_DATA, args);
1487 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
1488 args[i] = getBluetoothStateCount(i, which);
1489 }
1490 dumpLine(pw, 0 /* uid */, category, BLUETOOTH_STATE_COUNT_DATA, args);
1491
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001492 if (which == STATS_SINCE_UNPLUGGED) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001493 dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
Evan Millar633a1742009-04-02 16:36:33 -07001494 getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07001495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08001497 if (which == STATS_SINCE_UNPLUGGED) {
1498 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1499 getDischargeStartLevel()-getDischargeCurrentLevel(),
1500 getDischargeStartLevel()-getDischargeCurrentLevel(),
1501 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1502 } else {
1503 dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1504 getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1505 getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1506 }
1507
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001508 if (reqUid < 0) {
1509 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1510 if (kernelWakelocks.size() > 0) {
1511 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1512 sb.setLength(0);
1513 printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1514
1515 dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1516 sb.toString());
1517 }
Evan Millarc64edde2009-04-18 12:26:32 -07001518 }
1519 }
1520
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001521 BatteryStatsHelper helper = new BatteryStatsHelper(context);
1522 helper.create(this);
1523 helper.refreshStats(which, UserHandle.USER_ALL);
1524 List<BatterySipper> sippers = helper.getUsageList();
1525 if (sippers != null && sippers.size() > 0) {
1526 dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1527 BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
Dianne Hackborn099bc622014-01-22 13:39:16 -08001528 BatteryStatsHelper.makemAh(helper.getComputedPower()),
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001529 BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1530 BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1531 for (int i=0; i<sippers.size(); i++) {
1532 BatterySipper bs = sippers.get(i);
1533 int uid = 0;
1534 String label;
1535 switch (bs.drainType) {
1536 case IDLE:
1537 label="idle";
1538 break;
1539 case CELL:
1540 label="cell";
1541 break;
1542 case PHONE:
1543 label="phone";
1544 break;
1545 case WIFI:
1546 label="wifi";
1547 break;
1548 case BLUETOOTH:
1549 label="blue";
1550 break;
1551 case SCREEN:
1552 label="scrn";
1553 break;
1554 case APP:
1555 uid = bs.uidObj.getUid();
1556 label = "uid";
1557 break;
1558 case USER:
1559 uid = UserHandle.getUid(bs.userId, 0);
1560 label = "user";
1561 break;
1562 case UNACCOUNTED:
1563 label = "unacc";
1564 break;
1565 case OVERCOUNTED:
1566 label = "over";
1567 break;
1568 default:
1569 label = "???";
1570 }
1571 dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1572 BatteryStatsHelper.makemAh(bs.value));
1573 }
1574 }
1575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 for (int iu = 0; iu < NU; iu++) {
1577 final int uid = uidStats.keyAt(iu);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001578 if (reqUid >= 0 && uid != reqUid) {
1579 continue;
1580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 Uid u = uidStats.valueAt(iu);
1582 // Dump Network stats per uid, if any
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001583 long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1584 long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1585 long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1586 long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1587 long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1588 long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1589 long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1590 long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001591 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07001592 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001593 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001594
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001595 if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1596 || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
1597 || wifiPacketsTx > 0) {
1598 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1599 wifiBytesRx, wifiBytesTx,
1600 mobilePacketsRx, mobilePacketsTx,
1601 wifiPacketsRx, wifiPacketsTx);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001602 }
1603
Nick Pelly6ccaa542012-06-15 15:22:47 -07001604 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001605 || uidWifiRunningTime != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07001606 dumpLine(pw, uid, category, WIFI_DATA,
1607 fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
The Android Open Source Project10592532009-03-18 17:39:46 -07001608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609
Dianne Hackborn617f8772009-03-31 15:04:46 -07001610 if (u.hasUserActivity()) {
1611 args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1612 boolean hasData = false;
1613 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1614 int val = u.getUserActivityCount(i, which);
1615 args[i] = val;
1616 if (val != 0) hasData = true;
1617 }
1618 if (hasData) {
1619 dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1620 }
1621 }
1622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1624 if (wakelocks.size() > 0) {
1625 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1626 : wakelocks.entrySet()) {
1627 Uid.Wakelock wl = ent.getValue();
1628 String linePrefix = "";
1629 sb.setLength(0);
Evan Millarc64edde2009-04-18 12:26:32 -07001630 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1631 batteryRealtime, "f", which, linePrefix);
1632 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1633 batteryRealtime, "p", which, linePrefix);
1634 linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1635 batteryRealtime, "w", which, linePrefix);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636
1637 // Only log if we had at lease one wakelock...
1638 if (sb.length() > 0) {
Dianne Hackborn13ac0412013-06-25 19:34:49 -07001639 String name = ent.getKey();
1640 if (name.indexOf(',') >= 0) {
1641 name = name.replace(',', '_');
1642 }
1643 dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 }
1646 }
1647
1648 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1649 if (sensors.size() > 0) {
1650 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1651 : sensors.entrySet()) {
1652 Uid.Sensor se = ent.getValue();
1653 int sensorNumber = ent.getKey();
1654 Timer timer = se.getSensorTime();
1655 if (timer != null) {
1656 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07001657 long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1658 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 if (totalTime != 0) {
1660 dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1661 }
1662 }
1663 }
1664 }
1665
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001666 Timer vibTimer = u.getVibratorOnTimer();
1667 if (vibTimer != null) {
1668 // Convert from microseconds to milliseconds with rounding
1669 long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1670 int count = vibTimer.getCountLocked(which);
1671 if (totalTime != 0) {
1672 dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1673 }
1674 }
1675
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001676 Timer fgTimer = u.getForegroundActivityTimer();
1677 if (fgTimer != null) {
1678 // Convert from microseconds to milliseconds with rounding
1679 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1680 int count = fgTimer.getCountLocked(which);
1681 if (totalTime != 0) {
1682 dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1683 }
1684 }
1685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1687 if (processStats.size() > 0) {
1688 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1689 : processStats.entrySet()) {
1690 Uid.Proc ps = ent.getValue();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001691
1692 final long userMillis = ps.getUserTime(which) * 10;
1693 final long systemMillis = ps.getSystemTime(which) * 10;
1694 final long foregroundMillis = ps.getForegroundTime(which) * 10;
1695 final long starts = ps.getStarts(which);
1696
1697 if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1698 || starts != 0) {
1699 dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1700 systemMillis, foregroundMillis, starts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
1702 }
1703 }
1704
1705 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1706 if (packageStats.size() > 0) {
1707 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1708 : packageStats.entrySet()) {
1709
1710 Uid.Pkg ps = ent.getValue();
1711 int wakeups = ps.getWakeups(which);
1712 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1713 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1714 : serviceStats.entrySet()) {
1715 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1716 long startTime = ss.getStartTime(batteryUptime, which);
1717 int starts = ss.getStarts(which);
1718 int launches = ss.getLaunches(which);
1719 if (startTime != 0 || starts != 0 || launches != 0) {
1720 dumpLine(pw, uid, category, APK_DATA,
1721 wakeups, // wakeup alarms
1722 ent.getKey(), // Apk
1723 sent.getKey(), // service
1724 startTime / 1000, // time spent started, in ms
1725 starts,
1726 launches);
1727 }
1728 }
1729 }
1730 }
1731 }
1732 }
1733
Dianne Hackborn81038902012-11-26 17:04:09 -08001734 static final class TimerEntry {
1735 final String mName;
1736 final int mId;
1737 final BatteryStats.Timer mTimer;
1738 final long mTime;
1739 TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1740 mName = name;
1741 mId = id;
1742 mTimer = timer;
1743 mTime = time;
1744 }
1745 }
1746
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001747 private void printmAh(PrintWriter printer, double power) {
1748 printer.print(BatteryStatsHelper.makemAh(power));
1749 }
1750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08001752 public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1753 int reqUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 final long rawUptime = SystemClock.uptimeMillis() * 1000;
1755 final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1756 final long batteryUptime = getBatteryUptime(rawUptime);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07001757 final long batteryRealtime = getBatteryRealtime(rawRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758
1759 final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1760 final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1761 final long totalRealtime = computeRealtime(rawRealtime, which);
1762 final long totalUptime = computeUptime(rawUptime, which);
1763
1764 StringBuilder sb = new StringBuilder(128);
Evan Millar22ac0432009-03-31 11:33:18 -07001765
1766 SparseArray<? extends Uid> uidStats = getUidStats();
1767 final int NU = uidStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001769 sb.setLength(0);
1770 sb.append(prefix);
1771 sb.append(" Time on battery: ");
1772 formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1773 sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1774 sb.append(") realtime, ");
1775 formatTimeMs(sb, whichBatteryUptime / 1000);
1776 sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1777 sb.append(") uptime");
1778 pw.println(sb.toString());
1779 sb.setLength(0);
1780 sb.append(prefix);
1781 sb.append(" Total run time: ");
1782 formatTimeMs(sb, totalRealtime / 1000);
1783 sb.append("realtime, ");
1784 formatTimeMs(sb, totalUptime / 1000);
1785 sb.append("uptime, ");
1786 pw.println(sb.toString());
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08001787 pw.print(" Start clock time: ");
1788 pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
1789
The Android Open Source Project10592532009-03-18 17:39:46 -07001790 final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1791 final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07001792 final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07001793 final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1794 final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001795 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001796 sb.append(prefix);
1797 sb.append(" Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1798 sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1799 sb.append("), Input events: "); sb.append(getInputEventCount(which));
1800 sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1801 sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1802 sb.append(")");
1803 pw.println(sb.toString());
1804 sb.setLength(0);
1805 sb.append(prefix);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001806 sb.append(" Screen brightnesses: ");
1807 boolean didOne = false;
1808 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1809 final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1810 if (time == 0) {
1811 continue;
1812 }
1813 if (didOne) sb.append(", ");
1814 didOne = true;
1815 sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1816 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001817 formatTimeMs(sb, time/1000);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001818 sb.append("(");
1819 sb.append(formatRatioLocked(time, screenOnTime));
1820 sb.append(")");
1821 }
1822 if (!didOne) sb.append("No activity");
1823 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07001824
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001825 // Calculate wakelock times across all uids.
Evan Millar22ac0432009-03-31 11:33:18 -07001826 long fullWakeLockTimeTotalMicros = 0;
1827 long partialWakeLockTimeTotalMicros = 0;
Dianne Hackborn81038902012-11-26 17:04:09 -08001828
1829 final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1830 @Override
1831 public int compare(TimerEntry lhs, TimerEntry rhs) {
1832 long lhsTime = lhs.mTime;
1833 long rhsTime = rhs.mTime;
1834 if (lhsTime < rhsTime) {
1835 return 1;
1836 }
1837 if (lhsTime > rhsTime) {
1838 return -1;
1839 }
1840 return 0;
1841 }
1842 };
1843
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001844 if (reqUid < 0) {
1845 Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1846 if (kernelWakelocks.size() > 0) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001847 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001848 for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001849 BatteryStats.Timer timer = ent.getValue();
1850 long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1851 if (totalTimeMillis > 0) {
1852 timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1853 }
1854 }
1855 Collections.sort(timers, timerComparator);
1856 for (int i=0; i<timers.size(); i++) {
1857 TimerEntry timer = timers.get(i);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001858 String linePrefix = ": ";
1859 sb.setLength(0);
1860 sb.append(prefix);
1861 sb.append(" Kernel Wake lock ");
Dianne Hackborn81038902012-11-26 17:04:09 -08001862 sb.append(timer.mName);
1863 linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1864 which, linePrefix);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001865 if (!linePrefix.equals(": ")) {
1866 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05001867 // Only print out wake locks that were held
1868 pw.println(sb.toString());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001869 }
Evan Millarc64edde2009-04-18 12:26:32 -07001870 }
Evan Millarc64edde2009-04-18 12:26:32 -07001871 }
1872 }
Dianne Hackborn81038902012-11-26 17:04:09 -08001873
1874 final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1875
Evan Millar22ac0432009-03-31 11:33:18 -07001876 for (int iu = 0; iu < NU; iu++) {
1877 Uid u = uidStats.valueAt(iu);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001878
Evan Millar22ac0432009-03-31 11:33:18 -07001879 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1880 if (wakelocks.size() > 0) {
1881 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1882 : wakelocks.entrySet()) {
1883 Uid.Wakelock wl = ent.getValue();
1884
1885 Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1886 if (fullWakeTimer != null) {
Evan Millarc64edde2009-04-18 12:26:32 -07001887 fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001888 batteryRealtime, which);
1889 }
1890
1891 Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1892 if (partialWakeTimer != null) {
Dianne Hackborn81038902012-11-26 17:04:09 -08001893 long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
Evan Millar22ac0432009-03-31 11:33:18 -07001894 batteryRealtime, which);
Dianne Hackborn81038902012-11-26 17:04:09 -08001895 if (totalTimeMicros > 0) {
1896 if (reqUid < 0) {
1897 // Only show the ordered list of all wake
1898 // locks if the caller is not asking for data
1899 // about a specific uid.
1900 timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1901 partialWakeTimer, totalTimeMicros));
1902 }
1903 partialWakeLockTimeTotalMicros += totalTimeMicros;
1904 }
Evan Millar22ac0432009-03-31 11:33:18 -07001905 }
1906 }
1907 }
1908 }
1909
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001910 long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1911 long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1912 long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1913 long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1914 long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1915 long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1916 long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1917 long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1918
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001919 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001920 pw.print(" Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
1921 pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
1922 pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
1923 pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001924 pw.print(prefix);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001925 pw.print(" Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
1926 pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
1927 pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
1928 pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001929 sb.setLength(0);
1930 sb.append(prefix);
1931 sb.append(" Total full wakelock time: "); formatTimeMs(sb,
1932 (fullWakeLockTimeTotalMicros + 500) / 1000);
Dianne Hackborn81038902012-11-26 17:04:09 -08001933 sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001934 (partialWakeLockTimeTotalMicros + 500) / 1000);
1935 pw.println(sb.toString());
Evan Millar22ac0432009-03-31 11:33:18 -07001936
Dianne Hackborn627bba72009-03-24 22:32:56 -07001937 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001938 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001939 sb.append(" Signal levels:");
Dianne Hackborn617f8772009-03-31 15:04:46 -07001940 didOne = false;
Wink Saville52840902011-02-18 12:40:47 -08001941 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07001942 final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1943 if (time == 0) {
1944 continue;
1945 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001946 sb.append("\n ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001947 didOne = true;
Wink Saville52840902011-02-18 12:40:47 -08001948 sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001949 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001950 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001951 sb.append("(");
1952 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001953 sb.append(") ");
1954 sb.append(getPhoneSignalStrengthCount(i, which));
1955 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001956 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001957 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001958 pw.println(sb.toString());
Amith Yamasanif37447b2009-10-08 18:28:01 -07001959
1960 sb.setLength(0);
1961 sb.append(prefix);
1962 sb.append(" Signal scanning time: ");
1963 formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1964 pw.println(sb.toString());
1965
Dianne Hackborn627bba72009-03-24 22:32:56 -07001966 sb.setLength(0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001967 sb.append(prefix);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001968 sb.append(" Radio types:");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001969 didOne = false;
1970 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1971 final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1972 if (time == 0) {
1973 continue;
1974 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001975 sb.append("\n ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001976 didOne = true;
1977 sb.append(DATA_CONNECTION_NAMES[i]);
1978 sb.append(" ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001979 formatTimeMs(sb, time/1000);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001980 sb.append("(");
1981 sb.append(formatRatioLocked(time, whichBatteryRealtime));
Dianne Hackborn617f8772009-03-31 15:04:46 -07001982 sb.append(") ");
1983 sb.append(getPhoneDataConnectionCount(i, which));
1984 sb.append("x");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001985 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001986 if (!didOne) sb.append(" (no activity)");
Dianne Hackborn627bba72009-03-24 22:32:56 -07001987 pw.println(sb.toString());
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001988
1989 sb.setLength(0);
1990 sb.append(prefix);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001991 sb.append(" Mobile radio active time: ");
1992 formatTimeMs(sb, getMobileRadioActiveTime(batteryRealtime, which) / 1000);
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001993 pw.println(sb.toString());
1994
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001995 sb.setLength(0);
1996 sb.append(prefix);
1997 sb.append(" Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1998 sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1999 sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
2000 sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002001 sb.append(")");
2002 pw.println(sb.toString());
2003
2004 sb.setLength(0);
2005 sb.append(prefix);
2006 sb.append(" Wifi states:");
2007 didOne = false;
2008 for (int i=0; i<NUM_WIFI_STATES; i++) {
2009 final long time = getWifiStateTime(i, batteryRealtime, which);
2010 if (time == 0) {
2011 continue;
2012 }
2013 sb.append("\n ");
2014 didOne = true;
2015 sb.append(WIFI_STATE_NAMES[i]);
2016 sb.append(" ");
2017 formatTimeMs(sb, time/1000);
2018 sb.append("(");
2019 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2020 sb.append(") ");
2021 sb.append(getPhoneDataConnectionCount(i, which));
2022 sb.append("x");
2023 }
2024 if (!didOne) sb.append(" (no activity)");
2025 pw.println(sb.toString());
2026
2027 sb.setLength(0);
2028 sb.append(prefix);
2029 sb.append(" Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002030 sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
2031 sb.append(")");
2032 pw.println(sb.toString());
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002033
2034 sb.setLength(0);
2035 sb.append(prefix);
2036 sb.append(" Bluetooth states:");
2037 didOne = false;
2038 for (int i=0; i<NUM_BLUETOOTH_STATES; i++) {
2039 final long time = getBluetoothStateTime(i, batteryRealtime, which);
2040 if (time == 0) {
2041 continue;
2042 }
2043 sb.append("\n ");
2044 didOne = true;
2045 sb.append(BLUETOOTH_STATE_NAMES[i]);
2046 sb.append(" ");
2047 formatTimeMs(sb, time/1000);
2048 sb.append("(");
2049 sb.append(formatRatioLocked(time, whichBatteryRealtime));
2050 sb.append(") ");
2051 sb.append(getPhoneDataConnectionCount(i, which));
2052 sb.append("x");
2053 }
2054 if (!didOne) sb.append(" (no activity)");
2055 pw.println(sb.toString());
2056
2057 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002058
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002059 if (which == STATS_SINCE_UNPLUGGED) {
The Android Open Source Project10592532009-03-18 17:39:46 -07002060 if (getIsOnBattery()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002061 pw.print(prefix); pw.println(" Device is currently unplugged");
2062 pw.print(prefix); pw.print(" Discharge cycle start level: ");
2063 pw.println(getDischargeStartLevel());
2064 pw.print(prefix); pw.print(" Discharge cycle current level: ");
2065 pw.println(getDischargeCurrentLevel());
Dianne Hackborn99d04522010-08-20 13:43:00 -07002066 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002067 pw.print(prefix); pw.println(" Device is currently plugged into power");
2068 pw.print(prefix); pw.print(" Last discharge cycle start level: ");
2069 pw.println(getDischargeStartLevel());
2070 pw.print(prefix); pw.print(" Last discharge cycle end level: ");
2071 pw.println(getDischargeCurrentLevel());
The Android Open Source Project10592532009-03-18 17:39:46 -07002072 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002073 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2074 pw.println(getDischargeAmountScreenOn());
2075 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2076 pw.println(getDischargeAmountScreenOff());
Dianne Hackborn617f8772009-03-31 15:04:46 -07002077 pw.println(" ");
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002078 } else {
2079 pw.print(prefix); pw.println(" Device battery use since last full charge");
2080 pw.print(prefix); pw.print(" Amount discharged (lower bound): ");
2081 pw.println(getLowDischargeAmountSinceCharge());
2082 pw.print(prefix); pw.print(" Amount discharged (upper bound): ");
2083 pw.println(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002084 pw.print(prefix); pw.print(" Amount discharged while screen on: ");
2085 pw.println(getDischargeAmountScreenOnSinceCharge());
2086 pw.print(prefix); pw.print(" Amount discharged while screen off: ");
2087 pw.println(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn81038902012-11-26 17:04:09 -08002088 pw.println();
The Android Open Source Project10592532009-03-18 17:39:46 -07002089 }
Dianne Hackborn81038902012-11-26 17:04:09 -08002090
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002091 BatteryStatsHelper helper = new BatteryStatsHelper(context);
2092 helper.create(this);
2093 helper.refreshStats(which, UserHandle.USER_ALL);
2094 List<BatterySipper> sippers = helper.getUsageList();
2095 if (sippers != null && sippers.size() > 0) {
2096 pw.print(prefix); pw.println(" Estimated power use (mAh):");
2097 pw.print(prefix); pw.print(" Capacity: ");
2098 printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
Dianne Hackborn099bc622014-01-22 13:39:16 -08002099 pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002100 pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
2101 pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
2102 pw.println();
2103 for (int i=0; i<sippers.size(); i++) {
2104 BatterySipper bs = sippers.get(i);
2105 switch (bs.drainType) {
2106 case IDLE:
2107 pw.print(prefix); pw.print(" Idle: "); printmAh(pw, bs.value);
2108 pw.println();
2109 break;
2110 case CELL:
2111 pw.print(prefix); pw.print(" Cell standby: "); printmAh(pw, bs.value);
2112 pw.println();
2113 break;
2114 case PHONE:
2115 pw.print(prefix); pw.print(" Phone calls: "); printmAh(pw, bs.value);
2116 pw.println();
2117 break;
2118 case WIFI:
2119 pw.print(prefix); pw.print(" Wifi: "); printmAh(pw, bs.value);
2120 pw.println();
2121 break;
2122 case BLUETOOTH:
2123 pw.print(prefix); pw.print(" Bluetooth: "); printmAh(pw, bs.value);
2124 pw.println();
2125 break;
2126 case SCREEN:
2127 pw.print(prefix); pw.print(" Screen: "); printmAh(pw, bs.value);
2128 pw.println();
2129 break;
2130 case APP:
2131 pw.print(prefix); pw.print(" Uid "); pw.print(bs.uidObj.getUid());
2132 pw.print(": "); printmAh(pw, bs.value); pw.println();
2133 break;
2134 case USER:
2135 pw.print(prefix); pw.print(" User "); pw.print(bs.userId);
2136 pw.print(": "); printmAh(pw, bs.value); pw.println();
2137 break;
2138 case UNACCOUNTED:
2139 pw.print(prefix); pw.print(" Unaccounted: "); printmAh(pw, bs.value);
2140 pw.println();
2141 break;
2142 case OVERCOUNTED:
2143 pw.print(prefix); pw.print(" Over-counted: "); printmAh(pw, bs.value);
2144 pw.println();
2145 break;
2146 }
2147 }
Dianne Hackbornc46809e2014-01-15 16:20:44 -08002148 pw.println();
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002149 }
2150
Dianne Hackborn81038902012-11-26 17:04:09 -08002151 if (timers.size() > 0) {
2152 Collections.sort(timers, timerComparator);
2153 pw.print(prefix); pw.println(" All partial wake locks:");
2154 for (int i=0; i<timers.size(); i++) {
2155 TimerEntry timer = timers.get(i);
2156 sb.setLength(0);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002157 sb.append(" Wake lock ");
2158 UserHandle.formatUid(sb, timer.mId);
Dianne Hackborn81038902012-11-26 17:04:09 -08002159 sb.append(" ");
2160 sb.append(timer.mName);
2161 printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
2162 sb.append(" realtime");
2163 pw.println(sb.toString());
2164 }
2165 timers.clear();
2166 pw.println();
2167 }
Evan Millar22ac0432009-03-31 11:33:18 -07002168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 for (int iu=0; iu<NU; iu++) {
2170 final int uid = uidStats.keyAt(iu);
Dianne Hackborne4a59512010-12-07 11:08:07 -08002171 if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002172 continue;
2173 }
2174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 Uid u = uidStats.valueAt(iu);
Dianne Hackborna4cc2052013-07-08 17:31:25 -07002176
2177 pw.print(prefix);
2178 pw.print(" ");
2179 UserHandle.formatUid(pw, uid);
2180 pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 boolean uidActivity = false;
2182
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002183 long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
2184 long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
2185 long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
2186 long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
2187 long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
2188 long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
2189 long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
2190 long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07002191 long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
Nick Pelly6ccaa542012-06-15 15:22:47 -07002192 long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07002193 long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002194
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002195 if (mobileRxBytes > 0 || mobileTxBytes > 0
2196 || mobileRxPackets > 0 || mobileTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002197 pw.print(prefix); pw.print(" Mobile network: ");
2198 pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002199 pw.print(formatBytesLocked(mobileTxBytes));
2200 pw.print(" sent (packets "); pw.print(mobileRxPackets);
2201 pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002203 if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002204 pw.print(prefix); pw.print(" Wi-Fi network: ");
2205 pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002206 pw.print(formatBytesLocked(wifiTxBytes));
2207 pw.print(" sent (packets "); pw.print(wifiRxPackets);
2208 pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07002209 }
2210
Dianne Hackborn617f8772009-03-31 15:04:46 -07002211 if (u.hasUserActivity()) {
2212 boolean hasData = false;
Raph Levien4c7a4a72012-08-03 14:32:39 -07002213 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002214 int val = u.getUserActivityCount(i, which);
2215 if (val != 0) {
2216 if (!hasData) {
2217 sb.setLength(0);
2218 sb.append(" User activity: ");
2219 hasData = true;
2220 } else {
2221 sb.append(", ");
2222 }
2223 sb.append(val);
2224 sb.append(" ");
2225 sb.append(Uid.USER_ACTIVITY_TYPES[i]);
2226 }
2227 }
2228 if (hasData) {
2229 pw.println(sb.toString());
2230 }
2231 }
2232
Nick Pelly6ccaa542012-06-15 15:22:47 -07002233 if (fullWifiLockOnTime != 0 || wifiScanTime != 0
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07002234 || uidWifiRunningTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002235 sb.setLength(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07002236 sb.append(prefix); sb.append(" Wifi Running: ");
2237 formatTimeMs(sb, uidWifiRunningTime / 1000);
2238 sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002239 whichBatteryRealtime)); sb.append(")\n");
2240 sb.append(prefix); sb.append(" Full Wifi Lock: ");
Nick Pelly6ccaa542012-06-15 15:22:47 -07002241 formatTimeMs(sb, fullWifiLockOnTime / 1000);
2242 sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002243 whichBatteryRealtime)); sb.append(")\n");
Nick Pelly6ccaa542012-06-15 15:22:47 -07002244 sb.append(prefix); sb.append(" Wifi Scan: ");
2245 formatTimeMs(sb, wifiScanTime / 1000);
2246 sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002247 whichBatteryRealtime)); sb.append(")");
2248 pw.println(sb.toString());
The Android Open Source Project10592532009-03-18 17:39:46 -07002249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250
2251 Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2252 if (wakelocks.size() > 0) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002253 long totalFull = 0, totalPartial = 0, totalWindow = 0;
2254 int count = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002255 for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2256 : wakelocks.entrySet()) {
2257 Uid.Wakelock wl = ent.getValue();
2258 String linePrefix = ": ";
2259 sb.setLength(0);
2260 sb.append(prefix);
2261 sb.append(" Wake lock ");
2262 sb.append(ent.getKey());
2263 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
2264 "full", which, linePrefix);
2265 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
2266 "partial", which, linePrefix);
2267 linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
2268 "window", which, linePrefix);
2269 if (!linePrefix.equals(": ")) {
2270 sb.append(" realtime");
Jason Parks94b916d2010-07-20 12:39:07 -05002271 // Only print out wake locks that were held
2272 pw.println(sb.toString());
2273 uidActivity = true;
Dianne Hackbornc24ab862011-10-18 15:55:03 -07002274 count++;
2275 }
2276 totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
2277 batteryRealtime, which);
2278 totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
2279 batteryRealtime, which);
2280 totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
2281 batteryRealtime, which);
2282 }
2283 if (count > 1) {
2284 if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2285 sb.setLength(0);
2286 sb.append(prefix);
2287 sb.append(" TOTAL wake: ");
2288 boolean needComma = false;
2289 if (totalFull != 0) {
2290 needComma = true;
2291 formatTimeMs(sb, totalFull);
2292 sb.append("full");
2293 }
2294 if (totalPartial != 0) {
2295 if (needComma) {
2296 sb.append(", ");
2297 }
2298 needComma = true;
2299 formatTimeMs(sb, totalPartial);
2300 sb.append("partial");
2301 }
2302 if (totalWindow != 0) {
2303 if (needComma) {
2304 sb.append(", ");
2305 }
2306 needComma = true;
2307 formatTimeMs(sb, totalWindow);
2308 sb.append("window");
2309 }
2310 sb.append(" realtime");
2311 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 }
2314 }
2315
2316 Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2317 if (sensors.size() > 0) {
2318 for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2319 : sensors.entrySet()) {
2320 Uid.Sensor se = ent.getValue();
2321 int sensorNumber = ent.getKey();
2322 sb.setLength(0);
2323 sb.append(prefix);
2324 sb.append(" Sensor ");
2325 int handle = se.getHandle();
2326 if (handle == Uid.Sensor.GPS) {
2327 sb.append("GPS");
2328 } else {
2329 sb.append(handle);
2330 }
2331 sb.append(": ");
2332
2333 Timer timer = se.getSensorTime();
2334 if (timer != null) {
2335 // Convert from microseconds to milliseconds with rounding
Evan Millarc64edde2009-04-18 12:26:32 -07002336 long totalTime = (timer.getTotalTimeLocked(
2337 batteryRealtime, which) + 500) / 1000;
2338 int count = timer.getCountLocked(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 //timer.logState();
2340 if (totalTime != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002341 formatTimeMs(sb, totalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 sb.append("realtime (");
2343 sb.append(count);
2344 sb.append(" times)");
2345 } else {
2346 sb.append("(not used)");
2347 }
2348 } else {
2349 sb.append("(not used)");
2350 }
2351
2352 pw.println(sb.toString());
2353 uidActivity = true;
2354 }
2355 }
2356
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002357 Timer vibTimer = u.getVibratorOnTimer();
2358 if (vibTimer != null) {
2359 // Convert from microseconds to milliseconds with rounding
2360 long totalTime = (vibTimer.getTotalTimeLocked(
2361 batteryRealtime, which) + 500) / 1000;
2362 int count = vibTimer.getCountLocked(which);
2363 //timer.logState();
2364 if (totalTime != 0) {
2365 sb.setLength(0);
2366 sb.append(prefix);
2367 sb.append(" Vibrator: ");
2368 formatTimeMs(sb, totalTime);
2369 sb.append("realtime (");
2370 sb.append(count);
2371 sb.append(" times)");
2372 pw.println(sb.toString());
2373 uidActivity = true;
2374 }
2375 }
2376
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002377 Timer fgTimer = u.getForegroundActivityTimer();
2378 if (fgTimer != null) {
2379 // Convert from microseconds to milliseconds with rounding
2380 long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2381 int count = fgTimer.getCountLocked(which);
2382 if (totalTime != 0) {
2383 sb.setLength(0);
2384 sb.append(prefix);
2385 sb.append(" Foreground activities: ");
2386 formatTimeMs(sb, totalTime);
2387 sb.append("realtime (");
2388 sb.append(count);
2389 sb.append(" times)");
2390 pw.println(sb.toString());
2391 uidActivity = true;
2392 }
2393 }
2394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2396 if (processStats.size() > 0) {
2397 for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2398 : processStats.entrySet()) {
2399 Uid.Proc ps = ent.getValue();
2400 long userTime;
2401 long systemTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002402 long foregroundTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 int starts;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002404 int numExcessive;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405
2406 userTime = ps.getUserTime(which);
2407 systemTime = ps.getSystemTime(which);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002408 foregroundTime = ps.getForegroundTime(which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 starts = ps.getStarts(which);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002410 numExcessive = which == STATS_SINCE_CHARGED
Dianne Hackborn287952c2010-09-22 22:34:31 -07002411 ? ps.countExcessivePowers() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002413 if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002414 || numExcessive != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002415 sb.setLength(0);
2416 sb.append(prefix); sb.append(" Proc ");
2417 sb.append(ent.getKey()); sb.append(":\n");
2418 sb.append(prefix); sb.append(" CPU: ");
2419 formatTime(sb, userTime); sb.append("usr + ");
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002420 formatTime(sb, systemTime); sb.append("krn ; ");
2421 formatTime(sb, foregroundTime); sb.append("fg");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002422 if (starts != 0) {
Dianne Hackbornb8071d792010-09-09 16:45:15 -07002423 sb.append("\n"); sb.append(prefix); sb.append(" ");
2424 sb.append(starts); sb.append(" proc starts");
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002425 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002426 pw.println(sb.toString());
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002427 for (int e=0; e<numExcessive; e++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002428 Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002429 if (ew != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002430 pw.print(prefix); pw.print(" * Killed for ");
2431 if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2432 pw.print("wake lock");
2433 } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2434 pw.print("cpu");
2435 } else {
2436 pw.print("unknown");
2437 }
2438 pw.print(" use: ");
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002439 TimeUtils.formatDuration(ew.usedTime, pw);
2440 pw.print(" over ");
2441 TimeUtils.formatDuration(ew.overTime, pw);
Robert Greenwalta029ea12013-09-25 16:38:12 -07002442 if (ew.overTime != 0) {
2443 pw.print(" (");
2444 pw.print((ew.usedTime*100)/ew.overTime);
2445 pw.println("%)");
2446 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002447 }
2448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 uidActivity = true;
2450 }
2451 }
2452 }
2453
2454 Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2455 if (packageStats.size() > 0) {
2456 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2457 : packageStats.entrySet()) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002458 pw.print(prefix); pw.print(" Apk "); pw.print(ent.getKey()); pw.println(":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 boolean apkActivity = false;
2460 Uid.Pkg ps = ent.getValue();
2461 int wakeups = ps.getWakeups(which);
2462 if (wakeups != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002463 pw.print(prefix); pw.print(" ");
2464 pw.print(wakeups); pw.println(" wakeup alarms");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 apkActivity = true;
2466 }
2467 Map<String, ? extends Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2468 if (serviceStats.size() > 0) {
2469 for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2470 : serviceStats.entrySet()) {
2471 BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2472 long startTime = ss.getStartTime(batteryUptime, which);
2473 int starts = ss.getStarts(which);
2474 int launches = ss.getLaunches(which);
2475 if (startTime != 0 || starts != 0 || launches != 0) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002476 sb.setLength(0);
2477 sb.append(prefix); sb.append(" Service ");
2478 sb.append(sent.getKey()); sb.append(":\n");
2479 sb.append(prefix); sb.append(" Created for: ");
2480 formatTimeMs(sb, startTime / 1000);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002481 sb.append("uptime\n");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002482 sb.append(prefix); sb.append(" Starts: ");
2483 sb.append(starts);
2484 sb.append(", launches: "); sb.append(launches);
2485 pw.println(sb.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 apkActivity = true;
2487 }
2488 }
2489 }
2490 if (!apkActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002491 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 }
2493 uidActivity = true;
2494 }
2495 }
2496 if (!uidActivity) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002497 pw.print(prefix); pw.println(" (nothing executed)");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 }
2499 }
2500 }
2501
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002502 static void printBitDescriptions(PrintWriter pw, int oldval, int newval, HistoryTag wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002503 BitDescription[] descriptions, boolean longNames) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002504 int diff = oldval ^ newval;
2505 if (diff == 0) return;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002506 boolean didWake = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002507 for (int i=0; i<descriptions.length; i++) {
2508 BitDescription bd = descriptions[i];
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002509 if ((diff&bd.mask) != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002510 pw.print(longNames ? " " : ",");
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002511 if (bd.shift < 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002512 pw.print((newval&bd.mask) != 0 ? "+" : "-");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002513 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002514 if (bd.mask == HistoryItem.STATE_WAKE_LOCK_FLAG && wakelockTag != null) {
2515 didWake = true;
2516 pw.print("=");
2517 if (longNames) {
2518 UserHandle.formatUid(pw, wakelockTag.uid);
2519 pw.print(":\"");
2520 pw.print(wakelockTag.string);
2521 pw.print("\"");
2522 } else {
2523 pw.print(wakelockTag.poolIdx);
2524 }
2525 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002526 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002527 pw.print(longNames ? bd.name : bd.shortName);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002528 pw.print("=");
2529 int val = (newval&bd.mask)>>bd.shift;
2530 if (bd.values != null && val >= 0 && val < bd.values.length) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002531 pw.print(longNames? bd.values[val] : bd.shortValues[val]);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002532 } else {
2533 pw.print(val);
2534 }
2535 }
2536 }
2537 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002538 if (!didWake && wakelockTag != null) {
2539 pw.print(longNames ? "wake_lock=" : "w=");
2540 if (longNames) {
2541 UserHandle.formatUid(pw, wakelockTag.uid);
2542 pw.print(":\"");
2543 pw.print(wakelockTag.string);
2544 pw.print("\"");
2545 } else {
2546 pw.print(wakelockTag.poolIdx);
2547 }
2548 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002549 }
2550
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002551 public void prepareForDumpLocked() {
2552 }
2553
2554 public static class HistoryPrinter {
2555 int oldState = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002556 int oldLevel = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002557 int oldStatus = -1;
2558 int oldHealth = -1;
2559 int oldPlug = -1;
2560 int oldTemp = -1;
2561 int oldVolt = -1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002562 long lastTime = -1;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002563
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002564 public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) {
2565 if (!checkin) {
2566 pw.print(" ");
2567 TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002568 pw.print(" (");
2569 pw.print(rec.numReadInts);
2570 pw.print(") ");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002571 } else {
2572 if (lastTime < 0) {
2573 pw.print("@");
2574 pw.print(rec.time-now);
2575 } else {
2576 pw.print(rec.time-lastTime);
2577 }
2578 lastTime = rec.time;
2579 }
2580 if (rec.cmd == HistoryItem.CMD_START) {
2581 if (checkin) {
2582 pw.print(":");
2583 }
2584 pw.println("START");
2585 } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2586 if (checkin) {
2587 pw.print(":");
2588 }
2589 pw.println("*OVERFLOW*");
2590 } else {
2591 if (!checkin) {
2592 if (rec.batteryLevel < 10) pw.print("00");
2593 else if (rec.batteryLevel < 100) pw.print("0");
2594 pw.print(rec.batteryLevel);
2595 pw.print(" ");
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002596 if (rec.states < 0) ;
2597 else if (rec.states < 0x10) pw.print("0000000");
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002598 else if (rec.states < 0x100) pw.print("000000");
2599 else if (rec.states < 0x1000) pw.print("00000");
2600 else if (rec.states < 0x10000) pw.print("0000");
2601 else if (rec.states < 0x100000) pw.print("000");
2602 else if (rec.states < 0x1000000) pw.print("00");
2603 else if (rec.states < 0x10000000) pw.print("0");
2604 pw.print(Integer.toHexString(rec.states));
2605 } else {
2606 if (oldLevel != rec.batteryLevel) {
2607 oldLevel = rec.batteryLevel;
2608 pw.print(",Bl="); pw.print(rec.batteryLevel);
2609 }
2610 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002611 if (oldStatus != rec.batteryStatus) {
2612 oldStatus = rec.batteryStatus;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002613 pw.print(checkin ? ",Bs=" : " status=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002614 switch (oldStatus) {
2615 case BatteryManager.BATTERY_STATUS_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002616 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002617 break;
2618 case BatteryManager.BATTERY_STATUS_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002619 pw.print(checkin ? "c" : "charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002620 break;
2621 case BatteryManager.BATTERY_STATUS_DISCHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002622 pw.print(checkin ? "d" : "discharging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002623 break;
2624 case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002625 pw.print(checkin ? "n" : "not-charging");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002626 break;
2627 case BatteryManager.BATTERY_STATUS_FULL:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002628 pw.print(checkin ? "f" : "full");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002629 break;
2630 default:
2631 pw.print(oldStatus);
2632 break;
2633 }
2634 }
2635 if (oldHealth != rec.batteryHealth) {
2636 oldHealth = rec.batteryHealth;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002637 pw.print(checkin ? ",Bh=" : " health=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002638 switch (oldHealth) {
2639 case BatteryManager.BATTERY_HEALTH_UNKNOWN:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002640 pw.print(checkin ? "?" : "unknown");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002641 break;
2642 case BatteryManager.BATTERY_HEALTH_GOOD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002643 pw.print(checkin ? "g" : "good");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002644 break;
2645 case BatteryManager.BATTERY_HEALTH_OVERHEAT:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002646 pw.print(checkin ? "h" : "overheat");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002647 break;
2648 case BatteryManager.BATTERY_HEALTH_DEAD:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002649 pw.print(checkin ? "d" : "dead");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002650 break;
2651 case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002652 pw.print(checkin ? "v" : "over-voltage");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002653 break;
2654 case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002655 pw.print(checkin ? "f" : "failure");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002656 break;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002657 case BatteryManager.BATTERY_HEALTH_COLD:
2658 pw.print(checkin ? "c" : "cold");
2659 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002660 default:
2661 pw.print(oldHealth);
2662 break;
2663 }
2664 }
2665 if (oldPlug != rec.batteryPlugType) {
2666 oldPlug = rec.batteryPlugType;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002667 pw.print(checkin ? ",Bp=" : " plug=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002668 switch (oldPlug) {
2669 case 0:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002670 pw.print(checkin ? "n" : "none");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002671 break;
2672 case BatteryManager.BATTERY_PLUGGED_AC:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002673 pw.print(checkin ? "a" : "ac");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002674 break;
2675 case BatteryManager.BATTERY_PLUGGED_USB:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002676 pw.print(checkin ? "u" : "usb");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002677 break;
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002678 case BatteryManager.BATTERY_PLUGGED_WIRELESS:
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002679 pw.print(checkin ? "w" : "wireless");
Brian Muramatsu37a37f42012-08-14 15:21:02 -07002680 break;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002681 default:
2682 pw.print(oldPlug);
2683 break;
2684 }
2685 }
2686 if (oldTemp != rec.batteryTemperature) {
2687 oldTemp = rec.batteryTemperature;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002688 pw.print(checkin ? ",Bt=" : " temp=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002689 pw.print(oldTemp);
2690 }
2691 if (oldVolt != rec.batteryVoltage) {
2692 oldVolt = rec.batteryVoltage;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002693 pw.print(checkin ? ",Bv=" : " volt=");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002694 pw.print(oldVolt);
2695 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002696 printBitDescriptions(pw, oldState, rec.states, rec.wakelockTag,
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002697 HISTORY_STATE_DESCRIPTIONS, !checkin);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002698 if (rec.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002699 pw.print(checkin ? "," : " ");
2700 if ((rec.eventCode&HistoryItem.EVENT_FLAG_START) != 0) {
2701 pw.print("+");
2702 } else if ((rec.eventCode&HistoryItem.EVENT_FLAG_FINISH) != 0) {
2703 pw.print("-");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002704 }
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002705 String[] eventNames = checkin ? HISTORY_EVENT_CHECKIN_NAMES
2706 : HISTORY_EVENT_NAMES;
2707 int idx = rec.eventCode & ~(HistoryItem.EVENT_FLAG_START
2708 | HistoryItem.EVENT_FLAG_FINISH);
2709 if (idx >= 0 && idx < eventNames.length) {
2710 pw.print(eventNames[idx]);
2711 } else {
2712 pw.print(checkin ? "Ev" : "event");
2713 pw.print(idx);
2714 }
2715 pw.print("=");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002716 if (checkin) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002717 pw.print(rec.eventTag.poolIdx);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002718 } else {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002719 UserHandle.formatUid(pw, rec.eventTag.uid);
2720 pw.print(":\"");
2721 pw.print(rec.eventTag.string);
2722 pw.print("\"");
Dianne Hackborn099bc622014-01-22 13:39:16 -08002723 }
2724 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002725 pw.println();
2726 }
2727 oldState = rec.states;
2728 }
2729 }
2730
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002731 private void printSizeValue(PrintWriter pw, long size) {
2732 float result = size;
2733 String suffix = "";
2734 if (result >= 10*1024) {
2735 suffix = "KB";
2736 result = result / 1024;
2737 }
2738 if (result >= 10*1024) {
2739 suffix = "MB";
2740 result = result / 1024;
2741 }
2742 if (result >= 10*1024) {
2743 suffix = "GB";
2744 result = result / 1024;
2745 }
2746 if (result >= 10*1024) {
2747 suffix = "TB";
2748 result = result / 1024;
2749 }
2750 if (result >= 10*1024) {
2751 suffix = "PB";
2752 result = result / 1024;
2753 }
2754 pw.print((int)result);
2755 pw.print(suffix);
2756 }
2757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 /**
2759 * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2760 *
2761 * @param pw a Printer to receive the dump output.
2762 */
2763 @SuppressWarnings("unused")
Dianne Hackborn099bc622014-01-22 13:39:16 -08002764 public void dumpLocked(Context context, PrintWriter pw, boolean isUnpluggedOnly, int reqUid,
2765 boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002766 prepareForDumpLocked();
2767
2768 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2769
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002770 final HistoryItem rec = new HistoryItem();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002771 final long historyTotalSize = getHistoryTotalSize();
2772 final long historyUsedSize = getHistoryUsedSize();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07002773 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002774 try {
2775 pw.print("Battery History (");
2776 pw.print((100*historyUsedSize)/historyTotalSize);
2777 pw.print("% used, ");
2778 printSizeValue(pw, historyUsedSize);
2779 pw.print(" used of ");
2780 printSizeValue(pw, historyTotalSize);
2781 pw.print(", ");
2782 pw.print(getHistoryStringPoolSize());
2783 pw.print(" strings using ");
2784 printSizeValue(pw, getHistoryStringPoolBytes());
2785 pw.println("):");
2786 HistoryPrinter hprinter = new HistoryPrinter();
2787 while (getNextHistoryLocked(rec)) {
2788 hprinter.printNextItem(pw, rec, now, false);
2789 }
2790 pw.println();
2791 } finally {
2792 finishIteratingHistoryLocked();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002793 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002794 }
2795
2796 if (startIteratingOldHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002797 try {
2798 pw.println("Old battery History:");
2799 HistoryPrinter hprinter = new HistoryPrinter();
2800 while (getNextOldHistoryLocked(rec)) {
2801 hprinter.printNextItem(pw, rec, now, false);
2802 }
2803 pw.println();
2804 } finally {
2805 finishIteratingOldHistoryLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002806 }
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002807 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002808
2809 if (historyOnly) {
2810 return;
2811 }
2812
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002813 SparseArray<? extends Uid> uidStats = getUidStats();
2814 final int NU = uidStats.size();
2815 boolean didPid = false;
2816 long nowRealtime = SystemClock.elapsedRealtime();
Dianne Hackbornb5e31652010-09-07 12:13:55 -07002817 for (int i=0; i<NU; i++) {
2818 Uid uid = uidStats.valueAt(i);
2819 SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2820 if (pids != null) {
2821 for (int j=0; j<pids.size(); j++) {
2822 Uid.Pid pid = pids.valueAt(j);
2823 if (!didPid) {
2824 pw.println("Per-PID Stats:");
2825 didPid = true;
2826 }
2827 long time = pid.mWakeSum + (pid.mWakeStart != 0
2828 ? (nowRealtime - pid.mWakeStart) : 0);
2829 pw.print(" PID "); pw.print(pids.keyAt(j));
2830 pw.print(" wake time: ");
2831 TimeUtils.formatDuration(time, pw);
2832 pw.println("");
2833 }
2834 }
2835 }
2836 if (didPid) {
2837 pw.println("");
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002838 }
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002839
2840 if (!isUnpluggedOnly) {
2841 pw.println("Statistics since last charge:");
2842 pw.println(" System starts: " + getStartCount()
2843 + ", currently on battery: " + getIsOnBattery());
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002844 dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
Jeff Sharkeyec43a6b2013-04-30 13:33:18 -07002845 pw.println("");
2846 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002847 pw.println("Statistics since last unplugged:");
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002848 dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002849 }
2850
2851 @SuppressWarnings("unused")
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002852 public void dumpCheckinLocked(Context context,
Dianne Hackborn49021f52013-09-04 18:03:40 -07002853 PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly,
Dianne Hackborn099bc622014-01-22 13:39:16 -08002854 boolean includeHistory, boolean historyOnly) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002855 prepareForDumpLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002857 long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2858
Dianne Hackborn099bc622014-01-22 13:39:16 -08002859 if (includeHistory || historyOnly) {
Dianne Hackborn49021f52013-09-04 18:03:40 -07002860 final HistoryItem rec = new HistoryItem();
2861 if (startIteratingHistoryLocked()) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002862 try {
2863 for (int i=0; i<getHistoryStringPoolSize(); i++) {
2864 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2865 pw.print(HISTORY_STRING_POOL); pw.print(',');
2866 pw.print(i);
2867 pw.print(',');
2868 pw.print(getHistoryTagPoolString(i));
2869 pw.print(',');
2870 pw.print(getHistoryTagPoolUid(i));
2871 pw.println();
2872 }
2873 HistoryPrinter hprinter = new HistoryPrinter();
2874 while (getNextHistoryLocked(rec)) {
2875 pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2876 pw.print(HISTORY_DATA); pw.print(',');
2877 hprinter.printNextItem(pw, rec, now, true);
2878 }
2879 } finally {
2880 finishIteratingHistoryLocked();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002881 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002882 }
Dianne Hackborn13ac0412013-06-25 19:34:49 -07002883 }
2884
Dianne Hackborn099bc622014-01-22 13:39:16 -08002885 if (historyOnly) {
2886 return;
2887 }
2888
Dianne Hackborne4a59512010-12-07 11:08:07 -08002889 if (apps != null) {
2890 SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2891 for (int i=0; i<apps.size(); i++) {
2892 ApplicationInfo ai = apps.get(i);
2893 ArrayList<String> pkgs = uids.get(ai.uid);
2894 if (pkgs == null) {
2895 pkgs = new ArrayList<String>();
2896 uids.put(ai.uid, pkgs);
2897 }
2898 pkgs.add(ai.packageName);
2899 }
2900 SparseArray<? extends Uid> uidStats = getUidStats();
2901 final int NU = uidStats.size();
2902 String[] lineArgs = new String[2];
2903 for (int i=0; i<NU; i++) {
2904 int uid = uidStats.keyAt(i);
2905 ArrayList<String> pkgs = uids.get(uid);
2906 if (pkgs != null) {
2907 for (int j=0; j<pkgs.size(); j++) {
2908 lineArgs[0] = Integer.toString(uid);
2909 lineArgs[1] = pkgs.get(j);
2910 dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2911 (Object[])lineArgs);
2912 }
2913 }
2914 }
2915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 if (isUnpluggedOnly) {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002917 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 }
2919 else {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08002920 dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
2921 dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
2923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924}