Skip to content

Commit bbe1682

Browse files
dsandlerAndroid (Google) Code Review
authored andcommitted
Merge changes from topic 'fix-media-notifications' into mnc-dev
* changes: Fully support Icons in Notification actions. Fix media notification action icons.
2 parents a991c66 + 912282e commit bbe1682

5 files changed

Lines changed: 168 additions & 25 deletions

File tree

core/java/android/app/Notification.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,9 @@ public static class Action implements Parcelable {
930930
private Action(Parcel in) {
931931
if (in.readInt() != 0) {
932932
mIcon = Icon.CREATOR.createFromParcel(in);
933-
}
934-
if (mIcon.getType() == Icon.TYPE_RESOURCE) {
935-
icon = mIcon.getResId();
933+
if (mIcon.getType() == Icon.TYPE_RESOURCE) {
934+
icon = mIcon.getResId();
935+
}
936936
}
937937
title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
938938
if (in.readInt() == 1) {
@@ -3174,7 +3174,8 @@ private RemoteViews generateActionButton(Action action) {
31743174
RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
31753175
tombstone ? getActionTombstoneLayoutResource()
31763176
: getActionLayoutResource());
3177-
button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
3177+
final Icon ai = action.getIcon();
3178+
button.setTextViewCompoundDrawablesRelative(R.id.action0, ai, null, null, null);
31783179
button.setTextViewText(R.id.action0, processLegacyText(action.title));
31793180
if (!tombstone) {
31803181
button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
@@ -3193,7 +3194,7 @@ private boolean isLegacy() {
31933194
}
31943195

31953196
private void processLegacyAction(Action action, RemoteViews button) {
3196-
if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.icon)) {
3197+
if (!isLegacy() || mColorUtil.isGrayscaleIcon(mContext, action.getIcon())) {
31973198
button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
31983199
mContext.getColor(R.color.notification_action_color_filter),
31993200
PorterDuff.Mode.MULTIPLY);
@@ -3608,7 +3609,6 @@ private void restoreFromNotification(Notification n) {
36083609
mContentText = extras.getCharSequence(EXTRA_TEXT);
36093610
mSubText = extras.getCharSequence(EXTRA_SUB_TEXT);
36103611
mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT);
3611-
mSmallIcon = extras.getParcelable(EXTRA_SMALL_ICON);
36123612
mProgress = extras.getInt(EXTRA_PROGRESS);
36133613
mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX);
36143614
mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
@@ -4442,7 +4442,7 @@ private RemoteViews generateMediaActionButton(Action action) {
44424442
final boolean tombstone = (action.actionIntent == null);
44434443
RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
44444444
R.layout.notification_material_media_action);
4445-
button.setImageViewResource(R.id.action0, action.icon);
4445+
button.setImageViewIcon(R.id.action0, action.getIcon());
44464446
button.setDrawableParameters(R.id.action0, false, -1,
44474447
0xFFFFFFFF,
44484448
PorterDuff.Mode.SRC_ATOP, -1);

core/java/android/service/notification/StatusBarNotification.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package android.service.notification;
1818

1919
import android.app.Notification;
20+
import android.content.Context;
21+
import android.content.pm.ApplicationInfo;
22+
import android.content.pm.PackageManager;
2023
import android.os.Parcel;
2124
import android.os.Parcelable;
2225
import android.os.UserHandle;
@@ -40,6 +43,7 @@ public class StatusBarNotification implements Parcelable {
4043
private final long postTime;
4144

4245
private final int score;
46+
private Context mContext; // used for inflation & icon expansion
4347

4448
/** @hide */
4549
public StatusBarNotification(String pkg, String opPkg, int id, String tag, int uid,
@@ -261,4 +265,24 @@ public String getKey() {
261265
public String getGroupKey() {
262266
return groupKey;
263267
}
268+
269+
/**
270+
* @hide
271+
*/
272+
public Context getPackageContext(Context context) {
273+
if (mContext == null) {
274+
try {
275+
ApplicationInfo ai = context.getPackageManager()
276+
.getApplicationInfo(pkg, PackageManager.GET_UNINSTALLED_PACKAGES);
277+
mContext = context.createApplicationContext(ai,
278+
Context.CONTEXT_RESTRICTED);
279+
} catch (PackageManager.NameNotFoundException e) {
280+
mContext = null;
281+
}
282+
}
283+
if (mContext == null) {
284+
mContext = context;
285+
}
286+
return mContext;
287+
}
264288
}

core/java/android/widget/RemoteViews.java

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,39 +1412,108 @@ private class TextViewDrawableAction extends Action {
14121412
public TextViewDrawableAction(int viewId, boolean isRelative, int d1, int d2, int d3, int d4) {
14131413
this.viewId = viewId;
14141414
this.isRelative = isRelative;
1415+
this.useIcons = false;
14151416
this.d1 = d1;
14161417
this.d2 = d2;
14171418
this.d3 = d3;
14181419
this.d4 = d4;
14191420
}
14201421

1422+
public TextViewDrawableAction(int viewId, boolean isRelative,
1423+
Icon i1, Icon i2, Icon i3, Icon i4) {
1424+
this.viewId = viewId;
1425+
this.isRelative = isRelative;
1426+
this.useIcons = true;
1427+
this.i1 = i1;
1428+
this.i2 = i2;
1429+
this.i3 = i3;
1430+
this.i4 = i4;
1431+
}
1432+
14211433
public TextViewDrawableAction(Parcel parcel) {
14221434
viewId = parcel.readInt();
14231435
isRelative = (parcel.readInt() != 0);
1424-
d1 = parcel.readInt();
1425-
d2 = parcel.readInt();
1426-
d3 = parcel.readInt();
1427-
d4 = parcel.readInt();
1436+
useIcons = (parcel.readInt() != 0);
1437+
if (useIcons) {
1438+
if (parcel.readInt() != 0) {
1439+
i1 = Icon.CREATOR.createFromParcel(parcel);
1440+
}
1441+
if (parcel.readInt() != 0) {
1442+
i2 = Icon.CREATOR.createFromParcel(parcel);
1443+
}
1444+
if (parcel.readInt() != 0) {
1445+
i3 = Icon.CREATOR.createFromParcel(parcel);
1446+
}
1447+
if (parcel.readInt() != 0) {
1448+
i4 = Icon.CREATOR.createFromParcel(parcel);
1449+
}
1450+
} else {
1451+
d1 = parcel.readInt();
1452+
d2 = parcel.readInt();
1453+
d3 = parcel.readInt();
1454+
d4 = parcel.readInt();
1455+
}
14281456
}
14291457

14301458
public void writeToParcel(Parcel dest, int flags) {
14311459
dest.writeInt(TAG);
14321460
dest.writeInt(viewId);
14331461
dest.writeInt(isRelative ? 1 : 0);
1434-
dest.writeInt(d1);
1435-
dest.writeInt(d2);
1436-
dest.writeInt(d3);
1437-
dest.writeInt(d4);
1462+
dest.writeInt(useIcons ? 1 : 0);
1463+
if (useIcons) {
1464+
if (i1 != null) {
1465+
dest.writeInt(1);
1466+
i1.writeToParcel(dest, 0);
1467+
} else {
1468+
dest.writeInt(0);
1469+
}
1470+
if (i2 != null) {
1471+
dest.writeInt(1);
1472+
i2.writeToParcel(dest, 0);
1473+
} else {
1474+
dest.writeInt(0);
1475+
}
1476+
if (i3 != null) {
1477+
dest.writeInt(1);
1478+
i3.writeToParcel(dest, 0);
1479+
} else {
1480+
dest.writeInt(0);
1481+
}
1482+
if (i4 != null) {
1483+
dest.writeInt(1);
1484+
i4.writeToParcel(dest, 0);
1485+
} else {
1486+
dest.writeInt(0);
1487+
}
1488+
} else {
1489+
dest.writeInt(d1);
1490+
dest.writeInt(d2);
1491+
dest.writeInt(d3);
1492+
dest.writeInt(d4);
1493+
}
14381494
}
14391495

14401496
@Override
14411497
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
14421498
final TextView target = (TextView) root.findViewById(viewId);
14431499
if (target == null) return;
1444-
if (isRelative) {
1445-
target.setCompoundDrawablesRelativeWithIntrinsicBounds(d1, d2, d3, d4);
1500+
if (useIcons) {
1501+
final Context ctx = target.getContext();
1502+
final Drawable id1 = i1 == null ? null : i1.loadDrawable(ctx);
1503+
final Drawable id2 = i2 == null ? null : i2.loadDrawable(ctx);
1504+
final Drawable id3 = i3 == null ? null : i3.loadDrawable(ctx);
1505+
final Drawable id4 = i4 == null ? null : i4.loadDrawable(ctx);
1506+
if (isRelative) {
1507+
target.setCompoundDrawablesRelativeWithIntrinsicBounds(id1, id2, id3, id4);
1508+
} else {
1509+
target.setCompoundDrawablesWithIntrinsicBounds(id1, id2, id3, id4);
1510+
}
14461511
} else {
1447-
target.setCompoundDrawablesWithIntrinsicBounds(d1, d2, d3, d4);
1512+
if (isRelative) {
1513+
target.setCompoundDrawablesRelativeWithIntrinsicBounds(d1, d2, d3, d4);
1514+
} else {
1515+
target.setCompoundDrawablesWithIntrinsicBounds(d1, d2, d3, d4);
1516+
}
14481517
}
14491518
}
14501519

@@ -1453,7 +1522,9 @@ public String getActionName() {
14531522
}
14541523

14551524
boolean isRelative = false;
1525+
boolean useIcons = false;
14561526
int d1, d2, d3, d4;
1527+
Icon i1, i2, i3, i4;
14571528

14581529
public final static int TAG = 11;
14591530
}
@@ -2066,6 +2137,41 @@ public void setTextViewCompoundDrawablesRelativeColorFilter(int viewId,
20662137
addAction(new TextViewDrawableColorFilterAction(viewId, true, index, color, mode));
20672138
}
20682139

2140+
/**
2141+
* Equivalent to calling {@link
2142+
* TextView#setCompoundDrawablesWithIntrinsicBounds(Drawable, Drawable, Drawable, Drawable)}
2143+
* using the drawables yielded by {@link Icon#loadDrawable(Context)}.
2144+
*
2145+
* @param viewId The id of the view whose text should change
2146+
* @param left an Icon to place to the left of the text, or 0
2147+
* @param top an Icon to place above the text, or 0
2148+
* @param right an Icon to place to the right of the text, or 0
2149+
* @param bottom an Icon to place below the text, or 0
2150+
*
2151+
* @hide
2152+
*/
2153+
public void setTextViewCompoundDrawables(int viewId, Icon left, Icon top, Icon right, Icon bottom) {
2154+
addAction(new TextViewDrawableAction(viewId, false, left, top, right, bottom));
2155+
}
2156+
2157+
/**
2158+
* Equivalent to calling {@link
2159+
* TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable, Drawable, Drawable, Drawable)}
2160+
* using the drawables yielded by {@link Icon#loadDrawable(Context)}.
2161+
*
2162+
* @param viewId The id of the view whose text should change
2163+
* @param start an Icon to place before the text (relative to the
2164+
* layout direction), or 0
2165+
* @param top an Icon to place above the text, or 0
2166+
* @param end an Icon to place after the text, or 0
2167+
* @param bottom an Icon to place below the text, or 0
2168+
*
2169+
* @hide
2170+
*/
2171+
public void setTextViewCompoundDrawablesRelative(int viewId, Icon start, Icon top, Icon end, Icon bottom) {
2172+
addAction(new TextViewDrawableAction(viewId, true, start, top, end, bottom));
2173+
}
2174+
20692175
/**
20702176
* Equivalent to calling ImageView.setImageResource
20712177
*

core/java/android/widget/TextView.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,6 +2270,7 @@ public void setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left,
22702270
* @attr ref android.R.styleable#TextView_drawableRight
22712271
* @attr ref android.R.styleable#TextView_drawableBottom
22722272
*/
2273+
@android.view.RemotableViewMethod
22732274
public void setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left,
22742275
@Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
22752276

@@ -2302,6 +2303,7 @@ public void setCompoundDrawablesWithIntrinsicBounds(@Nullable Drawable left,
23022303
* @attr ref android.R.styleable#TextView_drawableEnd
23032304
* @attr ref android.R.styleable#TextView_drawableBottom
23042305
*/
2306+
@android.view.RemotableViewMethod
23052307
public void setCompoundDrawablesRelative(@Nullable Drawable start, @Nullable Drawable top,
23062308
@Nullable Drawable end, @Nullable Drawable bottom) {
23072309
Drawables dr = mDrawables;
@@ -2472,6 +2474,7 @@ public void setCompoundDrawablesRelativeWithIntrinsicBounds(@DrawableRes int sta
24722474
* @attr ref android.R.styleable#TextView_drawableEnd
24732475
* @attr ref android.R.styleable#TextView_drawableBottom
24742476
*/
2477+
@android.view.RemotableViewMethod
24752478
public void setCompoundDrawablesRelativeWithIntrinsicBounds(@Nullable Drawable start,
24762479
@Nullable Drawable top, @Nullable Drawable end, @Nullable Drawable bottom) {
24772480

packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,14 +1328,20 @@ protected boolean inflateViews(Entry entry, ViewGroup parent) {
13281328
View bigContentViewLocal = null;
13291329
View headsUpContentViewLocal = null;
13301330
try {
1331-
contentViewLocal = contentView.apply(mContext, contentContainer,
1331+
contentViewLocal = contentView.apply(
1332+
sbn.getPackageContext(mContext),
1333+
contentContainer,
13321334
mOnClickHandler);
13331335
if (bigContentView != null) {
1334-
bigContentViewLocal = bigContentView.apply(mContext, contentContainer,
1336+
bigContentViewLocal = bigContentView.apply(
1337+
sbn.getPackageContext(mContext),
1338+
contentContainer,
13351339
mOnClickHandler);
13361340
}
13371341
if (headsUpContentView != null) {
1338-
headsUpContentViewLocal = headsUpContentView.apply(mContext, contentContainer,
1342+
headsUpContentViewLocal = headsUpContentView.apply(
1343+
sbn.getPackageContext(mContext),
1344+
contentContainer,
13391345
mOnClickHandler);
13401346
}
13411347
}
@@ -1362,7 +1368,8 @@ protected boolean inflateViews(Entry entry, ViewGroup parent) {
13621368
View publicViewLocal = null;
13631369
if (publicNotification != null) {
13641370
try {
1365-
publicViewLocal = publicNotification.contentView.apply(mContext,
1371+
publicViewLocal = publicNotification.contentView.apply(
1372+
sbn.getPackageContext(mContext),
13661373
contentContainerPublic, mOnClickHandler);
13671374

13681375
if (publicViewLocal != null) {
@@ -1981,15 +1988,18 @@ private void updateNotificationViews(Entry entry, StatusBarNotification notifica
19811988
// Reapply the RemoteViews
19821989
contentView.reapply(mContext, entry.getContentView(), mOnClickHandler);
19831990
if (bigContentView != null && entry.getExpandedContentView() != null) {
1984-
bigContentView.reapply(mContext, entry.getExpandedContentView(),
1991+
bigContentView.reapply(notification.getPackageContext(mContext),
1992+
entry.getExpandedContentView(),
19851993
mOnClickHandler);
19861994
}
19871995
View headsUpChild = entry.getHeadsUpContentView();
19881996
if (headsUpContentView != null && headsUpChild != null) {
1989-
headsUpContentView.reapply(mContext, headsUpChild, mOnClickHandler);
1997+
headsUpContentView.reapply(notification.getPackageContext(mContext),
1998+
headsUpChild, mOnClickHandler);
19901999
}
19912000
if (publicContentView != null && entry.getPublicContentView() != null) {
1992-
publicContentView.reapply(mContext, entry.getPublicContentView(), mOnClickHandler);
2001+
publicContentView.reapply(notification.getPackageContext(mContext),
2002+
entry.getPublicContentView(), mOnClickHandler);
19932003
}
19942004
// update the contentIntent
19952005
mNotificationClicker.register(entry.row, notification);

0 commit comments

Comments
 (0)