Disable app usage item if this item is not clickable

some items are not clickable to launch the restriction page in the
battery usage list, we will apply the disabled visual in the
preferrence item to improve the UX (avoid users click the item without
any action)

Bug: 188751551
Bug: 188663505
Test: make SettingsgRoboTests
Change-Id: Ib8925b8e191117543bb1c74d6d01191e3043fc73
This commit is contained in:
ykhung
2021-05-25 14:34:50 +08:00
committed by YUKAI HUNG
parent f49190d769
commit 46bcdda9b9
5 changed files with 178 additions and 41 deletions

View File

@@ -39,6 +39,8 @@ public class BatteryDiffEntry {
static Locale sCurrentLocale = null;
// Caches app label and icon to improve loading performance.
static final Map<String, BatteryEntry.NameAndIcon> sResourceCache = new HashMap<>();
// Whether a specific item is valid to launch restriction page?
static final Map<String, Boolean> sValidForRestriction = new HashMap<>();
/** A comparator for {@link BatteryDiffEntry} based on consumed percentage. */
public static final Comparator<BatteryDiffEntry> COMPARATOR =
@@ -60,6 +62,7 @@ public class BatteryDiffEntry {
@VisibleForTesting String mAppLabel = null;
@VisibleForTesting Drawable mAppIcon = null;
@VisibleForTesting boolean mIsLoaded = false;
@VisibleForTesting boolean mValidForRestriction = true;
public BatteryDiffEntry(
Context context,
@@ -129,6 +132,12 @@ public class BatteryDiffEntry {
? mDefaultPackageName : mBatteryHistEntry.mPackageName;
}
/** Whether this item is valid for users to launch restriction page? */
public boolean validForRestriction() {
loadLabelAndIcon();
return mValidForRestriction;
}
/** Whether the current BatteryDiffEntry is system component or not. */
public boolean isSystemEntry() {
switch (mBatteryHistEntry.mConsumerType) {
@@ -146,7 +155,29 @@ public class BatteryDiffEntry {
if (mIsLoaded) {
return;
}
// Checks whether we have cached data or not first before fetching.
final BatteryEntry.NameAndIcon nameAndIcon = getCache();
if (nameAndIcon != null) {
mAppLabel = nameAndIcon.name;
mAppIcon = nameAndIcon.icon;
mAppIconId = nameAndIcon.iconId;
}
final Boolean validForRestriction = sValidForRestriction.get(getKey());
if (validForRestriction != null) {
mValidForRestriction = validForRestriction;
}
// Both nameAndIcon and restriction configuration have cached data.
if (nameAndIcon != null && validForRestriction != null) {
Log.w(TAG, String.format("cannot find cache data nameAndIcon:%s "
+ "validForRestriction:%s", nameAndIcon, validForRestriction));
return;
}
mIsLoaded = true;
// Configures whether we can launch restriction page or not.
updateRestrictionFlagState();
sValidForRestriction.put(getKey(), Boolean.valueOf(mValidForRestriction));
// Loads application icon and label based on consumer type.
switch (mBatteryHistEntry.mConsumerType) {
case ConvertUtils.CONSUMER_TYPE_USER_BATTERY:
@@ -156,6 +187,9 @@ public class BatteryDiffEntry {
if (nameAndIconForUser != null) {
mAppIcon = nameAndIconForUser.icon;
mAppLabel = nameAndIconForUser.name;
sResourceCache.put(
getKey(),
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /*iconId=*/ 0));
}
break;
case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY:
@@ -168,15 +202,12 @@ public class BatteryDiffEntry {
mAppIconId = nameAndIconForSystem.iconId;
mAppIcon = mContext.getDrawable(nameAndIconForSystem.iconId);
}
sResourceCache.put(
getKey(),
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, mAppIconId));
}
break;
case ConvertUtils.CONSUMER_TYPE_UID_BATTERY:
final BatteryEntry.NameAndIcon nameAndIcon = getCache();
if (nameAndIcon != null) {
mAppLabel = nameAndIcon.name;
mAppIcon = nameAndIcon.icon;
break;
}
loadNameAndIconForUid();
// Uses application default icon if we cannot find it from package.
if (mAppIcon == null) {
@@ -186,13 +217,47 @@ public class BatteryDiffEntry {
mAppIcon = getBadgeIconForUser(mAppIcon);
if (mAppLabel != null || mAppIcon != null) {
sResourceCache.put(
mBatteryHistEntry.getKey(),
getKey(),
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /*iconId=*/ 0));
}
break;
}
}
@VisibleForTesting
String getKey() {
return mBatteryHistEntry.getKey();
}
@VisibleForTesting
void updateRestrictionFlagState() {
mValidForRestriction = true;
if (!mBatteryHistEntry.isAppEntry()) {
return;
}
final boolean isValidPackage =
BatteryUtils.getInstance(mContext).getPackageUid(getPackageName())
!= BatteryUtils.UID_NULL;
if (!isValidPackage) {
mValidForRestriction = false;
return;
}
try {
mValidForRestriction =
mContext.getPackageManager().getPackageInfo(
getPackageName(),
PackageManager.MATCH_DISABLED_COMPONENTS
| PackageManager.MATCH_ANY_USER
| PackageManager.GET_SIGNATURES
| PackageManager.GET_PERMISSIONS)
!= null;
} catch (Exception e) {
Log.e(TAG, String.format("getPackageInfo() error %s for package=%s",
e.getCause(), getPackageName()));
mValidForRestriction = false;
}
}
private BatteryEntry.NameAndIcon getCache() {
final Locale locale = Locale.getDefault();
if (sCurrentLocale != locale) {
@@ -201,7 +266,7 @@ public class BatteryDiffEntry {
sCurrentLocale = locale;
clearCache();
}
return sResourceCache.get(mBatteryHistEntry.getKey());
return sResourceCache.get(getKey());
}
private void loadNameAndIconForUid() {
@@ -258,7 +323,8 @@ public class BatteryDiffEntry {
public String toString() {
final StringBuilder builder = new StringBuilder()
.append("BatteryDiffEntry{")
.append("\n\tname=" + mAppLabel)
.append(String.format("\n\tname=%s restrictable=%b",
mAppLabel, mValidForRestriction))
.append(String.format("\n\tconsume=%.2f%% %f/%f",
mPercentOfTotal, mConsumePower, mTotalConsumePower))
.append(String.format("\n\tforeground:%s background:%s",
@@ -274,6 +340,7 @@ public class BatteryDiffEntry {
static void clearCache() {
sResourceCache.clear();
sValidForRestriction.clear();
}
private Drawable getBadgeIconForUser(Drawable icon) {