summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/os/BatteryConsumer.java45
-rw-r--r--core/java/android/os/BatteryUsageStats.java19
-rw-r--r--core/java/android/os/PowerComponents.java98
-rw-r--r--core/java/android/os/UidBatteryConsumer.java49
4 files changed, 201 insertions, 10 deletions
diff --git a/core/java/android/os/BatteryConsumer.java b/core/java/android/os/BatteryConsumer.java
index d00c3c361722..74880b2d61d3 100644
--- a/core/java/android/os/BatteryConsumer.java
+++ b/core/java/android/os/BatteryConsumer.java
@@ -49,6 +49,28 @@ public abstract class BatteryConsumer {
public static final int FIRST_CUSTOM_POWER_COMPONENT_ID = 1000;
public static final int LAST_CUSTOM_POWER_COMPONENT_ID = 9999;
+ /**
+ * Time usage component, describing the particular part of the system
+ * that was used for the corresponding amount of time.
+ *
+ * @hide
+ */
+ @IntDef(prefix = {"TIME_COMPONENT_"}, value = {
+ TIME_COMPONENT_CPU,
+ TIME_COMPONENT_CPU_FOREGROUND,
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ public static @interface TimeComponent {
+ }
+
+ public static final int TIME_COMPONENT_CPU = 0;
+ public static final int TIME_COMPONENT_CPU_FOREGROUND = 1;
+
+ public static final int TIME_COMPONENT_COUNT = 2;
+
+ public static final int FIRST_CUSTOM_TIME_COMPONENT_ID = 1000;
+ public static final int LAST_CUSTOM_TIME_COMPONENT_ID = 9999;
+
private final PowerComponents mPowerComponents;
protected BatteryConsumer(@NonNull PowerComponents powerComponents) {
@@ -83,6 +105,29 @@ public abstract class BatteryConsumer {
return mPowerComponents.getConsumedPowerForCustomComponent(componentId);
}
+ /**
+ * Returns the amount of time since BatteryStats reset used by the specified component, e.g.
+ * CPU, WiFi etc.
+ *
+ * @param componentId The ID of the time component, e.g.
+ * {@link UidBatteryConsumer#TIME_COMPONENT_CPU}.
+ * @return Amount of time in milliseconds.
+ */
+ public long getUsageDurationMillis(@TimeComponent int componentId) {
+ return mPowerComponents.getUsageDurationMillis(componentId);
+ }
+
+ /**
+ * Returns the amount of usage time attributed to the specified custom component
+ * since BatteryStats reset.
+ *
+ * @param componentId The ID of the custom power component.
+ * @return Amount of time in milliseconds.
+ */
+ public long getUsageDurationForCustomComponentMillis(int componentId) {
+ return mPowerComponents.getUsageDurationForCustomComponentMillis(componentId);
+ }
+
protected void writeToParcel(Parcel dest, int flags) {
mPowerComponents.writeToParcel(dest, flags);
}
diff --git a/core/java/android/os/BatteryUsageStats.java b/core/java/android/os/BatteryUsageStats.java
index 3f036cdcfa72..79f58f68b637 100644
--- a/core/java/android/os/BatteryUsageStats.java
+++ b/core/java/android/os/BatteryUsageStats.java
@@ -35,7 +35,11 @@ public final class BatteryUsageStats implements Parcelable {
private BatteryUsageStats(@NonNull Builder builder) {
mConsumedPower = builder.mConsumedPower;
mDischargePercentage = builder.mDischargePercentage;
- mUidBatteryConsumers = builder.mUidBatteryConsumers;
+ final int uidBatteryConsumerCount = builder.mUidBatteryConsumerBuilders.size();
+ mUidBatteryConsumers = new ArrayList<>(uidBatteryConsumerCount);
+ for (int i = 0; i < uidBatteryConsumerCount; i++) {
+ mUidBatteryConsumers.add(builder.mUidBatteryConsumerBuilders.get(i).build());
+ }
}
/**
@@ -95,7 +99,8 @@ public final class BatteryUsageStats implements Parcelable {
public static final class Builder {
private double mConsumedPower;
private int mDischargePercentage;
- private final ArrayList<UidBatteryConsumer> mUidBatteryConsumers = new ArrayList<>();
+ private final ArrayList<UidBatteryConsumer.Builder> mUidBatteryConsumerBuilders =
+ new ArrayList<>();
/**
* Constructs a read-only object using the Builder values.
@@ -130,9 +135,15 @@ public final class BatteryUsageStats implements Parcelable {
* individual UID.
*/
@NonNull
- public Builder addUidBatteryConsumer(@NonNull UidBatteryConsumer uidBatteryConsumer) {
- mUidBatteryConsumers.add(uidBatteryConsumer);
+ public Builder addUidBatteryConsumerBuilder(
+ @NonNull UidBatteryConsumer.Builder uidBatteryConsumer) {
+ mUidBatteryConsumerBuilders.add(uidBatteryConsumer);
return this;
}
+
+ @NonNull
+ public List<UidBatteryConsumer.Builder> getUidBatteryConsumerBuilders() {
+ return mUidBatteryConsumerBuilders;
+ }
}
}
diff --git a/core/java/android/os/PowerComponents.java b/core/java/android/os/PowerComponents.java
index 42ba1ff60e5a..c18fff64ba62 100644
--- a/core/java/android/os/PowerComponents.java
+++ b/core/java/android/os/PowerComponents.java
@@ -29,21 +29,25 @@ class PowerComponents {
private final double mTotalPowerConsumed;
private final double[] mPowerComponents;
+ private final long[] mTimeComponents;
PowerComponents(@NonNull Builder builder) {
mTotalPowerConsumed = builder.mTotalPowerConsumed;
mPowerComponents = builder.mPowerComponents;
+ mTimeComponents = builder.mTimeComponents;
}
PowerComponents(@NonNull Parcel source) {
mTotalPowerConsumed = source.readDouble();
mPowerComponents = source.createDoubleArray();
+ mTimeComponents = source.createLongArray();
}
/** Writes contents to Parcel */
void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeDouble(mTotalPowerConsumed);
dest.writeDoubleArray(mPowerComponents);
+ dest.writeLongArray(mTimeComponents);
}
/**
@@ -94,15 +98,58 @@ class PowerComponents {
}
/**
+ * Returns the amount of time used by the specified component, e.g. CPU, WiFi etc.
+ *
+ * @param componentId The ID of the time component, e.g.
+ * {@link UidBatteryConsumer#TIME_COMPONENT_CPU}.
+ * @return Amount of time in milliseconds.
+ */
+ public long getUsageDurationMillis(@UidBatteryConsumer.TimeComponent int componentId) {
+ if (componentId >= UidBatteryConsumer.TIME_COMPONENT_COUNT) {
+ throw new IllegalArgumentException(
+ "Unsupported time component ID: " + componentId);
+ }
+ try {
+ return mTimeComponents[componentId];
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new IllegalArgumentException("Unsupported power component ID: " + componentId);
+ }
+ }
+
+ /**
+ * Returns the amount of usage time attributed to the specified custom component.
+ *
+ * @param componentId The ID of the custom power component.
+ * @return Amount of time in milliseconds.
+ */
+ public long getUsageDurationForCustomComponentMillis(int componentId) {
+ if (componentId < UidBatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID) {
+ throw new IllegalArgumentException(
+ "Unsupported custom time component ID: " + componentId);
+ }
+ try {
+ return mTimeComponents[
+ UidBatteryConsumer.TIME_COMPONENT_COUNT + componentId
+ - UidBatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID];
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new IllegalArgumentException(
+ "Unsupported custom time component ID: " + componentId);
+ }
+ }
+
+ /**
* Builder for PowerComponents.
*/
static final class Builder {
private double mTotalPowerConsumed;
private final double[] mPowerComponents;
+ private final long[] mTimeComponents;
- Builder(int customPowerComponentCount) {
- mPowerComponents = new double[BatteryConsumer.POWER_COMPONENT_COUNT
+ Builder(int customPowerComponentCount, int customTimeComponentCount) {
+ mPowerComponents = new double[UidBatteryConsumer.POWER_COMPONENT_COUNT
+ customPowerComponentCount];
+ mTimeComponents = new long[UidBatteryConsumer.TIME_COMPONENT_COUNT
+ + customTimeComponentCount];
}
/**
@@ -160,6 +207,53 @@ class PowerComponents {
}
/**
+ * Sets the amount of time used by the specified component, e.g. CPU, WiFi etc.
+ *
+ * @param componentId The ID of the time component, e.g.
+ * {@link UidBatteryConsumer#TIME_COMPONENT_CPU}.
+ * @param componentUsageDurationMillis Amount of time in milliseconds.
+ */
+ @NonNull
+ public Builder setUsageDurationMillis(@UidBatteryConsumer.TimeComponent int componentId,
+ long componentUsageDurationMillis) {
+ if (componentId >= UidBatteryConsumer.TIME_COMPONENT_COUNT) {
+ throw new IllegalArgumentException(
+ "Unsupported time component ID: " + componentId);
+ }
+ try {
+ mTimeComponents[componentId] = componentUsageDurationMillis;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new IllegalArgumentException(
+ "Unsupported time component ID: " + componentId);
+ }
+ return this;
+ }
+
+ /**
+ * Sets the amount of time used by the specified custom component.
+ *
+ * @param componentId The ID of the custom power component.
+ * @param componentUsageDurationMillis Amount of time in milliseconds.
+ */
+ @NonNull
+ public Builder setUsageDurationForCustomComponentMillis(int componentId,
+ long componentUsageDurationMillis) {
+ if (componentId < UidBatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID) {
+ throw new IllegalArgumentException(
+ "Unsupported custom time component ID: " + componentId);
+ }
+ try {
+ mTimeComponents[UidBatteryConsumer.TIME_COMPONENT_COUNT + componentId
+ - UidBatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID] =
+ componentUsageDurationMillis;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new IllegalArgumentException(
+ "Unsupported custom time component ID: " + componentId);
+ }
+ return this;
+ }
+
+ /**
* Creates a read-only object out of the Builder values.
*/
@NonNull
diff --git a/core/java/android/os/UidBatteryConsumer.java b/core/java/android/os/UidBatteryConsumer.java
index 7dcbf7d4cef3..d12ccb57794b 100644
--- a/core/java/android/os/UidBatteryConsumer.java
+++ b/core/java/android/os/UidBatteryConsumer.java
@@ -82,12 +82,24 @@ public final class UidBatteryConsumer extends BatteryConsumer implements Parcela
*/
public static final class Builder {
private final PowerComponents.Builder mPowerComponentsBuilder;
+ private final BatteryStats.Uid mBatteryStatsUid;
private final int mUid;
private String mPackageWithHighestDrain;
- public Builder(int customPowerComponentCount, int uid) {
- mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentCount);
- mUid = uid;
+ public Builder(int customPowerComponentCount, int customTimeComponentCount,
+ BatteryStats.Uid batteryStatsUid) {
+ mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentCount,
+ customTimeComponentCount);
+ mBatteryStatsUid = batteryStatsUid;
+ mUid = batteryStatsUid.getUid();
+ }
+
+ public BatteryStats.Uid getBatteryStatsUid() {
+ return mBatteryStatsUid;
+ }
+
+ public int getUid() {
+ return mUid;
}
/**
@@ -102,7 +114,7 @@ public final class UidBatteryConsumer extends BatteryConsumer implements Parcela
* Sets the amount of drain attributed to the specified drain type, e.g. CPU, WiFi etc.
*
* @param componentId The ID of the power component, e.g.
- * {@link BatteryConsumer#POWER_COMPONENT_CPU}.
+ * {@link BatteryConsumer#POWER_COMPONENT_CPU}.
* @param componentPower Amount of consumed power in mAh.
*/
@NonNull
@@ -133,6 +145,35 @@ public final class UidBatteryConsumer extends BatteryConsumer implements Parcela
}
/**
+ * Sets the amount of time used by the specified component, e.g. CPU, WiFi etc.
+ *
+ * @param componentId The ID of the time component, e.g.
+ * {@link UidBatteryConsumer#TIME_COMPONENT_CPU}.
+ * @param componentUsageDurationMillis Amount of time in milliseconds.
+ */
+ @NonNull
+ public Builder setUsageDurationMillis(@UidBatteryConsumer.TimeComponent int componentId,
+ long componentUsageDurationMillis) {
+ mPowerComponentsBuilder.setUsageDurationMillis(componentId,
+ componentUsageDurationMillis);
+ return this;
+ }
+
+ /**
+ * Sets the amount of time used by the specified custom component.
+ *
+ * @param componentId The ID of the custom power component.
+ * @param componentUsageDurationMillis Amount of time in milliseconds.
+ */
+ @NonNull
+ public Builder setUsageDurationForCustomComponentMillis(int componentId,
+ long componentUsageDurationMillis) {
+ mPowerComponentsBuilder.setUsageDurationForCustomComponentMillis(componentId,
+ componentUsageDurationMillis);
+ return this;
+ }
+
+ /**
* Sets the name of the package owned by this UID that consumed the highest amount
* of power since BatteryStats reset.
*/