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

Commit c71c42fd authored by Svetoslav's avatar Svetoslav Committed by Svetoslav Ganov
Browse files

Polish of the app widgets cross-profiles feature.

1. Added API for badging an arbitrary drawable at a given location.

2. Updated the icon and previewImage deprecation as they are no longer
   returning a badged drawable. The methods to load the icon and the
   preview are now just making it easier for a developer to get the
   drawables.

3. Fixed a bug in AppWidgetServiceImpl leading to a crash when a user
   is removed.

4. Fixed a bug in AppWidgetHost which was unnecessarily caching its
   package name and having code paths where the cached value was not
   populated when calling into the system.

bug:14991269

Change-Id: I50d011a6597d88814715d5ec04ee67815e8ce0bd
parent bc2fef0b
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -5834,7 +5834,7 @@ package android.appwidget {
    field public static final int WIDGET_CATEGORY_RECENTS = 4; // 0x4
    field public int autoAdvanceViewId;
    field public android.content.ComponentName configure;
    field public deprecated int icon;
    field public int icon;
    field public int initialKeyguardLayout;
    field public int initialLayout;
    field public deprecated java.lang.String label;
@@ -5842,7 +5842,7 @@ package android.appwidget {
    field public int minResizeHeight;
    field public int minResizeWidth;
    field public int minWidth;
    field public deprecated int previewImage;
    field public int previewImage;
    field public android.content.ComponentName provider;
    field public int resizeMode;
    field public int updatePeriodMillis;
@@ -22775,7 +22775,8 @@ 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);
    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();
+1 −1
Original line number Diff line number Diff line
@@ -2562,7 +2562,7 @@ public class Notification implements Parcelable
            // Note: This assumes that the current user can read the profile badge of the
            // originating user.
            UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
            Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId));
            Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0);
            if (badge == null) {
                return null;
            }
+4 −12
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.RemoteViews;
@@ -57,7 +56,6 @@ public class AppWidgetHost {
    private DisplayMetrics mDisplayMetrics;

    Context mContext;
    String mPackageName;
    Handler mHandler;
    int mHostId;
    Callbacks mCallbacks = new Callbacks();
@@ -154,10 +152,7 @@ public class AppWidgetHost {
        int[] updatedIds;
        ArrayList<RemoteViews> updatedViews = new ArrayList<RemoteViews>();
        try {
            if (mPackageName == null) {
                mPackageName = mContext.getPackageName();
            }
            updatedIds = sService.startListening(mCallbacks, mPackageName, mHostId,
            updatedIds = sService.startListening(mCallbacks, mContext.getPackageName(), mHostId,
                    updatedViews);
        }
        catch (RemoteException e) {
@@ -176,7 +171,7 @@ public class AppWidgetHost {
     */
    public void stopListening() {
        try {
            sService.stopListening(mPackageName, mHostId);
            sService.stopListening(mContext.getPackageName(), mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
@@ -194,10 +189,7 @@ public class AppWidgetHost {
     */
    public int allocateAppWidgetId() {
        try {
            if (mPackageName == null) {
                mPackageName = mContext.getPackageName();
            }
            return sService.allocateAppWidgetId(mPackageName, mHostId);
            return sService.allocateAppWidgetId(mContext.getPackageName(), mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
@@ -326,7 +318,7 @@ public class AppWidgetHost {
        }
        RemoteViews views;
        try {
            views = sService.getAppWidgetViews(mPackageName, appWidgetId);
            views = sService.getAppWidgetViews(mContext.getPackageName(), appWidgetId);
        } catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
+4 −35
Original line number Diff line number Diff line
@@ -20,13 +20,11 @@ import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.content.ComponentName;
import android.os.UserHandle;
import android.os.UserManager;

/**
 * Describes the meta data for an installed AppWidget provider.  The fields in this class
@@ -166,10 +164,7 @@ public class AppWidgetProviderInfo implements Parcelable {
     *
     * <p>This field corresponds to the <code>android:icon</code> attribute in
     * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
     *
     * @deprecated Use {@link #loadIcon(android.content.Context, int)}.
     */
    @Deprecated
    public int icon;

    /**
@@ -186,10 +181,7 @@ public class AppWidgetProviderInfo implements Parcelable {
     *
     * <p>This field corresponds to the <code>android:previewImage</code> attribute in
     * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
     *
     * @deprecated User {@link #loadPreviewImage(android.content.Context, int)}.
     */
    @Deprecated
    public int previewImage;

    /**
@@ -271,16 +263,11 @@ public class AppWidgetProviderInfo implements Parcelable {
     * The loaded icon corresponds to the <code>android:icon</code> attribute in
     * the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
     * </p>
     * <p>
     * <strong>Note:</strong> If you care about widgets from different profiles, you
     * should use this method to load the icon as the system will apply the correct
     * badging when applicable, so the user knows which profile a widget comes from.
     * </p>
     *
     * @param context Context for accessing resources.
     * @param density The optional desired density as per
     *         {@link android.util.DisplayMetrics#densityDpi}.
     * @return The potentially badged provider icon.
     * @return The provider icon.
     */
    public final Drawable loadIcon(Context context, int density) {
        return loadDrawable(context, density, providerInfo.getIconResource());
@@ -296,19 +283,12 @@ public class AppWidgetProviderInfo implements Parcelable {
     * The loaded image corresponds to the <code>android:previewImage</code> attribute
     * in the <code>&lt;receiver&gt;</code> element in the AndroidManifest.xml file.
     * </p>
     * <p>
     * <strong>Note:</strong> If you care about widgets from different profiles, you
     * should use this method to load the preview image as the system will apply the
     * correct badging when applicable, so the user knows which profile a previewed
     * widget comes from.
     * </p>
     *
     * @param context Context for accessing resources.
     * @param density The optional desired density as per
     *         {@link android.util.DisplayMetrics#densityDpi}.
     * @return The potentially badged widget preview image.
     * @return The widget preview image.
     */
    @SuppressWarnings("deprecation")
    public final Drawable loadPreviewImage(Context context, int density) {
        return loadDrawable(context, density, previewImage);
    }
@@ -384,27 +364,16 @@ public class AppWidgetProviderInfo implements Parcelable {
        try {
            Resources resources = context.getPackageManager().getResourcesForApplication(
                    providerInfo.applicationInfo);

            final Drawable drawable;
            if (resourceId > 0) {
                if (density <= 0) {
                    density = context.getResources().getDisplayMetrics().densityDpi;
                }
                drawable = resources.getDrawableForDensity(resourceId, density);
            } else {
                drawable = providerInfo.loadIcon(context.getPackageManager());
            }

            if (drawable instanceof BitmapDrawable) {
                UserManager userManager = (UserManager) context.getSystemService(
                        Context.USER_SERVICE);
                return userManager.getBadgedDrawableForUser(drawable, getProfile());
                return resources.getDrawableForDensity(resourceId, density);
            }
        } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
            /* ignore */
        }

        return null;
        return providerInfo.loadIcon(context.getPackageManager());
    }

    /**
+1 −2
Original line number Diff line number Diff line
@@ -179,8 +179,7 @@ public class LauncherActivityInfo {
        }

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