Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c53eb632 authored by Olivier Nshimiye's avatar Olivier Nshimiye Committed by Ankita Vyas
Browse files

Add user badge API to return a plain user badge

Bug: 307515481
Test: atest FrameworksServicesTests:UserManagerTest

Merged-In: I4a8247c8442651fc01d5a948dad2e1068abc75de
Change-Id: I4a8247c8442651fc01d5a948dad2e1068abc75de
parent d291eca7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10983,6 +10983,7 @@ package android.os {
    method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public android.os.PersistableBundle getSeedAccountOptions();
    method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public String getSeedAccountType();
    method @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS}) public long[] getSerialNumbersOfUsers(boolean);
    method @NonNull public android.graphics.drawable.Drawable getUserBadge();
    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.CREATE_USERS}) public java.util.List<android.os.UserHandle> getUserHandles(boolean);
    method @Nullable @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.GET_ACCOUNTS_PRIVILEGED}) public android.graphics.Bitmap getUserIcon();
    method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.MANAGE_USERS, android.Manifest.permission.QUERY_USERS, android.Manifest.permission.INTERACT_ACROSS_USERS}, conditional=true) public android.content.pm.UserProperties getUserProperties(@NonNull android.os.UserHandle);
+33 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.os;

import static android.app.admin.DevicePolicyResources.Drawables.Style.SOLID_COLORED;
import static android.app.admin.DevicePolicyResources.Strings.Core.WORK_PROFILE_BADGED_LABEL;
import static android.app.admin.DevicePolicyResources.UNDEFINED;

@@ -5392,6 +5393,38 @@ public class UserManager {
                badgeLocation, badgeDensity);
    }

    /**
     * Retrieves a user badge associated with the current context user. This is only
     * applicable to profile users since non-profile users do not have badges.
     *
     * @return A {@link Drawable} user badge corresponding to the context user
     * @throws android.content.res.Resources.NotFoundException if the user is not a profile or
     * does not have a badge defined.
     * @hide
     */
    @SystemApi
    @UserHandleAware(
            requiresAnyOfPermissionsIfNotCallerProfileGroup = {
                    Manifest.permission.MANAGE_USERS,
                    Manifest.permission.INTERACT_ACROSS_USERS})
    @SuppressLint("UnflaggedApi") // b/306636213
    public @NonNull Drawable getUserBadge() {
        if (!isProfile(mUserId)) {
            throw new Resources.NotFoundException("No badge found for this user.");
        }
        if (isManagedProfile(mUserId)) {
            DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
            return dpm.getResources().getDrawable(
                    android.app.admin.DevicePolicyResources.Drawables.WORK_PROFILE_ICON_BADGE,
                    SOLID_COLORED, () -> getDefaultUserBadge(mUserId));
        }
        return getDefaultUserBadge(mUserId);
    }

    private Drawable getDefaultUserBadge(@UserIdInt int userId) {
        return mContext.getResources().getDrawable(getUserBadgeResId(userId), mContext.getTheme());
    }

    /**
     * If the target user is a profile of the calling user or the caller
     * is itself a profile, then this returns a copy of the label with
+14 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertWithMessage;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;

import android.annotation.UserIdInt;
@@ -30,6 +31,7 @@ import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.content.pm.UserProperties;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
@@ -207,6 +209,9 @@ public final class UserManagerTest {
                .isEqualTo(cloneUserProperties.isCredentialShareableWithParent());
        assertThrows(SecurityException.class, cloneUserProperties::getDeleteAppWithParent);

        compareDrawables(mUserManager.getUserBadge(),
                Resources.getSystem().getDrawable(userTypeDetails.getBadgePlain()));

        // Verify clone user parent
        assertThat(mUserManager.getProfileParent(mainUserId)).isNull();
        UserInfo parentProfileInfo = mUserManager.getProfileParent(userInfo.id);
@@ -843,6 +848,9 @@ public final class UserManagerTest {
        assertThat(mUserManager.getUserBadgeNoBackgroundResId(userId))
                .isEqualTo(userTypeDetails.getBadgeNoBackground());

        compareDrawables(mUserManager.getUserBadge(),
                Resources.getSystem().getDrawable(userTypeDetails.getBadgePlain()));

        final int badgeIndex = userInfo.profileBadge;
        assertThat(mUserManager.getUserBadgeColor(userId)).isEqualTo(
                Resources.getSystem().getColor(userTypeDetails.getBadgeColor(badgeIndex), null));
@@ -1593,4 +1601,10 @@ public final class UserManagerTest {
                .getBoolean(com.android.internal.R.bool.config_isMainUserPermanentAdmin);
    }

    private void compareDrawables(Drawable actual, Drawable expected) {
        assertEquals(actual.getIntrinsicWidth(), expected.getIntrinsicWidth());
        assertEquals(actual.getIntrinsicHeight(), expected.getIntrinsicHeight());
        assertEquals(actual.getLevel(), expected.getLevel());
    }

}