diff options
| author | Dmitri Plotnikov <dplotnikov@google.com> | 2021-04-13 23:44:29 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2021-04-13 23:44:29 +0000 |
| commit | 07bc3d6d95d6635d417a993f40656bce1864fbd4 (patch) | |
| tree | 8aae62db61c25d2dfc6cd7e447ab002314b03861 /core/java/android | |
| parent | a6d96b39953fc851d0c5a5dfc069bcac5e951c47 (diff) | |
| parent | 3885aa960ba7054cdcedcf76fb5121826118ead1 (diff) | |
Merge "Add BatteryConsumer.getCustomPowerComponentName() and getCustomPowerComponentCount()" into sc-dev
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/os/BatteryConsumer.java | 24 | ||||
| -rw-r--r-- | core/java/android/os/BatteryUsageStats.java | 74 | ||||
| -rw-r--r-- | core/java/android/os/PowerComponents.java | 39 | ||||
| -rw-r--r-- | core/java/android/os/SystemBatteryConsumer.java | 4 | ||||
| -rw-r--r-- | core/java/android/os/UidBatteryConsumer.java | 4 | ||||
| -rw-r--r-- | core/java/android/os/UserBatteryConsumer.java | 4 |
6 files changed, 114 insertions, 35 deletions
diff --git a/core/java/android/os/BatteryConsumer.java b/core/java/android/os/BatteryConsumer.java index 033148379041..b558edcded47 100644 --- a/core/java/android/os/BatteryConsumer.java +++ b/core/java/android/os/BatteryConsumer.java @@ -149,6 +149,7 @@ public abstract class BatteryConsumer { public static final int POWER_MODEL_MEASURED_ENERGY = 1; private final PowerComponents mPowerComponents; + private String[] mCustomPowerComponentNames; protected BatteryConsumer(@NonNull PowerComponents powerComponents) { mPowerComponents = powerComponents; @@ -192,6 +193,23 @@ public abstract class BatteryConsumer { return mPowerComponents.getConsumedPowerForCustomComponent(componentId); } + public int getCustomPowerComponentCount() { + return mPowerComponents.getCustomPowerComponentCount(); + } + + void setCustomPowerComponentNames(String[] customPowerComponentNames) { + mPowerComponents.setCustomPowerComponentNames(customPowerComponentNames); + } + + /** + * Returns the name of the specified power component. + * + * @param componentId The ID of the custom power component. + */ + public String getCustomPowerComponentName(int componentId) { + return mPowerComponents.getCustomPowerComponentName(componentId); + } + /** * Returns the amount of time since BatteryStats reset used by the specified component, e.g. * CPU, WiFi etc. @@ -222,9 +240,9 @@ public abstract class BatteryConsumer { protected abstract static class BaseBuilder<T extends BaseBuilder<?>> { final PowerComponents.Builder mPowerComponentsBuilder; - public BaseBuilder(int customPowerComponentCount, int customTimeComponentCount, - boolean includePowerModels) { - mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentCount, + public BaseBuilder(@NonNull String[] customPowerComponentNames, + int customTimeComponentCount, boolean includePowerModels) { + mPowerComponentsBuilder = new PowerComponents.Builder(customPowerComponentNames, customTimeComponentCount, includePowerModels); } diff --git a/core/java/android/os/BatteryUsageStats.java b/core/java/android/os/BatteryUsageStats.java index f2887748f460..48f4ca4035ce 100644 --- a/core/java/android/os/BatteryUsageStats.java +++ b/core/java/android/os/BatteryUsageStats.java @@ -24,6 +24,7 @@ import com.android.internal.os.BatteryStatsHistory; import com.android.internal.os.BatteryStatsHistoryIterator; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -39,9 +40,10 @@ public final class BatteryUsageStats implements Parcelable { private final double mDischargedPowerUpperBound; private final long mBatteryTimeRemainingMs; private final long mChargeTimeRemainingMs; - private final ArrayList<UidBatteryConsumer> mUidBatteryConsumers; - private final ArrayList<SystemBatteryConsumer> mSystemBatteryConsumers; - private final ArrayList<UserBatteryConsumer> mUserBatteryConsumers; + private final String[] mCustomPowerComponentNames; + private final List<UidBatteryConsumer> mUidBatteryConsumers; + private final List<SystemBatteryConsumer> mSystemBatteryConsumers; + private final List<UserBatteryConsumer> mUserBatteryConsumers; private final Parcel mHistoryBuffer; private final List<BatteryStats.HistoryTag> mHistoryTagPool; @@ -54,6 +56,7 @@ public final class BatteryUsageStats implements Parcelable { mHistoryTagPool = builder.mHistoryTagPool; mBatteryTimeRemainingMs = builder.mBatteryTimeRemainingMs; mChargeTimeRemainingMs = builder.mChargeTimeRemainingMs; + mCustomPowerComponentNames = builder.mCustomPowerComponentNames; double totalPower = 0; @@ -182,12 +185,31 @@ public final class BatteryUsageStats implements Parcelable { mDischargedPowerUpperBound = source.readDouble(); mBatteryTimeRemainingMs = source.readLong(); mChargeTimeRemainingMs = source.readLong(); - mUidBatteryConsumers = new ArrayList<>(); - source.readParcelableList(mUidBatteryConsumers, getClass().getClassLoader()); - mSystemBatteryConsumers = new ArrayList<>(); - source.readParcelableList(mSystemBatteryConsumers, getClass().getClassLoader()); - mUserBatteryConsumers = new ArrayList<>(); - source.readParcelableList(mUserBatteryConsumers, getClass().getClassLoader()); + mCustomPowerComponentNames = source.readStringArray(); + int uidCount = source.readInt(); + mUidBatteryConsumers = new ArrayList<>(uidCount); + for (int i = 0; i < uidCount; i++) { + final UidBatteryConsumer consumer = + UidBatteryConsumer.CREATOR.createFromParcel(source); + consumer.setCustomPowerComponentNames(mCustomPowerComponentNames); + mUidBatteryConsumers.add(consumer); + } + int sysCount = source.readInt(); + mSystemBatteryConsumers = new ArrayList<>(sysCount); + for (int i = 0; i < sysCount; i++) { + final SystemBatteryConsumer consumer = + SystemBatteryConsumer.CREATOR.createFromParcel(source); + consumer.setCustomPowerComponentNames(mCustomPowerComponentNames); + mSystemBatteryConsumers.add(consumer); + } + int userCount = source.readInt(); + mUserBatteryConsumers = new ArrayList<>(userCount); + for (int i = 0; i < userCount; i++) { + final UserBatteryConsumer consumer = + UserBatteryConsumer.CREATOR.createFromParcel(source); + consumer.setCustomPowerComponentNames(mCustomPowerComponentNames); + mUserBatteryConsumers.add(consumer); + } if (source.readBoolean()) { mHistoryBuffer = Parcel.obtain(); mHistoryBuffer.setDataSize(0); @@ -211,6 +233,8 @@ public final class BatteryUsageStats implements Parcelable { mHistoryBuffer = null; mHistoryTagPool = null; } + System.out.println("From Parcel = " + Arrays.toString( + mCustomPowerComponentNames)); } @Override @@ -222,9 +246,19 @@ public final class BatteryUsageStats implements Parcelable { dest.writeDouble(mDischargedPowerUpperBound); dest.writeLong(mBatteryTimeRemainingMs); dest.writeLong(mChargeTimeRemainingMs); - dest.writeParcelableList(mUidBatteryConsumers, flags); - dest.writeParcelableList(mSystemBatteryConsumers, flags); - dest.writeParcelableList(mUserBatteryConsumers, flags); + dest.writeStringArray(mCustomPowerComponentNames); + dest.writeInt(mUidBatteryConsumers.size()); + for (int i = mUidBatteryConsumers.size() - 1; i >= 0; i--) { + mUidBatteryConsumers.get(i).writeToParcel(dest, flags); + } + dest.writeInt(mSystemBatteryConsumers.size()); + for (int i = mSystemBatteryConsumers.size() - 1; i >= 0; i--) { + mSystemBatteryConsumers.get(i).writeToParcel(dest, flags); + } + dest.writeInt(mUserBatteryConsumers.size()); + for (int i = mUserBatteryConsumers.size() - 1; i >= 0; i--) { + mUserBatteryConsumers.get(i).writeToParcel(dest, flags); + } if (mHistoryBuffer != null) { dest.writeBoolean(true); @@ -259,7 +293,7 @@ public final class BatteryUsageStats implements Parcelable { * Builder for BatteryUsageStats. */ public static final class Builder { - private final int mCustomPowerComponentCount; + private final String[] mCustomPowerComponentNames; private final int mCustomTimeComponentCount; private final boolean mIncludePowerModels; private long mStatsStartTimestampMs; @@ -277,13 +311,13 @@ public final class BatteryUsageStats implements Parcelable { private Parcel mHistoryBuffer; private List<BatteryStats.HistoryTag> mHistoryTagPool; - public Builder(int customPowerComponentCount, int customTimeComponentCount) { - this(customPowerComponentCount, customTimeComponentCount, false); + public Builder(String[] customPowerComponentNames, int customTimeComponentCount) { + this(customPowerComponentNames, customTimeComponentCount, false); } - public Builder(int customPowerComponentCount, int customTimeComponentCount, + public Builder(String[] customPowerComponentNames, int customTimeComponentCount, boolean includePowerModels) { - mCustomPowerComponentCount = customPowerComponentCount; + mCustomPowerComponentNames = customPowerComponentNames; mCustomTimeComponentCount = customTimeComponentCount; mIncludePowerModels = includePowerModels; } @@ -366,7 +400,7 @@ public final class BatteryUsageStats implements Parcelable { int uid = batteryStatsUid.getUid(); UidBatteryConsumer.Builder builder = mUidBatteryConsumerBuilders.get(uid); if (builder == null) { - builder = new UidBatteryConsumer.Builder(mCustomPowerComponentCount, + builder = new UidBatteryConsumer.Builder(mCustomPowerComponentNames, mCustomTimeComponentCount, mIncludePowerModels, batteryStatsUid); mUidBatteryConsumerBuilders.put(uid, builder); } @@ -382,7 +416,7 @@ public final class BatteryUsageStats implements Parcelable { @SystemBatteryConsumer.DrainType int drainType) { SystemBatteryConsumer.Builder builder = mSystemBatteryConsumerBuilders.get(drainType); if (builder == null) { - builder = new SystemBatteryConsumer.Builder(mCustomPowerComponentCount, + builder = new SystemBatteryConsumer.Builder(mCustomPowerComponentNames, mCustomTimeComponentCount, mIncludePowerModels, drainType); mSystemBatteryConsumerBuilders.put(drainType, builder); } @@ -397,7 +431,7 @@ public final class BatteryUsageStats implements Parcelable { public UserBatteryConsumer.Builder getOrCreateUserBatteryConsumerBuilder(int userId) { UserBatteryConsumer.Builder builder = mUserBatteryConsumerBuilders.get(userId); if (builder == null) { - builder = new UserBatteryConsumer.Builder(mCustomPowerComponentCount, + builder = new UserBatteryConsumer.Builder(mCustomPowerComponentNames, mCustomTimeComponentCount, mIncludePowerModels, userId); mUserBatteryConsumerBuilders.put(userId, builder); } diff --git a/core/java/android/os/PowerComponents.java b/core/java/android/os/PowerComponents.java index 238f4518b7d3..2fea4acf3876 100644 --- a/core/java/android/os/PowerComponents.java +++ b/core/java/android/os/PowerComponents.java @@ -26,7 +26,7 @@ import android.annotation.NonNull; class PowerComponents { private static final int CUSTOM_POWER_COMPONENT_OFFSET = BatteryConsumer.POWER_COMPONENT_COUNT - BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID; - public static final int CUSTOM_TIME_COMPONENT_OFFSET = BatteryConsumer.TIME_COMPONENT_COUNT + private static final int CUSTOM_TIME_COMPONENT_OFFSET = BatteryConsumer.TIME_COMPONENT_COUNT - BatteryConsumer.FIRST_CUSTOM_TIME_COMPONENT_ID; private final double mTotalConsumedPowerMah; @@ -34,9 +34,12 @@ class PowerComponents { private final long[] mTimeComponentsMs; private final int mCustomPowerComponentCount; private final byte[] mPowerModels; + // Not written to Parcel and must be explicitly restored during the parent object's unparceling + private String[] mCustomPowerComponentNames; PowerComponents(@NonNull Builder builder) { - mCustomPowerComponentCount = builder.mCustomPowerComponentCount; + mCustomPowerComponentNames = builder.mCustomPowerComponentNames; + mCustomPowerComponentCount = mCustomPowerComponentNames.length; mPowerComponentsMah = builder.mPowerComponentsMah; mTimeComponentsMs = builder.mTimeComponentsMs; mTotalConsumedPowerMah = builder.getTotalPower(); @@ -117,6 +120,26 @@ class PowerComponents { } } + void setCustomPowerComponentNames(String[] customPowerComponentNames) { + mCustomPowerComponentNames = customPowerComponentNames; + } + + public String getCustomPowerComponentName(int componentId) { + if (componentId >= BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID + && componentId < BatteryConsumer.LAST_CUSTOM_POWER_COMPONENT_ID) { + try { + return mCustomPowerComponentNames[componentId + - BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID]; + } catch (ArrayIndexOutOfBoundsException e) { + throw new IllegalArgumentException( + "Unsupported custom power component ID: " + componentId); + } + } else { + throw new IllegalArgumentException( + "Unsupported custom power component ID: " + componentId); + } + } + @BatteryConsumer.PowerModel int getPowerModel(@BatteryConsumer.PowerComponent int component) { if (mPowerModels == null) { @@ -164,20 +187,24 @@ class PowerComponents { } } + public int getCustomPowerComponentCount() { + return mCustomPowerComponentCount; + } + /** * Builder for PowerComponents. */ static final class Builder { private final double[] mPowerComponentsMah; - private final int mCustomPowerComponentCount; + private final String[] mCustomPowerComponentNames; private final long[] mTimeComponentsMs; private final byte[] mPowerModels; - Builder(int customPowerComponentCount, int customTimeComponentCount, + Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount, boolean includePowerModels) { - mCustomPowerComponentCount = customPowerComponentCount; + mCustomPowerComponentNames = customPowerComponentNames; int powerComponentCount = - BatteryConsumer.POWER_COMPONENT_COUNT + customPowerComponentCount; + BatteryConsumer.POWER_COMPONENT_COUNT + mCustomPowerComponentNames.length; mPowerComponentsMah = new double[powerComponentCount]; mTimeComponentsMs = new long[BatteryConsumer.TIME_COMPONENT_COUNT + customTimeComponentCount]; diff --git a/core/java/android/os/SystemBatteryConsumer.java b/core/java/android/os/SystemBatteryConsumer.java index e973e4c1044e..70b0532049fb 100644 --- a/core/java/android/os/SystemBatteryConsumer.java +++ b/core/java/android/os/SystemBatteryConsumer.java @@ -140,9 +140,9 @@ public class SystemBatteryConsumer extends BatteryConsumer implements Parcelable private double mPowerConsumedByAppsMah; private List<UidBatteryConsumer.Builder> mUidBatteryConsumers; - Builder(int customPowerComponentCount, int customTimeComponentCount, + Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount, boolean includePowerModels, @DrainType int drainType) { - super(customPowerComponentCount, customTimeComponentCount, includePowerModels); + super(customPowerComponentNames, customTimeComponentCount, includePowerModels); mDrainType = drainType; } diff --git a/core/java/android/os/UidBatteryConsumer.java b/core/java/android/os/UidBatteryConsumer.java index 87c263b3dd38..92e960344f3e 100644 --- a/core/java/android/os/UidBatteryConsumer.java +++ b/core/java/android/os/UidBatteryConsumer.java @@ -139,9 +139,9 @@ public final class UidBatteryConsumer extends BatteryConsumer implements Parcela public long mTimeInBackgroundMs; private boolean mExcludeFromBatteryUsageStats; - public Builder(int customPowerComponentCount, int customTimeComponentCount, + public Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount, boolean includePowerModels, @NonNull BatteryStats.Uid batteryStatsUid) { - super(customPowerComponentCount, customTimeComponentCount, includePowerModels); + super(customPowerComponentNames, customTimeComponentCount, includePowerModels); mBatteryStatsUid = batteryStatsUid; mUid = batteryStatsUid.getUid(); } diff --git a/core/java/android/os/UserBatteryConsumer.java b/core/java/android/os/UserBatteryConsumer.java index 78322088a3a4..de0a707fb656 100644 --- a/core/java/android/os/UserBatteryConsumer.java +++ b/core/java/android/os/UserBatteryConsumer.java @@ -77,9 +77,9 @@ public class UserBatteryConsumer extends BatteryConsumer implements Parcelable { private final int mUserId; private List<UidBatteryConsumer.Builder> mUidBatteryConsumers; - Builder(int customPowerComponentCount, int customTimeComponentCount, + Builder(@NonNull String[] customPowerComponentNames, int customTimeComponentCount, boolean includePowerModels, int userId) { - super(customPowerComponentCount, customTimeComponentCount, includePowerModels); + super(customPowerComponentNames, customTimeComponentCount, includePowerModels); mUserId = userId; } |
