diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java index f883b60b534f3..6bd8fd7c6ecfb 100644 --- a/core/java/android/app/ApplicationPackageManager.java +++ b/core/java/android/app/ApplicationPackageManager.java @@ -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; } diff --git a/core/java/android/os/IUserManager.aidl b/core/java/android/os/IUserManager.aidl index b10abe7557d59..07363edd3e754 100644 --- a/core/java/android/os/IUserManager.aidl +++ b/core/java/android/os/IUserManager.aidl @@ -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); diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index 187274a837a0a..0023b92ac04b7 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -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). * *
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). + * + *
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.
*
diff --git a/core/res/res/values/colors.xml b/core/res/res/values/colors.xml
index 831da6f7ce4e5..77ac6db65bee2 100644
--- a/core/res/res/values/colors.xml
+++ b/core/res/res/values/colors.xml
@@ -183,6 +183,10 @@
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}. + * + *
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);
}
diff --git a/services/core/java/com/android/server/pm/UserTypeFactory.java b/services/core/java/com/android/server/pm/UserTypeFactory.java
index 7d45516bdefcb..2e8a267c77794 100644
--- a/services/core/java/com/android/server/pm/UserTypeFactory.java
+++ b/services/core/java/com/android/server/pm/UserTypeFactory.java
@@ -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());
diff --git a/services/tests/servicestests/AndroidManifest.xml b/services/tests/servicestests/AndroidManifest.xml
index 270a3b50b9343..3b38d948b121d 100644
--- a/services/tests/servicestests/AndroidManifest.xml
+++ b/services/tests/servicestests/AndroidManifest.xml
@@ -79,6 +79,7 @@