Combine system apps to one item in the battery usage app list.

Bug: 262952385
Test: 262952385
Test: maunal
Change-Id: I078677b13a22f5e2f8f194bb5d5259f8c54c6b1e
This commit is contained in:
Zaiyue Xue
2022-12-17 15:53:40 +08:00
parent 949dd58112
commit e756675e69
12 changed files with 276 additions and 90 deletions

View File

@@ -22,6 +22,7 @@ import android.util.SparseIntArray;
import com.android.settingslib.fuelgauge.Estimate;
import java.util.List;
import java.util.Set;
/**
@@ -34,6 +35,11 @@ public interface PowerUsageFeatureProvider {
*/
boolean isBatteryUsageEnabled(Context context);
/**
* Returns an allowlist of app names combined into the system-apps item
*/
List<String> getSystemAppsAllowlist(Context context);
/**
* Check whether location setting is enabled
*/

View File

@@ -26,6 +26,7 @@ import android.util.SparseIntArray;
import com.android.internal.util.ArrayUtils;
import com.android.settingslib.fuelgauge.Estimate;
import java.util.List;
import java.util.Set;
/** Implementation of {@code PowerUsageFeatureProvider} */
@@ -70,6 +71,11 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
return true;
}
@Override
public List<String> getSystemAppsAllowlist(Context context) {
return null;
}
@Override
public boolean isLocationSettingEnabled(String[] packages) {
return false;

View File

@@ -15,12 +15,12 @@
*/
package com.android.settings.fuelgauge.batteryusage;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
@@ -62,10 +62,10 @@ public class BatteryDiffEntry {
// A BatteryHistEntry corresponding to this diff usage data.
public final BatteryHistEntry mBatteryHistEntry;
protected Context mContext;
private double mTotalConsumePower;
private double mPercentOfTotal;
private Context mContext;
private UserManager mUserManager;
private String mDefaultPackageName = null;
@@ -111,6 +111,11 @@ public class BatteryDiffEntry {
? 0 : (mConsumePower / mTotalConsumePower) * 100.0;
}
/** Gets the total consumed power in a specific time slot. */
public double getTotalConsumePower() {
return mTotalConsumePower;
}
/** Gets the percentage of total consumed power. */
public double getPercentOfTotal() {
return mPercentOfTotal;
@@ -176,23 +181,17 @@ public class BatteryDiffEntry {
/** Whether the current BatteryDiffEntry is system component or not. */
public boolean isSystemEntry() {
if (mBatteryHistEntry.mIsHidden) {
return false;
}
switch (mBatteryHistEntry.mConsumerType) {
case ConvertUtils.CONSUMER_TYPE_USER_BATTERY:
case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY:
return true;
case ConvertUtils.CONSUMER_TYPE_UID_BATTERY:
final int uid = (int) mBatteryHistEntry.mUid;
if (mBatteryHistEntry.mIsHidden
|| uid == BatteryUtils.UID_REMOVED_APPS
|| uid == BatteryUtils.UID_TETHERING) {
return true;
}
final boolean combineSystemComponents =
mContext.getResources().getBoolean(
R.bool.config_battery_combine_system_components);
return combineSystemComponents && isSystemUid(uid);
default:
return false;
}
return false;
}
void loadLabelAndIcon() {
@@ -396,8 +395,44 @@ public class BatteryDiffEntry {
mUserManager.getBadgedIconForUser(icon, new UserHandle(userId));
}
private static boolean isSystemUid(int uid) {
final int appUid = UserHandle.getAppId(uid);
return appUid >= Process.SYSTEM_UID && appUid < Process.FIRST_APPLICATION_UID;
/** Specific battery diff entry for system apps. */
static class SystemAppsBatteryDiffEntry extends BatteryDiffEntry {
SystemAppsBatteryDiffEntry(Context context) {
super(context,
/*foregroundUsageTimeInMs=*/ 0,
/*backgroundUsageTimeInMs=*/ 0,
/*screenOnTimeInMs=*/ 0,
/*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0,
/*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0,
/*cachedUsageConsumePower=*/ 0,
new BatteryHistEntry(new ContentValues()));
}
@Override
public String getKey() {
return "A|SystemApps";
}
@Override
public String getAppLabel() {
return mContext.getString(R.string.battery_usage_system_apps);
}
@Override
public Drawable getAppIcon() {
return mContext.getDrawable(R.drawable.ic_power_system);
}
@Override
public boolean validForRestriction() {
return false;
}
@Override
public boolean isSystemEntry() {
return false;
}
}
}

View File

@@ -200,11 +200,11 @@ public class BatteryEntry {
/** Battery entry for a power component of AggregateBatteryConsumer */
public BatteryEntry(Context context, int powerComponentId, double devicePowerMah,
long usageDurationMs) {
long usageDurationMs, boolean isHidden) {
mContext = context;
mBatteryConsumer = null;
mUid = Process.INVALID_UID;
mIsHidden = false;
mIsHidden = isHidden;
mPowerComponentId = powerComponentId;
mConsumedPower = devicePowerMah;
mUsageDurationMs = usageDurationMs;

View File

@@ -242,7 +242,7 @@ public class BatteryUsageBreakdownController extends BasePreferenceController
Log.w(TAG, "cannot find app resource for:" + entry.getPackageName());
continue;
}
final String prefKey = entry.mBatteryHistEntry.getKey();
final String prefKey = entry.getKey();
PowerGaugePreference pref = mAppListPreferenceGroup.findPreference(prefKey);
if (pref != null) {
isAdded = true;

View File

@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge.batteryusage;
import static com.android.settings.fuelgauge.batteryusage.ConvertUtils.getEffectivePackageName;
import static com.android.settings.fuelgauge.batteryusage.ConvertUtils.utcToLocalTime;
import android.app.Application;
import android.app.usage.IUsageStatsManager;
import android.app.usage.UsageEvents;
import android.app.usage.UsageEvents.Event;
@@ -52,6 +53,7 @@ import com.android.internal.os.PowerProfile;
import com.android.settings.Utils;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.fuelgauge.BatteryStatus;
import java.time.Duration;
@@ -75,6 +77,8 @@ import java.util.stream.Stream;
public final class DataProcessor {
private static final boolean DEBUG = false;
private static final String TAG = "DataProcessor";
private static final int POWER_COMPONENT_SYSTEM_SERVICES = 7;
private static final int POWER_COMPONENT_WAKELOCK = 12;
private static final int MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP = 10;
private static final int MIN_DAILY_DATA_SIZE = 2;
private static final int MIN_TIMESTAMP_DATA_SIZE = 2;
@@ -1714,6 +1718,7 @@ public final class DataProcessor {
hideBackgroundUsageTimeSet);
batteryDiffData.setTotalConsumePower();
batteryDiffData.sortEntries();
combineIntoSystemApps(context, batteryDiffData);
});
});
}
@@ -1739,6 +1744,64 @@ public final class DataProcessor {
}
}
private static void combineIntoSystemApps(
final Context context, final BatteryDiffData batteryDiffData) {
final List<String> systemAppsAllowlist =
FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context)
.getSystemAppsAllowlist(context);
final Application application = (Application) context.getApplicationContext();
final ApplicationsState applicationsState =
application == null ? null : ApplicationsState.getInstance(application);
BatteryDiffEntry.SystemAppsBatteryDiffEntry systemAppsDiffEntry = null;
final Iterator<BatteryDiffEntry> appListIterator =
batteryDiffData.getAppDiffEntryList().iterator();
while (appListIterator.hasNext()) {
final BatteryDiffEntry batteryDiffEntry = appListIterator.next();
if (needsCombineInSystemApp(batteryDiffEntry, systemAppsAllowlist, applicationsState)) {
if (systemAppsDiffEntry == null) {
systemAppsDiffEntry = new BatteryDiffEntry.SystemAppsBatteryDiffEntry(context);
}
systemAppsDiffEntry.mConsumePower += batteryDiffEntry.mConsumePower;
systemAppsDiffEntry.setTotalConsumePower(
batteryDiffEntry.getTotalConsumePower());
appListIterator.remove();
}
}
if (systemAppsDiffEntry != null) {
batteryDiffData.getAppDiffEntryList().add(systemAppsDiffEntry);
}
}
@VisibleForTesting
static boolean needsCombineInSystemApp(final BatteryDiffEntry batteryDiffEntry,
final List<String> systemAppsAllowlist, final ApplicationsState applicationsState) {
if (batteryDiffEntry.mBatteryHistEntry.mIsHidden) {
return true;
}
final String packageName = batteryDiffEntry.getPackageName();
if (packageName == null || packageName.isEmpty()) {
return false;
}
if (systemAppsAllowlist != null && systemAppsAllowlist.contains(packageName)) {
return true;
}
if (applicationsState == null) {
return false;
}
final ApplicationsState.AppEntry appEntry =
applicationsState.getEntry(packageName, /* userId= */ 0);
if (appEntry == null || appEntry.info == null) {
return false;
}
return !ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT.filterApp(
appEntry);
}
private static boolean shouldShowBatteryAttributionList(final Context context) {
final PowerProfile powerProfile = new PowerProfile(context);
// Cheap hack to try to figure out if the power_profile.xml was populated.
@@ -1804,7 +1867,9 @@ public final class DataProcessor {
componentId++) {
results.add(new BatteryEntry(context, componentId,
deviceConsumer.getConsumedPower(componentId),
deviceConsumer.getUsageDurationMillis(componentId)));
deviceConsumer.getUsageDurationMillis(componentId),
componentId == POWER_COMPONENT_SYSTEM_SERVICES
|| componentId == POWER_COMPONENT_WAKELOCK));
}
for (int componentId = BatteryConsumer.FIRST_CUSTOM_POWER_COMPONENT_ID;