Add an API for apps to get their System Health / Battery Stats data.
Change-Id: Ib27e1be469b826f6d83d73ce6024102bbdb1d47a
diff --git a/api/current.txt b/api/current.txt
index 5ac2e77..498a5b5 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -8142,6 +8142,7 @@
field public static final java.lang.String SENSOR_SERVICE = "sensor";
field public static final java.lang.String SHORTCUT_SERVICE = "shortcut";
field public static final java.lang.String STORAGE_SERVICE = "storage";
+ field public static final java.lang.String SYSTEM_HEALTH_SERVICE = "systemhealth";
field public static final java.lang.String TELECOM_SERVICE = "telecom";
field public static final java.lang.String TELEPHONY_SERVICE = "phone";
field public static final java.lang.String TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service";
@@ -29361,6 +29362,147 @@
}
+package android.os.health {
+
+ public class HealthStats {
+ method public java.lang.String getDataType();
+ method public long getMeasurement(int);
+ method public int getMeasurementKeyAt(int);
+ method public int getMeasurementKeyCount();
+ method public java.util.Map<java.lang.String, java.lang.Long> getMeasurements(int);
+ method public int getMeasurementsKeyAt(int);
+ method public int getMeasurementsKeyCount();
+ method public java.util.Map<java.lang.String, android.os.health.HealthStats> getStats(int);
+ method public int getStatsKeyAt(int);
+ method public int getStatsKeyCount();
+ method public android.os.health.TimerStat getTimer(int);
+ method public int getTimerCount(int);
+ method public int getTimerKeyAt(int);
+ method public int getTimerKeyCount();
+ method public long getTimerTime(int);
+ method public java.util.Map<java.lang.String, android.os.health.TimerStat> getTimers(int);
+ method public int getTimersKeyAt(int);
+ method public int getTimersKeyCount();
+ method public boolean hasMeasurement(int);
+ method public boolean hasMeasurements(int);
+ method public boolean hasStats(int);
+ method public boolean hasTimer(int);
+ method public boolean hasTimers(int);
+ }
+
+ public final class PackageHealthStats {
+ field public static final int MEASUREMENTS_WAKEUP_ALARMS_COUNT = 40002; // 0x9c42
+ field public static final int STATS_SERVICES = 40001; // 0x9c41
+ }
+
+ public final class PidHealthStats {
+ field public static final int MEASUREMENT_WAKE_NESTING_COUNT = 20001; // 0x4e21
+ field public static final int MEASUREMENT_WAKE_START_MS = 20003; // 0x4e23
+ field public static final int MEASUREMENT_WAKE_SUM_MS = 20002; // 0x4e22
+ }
+
+ public final class ProcessHealthStats {
+ field public static final int MEASUREMENT_ANR_COUNT = 30005; // 0x7535
+ field public static final int MEASUREMENT_CRASHES_COUNT = 30004; // 0x7534
+ field public static final int MEASUREMENT_FOREGROUND_MS = 30006; // 0x7536
+ field public static final int MEASUREMENT_STARTS_COUNT = 30003; // 0x7533
+ field public static final int MEASUREMENT_SYSTEM_TIME_MS = 30002; // 0x7532
+ field public static final int MEASUREMENT_USER_TIME_MS = 30001; // 0x7531
+ }
+
+ public final class ServiceHealthStats {
+ field public static final int MEASUREMENT_LAUNCH_COUNT = 50002; // 0xc352
+ field public static final int MEASUREMENT_START_SERVICE_COUNT = 50001; // 0xc351
+ }
+
+ public class SystemHealthManager {
+ method public static android.os.health.SystemHealthManager from(android.content.Context);
+ method public android.os.health.HealthStats takeMyUidSnapshot();
+ method public android.os.health.HealthStats takeUidSnapshot(int);
+ method public android.os.health.HealthStats[] takeUidSnapshots(int[]);
+ }
+
+ public class TimerStat implements android.os.Parcelable {
+ ctor public TimerStat();
+ ctor public TimerStat(int, long);
+ ctor public TimerStat(android.os.Parcel);
+ method public int describeContents();
+ method public int getCount();
+ method public long getTime();
+ method public void setCount(int);
+ method public void setTime(long);
+ method public void writeToParcel(android.os.Parcel, int);
+ field public static final android.os.Parcelable.Creator<android.os.health.TimerStat> CREATOR;
+ }
+
+ public final class UidHealthStats {
+ field public static final int MEASUREMENT_BLUETOOTH_IDLE_MS = 10020; // 0x2724
+ field public static final int MEASUREMENT_BLUETOOTH_POWER_MAMS = 10023; // 0x2727
+ field public static final int MEASUREMENT_BLUETOOTH_RX_BYTES = 10052; // 0x2744
+ field public static final int MEASUREMENT_BLUETOOTH_RX_MS = 10021; // 0x2725
+ field public static final int MEASUREMENT_BLUETOOTH_RX_PACKETS = 10058; // 0x274a
+ field public static final int MEASUREMENT_BLUETOOTH_TX_BYTES = 10053; // 0x2745
+ field public static final int MEASUREMENT_BLUETOOTH_TX_MS = 10022; // 0x2726
+ field public static final int MEASUREMENT_BLUETOOTH_TX_PACKETS = 10059; // 0x274b
+ field public static final int MEASUREMENT_BUTTON_USER_ACTIVITY_COUNT = 10046; // 0x273e
+ field public static final int MEASUREMENT_CPU_POWER_MAUS = 10064; // 0x2750
+ field public static final int MEASUREMENT_MOBILE_IDLE_MS = 10024; // 0x2728
+ field public static final int MEASUREMENT_MOBILE_POWER_MAMS = 10027; // 0x272b
+ field public static final int MEASUREMENT_MOBILE_RX_BYTES = 10048; // 0x2740
+ field public static final int MEASUREMENT_MOBILE_RX_MS = 10025; // 0x2729
+ field public static final int MEASUREMENT_MOBILE_RX_PACKETS = 10054; // 0x2746
+ field public static final int MEASUREMENT_MOBILE_TX_BYTES = 10049; // 0x2741
+ field public static final int MEASUREMENT_MOBILE_TX_MS = 10026; // 0x272a
+ field public static final int MEASUREMENT_MOBILE_TX_PACKETS = 10055; // 0x2747
+ field public static final int MEASUREMENT_OTHER_USER_ACTIVITY_COUNT = 10045; // 0x273d
+ field public static final int MEASUREMENT_REALTIME_BATTERY_MS = 10001; // 0x2711
+ field public static final int MEASUREMENT_REALTIME_SCREEN_OFF_BATTERY_MS = 10003; // 0x2713
+ field public static final int MEASUREMENT_SYSTEM_CPU_TIME_US = 10063; // 0x274f
+ field public static final int MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT = 10047; // 0x273f
+ field public static final int MEASUREMENT_UPTIME_BATTERY_MS = 10002; // 0x2712
+ field public static final int MEASUREMENT_UPTIME_SCREEN_OFF_BATTERY_MS = 10004; // 0x2714
+ field public static final int MEASUREMENT_USER_CPU_TIME_US = 10062; // 0x274e
+ field public static final int MEASUREMENT_WIFI_FULL_LOCK_MS = 10029; // 0x272d
+ field public static final int MEASUREMENT_WIFI_IDLE_MS = 10016; // 0x2720
+ field public static final int MEASUREMENT_WIFI_MULTICAST_MS = 10031; // 0x272f
+ field public static final int MEASUREMENT_WIFI_POWER_MAMS = 10019; // 0x2723
+ field public static final int MEASUREMENT_WIFI_RUNNING_MS = 10028; // 0x272c
+ field public static final int MEASUREMENT_WIFI_RX_BYTES = 10050; // 0x2742
+ field public static final int MEASUREMENT_WIFI_RX_MS = 10017; // 0x2721
+ field public static final int MEASUREMENT_WIFI_RX_PACKETS = 10056; // 0x2748
+ field public static final int MEASUREMENT_WIFI_TX_BYTES = 10051; // 0x2743
+ field public static final int MEASUREMENT_WIFI_TX_MS = 10018; // 0x2722
+ field public static final int MEASUREMENT_WIFI_TX_PACKETS = 10057; // 0x2749
+ field public static final int STATS_PACKAGES = 10015; // 0x271f
+ field public static final int STATS_PIDS = 10013; // 0x271d
+ field public static final int STATS_PROCESSES = 10014; // 0x271e
+ field public static final int TIMERS_JOBS = 10010; // 0x271a
+ field public static final int TIMERS_SENSORS = 10012; // 0x271c
+ field public static final int TIMERS_SYNCS = 10009; // 0x2719
+ field public static final int TIMERS_WAKELOCKS_DRAW = 10008; // 0x2718
+ field public static final int TIMERS_WAKELOCKS_FULL = 10005; // 0x2715
+ field public static final int TIMERS_WAKELOCKS_PARTIAL = 10006; // 0x2716
+ field public static final int TIMERS_WAKELOCKS_WINDOW = 10007; // 0x2717
+ field public static final int TIMER_AUDIO = 10032; // 0x2730
+ field public static final int TIMER_BLUETOOTH_SCAN = 10037; // 0x2735
+ field public static final int TIMER_CAMERA = 10035; // 0x2733
+ field public static final int TIMER_FLASHLIGHT = 10034; // 0x2732
+ field public static final int TIMER_FOREGROUND_ACTIVITY = 10036; // 0x2734
+ field public static final int TIMER_GPS_SENSOR = 10011; // 0x271b
+ field public static final int TIMER_MOBILE_RADIO_ACTIVE = 10061; // 0x274d
+ field public static final int TIMER_PROCESS_STATE_BACKGROUND_MS = 10042; // 0x273a
+ field public static final int TIMER_PROCESS_STATE_CACHED_MS = 10043; // 0x273b
+ field public static final int TIMER_PROCESS_STATE_FOREGROUND_MS = 10041; // 0x2739
+ field public static final int TIMER_PROCESS_STATE_FOREGROUND_SERVICE_MS = 10039; // 0x2737
+ field public static final int TIMER_PROCESS_STATE_TOP_MS = 10038; // 0x2736
+ field public static final int TIMER_PROCESS_STATE_TOP_SLEEPING_MS = 10040; // 0x2738
+ field public static final int TIMER_VIBRATOR = 10044; // 0x273c
+ field public static final int TIMER_VIDEO = 10033; // 0x2731
+ field public static final int TIMER_WIFI_SCAN = 10030; // 0x272e
+ }
+
+}
+
package android.os.storage {
public abstract class OnObbStateChangeListener {
diff --git a/api/system-current.txt b/api/system-current.txt
index 568f607..84dd51d 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -8448,6 +8448,7 @@
field public static final java.lang.String SENSOR_SERVICE = "sensor";
field public static final java.lang.String SHORTCUT_SERVICE = "shortcut";
field public static final java.lang.String STORAGE_SERVICE = "storage";
+ field public static final java.lang.String SYSTEM_HEALTH_SERVICE = "systemhealth";
field public static final java.lang.String TELECOM_SERVICE = "telecom";
field public static final java.lang.String TELEPHONY_SERVICE = "phone";
field public static final java.lang.String TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service";
@@ -31710,6 +31711,147 @@
}
+package android.os.health {
+
+ public class HealthStats {
+ method public java.lang.String getDataType();
+ method public long getMeasurement(int);
+ method public int getMeasurementKeyAt(int);
+ method public int getMeasurementKeyCount();
+ method public java.util.Map<java.lang.String, java.lang.Long> getMeasurements(int);
+ method public int getMeasurementsKeyAt(int);
+ method public int getMeasurementsKeyCount();
+ method public java.util.Map<java.lang.String, android.os.health.HealthStats> getStats(int);
+ method public int getStatsKeyAt(int);
+ method public int getStatsKeyCount();
+ method public android.os.health.TimerStat getTimer(int);
+ method public int getTimerCount(int);
+ method public int getTimerKeyAt(int);
+ method public int getTimerKeyCount();
+ method public long getTimerTime(int);
+ method public java.util.Map<java.lang.String, android.os.health.TimerStat> getTimers(int);
+ method public int getTimersKeyAt(int);
+ method public int getTimersKeyCount();
+ method public boolean hasMeasurement(int);
+ method public boolean hasMeasurements(int);
+ method public boolean hasStats(int);
+ method public boolean hasTimer(int);
+ method public boolean hasTimers(int);
+ }
+
+ public final class PackageHealthStats {
+ field public static final int MEASUREMENTS_WAKEUP_ALARMS_COUNT = 40002; // 0x9c42
+ field public static final int STATS_SERVICES = 40001; // 0x9c41
+ }
+
+ public final class PidHealthStats {
+ field public static final int MEASUREMENT_WAKE_NESTING_COUNT = 20001; // 0x4e21
+ field public static final int MEASUREMENT_WAKE_START_MS = 20003; // 0x4e23
+ field public static final int MEASUREMENT_WAKE_SUM_MS = 20002; // 0x4e22
+ }
+
+ public final class ProcessHealthStats {
+ field public static final int MEASUREMENT_ANR_COUNT = 30005; // 0x7535
+ field public static final int MEASUREMENT_CRASHES_COUNT = 30004; // 0x7534
+ field public static final int MEASUREMENT_FOREGROUND_MS = 30006; // 0x7536
+ field public static final int MEASUREMENT_STARTS_COUNT = 30003; // 0x7533
+ field public static final int MEASUREMENT_SYSTEM_TIME_MS = 30002; // 0x7532
+ field public static final int MEASUREMENT_USER_TIME_MS = 30001; // 0x7531
+ }
+
+ public final class ServiceHealthStats {
+ field public static final int MEASUREMENT_LAUNCH_COUNT = 50002; // 0xc352
+ field public static final int MEASUREMENT_START_SERVICE_COUNT = 50001; // 0xc351
+ }
+
+ public class SystemHealthManager {
+ method public static android.os.health.SystemHealthManager from(android.content.Context);
+ method public android.os.health.HealthStats takeMyUidSnapshot();
+ method public android.os.health.HealthStats takeUidSnapshot(int);
+ method public android.os.health.HealthStats[] takeUidSnapshots(int[]);
+ }
+
+ public class TimerStat implements android.os.Parcelable {
+ ctor public TimerStat();
+ ctor public TimerStat(int, long);
+ ctor public TimerStat(android.os.Parcel);
+ method public int describeContents();
+ method public int getCount();
+ method public long getTime();
+ method public void setCount(int);
+ method public void setTime(long);
+ method public void writeToParcel(android.os.Parcel, int);
+ field public static final android.os.Parcelable.Creator<android.os.health.TimerStat> CREATOR;
+ }
+
+ public final class UidHealthStats {
+ field public static final int MEASUREMENT_BLUETOOTH_IDLE_MS = 10020; // 0x2724
+ field public static final int MEASUREMENT_BLUETOOTH_POWER_MAMS = 10023; // 0x2727
+ field public static final int MEASUREMENT_BLUETOOTH_RX_BYTES = 10052; // 0x2744
+ field public static final int MEASUREMENT_BLUETOOTH_RX_MS = 10021; // 0x2725
+ field public static final int MEASUREMENT_BLUETOOTH_RX_PACKETS = 10058; // 0x274a
+ field public static final int MEASUREMENT_BLUETOOTH_TX_BYTES = 10053; // 0x2745
+ field public static final int MEASUREMENT_BLUETOOTH_TX_MS = 10022; // 0x2726
+ field public static final int MEASUREMENT_BLUETOOTH_TX_PACKETS = 10059; // 0x274b
+ field public static final int MEASUREMENT_BUTTON_USER_ACTIVITY_COUNT = 10046; // 0x273e
+ field public static final int MEASUREMENT_CPU_POWER_MAUS = 10064; // 0x2750
+ field public static final int MEASUREMENT_MOBILE_IDLE_MS = 10024; // 0x2728
+ field public static final int MEASUREMENT_MOBILE_POWER_MAMS = 10027; // 0x272b
+ field public static final int MEASUREMENT_MOBILE_RX_BYTES = 10048; // 0x2740
+ field public static final int MEASUREMENT_MOBILE_RX_MS = 10025; // 0x2729
+ field public static final int MEASUREMENT_MOBILE_RX_PACKETS = 10054; // 0x2746
+ field public static final int MEASUREMENT_MOBILE_TX_BYTES = 10049; // 0x2741
+ field public static final int MEASUREMENT_MOBILE_TX_MS = 10026; // 0x272a
+ field public static final int MEASUREMENT_MOBILE_TX_PACKETS = 10055; // 0x2747
+ field public static final int MEASUREMENT_OTHER_USER_ACTIVITY_COUNT = 10045; // 0x273d
+ field public static final int MEASUREMENT_REALTIME_BATTERY_MS = 10001; // 0x2711
+ field public static final int MEASUREMENT_REALTIME_SCREEN_OFF_BATTERY_MS = 10003; // 0x2713
+ field public static final int MEASUREMENT_SYSTEM_CPU_TIME_US = 10063; // 0x274f
+ field public static final int MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT = 10047; // 0x273f
+ field public static final int MEASUREMENT_UPTIME_BATTERY_MS = 10002; // 0x2712
+ field public static final int MEASUREMENT_UPTIME_SCREEN_OFF_BATTERY_MS = 10004; // 0x2714
+ field public static final int MEASUREMENT_USER_CPU_TIME_US = 10062; // 0x274e
+ field public static final int MEASUREMENT_WIFI_FULL_LOCK_MS = 10029; // 0x272d
+ field public static final int MEASUREMENT_WIFI_IDLE_MS = 10016; // 0x2720
+ field public static final int MEASUREMENT_WIFI_MULTICAST_MS = 10031; // 0x272f
+ field public static final int MEASUREMENT_WIFI_POWER_MAMS = 10019; // 0x2723
+ field public static final int MEASUREMENT_WIFI_RUNNING_MS = 10028; // 0x272c
+ field public static final int MEASUREMENT_WIFI_RX_BYTES = 10050; // 0x2742
+ field public static final int MEASUREMENT_WIFI_RX_MS = 10017; // 0x2721
+ field public static final int MEASUREMENT_WIFI_RX_PACKETS = 10056; // 0x2748
+ field public static final int MEASUREMENT_WIFI_TX_BYTES = 10051; // 0x2743
+ field public static final int MEASUREMENT_WIFI_TX_MS = 10018; // 0x2722
+ field public static final int MEASUREMENT_WIFI_TX_PACKETS = 10057; // 0x2749
+ field public static final int STATS_PACKAGES = 10015; // 0x271f
+ field public static final int STATS_PIDS = 10013; // 0x271d
+ field public static final int STATS_PROCESSES = 10014; // 0x271e
+ field public static final int TIMERS_JOBS = 10010; // 0x271a
+ field public static final int TIMERS_SENSORS = 10012; // 0x271c
+ field public static final int TIMERS_SYNCS = 10009; // 0x2719
+ field public static final int TIMERS_WAKELOCKS_DRAW = 10008; // 0x2718
+ field public static final int TIMERS_WAKELOCKS_FULL = 10005; // 0x2715
+ field public static final int TIMERS_WAKELOCKS_PARTIAL = 10006; // 0x2716
+ field public static final int TIMERS_WAKELOCKS_WINDOW = 10007; // 0x2717
+ field public static final int TIMER_AUDIO = 10032; // 0x2730
+ field public static final int TIMER_BLUETOOTH_SCAN = 10037; // 0x2735
+ field public static final int TIMER_CAMERA = 10035; // 0x2733
+ field public static final int TIMER_FLASHLIGHT = 10034; // 0x2732
+ field public static final int TIMER_FOREGROUND_ACTIVITY = 10036; // 0x2734
+ field public static final int TIMER_GPS_SENSOR = 10011; // 0x271b
+ field public static final int TIMER_MOBILE_RADIO_ACTIVE = 10061; // 0x274d
+ field public static final int TIMER_PROCESS_STATE_BACKGROUND_MS = 10042; // 0x273a
+ field public static final int TIMER_PROCESS_STATE_CACHED_MS = 10043; // 0x273b
+ field public static final int TIMER_PROCESS_STATE_FOREGROUND_MS = 10041; // 0x2739
+ field public static final int TIMER_PROCESS_STATE_FOREGROUND_SERVICE_MS = 10039; // 0x2737
+ field public static final int TIMER_PROCESS_STATE_TOP_MS = 10038; // 0x2736
+ field public static final int TIMER_PROCESS_STATE_TOP_SLEEPING_MS = 10040; // 0x2738
+ field public static final int TIMER_VIBRATOR = 10044; // 0x273c
+ field public static final int TIMER_VIDEO = 10033; // 0x2731
+ field public static final int TIMER_WIFI_SCAN = 10030; // 0x272e
+ }
+
+}
+
package android.os.storage {
public abstract class OnObbStateChangeListener {
diff --git a/api/test-current.txt b/api/test-current.txt
index 03dc353..4394ba5 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -8148,6 +8148,7 @@
field public static final java.lang.String SENSOR_SERVICE = "sensor";
field public static final java.lang.String SHORTCUT_SERVICE = "shortcut";
field public static final java.lang.String STORAGE_SERVICE = "storage";
+ field public static final java.lang.String SYSTEM_HEALTH_SERVICE = "systemhealth";
field public static final java.lang.String TELECOM_SERVICE = "telecom";
field public static final java.lang.String TELEPHONY_SERVICE = "phone";
field public static final java.lang.String TELEPHONY_SUBSCRIPTION_SERVICE = "telephony_subscription_service";
@@ -29428,6 +29429,147 @@
}
+package android.os.health {
+
+ public class HealthStats {
+ method public java.lang.String getDataType();
+ method public long getMeasurement(int);
+ method public int getMeasurementKeyAt(int);
+ method public int getMeasurementKeyCount();
+ method public java.util.Map<java.lang.String, java.lang.Long> getMeasurements(int);
+ method public int getMeasurementsKeyAt(int);
+ method public int getMeasurementsKeyCount();
+ method public java.util.Map<java.lang.String, android.os.health.HealthStats> getStats(int);
+ method public int getStatsKeyAt(int);
+ method public int getStatsKeyCount();
+ method public android.os.health.TimerStat getTimer(int);
+ method public int getTimerCount(int);
+ method public int getTimerKeyAt(int);
+ method public int getTimerKeyCount();
+ method public long getTimerTime(int);
+ method public java.util.Map<java.lang.String, android.os.health.TimerStat> getTimers(int);
+ method public int getTimersKeyAt(int);
+ method public int getTimersKeyCount();
+ method public boolean hasMeasurement(int);
+ method public boolean hasMeasurements(int);
+ method public boolean hasStats(int);
+ method public boolean hasTimer(int);
+ method public boolean hasTimers(int);
+ }
+
+ public final class PackageHealthStats {
+ field public static final int MEASUREMENTS_WAKEUP_ALARMS_COUNT = 40002; // 0x9c42
+ field public static final int STATS_SERVICES = 40001; // 0x9c41
+ }
+
+ public final class PidHealthStats {
+ field public static final int MEASUREMENT_WAKE_NESTING_COUNT = 20001; // 0x4e21
+ field public static final int MEASUREMENT_WAKE_START_MS = 20003; // 0x4e23
+ field public static final int MEASUREMENT_WAKE_SUM_MS = 20002; // 0x4e22
+ }
+
+ public final class ProcessHealthStats {
+ field public static final int MEASUREMENT_ANR_COUNT = 30005; // 0x7535
+ field public static final int MEASUREMENT_CRASHES_COUNT = 30004; // 0x7534
+ field public static final int MEASUREMENT_FOREGROUND_MS = 30006; // 0x7536
+ field public static final int MEASUREMENT_STARTS_COUNT = 30003; // 0x7533
+ field public static final int MEASUREMENT_SYSTEM_TIME_MS = 30002; // 0x7532
+ field public static final int MEASUREMENT_USER_TIME_MS = 30001; // 0x7531
+ }
+
+ public final class ServiceHealthStats {
+ field public static final int MEASUREMENT_LAUNCH_COUNT = 50002; // 0xc352
+ field public static final int MEASUREMENT_START_SERVICE_COUNT = 50001; // 0xc351
+ }
+
+ public class SystemHealthManager {
+ method public static android.os.health.SystemHealthManager from(android.content.Context);
+ method public android.os.health.HealthStats takeMyUidSnapshot();
+ method public android.os.health.HealthStats takeUidSnapshot(int);
+ method public android.os.health.HealthStats[] takeUidSnapshots(int[]);
+ }
+
+ public class TimerStat implements android.os.Parcelable {
+ ctor public TimerStat();
+ ctor public TimerStat(int, long);
+ ctor public TimerStat(android.os.Parcel);
+ method public int describeContents();
+ method public int getCount();
+ method public long getTime();
+ method public void setCount(int);
+ method public void setTime(long);
+ method public void writeToParcel(android.os.Parcel, int);
+ field public static final android.os.Parcelable.Creator<android.os.health.TimerStat> CREATOR;
+ }
+
+ public final class UidHealthStats {
+ field public static final int MEASUREMENT_BLUETOOTH_IDLE_MS = 10020; // 0x2724
+ field public static final int MEASUREMENT_BLUETOOTH_POWER_MAMS = 10023; // 0x2727
+ field public static final int MEASUREMENT_BLUETOOTH_RX_BYTES = 10052; // 0x2744
+ field public static final int MEASUREMENT_BLUETOOTH_RX_MS = 10021; // 0x2725
+ field public static final int MEASUREMENT_BLUETOOTH_RX_PACKETS = 10058; // 0x274a
+ field public static final int MEASUREMENT_BLUETOOTH_TX_BYTES = 10053; // 0x2745
+ field public static final int MEASUREMENT_BLUETOOTH_TX_MS = 10022; // 0x2726
+ field public static final int MEASUREMENT_BLUETOOTH_TX_PACKETS = 10059; // 0x274b
+ field public static final int MEASUREMENT_BUTTON_USER_ACTIVITY_COUNT = 10046; // 0x273e
+ field public static final int MEASUREMENT_CPU_POWER_MAUS = 10064; // 0x2750
+ field public static final int MEASUREMENT_MOBILE_IDLE_MS = 10024; // 0x2728
+ field public static final int MEASUREMENT_MOBILE_POWER_MAMS = 10027; // 0x272b
+ field public static final int MEASUREMENT_MOBILE_RX_BYTES = 10048; // 0x2740
+ field public static final int MEASUREMENT_MOBILE_RX_MS = 10025; // 0x2729
+ field public static final int MEASUREMENT_MOBILE_RX_PACKETS = 10054; // 0x2746
+ field public static final int MEASUREMENT_MOBILE_TX_BYTES = 10049; // 0x2741
+ field public static final int MEASUREMENT_MOBILE_TX_MS = 10026; // 0x272a
+ field public static final int MEASUREMENT_MOBILE_TX_PACKETS = 10055; // 0x2747
+ field public static final int MEASUREMENT_OTHER_USER_ACTIVITY_COUNT = 10045; // 0x273d
+ field public static final int MEASUREMENT_REALTIME_BATTERY_MS = 10001; // 0x2711
+ field public static final int MEASUREMENT_REALTIME_SCREEN_OFF_BATTERY_MS = 10003; // 0x2713
+ field public static final int MEASUREMENT_SYSTEM_CPU_TIME_US = 10063; // 0x274f
+ field public static final int MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT = 10047; // 0x273f
+ field public static final int MEASUREMENT_UPTIME_BATTERY_MS = 10002; // 0x2712
+ field public static final int MEASUREMENT_UPTIME_SCREEN_OFF_BATTERY_MS = 10004; // 0x2714
+ field public static final int MEASUREMENT_USER_CPU_TIME_US = 10062; // 0x274e
+ field public static final int MEASUREMENT_WIFI_FULL_LOCK_MS = 10029; // 0x272d
+ field public static final int MEASUREMENT_WIFI_IDLE_MS = 10016; // 0x2720
+ field public static final int MEASUREMENT_WIFI_MULTICAST_MS = 10031; // 0x272f
+ field public static final int MEASUREMENT_WIFI_POWER_MAMS = 10019; // 0x2723
+ field public static final int MEASUREMENT_WIFI_RUNNING_MS = 10028; // 0x272c
+ field public static final int MEASUREMENT_WIFI_RX_BYTES = 10050; // 0x2742
+ field public static final int MEASUREMENT_WIFI_RX_MS = 10017; // 0x2721
+ field public static final int MEASUREMENT_WIFI_RX_PACKETS = 10056; // 0x2748
+ field public static final int MEASUREMENT_WIFI_TX_BYTES = 10051; // 0x2743
+ field public static final int MEASUREMENT_WIFI_TX_MS = 10018; // 0x2722
+ field public static final int MEASUREMENT_WIFI_TX_PACKETS = 10057; // 0x2749
+ field public static final int STATS_PACKAGES = 10015; // 0x271f
+ field public static final int STATS_PIDS = 10013; // 0x271d
+ field public static final int STATS_PROCESSES = 10014; // 0x271e
+ field public static final int TIMERS_JOBS = 10010; // 0x271a
+ field public static final int TIMERS_SENSORS = 10012; // 0x271c
+ field public static final int TIMERS_SYNCS = 10009; // 0x2719
+ field public static final int TIMERS_WAKELOCKS_DRAW = 10008; // 0x2718
+ field public static final int TIMERS_WAKELOCKS_FULL = 10005; // 0x2715
+ field public static final int TIMERS_WAKELOCKS_PARTIAL = 10006; // 0x2716
+ field public static final int TIMERS_WAKELOCKS_WINDOW = 10007; // 0x2717
+ field public static final int TIMER_AUDIO = 10032; // 0x2730
+ field public static final int TIMER_BLUETOOTH_SCAN = 10037; // 0x2735
+ field public static final int TIMER_CAMERA = 10035; // 0x2733
+ field public static final int TIMER_FLASHLIGHT = 10034; // 0x2732
+ field public static final int TIMER_FOREGROUND_ACTIVITY = 10036; // 0x2734
+ field public static final int TIMER_GPS_SENSOR = 10011; // 0x271b
+ field public static final int TIMER_MOBILE_RADIO_ACTIVE = 10061; // 0x274d
+ field public static final int TIMER_PROCESS_STATE_BACKGROUND_MS = 10042; // 0x273a
+ field public static final int TIMER_PROCESS_STATE_CACHED_MS = 10043; // 0x273b
+ field public static final int TIMER_PROCESS_STATE_FOREGROUND_MS = 10041; // 0x2739
+ field public static final int TIMER_PROCESS_STATE_FOREGROUND_SERVICE_MS = 10039; // 0x2737
+ field public static final int TIMER_PROCESS_STATE_TOP_MS = 10038; // 0x2736
+ field public static final int TIMER_PROCESS_STATE_TOP_SLEEPING_MS = 10040; // 0x2738
+ field public static final int TIMER_VIBRATOR = 10044; // 0x273c
+ field public static final int TIMER_VIDEO = 10033; // 0x2731
+ field public static final int TIMER_WIFI_SCAN = 10030; // 0x272e
+ }
+
+}
+
package android.os.storage {
public abstract class OnObbStateChangeListener {
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index bd321ac..b98b363 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -103,6 +103,7 @@
import android.os.UserHandle;
import android.os.UserManager;
import android.os.Vibrator;
+import android.os.health.SystemHealthManager;
import android.os.storage.StorageManager;
import android.print.IPrintManager;
import android.print.PrintManager;
@@ -747,7 +748,6 @@
@Override
public SoundTriggerManager createService(ContextImpl ctx) {
IBinder b = ServiceManager.getService(Context.SOUND_TRIGGER_SERVICE);
- Log.i(TAG, "Creating new instance of SoundTriggerManager object.");
return new SoundTriggerManager(ctx, ISoundTriggerService.Stub.asInterface(b));
}});
@@ -758,6 +758,13 @@
IBinder b = ServiceManager.getService(Context.SHORTCUT_SERVICE);
return new ShortcutManager(ctx, IShortcutService.Stub.asInterface(b));
}});
+
+ registerService(Context.SYSTEM_HEALTH_SERVICE, SystemHealthManager.class,
+ new CachedServiceFetcher<SystemHealthManager>() {
+ @Override
+ public SystemHealthManager createService(ContextImpl ctx) {
+ return new SystemHealthManager();
+ }});
}
/**
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index f96ddf0..825dd5b 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -3585,6 +3585,15 @@
public static final String SHORTCUT_SERVICE = "shortcut";
/**
+ * Use with {@link #getSystemService} to retrieve a
+ * {@link android.os.health.SystemHealthManager} for accessing system health (battery, power,
+ * memory, etc) metrics.
+ *
+ * @see #getSystemService
+ */
+ public static final String SYSTEM_HEALTH_SERVICE = "systemhealth";
+
+ /**
* Determine whether the given permission is allowed for a particular
* process and user ID running in the system.
*
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index 8281279..e1c7ad77 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -465,6 +465,7 @@
};
public abstract long getProcessStateTime(int state, long elapsedRealtimeUs, int which);
+ public abstract Timer getProcessStateTimer(int state);
public abstract Timer getVibratorOnTimer();
diff --git a/core/java/android/os/health/HealthKeys.java b/core/java/android/os/health/HealthKeys.java
new file mode 100644
index 0000000..842def3
--- /dev/null
+++ b/core/java/android/os/health/HealthKeys.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+
+/**
+ * Constants and stuff for the android.os.health package.
+ *
+ * @hide
+ */
+public class HealthKeys {
+
+ /**
+ * No valid key will ever be 0.
+ */
+ public static final int UNKNOWN_KEY = 0;
+
+ /*
+ * Base key for each of the different classes. There is
+ * nothing intrinsic to the operation of the value of the
+ * keys. It's just segmented for better debugging. The
+ * classes don't mix them anway.
+ */
+ public static final int BASE_UID = 10000;
+ public static final int BASE_PID = 20000;
+ public static final int BASE_PROCESS = 30000;
+ public static final int BASE_PACKAGE = 40000;
+ public static final int BASE_SERVICE = 50000;
+
+ /*
+ * The types of values supported by HealthStats.
+ */
+ public static final int TYPE_TIMER = 0;
+ public static final int TYPE_MEASUREMENT = 1;
+ public static final int TYPE_STATS = 2;
+ public static final int TYPE_TIMERS = 3;
+ public static final int TYPE_MEASUREMENTS = 4;
+
+ public static final int TYPE_COUNT = 5;
+
+ /**
+ * Annotation to mark public static final int fields that are to be used
+ * as field keys in HealthStats.
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target({ElementType.FIELD})
+ public @interface Constant {
+ /**
+ * One of the TYPE_* constants above.
+ */
+ int type();
+ }
+
+ /**
+ * Class to gather the constants defined in a class full of constants and
+ * build the key indices used by HealthStatsWriter and HealthStats.
+ *
+ * @hide
+ */
+ public static class Constants {
+ private final String mDataType;
+ private final int[][] mKeys = new int[TYPE_COUNT][];
+
+ /**
+ * Pass in a class to gather the public static final int fields that are
+ * tagged with the @Constant annotation.
+ */
+ public Constants(Class clazz) {
+ // Save the class name for debugging
+ mDataType = clazz.getSimpleName();
+
+ // Iterate through the list of fields on this class, and build the
+ // constant arrays for these fields.
+ final Field[] fields = clazz.getDeclaredFields();
+ final Class<Constant> annotationClass = Constant.class;
+
+ final int N = fields.length;
+
+ final SortedIntArray[] keys = new SortedIntArray[mKeys.length];
+ for (int i=0; i<keys.length; i++) {
+ keys[i] = new SortedIntArray(N);
+ }
+
+ for (int i=0; i<N; i++) {
+ final Field field = fields[i];
+ final Constant constant = field.getAnnotation(annotationClass);
+ if (constant != null) {
+ final int type = constant.type();
+ if (type >= keys.length) {
+ throw new RuntimeException("Unknown Constant type " + type
+ + " on " + field);
+ }
+ try {
+ keys[type].addValue(field.getInt(null));
+ } catch (IllegalAccessException ex) {
+ throw new RuntimeException("Can't read constant value type=" + type
+ + " field=" + field, ex);
+ }
+ }
+ }
+
+ for (int i=0; i<keys.length; i++) {
+ mKeys[i] = keys[i].getArray();
+ }
+ }
+
+ /**
+ * Get a string representation of this class. Useful for debugging. It will be the
+ * simple name of the class passed in the constructor.
+ */
+ public String getDataType() {
+ return mDataType;
+ }
+
+ /**
+ * Return how many keys there are for the given field type.
+ *
+ * @see TYPE_TIMER
+ * @see TYPE_MEASUREMENT
+ * @see TYPE_TIMERS
+ * @see TYPE_MEASUREMENTS
+ * @see TYPE_STATS
+ */
+ public int getSize(int type) {
+ return mKeys[type].length;
+ }
+
+ /**
+ * Return the index for the given type and key combination in the array of field
+ * keys or values.
+ *
+ * @see TYPE_TIMER
+ * @see TYPE_MEASUREMENT
+ * @see TYPE_TIMERS
+ * @see TYPE_MEASUREMENTS
+ * @see TYPE_STATS
+ */
+ public int getIndex(int type, int key) {
+ final int index = Arrays.binarySearch(mKeys[type], key);
+ if (index >= 0) {
+ return index;
+ } else {
+ throw new RuntimeException("Unknown Constant " + key + " (of type "
+ + type + " )");
+ }
+ }
+
+ /**
+ * Get the array of keys for the given field type.
+ */
+ public int[] getKeys(int type) {
+ return mKeys[type];
+ }
+ }
+
+ /**
+ * An array of fixed size that will be sorted.
+ */
+ private static class SortedIntArray {
+ int mCount;
+ int[] mArray;
+
+ /**
+ * Construct with the maximum number of values.
+ */
+ SortedIntArray(int maxCount) {
+ mArray = new int[maxCount];
+ }
+
+ /**
+ * Add a value.
+ */
+ void addValue(int value) {
+ mArray[mCount++] = value;
+ }
+
+ /**
+ * Get the array of values that have been added, with the values in
+ * numerically increasing order.
+ */
+ int[] getArray() {
+ if (mCount == mArray.length) {
+ Arrays.sort(mArray);
+ return mArray;
+ } else {
+ final int[] result = new int[mCount];
+ System.arraycopy(mArray, 0, result, 0, mCount);
+ Arrays.sort(result);
+ return result;
+ }
+ }
+ }
+}
+
+
diff --git a/core/java/android/os/health/HealthStats.java b/core/java/android/os/health/HealthStats.java
new file mode 100644
index 0000000..f0489e2
--- /dev/null
+++ b/core/java/android/os/health/HealthStats.java
@@ -0,0 +1,481 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.util.ArrayMap;
+
+import java.util.Arrays;
+import java.util.Map;
+
+/**
+ * A HealthStats object contains system health data about an application.
+ *
+ * <p>
+ * <b>Data Types</b><br>
+ * Each of the keys references data in one of five data types:
+ *
+ * <p>
+ * A <b>measurement</b> metric contains a sinlge {@code long} value. That value may
+ * be a count, a time, or some other type of value. The unit for a measurement
+ * (COUNT, MS, etc) will always be in the name of the constant for the key to
+ * retrieve it. For example, the
+ * {@link android.os.health.UidHealthStats#MEASUREMENT_WIFI_TX_MS UidHealthStats.MEASUREMENT_WIFI_TX_MS}
+ * value is the number of milliseconds (ms) that were spent transmitting on wifi by an
+ * application. The
+ * {@link android.os.health.UidHealthStats#MEASUREMENT_MOBILE_RX_PACKETS UidHealthStats.MEASUREMENT_MOBILE_RX_PACKETS}
+ * measurement is the number of packets received on behalf of an application.
+ * The {@link android.os.health.UidHealthStats#MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT
+ * UidHealthStats.MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT}
+ * measurement is the number of times the user touched the screen, causing the
+ * screen to stay awake.
+ *
+ *
+ * <p>
+ * A <b>timer</b> metric contains an {@code int} count and a {@code long} time,
+ * measured in milliseconds. Timers track how many times a resource was used, and
+ * the total duration for that usage. For example, the
+ * {@link android.os.health.UidHealthStats#TIMER_FLASHLIGHT}
+ * timer tracks how many times the application turned on the flashlight, and for
+ * how many milliseconds total it kept it on.
+ *
+ * <p>
+ * A <b>measurement map</b> metric is a mapping of {@link java.lang.String} names to
+ * {@link java.lang.Long} values. The names typically are application provided names. For
+ * example, the
+ * {@link android.os.health.PackageHealthStats#MEASUREMENTS_WAKEUP_ALARMS_COUNT
+ * PackageHealthStats.MEASUREMENTS_WAKEUP_ALARMS_COUNT}
+ * measurement map is a mapping of the tag provided to the
+ * {@link android.app.AlarmManager} when the alarm is scheduled.
+ *
+ * <p>
+ * A <b>timer map</b> metric is a mapping of {@link java.lang.String} names to
+ * {@link android.os.health.TimerStat} objects. The names are typically application
+ * provided names. For example, the
+ * {@link android.os.health.UidHealthStats#TIMERS_WAKELOCKS_PARTIAL UidHealthStats.TIMERS_WAKELOCKS_PARTIAL}
+ * is a mapping of tag provided to the {@link android.os.PowerManager} when the
+ * wakelock is created to the number of times and for how long each wakelock was
+ * active.
+ *
+ * <p>
+ * Lastly, a <b>health stats</b> metric is a mapping of {@link java.lang.String}
+ * names to a recursive {@link android.os.health.HealthStats} object containing
+ * more detailed information. For example, the
+ * {@link android.os.health.UidHealthStats#STATS_PACKAGES UidHealthStats.STATS_PACKAGES}
+ * metric is a mapping of the package names for each of the APKs sharing a uid to
+ * the information recorded for that apk. The returned HealthStats objects will
+ * each be associated with a different set of constants. For the HealthStats
+ * returned for UidHealthStats.STATS_PACKAGES, the keys come from the
+ * {@link android.os.health.PackageHealthStats} class.
+ *
+ */
+public class HealthStats {
+ // Header fields
+ private String mDataType;
+
+ // TimerStat fields
+ private int[] mTimerKeys;
+ private int[] mTimerCounts;
+ private long[] mTimerTimes;
+
+ // Measurement fields
+ private int[] mMeasurementKeys;
+ private long[] mMeasurementValues;
+
+ // Stats fields
+ private int[] mStatsKeys;
+ private ArrayMap<String,HealthStats>[] mStatsValues;
+
+ // Timers fields
+ private int[] mTimersKeys;
+ private ArrayMap<String,TimerStat>[] mTimersValues;
+
+ // Measurements fields
+ private int[] mMeasurementsKeys;
+ private ArrayMap<String,Long>[] mMeasurementsValues;
+
+ /**
+ * HealthStats empty constructor not implemented because this
+ * class is read-only.
+ */
+ private HealthStats() {
+ throw new RuntimeException("unsupported");
+ }
+
+ /**
+ * Construct a health stats object from a parcel.
+ *
+ * @hide
+ */
+ public HealthStats(Parcel in) {
+ int count;
+
+ // Header fields
+ mDataType = in.readString();
+
+ // TimerStat fields
+ count = in.readInt();
+ mTimerKeys = new int[count];
+ mTimerCounts = new int[count];
+ mTimerTimes = new long[count];
+ for (int i=0; i<count; i++) {
+ mTimerKeys[i] = in.readInt();
+ mTimerCounts[i] = in.readInt();
+ mTimerTimes[i] = in.readLong();
+ }
+
+ // Measurement fields
+ count = in.readInt();
+ mMeasurementKeys = new int[count];
+ mMeasurementValues = new long[count];
+ for (int i=0; i<count; i++) {
+ mMeasurementKeys[i] = in.readInt();
+ mMeasurementValues[i] = in.readLong();
+ }
+
+ // Stats fields
+ count = in.readInt();
+ mStatsKeys = new int[count];
+ mStatsValues = new ArrayMap[count];
+ for (int i=0; i<count; i++) {
+ mStatsKeys[i] = in.readInt();
+ mStatsValues[i] = createHealthStatsMap(in);
+ }
+
+ // Timers fields
+ count = in.readInt();
+ mTimersKeys = new int[count];
+ mTimersValues = new ArrayMap[count];
+ for (int i=0; i<count; i++) {
+ mTimersKeys[i] = in.readInt();
+ mTimersValues[i] = createParcelableMap(in, TimerStat.CREATOR);
+ }
+
+ // Measurements fields
+ count = in.readInt();
+ mMeasurementsKeys = new int[count];
+ mMeasurementsValues = new ArrayMap[count];
+ for (int i=0; i<count; i++) {
+ mMeasurementsKeys[i] = in.readInt();
+ mMeasurementsValues[i] = createLongsMap(in);
+ }
+ }
+
+ /**
+ * Get a name representing the contents of this object.
+ *
+ * @see UidHealthStats
+ * @see PackageHealthStats
+ * @see PidHealthStats
+ * @see ProcessHealthStats
+ * @see ServiceHealthStats
+ */
+ public String getDataType() {
+ return mDataType;
+ }
+
+ /**
+ * Return whether this object contains a TimerStat for the supplied key.
+ */
+ public boolean hasTimer(int key) {
+ return getIndex(mTimerKeys, key) >= 0;
+ }
+
+ /**
+ * Return a TimerStat object for the given key.
+ *
+ * This will allocate a new {@link TimerStat} object, which may be wasteful. Instead, use
+ * {@link #getTimerCount} and {@link #getTimerTime}.
+ *
+ * @throws IndexOutOfBoundsException When the key is not present in this object.
+ * @see #hasTimer hasTimer(int) To check if a value for the given key is present.
+ */
+ public TimerStat getTimer(int key) {
+ final int index = getIndex(mTimerKeys, key);
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Bad timer key dataType=" + mDataType
+ + " key=" + key);
+ }
+ return new TimerStat(mTimerCounts[index], mTimerTimes[index]);
+ }
+
+ /**
+ * Get the count for the timer for the given key.
+ *
+ * @throws IndexOutOfBoundsException When the key is not present in this object.
+ * @see #hasTimer hasTimer(int) To check if a value for the given key is present.
+ */
+ public int getTimerCount(int key) {
+ final int index = getIndex(mTimerKeys, key);
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Bad timer key dataType=" + mDataType
+ + " key=" + key);
+ }
+ return mTimerCounts[index];
+ }
+
+ /**
+ * Get the time for the timer for the given key, in milliseconds.
+ *
+ * @throws IndexOutOfBoundsException When the key is not present in this object.
+ * @see #hasTimer hasTimer(int) To check if a value for the given key is present.
+ */
+ public long getTimerTime(int key) {
+ final int index = getIndex(mTimerKeys, key);
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Bad timer key dataType=" + mDataType
+ + " key=" + key);
+ }
+ return mTimerTimes[index];
+ }
+
+ /**
+ * Get the number of timer values in this object. Can be used to iterate through
+ * the available timers.
+ *
+ * @see #getTimerKeyAt
+ */
+ public int getTimerKeyCount() {
+ return mTimerKeys.length;
+ }
+
+ /**
+ * Get the key for the timer at the given index. Index must be between 0 and the result
+ * of {@link #getTimerKeyCount getTimerKeyCount()}.
+ *
+ * @see #getTimerKeyCount
+ */
+ public int getTimerKeyAt(int index) {
+ return mTimerKeys[index];
+ }
+
+ /**
+ * Return whether this object contains a measurement for the supplied key.
+ */
+ public boolean hasMeasurement(int key) {
+ return getIndex(mMeasurementKeys, key) >= 0;
+ }
+
+ /**
+ * Get the measurement for the given key.
+ *
+ * @throws IndexOutOfBoundsException When the key is not present in this object.
+ * @see #hasMeasurement hasMeasurement(int) To check if a value for the given key is present.
+ */
+ public long getMeasurement(int key) {
+ final int index = getIndex(mMeasurementKeys, key);
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Bad measurement key dataType=" + mDataType
+ + " key=" + key);
+ }
+ return mMeasurementValues[index];
+ }
+
+ /**
+ * Get the number of measurement values in this object. Can be used to iterate through
+ * the available measurements.
+ *
+ * @see #getMeasurementKeyAt
+ */
+ public int getMeasurementKeyCount() {
+ return mMeasurementKeys.length;
+ }
+
+ /**
+ * Get the key for the measurement at the given index. Index must be between 0 and the result
+ * of {@link #getMeasurementKeyCount getMeasurementKeyCount()}.
+ *
+ * @see #getMeasurementKeyCount
+ */
+ public int getMeasurementKeyAt(int index) {
+ return mMeasurementKeys[index];
+ }
+
+ /**
+ * Return whether this object contains a HealthStats map for the supplied key.
+ */
+ public boolean hasStats(int key) {
+ return getIndex(mStatsKeys, key) >= 0;
+ }
+
+ /**
+ * Get the HealthStats map for the given key.
+ *
+ * @throws IndexOutOfBoundsException When the key is not present in this object.
+ * @see #hasStats hasStats(int) To check if a value for the given key is present.
+ */
+ public Map<String,HealthStats> getStats(int key) {
+ final int index = getIndex(mStatsKeys, key);
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Bad stats key dataType=" + mDataType
+ + " key=" + key);
+ }
+ return mStatsValues[index];
+ }
+
+ /**
+ * Get the number of HealthStat map values in this object. Can be used to iterate through
+ * the available measurements.
+ *
+ * @see #getMeasurementKeyAt
+ */
+ public int getStatsKeyCount() {
+ return mStatsKeys.length;
+ }
+
+ /**
+ * Get the key for the timer at the given index. Index must be between 0 and the result
+ * of {@link #getStatsKeyCount getStatsKeyCount()}.
+ *
+ * @see #getStatsKeyCount
+ */
+ public int getStatsKeyAt(int index) {
+ return mStatsKeys[index];
+ }
+
+ /**
+ * Return whether this object contains a timers map for the supplied key.
+ */
+ public boolean hasTimers(int key) {
+ return getIndex(mTimersKeys, key) >= 0;
+ }
+
+ /**
+ * Get the TimerStat map for the given key.
+ *
+ * @throws IndexOutOfBoundsException When the key is not present in this object.
+ * @see #hasTimers hasTimers(int) To check if a value for the given key is present.
+ */
+ public Map<String,TimerStat> getTimers(int key) {
+ final int index = getIndex(mTimersKeys, key);
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Bad timers key dataType=" + mDataType
+ + " key=" + key);
+ }
+ return mTimersValues[index];
+ }
+
+ /**
+ * Get the number of timer map values in this object. Can be used to iterate through
+ * the available timer maps.
+ *
+ * @see #getTimersKeyAt
+ */
+ public int getTimersKeyCount() {
+ return mTimersKeys.length;
+ }
+
+ /**
+ * Get the key for the timer map at the given index. Index must be between 0 and the result
+ * of {@link #getTimersKeyCount getTimersKeyCount()}.
+ *
+ * @see #getTimersKeyCount
+ */
+ public int getTimersKeyAt(int index) {
+ return mTimersKeys[index];
+ }
+
+ /**
+ * Return whether this object contains a measurements map for the supplied key.
+ */
+ public boolean hasMeasurements(int key) {
+ return getIndex(mMeasurementsKeys, key) >= 0;
+ }
+
+ /**
+ * Get the measurements map for the given key.
+ *
+ * @throws IndexOutOfBoundsException When the key is not present in this object.
+ * @see #hasMeasurements To check if a value for the given key is present.
+ */
+ public Map<String,Long> getMeasurements(int key) {
+ final int index = getIndex(mMeasurementsKeys, key);
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Bad measurements key dataType=" + mDataType
+ + " key=" + key);
+ }
+ return mMeasurementsValues[index];
+ }
+
+ /**
+ * Get the number of measurement map values in this object. Can be used to iterate through
+ * the available measurement maps.
+ *
+ * @see #getMeasurementsKeyAt
+ */
+ public int getMeasurementsKeyCount() {
+ return mMeasurementsKeys.length;
+ }
+
+ /**
+ * Get the key for the measurement map at the given index.
+ * Index must be between 0 and the result
+ * of {@link #getMeasurementsKeyCount getMeasurementsKeyCount()}.
+ *
+ * @see #getMeasurementsKeyCount
+ */
+ public int getMeasurementsKeyAt(int index) {
+ return mMeasurementsKeys[index];
+ }
+
+ /**
+ * Get the index in keys of key.
+ */
+ private static int getIndex(int[] keys, int key) {
+ return Arrays.binarySearch(keys, key);
+ }
+
+ /**
+ * Create an ArrayMap<String,HealthStats> from the given Parcel.
+ */
+ private static ArrayMap<String,HealthStats> createHealthStatsMap(Parcel in) {
+ final int count = in.readInt();
+ final ArrayMap<String,HealthStats> result = new ArrayMap<String,HealthStats>(count);
+ for (int i=0; i<count; i++) {
+ result.put(in.readString(), new HealthStats(in));
+ }
+ return result;
+ }
+
+ /**
+ * Create an ArrayMap<String,T extends Parcelable> from the given Parcel using
+ * the given Parcelable.Creator.
+ */
+ private static <T extends Parcelable> ArrayMap<String,T> createParcelableMap(Parcel in,
+ Parcelable.Creator<T> creator) {
+ final int count = in.readInt();
+ final ArrayMap<String,T> result = new ArrayMap<String,T>(count);
+ for (int i=0; i<count; i++) {
+ result.put(in.readString(), creator.createFromParcel(in));
+ }
+ return result;
+ }
+
+ /**
+ * Create an ArrayMap<String,Long> from the given Parcel.
+ */
+ private static ArrayMap<String,Long> createLongsMap(Parcel in) {
+ final int count = in.readInt();
+ final ArrayMap<String,Long> result = new ArrayMap<String,Long>(count);
+ for (int i=0; i<count; i++) {
+ result.put(in.readString(), in.readLong());
+ }
+ return result;
+ }
+}
+
diff --git a/core/java/android/os/health/HealthStatsParceler.aidl b/core/java/android/os/health/HealthStatsParceler.aidl
new file mode 100644
index 0000000..68c348b
--- /dev/null
+++ b/core/java/android/os/health/HealthStatsParceler.aidl
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+parcelable HealthStatsParceler;
diff --git a/core/java/android/os/health/HealthStatsParceler.java b/core/java/android/os/health/HealthStatsParceler.java
new file mode 100644
index 0000000..28b3694
--- /dev/null
+++ b/core/java/android/os/health/HealthStatsParceler.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.util.ArrayMap;
+
+import java.util.Arrays;
+import java.util.Map;
+
+/**
+ * Class to allow sending the HealthStats through aidl generated glue.
+ *
+ * The alternative would be to send a HealthStats object, which would
+ * require constructing one, and then immediately flattening it. This
+ * saves that step at the cost of doing the extra flattening when
+ * accessed in the same process as the writer.
+ *
+ * The HealthStatsWriter passed in the constructor is retained, so don't
+ * reuse them.
+ * @hide
+ */
+public class HealthStatsParceler implements Parcelable {
+ private HealthStatsWriter mWriter;
+ private HealthStats mHealthStats;
+
+ public static final Parcelable.Creator<HealthStatsParceler> CREATOR
+ = new Parcelable.Creator<HealthStatsParceler>() {
+ public HealthStatsParceler createFromParcel(Parcel in) {
+ return new HealthStatsParceler(in);
+ }
+
+ public HealthStatsParceler[] newArray(int size) {
+ return new HealthStatsParceler[size];
+ }
+ };
+
+ public HealthStatsParceler(HealthStatsWriter writer) {
+ mWriter = writer;
+ }
+
+ public HealthStatsParceler(Parcel in) {
+ mHealthStats = new HealthStats(in);
+ }
+
+ public int describeContents() {
+ return 0;
+ }
+
+ public void writeToParcel(Parcel out, int flags) {
+ // See comment on mWriter declaration above.
+ if (mWriter != null) {
+ mWriter.flattenToParcel(out);
+ } else {
+ throw new RuntimeException("Can not re-parcel HealthStatsParceler that was"
+ + " constructed from a Parcel");
+ }
+ }
+
+ public HealthStats getHealthStats() {
+ if (mWriter != null) {
+ final Parcel parcel = Parcel.obtain();
+ mWriter.flattenToParcel(parcel);
+ parcel.setDataPosition(0);
+ mHealthStats = new HealthStats(parcel);
+ parcel.recycle();
+ }
+
+ return mHealthStats;
+ }
+}
+
diff --git a/core/java/android/os/health/HealthStatsWriter.java b/core/java/android/os/health/HealthStatsWriter.java
new file mode 100644
index 0000000..351836b
--- /dev/null
+++ b/core/java/android/os/health/HealthStatsWriter.java
@@ -0,0 +1,288 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.util.ArrayMap;
+
+import java.util.Map;
+
+/**
+ * Class to write the health stats data into a parcel, so it can then be
+ * retrieved via a {@link HealthStats} object.
+ *
+ * There is an attempt to keep this class as low overhead as possible, for
+ * example storing an int[] and a long[] instead of a TimerStat[].
+ *
+ * @hide
+ */
+public class HealthStatsWriter {
+ private final HealthKeys.Constants mConstants;
+
+ // TimerStat fields
+ private final boolean[] mTimerFields;
+ private final int[] mTimerCounts;
+ private final long[] mTimerTimes;
+
+ // Measurement fields
+ private final boolean[] mMeasurementFields;
+ private final long[] mMeasurementValues;
+
+ // Stats fields
+ private final ArrayMap<String,HealthStatsWriter>[] mStatsValues;
+
+ // Timers fields
+ private final ArrayMap<String,TimerStat>[] mTimersValues;
+
+ // Measurements fields
+ private final ArrayMap<String,Long>[] mMeasurementsValues;
+
+ /**
+ * Construct a HealthStatsWriter object with the given constants.
+ *
+ * The "getDataType()" of the resulting HealthStats object will be the
+ * short name of the java class that the Constants object was initalized
+ * with.
+ */
+ public HealthStatsWriter(HealthKeys.Constants constants) {
+ mConstants = constants;
+
+ // TimerStat
+ final int timerCount = constants.getSize(HealthKeys.TYPE_TIMER);
+ mTimerFields = new boolean[timerCount];
+ mTimerCounts = new int[timerCount];
+ mTimerTimes = new long[timerCount];
+
+ // Measurement
+ final int measurementCount = constants.getSize(HealthKeys.TYPE_MEASUREMENT);
+ mMeasurementFields = new boolean[measurementCount];
+ mMeasurementValues = new long[measurementCount];
+
+ // Stats
+ final int statsCount = constants.getSize(HealthKeys.TYPE_STATS);
+ mStatsValues = new ArrayMap[statsCount];
+
+ // Timers
+ final int timersCount = constants.getSize(HealthKeys.TYPE_TIMERS);
+ mTimersValues = new ArrayMap[timersCount];
+
+ // Measurements
+ final int measurementsCount = constants.getSize(HealthKeys.TYPE_MEASUREMENTS);
+ mMeasurementsValues = new ArrayMap[measurementsCount];
+ }
+
+ /**
+ * Add a timer for the given key.
+ */
+ public void addTimer(int timerId, int count, long time) {
+ final int index = mConstants.getIndex(HealthKeys.TYPE_TIMER, timerId);
+
+ mTimerFields[index] = true;
+ mTimerCounts[index] = count;
+ mTimerTimes[index] = time;
+ }
+
+ /**
+ * Add a measurement for the given key.
+ */
+ public void addMeasurement(int measurementId, long value) {
+ final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENT, measurementId);
+
+ mMeasurementFields[index] = true;
+ mMeasurementValues[index] = value;
+ }
+
+ /**
+ * Add a recursive HealthStats object for the given key and string name. The value
+ * is stored as a HealthStatsWriter until this object is written to a parcel, so
+ * don't attempt to reuse the HealthStatsWriter.
+ *
+ * The value field should not be null.
+ */
+ public void addStats(int key, String name, HealthStatsWriter value) {
+ final int index = mConstants.getIndex(HealthKeys.TYPE_STATS, key);
+
+ ArrayMap<String,HealthStatsWriter> map = mStatsValues[index];
+ if (map == null) {
+ map = mStatsValues[index] = new ArrayMap<String,HealthStatsWriter>(1);
+ }
+ map.put(name, value);
+ }
+
+ /**
+ * Add a TimerStat for the given key and string name.
+ *
+ * The value field should not be null.
+ */
+ public void addTimers(int key, String name, TimerStat value) {
+ final int index = mConstants.getIndex(HealthKeys.TYPE_TIMERS, key);
+
+ ArrayMap<String,TimerStat> map = mTimersValues[index];
+ if (map == null) {
+ map = mTimersValues[index] = new ArrayMap<String,TimerStat>(1);
+ }
+ map.put(name, value);
+ }
+
+ /**
+ * Add a measurement for the given key and string name.
+ */
+ public void addMeasurements(int key, String name, long value) {
+ final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENTS, key);
+
+ ArrayMap<String,Long> map = mMeasurementsValues[index];
+ if (map == null) {
+ map = mMeasurementsValues[index] = new ArrayMap<String,Long>(1);
+ }
+ map.put(name, value);
+ }
+
+ /**
+ * Flattens the data in this HealthStatsWriter to the Parcel format
+ * that can be unparceled into a HealthStat.
+ * @more
+ * (Called flattenToParcel because this HealthStatsWriter itself is
+ * not parcelable and we don't flatten all the business about the
+ * HealthKeys.Constants, only the values that were actually supplied)
+ */
+ public void flattenToParcel(Parcel out) {
+ int[] keys;
+
+ // Header fields
+ out.writeString(mConstants.getDataType());
+
+ // TimerStat fields
+ out.writeInt(countBooleanArray(mTimerFields));
+ keys = mConstants.getKeys(HealthKeys.TYPE_TIMER);
+ for (int i=0; i<keys.length; i++) {
+ if (mTimerFields[i]) {
+ out.writeInt(keys[i]);
+ out.writeInt(mTimerCounts[i]);
+ out.writeLong(mTimerTimes[i]);
+ }
+ }
+
+ // Measurement fields
+ out.writeInt(countBooleanArray(mMeasurementFields));
+ keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENT);
+ for (int i=0; i<keys.length; i++) {
+ if (mMeasurementFields[i]) {
+ out.writeInt(keys[i]);
+ out.writeLong(mMeasurementValues[i]);
+ }
+ }
+
+ // Stats
+ out.writeInt(countObjectArray(mStatsValues));
+ keys = mConstants.getKeys(HealthKeys.TYPE_STATS);
+ for (int i=0; i<keys.length; i++) {
+ if (mStatsValues[i] != null) {
+ out.writeInt(keys[i]);
+ writeHealthStatsWriterMap(out, mStatsValues[i]);
+ }
+ }
+
+ // Timers
+ out.writeInt(countObjectArray(mTimersValues));
+ keys = mConstants.getKeys(HealthKeys.TYPE_TIMERS);
+ for (int i=0; i<keys.length; i++) {
+ if (mTimersValues[i] != null) {
+ out.writeInt(keys[i]);
+ writeParcelableMap(out, mTimersValues[i]);
+ }
+ }
+
+ // Measurements
+ out.writeInt(countObjectArray(mMeasurementsValues));
+ keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENTS);
+ for (int i=0; i<keys.length; i++) {
+ if (mMeasurementsValues[i] != null) {
+ out.writeInt(keys[i]);
+ writeLongsMap(out, mMeasurementsValues[i]);
+ }
+ }
+ }
+
+ /**
+ * Count how many of the fields have been set.
+ */
+ private static int countBooleanArray(boolean[] fields) {
+ int count = 0;
+ final int N = fields.length;
+ for (int i=0; i<N; i++) {
+ if (fields[i]) {
+ count++;
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Count how many of the fields have been set.
+ */
+ private static <T extends Object> int countObjectArray(T[] fields) {
+ int count = 0;
+ final int N = fields.length;
+ for (int i=0; i<N; i++) {
+ if (fields[i] != null) {
+ count++;
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Write a map of String to HealthStatsWriter to the Parcel.
+ */
+ private static void writeHealthStatsWriterMap(Parcel out,
+ ArrayMap<String,HealthStatsWriter> map) {
+ final int N = map.size();
+ out.writeInt(N);
+ for (int i=0; i<N; i++) {
+ out.writeString(map.keyAt(i));
+ map.valueAt(i).flattenToParcel(out);
+ }
+ }
+
+ /**
+ * Write a map of String to Parcelables to the Parcel.
+ */
+ private static <T extends Parcelable> void writeParcelableMap(Parcel out,
+ ArrayMap<String,T> map) {
+ final int N = map.size();
+ out.writeInt(N);
+ for (int i=0; i<N; i++) {
+ out.writeString(map.keyAt(i));
+ map.valueAt(i).writeToParcel(out, 0);
+ }
+ }
+
+ /**
+ * Write a map of String to Longs to the Parcel.
+ */
+ private static void writeLongsMap(Parcel out, ArrayMap<String,Long> map) {
+ final int N = map.size();
+ out.writeInt(N);
+ for (int i=0; i<N; i++) {
+ out.writeString(map.keyAt(i));
+ out.writeLong(map.valueAt(i));
+ }
+ }
+}
+
+
diff --git a/core/java/android/os/health/PackageHealthStats.java b/core/java/android/os/health/PackageHealthStats.java
new file mode 100644
index 0000000..2c30d5f
--- /dev/null
+++ b/core/java/android/os/health/PackageHealthStats.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+/**
+ * Keys for {@link HealthStats} returned from
+ * {@link HealthStats#getStats(int) HealthStats.getStats(int)} with the
+ * {@link UidHealthStats#STATS_PACKAGES UidHealthStats.STATS_PACKAGES} key.
+ */
+public final class PackageHealthStats {
+
+ private PackageHealthStats() {
+ }
+
+ /**
+ * Key for a HealthStats with {@link ServiceHealthStats} keys for each of the
+ * services defined in this apk.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_STATS)
+ public static final int STATS_SERVICES = HealthKeys.BASE_PACKAGE + 1;
+
+ /**
+ * Key for a map of the number of times that a package's wakeup alarms have fired
+ * while the device was on battery.
+ *
+ * @see android.app.AlarmManager.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENTS)
+ public static final int MEASUREMENTS_WAKEUP_ALARMS_COUNT = HealthKeys.BASE_PACKAGE + 2;
+
+ /**
+ * @hide
+ */
+ public static final HealthKeys.Constants CONSTANTS
+ = new HealthKeys.Constants(PackageHealthStats.class);
+}
diff --git a/core/java/android/os/health/PidHealthStats.java b/core/java/android/os/health/PidHealthStats.java
new file mode 100644
index 0000000..fe3c02c
--- /dev/null
+++ b/core/java/android/os/health/PidHealthStats.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+/**
+ * Keys for {@link HealthStats} returned from
+ * {@link HealthStats#getStats(int) HealthStats.getStats(int)} with the
+ * {@link UidHealthStats#STATS_PIDS UidHealthStats.STATS_PIDS} key.
+ */
+public final class PidHealthStats {
+
+ private PidHealthStats() {
+ }
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WAKE_NESTING_COUNT = HealthKeys.BASE_PID + 1;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WAKE_SUM_MS = HealthKeys.BASE_PID + 2;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WAKE_START_MS = HealthKeys.BASE_PID + 3;
+
+ /**
+ * @hide
+ */
+ public static final HealthKeys.Constants CONSTANTS = new HealthKeys.Constants(PidHealthStats.class);
+}
diff --git a/core/java/android/os/health/ProcessHealthStats.java b/core/java/android/os/health/ProcessHealthStats.java
new file mode 100644
index 0000000..e004ecb
--- /dev/null
+++ b/core/java/android/os/health/ProcessHealthStats.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+/**
+ * Keys for {@link HealthStats} returned from
+ * {@link HealthStats#getStats(int) HealthStats.getStats(int)} with the
+ * {@link UidHealthStats#STATS_PROCESSES UidHealthStats.STATS_PROCESSES} key.
+ */
+public final class ProcessHealthStats {
+
+ private ProcessHealthStats() {
+ }
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_USER_TIME_MS = HealthKeys.BASE_PROCESS + 1;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_SYSTEM_TIME_MS = HealthKeys.BASE_PROCESS + 2;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_STARTS_COUNT = HealthKeys.BASE_PROCESS + 3;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_CRASHES_COUNT = HealthKeys.BASE_PROCESS + 4;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_ANR_COUNT = HealthKeys.BASE_PROCESS + 5;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_FOREGROUND_MS = HealthKeys.BASE_PROCESS + 6;
+
+ /**
+ * @hide
+ */
+ public static final HealthKeys.Constants CONSTANTS = new HealthKeys.Constants(ProcessHealthStats.class);
+}
diff --git a/core/java/android/os/health/ServiceHealthStats.java b/core/java/android/os/health/ServiceHealthStats.java
new file mode 100644
index 0000000..802ad31
--- /dev/null
+++ b/core/java/android/os/health/ServiceHealthStats.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+/**
+ * Keys for {@link HealthStats} returned from
+ * {@link HealthStats#getStats(int) HealthStats.getStats(int)} with the
+ * {@link PackageHealthStats#STATS_SERVICES PackageHealthStats.STATS_SERVICES} key.
+ */
+public final class ServiceHealthStats {
+
+ private ServiceHealthStats() {
+ }
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_START_SERVICE_COUNT = HealthKeys.BASE_SERVICE + 1;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_LAUNCH_COUNT = HealthKeys.BASE_SERVICE + 2;
+
+ /**
+ * @hide
+ */
+ public static final HealthKeys.Constants CONSTANTS
+ = new HealthKeys.Constants(ServiceHealthStats.class);
+}
diff --git a/core/java/android/os/health/SystemHealthManager.java b/core/java/android/os/health/SystemHealthManager.java
new file mode 100644
index 0000000..520e84e
--- /dev/null
+++ b/core/java/android/os/health/SystemHealthManager.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+import android.content.Context;
+import android.os.BatteryStats;
+import android.os.Process;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+
+import com.android.internal.app.IBatteryStats;
+
+/**
+ * Provides access to data about how various system resources are used by applications.
+ * @more
+ * <b>Battery Usage</b><br>
+ * The statistics related to power (battery) usage are recorded since the device
+ * was last unplugged. It is expected that applications schedule more work to do
+ * while the device is plugged in (e.g. using {@link android.app.job.JobScheduler
+ * JobScheduler}), and while that can affect charging rates, it is still preferable
+ * to actually draining the battery.
+ */
+public class SystemHealthManager {
+ private final IBatteryStats mBatteryStats;
+
+ /**
+ * Construct a new SystemHealthManager object.
+ * @hide
+ */
+ public SystemHealthManager() {
+ mBatteryStats = IBatteryStats.Stub.asInterface(
+ ServiceManager.getService(BatteryStats.SERVICE_NAME));
+ }
+
+ /**
+ * Obtain a SystemHealthManager object for the supplied context.
+ */
+ public static SystemHealthManager from(Context context) {
+ return (SystemHealthManager)context.getSystemService(Context.SYSTEM_HEALTH_SERVICE);
+ }
+
+ /**
+ * Return a {@link HealthStats} object containing a snapshot of system health
+ * metrics for the given uid (user-id, which in usually corresponds to application).
+ * @more
+ *
+ * An application must hold the {@link android.Manifest.permission#BATTERY_STATS
+ * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats
+ * other than its own.
+ *
+ * @param uid User ID for a given application.
+ * @return A {@link HealthStats} object containing the metrics for the requested
+ * application. The keys for this HealthStats object will be from the {@link UidHealthStats}
+ * class.
+ * @see Process#myUid()
+ */
+ public HealthStats takeUidSnapshot(int uid) {
+ try {
+ final HealthStatsParceler parceler = mBatteryStats.takeUidSnapshot(uid);
+ return parceler.getHealthStats();
+ } catch (RemoteException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+ /**
+ * Return a {@link HealthStats} object containing a snapshot of system health
+ * metrics for the application calling this API. This method is the same as calling
+ * {@code takeUidSnapshot(Process.myUid())}.
+ *
+ * @return A {@link HealthStats} object containing the metrics for this application. The keys
+ * for this HealthStats object will be from the {@link UidHealthStats} class.
+ */
+ public HealthStats takeMyUidSnapshot() {
+ return takeUidSnapshot(Process.myUid());
+ }
+
+ /**
+ * Return a {@link HealthStats} object containing a snapshot of system health
+ * metrics for the given uids (user-id, which in usually corresponds to application).
+ * @more
+ *
+ * An application must hold the {@link android.Manifest.permission#BATTERY_STATS
+ * android.permission.BATTERY_STATS} permission in order to retrieve any HealthStats
+ * other than its own.
+ *
+ * @param uids An array of User IDs to retrieve.
+ * @return An array of {@link HealthStats} objects containing the metrics for each of
+ * the requested uids. The keys for this HealthStats object will be from the
+ * {@link UidHealthStats} class.
+ */
+ public HealthStats[] takeUidSnapshots(int[] uids) {
+ try {
+ final HealthStatsParceler[] parcelers = mBatteryStats.takeUidSnapshots(uids);
+ final HealthStats[] results = new HealthStats[uids.length];
+ final int N = uids.length;
+ for (int i=0; i<N; i++) {
+ results[i] = parcelers[i].getHealthStats();
+ }
+ return results;
+ } catch (RemoteException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
+}
+
diff --git a/core/java/android/os/health/TimerStat.java b/core/java/android/os/health/TimerStat.java
new file mode 100644
index 0000000..fc51b60
--- /dev/null
+++ b/core/java/android/os/health/TimerStat.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+/**
+ * A TimerStat object stores a count and a time.
+ *
+ * @more
+ * When possible, the other APIs in this package avoid requiring a TimerStat
+ * object to be constructed, even internally, but the getTimers method on
+ * {@link android.os.health.HealthStats} does require TimerStat objects.
+ */
+public class TimerStat implements Parcelable {
+ private int mCount;
+ private long mTime;
+
+ /**
+ * The CREATOR instance for use by aidl Binder interfaces.
+ */
+ public static final Parcelable.Creator<TimerStat> CREATOR
+ = new Parcelable.Creator<TimerStat>() {
+ public TimerStat createFromParcel(Parcel in) {
+ return new TimerStat(in);
+ }
+
+ public TimerStat[] newArray(int size) {
+ return new TimerStat[size];
+ }
+ };
+
+ /**
+ * Construct an empty TimerStat object with the count and time set to 0.
+ */
+ public TimerStat() {
+ }
+
+ /**
+ * Construct a TimerStat object with the supplied count and time fields.
+ *
+ * @param count The count
+ * @param time The time
+ */
+ public TimerStat(int count, long time) {
+ mCount = count;
+ mTime = time;
+ }
+
+ /**
+ * Construct a TimerStat object reading the values from a {@link android.os.Parcel Parcel}
+ * object.
+ */
+ public TimerStat(Parcel in) {
+ mCount = in.readInt();
+ mTime = in.readLong();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ public int describeContents() {
+ return 0;
+ }
+
+ /**
+ * Write this TimerStat object to a parcel.
+ */
+ public void writeToParcel(Parcel out, int flags) {
+ out.writeInt(mCount);
+ out.writeLong(mTime);
+ }
+
+ /**
+ * Set the count for this timer.
+ */
+ public void setCount(int count) {
+ mCount = count;
+ }
+
+ /**
+ * Get the count for this timer.
+ */
+ public int getCount() {
+ return mCount;
+ }
+
+ /**
+ * Set the time for this timer.
+ */
+ public void setTime(long time) {
+ mTime = time;
+ }
+
+ /**
+ * Get the time for this timer.
+ */
+ public long getTime() {
+ return mTime;
+ }
+}
diff --git a/core/java/android/os/health/UidHealthStats.java b/core/java/android/os/health/UidHealthStats.java
new file mode 100644
index 0000000..c7d257f
--- /dev/null
+++ b/core/java/android/os/health/UidHealthStats.java
@@ -0,0 +1,280 @@
+/*
+ * Copyright (C) 2016 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.health;
+
+/**
+ * Keys for {@link HealthStats} returned from
+ * {@link SystemHealthManager#takeUidSnapshot(int) SystemHealthManager.takeUidSnapshot(int)},
+ * {@link SystemHealthManager#takeMyUidSnapshot() SystemHealthManager.takeMyUidSnapshot()}, and
+ * {@link SystemHealthManager#takeUidSnapshots(int[]) SystemHealthManager.takeUidSnapshots(int[])}.
+ */
+public final class UidHealthStats {
+
+ private UidHealthStats() {
+ }
+
+ /**
+ * How many milliseconds this statistics report covers in wall-clock time while the
+ * device was on battery including both screen-on and screen-off time.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_REALTIME_BATTERY_MS = HealthKeys.BASE_UID + 1;
+
+ /**
+ * How many milliseconds this statistics report covers that the CPU was running while the
+ * device was on battery including both screen-on and screen-off time.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_UPTIME_BATTERY_MS = HealthKeys.BASE_UID + 2;
+
+ /**
+ * How many milliseconds this statistics report covers in wall-clock time while the
+ * device was on battery including both screen-on and screen-off time.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_REALTIME_SCREEN_OFF_BATTERY_MS = HealthKeys.BASE_UID + 3;
+
+ /**
+ * How many milliseconds this statistics report covers that the CPU was running while the
+ * device was on battery including both screen-on and screen-off time.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_UPTIME_SCREEN_OFF_BATTERY_MS = HealthKeys.BASE_UID + 4;
+
+ /**
+ * Key for a TimerStat for the times a
+ * {@link android.os.PowerManager#FULL_WAKE_LOCK full wake lock}
+ * was acquired for this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
+ public static final int TIMERS_WAKELOCKS_FULL = HealthKeys.BASE_UID + 5;
+
+ /**
+ * Key for a TimerStat for the times a
+ * {@link android.os.PowerManager#PARTIAL_WAKE_LOCK full wake lock}
+ * was acquired for this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
+ public static final int TIMERS_WAKELOCKS_PARTIAL = HealthKeys.BASE_UID + 6;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
+ public static final int TIMERS_WAKELOCKS_WINDOW = HealthKeys.BASE_UID + 7;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
+ public static final int TIMERS_WAKELOCKS_DRAW = HealthKeys.BASE_UID + 8;
+
+ /**
+ * Key for a map of Timers for the sync adapter syncs that were done for
+ * this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
+ public static final int TIMERS_SYNCS = HealthKeys.BASE_UID + 9;
+
+ /**
+ * Key for a map of Timers for the {@link android.app.job.JobScheduler} jobs for
+ * this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
+ public static final int TIMERS_JOBS = HealthKeys.BASE_UID + 10;
+
+ /**
+ * Key for a timer for the applications use of the GPS sensor.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_GPS_SENSOR = HealthKeys.BASE_UID + 11;
+
+ /**
+ * Key for a map of the sensor usage for this uid. The keys are a
+ * string representation of the handle for the sensor.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMERS)
+ public static final int TIMERS_SENSORS = HealthKeys.BASE_UID + 12;
+
+ /**
+ * Key for a HealthStats with {@link PidHealthStats} keys for each of the
+ * currently running processes for this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_STATS)
+ public static final int STATS_PIDS = HealthKeys.BASE_UID + 13;
+
+ /**
+ * Key for a HealthStats with {@link ProcessHealthStats} keys for each of the
+ * named processes for this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_STATS)
+ public static final int STATS_PROCESSES = HealthKeys.BASE_UID + 14;
+
+ /**
+ * Key for a HealthStats with {@link PackageHealthStats} keys for each of the
+ * APKs that share this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_STATS)
+ public static final int STATS_PACKAGES = HealthKeys.BASE_UID + 15;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_IDLE_MS = HealthKeys.BASE_UID + 16;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_RX_MS = HealthKeys.BASE_UID + 17;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_TX_MS = HealthKeys.BASE_UID + 18;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_POWER_MAMS = HealthKeys.BASE_UID + 19;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_IDLE_MS = HealthKeys.BASE_UID + 20;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_RX_MS = HealthKeys.BASE_UID + 21;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_TX_MS = HealthKeys.BASE_UID + 22;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_POWER_MAMS = HealthKeys.BASE_UID + 23;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_IDLE_MS = HealthKeys.BASE_UID + 24;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_RX_MS = HealthKeys.BASE_UID + 25;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_TX_MS = HealthKeys.BASE_UID + 26;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_POWER_MAMS = HealthKeys.BASE_UID + 27;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_RUNNING_MS = HealthKeys.BASE_UID + 28;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_FULL_LOCK_MS = HealthKeys.BASE_UID + 29;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_WIFI_SCAN = HealthKeys.BASE_UID + 30;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_MULTICAST_MS = HealthKeys.BASE_UID + 31;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_AUDIO = HealthKeys.BASE_UID + 32;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_VIDEO = HealthKeys.BASE_UID + 33;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_FLASHLIGHT = HealthKeys.BASE_UID + 34;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_CAMERA = HealthKeys.BASE_UID + 35;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_FOREGROUND_ACTIVITY = HealthKeys.BASE_UID + 36;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_BLUETOOTH_SCAN = HealthKeys.BASE_UID + 37;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_PROCESS_STATE_TOP_MS = HealthKeys.BASE_UID + 38;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_PROCESS_STATE_FOREGROUND_SERVICE_MS = HealthKeys.BASE_UID + 39;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_PROCESS_STATE_TOP_SLEEPING_MS = HealthKeys.BASE_UID + 40;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_PROCESS_STATE_FOREGROUND_MS = HealthKeys.BASE_UID + 41;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_PROCESS_STATE_BACKGROUND_MS = HealthKeys.BASE_UID + 42;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_PROCESS_STATE_CACHED_MS = HealthKeys.BASE_UID + 43;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_VIBRATOR = HealthKeys.BASE_UID + 44;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_OTHER_USER_ACTIVITY_COUNT = HealthKeys.BASE_UID + 45;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BUTTON_USER_ACTIVITY_COUNT = HealthKeys.BASE_UID + 46;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT = HealthKeys.BASE_UID + 47;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_RX_BYTES = HealthKeys.BASE_UID + 48;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_TX_BYTES = HealthKeys.BASE_UID + 49;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_RX_BYTES = HealthKeys.BASE_UID + 50;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_TX_BYTES = HealthKeys.BASE_UID + 51;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_RX_BYTES = HealthKeys.BASE_UID + 52;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_TX_BYTES = HealthKeys.BASE_UID + 53;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_RX_PACKETS = HealthKeys.BASE_UID + 54;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_MOBILE_TX_PACKETS = HealthKeys.BASE_UID + 55;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_RX_PACKETS = HealthKeys.BASE_UID + 56;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_WIFI_TX_PACKETS = HealthKeys.BASE_UID + 57;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_RX_PACKETS = HealthKeys.BASE_UID + 58;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_BLUETOOTH_TX_PACKETS = HealthKeys.BASE_UID + 59;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_TIMER)
+ public static final int TIMER_MOBILE_RADIO_ACTIVE = HealthKeys.BASE_UID + 61;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_USER_CPU_TIME_US = HealthKeys.BASE_UID + 62;
+
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_SYSTEM_CPU_TIME_US = HealthKeys.BASE_UID + 63;
+
+ /**
+ * An estimate of the number of milliamp-microsends used by this uid.
+ */
+ @HealthKeys.Constant(type=HealthKeys.TYPE_MEASUREMENT)
+ public static final int MEASUREMENT_CPU_POWER_MAUS = HealthKeys.BASE_UID + 64;
+
+ /**
+ * @hide
+ */
+ public static final HealthKeys.Constants CONSTANTS = new HealthKeys.Constants(UidHealthStats.class);
+}
+
diff --git a/core/java/android/os/health/package.html b/core/java/android/os/health/package.html
new file mode 100644
index 0000000..3a46a5b
--- /dev/null
+++ b/core/java/android/os/health/package.html
@@ -0,0 +1,39 @@
+<html>
+<body>
+
+The android.os.health package contains a set of classes to provide data
+to track the system resources of applications.
+<p>
+Applications running in the background are responsible for a significant amount
+of battery usage on a typical android device. There are several things that
+applications can do in order to reduce their impact. For example, by using
+{@link android.app.job.JobScheduler JobScheduler}, an application does not need
+to independently monitor whether the network is available, whether the device is
+plugged in, etc. In addition to being simpler to use, the application's
+services are only started when the required conditions have been met. But even
+when using the appropriate helper APIs, applications still can reduce their
+footprint. This package provides more insight into what is going on behind the
+scenes when an application is running.
+<p>
+Application data is tracked by which user id (uid) is using particular
+resources. A snapshot of an application's measurements can be taken with the
+{@link android.os.health.SystemHealthManager#takeMyUidSnapshot() SystemHealth.takeMyUidSnapshot()}
+method. The {@link android.os.health.HealthStats} object returned contains the
+statistics.
+<p>
+<b>HealthStats</b><br>
+In order to be returned efficiently, the {@link android.os.health.HealthStats}
+class uses a set of int keys to identify the data returned. The
+{@link android.os.health.UidHealthStats}, {@link android.os.health.PidHealthStats},
+{@link android.os.health.PackageHealthStats} , {@link android.os.health.ProcessHealthStats},
+and {@link android.os.health.ServiceHealthStats} classes provide those constants.
+Each {@link android.os.health.HealthStats} object will be associated with
+exactly one of those classes. The object returned from
+{@link android.os.health.SystemHealthManager#takeMyUidSnapshot() SystemHealth.takeMyUidSnapshot()}
+will be using the {@link android.os.health.UidHealthStats} keys, as it contains all
+of the data available for that uid.
+
+
+</body>
+</html>
+
diff --git a/core/java/com/android/internal/app/IBatteryStats.aidl b/core/java/com/android/internal/app/IBatteryStats.aidl
index 74fe94f..8e38c5a 100644
--- a/core/java/com/android/internal/app/IBatteryStats.aidl
+++ b/core/java/com/android/internal/app/IBatteryStats.aidl
@@ -20,6 +20,7 @@
import android.os.ParcelFileDescriptor;
import android.os.WorkSource;
+import android.os.health.HealthStatsParceler;
import android.telephony.DataConnectionRealTimeInfo;
import android.telephony.SignalStrength;
@@ -125,4 +126,7 @@
void noteBleScanStarted(in WorkSource ws);
void noteBleScanStopped(in WorkSource ws);
void noteResetBleScan();
+
+ HealthStatsParceler takeUidSnapshot(int uid);
+ HealthStatsParceler[] takeUidSnapshots(in int[] uid);
}
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index 60c9e14..c484121 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -5300,6 +5300,12 @@
}
@Override
+ public Timer getProcessStateTimer(int state) {
+ if (state < 0 || state >= NUM_PROCESS_STATE) return null;
+ return mProcessStateTimer[state];
+ }
+
+ @Override
public Timer getVibratorOnTimer() {
return mVibratorOnTimer;
}
diff --git a/services/core/java/com/android/server/am/BatteryStatsService.java b/services/core/java/com/android/server/am/BatteryStatsService.java
index 28882de..71a1f97 100644
--- a/services/core/java/com/android/server/am/BatteryStatsService.java
+++ b/services/core/java/com/android/server/am/BatteryStatsService.java
@@ -32,6 +32,7 @@
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFormatException;
+import android.os.PowerManager;
import android.os.PowerManagerInternal;
import android.os.Process;
import android.os.RemoteException;
@@ -39,6 +40,9 @@
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.WorkSource;
+import android.os.health.HealthStatsParceler;
+import android.os.health.HealthStatsWriter;
+import android.os.health.UidHealthStats;
import android.telephony.DataConnectionRealTimeInfo;
import android.telephony.ModemActivityInfo;
import android.telephony.SignalStrength;
@@ -65,7 +69,9 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
import java.util.List;
+import java.util.Map;
/**
* All information we are collecting about things that can happen that impact
@@ -1419,4 +1425,88 @@
}
}
}
+
+ /**
+ * Gets a snapshot of the system health for a particular uid.
+ */
+ @Override
+ public HealthStatsParceler takeUidSnapshot(int requestUid) {
+ if (requestUid != Binder.getCallingUid()) {
+ mContext.enforceCallingOrSelfPermission(
+ android.Manifest.permission.BATTERY_STATS, null);
+ }
+ long ident = Binder.clearCallingIdentity();
+ try {
+ updateExternalStats("get-health-stats-for-uid",
+ BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
+ synchronized (mStats) {
+ return getHealthStatsForUidLocked(requestUid);
+ }
+ } catch (Exception ex) {
+ Slog.d(TAG, "Crashed while writing for takeUidSnapshot(" + requestUid + ")", ex);
+ throw ex;
+ } finally {
+ Binder.restoreCallingIdentity(ident);
+ }
+ }
+
+ /**
+ * Gets a snapshot of the system health for a number of uids.
+ */
+ @Override
+ public HealthStatsParceler[] takeUidSnapshots(int[] requestUids) {
+ if (!onlyCaller(requestUids)) {
+ mContext.enforceCallingOrSelfPermission(
+ android.Manifest.permission.BATTERY_STATS, null);
+ }
+ long ident = Binder.clearCallingIdentity();
+ int i=-1;
+ try {
+ updateExternalStats("get-health-stats-for-uids",
+ BatteryStatsImpl.ExternalStatsSync.UPDATE_ALL);
+ synchronized (mStats) {
+ final int N = requestUids.length;
+ final HealthStatsParceler[] results = new HealthStatsParceler[N];
+ for (i=0; i<N; i++) {
+ results[i] = getHealthStatsForUidLocked(requestUids[i]);
+ }
+ return results;
+ }
+ } catch (Exception ex) {
+ Slog.d(TAG, "Crashed while writing for takeUidSnapshots("
+ + Arrays.toString(requestUids) + ") i=" + i, ex);
+ throw ex;
+ } finally {
+ Binder.restoreCallingIdentity(ident);
+ }
+ }
+
+ /**
+ * Returns whether the Binder.getCallingUid is the only thing in requestUids.
+ */
+ private static boolean onlyCaller(int[] requestUids) {
+ final int caller = Binder.getCallingUid();
+ final int N = requestUids.length;
+ for (int i=0; i<N; i++) {
+ if (requestUids[i] != caller) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Gets a HealthStatsParceler for the given uid. You should probably call
+ * updateExternalStats first.
+ */
+ HealthStatsParceler getHealthStatsForUidLocked(int requestUid) {
+ final HealthStatsBatteryStatsWriter writer = new HealthStatsBatteryStatsWriter();
+ final HealthStatsWriter uidWriter = new HealthStatsWriter(UidHealthStats.CONSTANTS);
+ final BatteryStats.Uid uid = mStats.getUidStats().get(requestUid);
+ if (uid != null) {
+ writer.writeUid(uidWriter, mStats, uid);
+ }
+ return new HealthStatsParceler(uidWriter);
+ }
+
}
diff --git a/services/core/java/com/android/server/am/HealthStatsBatteryStatsWriter.java b/services/core/java/com/android/server/am/HealthStatsBatteryStatsWriter.java
new file mode 100644
index 0000000..39c6ce6
--- /dev/null
+++ b/services/core/java/com/android/server/am/HealthStatsBatteryStatsWriter.java
@@ -0,0 +1,473 @@
+/*
+ * Copyright (C) 2016 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 com.android.server.am;
+
+import android.os.BatteryStats;
+import static android.os.BatteryStats.STATS_SINCE_UNPLUGGED;
+import android.os.PowerManager;
+import android.os.SystemClock;
+import android.os.health.HealthKeys;
+import android.os.health.HealthStatsParceler;
+import android.os.health.HealthStatsWriter;
+import android.os.health.PackageHealthStats;
+import android.os.health.ProcessHealthStats;
+import android.os.health.PidHealthStats;
+import android.os.health.ServiceHealthStats;
+import android.os.health.TimerStat;
+import android.os.health.UidHealthStats;
+import android.util.SparseArray;
+
+import java.util.Map;
+
+public class HealthStatsBatteryStatsWriter {
+
+ private final long mNowRealtime;
+ private final long mNowUptime;
+
+ public HealthStatsBatteryStatsWriter() {
+ mNowRealtime = SystemClock.elapsedRealtime();
+ mNowUptime = SystemClock.uptimeMillis();
+ }
+
+ /**
+ * Writes the contents of a BatteryStats.Uid into a HealthStatsWriter.
+ */
+ public void writeUid(HealthStatsWriter uidWriter, BatteryStats bs, BatteryStats.Uid uid) {
+ int N;
+ BatteryStats.Timer timer;
+ SparseArray<? extends BatteryStats.Uid.Sensor> sensors;
+ SparseArray<? extends BatteryStats.Uid.Pid> pids;
+ BatteryStats.ControllerActivityCounter controller;
+ long sum;
+
+ //
+ // It's a little odd for these first four to be here but it's not the end of the
+ // world. It would be easy enough to duplicate them somewhere else if this API
+ // grows.
+ //
+
+ // MEASUREMENT_REALTIME_BATTERY_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_REALTIME_BATTERY_MS,
+ bs.computeBatteryRealtime(mNowRealtime*1000, STATS_SINCE_UNPLUGGED)/1000);
+
+ // MEASUREMENT_UPTIME_BATTERY_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_UPTIME_BATTERY_MS,
+ bs.computeBatteryUptime(mNowUptime*1000, STATS_SINCE_UNPLUGGED)/1000);
+
+ // MEASUREMENT_REALTIME_SCREEN_OFF_BATTERY_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_REALTIME_SCREEN_OFF_BATTERY_MS,
+ bs.computeBatteryScreenOffRealtime(mNowRealtime*1000, STATS_SINCE_UNPLUGGED)/1000);
+
+ // MEASUREMENT_UPTIME_SCREEN_OFF_BATTERY_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_UPTIME_SCREEN_OFF_BATTERY_MS,
+ bs.computeBatteryScreenOffUptime(mNowUptime*1000, STATS_SINCE_UNPLUGGED)/1000);
+
+ //
+ // Now on to the real per-uid stats...
+ //
+
+ for (final Map.Entry<String,? extends BatteryStats.Uid.Wakelock> entry:
+ uid.getWakelockStats().entrySet()) {
+ final String key = entry.getKey();
+ final BatteryStats.Uid.Wakelock wakelock = entry.getValue();
+
+ // TIMERS_WAKELOCKS_FULL
+ timer = wakelock.getWakeTime(BatteryStats.WAKE_TYPE_FULL);
+ addTimers(uidWriter, UidHealthStats.TIMERS_WAKELOCKS_FULL, key, timer);
+
+ // TIMERS_WAKELOCKS_PARTIAL
+ timer = wakelock.getWakeTime(BatteryStats.WAKE_TYPE_PARTIAL);
+ addTimers(uidWriter, UidHealthStats.TIMERS_WAKELOCKS_PARTIAL, key, timer);
+
+ // TIMERS_WAKELOCKS_WINDOW
+ timer = wakelock.getWakeTime(BatteryStats.WAKE_TYPE_WINDOW);
+ addTimers(uidWriter, UidHealthStats.TIMERS_WAKELOCKS_WINDOW, key, timer);
+
+ // TIMERS_WAKELOCKS_DRAW
+ timer = wakelock.getWakeTime(BatteryStats.WAKE_TYPE_DRAW);
+ addTimers(uidWriter, UidHealthStats.TIMERS_WAKELOCKS_DRAW, key, timer);
+ }
+
+ // TIMERS_SYNCS
+ for (final Map.Entry<String,? extends BatteryStats.Timer> entry:
+ uid.getSyncStats().entrySet()) {
+ addTimers(uidWriter, UidHealthStats.TIMERS_SYNCS, entry.getKey(), entry.getValue());
+ }
+
+ // TIMERS_JOBS
+ for (final Map.Entry<String,? extends BatteryStats.Timer> entry:
+ uid.getJobStats().entrySet()) {
+ addTimers(uidWriter, UidHealthStats.TIMERS_JOBS, entry.getKey(), entry.getValue());
+ }
+
+ // TIMERS_SENSORS
+ sensors = uid.getSensorStats();
+ N = sensors.size();
+ for (int i=0; i<N; i++) {
+ int sensorId = sensors.keyAt(i);
+ // Battery Stats stores the GPS sensors with a bogus key in this API. Pull it out
+ // as a separate metric here so as to not expose that in the API.
+ if (sensorId == BatteryStats.Uid.Sensor.GPS) {
+ addTimer(uidWriter, UidHealthStats.TIMER_GPS_SENSOR, sensors.valueAt(i).getSensorTime());
+ } else {
+ addTimers(uidWriter, UidHealthStats.TIMERS_SENSORS, Integer.toString(sensorId),
+ sensors.valueAt(i).getSensorTime());
+ }
+ }
+
+ // STATS_PIDS
+ pids = uid.getPidStats();
+ N = sensors.size();
+ for (int i=0; i<N; i++) {
+ final HealthStatsWriter writer = new HealthStatsWriter(PidHealthStats.CONSTANTS);
+ writePid(writer, pids.valueAt(i));
+ uidWriter.addStats(UidHealthStats.STATS_PIDS, Integer.toString(pids.keyAt(i)), writer);
+ }
+
+ // STATS_PROCESSES
+ for (final Map.Entry<String,? extends BatteryStats.Uid.Proc> entry:
+ uid.getProcessStats().entrySet()) {
+ final HealthStatsWriter writer = new HealthStatsWriter(ProcessHealthStats.CONSTANTS);
+ writeProc(writer, entry.getValue());
+ uidWriter.addStats(UidHealthStats.STATS_PROCESSES, entry.getKey(), writer);
+ }
+
+ // STATS_PACKAGES
+ for (final Map.Entry<String,? extends BatteryStats.Uid.Pkg> entry:
+ uid.getPackageStats().entrySet()) {
+ final HealthStatsWriter writer = new HealthStatsWriter(PackageHealthStats.CONSTANTS);
+ writePkg(writer, entry.getValue());
+ uidWriter.addStats(UidHealthStats.STATS_PACKAGES, entry.getKey(), writer);
+ }
+
+ controller = uid.getWifiControllerActivity();
+ if (controller != null) {
+ // MEASUREMENT_WIFI_IDLE_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_IDLE_MS,
+ controller.getIdleTimeCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ // MEASUREMENT_WIFI_RX_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_RX_MS,
+ controller.getRxTimeCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ // MEASUREMENT_WIFI_TX_MS
+ sum = 0;
+ for (final BatteryStats.LongCounter counter: controller.getTxTimeCounters()) {
+ sum += counter.getCountLocked(STATS_SINCE_UNPLUGGED);
+ }
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_TX_MS, sum);
+ // MEASUREMENT_WIFI_POWER_MAMS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_POWER_MAMS,
+ controller.getPowerCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ }
+
+ controller = uid.getBluetoothControllerActivity();
+ if (controller != null) {
+ // MEASUREMENT_BLUETOOTH_IDLE_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_IDLE_MS,
+ controller.getIdleTimeCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ // MEASUREMENT_BLUETOOTH_RX_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_RX_MS,
+ controller.getRxTimeCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ // MEASUREMENT_BLUETOOTH_TX_MS
+ sum = 0;
+ for (final BatteryStats.LongCounter counter: controller.getTxTimeCounters()) {
+ sum += counter.getCountLocked(STATS_SINCE_UNPLUGGED);
+ }
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_TX_MS, sum);
+ // MEASUREMENT_BLUETOOTH_POWER_MAMS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_POWER_MAMS,
+ controller.getPowerCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ }
+
+ controller = uid.getModemControllerActivity();
+ if (controller != null) {
+ // MEASUREMENT_MOBILE_IDLE_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_IDLE_MS,
+ controller.getIdleTimeCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ // MEASUREMENT_MOBILE_RX_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_RX_MS,
+ controller.getRxTimeCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ // MEASUREMENT_MOBILE_TX_MS
+ sum = 0;
+ for (final BatteryStats.LongCounter counter: controller.getTxTimeCounters()) {
+ sum += counter.getCountLocked(STATS_SINCE_UNPLUGGED);
+ }
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_TX_MS, sum);
+ // MEASUREMENT_MOBILE_POWER_MAMS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_POWER_MAMS,
+ controller.getPowerCounter().getCountLocked(STATS_SINCE_UNPLUGGED));
+ }
+
+ // MEASUREMENT_WIFI_RUNNING_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_RUNNING_MS,
+ uid.getWifiRunningTime(mNowRealtime, STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_WIFI_FULL_LOCK_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_FULL_LOCK_MS,
+ uid.getFullWifiLockTime(mNowRealtime, STATS_SINCE_UNPLUGGED));
+
+ // TIMER_WIFI_SCAN
+ uidWriter.addTimer(UidHealthStats.TIMER_WIFI_SCAN,
+ uid.getWifiScanCount(STATS_SINCE_UNPLUGGED),
+ uid.getWifiScanTime(mNowRealtime, STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_WIFI_MULTICAST_MS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_MULTICAST_MS,
+ uid.getWifiMulticastTime(mNowRealtime, STATS_SINCE_UNPLUGGED));
+
+ // TIMER_AUDIO
+ addTimer(uidWriter, UidHealthStats.TIMER_AUDIO, uid.getAudioTurnedOnTimer());
+
+ // TIMER_VIDEO
+ addTimer(uidWriter, UidHealthStats.TIMER_VIDEO, uid.getVideoTurnedOnTimer());
+
+ // TIMER_FLASHLIGHT
+ addTimer(uidWriter, UidHealthStats.TIMER_FLASHLIGHT, uid.getFlashlightTurnedOnTimer());
+
+ // TIMER_CAMERA
+ addTimer(uidWriter, UidHealthStats.TIMER_CAMERA, uid.getCameraTurnedOnTimer());
+
+ // TIMER_FOREGROUND_ACTIVITY
+ addTimer(uidWriter, UidHealthStats.TIMER_FOREGROUND_ACTIVITY, uid.getForegroundActivityTimer());
+
+ // TIMER_BLUETOOTH_SCAN
+ addTimer(uidWriter, UidHealthStats.TIMER_BLUETOOTH_SCAN, uid.getBluetoothScanTimer());
+
+ // TIMER_PROCESS_STATE_TOP_MS
+ addTimer(uidWriter, UidHealthStats.TIMER_PROCESS_STATE_TOP_MS,
+ uid.getProcessStateTimer(BatteryStats.Uid.PROCESS_STATE_TOP));
+
+ // TIMER_PROCESS_STATE_FOREGROUND_SERVICE_MS
+ addTimer(uidWriter, UidHealthStats.TIMER_PROCESS_STATE_FOREGROUND_SERVICE_MS,
+ uid.getProcessStateTimer(BatteryStats.Uid.PROCESS_STATE_FOREGROUND_SERVICE));
+
+ // TIMER_PROCESS_STATE_TOP_SLEEPING_MS
+ addTimer(uidWriter, UidHealthStats.TIMER_PROCESS_STATE_TOP_SLEEPING_MS,
+ uid.getProcessStateTimer(BatteryStats.Uid.PROCESS_STATE_TOP_SLEEPING));
+
+ // TIMER_PROCESS_STATE_FOREGROUND_MS
+ addTimer(uidWriter, UidHealthStats.TIMER_PROCESS_STATE_FOREGROUND_MS,
+ uid.getProcessStateTimer(BatteryStats.Uid.PROCESS_STATE_FOREGROUND));
+
+ // TIMER_PROCESS_STATE_BACKGROUND_MS
+ addTimer(uidWriter, UidHealthStats.TIMER_PROCESS_STATE_BACKGROUND_MS,
+ uid.getProcessStateTimer(BatteryStats.Uid.PROCESS_STATE_BACKGROUND));
+
+ // TIMER_PROCESS_STATE_CACHED_MS
+ addTimer(uidWriter, UidHealthStats.TIMER_PROCESS_STATE_CACHED_MS,
+ uid.getProcessStateTimer(BatteryStats.Uid.PROCESS_STATE_CACHED));
+
+ // TIMER_VIBRATOR
+ addTimer(uidWriter, UidHealthStats.TIMER_VIBRATOR, uid.getVibratorOnTimer());
+
+ // MEASUREMENT_OTHER_USER_ACTIVITY_COUNT
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_OTHER_USER_ACTIVITY_COUNT,
+ uid.getUserActivityCount(PowerManager.USER_ACTIVITY_EVENT_OTHER,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_BUTTON_USER_ACTIVITY_COUNT
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BUTTON_USER_ACTIVITY_COUNT,
+ uid.getUserActivityCount(PowerManager.USER_ACTIVITY_EVENT_BUTTON,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_TOUCH_USER_ACTIVITY_COUNT,
+ uid.getUserActivityCount(PowerManager.USER_ACTIVITY_EVENT_TOUCH,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_MOBILE_RX_BYTES
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_RX_BYTES,
+ uid.getNetworkActivityBytes(BatteryStats.NETWORK_MOBILE_RX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_MOBILE_TX_BYTES
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_TX_BYTES,
+ uid.getNetworkActivityBytes(BatteryStats.NETWORK_MOBILE_TX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_WIFI_RX_BYTES
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_RX_BYTES,
+ uid.getNetworkActivityBytes(BatteryStats.NETWORK_WIFI_RX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_WIFI_TX_BYTES
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_TX_BYTES,
+ uid.getNetworkActivityBytes(BatteryStats.NETWORK_WIFI_TX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_BLUETOOTH_RX_BYTES
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_RX_BYTES,
+ uid.getNetworkActivityBytes(BatteryStats.NETWORK_BT_RX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_BLUETOOTH_TX_BYTES
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_TX_BYTES,
+ uid.getNetworkActivityBytes(BatteryStats.NETWORK_BT_TX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_MOBILE_RX_PACKETS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_RX_PACKETS,
+ uid.getNetworkActivityPackets(BatteryStats.NETWORK_MOBILE_RX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_MOBILE_TX_PACKETS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_MOBILE_TX_PACKETS,
+ uid.getNetworkActivityPackets(BatteryStats.NETWORK_MOBILE_TX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_WIFI_RX_PACKETS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_RX_PACKETS,
+ uid.getNetworkActivityPackets(BatteryStats.NETWORK_WIFI_RX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_WIFI_TX_PACKETS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_WIFI_TX_PACKETS,
+ uid.getNetworkActivityPackets(BatteryStats.NETWORK_WIFI_TX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_BLUETOOTH_RX_PACKETS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_RX_PACKETS,
+ uid.getNetworkActivityPackets(BatteryStats.NETWORK_BT_RX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_BLUETOOTH_TX_PACKETS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_TX_PACKETS,
+ uid.getNetworkActivityPackets(BatteryStats.NETWORK_BT_TX_DATA,
+ STATS_SINCE_UNPLUGGED));
+
+ // TIMER_MOBILE_RADIO_ACTIVE
+ uidWriter.addTimer(UidHealthStats.TIMER_MOBILE_RADIO_ACTIVE,
+ uid.getMobileRadioActiveCount(STATS_SINCE_UNPLUGGED),
+ uid.getMobileRadioActiveTime(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_USER_CPU_TIME_US
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_USER_CPU_TIME_US,
+ uid.getUserCpuTimeUs(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_SYSTEM_CPU_TIME_US
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_SYSTEM_CPU_TIME_US,
+ uid.getSystemCpuTimeUs(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_CPU_POWER_MAUS
+ uidWriter.addMeasurement(UidHealthStats.MEASUREMENT_CPU_POWER_MAUS,
+ uid.getCpuPowerMaUs(STATS_SINCE_UNPLUGGED));
+ }
+
+ /**
+ * Writes the contents of a BatteryStats.Uid.Pid into a HealthStatsWriter.
+ */
+ public void writePid(HealthStatsWriter pidWriter, BatteryStats.Uid.Pid pid) {
+ if (pid == null) {
+ return;
+ }
+
+ // MEASUREMENT_WAKE_NESTING_COUNT
+ pidWriter.addMeasurement(PidHealthStats.MEASUREMENT_WAKE_NESTING_COUNT, pid.mWakeNesting);
+
+ // MEASUREMENT_WAKE_SUM_MS
+ pidWriter.addMeasurement(PidHealthStats.MEASUREMENT_WAKE_SUM_MS, pid.mWakeSumMs);
+
+ // MEASUREMENT_WAKE_START_MS
+ pidWriter.addMeasurement(PidHealthStats.MEASUREMENT_WAKE_SUM_MS, pid.mWakeStartMs);
+ }
+
+ /**
+ * Writes the contents of a BatteryStats.Uid.Proc into a HealthStatsWriter.
+ */
+ public void writeProc(HealthStatsWriter procWriter, BatteryStats.Uid.Proc proc) {
+ // MEASUREMENT_USER_TIME_MS
+ procWriter.addMeasurement(ProcessHealthStats.MEASUREMENT_USER_TIME_MS,
+ proc.getUserTime(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_SYSTEM_TIME_MS
+ procWriter.addMeasurement(ProcessHealthStats.MEASUREMENT_SYSTEM_TIME_MS,
+ proc.getSystemTime(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_STARTS_COUNT
+ procWriter.addMeasurement(ProcessHealthStats.MEASUREMENT_STARTS_COUNT,
+ proc.getStarts(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_CRASHES_COUNT
+ procWriter.addMeasurement(ProcessHealthStats.MEASUREMENT_CRASHES_COUNT,
+ proc.getNumCrashes(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_ANR_COUNT
+ procWriter.addMeasurement(ProcessHealthStats.MEASUREMENT_ANR_COUNT,
+ proc.getNumAnrs(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_FOREGROUND_MS
+ procWriter.addMeasurement(ProcessHealthStats.MEASUREMENT_FOREGROUND_MS,
+ proc.getForegroundTime(STATS_SINCE_UNPLUGGED));
+ }
+
+ /**
+ * Writes the contents of a BatteryStats.Uid.Pkg into a HealthStatsWriter.
+ */
+ public void writePkg(HealthStatsWriter pkgWriter, BatteryStats.Uid.Pkg pkg) {
+ // STATS_SERVICES
+ for (final Map.Entry<String,? extends BatteryStats.Uid.Pkg.Serv> entry:
+ pkg.getServiceStats().entrySet()) {
+ final HealthStatsWriter writer = new HealthStatsWriter(ServiceHealthStats.CONSTANTS);
+ writeServ(writer, entry.getValue());
+ pkgWriter.addStats(PackageHealthStats.STATS_SERVICES, entry.getKey(), writer);
+ }
+
+ // MEASUREMENTS_WAKEUP_ALARMS_COUNT
+ for (final Map.Entry<String,? extends BatteryStats.Counter> entry:
+ pkg.getWakeupAlarmStats().entrySet()) {
+ final BatteryStats.Counter counter = entry.getValue();
+ if (counter != null) {
+ pkgWriter.addMeasurements(PackageHealthStats.MEASUREMENTS_WAKEUP_ALARMS_COUNT,
+ entry.getKey(), counter.getCountLocked(STATS_SINCE_UNPLUGGED));
+ }
+ }
+ }
+
+ /**
+ * Writes the contents of a BatteryStats.Uid.Pkg.Serv into a HealthStatsWriter.
+ */
+ public void writeServ(HealthStatsWriter servWriter, BatteryStats.Uid.Pkg.Serv serv) {
+ // MEASUREMENT_START_SERVICE_COUNT
+ servWriter.addMeasurement(ServiceHealthStats.MEASUREMENT_START_SERVICE_COUNT,
+ serv.getStarts(STATS_SINCE_UNPLUGGED));
+
+ // MEASUREMENT_LAUNCH_COUNT
+ servWriter.addMeasurement(ServiceHealthStats.MEASUREMENT_LAUNCH_COUNT,
+ serv.getLaunches(STATS_SINCE_UNPLUGGED));
+ }
+
+ /**
+ * Adds a BatteryStats.Timer into a HealthStatsWriter. Safe to pass a null timer.
+ */
+ private void addTimer(HealthStatsWriter writer, int key, BatteryStats.Timer timer) {
+ if (timer != null) {
+ writer.addTimer(key, timer.getCountLocked(STATS_SINCE_UNPLUGGED),
+ timer.getTotalTimeLocked(mNowRealtime, STATS_SINCE_UNPLUGGED));
+ }
+ }
+
+ /**
+ * Adds a named BatteryStats.Timer into a HealthStatsWriter. Safe to pass a null timer.
+ */
+ private void addTimers(HealthStatsWriter writer, int key, String name,
+ BatteryStats.Timer timer) {
+ if (timer != null) {
+ writer.addTimers(key, name, new TimerStat(timer.getCountLocked(STATS_SINCE_UNPLUGGED),
+ timer.getTotalTimeLocked(mNowRealtime, STATS_SINCE_UNPLUGGED)));
+ }
+ }
+}
+