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

Commit e2da374d authored by Svetoslav's avatar Svetoslav Committed by Android (Google) Code Review
Browse files

Merge "Addressing API council comments for UserManager." into lmp-dev

parents 133f1430 c7d62f02
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -8843,6 +8843,9 @@ package android.content.pm {
    method public abstract android.content.pm.FeatureInfo[] getSystemAvailableFeatures();
    method public abstract java.lang.String[] getSystemSharedLibraryNames();
    method public abstract java.lang.CharSequence getText(java.lang.String, int, android.content.pm.ApplicationInfo);
    method public abstract android.graphics.drawable.Drawable getUserBadgedDrawableForDensity(android.graphics.drawable.Drawable, android.os.UserHandle, android.graphics.Rect, int);
    method public abstract android.graphics.drawable.Drawable getUserBadgedIcon(android.graphics.drawable.Drawable, android.os.UserHandle);
    method public abstract java.lang.CharSequence getUserBadgedLabel(java.lang.CharSequence, android.os.UserHandle);
    method public abstract android.content.res.XmlResourceParser getXml(java.lang.String, int, android.content.pm.ApplicationInfo);
    method public abstract boolean hasSystemFeature(java.lang.String);
    method public abstract boolean isSafeMode();
@@ -22465,9 +22468,6 @@ package android.os {
  public class UserManager {
    method public android.os.Bundle getApplicationRestrictions(java.lang.String);
    method public android.graphics.drawable.Drawable getBadgedDrawableForUser(android.graphics.drawable.Drawable, android.os.UserHandle, android.graphics.Rect, int);
    method public android.graphics.drawable.Drawable getBadgedIconForUser(android.graphics.drawable.Drawable, android.os.UserHandle);
    method public java.lang.CharSequence getBadgedLabelForUser(java.lang.CharSequence, android.os.UserHandle);
    method public long getSerialNumberForUser(android.os.UserHandle);
    method public int getUserCount();
    method public android.os.UserHandle getUserForSerialNumber(long);
@@ -29741,6 +29741,9 @@ package android.test.mock {
    method public android.content.pm.FeatureInfo[] getSystemAvailableFeatures();
    method public java.lang.String[] getSystemSharedLibraryNames();
    method public java.lang.CharSequence getText(java.lang.String, int, android.content.pm.ApplicationInfo);
    method public android.graphics.drawable.Drawable getUserBadgedDrawableForDensity(android.graphics.drawable.Drawable, android.os.UserHandle, android.graphics.Rect, int);
    method public android.graphics.drawable.Drawable getUserBadgedIcon(android.graphics.drawable.Drawable, android.os.UserHandle);
    method public java.lang.CharSequence getUserBadgedLabel(java.lang.CharSequence, android.os.UserHandle);
    method public android.content.res.XmlResourceParser getXml(java.lang.String, int, android.content.pm.ApplicationInfo);
    method public boolean hasSystemFeature(java.lang.String);
    method public boolean isSafeMode();
+6 −0
Original line number Diff line number Diff line
@@ -30,6 +30,12 @@ package android.os {
    method public void wakeUp(long);
  }

  public class UserManager {
    method public android.graphics.drawable.Drawable getBadgedDrawableForUser(android.graphics.drawable.Drawable, android.os.UserHandle, android.graphics.Rect, int);
    method public android.graphics.drawable.Drawable getBadgedIconForUser(android.graphics.drawable.Drawable, android.os.UserHandle);
    method public java.lang.CharSequence getBadgedLabelForUser(java.lang.CharSequence, android.os.UserHandle);
  }

}

package android.service.notification {
+120 −3
Original line number Diff line number Diff line
@@ -45,14 +45,17 @@ import android.content.pm.PermissionInfo;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.UserInfo;
import android.content.pm.VerificationParams;
import android.content.pm.VerifierDeviceIdentity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -864,6 +867,49 @@ final class ApplicationPackageManager extends PackageManager {
        return getApplicationLogo(getApplicationInfo(packageName, 0));
    }

    @Override
    public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
        final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
        if (badgeResId == 0) {
            return icon;
        }
        Drawable badgeIcon = getDrawable("system", badgeResId, null);
        return getBadgedDrawable(icon, badgeIcon, null, true);
    }

    @Override
    public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
            Rect badgeLocation, int badgeDensity) {
        Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
        if (badgeDrawable == null) {
            return drawable;
        }
        return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
    }

    @Override
    public Drawable getUserBadgeForDensity(UserHandle user, int density) {
        UserInfo userInfo = getUserIfProfile(user.getIdentifier());
        if (userInfo != null && userInfo.isManagedProfile()) {
            if (density <= 0) {
                density = mContext.getResources().getDisplayMetrics().densityDpi;
            }
            return Resources.getSystem().getDrawableForDensity(
                    com.android.internal.R.drawable.ic_corp_badge, density);
        }
        return null;
    }

    @Override
    public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
        UserInfo userInfo = getUserIfProfile(user.getIdentifier());
        if (userInfo != null && userInfo.isManagedProfile()) {
            return Resources.getSystem().getString(
                    com.android.internal.R.string.managed_profile_label_badge, label);
        }
        return label;
    }

    @Override public Resources getResourcesForActivity(
        ComponentName activityName) throws NameNotFoundException {
        return getResourcesForApplication(
@@ -1647,8 +1693,79 @@ final class ApplicationPackageManager extends PackageManager {
        if (dr == null) {
            dr = itemInfo.loadDefaultIcon(this);
        }
        return getUserManager().getBadgedDrawableForUser(dr,
                new UserHandle(mContext.getUserId()));
        return getUserBadgedDrawableForDensity(dr, new UserHandle(mContext.getUserId()), null, 0);
    }

    private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
            Rect badgeLocation, boolean tryBadgeInPlace) {
        final int badgedWidth = drawable.getIntrinsicWidth();
        final int badgedHeight = drawable.getIntrinsicHeight();
        final boolean canBadgeInPlace = tryBadgeInPlace
                && (drawable instanceof BitmapDrawable)
                && ((BitmapDrawable) drawable).getBitmap().isMutable();

        final Bitmap bitmap;
        if (canBadgeInPlace) {
            bitmap = ((BitmapDrawable) drawable).getBitmap();
        } else {
            bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(bitmap);

        if (!canBadgeInPlace) {
            drawable.setBounds(0, 0, badgedWidth, badgedHeight);
            drawable.draw(canvas);
        }

        if (badgeLocation != null) {
            if (badgeLocation.left < 0 || badgeLocation.top < 0
                    || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
                throw new IllegalArgumentException("Badge location " + badgeLocation
                        + " not in badged drawable bounds "
                        + new Rect(0, 0, badgedWidth, badgedHeight));
            }
            badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

            canvas.save();
            canvas.translate(badgeLocation.left, badgeLocation.top);
            badgeDrawable.draw(canvas);
            canvas.restore();
        } else {
            badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
            badgeDrawable.draw(canvas);
        }

        if (!canBadgeInPlace) {
            BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

            if (drawable instanceof BitmapDrawable) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
            }

            return mergedDrawable;
        }

        return drawable;
    }

    private int getBadgeResIdForUser(int userHandle) {
        // Return the framework-provided badge.
        UserInfo userInfo = getUserIfProfile(userHandle);
        if (userInfo != null && userInfo.isManagedProfile()) {
            return com.android.internal.R.drawable.ic_corp_icon_badge;
        }
        return 0;
    }

    private UserInfo getUserIfProfile(int userHandle) {
        List<UserInfo> userProfiles = mUserManager.getProfiles(UserHandle.myUserId());
        for (UserInfo user : userProfiles) {
            if (user.id == userHandle) {
                return user;
            }
        }
        return null;
    }

    private final ContextImpl mContext;
+2 −3
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.text.TextUtils;
import android.util.Log;
import android.util.MathUtils;
@@ -2581,8 +2580,8 @@ public class Notification implements Parcelable
        private Drawable getProfileBadgeDrawable() {
            // Note: This assumes that the current user can read the profile badge of the
            // originating user.
            UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
            return userManager.getBadgeForUser(new UserHandle(mContext.getUserId()), 0);
            return mContext.getPackageManager().getUserBadgeForDensity(
                    new UserHandle(mContext.getUserId()), 0);
        }

        private Bitmap getProfileBadge() {
+1 −10
Original line number Diff line number Diff line
@@ -18,16 +18,10 @@ package android.content.pm;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.DisplayMetrics;
@@ -39,11 +33,9 @@ import android.util.Log;
 * and badged icon for the activity.
 */
public class LauncherActivityInfo {
    private static final boolean DEBUG = false;
    private static final String TAG = "LauncherActivityInfo";

    private final PackageManager mPm;
    private final UserManager mUm;

    private ActivityInfo mActivityInfo;
    private ComponentName mComponentName;
@@ -68,7 +60,6 @@ public class LauncherActivityInfo {

    LauncherActivityInfo(Context context) {
        mPm = context.getPackageManager();
        mUm = UserManager.get(context);
    }

    /**
@@ -179,7 +170,7 @@ public class LauncherActivityInfo {
        }

        if (originalIcon instanceof BitmapDrawable) {
            return mUm.getBadgedIconForUser(originalIcon, mUser);
            return mPm.getUserBadgedIcon(originalIcon, mUser);
        } else {
            Log.e(TAG, "Unable to create badged icon for " + mActivityInfo);
        }
Loading