From f79877c04c071b7ae1618395f0a1dce134fec36e Mon Sep 17 00:00:00 2001 From: Tony Wickham Date: Fri, 27 Jan 2017 08:45:49 -0800 Subject: Ensure notification icons have enough contrast with background. This uses the same color calculations as the system, except that we use the extracted notification background instead of assuming it is white. Bug: 32410600 Change-Id: I7be8b9459ca38d01a6780758898541e69ec42576 --- .../notification/NotificationFooterLayout.java | 6 +++-- .../launcher3/notification/NotificationInfo.java | 28 ++++++++++++++++++---- .../notification/NotificationItemView.java | 6 +++-- .../notification/NotificationMainView.java | 24 +++++++++++-------- 4 files changed, 46 insertions(+), 18 deletions(-) (limited to 'src/com/android/launcher3/notification') diff --git a/src/com/android/launcher3/notification/NotificationFooterLayout.java b/src/com/android/launcher3/notification/NotificationFooterLayout.java index cd610bd3b..07178ce0c 100644 --- a/src/com/android/launcher3/notification/NotificationFooterLayout.java +++ b/src/com/android/launcher3/notification/NotificationFooterLayout.java @@ -61,6 +61,7 @@ public class NotificationFooterLayout extends LinearLayout { LinearLayout.LayoutParams mIconLayoutParams; private LinearLayout mIconRow; + private int mBackgroundColor; private int mTextColor; public NotificationFooterLayout(Context context) { @@ -90,7 +91,8 @@ public class NotificationFooterLayout extends LinearLayout { } public void applyColors(IconPalette iconPalette) { - setBackgroundTintList(ColorStateList.valueOf(iconPalette.backgroundColor)); + mBackgroundColor = iconPalette.backgroundColor; + setBackgroundTintList(ColorStateList.valueOf(mBackgroundColor)); findViewById(R.id.divider).setBackgroundColor(iconPalette.secondaryColor); mTextColor = iconPalette.textColor; } @@ -130,7 +132,7 @@ public class NotificationFooterLayout extends LinearLayout { private void addNotificationIconForInfo(NotificationInfo info, boolean fromOverflow) { View icon = new View(getContext()); - icon.setBackground(info.iconDrawable); + icon.setBackground(info.getIconForBackground(getContext(), mBackgroundColor)); icon.setOnClickListener(info); int addIndex = mIconRow.getChildCount(); if (fromOverflow) { diff --git a/src/com/android/launcher3/notification/NotificationInfo.java b/src/com/android/launcher3/notification/NotificationInfo.java index bf57b2aff..af5e817f1 100644 --- a/src/com/android/launcher3/notification/NotificationInfo.java +++ b/src/com/android/launcher3/notification/NotificationInfo.java @@ -25,6 +25,7 @@ import android.service.notification.StatusBarNotification; import android.view.View; import com.android.launcher3.Launcher; +import com.android.launcher3.graphics.IconPalette; import com.android.launcher3.popup.PopupContainerWithArrow; import com.android.launcher3.util.PackageUserKey; @@ -41,11 +42,14 @@ public class NotificationInfo implements View.OnClickListener { public final String notificationKey; public final CharSequence title; public final CharSequence text; - public final Drawable iconDrawable; public final PendingIntent intent; public final boolean autoCancel; public final boolean dismissable; + private final Drawable mIconDrawable; + private boolean mShouldTintIcon; + private int mIconColor; + /** * Extracts the data that we need from the StatusBarNotification. */ @@ -60,10 +64,12 @@ public class NotificationInfo implements View.OnClickListener { Icon icon = notification.getLargeIcon(); if (icon == null) { icon = notification.getSmallIcon(); - iconDrawable = icon.loadDrawable(context); - iconDrawable.setTint(statusBarNotification.getNotification().color); + mIconDrawable = icon.loadDrawable(context); + mIconColor = statusBarNotification.getNotification().color; + mShouldTintIcon = true; } else { - iconDrawable = icon.loadDrawable(context); + mIconDrawable = icon.loadDrawable(context); + mShouldTintIcon = false; } intent = notification.contentIntent; autoCancel = (notification.flags & Notification.FLAG_AUTO_CANCEL) != 0; @@ -83,4 +89,18 @@ public class NotificationInfo implements View.OnClickListener { } PopupContainerWithArrow.getOpen(launcher).close(true); } + + public Drawable getIconForBackground(Context context, int background) { + if (!mShouldTintIcon) { + return mIconDrawable; + } + mIconColor = IconPalette.resolveContrastColor(context, mIconColor, background); + Drawable icon = mIconDrawable.mutate(); + // DrawableContainer ignores the color filter if it's already set, so clear it first to + // get it set and invalidated properly. + icon.setTintList(null); + icon.setTint(mIconColor); + mShouldTintIcon = false; + return icon; + } } diff --git a/src/com/android/launcher3/notification/NotificationItemView.java b/src/com/android/launcher3/notification/NotificationItemView.java index b74cd4e1e..422722d8c 100644 --- a/src/com/android/launcher3/notification/NotificationItemView.java +++ b/src/com/android/launcher3/notification/NotificationItemView.java @@ -35,7 +35,9 @@ import com.android.launcher3.graphics.IconPalette; import com.android.launcher3.popup.PopupItemView; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import static com.android.launcher3.LauncherAnimUtils.animateViewHeight; @@ -117,7 +119,7 @@ public class NotificationItemView extends PopupItemView { mHeader.setBackgroundTintList(ColorStateList.valueOf(iconPalette.backgroundColor)); mHeader.setTextColor(ColorStateList.valueOf(iconPalette.textColor)); mDivider.setBackgroundColor(iconPalette.secondaryColor); - mMainView.setBackgroundColor(iconPalette.backgroundColor); + mMainView.applyColors(iconPalette); mFooter.applyColors(iconPalette); } @@ -135,7 +137,7 @@ public class NotificationItemView extends PopupItemView { @Override public void onIconAnimationEnd(NotificationInfo newMainNotification) { if (newMainNotification != null) { - mMainView.applyNotificationInfo(newMainNotification, mIconView, mIconPalette); + mMainView.applyNotificationInfo(newMainNotification, mIconView, true); // Remove the animated notification from the footer by calling trim // TODO: Remove the notification in NotificationFooterLayout directly // instead of relying on this hack. diff --git a/src/com/android/launcher3/notification/NotificationMainView.java b/src/com/android/launcher3/notification/NotificationMainView.java index 2997d4010..76a84b7c6 100644 --- a/src/com/android/launcher3/notification/NotificationMainView.java +++ b/src/com/android/launcher3/notification/NotificationMainView.java @@ -41,6 +41,7 @@ public class NotificationMainView extends LinearLayout implements SwipeHelper.Ca private NotificationInfo mNotificationInfo; private TextView mTitleView; private TextView mTextView; + private IconPalette mIconPalette; public NotificationMainView(Context context) { this(context, null, 0); @@ -62,35 +63,38 @@ public class NotificationMainView extends LinearLayout implements SwipeHelper.Ca mTextView = (TextView) findViewById(R.id.text); } + public void applyColors(IconPalette iconPalette) { + setBackgroundColor(iconPalette.backgroundColor); + mIconPalette = iconPalette; + } + public void applyNotificationInfo(NotificationInfo mainNotification, View iconView) { - applyNotificationInfo(mainNotification, iconView, null); + applyNotificationInfo(mainNotification, iconView, false); } /** - * @param iconPalette if not null, indicates that the new info should be animated in, - * and that part of this animation includes animating the background - * from iconPalette.secondaryColor to iconPalette.backgroundColor. + * Sets the content of this view, animating it after a new icon shifts up if necessary. */ public void applyNotificationInfo(NotificationInfo mainNotification, View iconView, - @Nullable IconPalette iconPalette) { - boolean animate = iconPalette != null; + boolean animate) { if (animate) { mTitleView.setAlpha(0); mTextView.setAlpha(0); - setBackgroundColor(iconPalette.secondaryColor); + setBackgroundColor(mIconPalette.secondaryColor); } mNotificationInfo = mainNotification; mTitleView.setText(mNotificationInfo.title); mTextView.setText(mNotificationInfo.text); - iconView.setBackground(mNotificationInfo.iconDrawable); + iconView.setBackground(mNotificationInfo.getIconForBackground( + getContext(), mIconPalette.backgroundColor)); setOnClickListener(mNotificationInfo); setTranslationX(0); if (animate) { AnimatorSet animation = LauncherAnimUtils.createAnimatorSet(); Animator textFade = new LauncherViewPropertyAnimator(mTextView).alpha(1); Animator titleFade = new LauncherViewPropertyAnimator(mTitleView).alpha(1); - ValueAnimator colorChange = ValueAnimator.ofArgb(iconPalette.secondaryColor, - iconPalette.backgroundColor); + ValueAnimator colorChange = ValueAnimator.ofArgb(mIconPalette.secondaryColor, + mIconPalette.backgroundColor); colorChange.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { -- cgit v1.2.3