BatteryStats.java revision 5f4a5f9536eb171bf21a9293198d90c9891e7205
1/*
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
17package android.os;
18
19import java.io.PrintWriter;
20import java.util.ArrayList;
21import java.util.Collections;
22import java.util.Comparator;
23import java.util.Formatter;
24import java.util.List;
25import java.util.Map;
26
27import android.content.Context;
28import android.content.pm.ApplicationInfo;
29import android.telephony.SignalStrength;
30import android.text.format.DateFormat;
31import android.util.Printer;
32import android.util.SparseArray;
33import android.util.TimeUtils;
34import com.android.internal.os.BatterySipper;
35import com.android.internal.os.BatteryStatsHelper;
36
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;
46
47    /** @hide */
48    public static final String SERVICE_NAME = "batterystats";
49
50    /**
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.
67     */
68    public static final int SENSOR = 3;
69
70    /**
71     * A constant indicating a a wifi running timer
72     */
73    public static final int WIFI_RUNNING = 4;
74
75    /**
76     * A constant indicating a full wifi lock timer
77     */
78    public static final int FULL_WIFI_LOCK = 5;
79
80    /**
81     * A constant indicating a wifi scan
82     */
83    public static final int WIFI_SCAN = 6;
84
85     /**
86      * A constant indicating a wifi multicast timer
87      */
88     public static final int WIFI_MULTICAST_ENABLED = 7;
89
90    /**
91     * A constant indicating an audio turn on timer
92     */
93    public static final int AUDIO_TURNED_ON = 7;
94
95    /**
96     * A constant indicating a video turn on timer
97     */
98    public static final int VIDEO_TURNED_ON = 8;
99
100    /**
101     * A constant indicating a vibrator on timer
102     */
103    public static final int VIBRATOR_ON = 9;
104
105    /**
106     * A constant indicating a foreground activity timer
107     */
108    public static final int FOREGROUND_ACTIVITY = 10;
109
110    /**
111     * A constant indicating a wifi batched scan is active
112     */
113    public static final int WIFI_BATCHED_SCAN = 11;
114
115    /**
116     * Include all of the data in the stats, including previously saved data.
117     */
118    public static final int STATS_SINCE_CHARGED = 0;
119
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     */
133    public static final int STATS_SINCE_UNPLUGGED = 3;
134
135    // NOTE: Update this list if you add/change any stats above.
136    // These characters are supposed to represent "total", "last", "current",
137    // and "unplugged". They were shortened for efficiency sake.
138    private static final String[] STAT_NAMES = { "t", "l", "c", "u" };
139
140    /**
141     * Bump the version on this if the checkin format changes.
142     */
143    private static final int BATTERY_STATS_CHECKIN_VERSION = 7;
144
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
148
149
150    private static final String UID_DATA = "uid";
151    private static final String APK_DATA = "apk";
152    private static final String PROCESS_DATA = "pr";
153    private static final String SENSOR_DATA = "sr";
154    private static final String VIBRATOR_DATA = "vib";
155    private static final String FOREGROUND_DATA = "fg";
156    private static final String WAKELOCK_DATA = "wl";
157    private static final String KERNEL_WAKELOCK_DATA = "kwl";
158    private static final String NETWORK_DATA = "nt";
159    private static final String USER_ACTIVITY_DATA = "ua";
160    private static final String BATTERY_DATA = "bt";
161    private static final String BATTERY_DISCHARGE_DATA = "dc";
162    private static final String BATTERY_LEVEL_DATA = "lv";
163    private static final String WIFI_DATA = "wfl";
164    private static final String MISC_DATA = "m";
165    private static final String GLOBAL_NETWORK_DATA = "gn";
166    private static final String HISTORY_STRING_POOL = "hsp";
167    private static final String HISTORY_DATA = "h";
168    private static final String SCREEN_BRIGHTNESS_DATA = "br";
169    private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
170    private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
171    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";
174    private static final String POWER_USE_SUMMARY_DATA = "pws";
175    private static final String POWER_USE_ITEM_DATA = "pwi";
176
177    private final StringBuilder mFormatBuilder = new StringBuilder(32);
178    private final Formatter mFormatter = new Formatter(mFormatBuilder);
179
180    /**
181     * State for keeping track of counting information.
182     */
183    public static abstract class Counter {
184
185        /**
186         * Returns the count associated with this Counter for the
187         * selected type of statistics.
188         *
189         * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
190         */
191        public abstract int getCountLocked(int which);
192
193        /**
194         * Temporary for debugging.
195         */
196        public abstract void logState(Printer pw, String prefix);
197    }
198
199    /**
200     * State for keeping track of timing information.
201     */
202    public static abstract class Timer {
203
204        /**
205         * Returns the count associated with this Timer for the
206         * selected type of statistics.
207         *
208         * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
209         */
210        public abstract int getCountLocked(int which);
211
212        /**
213         * Returns the total time in microseconds associated with this Timer for the
214         * selected type of statistics.
215         *
216         * @param batteryRealtime system realtime on  battery in microseconds
217         * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT
218         * @return a time in microseconds
219         */
220        public abstract long getTotalTimeLocked(long batteryRealtime, int which);
221
222        /**
223         * Temporary for debugging.
224         */
225        public abstract void logState(Printer pw, String prefix);
226    }
227
228    /**
229     * The statistics associated with a particular uid.
230     */
231    public static abstract class Uid {
232
233        /**
234         * Returns a mapping containing wakelock statistics.
235         *
236         * @return a Map from Strings to Uid.Wakelock objects.
237         */
238        public abstract Map<String, ? extends Wakelock> getWakelockStats();
239
240        /**
241         * The statistics associated with a particular wake lock.
242         */
243        public static abstract class Wakelock {
244            public abstract Timer getWakeTime(int type);
245        }
246
247        /**
248         * Returns a mapping containing sensor statistics.
249         *
250         * @return a Map from Integer sensor ids to Uid.Sensor objects.
251         */
252        public abstract Map<Integer, ? extends Sensor> getSensorStats();
253
254        /**
255         * Returns a mapping containing active process data.
256         */
257        public abstract SparseArray<? extends Pid> getPidStats();
258
259        /**
260         * Returns a mapping containing process statistics.
261         *
262         * @return a Map from Strings to Uid.Proc objects.
263         */
264        public abstract Map<String, ? extends Proc> getProcessStats();
265
266        /**
267         * Returns a mapping containing package statistics.
268         *
269         * @return a Map from Strings to Uid.Pkg objects.
270         */
271        public abstract Map<String, ? extends Pkg> getPackageStats();
272
273        /**
274         * {@hide}
275         */
276        public abstract int getUid();
277
278        public abstract void noteWifiRunningLocked();
279        public abstract void noteWifiStoppedLocked();
280        public abstract void noteFullWifiLockAcquiredLocked();
281        public abstract void noteFullWifiLockReleasedLocked();
282        public abstract void noteWifiScanStartedLocked();
283        public abstract void noteWifiScanStoppedLocked();
284        public abstract void noteWifiBatchedScanStartedLocked(int csph);
285        public abstract void noteWifiBatchedScanStoppedLocked();
286        public abstract void noteWifiMulticastEnabledLocked();
287        public abstract void noteWifiMulticastDisabledLocked();
288        public abstract void noteAudioTurnedOnLocked();
289        public abstract void noteAudioTurnedOffLocked();
290        public abstract void noteVideoTurnedOnLocked();
291        public abstract void noteVideoTurnedOffLocked();
292        public abstract void noteActivityResumedLocked();
293        public abstract void noteActivityPausedLocked();
294        public abstract long getWifiRunningTime(long batteryRealtime, int which);
295        public abstract long getFullWifiLockTime(long batteryRealtime, int which);
296        public abstract long getWifiScanTime(long batteryRealtime, int which);
297        public abstract long getWifiBatchedScanTime(int csphBin, long batteryRealtime, int which);
298        public abstract long getWifiMulticastTime(long batteryRealtime,
299                                                  int which);
300        public abstract long getAudioTurnedOnTime(long batteryRealtime, int which);
301        public abstract long getVideoTurnedOnTime(long batteryRealtime, int which);
302        public abstract Timer getForegroundActivityTimer();
303        public abstract Timer getVibratorOnTimer();
304
305        public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
306
307        /**
308         * Note that these must match the constants in android.os.PowerManager.
309         * Also, if the user activity types change, the BatteryStatsImpl.VERSION must
310         * also be bumped.
311         */
312        static final String[] USER_ACTIVITY_TYPES = {
313            "other", "button", "touch"
314        };
315
316        public static final int NUM_USER_ACTIVITY_TYPES = 3;
317
318        public abstract void noteUserActivityLocked(int type);
319        public abstract boolean hasUserActivity();
320        public abstract int getUserActivityCount(int type, int which);
321
322        public abstract boolean hasNetworkActivity();
323        public abstract long getNetworkActivityBytes(int type, int which);
324        public abstract long getNetworkActivityPackets(int type, int which);
325
326        public static abstract class Sensor {
327            /*
328             * FIXME: it's not correct to use this magic value because it
329             * could clash with a sensor handle (which are defined by
330             * the sensor HAL, and therefore out of our control
331             */
332            // Magic sensor number for the GPS.
333            public static final int GPS = -10000;
334
335            public abstract int getHandle();
336
337            public abstract Timer getSensorTime();
338        }
339
340        public class Pid {
341            public long mWakeSum;
342            public long mWakeStart;
343        }
344
345        /**
346         * The statistics associated with a particular process.
347         */
348        public static abstract class Proc {
349
350            public static class ExcessivePower {
351                public static final int TYPE_WAKE = 1;
352                public static final int TYPE_CPU = 2;
353
354                public int type;
355                public long overTime;
356                public long usedTime;
357            }
358
359            /**
360             * Returns true if this process is still active in the battery stats.
361             */
362            public abstract boolean isActive();
363
364            /**
365             * Returns the total time (in 1/100 sec) spent executing in user code.
366             *
367             * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
368             */
369            public abstract long getUserTime(int which);
370
371            /**
372             * Returns the total time (in 1/100 sec) spent executing in system code.
373             *
374             * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
375             */
376            public abstract long getSystemTime(int which);
377
378            /**
379             * Returns the number of times the process has been started.
380             *
381             * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
382             */
383            public abstract int getStarts(int which);
384
385            /**
386             * Returns the cpu time spent in microseconds while the process was in the foreground.
387             * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
388             * @return foreground cpu time in microseconds
389             */
390            public abstract long getForegroundTime(int which);
391
392            /**
393             * Returns the approximate cpu time spent in microseconds, at a certain CPU speed.
394             * @param speedStep the index of the CPU speed. This is not the actual speed of the
395             * CPU.
396             * @param which one of STATS_TOTAL, STATS_LAST, STATS_CURRENT or STATS_UNPLUGGED
397             * @see BatteryStats#getCpuSpeedSteps()
398             */
399            public abstract long getTimeAtCpuSpeedStep(int speedStep, int which);
400
401            public abstract int countExcessivePowers();
402
403            public abstract ExcessivePower getExcessivePower(int i);
404        }
405
406        /**
407         * The statistics associated with a particular package.
408         */
409        public static abstract class Pkg {
410
411            /**
412             * Returns the number of times this package has done something that could wake up the
413             * device from sleep.
414             *
415             * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
416             */
417            public abstract int getWakeups(int which);
418
419            /**
420             * Returns a mapping containing service statistics.
421             */
422            public abstract Map<String, ? extends Serv> getServiceStats();
423
424            /**
425             * The statistics associated with a particular service.
426             */
427            public abstract class Serv {
428
429                /**
430                 * Returns the amount of time spent started.
431                 *
432                 * @param batteryUptime elapsed uptime on battery in microseconds.
433                 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
434                 * @return
435                 */
436                public abstract long getStartTime(long batteryUptime, int which);
437
438                /**
439                 * Returns the total number of times startService() has been called.
440                 *
441                 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
442                 */
443                public abstract int getStarts(int which);
444
445                /**
446                 * Returns the total number times the service has been launched.
447                 *
448                 * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
449                 */
450                public abstract int getLaunches(int which);
451            }
452        }
453    }
454
455    public final static class HistoryItem implements Parcelable {
456        public HistoryItem next;
457
458        public long time;
459
460        // The command codes 0-3 can be written with delta updates; all others require
461        // that a full entry be written.
462        public static final byte CMD_UPDATE = 0;
463        public static final byte CMD_EVENT = 1;
464        public static final byte CMD_NULL = -1;
465        public static final byte CMD_START = 4;
466        public static final byte CMD_OVERFLOW = 5;
467
468        public byte cmd = CMD_NULL;
469
470        /**
471         * Return whether the command code is a delta data update.
472         */
473        public static boolean isDeltaData(byte cmd) {
474            return cmd >= 0 && cmd <= 3;
475        }
476
477        /**
478         * Return whether the command code is a delta data update.
479         */
480        public boolean isDeltaData() {
481            return cmd >= 0 && cmd <= 3;
482        }
483
484        public byte batteryLevel;
485        public byte batteryStatus;
486        public byte batteryHealth;
487        public byte batteryPlugType;
488
489        public short batteryTemperature;
490        public char batteryVoltage;
491
492        // Constants from SCREEN_BRIGHTNESS_*
493        public static final int STATE_BRIGHTNESS_SHIFT = 0;
494        public static final int STATE_BRIGHTNESS_MASK = 0x7;
495        // Constants from SIGNAL_STRENGTH_*
496        public static final int STATE_SIGNAL_STRENGTH_SHIFT = 3;
497        public static final int STATE_SIGNAL_STRENGTH_MASK = 0x7 << STATE_SIGNAL_STRENGTH_SHIFT;
498        // Constants from ServiceState.STATE_*
499        public static final int STATE_PHONE_STATE_SHIFT = 6;
500        public static final int STATE_PHONE_STATE_MASK = 0x7 << STATE_PHONE_STATE_SHIFT;
501        // Constants from DATA_CONNECTION_*
502        public static final int STATE_DATA_CONNECTION_SHIFT = 9;
503        public static final int STATE_DATA_CONNECTION_MASK = 0x1f;
504
505        // These states always appear directly in the first int token
506        // of a delta change; they should be ones that change relatively
507        // frequently.
508        public static final int STATE_WAKE_LOCK_FLAG = 1<<30;
509        public static final int STATE_SENSOR_ON_FLAG = 1<<29;
510        public static final int STATE_GPS_ON_FLAG = 1<<28;
511        public static final int STATE_PHONE_SCANNING_FLAG = 1<<27;
512        public static final int STATE_WIFI_RUNNING_FLAG = 1<<26;
513        public static final int STATE_WIFI_FULL_LOCK_FLAG = 1<<25;
514        public static final int STATE_WIFI_SCAN_FLAG = 1<<24;
515        public static final int STATE_WIFI_MULTICAST_ON_FLAG = 1<<23;
516        // These are on the lower bits used for the command; if they change
517        // we need to write another int of data.
518        public static final int STATE_AUDIO_ON_FLAG = 1<<22;
519        public static final int STATE_VIDEO_ON_FLAG = 1<<21;
520        public static final int STATE_SCREEN_ON_FLAG = 1<<20;
521        public static final int STATE_BATTERY_PLUGGED_FLAG = 1<<19;
522        public static final int STATE_PHONE_IN_CALL_FLAG = 1<<18;
523        public static final int STATE_WIFI_ON_FLAG = 1<<17;
524        public static final int STATE_BLUETOOTH_ON_FLAG = 1<<16;
525
526        public static final int MOST_INTERESTING_STATES =
527            STATE_BATTERY_PLUGGED_FLAG | STATE_SCREEN_ON_FLAG
528            | STATE_GPS_ON_FLAG | STATE_PHONE_IN_CALL_FLAG;
529
530        public int states;
531
532        public static final int EVENT_NONE = 0;
533        public static final int EVENT_PROC_STARTED = 1;
534        public static final int EVENT_PROC_FINISHED = 2;
535
536        // For CMD_EVENT.
537        public int eventCode;
538        public String eventName;
539        public int eventNameIdx;    // only filled in when iterating.
540        public int eventUid;
541
542        public HistoryItem() {
543        }
544
545        public HistoryItem(long time, Parcel src) {
546            this.time = time;
547            readFromParcel(src);
548        }
549
550        public int describeContents() {
551            return 0;
552        }
553
554        public void writeToParcel(Parcel dest, int flags) {
555            dest.writeLong(time);
556            int bat = (((int)cmd)&0xff)
557                    | ((((int)batteryLevel)<<8)&0xff00)
558                    | ((((int)batteryStatus)<<16)&0xf0000)
559                    | ((((int)batteryHealth)<<20)&0xf00000)
560                    | ((((int)batteryPlugType)<<24)&0xf000000);
561            dest.writeInt(bat);
562            bat = (((int)batteryTemperature)&0xffff)
563                    | ((((int)batteryVoltage)<<16)&0xffff0000);
564            dest.writeInt(bat);
565            dest.writeInt(states);
566            if (cmd == CMD_EVENT) {
567                dest.writeInt(eventCode);
568                dest.writeInt(eventUid);
569                dest.writeString(eventName);
570            }
571        }
572
573        public void readFromParcel(Parcel src) {
574            int bat = src.readInt();
575            cmd = (byte)(bat&0xff);
576            batteryLevel = (byte)((bat>>8)&0xff);
577            batteryStatus = (byte)((bat>>16)&0xf);
578            batteryHealth = (byte)((bat>>20)&0xf);
579            batteryPlugType = (byte)((bat>>24)&0xf);
580            bat = src.readInt();
581            batteryTemperature = (short)(bat&0xffff);
582            batteryVoltage = (char)((bat>>16)&0xffff);
583            states = src.readInt();
584            if (cmd == CMD_EVENT) {
585                eventCode = src.readInt();
586                eventUid = src.readInt();
587                eventName = src.readString();
588                eventNameIdx = 0;
589            } else {
590                eventCode = EVENT_NONE;
591            }
592        }
593
594        public void clear() {
595            time = 0;
596            cmd = CMD_NULL;
597            batteryLevel = 0;
598            batteryStatus = 0;
599            batteryHealth = 0;
600            batteryPlugType = 0;
601            batteryTemperature = 0;
602            batteryVoltage = 0;
603            states = 0;
604            eventCode = EVENT_NONE;
605            eventUid = 0;
606            eventName = null;
607        }
608
609        public void setTo(HistoryItem o) {
610            time = o.time;
611            cmd = o.cmd;
612            batteryLevel = o.batteryLevel;
613            batteryStatus = o.batteryStatus;
614            batteryHealth = o.batteryHealth;
615            batteryPlugType = o.batteryPlugType;
616            batteryTemperature = o.batteryTemperature;
617            batteryVoltage = o.batteryVoltage;
618            states = o.states;
619            eventCode = o.eventCode;
620            eventUid = o.eventUid;
621            eventName = o.eventName;
622            eventNameIdx = o.eventNameIdx;
623        }
624
625        public void setTo(long time, byte cmd, int eventCode, int eventUid, String eventName,
626                HistoryItem o) {
627            this.time = time;
628            this.cmd = cmd;
629            this.eventCode = eventCode;
630            this.eventUid = eventUid;
631            this.eventName = eventName;
632            this.eventNameIdx = 0;
633            batteryLevel = o.batteryLevel;
634            batteryStatus = o.batteryStatus;
635            batteryHealth = o.batteryHealth;
636            batteryPlugType = o.batteryPlugType;
637            batteryTemperature = o.batteryTemperature;
638            batteryVoltage = o.batteryVoltage;
639            states = o.states;
640        }
641
642        public boolean sameNonEvent(HistoryItem o) {
643            return batteryLevel == o.batteryLevel
644                    && batteryStatus == o.batteryStatus
645                    && batteryHealth == o.batteryHealth
646                    && batteryPlugType == o.batteryPlugType
647                    && batteryTemperature == o.batteryTemperature
648                    && batteryVoltage == o.batteryVoltage
649                    && states == o.states;
650        }
651
652        public boolean same(HistoryItem o) {
653            if (!sameNonEvent(o) || eventCode != o.eventCode || eventUid != o.eventUid) {
654                return false;
655            }
656            if (eventName == o.eventName) {
657                return true;
658            }
659            return eventName != null && o.eventName != null && eventName.equals(o.eventName);
660        }
661    }
662
663    public static final class BitDescription {
664        public final int mask;
665        public final int shift;
666        public final String name;
667        public final String shortName;
668        public final String[] values;
669        public final String[] shortValues;
670
671        public BitDescription(int mask, String name, String shortName) {
672            this.mask = mask;
673            this.shift = -1;
674            this.name = name;
675            this.shortName = shortName;
676            this.values = null;
677            this.shortValues = null;
678        }
679
680        public BitDescription(int mask, int shift, String name, String shortName,
681                String[] values, String[] shortValues) {
682            this.mask = mask;
683            this.shift = shift;
684            this.name = name;
685            this.shortName = shortName;
686            this.values = values;
687            this.shortValues = shortValues;
688        }
689    }
690
691    public abstract boolean startIteratingHistoryLocked();
692
693    public abstract int getHistoryStringPoolSize();
694
695    public abstract String getHistoryStringPoolItem(int index);
696
697    public abstract boolean getNextHistoryLocked(HistoryItem out);
698
699    public abstract void finishIteratingHistoryLocked();
700
701    public abstract boolean startIteratingOldHistoryLocked();
702
703    public abstract boolean getNextOldHistoryLocked(HistoryItem out);
704
705    public abstract void finishIteratingOldHistoryLocked();
706
707    /**
708     * Return the base time offset for the battery history.
709     */
710    public abstract long getHistoryBaseTime();
711
712    /**
713     * Returns the number of times the device has been started.
714     */
715    public abstract int getStartCount();
716
717    /**
718     * Returns the time in microseconds that the screen has been on while the device was
719     * running on battery.
720     *
721     * {@hide}
722     */
723    public abstract long getScreenOnTime(long batteryRealtime, int which);
724
725    public static final int SCREEN_BRIGHTNESS_DARK = 0;
726    public static final int SCREEN_BRIGHTNESS_DIM = 1;
727    public static final int SCREEN_BRIGHTNESS_MEDIUM = 2;
728    public static final int SCREEN_BRIGHTNESS_LIGHT = 3;
729    public static final int SCREEN_BRIGHTNESS_BRIGHT = 4;
730
731    static final String[] SCREEN_BRIGHTNESS_NAMES = {
732        "dark", "dim", "medium", "light", "bright"
733    };
734
735    static final String[] SCREEN_BRIGHTNESS_SHORT_NAMES = {
736        "0", "1", "2", "3", "4"
737    };
738
739    public static final int NUM_SCREEN_BRIGHTNESS_BINS = 5;
740
741    /**
742     * Returns the time in microseconds that the screen has been on with
743     * the given brightness
744     *
745     * {@hide}
746     */
747    public abstract long getScreenBrightnessTime(int brightnessBin,
748            long batteryRealtime, int which);
749
750    public abstract int getInputEventCount(int which);
751
752    /**
753     * Returns the time in microseconds that the phone has been on while the device was
754     * running on battery.
755     *
756     * {@hide}
757     */
758    public abstract long getPhoneOnTime(long batteryRealtime, int which);
759
760    /**
761     * Returns the time in microseconds that the phone has been running with
762     * the given signal strength.
763     *
764     * {@hide}
765     */
766    public abstract long getPhoneSignalStrengthTime(int strengthBin,
767            long batteryRealtime, int which);
768
769    /**
770     * Returns the time in microseconds that the phone has been trying to
771     * acquire a signal.
772     *
773     * {@hide}
774     */
775    public abstract long getPhoneSignalScanningTime(
776            long batteryRealtime, int which);
777
778    /**
779     * Returns the number of times the phone has entered the given signal strength.
780     *
781     * {@hide}
782     */
783    public abstract int getPhoneSignalStrengthCount(int strengthBin, int which);
784
785    public static final int DATA_CONNECTION_NONE = 0;
786    public static final int DATA_CONNECTION_GPRS = 1;
787    public static final int DATA_CONNECTION_EDGE = 2;
788    public static final int DATA_CONNECTION_UMTS = 3;
789    public static final int DATA_CONNECTION_CDMA = 4;
790    public static final int DATA_CONNECTION_EVDO_0 = 5;
791    public static final int DATA_CONNECTION_EVDO_A = 6;
792    public static final int DATA_CONNECTION_1xRTT = 7;
793    public static final int DATA_CONNECTION_HSDPA = 8;
794    public static final int DATA_CONNECTION_HSUPA = 9;
795    public static final int DATA_CONNECTION_HSPA = 10;
796    public static final int DATA_CONNECTION_IDEN = 11;
797    public static final int DATA_CONNECTION_EVDO_B = 12;
798    public static final int DATA_CONNECTION_LTE = 13;
799    public static final int DATA_CONNECTION_EHRPD = 14;
800    public static final int DATA_CONNECTION_HSPAP = 15;
801    public static final int DATA_CONNECTION_OTHER = 16;
802
803    static final String[] DATA_CONNECTION_NAMES = {
804        "none", "gprs", "edge", "umts", "cdma", "evdo_0", "evdo_A",
805        "1xrtt", "hsdpa", "hsupa", "hspa", "iden", "evdo_b", "lte",
806        "ehrpd", "hspap", "other"
807    };
808
809    public static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER+1;
810
811    /**
812     * Returns the time in microseconds that the phone has been running with
813     * the given data connection.
814     *
815     * {@hide}
816     */
817    public abstract long getPhoneDataConnectionTime(int dataType,
818            long batteryRealtime, int which);
819
820    /**
821     * Returns the number of times the phone has entered the given data
822     * connection type.
823     *
824     * {@hide}
825     */
826    public abstract int getPhoneDataConnectionCount(int dataType, int which);
827
828    public static final BitDescription[] HISTORY_STATE_DESCRIPTIONS
829            = new BitDescription[] {
830        new BitDescription(HistoryItem.STATE_BATTERY_PLUGGED_FLAG, "plugged", "BP"),
831        new BitDescription(HistoryItem.STATE_SCREEN_ON_FLAG, "screen", "S"),
832        new BitDescription(HistoryItem.STATE_GPS_ON_FLAG, "gps", "g"),
833        new BitDescription(HistoryItem.STATE_PHONE_IN_CALL_FLAG, "phone_in_call", "Pcl"),
834        new BitDescription(HistoryItem.STATE_PHONE_SCANNING_FLAG, "phone_scanning", "Psc"),
835        new BitDescription(HistoryItem.STATE_WIFI_ON_FLAG, "wifi", "W"),
836        new BitDescription(HistoryItem.STATE_WIFI_RUNNING_FLAG, "wifi_running", "Wr"),
837        new BitDescription(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG, "wifi_full_lock", "Wl"),
838        new BitDescription(HistoryItem.STATE_WIFI_SCAN_FLAG, "wifi_scan", "Ws"),
839        new BitDescription(HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG, "wifi_multicast", "Wm"),
840        new BitDescription(HistoryItem.STATE_BLUETOOTH_ON_FLAG, "bluetooth", "b"),
841        new BitDescription(HistoryItem.STATE_AUDIO_ON_FLAG, "audio", "a"),
842        new BitDescription(HistoryItem.STATE_VIDEO_ON_FLAG, "video", "v"),
843        new BitDescription(HistoryItem.STATE_WAKE_LOCK_FLAG, "wake_lock", "w"),
844        new BitDescription(HistoryItem.STATE_SENSOR_ON_FLAG, "sensor", "s"),
845        new BitDescription(HistoryItem.STATE_BRIGHTNESS_MASK,
846                HistoryItem.STATE_BRIGHTNESS_SHIFT, "brightness", "Sb",
847                SCREEN_BRIGHTNESS_NAMES, SCREEN_BRIGHTNESS_SHORT_NAMES),
848        new BitDescription(HistoryItem.STATE_SIGNAL_STRENGTH_MASK,
849                HistoryItem.STATE_SIGNAL_STRENGTH_SHIFT, "signal_strength", "Pss",
850                SignalStrength.SIGNAL_STRENGTH_NAMES, new String[] {
851                    "0", "1", "2", "3", "4"
852        }),
853        new BitDescription(HistoryItem.STATE_PHONE_STATE_MASK,
854                HistoryItem.STATE_PHONE_STATE_SHIFT, "phone_state", "Pst",
855                new String[] {"in", "out", "emergency", "off"},
856                new String[] {"in", "out", "em", "off"}),
857        new BitDescription(HistoryItem.STATE_DATA_CONNECTION_MASK,
858                HistoryItem.STATE_DATA_CONNECTION_SHIFT, "data_conn", "Pcn",
859                DATA_CONNECTION_NAMES, DATA_CONNECTION_NAMES),
860    };
861
862    /**
863     * Returns the time in microseconds that wifi has been on while the device was
864     * running on battery.
865     *
866     * {@hide}
867     */
868    public abstract long getWifiOnTime(long batteryRealtime, int which);
869
870    /**
871     * Returns the time in microseconds that wifi has been on and the driver has
872     * been in the running state while the device was running on battery.
873     *
874     * {@hide}
875     */
876    public abstract long getGlobalWifiRunningTime(long batteryRealtime, int which);
877
878    /**
879     * Returns the time in microseconds that bluetooth has been on while the device was
880     * running on battery.
881     *
882     * {@hide}
883     */
884    public abstract long getBluetoothOnTime(long batteryRealtime, int which);
885
886    public abstract int getBluetoothPingCount();
887
888    public static final int NETWORK_MOBILE_RX_DATA = 0;
889    public static final int NETWORK_MOBILE_TX_DATA = 1;
890    public static final int NETWORK_WIFI_RX_DATA = 2;
891    public static final int NETWORK_WIFI_TX_DATA = 3;
892
893    public static final int NUM_NETWORK_ACTIVITY_TYPES = NETWORK_WIFI_TX_DATA + 1;
894
895    public abstract long getNetworkActivityBytes(int type, int which);
896    public abstract long getNetworkActivityPackets(int type, int which);
897
898    /**
899     * Return the wall clock time when battery stats data collection started.
900     */
901    public abstract long getStartClockTime();
902
903    /**
904     * Return whether we are currently running on battery.
905     */
906    public abstract boolean getIsOnBattery();
907
908    /**
909     * Returns a SparseArray containing the statistics for each uid.
910     */
911    public abstract SparseArray<? extends Uid> getUidStats();
912
913    /**
914     * Returns the current battery uptime in microseconds.
915     *
916     * @param curTime the amount of elapsed realtime in microseconds.
917     */
918    public abstract long getBatteryUptime(long curTime);
919
920    /**
921     * @deprecated use getRadioDataUptime
922     */
923    public long getRadioDataUptimeMs() {
924        return getRadioDataUptime() / 1000;
925    }
926
927    /**
928     * Returns the time that the radio was on for data transfers.
929     * @return the uptime in microseconds while unplugged
930     */
931    public abstract long getRadioDataUptime();
932
933    /**
934     * Returns the current battery realtime in microseconds.
935     *
936     * @param curTime the amount of elapsed realtime in microseconds.
937     */
938    public abstract long getBatteryRealtime(long curTime);
939
940    /**
941     * Returns the battery percentage level at the last time the device was unplugged from power, or
942     * the last time it booted on battery power.
943     */
944    public abstract int getDischargeStartLevel();
945
946    /**
947     * Returns the current battery percentage level if we are in a discharge cycle, otherwise
948     * returns the level at the last plug event.
949     */
950    public abstract int getDischargeCurrentLevel();
951
952    /**
953     * Get the amount the battery has discharged since the stats were
954     * last reset after charging, as a lower-end approximation.
955     */
956    public abstract int getLowDischargeAmountSinceCharge();
957
958    /**
959     * Get the amount the battery has discharged since the stats were
960     * last reset after charging, as an upper-end approximation.
961     */
962    public abstract int getHighDischargeAmountSinceCharge();
963
964    /**
965     * Get the amount the battery has discharged while the screen was on,
966     * since the last time power was unplugged.
967     */
968    public abstract int getDischargeAmountScreenOn();
969
970    /**
971     * Get the amount the battery has discharged while the screen was on,
972     * since the last time the device was charged.
973     */
974    public abstract int getDischargeAmountScreenOnSinceCharge();
975
976    /**
977     * Get the amount the battery has discharged while the screen was off,
978     * since the last time power was unplugged.
979     */
980    public abstract int getDischargeAmountScreenOff();
981
982    /**
983     * Get the amount the battery has discharged while the screen was off,
984     * since the last time the device was charged.
985     */
986    public abstract int getDischargeAmountScreenOffSinceCharge();
987
988    /**
989     * Returns the total, last, or current battery uptime in microseconds.
990     *
991     * @param curTime the elapsed realtime in microseconds.
992     * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
993     */
994    public abstract long computeBatteryUptime(long curTime, int which);
995
996    /**
997     * Returns the total, last, or current battery realtime in microseconds.
998     *
999     * @param curTime the current elapsed realtime in microseconds.
1000     * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1001     */
1002    public abstract long computeBatteryRealtime(long curTime, int which);
1003
1004    /**
1005     * Returns the total, last, or current uptime in microseconds.
1006     *
1007     * @param curTime the current elapsed realtime in microseconds.
1008     * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1009     */
1010    public abstract long computeUptime(long curTime, int which);
1011
1012    /**
1013     * Returns the total, last, or current realtime in microseconds.
1014     * *
1015     * @param curTime the current elapsed realtime in microseconds.
1016     * @param which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1017     */
1018    public abstract long computeRealtime(long curTime, int which);
1019
1020    public abstract Map<String, ? extends Timer> getKernelWakelockStats();
1021
1022    /** Returns the number of different speeds that the CPU can run at */
1023    public abstract int getCpuSpeedSteps();
1024
1025    public abstract void writeToParcelWithoutUids(Parcel out, int flags);
1026
1027    private final static void formatTimeRaw(StringBuilder out, long seconds) {
1028        long days = seconds / (60 * 60 * 24);
1029        if (days != 0) {
1030            out.append(days);
1031            out.append("d ");
1032        }
1033        long used = days * 60 * 60 * 24;
1034
1035        long hours = (seconds - used) / (60 * 60);
1036        if (hours != 0 || used != 0) {
1037            out.append(hours);
1038            out.append("h ");
1039        }
1040        used += hours * 60 * 60;
1041
1042        long mins = (seconds-used) / 60;
1043        if (mins != 0 || used != 0) {
1044            out.append(mins);
1045            out.append("m ");
1046        }
1047        used += mins * 60;
1048
1049        if (seconds != 0 || used != 0) {
1050            out.append(seconds-used);
1051            out.append("s ");
1052        }
1053    }
1054
1055    private final static void formatTime(StringBuilder sb, long time) {
1056        long sec = time / 100;
1057        formatTimeRaw(sb, sec);
1058        sb.append((time - (sec * 100)) * 10);
1059        sb.append("ms ");
1060    }
1061
1062    private final static void formatTimeMs(StringBuilder sb, long time) {
1063        long sec = time / 1000;
1064        formatTimeRaw(sb, sec);
1065        sb.append(time - (sec * 1000));
1066        sb.append("ms ");
1067    }
1068
1069    private final String formatRatioLocked(long num, long den) {
1070        if (den == 0L) {
1071            return "--%";
1072        }
1073        float perc = ((float)num) / ((float)den) * 100;
1074        mFormatBuilder.setLength(0);
1075        mFormatter.format("%.1f%%", perc);
1076        return mFormatBuilder.toString();
1077    }
1078
1079    private final String formatBytesLocked(long bytes) {
1080        mFormatBuilder.setLength(0);
1081
1082        if (bytes < BYTES_PER_KB) {
1083            return bytes + "B";
1084        } else if (bytes < BYTES_PER_MB) {
1085            mFormatter.format("%.2fKB", bytes / (double) BYTES_PER_KB);
1086            return mFormatBuilder.toString();
1087        } else if (bytes < BYTES_PER_GB){
1088            mFormatter.format("%.2fMB", bytes / (double) BYTES_PER_MB);
1089            return mFormatBuilder.toString();
1090        } else {
1091            mFormatter.format("%.2fGB", bytes / (double) BYTES_PER_GB);
1092            return mFormatBuilder.toString();
1093        }
1094    }
1095
1096    private static long computeWakeLock(Timer timer, long batteryRealtime, int which) {
1097        if (timer != null) {
1098            // Convert from microseconds to milliseconds with rounding
1099            long totalTimeMicros = timer.getTotalTimeLocked(batteryRealtime, which);
1100            long totalTimeMillis = (totalTimeMicros + 500) / 1000;
1101            return totalTimeMillis;
1102        }
1103        return 0;
1104    }
1105
1106    /**
1107     *
1108     * @param sb a StringBuilder object.
1109     * @param timer a Timer object contining the wakelock times.
1110     * @param batteryRealtime the current on-battery time in microseconds.
1111     * @param name the name of the wakelock.
1112     * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1113     * @param linePrefix a String to be prepended to each line of output.
1114     * @return the line prefix
1115     */
1116    private static final String printWakeLock(StringBuilder sb, Timer timer,
1117            long batteryRealtime, String name, int which, String linePrefix) {
1118
1119        if (timer != null) {
1120            long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1121
1122            int count = timer.getCountLocked(which);
1123            if (totalTimeMillis != 0) {
1124                sb.append(linePrefix);
1125                formatTimeMs(sb, totalTimeMillis);
1126                if (name != null) {
1127                    sb.append(name);
1128                    sb.append(' ');
1129                }
1130                sb.append('(');
1131                sb.append(count);
1132                sb.append(" times)");
1133                return ", ";
1134            }
1135        }
1136        return linePrefix;
1137    }
1138
1139    /**
1140     * Checkin version of wakelock printer. Prints simple comma-separated list.
1141     *
1142     * @param sb a StringBuilder object.
1143     * @param timer a Timer object contining the wakelock times.
1144     * @param now the current time in microseconds.
1145     * @param name the name of the wakelock.
1146     * @param which which one of STATS_TOTAL, STATS_LAST, or STATS_CURRENT.
1147     * @param linePrefix a String to be prepended to each line of output.
1148     * @return the line prefix
1149     */
1150    private static final String printWakeLockCheckin(StringBuilder sb, Timer timer, long now,
1151            String name, int which, String linePrefix) {
1152        long totalTimeMicros = 0;
1153        int count = 0;
1154        if (timer != null) {
1155            totalTimeMicros = timer.getTotalTimeLocked(now, which);
1156            count = timer.getCountLocked(which);
1157        }
1158        sb.append(linePrefix);
1159        sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
1160        sb.append(',');
1161        sb.append(name != null ? name + "," : "");
1162        sb.append(count);
1163        return ",";
1164    }
1165
1166    /**
1167     * Dump a comma-separated line of values for terse checkin mode.
1168     *
1169     * @param pw the PageWriter to dump log to
1170     * @param category category of data (e.g. "total", "last", "unplugged", "current" )
1171     * @param type type of data (e.g. "wakelock", "sensor", "process", "apk" ,  "process", "network")
1172     * @param args type-dependent data arguments
1173     */
1174    private static final void dumpLine(PrintWriter pw, int uid, String category, String type,
1175           Object... args ) {
1176        pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
1177        pw.print(uid); pw.print(',');
1178        pw.print(category); pw.print(',');
1179        pw.print(type);
1180
1181        for (Object arg : args) {
1182            pw.print(',');
1183            pw.print(arg);
1184        }
1185        pw.println();
1186    }
1187
1188    /**
1189     * Checkin server version of dump to produce more compact, computer-readable log.
1190     *
1191     * NOTE: all times are expressed in 'ms'.
1192     */
1193    public final void dumpCheckinLocked(Context context, PrintWriter pw, int which, int reqUid) {
1194        final long rawUptime = SystemClock.uptimeMillis() * 1000;
1195        final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1196        final long batteryUptime = getBatteryUptime(rawUptime);
1197        final long batteryRealtime = getBatteryRealtime(rawRealtime);
1198        final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1199        final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1200        final long totalRealtime = computeRealtime(rawRealtime, which);
1201        final long totalUptime = computeUptime(rawUptime, which);
1202        final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1203        final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
1204        final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1205        final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
1206        final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
1207
1208        StringBuilder sb = new StringBuilder(128);
1209
1210        SparseArray<? extends Uid> uidStats = getUidStats();
1211        final int NU = uidStats.size();
1212
1213        String category = STAT_NAMES[which];
1214
1215        // Dump "battery" stat
1216        dumpLine(pw, 0 /* uid */, category, BATTERY_DATA,
1217                which == STATS_SINCE_CHARGED ? getStartCount() : "N/A",
1218                whichBatteryRealtime / 1000, whichBatteryUptime / 1000,
1219                totalRealtime / 1000, totalUptime / 1000,
1220                getStartClockTime());
1221
1222        // Calculate wakelock times across all uids.
1223        long fullWakeLockTimeTotal = 0;
1224        long partialWakeLockTimeTotal = 0;
1225
1226        for (int iu = 0; iu < NU; iu++) {
1227            Uid u = uidStats.valueAt(iu);
1228
1229            Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1230            if (wakelocks.size() > 0) {
1231                for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1232                        : wakelocks.entrySet()) {
1233                    Uid.Wakelock wl = ent.getValue();
1234
1235                    Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1236                    if (fullWakeTimer != null) {
1237                        fullWakeLockTimeTotal += fullWakeTimer.getTotalTimeLocked(batteryRealtime, which);
1238                    }
1239
1240                    Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1241                    if (partialWakeTimer != null) {
1242                        partialWakeLockTimeTotal += partialWakeTimer.getTotalTimeLocked(
1243                            batteryRealtime, which);
1244                    }
1245                }
1246            }
1247        }
1248
1249        long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1250        long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1251        long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1252        long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1253        long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1254        long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1255        long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1256        long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1257
1258        // Dump network stats
1259        dumpLine(pw, 0 /* uid */, category, GLOBAL_NETWORK_DATA,
1260                mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1261                mobileRxTotalPackets, mobileTxTotalPackets, wifiRxTotalPackets, wifiTxTotalPackets);
1262
1263        // Dump misc stats
1264        dumpLine(pw, 0 /* uid */, category, MISC_DATA,
1265                screenOnTime / 1000, phoneOnTime / 1000, wifiOnTime / 1000,
1266                wifiRunningTime / 1000, bluetoothOnTime / 1000,
1267                mobileRxTotalBytes, mobileTxTotalBytes, wifiRxTotalBytes, wifiTxTotalBytes,
1268                fullWakeLockTimeTotal, partialWakeLockTimeTotal,
1269                getInputEventCount(which));
1270
1271        // Dump screen brightness stats
1272        Object[] args = new Object[NUM_SCREEN_BRIGHTNESS_BINS];
1273        for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1274            args[i] = getScreenBrightnessTime(i, batteryRealtime, which) / 1000;
1275        }
1276        dumpLine(pw, 0 /* uid */, category, SCREEN_BRIGHTNESS_DATA, args);
1277
1278        // Dump signal strength stats
1279        args = new Object[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
1280        for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1281            args[i] = getPhoneSignalStrengthTime(i, batteryRealtime, which) / 1000;
1282        }
1283        dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_TIME_DATA, args);
1284        dumpLine(pw, 0 /* uid */, category, SIGNAL_SCANNING_TIME_DATA,
1285                getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1286        for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1287            args[i] = getPhoneSignalStrengthCount(i, which);
1288        }
1289        dumpLine(pw, 0 /* uid */, category, SIGNAL_STRENGTH_COUNT_DATA, args);
1290
1291        // Dump network type stats
1292        args = new Object[NUM_DATA_CONNECTION_TYPES];
1293        for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1294            args[i] = getPhoneDataConnectionTime(i, batteryRealtime, which) / 1000;
1295        }
1296        dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_TIME_DATA, args);
1297        for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1298            args[i] = getPhoneDataConnectionCount(i, which);
1299        }
1300        dumpLine(pw, 0 /* uid */, category, DATA_CONNECTION_COUNT_DATA, args);
1301
1302        if (which == STATS_SINCE_UNPLUGGED) {
1303            dumpLine(pw, 0 /* uid */, category, BATTERY_LEVEL_DATA, getDischargeStartLevel(),
1304                    getDischargeCurrentLevel());
1305        }
1306
1307        if (which == STATS_SINCE_UNPLUGGED) {
1308            dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1309                    getDischargeStartLevel()-getDischargeCurrentLevel(),
1310                    getDischargeStartLevel()-getDischargeCurrentLevel(),
1311                    getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1312        } else {
1313            dumpLine(pw, 0 /* uid */, category, BATTERY_DISCHARGE_DATA,
1314                    getLowDischargeAmountSinceCharge(), getHighDischargeAmountSinceCharge(),
1315                    getDischargeAmountScreenOn(), getDischargeAmountScreenOff());
1316        }
1317
1318        if (reqUid < 0) {
1319            Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1320            if (kernelWakelocks.size() > 0) {
1321                for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1322                    sb.setLength(0);
1323                    printWakeLockCheckin(sb, ent.getValue(), batteryRealtime, null, which, "");
1324
1325                    dumpLine(pw, 0 /* uid */, category, KERNEL_WAKELOCK_DATA, ent.getKey(),
1326                            sb.toString());
1327                }
1328            }
1329        }
1330
1331        BatteryStatsHelper helper = new BatteryStatsHelper(context);
1332        helper.create(this);
1333        helper.refreshStats(which, UserHandle.USER_ALL);
1334        List<BatterySipper> sippers = helper.getUsageList();
1335        if (sippers != null && sippers.size() > 0) {
1336            dumpLine(pw, 0 /* uid */, category, POWER_USE_SUMMARY_DATA,
1337                    BatteryStatsHelper.makemAh(helper.getPowerProfile().getBatteryCapacity()),
1338                    BatteryStatsHelper.makemAh(helper.getComputedPower()),
1339                    BatteryStatsHelper.makemAh(helper.getMinDrainedPower()),
1340                    BatteryStatsHelper.makemAh(helper.getMaxDrainedPower()));
1341            for (int i=0; i<sippers.size(); i++) {
1342                BatterySipper bs = sippers.get(i);
1343                int uid = 0;
1344                String label;
1345                switch (bs.drainType) {
1346                    case IDLE:
1347                        label="idle";
1348                        break;
1349                    case CELL:
1350                        label="cell";
1351                        break;
1352                    case PHONE:
1353                        label="phone";
1354                        break;
1355                    case WIFI:
1356                        label="wifi";
1357                        break;
1358                    case BLUETOOTH:
1359                        label="blue";
1360                        break;
1361                    case SCREEN:
1362                        label="scrn";
1363                        break;
1364                    case APP:
1365                        uid = bs.uidObj.getUid();
1366                        label = "uid";
1367                        break;
1368                    case USER:
1369                        uid = UserHandle.getUid(bs.userId, 0);
1370                        label = "user";
1371                        break;
1372                    case UNACCOUNTED:
1373                        label = "unacc";
1374                        break;
1375                    case OVERCOUNTED:
1376                        label = "over";
1377                        break;
1378                    default:
1379                        label = "???";
1380                }
1381                dumpLine(pw, uid, category, POWER_USE_ITEM_DATA, label,
1382                        BatteryStatsHelper.makemAh(bs.value));
1383            }
1384        }
1385
1386        for (int iu = 0; iu < NU; iu++) {
1387            final int uid = uidStats.keyAt(iu);
1388            if (reqUid >= 0 && uid != reqUid) {
1389                continue;
1390            }
1391            Uid u = uidStats.valueAt(iu);
1392            // Dump Network stats per uid, if any
1393            long mobileBytesRx = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1394            long mobileBytesTx = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1395            long wifiBytesRx = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1396            long wifiBytesTx = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1397            long mobilePacketsRx = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1398            long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1399            long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1400            long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1401            long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
1402            long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
1403            long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
1404
1405            if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
1406                    || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
1407                    || wifiPacketsTx > 0) {
1408                dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
1409                        wifiBytesRx, wifiBytesTx,
1410                        mobilePacketsRx, mobilePacketsTx,
1411                        wifiPacketsRx, wifiPacketsTx);
1412            }
1413
1414            if (fullWifiLockOnTime != 0 || wifiScanTime != 0
1415                    || uidWifiRunningTime != 0) {
1416                dumpLine(pw, uid, category, WIFI_DATA,
1417                        fullWifiLockOnTime, wifiScanTime, uidWifiRunningTime);
1418            }
1419
1420            if (u.hasUserActivity()) {
1421                args = new Object[Uid.NUM_USER_ACTIVITY_TYPES];
1422                boolean hasData = false;
1423                for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1424                    int val = u.getUserActivityCount(i, which);
1425                    args[i] = val;
1426                    if (val != 0) hasData = true;
1427                }
1428                if (hasData) {
1429                    dumpLine(pw, 0 /* uid */, category, USER_ACTIVITY_DATA, args);
1430                }
1431            }
1432
1433            Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1434            if (wakelocks.size() > 0) {
1435                for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1436                        : wakelocks.entrySet()) {
1437                    Uid.Wakelock wl = ent.getValue();
1438                    String linePrefix = "";
1439                    sb.setLength(0);
1440                    linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_FULL),
1441                            batteryRealtime, "f", which, linePrefix);
1442                    linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL),
1443                            batteryRealtime, "p", which, linePrefix);
1444                    linePrefix = printWakeLockCheckin(sb, wl.getWakeTime(WAKE_TYPE_WINDOW),
1445                            batteryRealtime, "w", which, linePrefix);
1446
1447                    // Only log if we had at lease one wakelock...
1448                    if (sb.length() > 0) {
1449                        String name = ent.getKey();
1450                        if (name.indexOf(',') >= 0) {
1451                            name = name.replace(',', '_');
1452                        }
1453                        dumpLine(pw, uid, category, WAKELOCK_DATA, name, sb.toString());
1454                    }
1455                }
1456            }
1457
1458            Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
1459            if (sensors.size() > 0)  {
1460                for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
1461                        : sensors.entrySet()) {
1462                    Uid.Sensor se = ent.getValue();
1463                    int sensorNumber = ent.getKey();
1464                    Timer timer = se.getSensorTime();
1465                    if (timer != null) {
1466                        // Convert from microseconds to milliseconds with rounding
1467                        long totalTime = (timer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1468                        int count = timer.getCountLocked(which);
1469                        if (totalTime != 0) {
1470                            dumpLine(pw, uid, category, SENSOR_DATA, sensorNumber, totalTime, count);
1471                        }
1472                    }
1473                }
1474            }
1475
1476            Timer vibTimer = u.getVibratorOnTimer();
1477            if (vibTimer != null) {
1478                // Convert from microseconds to milliseconds with rounding
1479                long totalTime = (vibTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1480                int count = vibTimer.getCountLocked(which);
1481                if (totalTime != 0) {
1482                    dumpLine(pw, uid, category, VIBRATOR_DATA, totalTime, count);
1483                }
1484            }
1485
1486            Timer fgTimer = u.getForegroundActivityTimer();
1487            if (fgTimer != null) {
1488                // Convert from microseconds to milliseconds with rounding
1489                long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
1490                int count = fgTimer.getCountLocked(which);
1491                if (totalTime != 0) {
1492                    dumpLine(pw, uid, category, FOREGROUND_DATA, totalTime, count);
1493                }
1494            }
1495
1496            Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
1497            if (processStats.size() > 0) {
1498                for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
1499                        : processStats.entrySet()) {
1500                    Uid.Proc ps = ent.getValue();
1501
1502                    final long userMillis = ps.getUserTime(which) * 10;
1503                    final long systemMillis = ps.getSystemTime(which) * 10;
1504                    final long foregroundMillis = ps.getForegroundTime(which) * 10;
1505                    final long starts = ps.getStarts(which);
1506
1507                    if (userMillis != 0 || systemMillis != 0 || foregroundMillis != 0
1508                            || starts != 0) {
1509                        dumpLine(pw, uid, category, PROCESS_DATA, ent.getKey(), userMillis,
1510                                systemMillis, foregroundMillis, starts);
1511                    }
1512                }
1513            }
1514
1515            Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
1516            if (packageStats.size() > 0) {
1517                for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
1518                        : packageStats.entrySet()) {
1519
1520                    Uid.Pkg ps = ent.getValue();
1521                    int wakeups = ps.getWakeups(which);
1522                    Map<String, ? extends  Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
1523                    for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
1524                            : serviceStats.entrySet()) {
1525                        BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
1526                        long startTime = ss.getStartTime(batteryUptime, which);
1527                        int starts = ss.getStarts(which);
1528                        int launches = ss.getLaunches(which);
1529                        if (startTime != 0 || starts != 0 || launches != 0) {
1530                            dumpLine(pw, uid, category, APK_DATA,
1531                                    wakeups, // wakeup alarms
1532                                    ent.getKey(), // Apk
1533                                    sent.getKey(), // service
1534                                    startTime / 1000, // time spent started, in ms
1535                                    starts,
1536                                    launches);
1537                        }
1538                    }
1539                }
1540            }
1541        }
1542    }
1543
1544    static final class TimerEntry {
1545        final String mName;
1546        final int mId;
1547        final BatteryStats.Timer mTimer;
1548        final long mTime;
1549        TimerEntry(String name, int id, BatteryStats.Timer timer, long time) {
1550            mName = name;
1551            mId = id;
1552            mTimer = timer;
1553            mTime = time;
1554        }
1555    }
1556
1557    private void printmAh(PrintWriter printer, double power) {
1558        printer.print(BatteryStatsHelper.makemAh(power));
1559    }
1560
1561    @SuppressWarnings("unused")
1562    public final void dumpLocked(Context context, PrintWriter pw, String prefix, final int which,
1563            int reqUid) {
1564        final long rawUptime = SystemClock.uptimeMillis() * 1000;
1565        final long rawRealtime = SystemClock.elapsedRealtime() * 1000;
1566        final long batteryUptime = getBatteryUptime(rawUptime);
1567        final long batteryRealtime = getBatteryRealtime(rawRealtime);
1568
1569        final long whichBatteryUptime = computeBatteryUptime(rawUptime, which);
1570        final long whichBatteryRealtime = computeBatteryRealtime(rawRealtime, which);
1571        final long totalRealtime = computeRealtime(rawRealtime, which);
1572        final long totalUptime = computeUptime(rawUptime, which);
1573
1574        StringBuilder sb = new StringBuilder(128);
1575
1576        SparseArray<? extends Uid> uidStats = getUidStats();
1577        final int NU = uidStats.size();
1578
1579        sb.setLength(0);
1580        sb.append(prefix);
1581                sb.append("  Time on battery: ");
1582                formatTimeMs(sb, whichBatteryRealtime / 1000); sb.append("(");
1583                sb.append(formatRatioLocked(whichBatteryRealtime, totalRealtime));
1584                sb.append(") realtime, ");
1585                formatTimeMs(sb, whichBatteryUptime / 1000);
1586                sb.append("("); sb.append(formatRatioLocked(whichBatteryUptime, totalRealtime));
1587                sb.append(") uptime");
1588        pw.println(sb.toString());
1589        sb.setLength(0);
1590        sb.append(prefix);
1591                sb.append("  Total run time: ");
1592                formatTimeMs(sb, totalRealtime / 1000);
1593                sb.append("realtime, ");
1594                formatTimeMs(sb, totalUptime / 1000);
1595                sb.append("uptime, ");
1596        pw.println(sb.toString());
1597        pw.print("  Start clock time: ");
1598        pw.println(DateFormat.format("yyyy-MM-dd-HH-mm-ss", getStartClockTime()).toString());
1599
1600        final long screenOnTime = getScreenOnTime(batteryRealtime, which);
1601        final long phoneOnTime = getPhoneOnTime(batteryRealtime, which);
1602        final long wifiRunningTime = getGlobalWifiRunningTime(batteryRealtime, which);
1603        final long wifiOnTime = getWifiOnTime(batteryRealtime, which);
1604        final long bluetoothOnTime = getBluetoothOnTime(batteryRealtime, which);
1605        sb.setLength(0);
1606        sb.append(prefix);
1607                sb.append("  Screen on: "); formatTimeMs(sb, screenOnTime / 1000);
1608                sb.append("("); sb.append(formatRatioLocked(screenOnTime, whichBatteryRealtime));
1609                sb.append("), Input events: "); sb.append(getInputEventCount(which));
1610                sb.append(", Active phone call: "); formatTimeMs(sb, phoneOnTime / 1000);
1611                sb.append("("); sb.append(formatRatioLocked(phoneOnTime, whichBatteryRealtime));
1612                sb.append(")");
1613        pw.println(sb.toString());
1614        sb.setLength(0);
1615        sb.append(prefix);
1616        sb.append("  Screen brightnesses: ");
1617        boolean didOne = false;
1618        for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
1619            final long time = getScreenBrightnessTime(i, batteryRealtime, which);
1620            if (time == 0) {
1621                continue;
1622            }
1623            if (didOne) sb.append(", ");
1624            didOne = true;
1625            sb.append(SCREEN_BRIGHTNESS_NAMES[i]);
1626            sb.append(" ");
1627            formatTimeMs(sb, time/1000);
1628            sb.append("(");
1629            sb.append(formatRatioLocked(time, screenOnTime));
1630            sb.append(")");
1631        }
1632        if (!didOne) sb.append("No activity");
1633        pw.println(sb.toString());
1634
1635        // Calculate wakelock times across all uids.
1636        long fullWakeLockTimeTotalMicros = 0;
1637        long partialWakeLockTimeTotalMicros = 0;
1638
1639        final Comparator<TimerEntry> timerComparator = new Comparator<TimerEntry>() {
1640            @Override
1641            public int compare(TimerEntry lhs, TimerEntry rhs) {
1642                long lhsTime = lhs.mTime;
1643                long rhsTime = rhs.mTime;
1644                if (lhsTime < rhsTime) {
1645                    return 1;
1646                }
1647                if (lhsTime > rhsTime) {
1648                    return -1;
1649                }
1650                return 0;
1651            }
1652        };
1653
1654        if (reqUid < 0) {
1655            Map<String, ? extends BatteryStats.Timer> kernelWakelocks = getKernelWakelockStats();
1656            if (kernelWakelocks.size() > 0) {
1657                final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1658                for (Map.Entry<String, ? extends BatteryStats.Timer> ent : kernelWakelocks.entrySet()) {
1659                    BatteryStats.Timer timer = ent.getValue();
1660                    long totalTimeMillis = computeWakeLock(timer, batteryRealtime, which);
1661                    if (totalTimeMillis > 0) {
1662                        timers.add(new TimerEntry(ent.getKey(), 0, timer, totalTimeMillis));
1663                    }
1664                }
1665                Collections.sort(timers, timerComparator);
1666                for (int i=0; i<timers.size(); i++) {
1667                    TimerEntry timer = timers.get(i);
1668                    String linePrefix = ": ";
1669                    sb.setLength(0);
1670                    sb.append(prefix);
1671                    sb.append("  Kernel Wake lock ");
1672                    sb.append(timer.mName);
1673                    linePrefix = printWakeLock(sb, timer.mTimer, batteryRealtime, null,
1674                            which, linePrefix);
1675                    if (!linePrefix.equals(": ")) {
1676                        sb.append(" realtime");
1677                        // Only print out wake locks that were held
1678                        pw.println(sb.toString());
1679                    }
1680                }
1681            }
1682        }
1683
1684        final ArrayList<TimerEntry> timers = new ArrayList<TimerEntry>();
1685
1686        for (int iu = 0; iu < NU; iu++) {
1687            Uid u = uidStats.valueAt(iu);
1688
1689            Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
1690            if (wakelocks.size() > 0) {
1691                for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
1692                        : wakelocks.entrySet()) {
1693                    Uid.Wakelock wl = ent.getValue();
1694
1695                    Timer fullWakeTimer = wl.getWakeTime(WAKE_TYPE_FULL);
1696                    if (fullWakeTimer != null) {
1697                        fullWakeLockTimeTotalMicros += fullWakeTimer.getTotalTimeLocked(
1698                                batteryRealtime, which);
1699                    }
1700
1701                    Timer partialWakeTimer = wl.getWakeTime(WAKE_TYPE_PARTIAL);
1702                    if (partialWakeTimer != null) {
1703                        long totalTimeMicros = partialWakeTimer.getTotalTimeLocked(
1704                                batteryRealtime, which);
1705                        if (totalTimeMicros > 0) {
1706                            if (reqUid < 0) {
1707                                // Only show the ordered list of all wake
1708                                // locks if the caller is not asking for data
1709                                // about a specific uid.
1710                                timers.add(new TimerEntry(ent.getKey(), u.getUid(),
1711                                        partialWakeTimer, totalTimeMicros));
1712                            }
1713                            partialWakeLockTimeTotalMicros += totalTimeMicros;
1714                        }
1715                    }
1716                }
1717            }
1718        }
1719
1720        long mobileRxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1721        long mobileTxTotalBytes = getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1722        long wifiRxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1723        long wifiTxTotalBytes = getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1724        long mobileRxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1725        long mobileTxTotalPackets = getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1726        long wifiRxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1727        long wifiTxTotalPackets = getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1728
1729        pw.print(prefix);
1730                pw.print("  Mobile total received: "); pw.print(formatBytesLocked(mobileRxTotalBytes));
1731                pw.print(", sent: "); pw.print(formatBytesLocked(mobileTxTotalBytes));
1732                pw.print(" (packets received "); pw.print(mobileRxTotalPackets);
1733                pw.print(", sent "); pw.print(mobileTxTotalPackets); pw.println(")");
1734        pw.print(prefix);
1735                pw.print("  Wi-Fi total received: "); pw.print(formatBytesLocked(wifiRxTotalBytes));
1736                pw.print(", sent: "); pw.print(formatBytesLocked(wifiTxTotalBytes));
1737                pw.print(" (packets received "); pw.print(wifiRxTotalPackets);
1738                pw.print(", sent "); pw.print(wifiTxTotalPackets); pw.println(")");
1739        sb.setLength(0);
1740        sb.append(prefix);
1741                sb.append("  Total full wakelock time: "); formatTimeMs(sb,
1742                        (fullWakeLockTimeTotalMicros + 500) / 1000);
1743                sb.append(", Total partial wakelock time: "); formatTimeMs(sb,
1744                        (partialWakeLockTimeTotalMicros + 500) / 1000);
1745        pw.println(sb.toString());
1746
1747        sb.setLength(0);
1748        sb.append(prefix);
1749        sb.append("  Signal levels: ");
1750        didOne = false;
1751        for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
1752            final long time = getPhoneSignalStrengthTime(i, batteryRealtime, which);
1753            if (time == 0) {
1754                continue;
1755            }
1756            if (didOne) sb.append(", ");
1757            didOne = true;
1758            sb.append(SignalStrength.SIGNAL_STRENGTH_NAMES[i]);
1759            sb.append(" ");
1760            formatTimeMs(sb, time/1000);
1761            sb.append("(");
1762            sb.append(formatRatioLocked(time, whichBatteryRealtime));
1763            sb.append(") ");
1764            sb.append(getPhoneSignalStrengthCount(i, which));
1765            sb.append("x");
1766        }
1767        if (!didOne) sb.append("No activity");
1768        pw.println(sb.toString());
1769
1770        sb.setLength(0);
1771        sb.append(prefix);
1772        sb.append("  Signal scanning time: ");
1773        formatTimeMs(sb, getPhoneSignalScanningTime(batteryRealtime, which) / 1000);
1774        pw.println(sb.toString());
1775
1776        sb.setLength(0);
1777        sb.append(prefix);
1778        sb.append("  Radio types: ");
1779        didOne = false;
1780        for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
1781            final long time = getPhoneDataConnectionTime(i, batteryRealtime, which);
1782            if (time == 0) {
1783                continue;
1784            }
1785            if (didOne) sb.append(", ");
1786            didOne = true;
1787            sb.append(DATA_CONNECTION_NAMES[i]);
1788            sb.append(" ");
1789            formatTimeMs(sb, time/1000);
1790            sb.append("(");
1791            sb.append(formatRatioLocked(time, whichBatteryRealtime));
1792            sb.append(") ");
1793            sb.append(getPhoneDataConnectionCount(i, which));
1794            sb.append("x");
1795        }
1796        if (!didOne) sb.append("No activity");
1797        pw.println(sb.toString());
1798
1799        sb.setLength(0);
1800        sb.append(prefix);
1801        sb.append("  Radio data uptime when unplugged: ");
1802        sb.append(getRadioDataUptime() / 1000);
1803        sb.append(" ms");
1804        pw.println(sb.toString());
1805
1806        sb.setLength(0);
1807        sb.append(prefix);
1808                sb.append("  Wifi on: "); formatTimeMs(sb, wifiOnTime / 1000);
1809                sb.append("("); sb.append(formatRatioLocked(wifiOnTime, whichBatteryRealtime));
1810                sb.append("), Wifi running: "); formatTimeMs(sb, wifiRunningTime / 1000);
1811                sb.append("("); sb.append(formatRatioLocked(wifiRunningTime, whichBatteryRealtime));
1812                sb.append("), Bluetooth on: "); formatTimeMs(sb, bluetoothOnTime / 1000);
1813                sb.append("("); sb.append(formatRatioLocked(bluetoothOnTime, whichBatteryRealtime));
1814                sb.append(")");
1815        pw.println(sb.toString());
1816
1817        pw.println(" ");
1818
1819        if (which == STATS_SINCE_UNPLUGGED) {
1820            if (getIsOnBattery()) {
1821                pw.print(prefix); pw.println("  Device is currently unplugged");
1822                pw.print(prefix); pw.print("    Discharge cycle start level: ");
1823                        pw.println(getDischargeStartLevel());
1824                pw.print(prefix); pw.print("    Discharge cycle current level: ");
1825                        pw.println(getDischargeCurrentLevel());
1826            } else {
1827                pw.print(prefix); pw.println("  Device is currently plugged into power");
1828                pw.print(prefix); pw.print("    Last discharge cycle start level: ");
1829                        pw.println(getDischargeStartLevel());
1830                pw.print(prefix); pw.print("    Last discharge cycle end level: ");
1831                        pw.println(getDischargeCurrentLevel());
1832            }
1833            pw.print(prefix); pw.print("    Amount discharged while screen on: ");
1834                    pw.println(getDischargeAmountScreenOn());
1835            pw.print(prefix); pw.print("    Amount discharged while screen off: ");
1836                    pw.println(getDischargeAmountScreenOff());
1837            pw.println(" ");
1838        } else {
1839            pw.print(prefix); pw.println("  Device battery use since last full charge");
1840            pw.print(prefix); pw.print("    Amount discharged (lower bound): ");
1841                    pw.println(getLowDischargeAmountSinceCharge());
1842            pw.print(prefix); pw.print("    Amount discharged (upper bound): ");
1843                    pw.println(getHighDischargeAmountSinceCharge());
1844            pw.print(prefix); pw.print("    Amount discharged while screen on: ");
1845                    pw.println(getDischargeAmountScreenOnSinceCharge());
1846            pw.print(prefix); pw.print("    Amount discharged while screen off: ");
1847                    pw.println(getDischargeAmountScreenOffSinceCharge());
1848            pw.println();
1849        }
1850
1851        BatteryStatsHelper helper = new BatteryStatsHelper(context);
1852        helper.create(this);
1853        helper.refreshStats(which, UserHandle.USER_ALL);
1854        List<BatterySipper> sippers = helper.getUsageList();
1855        if (sippers != null && sippers.size() > 0) {
1856            pw.print(prefix); pw.println("  Estimated power use (mAh):");
1857            pw.print(prefix); pw.print("    Capacity: ");
1858                    printmAh(pw, helper.getPowerProfile().getBatteryCapacity());
1859                    pw.print(", Computed drain: "); printmAh(pw, helper.getComputedPower());
1860                    pw.print(", Min drain: "); printmAh(pw, helper.getMinDrainedPower());
1861                    pw.print(", Max drain: "); printmAh(pw, helper.getMaxDrainedPower());
1862                    pw.println();
1863            for (int i=0; i<sippers.size(); i++) {
1864                BatterySipper bs = sippers.get(i);
1865                switch (bs.drainType) {
1866                    case IDLE:
1867                        pw.print(prefix); pw.print("    Idle: "); printmAh(pw, bs.value);
1868                        pw.println();
1869                        break;
1870                    case CELL:
1871                        pw.print(prefix); pw.print("    Cell standby: "); printmAh(pw, bs.value);
1872                        pw.println();
1873                        break;
1874                    case PHONE:
1875                        pw.print(prefix); pw.print("    Phone calls: "); printmAh(pw, bs.value);
1876                        pw.println();
1877                        break;
1878                    case WIFI:
1879                        pw.print(prefix); pw.print("    Wifi: "); printmAh(pw, bs.value);
1880                        pw.println();
1881                        break;
1882                    case BLUETOOTH:
1883                        pw.print(prefix); pw.print("    Bluetooth: "); printmAh(pw, bs.value);
1884                        pw.println();
1885                        break;
1886                    case SCREEN:
1887                        pw.print(prefix); pw.print("    Screen: "); printmAh(pw, bs.value);
1888                        pw.println();
1889                        break;
1890                    case APP:
1891                        pw.print(prefix); pw.print("    Uid "); pw.print(bs.uidObj.getUid());
1892                        pw.print(": "); printmAh(pw, bs.value); pw.println();
1893                        break;
1894                    case USER:
1895                        pw.print(prefix); pw.print("    User "); pw.print(bs.userId);
1896                        pw.print(": "); printmAh(pw, bs.value); pw.println();
1897                        break;
1898                    case UNACCOUNTED:
1899                        pw.print(prefix); pw.print("    Unaccounted: "); printmAh(pw, bs.value);
1900                        pw.println();
1901                        break;
1902                    case OVERCOUNTED:
1903                        pw.print(prefix); pw.print("    Over-counted: "); printmAh(pw, bs.value);
1904                        pw.println();
1905                        break;
1906                }
1907            }
1908            pw.println();
1909        }
1910
1911        if (timers.size() > 0) {
1912            Collections.sort(timers, timerComparator);
1913            pw.print(prefix); pw.println("  All partial wake locks:");
1914            for (int i=0; i<timers.size(); i++) {
1915                TimerEntry timer = timers.get(i);
1916                sb.setLength(0);
1917                sb.append("  Wake lock ");
1918                UserHandle.formatUid(sb, timer.mId);
1919                sb.append(" ");
1920                sb.append(timer.mName);
1921                printWakeLock(sb, timer.mTimer, batteryRealtime, null, which, ": ");
1922                sb.append(" realtime");
1923                pw.println(sb.toString());
1924            }
1925            timers.clear();
1926            pw.println();
1927        }
1928
1929        for (int iu=0; iu<NU; iu++) {
1930            final int uid = uidStats.keyAt(iu);
1931            if (reqUid >= 0 && uid != reqUid && uid != Process.SYSTEM_UID) {
1932                continue;
1933            }
1934
1935            Uid u = uidStats.valueAt(iu);
1936
1937            pw.print(prefix);
1938            pw.print("  ");
1939            UserHandle.formatUid(pw, uid);
1940            pw.println(":");
1941            boolean uidActivity = false;
1942
1943            long mobileRxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_RX_DATA, which);
1944            long mobileTxBytes = u.getNetworkActivityBytes(NETWORK_MOBILE_TX_DATA, which);
1945            long wifiRxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_RX_DATA, which);
1946            long wifiTxBytes = u.getNetworkActivityBytes(NETWORK_WIFI_TX_DATA, which);
1947            long mobileRxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_RX_DATA, which);
1948            long mobileTxPackets = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
1949            long wifiRxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
1950            long wifiTxPackets = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
1951            long fullWifiLockOnTime = u.getFullWifiLockTime(batteryRealtime, which);
1952            long wifiScanTime = u.getWifiScanTime(batteryRealtime, which);
1953            long uidWifiRunningTime = u.getWifiRunningTime(batteryRealtime, which);
1954
1955            if (mobileRxBytes > 0 || mobileTxBytes > 0
1956                    || mobileRxPackets > 0 || mobileTxPackets > 0) {
1957                pw.print(prefix); pw.print("    Mobile network: ");
1958                        pw.print(formatBytesLocked(mobileRxBytes)); pw.print(" received, ");
1959                        pw.print(formatBytesLocked(mobileTxBytes));
1960                        pw.print(" sent (packets "); pw.print(mobileRxPackets);
1961                        pw.print(" received, "); pw.print(mobileTxPackets); pw.println(" sent)");
1962            }
1963            if (wifiRxBytes > 0 || wifiTxBytes > 0 || wifiRxPackets > 0 || wifiTxPackets > 0) {
1964                pw.print(prefix); pw.print("    Wi-Fi network: ");
1965                        pw.print(formatBytesLocked(wifiRxBytes)); pw.print(" received, ");
1966                        pw.print(formatBytesLocked(wifiTxBytes));
1967                        pw.print(" sent (packets "); pw.print(wifiRxPackets);
1968                        pw.print(" received, "); pw.print(wifiTxPackets); pw.println(" sent)");
1969            }
1970
1971            if (u.hasUserActivity()) {
1972                boolean hasData = false;
1973                for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
1974                    int val = u.getUserActivityCount(i, which);
1975                    if (val != 0) {
1976                        if (!hasData) {
1977                            sb.setLength(0);
1978                            sb.append("    User activity: ");
1979                            hasData = true;
1980                        } else {
1981                            sb.append(", ");
1982                        }
1983                        sb.append(val);
1984                        sb.append(" ");
1985                        sb.append(Uid.USER_ACTIVITY_TYPES[i]);
1986                    }
1987                }
1988                if (hasData) {
1989                    pw.println(sb.toString());
1990                }
1991            }
1992
1993            if (fullWifiLockOnTime != 0 || wifiScanTime != 0
1994                    || uidWifiRunningTime != 0) {
1995                sb.setLength(0);
1996                sb.append(prefix); sb.append("    Wifi Running: ");
1997                        formatTimeMs(sb, uidWifiRunningTime / 1000);
1998                        sb.append("("); sb.append(formatRatioLocked(uidWifiRunningTime,
1999                                whichBatteryRealtime)); sb.append(")\n");
2000                sb.append(prefix); sb.append("    Full Wifi Lock: ");
2001                        formatTimeMs(sb, fullWifiLockOnTime / 1000);
2002                        sb.append("("); sb.append(formatRatioLocked(fullWifiLockOnTime,
2003                                whichBatteryRealtime)); sb.append(")\n");
2004                sb.append(prefix); sb.append("    Wifi Scan: ");
2005                        formatTimeMs(sb, wifiScanTime / 1000);
2006                        sb.append("("); sb.append(formatRatioLocked(wifiScanTime,
2007                                whichBatteryRealtime)); sb.append(")");
2008                pw.println(sb.toString());
2009            }
2010
2011            Map<String, ? extends BatteryStats.Uid.Wakelock> wakelocks = u.getWakelockStats();
2012            if (wakelocks.size() > 0) {
2013                long totalFull = 0, totalPartial = 0, totalWindow = 0;
2014                int count = 0;
2015                for (Map.Entry<String, ? extends BatteryStats.Uid.Wakelock> ent
2016                    : wakelocks.entrySet()) {
2017                    Uid.Wakelock wl = ent.getValue();
2018                    String linePrefix = ": ";
2019                    sb.setLength(0);
2020                    sb.append(prefix);
2021                    sb.append("    Wake lock ");
2022                    sb.append(ent.getKey());
2023                    linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_FULL), batteryRealtime,
2024                            "full", which, linePrefix);
2025                    linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_PARTIAL), batteryRealtime,
2026                            "partial", which, linePrefix);
2027                    linePrefix = printWakeLock(sb, wl.getWakeTime(WAKE_TYPE_WINDOW), batteryRealtime,
2028                            "window", which, linePrefix);
2029                    if (!linePrefix.equals(": ")) {
2030                        sb.append(" realtime");
2031                        // Only print out wake locks that were held
2032                        pw.println(sb.toString());
2033                        uidActivity = true;
2034                        count++;
2035                    }
2036                    totalFull += computeWakeLock(wl.getWakeTime(WAKE_TYPE_FULL),
2037                            batteryRealtime, which);
2038                    totalPartial += computeWakeLock(wl.getWakeTime(WAKE_TYPE_PARTIAL),
2039                            batteryRealtime, which);
2040                    totalWindow += computeWakeLock(wl.getWakeTime(WAKE_TYPE_WINDOW),
2041                            batteryRealtime, which);
2042                }
2043                if (count > 1) {
2044                    if (totalFull != 0 || totalPartial != 0 || totalWindow != 0) {
2045                        sb.setLength(0);
2046                        sb.append(prefix);
2047                        sb.append("    TOTAL wake: ");
2048                        boolean needComma = false;
2049                        if (totalFull != 0) {
2050                            needComma = true;
2051                            formatTimeMs(sb, totalFull);
2052                            sb.append("full");
2053                        }
2054                        if (totalPartial != 0) {
2055                            if (needComma) {
2056                                sb.append(", ");
2057                            }
2058                            needComma = true;
2059                            formatTimeMs(sb, totalPartial);
2060                            sb.append("partial");
2061                        }
2062                        if (totalWindow != 0) {
2063                            if (needComma) {
2064                                sb.append(", ");
2065                            }
2066                            needComma = true;
2067                            formatTimeMs(sb, totalWindow);
2068                            sb.append("window");
2069                        }
2070                        sb.append(" realtime");
2071                        pw.println(sb.toString());
2072                    }
2073                }
2074            }
2075
2076            Map<Integer, ? extends BatteryStats.Uid.Sensor> sensors = u.getSensorStats();
2077            if (sensors.size() > 0) {
2078                for (Map.Entry<Integer, ? extends BatteryStats.Uid.Sensor> ent
2079                    : sensors.entrySet()) {
2080                    Uid.Sensor se = ent.getValue();
2081                    int sensorNumber = ent.getKey();
2082                    sb.setLength(0);
2083                    sb.append(prefix);
2084                    sb.append("    Sensor ");
2085                    int handle = se.getHandle();
2086                    if (handle == Uid.Sensor.GPS) {
2087                        sb.append("GPS");
2088                    } else {
2089                        sb.append(handle);
2090                    }
2091                    sb.append(": ");
2092
2093                    Timer timer = se.getSensorTime();
2094                    if (timer != null) {
2095                        // Convert from microseconds to milliseconds with rounding
2096                        long totalTime = (timer.getTotalTimeLocked(
2097                                batteryRealtime, which) + 500) / 1000;
2098                        int count = timer.getCountLocked(which);
2099                        //timer.logState();
2100                        if (totalTime != 0) {
2101                            formatTimeMs(sb, totalTime);
2102                            sb.append("realtime (");
2103                            sb.append(count);
2104                            sb.append(" times)");
2105                        } else {
2106                            sb.append("(not used)");
2107                        }
2108                    } else {
2109                        sb.append("(not used)");
2110                    }
2111
2112                    pw.println(sb.toString());
2113                    uidActivity = true;
2114                }
2115            }
2116
2117            Timer vibTimer = u.getVibratorOnTimer();
2118            if (vibTimer != null) {
2119                // Convert from microseconds to milliseconds with rounding
2120                long totalTime = (vibTimer.getTotalTimeLocked(
2121                        batteryRealtime, which) + 500) / 1000;
2122                int count = vibTimer.getCountLocked(which);
2123                //timer.logState();
2124                if (totalTime != 0) {
2125                    sb.setLength(0);
2126                    sb.append(prefix);
2127                    sb.append("    Vibrator: ");
2128                    formatTimeMs(sb, totalTime);
2129                    sb.append("realtime (");
2130                    sb.append(count);
2131                    sb.append(" times)");
2132                    pw.println(sb.toString());
2133                    uidActivity = true;
2134                }
2135            }
2136
2137            Timer fgTimer = u.getForegroundActivityTimer();
2138            if (fgTimer != null) {
2139                // Convert from microseconds to milliseconds with rounding
2140                long totalTime = (fgTimer.getTotalTimeLocked(batteryRealtime, which) + 500) / 1000;
2141                int count = fgTimer.getCountLocked(which);
2142                if (totalTime != 0) {
2143                    sb.setLength(0);
2144                    sb.append(prefix);
2145                    sb.append("    Foreground activities: ");
2146                    formatTimeMs(sb, totalTime);
2147                    sb.append("realtime (");
2148                    sb.append(count);
2149                    sb.append(" times)");
2150                    pw.println(sb.toString());
2151                    uidActivity = true;
2152                }
2153            }
2154
2155            Map<String, ? extends BatteryStats.Uid.Proc> processStats = u.getProcessStats();
2156            if (processStats.size() > 0) {
2157                for (Map.Entry<String, ? extends BatteryStats.Uid.Proc> ent
2158                    : processStats.entrySet()) {
2159                    Uid.Proc ps = ent.getValue();
2160                    long userTime;
2161                    long systemTime;
2162                    long foregroundTime;
2163                    int starts;
2164                    int numExcessive;
2165
2166                    userTime = ps.getUserTime(which);
2167                    systemTime = ps.getSystemTime(which);
2168                    foregroundTime = ps.getForegroundTime(which);
2169                    starts = ps.getStarts(which);
2170                    numExcessive = which == STATS_SINCE_CHARGED
2171                            ? ps.countExcessivePowers() : 0;
2172
2173                    if (userTime != 0 || systemTime != 0 || foregroundTime != 0 || starts != 0
2174                            || numExcessive != 0) {
2175                        sb.setLength(0);
2176                        sb.append(prefix); sb.append("    Proc ");
2177                                sb.append(ent.getKey()); sb.append(":\n");
2178                        sb.append(prefix); sb.append("      CPU: ");
2179                                formatTime(sb, userTime); sb.append("usr + ");
2180                                formatTime(sb, systemTime); sb.append("krn ; ");
2181                                formatTime(sb, foregroundTime); sb.append("fg");
2182                        if (starts != 0) {
2183                            sb.append("\n"); sb.append(prefix); sb.append("      ");
2184                                    sb.append(starts); sb.append(" proc starts");
2185                        }
2186                        pw.println(sb.toString());
2187                        for (int e=0; e<numExcessive; e++) {
2188                            Uid.Proc.ExcessivePower ew = ps.getExcessivePower(e);
2189                            if (ew != null) {
2190                                pw.print(prefix); pw.print("      * Killed for ");
2191                                        if (ew.type == Uid.Proc.ExcessivePower.TYPE_WAKE) {
2192                                            pw.print("wake lock");
2193                                        } else if (ew.type == Uid.Proc.ExcessivePower.TYPE_CPU) {
2194                                            pw.print("cpu");
2195                                        } else {
2196                                            pw.print("unknown");
2197                                        }
2198                                        pw.print(" use: ");
2199                                        TimeUtils.formatDuration(ew.usedTime, pw);
2200                                        pw.print(" over ");
2201                                        TimeUtils.formatDuration(ew.overTime, pw);
2202                                        if (ew.overTime != 0) {
2203                                            pw.print(" (");
2204                                            pw.print((ew.usedTime*100)/ew.overTime);
2205                                            pw.println("%)");
2206                                        }
2207                            }
2208                        }
2209                        uidActivity = true;
2210                    }
2211                }
2212            }
2213
2214            Map<String, ? extends BatteryStats.Uid.Pkg> packageStats = u.getPackageStats();
2215            if (packageStats.size() > 0) {
2216                for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg> ent
2217                    : packageStats.entrySet()) {
2218                    pw.print(prefix); pw.print("    Apk "); pw.print(ent.getKey()); pw.println(":");
2219                    boolean apkActivity = false;
2220                    Uid.Pkg ps = ent.getValue();
2221                    int wakeups = ps.getWakeups(which);
2222                    if (wakeups != 0) {
2223                        pw.print(prefix); pw.print("      ");
2224                                pw.print(wakeups); pw.println(" wakeup alarms");
2225                        apkActivity = true;
2226                    }
2227                    Map<String, ? extends  Uid.Pkg.Serv> serviceStats = ps.getServiceStats();
2228                    if (serviceStats.size() > 0) {
2229                        for (Map.Entry<String, ? extends BatteryStats.Uid.Pkg.Serv> sent
2230                                : serviceStats.entrySet()) {
2231                            BatteryStats.Uid.Pkg.Serv ss = sent.getValue();
2232                            long startTime = ss.getStartTime(batteryUptime, which);
2233                            int starts = ss.getStarts(which);
2234                            int launches = ss.getLaunches(which);
2235                            if (startTime != 0 || starts != 0 || launches != 0) {
2236                                sb.setLength(0);
2237                                sb.append(prefix); sb.append("      Service ");
2238                                        sb.append(sent.getKey()); sb.append(":\n");
2239                                sb.append(prefix); sb.append("        Created for: ");
2240                                        formatTimeMs(sb, startTime / 1000);
2241                                        sb.append("uptime\n");
2242                                sb.append(prefix); sb.append("        Starts: ");
2243                                        sb.append(starts);
2244                                        sb.append(", launches: "); sb.append(launches);
2245                                pw.println(sb.toString());
2246                                apkActivity = true;
2247                            }
2248                        }
2249                    }
2250                    if (!apkActivity) {
2251                        pw.print(prefix); pw.println("      (nothing executed)");
2252                    }
2253                    uidActivity = true;
2254                }
2255            }
2256            if (!uidActivity) {
2257                pw.print(prefix); pw.println("    (nothing executed)");
2258            }
2259        }
2260    }
2261
2262    static void printBitDescriptions(PrintWriter pw, int oldval, int newval,
2263            BitDescription[] descriptions, boolean longNames) {
2264        int diff = oldval ^ newval;
2265        if (diff == 0) return;
2266        for (int i=0; i<descriptions.length; i++) {
2267            BitDescription bd = descriptions[i];
2268            int mask = bd.mask;
2269            if (bd.shift > 0) {
2270                mask <<= bd.shift;
2271            }
2272            if ((diff&mask) != 0) {
2273                pw.print(longNames ? " " : ",");
2274                if (bd.shift < 0) {
2275                    pw.print((newval&mask) != 0 ? "+" : "-");
2276                    pw.print(longNames ? bd.name : bd.shortName);
2277                } else {
2278                    pw.print(longNames ? bd.name : bd.shortName);
2279                    pw.print("=");
2280                    int val = (newval&bd.mask)>>bd.shift;
2281                    if (bd.values != null && val >= 0 && val < bd.values.length) {
2282                        pw.print(longNames? bd.values[val] : bd.shortValues[val]);
2283                    } else {
2284                        pw.print(val);
2285                    }
2286                }
2287            }
2288        }
2289    }
2290
2291    public void prepareForDumpLocked() {
2292    }
2293
2294    public static class HistoryPrinter {
2295        int oldState = 0;
2296        int oldLevel = -1;
2297        int oldStatus = -1;
2298        int oldHealth = -1;
2299        int oldPlug = -1;
2300        int oldTemp = -1;
2301        int oldVolt = -1;
2302        long lastTime = -1;
2303
2304        public void printNextItem(PrintWriter pw, HistoryItem rec, long now, boolean checkin) {
2305            if (!checkin) {
2306                pw.print("  ");
2307                TimeUtils.formatDuration(rec.time-now, pw, TimeUtils.HUNDRED_DAY_FIELD_LEN);
2308                pw.print(" ");
2309            } else {
2310                if (lastTime < 0) {
2311                    pw.print("@");
2312                    pw.print(rec.time-now);
2313                } else {
2314                    pw.print(rec.time-lastTime);
2315                }
2316                lastTime = rec.time;
2317            }
2318            if (rec.cmd == HistoryItem.CMD_START) {
2319                if (checkin) {
2320                    pw.print(":");
2321                }
2322                pw.println("START");
2323            } else if (rec.cmd == HistoryItem.CMD_OVERFLOW) {
2324                if (checkin) {
2325                    pw.print(":");
2326                }
2327                pw.println("*OVERFLOW*");
2328            } else {
2329                if (!checkin) {
2330                    if (rec.batteryLevel < 10) pw.print("00");
2331                    else if (rec.batteryLevel < 100) pw.print("0");
2332                    pw.print(rec.batteryLevel);
2333                    pw.print(" ");
2334                    if (rec.states < 0x10) pw.print("0000000");
2335                    else if (rec.states < 0x100) pw.print("000000");
2336                    else if (rec.states < 0x1000) pw.print("00000");
2337                    else if (rec.states < 0x10000) pw.print("0000");
2338                    else if (rec.states < 0x100000) pw.print("000");
2339                    else if (rec.states < 0x1000000) pw.print("00");
2340                    else if (rec.states < 0x10000000) pw.print("0");
2341                    pw.print(Integer.toHexString(rec.states));
2342                } else {
2343                    if (oldLevel != rec.batteryLevel) {
2344                        oldLevel = rec.batteryLevel;
2345                        pw.print(",Bl="); pw.print(rec.batteryLevel);
2346                    }
2347                }
2348                if (oldStatus != rec.batteryStatus) {
2349                    oldStatus = rec.batteryStatus;
2350                    pw.print(checkin ? ",Bs=" : " status=");
2351                    switch (oldStatus) {
2352                        case BatteryManager.BATTERY_STATUS_UNKNOWN:
2353                            pw.print(checkin ? "?" : "unknown");
2354                            break;
2355                        case BatteryManager.BATTERY_STATUS_CHARGING:
2356                            pw.print(checkin ? "c" : "charging");
2357                            break;
2358                        case BatteryManager.BATTERY_STATUS_DISCHARGING:
2359                            pw.print(checkin ? "d" : "discharging");
2360                            break;
2361                        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
2362                            pw.print(checkin ? "n" : "not-charging");
2363                            break;
2364                        case BatteryManager.BATTERY_STATUS_FULL:
2365                            pw.print(checkin ? "f" : "full");
2366                            break;
2367                        default:
2368                            pw.print(oldStatus);
2369                            break;
2370                    }
2371                }
2372                if (oldHealth != rec.batteryHealth) {
2373                    oldHealth = rec.batteryHealth;
2374                    pw.print(checkin ? ",Bh=" : " health=");
2375                    switch (oldHealth) {
2376                        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
2377                            pw.print(checkin ? "?" : "unknown");
2378                            break;
2379                        case BatteryManager.BATTERY_HEALTH_GOOD:
2380                            pw.print(checkin ? "g" : "good");
2381                            break;
2382                        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
2383                            pw.print(checkin ? "h" : "overheat");
2384                            break;
2385                        case BatteryManager.BATTERY_HEALTH_DEAD:
2386                            pw.print(checkin ? "d" : "dead");
2387                            break;
2388                        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
2389                            pw.print(checkin ? "v" : "over-voltage");
2390                            break;
2391                        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
2392                            pw.print(checkin ? "f" : "failure");
2393                            break;
2394                        default:
2395                            pw.print(oldHealth);
2396                            break;
2397                    }
2398                }
2399                if (oldPlug != rec.batteryPlugType) {
2400                    oldPlug = rec.batteryPlugType;
2401                    pw.print(checkin ? ",Bp=" : " plug=");
2402                    switch (oldPlug) {
2403                        case 0:
2404                            pw.print(checkin ? "n" : "none");
2405                            break;
2406                        case BatteryManager.BATTERY_PLUGGED_AC:
2407                            pw.print(checkin ? "a" : "ac");
2408                            break;
2409                        case BatteryManager.BATTERY_PLUGGED_USB:
2410                            pw.print(checkin ? "u" : "usb");
2411                            break;
2412                        case BatteryManager.BATTERY_PLUGGED_WIRELESS:
2413                            pw.print(checkin ? "w" : "wireless");
2414                            break;
2415                        default:
2416                            pw.print(oldPlug);
2417                            break;
2418                    }
2419                }
2420                if (oldTemp != rec.batteryTemperature) {
2421                    oldTemp = rec.batteryTemperature;
2422                    pw.print(checkin ? ",Bt=" : " temp=");
2423                    pw.print(oldTemp);
2424                }
2425                if (oldVolt != rec.batteryVoltage) {
2426                    oldVolt = rec.batteryVoltage;
2427                    pw.print(checkin ? ",Bv=" : " volt=");
2428                    pw.print(oldVolt);
2429                }
2430                printBitDescriptions(pw, oldState, rec.states,
2431                        HISTORY_STATE_DESCRIPTIONS, !checkin);
2432                if (rec.eventCode != HistoryItem.EVENT_NONE) {
2433                    switch (rec.eventCode) {
2434                        case HistoryItem.EVENT_PROC_STARTED:
2435                            pw.print(checkin ? ",PrSt=" : " procstart=");
2436                            break;
2437                        case HistoryItem.EVENT_PROC_FINISHED:
2438                            pw.print(checkin ? ",PrFn=" : " procfin=");
2439                            break;
2440                        default:
2441                            if (checkin) {
2442                                pw.print(",?cmd_");
2443                                pw.print(rec.eventCode);
2444                                pw.print("=");
2445                            } else {
2446                                pw.print(" cmd_");
2447                                pw.print(rec.eventCode);
2448                                pw.print("=");
2449                            }
2450                            break;
2451                    }
2452                    if (checkin) {
2453                        pw.print(rec.eventUid);
2454                    } else {
2455                        UserHandle.formatUid(pw, rec.eventUid);
2456                    }
2457                    pw.print(":");
2458                    if (checkin) {
2459                        pw.print(rec.eventNameIdx);
2460                    } else {
2461                        pw.print(rec.eventName);
2462                    }
2463                }
2464                pw.println();
2465            }
2466            oldState = rec.states;
2467        }
2468    }
2469
2470    /**
2471     * Dumps a human-readable summary of the battery statistics to the given PrintWriter.
2472     *
2473     * @param pw a Printer to receive the dump output.
2474     */
2475    @SuppressWarnings("unused")
2476    public void dumpLocked(Context context, PrintWriter pw, boolean isUnpluggedOnly, int reqUid,
2477            boolean historyOnly) {
2478        prepareForDumpLocked();
2479
2480        long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2481
2482        final HistoryItem rec = new HistoryItem();
2483        if (startIteratingHistoryLocked()) {
2484            pw.println("Battery History:");
2485            HistoryPrinter hprinter = new HistoryPrinter();
2486            while (getNextHistoryLocked(rec)) {
2487                hprinter.printNextItem(pw, rec, now, false);
2488            }
2489            finishIteratingHistoryLocked();
2490            pw.println("");
2491        }
2492
2493        if (startIteratingOldHistoryLocked()) {
2494            pw.println("Old battery History:");
2495            HistoryPrinter hprinter = new HistoryPrinter();
2496            while (getNextOldHistoryLocked(rec)) {
2497                hprinter.printNextItem(pw, rec, now, false);
2498            }
2499            finishIteratingOldHistoryLocked();
2500            pw.println("");
2501        }
2502
2503        if (historyOnly) {
2504            return;
2505        }
2506
2507        SparseArray<? extends Uid> uidStats = getUidStats();
2508        final int NU = uidStats.size();
2509        boolean didPid = false;
2510        long nowRealtime = SystemClock.elapsedRealtime();
2511        for (int i=0; i<NU; i++) {
2512            Uid uid = uidStats.valueAt(i);
2513            SparseArray<? extends Uid.Pid> pids = uid.getPidStats();
2514            if (pids != null) {
2515                for (int j=0; j<pids.size(); j++) {
2516                    Uid.Pid pid = pids.valueAt(j);
2517                    if (!didPid) {
2518                        pw.println("Per-PID Stats:");
2519                        didPid = true;
2520                    }
2521                    long time = pid.mWakeSum + (pid.mWakeStart != 0
2522                            ? (nowRealtime - pid.mWakeStart) : 0);
2523                    pw.print("  PID "); pw.print(pids.keyAt(j));
2524                            pw.print(" wake time: ");
2525                            TimeUtils.formatDuration(time, pw);
2526                            pw.println("");
2527                }
2528            }
2529        }
2530        if (didPid) {
2531            pw.println("");
2532        }
2533
2534        if (!isUnpluggedOnly) {
2535            pw.println("Statistics since last charge:");
2536            pw.println("  System starts: " + getStartCount()
2537                    + ", currently on battery: " + getIsOnBattery());
2538            dumpLocked(context, pw, "", STATS_SINCE_CHARGED, reqUid);
2539            pw.println("");
2540        }
2541        pw.println("Statistics since last unplugged:");
2542        dumpLocked(context, pw, "", STATS_SINCE_UNPLUGGED, reqUid);
2543    }
2544
2545    @SuppressWarnings("unused")
2546    public void dumpCheckinLocked(Context context,
2547            PrintWriter pw, List<ApplicationInfo> apps, boolean isUnpluggedOnly,
2548            boolean includeHistory, boolean historyOnly) {
2549        prepareForDumpLocked();
2550
2551        long now = getHistoryBaseTime() + SystemClock.elapsedRealtime();
2552
2553        if (includeHistory || historyOnly) {
2554            final HistoryItem rec = new HistoryItem();
2555            if (startIteratingHistoryLocked()) {
2556                for (int i=0; i<getHistoryStringPoolSize(); i++) {
2557                    pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2558                    pw.print(HISTORY_STRING_POOL); pw.print(',');
2559                    pw.print(i);
2560                    pw.print(',');
2561                    pw.print(getHistoryStringPoolItem(i));
2562                    pw.println();
2563                }
2564                HistoryPrinter hprinter = new HistoryPrinter();
2565                while (getNextHistoryLocked(rec)) {
2566                    pw.print(BATTERY_STATS_CHECKIN_VERSION); pw.print(',');
2567                    pw.print(HISTORY_DATA); pw.print(',');
2568                    hprinter.printNextItem(pw, rec, now, true);
2569                }
2570                finishIteratingHistoryLocked();
2571            }
2572        }
2573
2574        if (historyOnly) {
2575            return;
2576        }
2577
2578        if (apps != null) {
2579            SparseArray<ArrayList<String>> uids = new SparseArray<ArrayList<String>>();
2580            for (int i=0; i<apps.size(); i++) {
2581                ApplicationInfo ai = apps.get(i);
2582                ArrayList<String> pkgs = uids.get(ai.uid);
2583                if (pkgs == null) {
2584                    pkgs = new ArrayList<String>();
2585                    uids.put(ai.uid, pkgs);
2586                }
2587                pkgs.add(ai.packageName);
2588            }
2589            SparseArray<? extends Uid> uidStats = getUidStats();
2590            final int NU = uidStats.size();
2591            String[] lineArgs = new String[2];
2592            for (int i=0; i<NU; i++) {
2593                int uid = uidStats.keyAt(i);
2594                ArrayList<String> pkgs = uids.get(uid);
2595                if (pkgs != null) {
2596                    for (int j=0; j<pkgs.size(); j++) {
2597                        lineArgs[0] = Integer.toString(uid);
2598                        lineArgs[1] = pkgs.get(j);
2599                        dumpLine(pw, 0 /* uid */, "i" /* category */, UID_DATA,
2600                                (Object[])lineArgs);
2601                    }
2602                }
2603            }
2604        }
2605        if (isUnpluggedOnly) {
2606            dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
2607        }
2608        else {
2609            dumpCheckinLocked(context, pw, STATS_SINCE_CHARGED, -1);
2610            dumpCheckinLocked(context, pw, STATS_SINCE_UNPLUGGED, -1);
2611        }
2612    }
2613}
2614