Support badged icon for work profile mode and add system checking am: 5aa3c23535
Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/14215687 Change-Id: I279ad55d915c81970e77a04071fe751b153fb502
This commit is contained in:
@@ -20,6 +20,9 @@ import android.content.pm.PackageInfo;
|
|||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.os.Process;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
import android.os.UserManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
@@ -51,6 +54,7 @@ public final class BatteryDiffEntry {
|
|||||||
private double mPercentOfTotal;
|
private double mPercentOfTotal;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
private UserManager mUserManager;
|
||||||
private String mDefaultPackageName = null;
|
private String mDefaultPackageName = null;
|
||||||
|
|
||||||
@VisibleForTesting String mAppLabel = null;
|
@VisibleForTesting String mAppLabel = null;
|
||||||
@@ -68,6 +72,7 @@ public final class BatteryDiffEntry {
|
|||||||
mForegroundUsageTimeInMs = foregroundUsageTimeInMs;
|
mForegroundUsageTimeInMs = foregroundUsageTimeInMs;
|
||||||
mBackgroundUsageTimeInMs = backgroundUsageTimeInMs;
|
mBackgroundUsageTimeInMs = backgroundUsageTimeInMs;
|
||||||
mBatteryHistEntry = batteryHistEntry;
|
mBatteryHistEntry = batteryHistEntry;
|
||||||
|
mUserManager = context.getSystemService(UserManager.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the total consumed power in a specific time slot. */
|
/** Sets the total consumed power in a specific time slot. */
|
||||||
@@ -112,6 +117,19 @@ public final class BatteryDiffEntry {
|
|||||||
return mDefaultPackageName;
|
return mDefaultPackageName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Whether the current BatteryDiffEntry is system component or not. */
|
||||||
|
public boolean isSystemEntry() {
|
||||||
|
switch (mBatteryHistEntry.mConsumerType) {
|
||||||
|
case ConvertUtils.CONSUMER_TYPE_USER_BATTERY:
|
||||||
|
case ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY:
|
||||||
|
return true;
|
||||||
|
case ConvertUtils.CONSUMER_TYPE_UID_BATTERY:
|
||||||
|
return isSystemUid((int) mBatteryHistEntry.mUid)
|
||||||
|
|| mBatteryHistEntry.mIsHidden;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void loadLabelAndIcon() {
|
private void loadLabelAndIcon() {
|
||||||
if (mIsLoaded) {
|
if (mIsLoaded) {
|
||||||
return;
|
return;
|
||||||
@@ -151,7 +169,9 @@ public final class BatteryDiffEntry {
|
|||||||
if (mAppIcon == null) {
|
if (mAppIcon == null) {
|
||||||
mAppIcon = mContext.getPackageManager().getDefaultActivityIcon();
|
mAppIcon = mContext.getPackageManager().getDefaultActivityIcon();
|
||||||
}
|
}
|
||||||
if (mAppLabel != null && mAppIcon != null) {
|
// Adds badge icon into app icon for work profile.
|
||||||
|
mAppIcon = getBadgeIconForUser(mAppIcon);
|
||||||
|
if (mAppLabel != null || mAppIcon != null) {
|
||||||
sResourceCache.put(
|
sResourceCache.put(
|
||||||
mBatteryHistEntry.getKey(),
|
mBatteryHistEntry.getKey(),
|
||||||
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /*iconId=*/ 0));
|
new BatteryEntry.NameAndIcon(mAppLabel, mAppIcon, /*iconId=*/ 0));
|
||||||
@@ -235,6 +255,17 @@ public final class BatteryDiffEntry {
|
|||||||
sResourceCache.clear();
|
sResourceCache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Drawable getBadgeIconForUser(Drawable icon) {
|
||||||
|
final int userId = UserHandle.getUserId((int) mBatteryHistEntry.mUid);
|
||||||
|
final UserHandle userHandle = new UserHandle(userId);
|
||||||
|
return mUserManager.getBadgedIconForUser(icon, userHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isSystemUid(int uid) {
|
||||||
|
final int appUid = UserHandle.getAppId(uid);
|
||||||
|
return appUid >= Process.SYSTEM_UID && appUid < Process.FIRST_APPLICATION_UID;
|
||||||
|
}
|
||||||
|
|
||||||
private static <T> T getNonNull(T originalObj, T newObj) {
|
private static <T> T getNonNull(T originalObj, T newObj) {
|
||||||
return newObj != null ? newObj : originalObj;
|
return newObj != null ? newObj : originalObj;
|
||||||
}
|
}
|
||||||
|
@@ -28,6 +28,7 @@ import android.content.pm.PackageManager;
|
|||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.SystemBatteryConsumer;
|
import android.os.SystemBatteryConsumer;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
import android.os.UserHandle;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -52,6 +53,7 @@ public final class BatteryDiffEntryTest {
|
|||||||
@Mock private UserManager mockUserManager;
|
@Mock private UserManager mockUserManager;
|
||||||
@Mock private Drawable mockDrawable;
|
@Mock private Drawable mockDrawable;
|
||||||
@Mock private Drawable mockDrawable2;
|
@Mock private Drawable mockDrawable2;
|
||||||
|
@Mock private Drawable mockBadgedDrawable;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -209,21 +211,29 @@ public final class BatteryDiffEntryTest {
|
|||||||
public void testGetAppIcon_uidConsumerWithNullIcon_returnDefaultActivityIcon()
|
public void testGetAppIcon_uidConsumerWithNullIcon_returnDefaultActivityIcon()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
final BatteryDiffEntry entry = createBatteryDiffEntry(mockDrawable);
|
final BatteryDiffEntry entry = createBatteryDiffEntry(mockDrawable);
|
||||||
|
final int userId = UserHandle.getUserId(1001);
|
||||||
|
doReturn(mockBadgedDrawable).when(mockUserManager)
|
||||||
|
.getBadgedIconForUser(mockDrawable, new UserHandle(userId));
|
||||||
|
|
||||||
entry.mAppIcon = null;
|
entry.mAppIcon = null;
|
||||||
assertThat(entry.getAppIcon()).isEqualTo(mockDrawable);
|
assertThat(entry.getAppIcon()).isEqualTo(mockBadgedDrawable);
|
||||||
assertThat(BatteryDiffEntry.sResourceCache).hasSize(1);
|
assertThat(BatteryDiffEntry.sResourceCache).hasSize(1);
|
||||||
// Verifies the app label in the cache.
|
// Verifies the app label in the cache.
|
||||||
final BatteryEntry.NameAndIcon nameAndIcon =
|
final BatteryEntry.NameAndIcon nameAndIcon =
|
||||||
BatteryDiffEntry.sResourceCache.get(entry.mBatteryHistEntry.getKey());
|
BatteryDiffEntry.sResourceCache.get(entry.mBatteryHistEntry.getKey());
|
||||||
assertThat(nameAndIcon.icon).isEqualTo(mockDrawable);
|
assertThat(nameAndIcon.icon).isEqualTo(mockBadgedDrawable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testClearCache_switchLocale_clearCacheIconAndLabel() throws Exception {
|
public void testClearCache_switchLocale_clearCacheIconAndLabel() throws Exception {
|
||||||
|
final int userId = UserHandle.getUserId(1001);
|
||||||
|
doReturn(mockBadgedDrawable).when(mockUserManager)
|
||||||
|
.getBadgedIconForUser(mockDrawable, new UserHandle(userId));
|
||||||
|
doReturn(mockDrawable2).when(mockUserManager)
|
||||||
|
.getBadgedIconForUser(mockDrawable2, new UserHandle(userId));
|
||||||
Locale.setDefault(new Locale("en_US"));
|
Locale.setDefault(new Locale("en_US"));
|
||||||
final BatteryDiffEntry entry1 = createBatteryDiffEntry(mockDrawable);
|
final BatteryDiffEntry entry1 = createBatteryDiffEntry(mockDrawable);
|
||||||
assertThat(entry1.getAppIcon()).isEqualTo(mockDrawable);
|
assertThat(entry1.getAppIcon()).isEqualTo(mockBadgedDrawable);
|
||||||
// Switch the locale into another one.
|
// Switch the locale into another one.
|
||||||
Locale.setDefault(new Locale("zh_TW"));
|
Locale.setDefault(new Locale("zh_TW"));
|
||||||
|
|
||||||
@@ -237,6 +247,64 @@ public final class BatteryDiffEntryTest {
|
|||||||
assertThat(nameAndIcon.icon).isEqualTo(mockDrawable2);
|
assertThat(nameAndIcon.icon).isEqualTo(mockDrawable2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsSystemEntry_userBattery_returnTrue() {
|
||||||
|
final BatteryDiffEntry entry =
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
ConvertUtils.CONSUMER_TYPE_USER_BATTERY,
|
||||||
|
/*uid=*/ 0, /*isHidden=*/ false);
|
||||||
|
assertThat(entry.isSystemEntry()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsSystemEntry_systemBattery_returnTrue() {
|
||||||
|
final BatteryDiffEntry entry =
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
ConvertUtils.CONSUMER_TYPE_SYSTEM_BATTERY,
|
||||||
|
/*uid=*/ 0, /*isHidden=*/ false);
|
||||||
|
assertThat(entry.isSystemEntry()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsSystemEntry_uidBattery_returnFalse() {
|
||||||
|
final BatteryDiffEntry entry =
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
|
/*uid=*/ 123, /*isHidden=*/ false);
|
||||||
|
assertThat(entry.isSystemEntry()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsSystemEntry_uidBatteryWithHiddenState_returnTrue() {
|
||||||
|
final BatteryDiffEntry entry =
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
|
/*uid=*/ 123, /*isHidden=*/ true);
|
||||||
|
assertThat(entry.isSystemEntry()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsSystemEntry_uidBatteryWithSystemProcess_returnTrue() {
|
||||||
|
final BatteryDiffEntry entry =
|
||||||
|
createBatteryDiffEntry(
|
||||||
|
ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
|
||||||
|
/*uid=*/ 1230, /*isHidden=*/ false);
|
||||||
|
assertThat(entry.isSystemEntry()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private BatteryDiffEntry createBatteryDiffEntry(
|
||||||
|
int consumerType, long uid, boolean isHidden) {
|
||||||
|
final ContentValues values = getContentValuesWithType(consumerType);
|
||||||
|
values.put("isHidden", isHidden);
|
||||||
|
values.put("uid", uid);
|
||||||
|
return new BatteryDiffEntry(
|
||||||
|
mContext,
|
||||||
|
/*foregroundUsageTimeInMs=*/ 0,
|
||||||
|
/*backgroundUsageTimeInMs=*/ 0,
|
||||||
|
/*consumePower=*/ 0,
|
||||||
|
new BatteryHistEntry(values));
|
||||||
|
}
|
||||||
|
|
||||||
private BatteryDiffEntry createBatteryDiffEntry(
|
private BatteryDiffEntry createBatteryDiffEntry(
|
||||||
double consumePower, BatteryHistEntry batteryHistEntry) {
|
double consumePower, BatteryHistEntry batteryHistEntry) {
|
||||||
final BatteryDiffEntry entry = new BatteryDiffEntry(
|
final BatteryDiffEntry entry = new BatteryDiffEntry(
|
||||||
|
Reference in New Issue
Block a user