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

Commit ac5f0274 authored by Selim Cinek's avatar Selim Cinek
Browse files

Fixed the contrast for low-priority notifications

The low priority notifications had their contrast calculated
against the white background and weren't satisfying
our contrast requirements.
This also aligns the case where the app had no colors better,
and ensures that it's actually using the secondary text
color there.

Test: existing tests pass
Change-Id: Ic11e9d06783e60998f35e0eb7f6f29fb1d86c7df
Fixes: 37444266
parent 0f66a4cc
Loading
Loading
Loading
Loading
+39 −3
Original line number Diff line number Diff line
@@ -2675,6 +2675,7 @@ public class Notification implements Parcelable
        private int mActionBarColor = COLOR_INVALID;
        private int mBackgroundColor = COLOR_INVALID;
        private int mForegroundColor = COLOR_INVALID;
        private int mBackgroundColorHint = COLOR_INVALID;

        /**
         * Constructs a new Builder with the defaults:
@@ -3839,6 +3840,13 @@ public class Notification implements Parcelable
                            backgroundColor);
                    mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
                            backgroundColor);
                    if (backgroundColor != COLOR_DEFAULT
                            && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
                        mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
                                mPrimaryTextColor, backgroundColor, 4.5);
                        mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
                                mSecondaryTextColor, backgroundColor, 4.5);
                    }
                } else {
                    double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
                    double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
@@ -4659,10 +4667,26 @@ public class Notification implements Parcelable
            if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
                return mCachedContrastColor;
            }
            final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);

            int color;
            int background = mBackgroundColorHint;
            if (mBackgroundColorHint == COLOR_INVALID) {
                background = mContext.getColor(
                        com.android.internal.R.color.notification_material_background_color);
            }
            if (mN.color == COLOR_DEFAULT) {
                ensureColors();
                color = mSecondaryTextColor;
            } else {
                color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
                        background);
            }
            if (Color.alpha(color) < 255) {
                // alpha doesn't go well for color filters, so let's blend it manually
                color = NotificationColorUtil.compositeColors(color, background);
            }
            mCachedContrastColorIsFor = mN.color;
            return mCachedContrastColor = contrasted;
            return mCachedContrastColor = color;
        }

        int resolveAmbientColor() {
@@ -4879,7 +4903,8 @@ public class Notification implements Parcelable
            if (isColorized()) {
                return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
            } else {
                return COLOR_DEFAULT;
                return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
                        : COLOR_DEFAULT;
            }
        }

@@ -4910,6 +4935,17 @@ public class Notification implements Parcelable
            mTextColorsAreForBackground = COLOR_INVALID;
            ensureColors();
        }

        /**
         * Sets the background color for this notification to be a different one then the default.
         * This is mainly used to calculate contrast and won't necessarily be applied to the
         * background.
         *
         * @hide
         */
        public void setBackgroundColorHint(int backgroundColor) {
            mBackgroundColorHint = backgroundColor;
        }
    }

    /**
+44 −5
Original line number Diff line number Diff line
@@ -285,6 +285,38 @@ public class NotificationColorUtil {
        return ColorUtilsFromCompat.LABToColor(low, a, b);
    }

    /**
     * Finds a suitable alpha such that there's enough contrast.
     *
     * @param color the color to start searching from.
     * @param backgroundColor the color to ensure contrast against.
     * @param minRatio the minimum contrast ratio required.
     * @return the same color as {@param color} with potentially modified alpha to meet contrast
     */
    public static int findAlphaToMeetContrast(int color, int backgroundColor, double minRatio) {
        int fg = color;
        int bg = backgroundColor;
        if (ColorUtilsFromCompat.calculateContrast(fg, bg) >= minRatio) {
            return color;
        }
        int startAlpha = Color.alpha(color);
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);

        int low = startAlpha, high = 255;
        for (int i = 0; i < 15 && high - low > 0; i++) {
            final int alpha = (low + high) / 2;
            fg = Color.argb(alpha, r, g, b);
            if (ColorUtilsFromCompat.calculateContrast(fg, bg) > minRatio) {
                high = alpha;
            } else {
                low = alpha;
            }
        }
        return Color.argb(high, r, g, b);
    }

    /**
     * Finds a suitable color such that there's enough contrast.
     *
@@ -373,19 +405,19 @@ public class NotificationColorUtil {
     * color for the Notification's action and header text.
     *
     * @param notificationColor the color of the notification or {@link Notification#COLOR_DEFAULT}
     * @param backgroundColor the background color to ensure the contrast against.
     * @return a color of the same hue with enough contrast against the backgrounds.
     */
    public static int resolveContrastColor(Context context, int notificationColor) {
    public static int resolveContrastColor(Context context, int notificationColor,
            int backgroundColor) {
        final int resolvedColor = resolveColor(context, notificationColor);

        final int actionBg = context.getColor(
                com.android.internal.R.color.notification_action_list);
        final int notiBg = context.getColor(
                com.android.internal.R.color.notification_material_background_color);

        int color = resolvedColor;
        color = NotificationColorUtil.ensureLargeTextContrast(color, actionBg);
        color = NotificationColorUtil.ensureTextContrast(color, notiBg);
        color = NotificationColorUtil.ensureTextContrast(color, backgroundColor);

        if (color != resolvedColor) {
            if (DEBUG){
@@ -394,7 +426,7 @@ public class NotificationColorUtil {
                                + " and %s (over background) by changing #%s to %s",
                        context.getPackageName(),
                        NotificationColorUtil.contrastChange(resolvedColor, color, actionBg),
                        NotificationColorUtil.contrastChange(resolvedColor, color, notiBg),
                        NotificationColorUtil.contrastChange(resolvedColor, color, backgroundColor),
                        Integer.toHexString(resolvedColor), Integer.toHexString(color)));
            }
        }
@@ -501,6 +533,13 @@ public class NotificationColorUtil {
        return ColorUtilsFromCompat.calculateContrast(foregroundColor, backgroundColor);
    }

    /**
     * Composite two potentially translucent colors over each other and returns the result.
     */
    public static int compositeColors(int foreground, int background) {
        return ColorUtilsFromCompat.compositeColors(foreground, background);
    }

    /**
     * Framework copy of functions needed from android.support.v4.graphics.ColorUtils.
     */
+1 −1
Original line number Diff line number Diff line
@@ -891,7 +891,7 @@ public abstract class ActivatableNotificationView extends ExpandableOutlineView
     * @return the calculated background color
     */
    private int calculateBgColor(boolean withTint, boolean withOverRide) {
        if (mDark) {
        if (withTint && mDark) {
            return getContext().getColor(R.color.notification_material_background_dark_color);
        }
        if (withOverRide && mOverrideTint != NO_COLOR) {
+4 −2
Original line number Diff line number Diff line
@@ -355,7 +355,8 @@ public class ExpandableNotificationRow extends ActivatableNotificationView
                NotificationColorUtil.getInstance(mContext));
        int color = StatusBarIconView.NO_COLOR;
        if (colorize) {
            color = mEntry.getContrastedColor(mContext, mIsLowPriority && !isExpanded());
            color = mEntry.getContrastedColor(mContext, mIsLowPriority && !isExpanded(),
                    getBackgroundColorWithoutTint());
        }
        expandedIcon.setStaticDrawableColor(color);
    }
@@ -859,7 +860,8 @@ public class ExpandableNotificationRow extends ActivatableNotificationView

    private void updateNotificationColor() {
        mNotificationColor = NotificationColorUtil.resolveContrastColor(mContext,
                getStatusBarNotification().getNotification().color);
                getStatusBarNotification().getNotification().color,
                getBackgroundColorWithoutTint());
    }

    public HybridNotificationView getSingleLineView() {
+5 −3
Original line number Diff line number Diff line
@@ -201,13 +201,15 @@ public class NotificationData {
            }
        }

        public int getContrastedColor(Context context, boolean ambient) {
            int rawColor = ambient ? Notification.COLOR_DEFAULT :
        public int getContrastedColor(Context context, boolean isLowPriority,
                int backgroundColor) {
            int rawColor = isLowPriority ? Notification.COLOR_DEFAULT :
                    notification.getNotification().color;
            if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) {
                return mCachedContrastColor;
            }
            final int contrasted = NotificationColorUtil.resolveContrastColor(context, rawColor);
            final int contrasted = NotificationColorUtil.resolveContrastColor(context, rawColor,
                    backgroundColor);
            mCachedContrastColorIsFor = rawColor;
            mCachedContrastColor = contrasted;
            return mCachedContrastColor;
Loading