Merge "Replace uses of SystemBatteryConsumer with aggregate BatteryConsumers" into sc-dev

This commit is contained in:
Dmitri Plotnikov
2021-05-03 20:40:25 +00:00
committed by Android (Google) Code Review
11 changed files with 224 additions and 233 deletions

View File

@@ -21,13 +21,13 @@ import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.AggregateBatteryConsumer;
import android.os.BatteryConsumer;
import android.os.BatteryUsageStats;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.SystemBatteryConsumer;
import android.os.UidBatteryConsumer;
import android.os.UserBatteryConsumer;
import android.os.UserHandle;
@@ -344,16 +344,38 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
}
}
final List<SystemBatteryConsumer> systemBatteryConsumers =
mBatteryUsageStats.getSystemBatteryConsumers();
for (int i = 0, size = systemBatteryConsumers.size(); i < size; i++) {
final SystemBatteryConsumer consumer = systemBatteryConsumers.get(i);
if (!showAllApps && mBatteryUtils.shouldHideSystemBatteryConsumer(consumer)) {
final BatteryConsumer deviceConsumer = mBatteryUsageStats.getAggregateBatteryConsumer(
BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE);
final BatteryConsumer appsConsumer = mBatteryUsageStats.getAggregateBatteryConsumer(
BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS);
for (int componentId = 0; componentId < BatteryConsumer.POWER_COMPONENT_COUNT;
componentId++) {
if (!showAllApps
&& mBatteryUtils.shouldHideDevicePowerComponent(deviceConsumer, componentId)) {
continue;
}
results.add(new BatteryEntry(mContext, mHandler, mUserManager,
consumer, /* isHidden */ true, null, null, loadDataInBackground));
results.add(new BatteryEntry(mContext, componentId,
deviceConsumer.getConsumedPower(componentId),
appsConsumer.getConsumedPower(componentId),
deviceConsumer.getUsageDurationMillis(componentId)));
}
for (int componentId = BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID;
componentId < BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID
+ deviceConsumer.getCustomPowerComponentCount();
componentId++) {
if (!showAllApps
&& mBatteryUtils.shouldHideCustomDevicePowerComponent(deviceConsumer,
componentId)) {
continue;
}
results.add(new BatteryEntry(mContext, componentId,
deviceConsumer.getCustomPowerComponentName(componentId),
deviceConsumer.getConsumedPowerForCustomComponent(componentId),
appsConsumer.getConsumedPowerForCustomComponent(componentId)));
}
if (showAllApps) {
@@ -431,20 +453,26 @@ public class BatteryAppListPreferenceController extends AbstractPreferenceContro
.setDischargePercentage(100);
float use = 500;
for (@SystemBatteryConsumer.DrainType int drainType : new int[]{
SystemBatteryConsumer.DRAIN_TYPE_AMBIENT_DISPLAY,
SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH,
SystemBatteryConsumer.DRAIN_TYPE_CAMERA,
SystemBatteryConsumer.DRAIN_TYPE_FLASHLIGHT,
SystemBatteryConsumer.DRAIN_TYPE_IDLE,
SystemBatteryConsumer.DRAIN_TYPE_MEMORY,
SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO,
SystemBatteryConsumer.DRAIN_TYPE_PHONE,
SystemBatteryConsumer.DRAIN_TYPE_SCREEN,
SystemBatteryConsumer.DRAIN_TYPE_WIFI,
final AggregateBatteryConsumer.Builder appsBatteryConsumerBuilder =
builder.getAggregateBatteryConsumerBuilder(
BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS);
final AggregateBatteryConsumer.Builder deviceBatteryConsumerBuilder =
builder.getAggregateBatteryConsumerBuilder(
BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE);
for (@BatteryConsumer.PowerComponent int componentId : new int[]{
BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY,
BatteryConsumer.POWER_COMPONENT_BLUETOOTH,
BatteryConsumer.POWER_COMPONENT_CAMERA,
BatteryConsumer.POWER_COMPONENT_FLASHLIGHT,
BatteryConsumer.POWER_COMPONENT_IDLE,
BatteryConsumer.POWER_COMPONENT_MEMORY,
BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO,
BatteryConsumer.POWER_COMPONENT_PHONE,
BatteryConsumer.POWER_COMPONENT_SCREEN,
BatteryConsumer.POWER_COMPONENT_WIFI,
}) {
builder.getOrCreateSystemBatteryConsumerBuilder(drainType)
.setConsumedPower(BatteryConsumer.POWER_COMPONENT_CPU, use);
appsBatteryConsumerBuilder.setConsumedPower(componentId, use);
deviceBatteryConsumerBuilder.setConsumedPower(componentId, use * 2);
use += 5;
}

View File

@@ -15,8 +15,6 @@ package com.android.settings.fuelgauge;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
@@ -29,7 +27,6 @@ import androidx.annotation.VisibleForTesting;
import com.android.settingslib.utils.StringUtil;
import java.time.Duration;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
@@ -158,8 +155,8 @@ public class BatteryDiffEntry {
break;
case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY:
final BatteryEntry.NameAndIcon nameAndIconForSystem =
BatteryEntry.getNameAndIconFromDrainType(
mContext, mBatteryHistEntry.mDrainType);
BatteryEntry.getNameAndIconFromPowerComponent(
mContext, mBatteryHistEntry.mPowerComponentId);
if (nameAndIconForSystem != null) {
mAppLabel = nameAndIconForSystem.name;
if (nameAndIconForSystem.iconId != 0) {

View File

@@ -29,11 +29,11 @@ import android.os.BatteryConsumer;
import android.os.Handler;
import android.os.Process;
import android.os.RemoteException;
import android.os.SystemBatteryConsumer;
import android.os.UidBatteryConsumer;
import android.os.UserBatteryConsumer;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.DebugUtils;
import android.util.Log;
import androidx.annotation.NonNull;
@@ -160,6 +160,11 @@ public class BatteryEntry {
private final Context mContext;
private final BatteryConsumer mBatteryConsumer;
private final boolean mIsHidden;
@ConvertUtils.ConsumerType
private final int mConsumerType;
@BatteryConsumer.PowerComponent
private final int mPowerComponentId;
private long mUsageDurationMs;
public String name;
public Drawable icon;
@@ -188,8 +193,10 @@ public class BatteryEntry {
mBatteryConsumer = batteryConsumer;
mIsHidden = isHidden;
mDefaultPackageName = packageName;
mPowerComponentId = -1;
if (batteryConsumer instanceof UidBatteryConsumer) {
mConsumerType = ConvertUtils.CONSUMER_TYPE_UID_BATTERY;
mConsumedPower = batteryConsumer.getConsumedPower();
UidBatteryConsumer uidBatteryConsumer = (UidBatteryConsumer) batteryConsumer;
@@ -215,27 +222,54 @@ public class BatteryEntry {
}
}
getQuickNameIconForUid(uid, packages, loadDataInBackground);
return;
} else if (batteryConsumer instanceof SystemBatteryConsumer) {
mConsumedPower = batteryConsumer.getConsumedPower()
- ((SystemBatteryConsumer) batteryConsumer).getPowerConsumedByApps();
final NameAndIcon nameAndIcon = getNameAndIconFromDrainType(
context, ((SystemBatteryConsumer) batteryConsumer).getDrainType());
iconId = nameAndIcon.iconId;
name = nameAndIcon.name;
} else if (batteryConsumer instanceof UserBatteryConsumer) {
mConsumerType = ConvertUtils.CONSUMER_TYPE_USER_BATTERY;
mConsumedPower = batteryConsumer.getConsumedPower();
final NameAndIcon nameAndIcon = getNameAndIconFromUserId(
context, ((UserBatteryConsumer) batteryConsumer).getUserId());
icon = nameAndIcon.icon;
name = nameAndIcon.name;
} else {
throw new IllegalArgumentException("Unsupported battery consumer: " + batteryConsumer);
}
}
/** Battery entry for a power component of AggregateBatteryConsumer */
public BatteryEntry(Context context, int powerComponentId, double devicePowerMah,
double appsPowerMah, long usageDurationMs) {
mContext = context;
mBatteryConsumer = null;
mIsHidden = false;
mPowerComponentId = powerComponentId;
mConsumedPower = devicePowerMah - appsPowerMah;
mUsageDurationMs = usageDurationMs;
mConsumerType = ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY;
final NameAndIcon nameAndIcon = getNameAndIconFromPowerComponent(context, powerComponentId);
iconId = nameAndIcon.iconId;
name = nameAndIcon.name;
if (iconId != 0) {
icon = context.getDrawable(iconId);
}
}
/** Battery entry for a custom power component of AggregateBatteryConsumer */
public BatteryEntry(Context context, int powerComponentId, String powerComponentName,
double devicePowerMah, double appsPowerMah) {
mContext = context;
mBatteryConsumer = null;
mIsHidden = false;
mPowerComponentId = powerComponentId;
iconId = R.drawable.ic_power_system;
icon = context.getDrawable(iconId);
name = powerComponentName;
mConsumedPower = devicePowerMah - appsPowerMah;
mConsumerType = ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY;
}
public Drawable getIcon() {
return icon;
}
@@ -247,6 +281,16 @@ public class BatteryEntry {
return name;
}
@ConvertUtils.ConsumerType
public int getConsumerType() {
return mConsumerType;
}
@BatteryConsumer.PowerComponent
public int getPowerComponentId() {
return mPowerComponentId;
}
void getQuickNameIconForUid(
final int uid, final String[] packages, final boolean loadDataInBackground) {
// Locale sync to system config in Settings
@@ -395,13 +439,10 @@ public class BatteryEntry {
public String getKey() {
if (mBatteryConsumer instanceof UidBatteryConsumer) {
return Integer.toString(((UidBatteryConsumer) mBatteryConsumer).getUid());
} else if (mBatteryConsumer instanceof SystemBatteryConsumer) {
return "S|" + ((SystemBatteryConsumer) mBatteryConsumer).getDrainType();
} else if (mBatteryConsumer instanceof UserBatteryConsumer) {
return "U|" + ((UserBatteryConsumer) mBatteryConsumer).getUserId();
} else {
Log.w(TAG, "Unsupported BatteryConsumer: " + mBatteryConsumer);
return "";
return "S|" + mPowerComponentId;
}
}
@@ -448,13 +489,6 @@ public class BatteryEntry {
}
}
/**
* Returns the BatteryConsumer of the app described by this entry.
*/
public BatteryConsumer getBatteryConsumer() {
return mBatteryConsumer;
}
/**
* Returns foreground foreground time (in milliseconds) that is attributed to this entry.
*/
@@ -462,10 +496,8 @@ public class BatteryEntry {
if (mBatteryConsumer instanceof UidBatteryConsumer) {
return ((UidBatteryConsumer) mBatteryConsumer).getTimeInStateMs(
UidBatteryConsumer.STATE_FOREGROUND);
} else if (mBatteryConsumer instanceof SystemBatteryConsumer) {
return ((SystemBatteryConsumer) mBatteryConsumer).getUsageDurationMillis();
} else {
return 0;
return mUsageDurationMs;
}
}
@@ -537,52 +569,53 @@ public class BatteryEntry {
}
/**
* Gets name annd icon resource from SystemBatteryConsumer drain type.
* Gets name and icon resource from BatteryConsumer power component ID.
*/
public static NameAndIcon getNameAndIconFromDrainType(
Context context, final int drainType) {
String name = null;
int iconId = 0;
switch (drainType) {
case SystemBatteryConsumer.DRAIN_TYPE_AMBIENT_DISPLAY:
public static NameAndIcon getNameAndIconFromPowerComponent(
Context context, @BatteryConsumer.PowerComponent int powerComponentId) {
String name;
int iconId;
switch (powerComponentId) {
case BatteryConsumer.POWER_COMPONENT_AMBIENT_DISPLAY:
name = context.getResources().getString(R.string.ambient_display_screen_title);
iconId = R.drawable.ic_settings_aod;
break;
case SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH:
case BatteryConsumer.POWER_COMPONENT_BLUETOOTH:
name = context.getResources().getString(R.string.power_bluetooth);
iconId = com.android.internal.R.drawable.ic_settings_bluetooth;
break;
case SystemBatteryConsumer.DRAIN_TYPE_CAMERA:
case BatteryConsumer.POWER_COMPONENT_CAMERA:
name = context.getResources().getString(R.string.power_camera);
iconId = R.drawable.ic_settings_camera;
break;
case SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO:
case BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO:
name = context.getResources().getString(R.string.power_cell);
iconId = R.drawable.ic_cellular_1_bar;
break;
case SystemBatteryConsumer.DRAIN_TYPE_FLASHLIGHT:
case BatteryConsumer.POWER_COMPONENT_FLASHLIGHT:
name = context.getResources().getString(R.string.power_flashlight);
iconId = R.drawable.ic_settings_display;
break;
case SystemBatteryConsumer.DRAIN_TYPE_PHONE:
case BatteryConsumer.POWER_COMPONENT_PHONE:
name = context.getResources().getString(R.string.power_phone);
iconId = R.drawable.ic_settings_voice_calls;
break;
case SystemBatteryConsumer.DRAIN_TYPE_SCREEN:
case BatteryConsumer.POWER_COMPONENT_SCREEN:
name = context.getResources().getString(R.string.power_screen);
iconId = R.drawable.ic_settings_display;
break;
case SystemBatteryConsumer.DRAIN_TYPE_WIFI:
case BatteryConsumer.POWER_COMPONENT_WIFI:
name = context.getResources().getString(R.string.power_wifi);
iconId = R.drawable.ic_settings_wireless;
break;
case SystemBatteryConsumer.DRAIN_TYPE_IDLE:
case SystemBatteryConsumer.DRAIN_TYPE_MEMORY:
case BatteryConsumer.POWER_COMPONENT_IDLE:
case BatteryConsumer.POWER_COMPONENT_MEMORY:
name = context.getResources().getString(R.string.power_idle);
iconId = R.drawable.ic_settings_phone_idle;
break;
case SystemBatteryConsumer.DRAIN_TYPE_CUSTOM:
name = null;
default:
name = DebugUtils.constantToString(BatteryConsumer.class, "POWER_COMPONENT_",
powerComponentId);
iconId = R.drawable.ic_power_system;
break;
}

View File

@@ -15,10 +15,8 @@ package com.android.settings.fuelgauge;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import java.time.Duration;
import java.util.TimeZone;
/** A container class to carry data from {@link ContentValues}. */
public class BatteryHistEntry {
@@ -61,7 +59,7 @@ public class BatteryHistEntry {
public final double mPercentOfTotal;
public final long mForegroundUsageTimeInMs;
public final long mBackgroundUsageTimeInMs;
public final int mDrainType;
public final int mPowerComponentId;
public final int mConsumerType;
// Records the battery intent relative information.
public final int mBatteryLevel;
@@ -85,7 +83,7 @@ public class BatteryHistEntry {
mPercentOfTotal = getDouble(values, KEY_PERCENT_OF_TOTAL);
mForegroundUsageTimeInMs = getLong(values, KEY_FOREGROUND_USAGE_TIME);
mBackgroundUsageTimeInMs = getLong(values, KEY_BACKGROUND_USAGE_TIME);
mDrainType = getInteger(values, KEY_DRAIN_TYPE);
mPowerComponentId = getInteger(values, KEY_DRAIN_TYPE);
mConsumerType = getInteger(values, KEY_CONSUMER_TYPE);
mBatteryLevel = getInteger(values, KEY_BATTERY_LEVEL);
mBatteryStatus = getInteger(values, KEY_BATTERY_STATUS);
@@ -106,7 +104,7 @@ public class BatteryHistEntry {
mPercentOfTotal = getDouble(cursor, KEY_PERCENT_OF_TOTAL);
mForegroundUsageTimeInMs = getLong(cursor, KEY_FOREGROUND_USAGE_TIME);
mBackgroundUsageTimeInMs = getLong(cursor, KEY_BACKGROUND_USAGE_TIME);
mDrainType = getInteger(cursor, KEY_DRAIN_TYPE);
mPowerComponentId = getInteger(cursor, KEY_DRAIN_TYPE);
mConsumerType = getInteger(cursor, KEY_CONSUMER_TYPE);
mBatteryLevel = getInteger(cursor, KEY_BATTERY_LEVEL);
mBatteryStatus = getInteger(cursor, KEY_BATTERY_STATUS);
@@ -141,7 +139,7 @@ public class BatteryHistEntry {
mKey = Long.toString(mUid);
break;
case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY:
mKey = "S|" + mDrainType;
mKey = "S|" + mPowerComponentId;
break;
case ConvertUtils.CONSUMER_TYPE_USER_BATTERY:
mKey = "U|" + mUserId;
@@ -164,7 +162,7 @@ public class BatteryHistEntry {
mPercentOfTotal, mTotalPower, mConsumePower,
Duration.ofMillis(mForegroundUsageTimeInMs).getSeconds(),
Duration.ofMillis(mBackgroundUsageTimeInMs).getSeconds()))
.append(String.format("\n\tdrain=%d|consumer=%d", mDrainType, mConsumerType))
.append(String.format("\n\tdrain=%d|consumer=%d", mPowerComponentId, mConsumerType))
.append(String.format("\n\tbattery=%d|status=%d|health=%d\n}",
mBatteryLevel, mBatteryStatus, mBatteryHealth));
return builder.toString();

View File

@@ -23,13 +23,13 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.BatteryConsumer;
import android.os.BatteryStats;
import android.os.BatteryStatsManager;
import android.os.BatteryUsageStats;
import android.os.BatteryUsageStatsQuery;
import android.os.Build;
import android.os.Process;
import android.os.SystemBatteryConsumer;
import android.os.SystemClock;
import android.os.UidBatteryConsumer;
import android.os.UserHandle;
@@ -195,22 +195,34 @@ public class BatteryUtils {
}
/**
* Returns true if the specified battery consumer should be excluded from the summary
* Returns true if the specified device power component should be excluded from the summary
* battery consumption list.
*/
public boolean shouldHideSystemBatteryConsumer(SystemBatteryConsumer consumer) {
switch (consumer.getDrainType()) {
case SystemBatteryConsumer.DRAIN_TYPE_IDLE:
case SystemBatteryConsumer.DRAIN_TYPE_MOBILE_RADIO:
case SystemBatteryConsumer.DRAIN_TYPE_SCREEN:
case SystemBatteryConsumer.DRAIN_TYPE_BLUETOOTH:
case SystemBatteryConsumer.DRAIN_TYPE_WIFI:
public boolean shouldHideDevicePowerComponent(BatteryConsumer consumer,
@BatteryConsumer.PowerComponent int powerComponentId) {
switch (powerComponentId) {
case BatteryConsumer.POWER_COMPONENT_IDLE:
case BatteryConsumer.POWER_COMPONENT_MOBILE_RADIO:
case BatteryConsumer.POWER_COMPONENT_SCREEN:
case BatteryConsumer.POWER_COMPONENT_BLUETOOTH:
case BatteryConsumer.POWER_COMPONENT_WIFI:
return true;
default:
return consumer.getConsumedPower() < MIN_POWER_THRESHOLD_MILLI_AMP_HOURS;
return consumer.getConsumedPower(powerComponentId)
< MIN_POWER_THRESHOLD_MILLI_AMP_HOURS;
}
}
/**
* Returns true if the specified device custom power component should be excluded from the
* summary battery consumption list.
*/
public boolean shouldHideCustomDevicePowerComponent(BatteryConsumer consumer,
int customPowerComponentId) {
return consumer.getConsumedPowerForCustomComponent(customPowerComponentId)
< MIN_POWER_THRESHOLD_MILLI_AMP_HOURS;
}
/**
* Returns true if one the specified packages belongs to a hidden system module.
*/

View File

@@ -15,14 +15,9 @@ package com.android.settings.fuelgauge;
import android.annotation.IntDef;
import android.content.ContentValues;
import android.os.BatteryConsumer;
import android.os.BatteryUsageStats;
import android.content.Context;
import android.os.SystemBatteryConsumer;
import android.os.UidBatteryConsumer;
import android.os.UserBatteryConsumer;
import android.os.BatteryUsageStats;
import android.os.UserHandle;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
@@ -79,28 +74,6 @@ public final class ConvertUtils {
private ConvertUtils() {}
/** Gets consumer type from {@link BatteryConsumer}. */
@ConsumerType
public static int getConsumerType(BatteryConsumer consumer) {
if (consumer instanceof UidBatteryConsumer) {
return CONSUMER_TYPE_UID_BATTERY;
} else if (consumer instanceof UserBatteryConsumer) {
return CONSUMER_TYPE_USER_BATTERY;
} else if (consumer instanceof SystemBatteryConsumer) {
return CONSUMER_TYPE_SYSTEM_BATTERY;
} else {
return CONSUMER_TYPE_UNKNOWN;
}
}
/** Gets battery drain type for {@link SystemBatteryConsumer}. */
public static int getDrainType(BatteryConsumer consumer) {
if (consumer instanceof SystemBatteryConsumer) {
return ((SystemBatteryConsumer) consumer).getDrainType();
}
return INVALID_DRAIN_TYPE;
}
public static ContentValues convert(
BatteryEntry entry,
BatteryUsageStats batteryUsageStats,
@@ -129,9 +102,9 @@ public final class ConvertUtils {
values.put(BatteryHistEntry.KEY_BACKGROUND_USAGE_TIME,
Long.valueOf(entry.getTimeInBackgroundMs()));
values.put(BatteryHistEntry.KEY_DRAIN_TYPE,
getDrainType(entry.getBatteryConsumer()));
entry.getPowerComponentId());
values.put(BatteryHistEntry.KEY_CONSUMER_TYPE,
getConsumerType(entry.getBatteryConsumer()));
entry.getConsumerType());
} else {
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, FAKE_PACKAGE_NAME);
}