Load application icon and label for UID battery consumer type
Bug: 185187669 Test: make SettingsRoboTests Test: make SettingsGoogleRoboTests Change-Id: Idb77a1bd48f7f4b66b86ff12c166a204d17bd9ec
This commit is contained in:
@@ -22,6 +22,8 @@ import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Comparator;
|
||||
|
||||
@@ -42,9 +44,11 @@ public final class BatteryDiffEntry {
|
||||
private double mPercentOfTotal;
|
||||
|
||||
private Context mContext;
|
||||
private String mAppLabel = null;
|
||||
private Drawable mAppIcon = null;
|
||||
private boolean mIsLoaded = false;
|
||||
private String mDefaultPackageName = null;
|
||||
|
||||
@VisibleForTesting String mAppLabel = null;
|
||||
@VisibleForTesting Drawable mAppIcon = null;
|
||||
@VisibleForTesting boolean mIsLoaded = false;
|
||||
|
||||
public BatteryDiffEntry(
|
||||
Context context,
|
||||
@@ -84,13 +88,28 @@ public final class BatteryDiffEntry {
|
||||
/** Gets the app label name for this entry. */
|
||||
public String getAppLabel() {
|
||||
loadLabelAndIcon();
|
||||
return mAppLabel;
|
||||
// Returns default applicationn label if we cannot find it.
|
||||
return mAppLabel == null || mAppLabel.length() == 0
|
||||
? mBatteryHistEntry.mAppLabel
|
||||
: mAppLabel;
|
||||
}
|
||||
|
||||
/** Gets the app icon {@link Drawable} for this entry. */
|
||||
public Drawable getAppIcon() {
|
||||
loadLabelAndIcon();
|
||||
return mAppIcon;
|
||||
if (mBatteryHistEntry.mConsumerType !=
|
||||
ConvertUtils.CONSUMER_TYPE_UID_BATTERY) {
|
||||
return mAppIcon;
|
||||
}
|
||||
// Returns default application icon if UID_BATTERY icon is null.
|
||||
return mAppIcon == null
|
||||
? mContext.getPackageManager().getDefaultActivityIcon()
|
||||
: mAppIcon;
|
||||
}
|
||||
|
||||
/** Gets the searching package name for UID battery type. */
|
||||
public String getPackageName() {
|
||||
return mDefaultPackageName;
|
||||
}
|
||||
|
||||
private void loadLabelAndIcon() {
|
||||
@@ -127,7 +146,46 @@ public final class BatteryDiffEntry {
|
||||
}
|
||||
|
||||
private void loadNameAndIconForUid() {
|
||||
// TODO(b/185187669) fetch label and icon for UID battery type
|
||||
final String packageName = mBatteryHistEntry.mPackageName;
|
||||
final PackageManager packageManager = mContext.getPackageManager();
|
||||
// Gets the application label from PackageManager.
|
||||
if (packageName != null && packageName.length() != 0) {
|
||||
try {
|
||||
final ApplicationInfo appInfo =
|
||||
packageManager.getApplicationInfo(packageName, /*no flags*/ 0);
|
||||
mAppLabel = packageManager.getApplicationLabel(appInfo).toString();
|
||||
} catch (NameNotFoundException e) {
|
||||
Log.e(TAG, "failed to retrieve ApplicationInfo for: " + packageName);
|
||||
mAppLabel = packageName;
|
||||
}
|
||||
}
|
||||
|
||||
final int uid = (int) mBatteryHistEntry.mUid;
|
||||
final String[] packages = packageManager.getPackagesForUid(uid);
|
||||
// Loads special defined application label and icon if available.
|
||||
if (packages == null || packages.length == 0) {
|
||||
final BatteryEntry.NameAndIcon nameAndIcon =
|
||||
BatteryEntry.getNameAndIconFromUid(mContext, mAppLabel, uid);
|
||||
mAppLabel = nameAndIcon.name;
|
||||
mAppIcon = nameAndIcon.icon;
|
||||
}
|
||||
|
||||
final BatteryEntry.NameAndIcon nameAndIcon =
|
||||
BatteryEntry.loadNameAndIcon(
|
||||
mContext, uid, /*uid=*/ null, /*batteryEntry=*/ null,
|
||||
packageName, mAppLabel, mAppIcon);
|
||||
// Clears BatteryEntry internal cache since we will have another one.
|
||||
BatteryEntry.clearUidCache();
|
||||
if (nameAndIcon != null) {
|
||||
mAppLabel = getNonNull(mAppLabel, nameAndIcon.name);
|
||||
mAppIcon = getNonNull(mAppIcon, nameAndIcon.icon);
|
||||
mDefaultPackageName = nameAndIcon.packageName;
|
||||
if (mDefaultPackageName != null
|
||||
&& !mDefaultPackageName.equals(nameAndIcon.packageName)) {
|
||||
Log.w(TAG, String.format("found different package: %s | %s",
|
||||
mDefaultPackageName, nameAndIcon.packageName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -144,4 +202,8 @@ public final class BatteryDiffEntry {
|
||||
mBatteryHistEntry.mPackageName, mBatteryHistEntry.mUid));
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private static <T> T getNonNull(T originalObj, T newObj) {
|
||||
return newObj != null ? newObj : originalObj;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user