| Index: chrome/android/java/src/org/chromium/chrome/browser/notifications/CustomNotificationBuilder.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/notifications/CustomNotificationBuilder.java b/chrome/android/java/src/org/chromium/chrome/browser/notifications/CustomNotificationBuilder.java
|
| index 274ebd0f36bc7cdb18d74e9b51ff3d8f75091061..b342cab1ae089d63c1fd34fd01a010f178f1b6dd 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/notifications/CustomNotificationBuilder.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/notifications/CustomNotificationBuilder.java
|
| @@ -4,6 +4,7 @@
|
|
|
| package org.chromium.chrome.browser.notifications;
|
|
|
| +import android.annotation.TargetApi;
|
| import android.app.Notification;
|
| import android.app.PendingIntent;
|
| import android.content.Context;
|
| @@ -12,9 +13,8 @@
|
| import android.graphics.BitmapFactory;
|
| import android.graphics.drawable.BitmapDrawable;
|
| import android.graphics.drawable.Drawable;
|
| +import android.graphics.drawable.Icon;
|
| import android.os.Build;
|
| -import android.support.v4.app.NotificationCompat;
|
| -import android.support.v4.app.NotificationCompat.Action;
|
| import android.text.format.DateFormat;
|
| import android.util.DisplayMetrics;
|
| import android.util.TypedValue;
|
| @@ -37,18 +37,43 @@
|
| * Builds a notification using the given inputs. Uses RemoteViews to provide a custom layout.
|
| */
|
| public class CustomNotificationBuilder implements NotificationBuilder {
|
| + // Notification.Action was only introduced in API level 19.
|
| + private static class Action {
|
| + public int iconId;
|
| + @Nullable
|
| + public Bitmap icon;
|
| + public CharSequence title;
|
| + public PendingIntent intent;
|
| +
|
| + Action(int iconId, CharSequence title, PendingIntent intent) {
|
| + this.iconId = iconId;
|
| + this.title = title;
|
| + this.intent = intent;
|
| + }
|
| +
|
| + Action(Bitmap icon, CharSequence title, PendingIntent intent) {
|
| + this.icon = icon;
|
| + this.title = title;
|
| + this.intent = intent;
|
| + }
|
| + }
|
| +
|
| /**
|
| * Maximum length of CharSequence inputs to prevent excessive memory consumption. At current
|
| * screen sizes we display about 500 characters at most, so this is a pretty generous limit, and
|
| - * it matches what NotificationCompat does.
|
| + * it matches what the Notification class does.
|
| */
|
| @VisibleForTesting static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
|
|
|
| /**
|
| - * The maximum number of action buttons. One is for the settings button, and two more slots are
|
| - * for developer provided buttons.
|
| + * The maximum number of developer provided action buttons.
|
| + */
|
| + private static final int MAX_ACTION_BUTTONS = 2;
|
| +
|
| + /**
|
| + * The maximum width of action icons in dp units.
|
| */
|
| - private static final int MAX_ACTION_BUTTONS = 3;
|
| + private static final int MAX_ACTION_ICON_WIDTH_DP = 32;
|
|
|
| /**
|
| * The maximum number of lines of body text for the expanded state. Fewer lines are used when
|
| @@ -151,7 +176,7 @@ public Notification build() {
|
| bigView.setViewVisibility(R.id.small_icon_footer, View.VISIBLE);
|
| }
|
|
|
| - NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
|
| + Notification.Builder builder = new Notification.Builder(mContext);
|
| builder.setTicker(mTickerText);
|
| builder.setSmallIcon(mSmallIconId);
|
| builder.setContentIntent(mContentIntent);
|
| @@ -167,10 +192,10 @@ public Notification build() {
|
| builder.setSubText(mOrigin);
|
| builder.setLargeIcon(mLargeIcon);
|
| for (Action action : mActions) {
|
| - builder.addAction(action);
|
| + addAction(builder, action);
|
| }
|
| if (mSettingsAction != null) {
|
| - builder.addAction(mSettingsAction);
|
| + addAction(builder, mSettingsAction);
|
| }
|
|
|
| Notification notification = builder.build();
|
| @@ -236,6 +261,15 @@ public NotificationBuilder addAction(int iconId, CharSequence title, PendingInte
|
| return this;
|
| }
|
|
|
| + public NotificationBuilder addAction(Bitmap icon, CharSequence title, PendingIntent intent) {
|
| + if (mActions.size() == MAX_ACTION_BUTTONS) {
|
| + throw new IllegalStateException(
|
| + "Cannot add more than " + MAX_ACTION_BUTTONS + " actions.");
|
| + }
|
| + mActions.add(new Action(icon, limitLength(title), intent));
|
| + return this;
|
| + }
|
| +
|
| @Override
|
| public NotificationBuilder addSettingsAction(
|
| int iconId, CharSequence title, PendingIntent intent) {
|
| @@ -274,30 +308,40 @@ private void addActionButtons(RemoteViews bigView) {
|
| RemoteViews view =
|
| new RemoteViews(mContext.getPackageName(), R.layout.web_notification_button);
|
|
|
| - if (action.getIcon() != 0) {
|
| + if ((action.icon != null && useActionIconBitmaps()) || action.iconId != 0) {
|
| // TODO(mvanouwerkerk): If the icon can be provided by web developers, limit its
|
| // dimensions and decide whether or not to paint it.
|
| if (useMaterial()) {
|
| view.setInt(R.id.button_icon, "setColorFilter", BUTTON_ICON_COLOR_MATERIAL);
|
| }
|
| - view.setImageViewResource(R.id.button_icon, action.getIcon());
|
| +
|
| + int iconWidth = 0;
|
| + if (action.icon != null && useActionIconBitmaps()) {
|
| + view.setImageViewBitmap(R.id.button_icon, action.icon);
|
| + iconWidth = action.icon.getWidth();
|
| + } else if (action.iconId != 0) {
|
| + view.setImageViewResource(R.id.button_icon, action.iconId);
|
| + BitmapFactory.Options options = new BitmapFactory.Options();
|
| + options.inJustDecodeBounds = true;
|
| + BitmapFactory.decodeResource(resources, action.iconId, options);
|
| + iconWidth = options.outWidth;
|
| + }
|
| + iconWidth = dpToPx(
|
| + Math.min(pxToDp(iconWidth, metrics), MAX_ACTION_ICON_WIDTH_DP), metrics);
|
|
|
| // Set the padding of the button so the text does not overlap with the icon. Flip
|
| // between left and right manually as RemoteViews does not expose a method that sets
|
| // padding in a writing-direction independent way.
|
| - BitmapFactory.Options options = new BitmapFactory.Options();
|
| - options.inJustDecodeBounds = true;
|
| - BitmapFactory.decodeResource(resources, action.getIcon(), options);
|
| int buttonPadding =
|
| dpToPx(BUTTON_PADDING_START_DP + BUTTON_ICON_PADDING_DP, metrics)
|
| - + options.outWidth;
|
| + + iconWidth;
|
| int buttonPaddingLeft = LocalizationUtils.isLayoutRtl() ? 0 : buttonPadding;
|
| int buttonPaddingRight = LocalizationUtils.isLayoutRtl() ? buttonPadding : 0;
|
| view.setViewPadding(R.id.button, buttonPaddingLeft, 0, buttonPaddingRight, 0);
|
| }
|
|
|
| - view.setTextViewText(R.id.button, action.getTitle());
|
| - view.setOnClickPendingIntent(R.id.button, action.getActionIntent());
|
| + view.setTextViewText(R.id.button, action.title);
|
| + view.setOnClickPendingIntent(R.id.button, action.intent);
|
| bigView.addView(R.id.buttons, view);
|
| }
|
| }
|
| @@ -306,7 +350,7 @@ private void configureSettingsButton(RemoteViews bigView) {
|
| if (mSettingsAction == null) {
|
| return;
|
| }
|
| - bigView.setOnClickPendingIntent(R.id.origin, mSettingsAction.getActionIntent());
|
| + bigView.setOnClickPendingIntent(R.id.origin, mSettingsAction.intent);
|
| if (useMaterial()) {
|
| bigView.setInt(R.id.origin_settings_icon, "setColorFilter", BUTTON_ICON_COLOR_MATERIAL);
|
| }
|
| @@ -376,21 +420,27 @@ static int calculateMaxBodyLines(float fontScale) {
|
| * @return The amount of padding to be used, in pixels.
|
| */
|
| @VisibleForTesting
|
| - static int calculateScaledPadding(float fontScale, DisplayMetrics displayMetrics) {
|
| + static int calculateScaledPadding(float fontScale, DisplayMetrics metrics) {
|
| float paddingScale = 1.0f;
|
| if (fontScale > 1.0f) {
|
| fontScale = Math.min(fontScale, FONT_SCALE_LARGE);
|
| paddingScale = (FONT_SCALE_LARGE - fontScale) / (FONT_SCALE_LARGE - 1.0f);
|
| }
|
| - return dpToPx(paddingScale * MAX_SCALABLE_PADDING_DP, displayMetrics);
|
| + return dpToPx(paddingScale * MAX_SCALABLE_PADDING_DP, metrics);
|
| }
|
|
|
| /**
|
| * Converts a dp value to a px value.
|
| */
|
| - private static int dpToPx(float value, DisplayMetrics displayMetrics) {
|
| - return Math.round(
|
| - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, displayMetrics));
|
| + private static int dpToPx(float value, DisplayMetrics metrics) {
|
| + return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, metrics));
|
| + }
|
| +
|
| + /**
|
| + * Converts a px value to a dp value.
|
| + */
|
| + private static int pxToDp(int value, DisplayMetrics metrics) {
|
| + return Math.round(value / (metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
|
| }
|
|
|
| /**
|
| @@ -399,4 +449,25 @@ private static int dpToPx(float value, DisplayMetrics displayMetrics) {
|
| private static boolean useMaterial() {
|
| return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
|
| }
|
| +
|
| + /**
|
| + * Whether to use bitmaps for action icons, if they are specified.
|
| + */
|
| + private static boolean useActionIconBitmaps() {
|
| + // The Notification.Action.Builder(Icon, CharSequence, PendingIntent) constructor is API
|
| + // level 23.
|
| + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
|
| + }
|
| +
|
| + @SuppressWarnings("deprecation") // For addAction(int, CharSequence, PendingIntent)
|
| + @TargetApi(Build.VERSION_CODES.M) // For Icon.
|
| + private static void addAction(Notification.Builder builder, Action action) {
|
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && action.icon != null) {
|
| + Icon icon = Icon.createWithBitmap(action.icon);
|
| + builder.addAction(
|
| + new Notification.Action.Builder(icon, action.title, action.intent).build());
|
| + } else {
|
| + builder.addAction(action.iconId, action.title, action.intent);
|
| + }
|
| + }
|
| }
|
|
|