Add badge colors for dark theme

Test: manual
Test: atest UserManagerTest#testProfileTypeInformation
Fixes: 149669756
Change-Id: Ib3fd377a4902bbe936d574dc52bac4eae6e25dd4
This commit is contained in:
Beverly
2020-04-27 16:15:15 -04:00
parent 27f4c791a2
commit 2b4306a8ec
10 changed files with 116 additions and 9 deletions

View File

@@ -1574,7 +1574,7 @@ public class ApplicationPackageManager extends PackageManager {
}
Drawable badge = new LauncherIcons(mContext).getBadgeDrawable(
getUserManager().getUserIconBadgeResId(user.getIdentifier()),
getUserBadgeColor(user));
getUserBadgeColor(user, false));
return getBadgedDrawable(icon, badge, null, true);
}
@@ -1588,8 +1588,16 @@ public class ApplicationPackageManager extends PackageManager {
return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
}
/** Returns the color of the user's actual badge (not the badge's shadow). */
private int getUserBadgeColor(UserHandle user) {
/**
* Returns the color of the user's actual badge (not the badge's shadow).
* @param checkTheme whether to check the theme to determine the badge color. This should be
* true if the background is determined by the theme. Otherwise, if
* checkTheme is false, returns the color assuming a light background.
*/
private int getUserBadgeColor(UserHandle user, boolean checkTheme) {
if (checkTheme && mContext.getResources().getConfiguration().isNightModeActive()) {
return getUserManager().getUserBadgeDarkColor(user.getIdentifier());
}
return getUserManager().getUserBadgeColor(user.getIdentifier());
}
@@ -1603,11 +1611,14 @@ public class ApplicationPackageManager extends PackageManager {
}
Drawable badgeForeground = getDrawableForDensity(
getUserManager().getUserBadgeResId(user.getIdentifier()), density);
badgeForeground.setTint(getUserBadgeColor(user));
badgeForeground.setTint(getUserBadgeColor(user, false));
Drawable badge = new LayerDrawable(new Drawable[] {badgeColor, badgeForeground });
return badge;
}
/**
* Returns the badge color based on whether device has dark theme enabled or not.
*/
@Override
public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
if (!hasUserBadge(user.getIdentifier())) {
@@ -1616,7 +1627,7 @@ public class ApplicationPackageManager extends PackageManager {
Drawable badge = getDrawableForDensity(
getUserManager().getUserBadgeNoBackgroundResId(user.getIdentifier()), density);
if (badge != null) {
badge.setTint(getUserBadgeColor(user));
badge.setTint(getUserBadgeColor(user, true));
}
return badge;
}

View File

@@ -108,6 +108,7 @@ interface IUserManager {
int getUserBadgeNoBackgroundResId(int userId);
int getUserBadgeLabelResId(int userId);
int getUserBadgeColorResId(int userId);
int getUserBadgeDarkColorResId(int userId);
boolean hasBadge(int userId);
boolean isUserUnlocked(int userId);
boolean isUserRunning(int userId);

View File

@@ -3662,7 +3662,8 @@ public class UserManager {
}
/**
* Returns the badge color for the given user (generally to color a profile's icon's badge).
* Returns the light theme badge color for the given user (generally to color a profile's
* icon's badge).
*
* <p>To check whether a badge color is expected for the user, first call {@link #hasBadge}.
*
@@ -3681,6 +3682,27 @@ public class UserManager {
}
}
/**
* Returns the dark theme badge color for the given user (generally to color a profile's icon's
* badge).
*
* <p>To check whether a badge color is expected for the user, first call {@link #hasBadge}.
*
* @return the color (not the resource ID) to be used for the user's badge
* @throws Resources.NotFoundException if no valid badge color exists for this user
*
* @see #getBadgedIconForUser more information about badging in general
* @hide
*/
public @ColorInt int getUserBadgeDarkColor(@UserIdInt int userId) {
try {
final int resourceId = mService.getUserBadgeDarkColorResId(userId);
return Resources.getSystem().getColor(resourceId, null);
} catch (RemoteException re) {
throw re.rethrowFromSystemServer();
}
}
/**
* Returns the Resource ID of the user's icon badge.
*

View File

@@ -183,6 +183,10 @@
<color name="profile_badge_2">#ffff6d00</color><!-- Orange -->
<color name="profile_badge_3">#ff22f033</color><!-- Green -->
<color name="profile_badge_1_dark">#ff8ab4f8</color><!-- Blue 300-->
<color name="profile_badge_2_dark">#fffdd663</color><!-- Orange 300 -->
<color name="profile_badge_3_dark">#ff81c995</color><!-- Green 300 -->
<!-- Default instant app badge color -->
<color name="instant_app_badge">#ff757575</color><!-- Grey -->

View File

@@ -1446,6 +1446,9 @@
<java-symbol type="color" name="profile_badge_1" />
<java-symbol type="color" name="profile_badge_2" />
<java-symbol type="color" name="profile_badge_3" />
<java-symbol type="color" name="profile_badge_1_dark" />
<java-symbol type="color" name="profile_badge_2_dark" />
<java-symbol type="color" name="profile_badge_3_dark" />
<java-symbol type="color" name="instant_app_badge" />
<java-symbol type="layout" name="action_bar_home" />

View File

@@ -1330,18 +1330,36 @@ public class UserManagerService extends IUserManager.Stub {
return userTypeDetails.getBadgeLabel(badgeIndex);
}
/**
* @return the color (not the resource ID) to be used for the user's badge in light theme
*/
@Override
public @ColorRes int getUserBadgeColorResId(@UserIdInt int userId) {
checkManageOrInteractPermissionIfCallerInOtherProfileGroup(userId,
"getUserBadgeColorResId");
final UserInfo userInfo = getUserInfoNoChecks(userId);
final UserTypeDetails userTypeDetails = getUserTypeDetails(userInfo);
if (userInfo == null || userTypeDetails == null || !userTypeDetails.hasBadge()) {
Slog.e(LOG_TAG, "Requested badge dark color for non-badged user " + userId);
return Resources.ID_NULL;
}
return userTypeDetails.getBadgeColor(userInfo.profileBadge);
}
/**
* @return the color (not the resource ID) to be used for the user's badge in dark theme
*/
@Override
public @ColorRes int getUserBadgeDarkColorResId(@UserIdInt int userId) {
checkManageOrInteractPermissionIfCallerInOtherProfileGroup(userId,
"getUserBadgeDarkColorResId");
final UserInfo userInfo = getUserInfoNoChecks(userId);
final UserTypeDetails userTypeDetails = getUserTypeDetails(userInfo);
if (userInfo == null || userTypeDetails == null || !userTypeDetails.hasBadge()) {
Slog.e(LOG_TAG, "Requested badge color for non-badged user " + userId);
return Resources.ID_NULL;
}
final int badgeIndex = userInfo.profileBadge;
return userTypeDetails.getBadgeColor(badgeIndex);
return userTypeDetails.getDarkThemeBadgeColor(userInfo.profileBadge);
}
@Override

View File

@@ -116,11 +116,23 @@ public final class UserTypeDetails {
*/
private final @Nullable int[] mBadgeColors;
/**
* Resource ID ({@link ColorRes}) of the colors badge put on icons when in dark theme.
* (The value is a resource ID referring to the color; it is not the color value itself).
*
* <p>This is an array because, in general, there may be multiple users of the same user type.
* In this case, the user is indexed according to its {@link UserInfo#profileBadge}.
*
* <p>Must be set if mIconBadge is set.
*/
private final @Nullable int[] mDarkThemeBadgeColors;
private UserTypeDetails(@NonNull String name, boolean enabled, int maxAllowed,
@UserInfoFlag int baseType, @UserInfoFlag int defaultUserInfoPropertyFlags, int label,
int maxAllowedPerParent,
int iconBadge, int badgePlain, int badgeNoBackground,
@Nullable int[] badgeLabels, @Nullable int[] badgeColors,
@Nullable int[] darkThemeBadgeColors,
@Nullable Bundle defaultRestrictions) {
this.mName = name;
this.mEnabled = enabled;
@@ -136,6 +148,7 @@ public final class UserTypeDetails {
this.mLabel = label;
this.mBadgeLabels = badgeLabels;
this.mBadgeColors = badgeColors;
this.mDarkThemeBadgeColors = darkThemeBadgeColors;
}
/**
@@ -222,6 +235,19 @@ public final class UserTypeDetails {
return mBadgeColors[Math.min(badgeIndex, mBadgeColors.length - 1)];
}
/**
* Returns the Resource ID of the badgeIndexth dark theme badge color, where the badgeIndex is
* expected to be the {@link UserInfo#profileBadge} of the user.
* If dark theme badge colors haven't been set, use the light theme badge color.
* If badgeIndex exceeds the number of colors, returns the color for the highest index.
*/
public @ColorRes int getDarkThemeBadgeColor(int badgeIndex) {
if (mDarkThemeBadgeColors == null || mDarkThemeBadgeColors.length == 0 || badgeIndex < 0) {
return getBadgeColor(badgeIndex);
}
return mDarkThemeBadgeColors[Math.min(badgeIndex, mDarkThemeBadgeColors.length - 1)];
}
public boolean isProfile() {
return (mBaseType & UserInfo.FLAG_PROFILE) != 0;
}
@@ -264,6 +290,8 @@ public final class UserTypeDetails {
pw.println(mBadgeLabels != null ? mBadgeLabels.length : "0(null)");
pw.print(prefix); pw.print("mBadgeColors.length: ");
pw.println(mBadgeColors != null ? mBadgeColors.length : "0(null)");
pw.print(prefix); pw.print("mDarkThemeBadgeColors.length: ");
pw.println(mDarkThemeBadgeColors != null ? mDarkThemeBadgeColors.length : "0(null)");
}
/** Builder for a {@link UserTypeDetails}; see that class for documentation. */
@@ -279,6 +307,7 @@ public final class UserTypeDetails {
private int mLabel = Resources.ID_NULL;
private @Nullable int[] mBadgeLabels = null;
private @Nullable int[] mBadgeColors = null;
private @Nullable int[] mDarkThemeBadgeColors = null;
private @DrawableRes int mIconBadge = Resources.ID_NULL;
private @DrawableRes int mBadgePlain = Resources.ID_NULL;
private @DrawableRes int mBadgeNoBackground = Resources.ID_NULL;
@@ -323,6 +352,14 @@ public final class UserTypeDetails {
return this;
}
/**
* The badge colors when the badge is on a dark background.
*/
public Builder setDarkThemeBadgeColors(@ColorRes int ... darkThemeBadgeColors) {
mDarkThemeBadgeColors = darkThemeBadgeColors;
return this;
}
public Builder setIconBadge(@DrawableRes int badgeIcon) {
mIconBadge = badgeIcon;
return this;
@@ -366,10 +403,10 @@ public final class UserTypeDetails {
Preconditions.checkArgument(mBadgeColors != null && mBadgeColors.length != 0,
"UserTypeDetails " + mName + " has badge but no badgeColors.");
}
return new UserTypeDetails(mName, mEnabled, mMaxAllowed, mBaseType,
mDefaultUserInfoPropertyFlags, mLabel, mMaxAllowedPerParent,
mIconBadge, mBadgePlain, mBadgeNoBackground, mBadgeLabels, mBadgeColors,
mDarkThemeBadgeColors == null ? mBadgeColors : mDarkThemeBadgeColors,
mDefaultRestrictions);
}

View File

@@ -116,6 +116,10 @@ public final class UserTypeFactory {
com.android.internal.R.color.profile_badge_1,
com.android.internal.R.color.profile_badge_2,
com.android.internal.R.color.profile_badge_3)
.setDarkThemeBadgeColors(
com.android.internal.R.color.profile_badge_1_dark,
com.android.internal.R.color.profile_badge_2_dark,
com.android.internal.R.color.profile_badge_3_dark)
.setDefaultRestrictions(null);
}
@@ -292,6 +296,8 @@ public final class UserTypeFactory {
setResAttributeArray(parser, builder::setBadgeLabels);
} else if (isProfile && "badge-colors".equals(childName)) {
setResAttributeArray(parser, builder::setBadgeColors);
} else if (isProfile && "badge-colors-dark".equals(childName)) {
setResAttributeArray(parser, builder::setDarkThemeBadgeColors);
} else {
Slog.w(LOG_TAG, "Unrecognized tag " + childName + " in "
+ parser.getPositionDescription());

View File

@@ -79,6 +79,7 @@
<uses-permission android:name="android.permission.READ_DREAM_STATE"/>
<uses-permission android:name="android.permission.WRITE_DREAM_STATE"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.MODIFY_DAY_NIGHT_MODE"/>
<!-- Uses API introduced in O (26) -->
<uses-sdk android:minSdkVersion="1"

View File

@@ -347,6 +347,10 @@ public final class UserManagerTest {
final int badgeIndex = userInfo.profileBadge;
assertThat(mUserManager.getUserBadgeColor(userId)).isEqualTo(
Resources.getSystem().getColor(userTypeDetails.getBadgeColor(badgeIndex), null));
assertThat(mUserManager.getUserBadgeDarkColor(userId)).isEqualTo(
Resources.getSystem().getColor(userTypeDetails.getDarkThemeBadgeColor(badgeIndex),
null));
assertThat(mUserManager.getBadgedLabelForUser("Test", asHandle(userId))).isEqualTo(
Resources.getSystem().getString(userTypeDetails.getBadgeLabel(badgeIndex), "Test"));