OSDN Git Service

Fix media notification action icons.
authorDan Sandler <dsandler@android.com>
Wed, 22 Jul 2015 14:45:30 +0000 (10:45 -0400)
committerDan Sandler <dsandler@android.com>
Wed, 29 Jul 2015 17:50:32 +0000 (13:50 -0400)
In addition to cleaning up some bare references to the icon
slot, we now apply updates to notification RemoteViews in
the context of the supplying app's package. This ensures we
can find the drawables inside any Icon objects that were
constructed without a proper package name, such as is the
case with Actions (because the builder and constructor are
Context-free and so don't know the package name).

This CL also makes clear what was previously only implied:
Non-resource action icons are not actually supported yet
since they can't be pushed to TextView compound drawables
using today's RemoteViews APIs. That will require an API
change.

Bug: 22600607
Change-Id: Ie6b88aed36e4db05be35f843ea3bc1898d4a5c96

core/java/android/app/Notification.java
core/java/android/service/notification/StatusBarNotification.java
packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java

index b0d8541..0937af6 100644 (file)
@@ -930,9 +930,9 @@ public class Notification implements Parcelable
         private Action(Parcel in) {
             if (in.readInt() != 0) {
                 mIcon = Icon.CREATOR.createFromParcel(in);
-            }
-            if (mIcon.getType() == Icon.TYPE_RESOURCE) {
-                icon = mIcon.getResId();
+                if (mIcon.getType() == Icon.TYPE_RESOURCE) {
+                    icon = mIcon.getResId();
+                }
             }
             title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
             if (in.readInt() == 1) {
@@ -3171,7 +3171,12 @@ public class Notification implements Parcelable
             RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
                     tombstone ? getActionTombstoneLayoutResource()
                               : getActionLayoutResource());
-            button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
+            // TODO: support Bitmaps in action icons when TextView compound drawables support Icon
+            final Icon ai = action.getIcon();
+            final int icon = (ai.getType() == Icon.TYPE_RESOURCE)
+                    ? ai.getResId()
+                    : 0;
+            button.setTextViewCompoundDrawablesRelative(R.id.action0, icon, 0, 0, 0);
             button.setTextViewText(R.id.action0, processLegacyText(action.title));
             if (!tombstone) {
                 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
@@ -3190,7 +3195,7 @@ public class Notification implements Parcelable
         }
 
         private void processLegacyAction(Action action, RemoteViews button) {
-            if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) {
+            if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.getIcon())) {
                 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
                         mContext.getColor(R.color.notification_action_color_filter),
                         PorterDuff.Mode.MULTIPLY);
@@ -3605,7 +3610,6 @@ public class Notification implements Parcelable
             mContentText = extras.getCharSequence(EXTRA_TEXT);
             mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
             mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
-            mSmallIcon = extras.getParcelable(EXTRA_SMALL_ICON);
             mProgress = extras.getInt(EXTRA_PROGRESS);
             mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
             mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
@@ -4439,7 +4443,7 @@ public class Notification implements Parcelable
             final boolean tombstone = (action.actionIntent == null);
             RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
                     R.layout.notification_material_media_action);
-            button.setImageViewResource(R.id.action0, action.icon);
+            button.setImageViewIcon(R.id.action0, action.getIcon());
             button.setDrawableParameters(R.id.action0, false, -1,
                     0xFFFFFFFF,
                     PorterDuff.Mode.SRC_ATOP, -1);
index 0eda692..2cab914 100644 (file)
@@ -17,6 +17,9 @@
 package android.service.notification;
 
 import android.app.Notification;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.os.UserHandle;
@@ -40,6 +43,7 @@ public class StatusBarNotification implements Parcelable {
     private final long postTime;
 
     private final int score;
+    private Context mContext; // used for inflation & icon expansion
 
     /** @hide */
     public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid,
@@ -261,4 +265,24 @@ public class StatusBarNotification implements Parcelable {
     public String getGroupKey() {
         return groupKey;
     }
+
+    /**
+     * @hide
+     */
+    public Context getPackageContext(Context context) {
+        if (mContext == null) {
+            try {
+                ApplicationInfo ai = context.getPackageManager()
+                        .getApplicationInfo(pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
+                mContext = context.createApplicationContext(ai,
+                        Context.CONTEXT_RESTRICTED);
+            } catch (PackageManager.NameNotFoundException e) {
+                mContext = null;
+            }
+        }
+        if (mContext == null) {
+            mContext = context;
+        }
+        return mContext;
+    }
 }
index a2e6632..d5da179 100644 (file)
@@ -1328,14 +1328,20 @@ public abstract class BaseStatusBar extends SystemUI implements
         View bigContentViewLocal = null;
         View headsUpContentViewLocal = null;
         try {
-            contentViewLocal = contentView.apply(mContext, contentContainer,
+            contentViewLocal = contentView.apply(
+                    sbn.getPackageContext(mContext),
+                    contentContainer,
                     mOnClickHandler);
             if (bigContentView != null) {
-                bigContentViewLocal = bigContentView.apply(mContext, contentContainer,
+                bigContentViewLocal = bigContentView.apply(
+                        sbn.getPackageContext(mContext),
+                        contentContainer,
                         mOnClickHandler);
             }
             if (headsUpContentView != null) {
-                headsUpContentViewLocal = headsUpContentView.apply(mContext, contentContainer,
+                headsUpContentViewLocal = headsUpContentView.apply(
+                        sbn.getPackageContext(mContext),
+                        contentContainer,
                         mOnClickHandler);
             }
         }
@@ -1362,7 +1368,8 @@ public abstract class BaseStatusBar extends SystemUI implements
         View publicViewLocal = null;
         if (publicNotification != null) {
             try {
-                publicViewLocal = publicNotification.contentView.apply(mContext,
+                publicViewLocal = publicNotification.contentView.apply(
+                        sbn.getPackageContext(mContext),
                         contentContainerPublic, mOnClickHandler);
 
                 if (publicViewLocal != null) {
@@ -1981,15 +1988,18 @@ public abstract class BaseStatusBar extends SystemUI implements
         // Reapply the RemoteViews
         contentView.reapply(mContext, entry.getContentView(), mOnClickHandler);
         if (bigContentView != null && entry.getExpandedContentView() != null) {
-            bigContentView.reapply(mContext, entry.getExpandedContentView(),
+            bigContentView.reapply(notification.getPackageContext(mContext),
+                    entry.getExpandedContentView(),
                     mOnClickHandler);
         }
         View headsUpChild = entry.getHeadsUpContentView();
         if (headsUpContentView != null && headsUpChild != null) {
-            headsUpContentView.reapply(mContext, headsUpChild, mOnClickHandler);
+            headsUpContentView.reapply(notification.getPackageContext(mContext),
+                    headsUpChild, mOnClickHandler);
         }
         if (publicContentView != null && entry.getPublicContentView() != null) {
-            publicContentView.reapply(mContext, entry.getPublicContentView(), mOnClickHandler);
+            publicContentView.reapply(notification.getPackageContext(mContext),
+                    entry.getPublicContentView(), mOnClickHandler);
         }
         // update the contentIntent
         mNotificationClicker.register(entry.row, notification);