-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatteryStats.java
More file actions
5132 lines (4668 loc) · 216 KB
/
Copy pathBatteryStats.java
File metadata and controls
5132 lines (4668 loc) · 216 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.telephony.SignalStrength;
import android.text.format.DateFormat;
import android.util.ArrayMap;
import android.util.Printer;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.util.TimeUtils;
import android.view.Display;
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
/**
* A class providing access to battery usage statistics, including information on
* wakelocks, processes, packages, and services. All times are represented in microseconds
* except where indicated otherwise.
* @hide
*/
public abstract class BatteryStats implements Parcelable {
private static final boolean LOCAL_LOGV = false;
/** @hide */
public static final String SERVICE_NAME = "batterystats";
/**
* A constant indicating a partial wake lock timer.
*/
public static final int WAKE_TYPE_PARTIAL = 0;
/**
* A constant indicating a full wake lock timer.
*/
public static final int WAKE_TYPE_FULL = 1;
/**
* A constant indicating a window wake lock timer.
*/
public static final int WAKE_TYPE_WINDOW = 2;
/**
* A constant indicating a sensor timer.
*/
public static final int SENSOR = 3;
/**
* A constant indicating a a wifi running timer
*/
public static final int WIFI_RUNNING = 4;
/**
* A constant indicating a full wifi lock timer
*/
public static final int FULL_WIFI_LOCK = 5;
/**
* A constant indicating a wifi scan
*/
public static final int WIFI_SCAN = 6;
/**
* A constant indicating a wifi multicast timer
*/
public static final int WIFI_MULTICAST_ENABLED = 7;
/**
* A constant indicating a video turn on timer
*/
public static final int VIDEO_TURNED_ON = 8;
/**
* A constant indicating a vibrator on timer
*/
public static final int VIBRATOR_ON = 9;
/**
* A constant indicating a foreground activity timer
*/
public static final int FOREGROUND_ACTIVITY = 10;
/**
* A constant indicating a wifi batched scan is active
*/
public static final int WIFI_BATCHED_SCAN = 11;
/**
* A constant indicating a process state timer
*/
public static final int PROCESS_STATE = 12;
/**
* A constant indicating a sync timer
*/
public static final int SYNC = 13;
/**
* A constant indicating a job timer
*/
public static final int JOB = 14;
/**
* A constant indicating an audio turn on timer
*/
public static final int AUDIO_TURNED_ON = 15;
/**
* A constant indicating a flashlight turn on timer
*/
public static final int FLASHLIGHT_TURNED_ON = 16;
/**
* A constant indicating a camera turn on timer
*/
public static final int CAMERA_TURNED_ON = 17;
/**
* A constant indicating a draw wake lock timer.
*/
public static final int WAKE_TYPE_DRAW = 18;
/**
* Include all of the data in the stats, including previously saved data.
*/
public static final int STATS_SINCE_CHARGED = 0;
/**
* Include only the current run in the stats.
*/
public static final int STATS_CURRENT = 1;
/**
* Include only the run since the last time the device was unplugged in the stats.
*/
public static final int STATS_SINCE_UNPLUGGED = 2;
// NOTE: Update this list if you add/change any stats above.
// These characters are supposed to represent "total", "last", "current",
// and "unplugged". They were shortened for efficiency sake.
private static final String[] STAT_NAMES = { "l", "c", "u" };
/**
* Current version of checkin data format.
*/
static final String CHECKIN_VERSION = "15";
/**
* Old version, we hit 9 and ran out of room, need to remove.
*/
private static final int BATTERY_STATS_CHECKIN_VERSION = 9;
private static final long BYTES_PER_KB = 1024;
private static final long BYTES_PER_MB = 1048576; // 1024^2
private static final long BYTES_PER_GB = 1073741824; //1024^3
private static final String VERSION_DATA = "vers";
private static final String UID_DATA = "uid";
private static final String APK_DATA = "apk";
private static final String PROCESS_DATA = "pr";
private static final String CPU_DATA = "cpu";
private static final String SENSOR_DATA = "sr";
private static final String VIBRATOR_DATA = "vib";
private static final String FOREGROUND_DATA = "fg";
private static final String STATE_TIME_DATA = "st";
private static final String WAKELOCK_DATA = "wl";
private static final String SYNC_DATA = "sy";
private static final String JOB_DATA = "jb";
private static final String KERNEL_WAKELOCK_DATA = "kwl";
private static final String WAKEUP_REASON_DATA = "wr";
private static final String NETWORK_DATA = "nt";
private static final String USER_ACTIVITY_DATA = "ua";
private static final String BATTERY_DATA = "bt";
private static final String BATTERY_DISCHARGE_DATA = "dc";
private static final String BATTERY_LEVEL_DATA = "lv";
private static final String GLOBAL_WIFI_DATA = "gwfl";
private static final String WIFI_DATA = "wfl";
private static final String GLOBAL_BLUETOOTH_DATA = "gble";
private static final String MISC_DATA = "m";
private static final String GLOBAL_NETWORK_DATA = "gn";
private static final String HISTORY_STRING_POOL = "hsp";
private static final String HISTORY_DATA = "h";
private static final String SCREEN_BRIGHTNESS_DATA = "br";
private static final String SIGNAL_STRENGTH_TIME_DATA = "sgt";
private static final String SIGNAL_SCANNING_TIME_DATA = "sst";
private static final String SIGNAL_STRENGTH_COUNT_DATA = "sgc";
private static final String DATA_CONNECTION_TIME_DATA = "dct";
private static final String DATA_CONNECTION_COUNT_DATA = "dcc";
private static final String WIFI_STATE_TIME_DATA = "wst";
private static final String WIFI_STATE_COUNT_DATA = "wsc";
private static final String WIFI_SUPPL_STATE_TIME_DATA = "wsst";
private static final String WIFI_SUPPL_STATE_COUNT_DATA = "wssc";
private static final String WIFI_SIGNAL_STRENGTH_TIME_DATA = "wsgt";
private static final String WIFI_SIGNAL_STRENGTH_COUNT_DATA = "wsgc";
private static final String POWER_USE_SUMMARY_DATA = "pws";
private static final String POWER_USE_ITEM_DATA = "pwi";
private static final String DISCHARGE_STEP_DATA = "dsd";
private static final String CHARGE_STEP_DATA = "csd";
private static final String DISCHARGE_TIME_REMAIN_DATA = "dtr";
private static final String CHARGE_TIME_REMAIN_DATA = "ctr";
private static final String FLASHLIGHT_DATA = "fla";
private static final String CAMERA_DATA = "cam";
private static final String VIDEO_DATA = "vid";
private static final String AUDIO_DATA = "aud";
private final StringBuilder mFormatBuilder = new StringBuilder(32);
private final Formatter mFormatter = new Formatter(mFormatBuilder);
/**
* State for keeping track of counting information.
*/
public static abstract class Counter {
/**
* Returns the count associated with this Counter for the
* selected type of statistics.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
*/
public abstract int getCountLocked(int which);
/**
* Temporary for debugging.
*/
public abstract void logState(Printer pw, String prefix);
}
/**
* State for keeping track of long counting information.
*/
public static abstract class LongCounter {
/**
* Returns the count associated with this Counter for the
* selected type of statistics.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
*/
public abstract long getCountLocked(int which);
/**
* Temporary for debugging.
*/
public abstract void logState(Printer pw, String prefix);
}
/**
* State for keeping track of timing information.
*/
public static abstract class Timer {
/**
* Returns the count associated with this Timer for the
* selected type of statistics.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
*/
public abstract int getCountLocked(int which);
/**
* Returns the total time in microseconds associated with this Timer for the
* selected type of statistics.
*
* @param elapsedRealtimeUs current elapsed realtime of system in microseconds
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT
* @return a time in microseconds
*/
public abstract long getTotalTimeLocked(long elapsedRealtimeUs, int which);
/**
* Returns the total time in microseconds associated with this Timer since the
* 'mark' was last set.
*
* @param elapsedRealtimeUs current elapsed realtime of system in microseconds
* @return a time in microseconds
*/
public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
/**
* Temporary for debugging.
*/
public abstract void logState(Printer pw, String prefix);
}
/**
* The statistics associated with a particular uid.
*/
public static abstract class Uid {
/**
* Returns a mapping containing wakelock statistics.
*
* @return a Map from Strings to Uid.Wakelock objects.
*/
public abstract ArrayMap<String, ? extends Wakelock> getWakelockStats();
/**
* Returns a mapping containing sync statistics.
*
* @return a Map from Strings to Timer objects.
*/
public abstract ArrayMap<String, ? extends Timer> getSyncStats();
/**
* Returns a mapping containing scheduled job statistics.
*
* @return a Map from Strings to Timer objects.
*/
public abstract ArrayMap<String, ? extends Timer> getJobStats();
/**
* The statistics associated with a particular wake lock.
*/
public static abstract class Wakelock {
public abstract Timer getWakeTime(int type);
}
/**
* Returns a mapping containing sensor statistics.
*
* @return a Map from Integer sensor ids to Uid.Sensor objects.
*/
public abstract SparseArray<? extends Sensor> getSensorStats();
/**
* Returns a mapping containing active process data.
*/
public abstract SparseArray<? extends Pid> getPidStats();
/**
* Returns a mapping containing process statistics.
*
* @return a Map from Strings to Uid.Proc objects.
*/
public abstract ArrayMap<String, ? extends Proc> getProcessStats();
/**
* Returns a mapping containing package statistics.
*
* @return a Map from Strings to Uid.Pkg objects.
*/
public abstract ArrayMap<String, ? extends Pkg> getPackageStats();
/**
* Returns the time in milliseconds that this app kept the WiFi controller in the
* specified state <code>type</code>.
* @param type one of {@link #CONTROLLER_IDLE_TIME}, {@link #CONTROLLER_RX_TIME}, or
* {@link #CONTROLLER_TX_TIME}.
* @param which one of {@link #STATS_CURRENT}, {@link #STATS_SINCE_CHARGED}, or
* {@link #STATS_SINCE_UNPLUGGED}.
*/
public abstract long getWifiControllerActivity(int type, int which);
/**
* {@hide}
*/
public abstract int getUid();
public abstract void noteWifiRunningLocked(long elapsedRealtime);
public abstract void noteWifiStoppedLocked(long elapsedRealtime);
public abstract void noteFullWifiLockAcquiredLocked(long elapsedRealtime);
public abstract void noteFullWifiLockReleasedLocked(long elapsedRealtime);
public abstract void noteWifiScanStartedLocked(long elapsedRealtime);
public abstract void noteWifiScanStoppedLocked(long elapsedRealtime);
public abstract void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtime);
public abstract void noteWifiBatchedScanStoppedLocked(long elapsedRealtime);
public abstract void noteWifiMulticastEnabledLocked(long elapsedRealtime);
public abstract void noteWifiMulticastDisabledLocked(long elapsedRealtime);
public abstract void noteActivityResumedLocked(long elapsedRealtime);
public abstract void noteActivityPausedLocked(long elapsedRealtime);
public abstract long getWifiRunningTime(long elapsedRealtimeUs, int which);
public abstract long getFullWifiLockTime(long elapsedRealtimeUs, int which);
public abstract long getWifiScanTime(long elapsedRealtimeUs, int which);
public abstract int getWifiScanCount(int which);
public abstract long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which);
public abstract int getWifiBatchedScanCount(int csphBin, int which);
public abstract long getWifiMulticastTime(long elapsedRealtimeUs, int which);
public abstract Timer getAudioTurnedOnTimer();
public abstract Timer getVideoTurnedOnTimer();
public abstract Timer getFlashlightTurnedOnTimer();
public abstract Timer getCameraTurnedOnTimer();
public abstract Timer getForegroundActivityTimer();
// Time this uid has any processes in foreground state.
public static final int PROCESS_STATE_FOREGROUND = 0;
// Time this uid has any process in active state (not cached).
public static final int PROCESS_STATE_ACTIVE = 1;
// Time this uid has any processes running at all.
public static final int PROCESS_STATE_RUNNING = 2;
// Total number of process states we track.
public static final int NUM_PROCESS_STATE = 3;
static final String[] PROCESS_STATE_NAMES = {
"Foreground", "Active", "Running"
};
public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
public abstract Timer getVibratorOnTimer();
public static final int NUM_WIFI_BATCHED_SCAN_BINS = 5;
/**
* Note that these must match the constants in android.os.PowerManager.
* Also, if the user activity types change, the BatteryStatsImpl.VERSION must
* also be bumped.
*/
static final String[] USER_ACTIVITY_TYPES = {
"other", "button", "touch"
};
public static final int NUM_USER_ACTIVITY_TYPES = 3;
public abstract void noteUserActivityLocked(int type);
public abstract boolean hasUserActivity();
public abstract int getUserActivityCount(int type, int which);
public abstract boolean hasNetworkActivity();
public abstract long getNetworkActivityBytes(int type, int which);
public abstract long getNetworkActivityPackets(int type, int which);
public abstract long getMobileRadioActiveTime(int which);
public abstract int getMobileRadioActiveCount(int which);
/**
* Get the total cpu time (in microseconds) this UID had processes executing in userspace.
*/
public abstract long getUserCpuTimeUs(int which);
/**
* Get the total cpu time (in microseconds) this UID had processes executing kernel syscalls.
*/
public abstract long getSystemCpuTimeUs(int which);
/**
* Get the total cpu power consumed (in milli-ampere-microseconds).
*/
public abstract long getCpuPowerMaUs(int which);
/**
* Returns the approximate cpu time (in milliseconds) spent at a certain CPU speed for a
* given CPU cluster.
* @param cluster the index of the CPU cluster.
* @param step the index of the CPU speed. This is not the actual speed of the CPU.
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
* @see PowerProfile.getNumCpuClusters()
* @see PowerProfile.getNumSpeedStepsInCpuCluster(int)
*/
public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
public static abstract class Sensor {
/*
* FIXME: it's not correct to use this magic value because it
* could clash with a sensor handle (which are defined by
* the sensor HAL, and therefore out of our control
*/
// Magic sensor number for the GPS.
public static final int GPS = -10000;
public abstract int getHandle();
public abstract Timer getSensorTime();
}
public class Pid {
public int mWakeNesting;
public long mWakeSumMs;
public long mWakeStartMs;
}
/**
* The statistics associated with a particular process.
*/
public static abstract class Proc {
public static class ExcessivePower {
public static final int TYPE_WAKE = 1;
public static final int TYPE_CPU = 2;
public int type;
public long overTime;
public long usedTime;
}
/**
* Returns true if this process is still active in the battery stats.
*/
public abstract boolean isActive();
/**
* Returns the total time (in milliseconds) spent executing in user code.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
*/
public abstract long getUserTime(int which);
/**
* Returns the total time (in milliseconds) spent executing in system code.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
*/
public abstract long getSystemTime(int which);
/**
* Returns the number of times the process has been started.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
*/
public abstract int getStarts(int which);
/**
* Returns the number of times the process has crashed.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
*/
public abstract int getNumCrashes(int which);
/**
* Returns the number of times the process has ANRed.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
*/
public abstract int getNumAnrs(int which);
/**
* Returns the cpu time (milliseconds) spent while the process was in the foreground.
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
* @return foreground cpu time in microseconds
*/
public abstract long getForegroundTime(int which);
public abstract int countExcessivePowers();
public abstract ExcessivePower getExcessivePower(int i);
}
/**
* The statistics associated with a particular package.
*/
public static abstract class Pkg {
/**
* Returns information about all wakeup alarms that have been triggered for this
* package. The mapping keys are tag names for the alarms, the counter contains
* the number of times the alarm was triggered while on battery.
*/
public abstract ArrayMap<String, ? extends Counter> getWakeupAlarmStats();
/**
* Returns a mapping containing service statistics.
*/
public abstract ArrayMap<String, ? extends Serv> getServiceStats();
/**
* The statistics associated with a particular service.
*/
public abstract class Serv {
/**
* Returns the amount of time spent started.
*
* @param batteryUptime elapsed uptime on battery in microseconds.
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
* @return
*/
public abstract long getStartTime(long batteryUptime, int which);
/**
* Returns the total number of times startService() has been called.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
*/
public abstract int getStarts(int which);
/**
* Returns the total number times the service has been launched.
*
* @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
*/
public abstract int getLaunches(int which);
}
}
}
public static final class LevelStepTracker {
public long mLastStepTime = -1;
public int mNumStepDurations;
public final long[] mStepDurations;
public LevelStepTracker(int maxLevelSteps) {
mStepDurations = new long[maxLevelSteps];
}
public LevelStepTracker(int numSteps, long[] steps) {
mNumStepDurations = numSteps;
mStepDurations = new long[numSteps];
System.arraycopy(steps, 0, mStepDurations, 0, numSteps);
}
public long getDurationAt(int index) {
return mStepDurations[index] & STEP_LEVEL_TIME_MASK;
}
public int getLevelAt(int index) {
return (int)((mStepDurations[index] & STEP_LEVEL_LEVEL_MASK)
>> STEP_LEVEL_LEVEL_SHIFT);
}
public int getInitModeAt(int index) {
return (int)((mStepDurations[index] & STEP_LEVEL_INITIAL_MODE_MASK)
>> STEP_LEVEL_INITIAL_MODE_SHIFT);
}
public int getModModeAt(int index) {
return (int)((mStepDurations[index] & STEP_LEVEL_MODIFIED_MODE_MASK)
>> STEP_LEVEL_MODIFIED_MODE_SHIFT);
}
private void appendHex(long val, int topOffset, StringBuilder out) {
boolean hasData = false;
while (topOffset >= 0) {
int digit = (int)( (val>>topOffset) & 0xf );
topOffset -= 4;
if (!hasData && digit == 0) {
continue;
}
hasData = true;
if (digit >= 0 && digit <= 9) {
out.append((char)('0' + digit));
} else {
out.append((char)('a' + digit - 10));
}
}
}
public void encodeEntryAt(int index, StringBuilder out) {
long item = mStepDurations[index];
long duration = item & STEP_LEVEL_TIME_MASK;
int level = (int)((item & STEP_LEVEL_LEVEL_MASK)
>> STEP_LEVEL_LEVEL_SHIFT);
int initMode = (int)((item & STEP_LEVEL_INITIAL_MODE_MASK)
>> STEP_LEVEL_INITIAL_MODE_SHIFT);
int modMode = (int)((item & STEP_LEVEL_MODIFIED_MODE_MASK)
>> STEP_LEVEL_MODIFIED_MODE_SHIFT);
switch ((initMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
case Display.STATE_OFF: out.append('f'); break;
case Display.STATE_ON: out.append('o'); break;
case Display.STATE_DOZE: out.append('d'); break;
case Display.STATE_DOZE_SUSPEND: out.append('z'); break;
}
if ((initMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
out.append('p');
}
if ((initMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
out.append('i');
}
switch ((modMode&STEP_LEVEL_MODE_SCREEN_STATE) + 1) {
case Display.STATE_OFF: out.append('F'); break;
case Display.STATE_ON: out.append('O'); break;
case Display.STATE_DOZE: out.append('D'); break;
case Display.STATE_DOZE_SUSPEND: out.append('Z'); break;
}
if ((modMode&STEP_LEVEL_MODE_POWER_SAVE) != 0) {
out.append('P');
}
if ((modMode&STEP_LEVEL_MODE_DEVICE_IDLE) != 0) {
out.append('I');
}
out.append('-');
appendHex(level, 4, out);
out.append('-');
appendHex(duration, STEP_LEVEL_LEVEL_SHIFT-4, out);
}
public void decodeEntryAt(int index, String value) {
final int N = value.length();
int i = 0;
char c;
long out = 0;
while (i < N && (c=value.charAt(i)) != '-') {
i++;
switch (c) {
case 'f': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
break;
case 'o': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
break;
case 'd': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_INITIAL_MODE_SHIFT);
break;
case 'z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
<< STEP_LEVEL_INITIAL_MODE_SHIFT);
break;
case 'p': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
<< STEP_LEVEL_INITIAL_MODE_SHIFT);
break;
case 'i': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
<< STEP_LEVEL_INITIAL_MODE_SHIFT);
break;
case 'F': out |= (((long)Display.STATE_OFF-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
break;
case 'O': out |= (((long)Display.STATE_ON-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
break;
case 'D': out |= (((long)Display.STATE_DOZE-1)<<STEP_LEVEL_MODIFIED_MODE_SHIFT);
break;
case 'Z': out |= (((long)Display.STATE_DOZE_SUSPEND-1)
<< STEP_LEVEL_MODIFIED_MODE_SHIFT);
break;
case 'P': out |= (((long)STEP_LEVEL_MODE_POWER_SAVE)
<< STEP_LEVEL_MODIFIED_MODE_SHIFT);
break;
case 'I': out |= (((long)STEP_LEVEL_MODE_DEVICE_IDLE)
<< STEP_LEVEL_MODIFIED_MODE_SHIFT);
break;
}
}
i++;
long level = 0;
while (i < N && (c=value.charAt(i)) != '-') {
i++;
level <<= 4;
if (c >= '0' && c <= '9') {
level += c - '0';
} else if (c >= 'a' && c <= 'f') {
level += c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
level += c - 'A' + 10;
}
}
i++;
out |= (level << STEP_LEVEL_LEVEL_SHIFT) & STEP_LEVEL_LEVEL_MASK;
long duration = 0;
while (i < N && (c=value.charAt(i)) != '-') {
i++;
duration <<= 4;
if (c >= '0' && c <= '9') {
duration += c - '0';
} else if (c >= 'a' && c <= 'f') {
duration += c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
duration += c - 'A' + 10;
}
}
mStepDurations[index] = out | (duration & STEP_LEVEL_TIME_MASK);
}
public void init() {
mLastStepTime = -1;
mNumStepDurations = 0;
}
public void clearTime() {
mLastStepTime = -1;
}
public long computeTimePerLevel() {
final long[] steps = mStepDurations;
final int numSteps = mNumStepDurations;
// For now we'll do a simple average across all steps.
if (numSteps <= 0) {
return -1;
}
long total = 0;
for (int i=0; i<numSteps; i++) {
total += steps[i] & STEP_LEVEL_TIME_MASK;
}
return total / numSteps;
/*
long[] buckets = new long[numSteps];
int numBuckets = 0;
int numToAverage = 4;
int i = 0;
while (i < numSteps) {
long totalTime = 0;
int num = 0;
for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
num++;
}
buckets[numBuckets] = totalTime / num;
numBuckets++;
numToAverage *= 2;
i += num;
}
if (numBuckets < 1) {
return -1;
}
long averageTime = buckets[numBuckets-1];
for (i=numBuckets-2; i>=0; i--) {
averageTime = (averageTime + buckets[i]) / 2;
}
return averageTime;
*/
}
public long computeTimeEstimate(long modesOfInterest, long modeValues,
int[] outNumOfInterest) {
final long[] steps = mStepDurations;
final int count = mNumStepDurations;
if (count <= 0) {
return -1;
}
long total = 0;
int numOfInterest = 0;
for (int i=0; i<count; i++) {
long initMode = (steps[i] & STEP_LEVEL_INITIAL_MODE_MASK)
>> STEP_LEVEL_INITIAL_MODE_SHIFT;
long modMode = (steps[i] & STEP_LEVEL_MODIFIED_MODE_MASK)
>> STEP_LEVEL_MODIFIED_MODE_SHIFT;
// If the modes of interest didn't change during this step period...
if ((modMode&modesOfInterest) == 0) {
// And the mode values during this period match those we are measuring...
if ((initMode&modesOfInterest) == modeValues) {
// Then this can be used to estimate the total time!
numOfInterest++;
total += steps[i] & STEP_LEVEL_TIME_MASK;
}
}
}
if (numOfInterest <= 0) {
return -1;
}
if (outNumOfInterest != null) {
outNumOfInterest[0] = numOfInterest;
}
// The estimated time is the average time we spend in each level, multipled
// by 100 -- the total number of battery levels
return (total / numOfInterest) * 100;
}
public void addLevelSteps(int numStepLevels, long modeBits, long elapsedRealtime) {
int stepCount = mNumStepDurations;
final long lastStepTime = mLastStepTime;
if (lastStepTime >= 0 && numStepLevels > 0) {
final long[] steps = mStepDurations;
long duration = elapsedRealtime - lastStepTime;
for (int i=0; i<numStepLevels; i++) {
System.arraycopy(steps, 0, steps, 1, steps.length-1);
long thisDuration = duration / (numStepLevels-i);
duration -= thisDuration;
if (thisDuration > STEP_LEVEL_TIME_MASK) {
thisDuration = STEP_LEVEL_TIME_MASK;
}
steps[0] = thisDuration | modeBits;
}
stepCount += numStepLevels;
if (stepCount > steps.length) {
stepCount = steps.length;
}
}
mNumStepDurations = stepCount;
mLastStepTime = elapsedRealtime;
}
public void readFromParcel(Parcel in) {
final int N = in.readInt();
if (N > mStepDurations.length) {
throw new ParcelFormatException("more step durations than available: " + N);
}
mNumStepDurations = N;
for (int i=0; i<N; i++) {
mStepDurations[i] = in.readLong();
}
}
public void writeToParcel(Parcel out) {
final int N = mNumStepDurations;
out.writeInt(N);
for (int i=0; i<N; i++) {
out.writeLong(mStepDurations[i]);
}
}
}
public static final class PackageChange {
public String mPackageName;
public boolean mUpdate;
public int mVersionCode;
}
public static final class DailyItem {
public long mStartTime;
public long mEndTime;
public LevelStepTracker mDischargeSteps;
public LevelStepTracker mChargeSteps;
public ArrayList<PackageChange> mPackageChanges;
}
public abstract DailyItem getDailyItemLocked(int daysAgo);
public abstract long getCurrentDailyStartTime();
public abstract long getNextMinDailyDeadline();
public abstract long getNextMaxDailyDeadline();
public final static class HistoryTag {
public String string;
public int uid;
public int poolIdx;
public void setTo(HistoryTag o) {
string = o.string;
uid = o.uid;
poolIdx = o.poolIdx;
}
public void setTo(String _string, int _uid) {
string = _string;
uid = _uid;
poolIdx = -1;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(string);
dest.writeInt(uid);
}
public void readFromParcel(Parcel src) {
string = src.readString();
uid = src.readInt();
poolIdx = -1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HistoryTag that = (HistoryTag) o;
if (uid != that.uid) return false;
if (!string.equals(that.string)) return false;
return true;
}
@Override
public int hashCode() {
int result = string.hashCode();
result = 31 * result + uid;
return result;
}
}
/**
* Optional detailed information that can go into a history step. This is typically
* generated each time the battery level changes.
*/
public final static class HistoryStepDetails {
// Time (in 1/100 second) spent in user space and the kernel since the last step.
public int userTime;
public int systemTime;
// Top three apps using CPU in the last step, with times in 1/100 second.
public int appCpuUid1;
public int appCpuUTime1;
public int appCpuSTime1;
public int appCpuUid2;
public int appCpuUTime2;
public int appCpuSTime2;
public int appCpuUid3;
public int appCpuUTime3;
public int appCpuSTime3;
// Information from /proc/stat
public int statUserTime;
public int statSystemTime;
public int statIOWaitTime;
public int statIrqTime;
public int statSoftIrqTime;
public int statIdlTime;