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

Commit f437e2c6 authored by Jeff DeCew's avatar Jeff DeCew
Browse files

Implement the new visual style of the expand button

Bug: 181048615
Test: visual and talkback testing of normal notifs, conversations, groups.
Change-Id: Ic9cd89e899a03010a523bee4f1ef72fbfbd8b43f
parent 3a681937
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -5326,11 +5326,17 @@ public class Notification implements Parcelable
        }

        private void bindExpandButton(RemoteViews contentView, StandardTemplateParams p) {
            int color = isColorized(p) ? getPrimaryTextColor(p) : getSecondaryTextColor(p);
            contentView.setDrawableTint(R.id.expand_button, false, color,
                    PorterDuff.Mode.SRC_ATOP);
            contentView.setInt(R.id.expand_button, "setOriginalNotificationColor",
                    color);
            int primaryColor = getPrimaryTextColor(p);
            // TODO(b/181048615): Use 'protection' color from the new palette
            int protectionColor = primaryColor & 0x44ffffff;
            int opaqueProtectionColor =
                    ContrastColorUtil.compositeColors(protectionColor, resolveBackgroundColor(p));
            contentView.setInt(R.id.expand_button, "setDefaultTextColor", primaryColor);
            contentView.setInt(R.id.expand_button, "setDefaultPillColor", opaqueProtectionColor);
            contentView.setInt(
                    R.id.expand_button, "setHighlightTextColor", resolveBackgroundColor(p));
            contentView.setInt(
                    R.id.expand_button, "setHighlightPillColor", resolveContrastColor(p));
        }

        private void bindHeaderChronometerAndTime(RemoteViews contentView,
@@ -9373,7 +9379,6 @@ public class Notification implements Parcelable
                    .hideLargeIcon(true)
                    .text(text)
                    .summaryText(mBuilder.processLegacyText(mVerificationText));
            // TODO(b/179178086): hide the snooze button
            RemoteViews contentView = mBuilder.applyStandardTemplate(
                    mBuilder.getCallLayoutResource(), p, null /* result */);

+10 −26
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import android.app.Person;
import android.app.RemoteInputHistoryItem;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
@@ -62,11 +61,9 @@ import android.widget.RemoteViews;
import android.widget.TextView;

import com.android.internal.R;
import com.android.internal.graphics.ColorUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Consumer;

@@ -118,7 +115,6 @@ public class ConversationLayout extends FrameLayout
    private ViewGroup mExpandButtonAndContentContainer;
    private NotificationExpandButton mExpandButton;
    private MessagingLinearLayout mImageMessageContainer;
    private int mExpandButtonExpandedTopMargin;
    private int mBadgedSideMargins;
    private int mConversationAvatarSize;
    private int mConversationAvatarSizeExpanded;
@@ -147,7 +143,6 @@ public class ConversationLayout extends FrameLayout
    private int mFacePileProtectionWidth;
    private int mFacePileProtectionWidthExpanded;
    private boolean mImportantConversation;
    private TextView mUnreadBadge;
    private View mFeedbackIcon;
    private float mMinTouchSize;
    private Icon mConversationIcon;
@@ -245,8 +240,6 @@ public class ConversationLayout extends FrameLayout
        mContentContainer = findViewById(R.id.notification_action_list_margin_target);
        mExpandButtonAndContentContainer = findViewById(R.id.expand_button_and_content_container);
        mExpandButton = findViewById(R.id.expand_button);
        mExpandButtonExpandedTopMargin = getResources().getDimensionPixelSize(
                R.dimen.conversation_expand_button_top_margin_expanded);
        mNotificationHeaderExpandedPadding = getResources().getDimensionPixelSize(
                R.dimen.conversation_header_expanded_padding_end);
        mContentMarginEnd = getResources().getDimensionPixelSize(
@@ -286,7 +279,6 @@ public class ConversationLayout extends FrameLayout
        mAppName.setOnVisibilityChangedListener((visibility) -> {
            onAppNameVisibilityChanged();
        });
        mUnreadBadge = findViewById(R.id.conversation_unread_count);
        mConversationContentStart = getResources().getDimensionPixelSize(
                R.dimen.conversation_content_start);
        mInternalButtonPadding
@@ -426,17 +418,7 @@ public class ConversationLayout extends FrameLayout

    /** @hide */
    public void setUnreadCount(int unreadCount) {
        boolean visible = mIsCollapsed && unreadCount > 1;
        mUnreadBadge.setVisibility(visible ? VISIBLE : GONE);
        if (visible) {
            CharSequence text = unreadCount >= 100
                    ? getResources().getString(R.string.unread_convo_overflow, 99)
                    : String.format(Locale.getDefault(), "%d", unreadCount);
            mUnreadBadge.setText(text);
            mUnreadBadge.setBackgroundTintList(ColorStateList.valueOf(mLayoutColor));
            boolean needDarkText = ColorUtils.calculateLuminance(mLayoutColor) > 0.5f;
            mUnreadBadge.setTextColor(needDarkText ? Color.BLACK : Color.WHITE);
        }
        mExpandButton.setNumber(unreadCount);
    }

    private void addRemoteInputHistoryToMessages(
@@ -1132,15 +1114,16 @@ public class ConversationLayout extends FrameLayout
    }

    private void updateExpandButton() {
        int gravity;
        int topMargin = 0;
        int buttonGravity;
        int containerHeight;
        ViewGroup newContainer;
        if (mIsCollapsed) {
            gravity = Gravity.CENTER;
            buttonGravity = Gravity.CENTER;
            containerHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
            newContainer = mExpandButtonAndContentContainer;
        } else {
            gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
            topMargin = mExpandButtonExpandedTopMargin;
            buttonGravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
            containerHeight = ViewGroup.LayoutParams.MATCH_PARENT;
            newContainer = this;
        }
        mExpandButton.setExpanded(!mIsCollapsed);
@@ -1149,14 +1132,14 @@ public class ConversationLayout extends FrameLayout
        // content when collapsed, but allows the content to flow under it when expanded.
        if (newContainer != mExpandButtonContainer.getParent()) {
            ((ViewGroup) mExpandButtonContainer.getParent()).removeView(mExpandButtonContainer);
            mExpandButtonContainer.getLayoutParams().height = containerHeight;
            newContainer.addView(mExpandButtonContainer);
        }

        // update if the expand button is centered
        LinearLayout.LayoutParams layoutParams =
                (LinearLayout.LayoutParams) mExpandButton.getLayoutParams();
        layoutParams.gravity = gravity;
        layoutParams.topMargin = topMargin;
        layoutParams.gravity = buttonGravity;
        mExpandButton.setLayoutParams(layoutParams);
    }

@@ -1210,6 +1193,7 @@ public class ConversationLayout extends FrameLayout
            mExpandButtonContainer.setVisibility(GONE);
            mConversationIconContainer.setOnClickListener(null);
        }
        mExpandButton.setVisibility(VISIBLE);
        updateContentEndPaddings();
    }

+111 −34
Original line number Diff line number Diff line
@@ -16,30 +16,42 @@

package com.android.internal.widget;

import static com.android.internal.widget.ColoredIconHelper.applyGrayTint;

import android.annotation.ColorInt;
import android.annotation.Nullable;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.RemotableViewMethod;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;

import com.android.internal.R;

import java.util.Locale;

/**
 * An expand button in a notification
 */
@RemoteViews.RemoteView
public class NotificationExpandButton extends ImageView {
public class NotificationExpandButton extends FrameLayout {

    private final int mMinTouchTargetSize;
    private View mPillView;
    private TextView mNumberView;
    private ImageView mIconView;
    private boolean mExpanded;
    private int mOriginalNotificationColor;
    private int mNumber;
    private int mDefaultPillColor;
    private int mDefaultTextColor;
    private int mHighlightPillColor;
    private int mHighlightTextColor;
    private boolean mDisallowColor;

    public NotificationExpandButton(Context context) {
        this(context, null, 0, 0);
@@ -57,7 +69,14 @@ public class NotificationExpandButton extends ImageView {
    public NotificationExpandButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mMinTouchTargetSize = (int) (getResources().getDisplayMetrics().density * 48 + 0.5);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mPillView = findViewById(R.id.expand_button_pill);
        mNumberView = findViewById(R.id.expand_button_number);
        mIconView = findViewById(R.id.expand_button_icon);
    }

    /**
@@ -72,7 +91,6 @@ public class NotificationExpandButton extends ImageView {
        } else {
            super.getBoundsOnScreen(outRect, clipToParent);
        }
        extendRectToMinTouchSize(outRect);
    }

    /**
@@ -89,32 +107,12 @@ public class NotificationExpandButton extends ImageView {
        return super.pointInView(localX, localY, slop);
    }

    @RemotableViewMethod
    public void setOriginalNotificationColor(int color) {
        mOriginalNotificationColor = color;
    }

    public int getOriginalNotificationColor() {
        return mOriginalNotificationColor;
    }

    /**
     * Set the button's color filter: to gray if true, otherwise colored.
     * If this button has no original color, this has no effect.
     * Disable the use of the accent colors for this view, if true.
     */
    public void setGrayedOut(boolean shouldApply) {
        applyGrayTint(mContext, getDrawable(), shouldApply, mOriginalNotificationColor);
    }

    private void extendRectToMinTouchSize(Rect rect) {
        if (rect.width() < mMinTouchTargetSize) {
            rect.left = rect.centerX() - mMinTouchTargetSize / 2;
            rect.right = rect.left + mMinTouchTargetSize;
        }
        if (rect.height() < mMinTouchTargetSize) {
            rect.top = rect.centerY() - mMinTouchTargetSize / 2;
            rect.bottom = rect.top + mMinTouchTargetSize;
        }
        mDisallowColor = shouldApply;
        updateColors();
    }

    @Override
@@ -129,10 +127,10 @@ public class NotificationExpandButton extends ImageView {
    @RemotableViewMethod
    public void setExpanded(boolean expanded) {
        mExpanded = expanded;
        updateExpandButton();
        updateExpandedState();
    }

    private void updateExpandButton() {
    private void updateExpandedState() {
        int drawableId;
        int contentDescriptionId;
        if (mExpanded) {
@@ -142,8 +140,87 @@ public class NotificationExpandButton extends ImageView {
            drawableId = R.drawable.ic_expand_notification;
            contentDescriptionId = R.string.expand_button_content_description_collapsed;
        }
        setImageDrawable(getContext().getDrawable(drawableId));
        setColorFilter(mOriginalNotificationColor);
        setContentDescription(mContext.getText(contentDescriptionId));
        mIconView.setImageDrawable(getContext().getDrawable(drawableId));

        // changing the expanded state can affect the number display
        updateNumber();
    }

    private void updateNumber() {
        if (shouldShowNumber()) {
            CharSequence text = mNumber >= 100
                    ? getResources().getString(R.string.unread_convo_overflow, 99)
                    : String.format(Locale.getDefault(), "%d", mNumber);
            mNumberView.setText(text);
            mNumberView.setVisibility(VISIBLE);
        } else {
            mNumberView.setVisibility(GONE);
        }

        // changing number can affect the color
        updateColors();
    }

    private void updateColors() {
        if (shouldShowNumber() && !mDisallowColor) {
            mPillView.setBackgroundTintList(ColorStateList.valueOf(mHighlightPillColor));
            mIconView.setColorFilter(mHighlightTextColor);
            mNumberView.setTextColor(mHighlightTextColor);
        } else {
            mPillView.setBackgroundTintList(ColorStateList.valueOf(mDefaultPillColor));
            mIconView.setColorFilter(mDefaultTextColor);
            mNumberView.setTextColor(mDefaultTextColor);
        }
    }

    private boolean shouldShowNumber() {
        return !mExpanded && mNumber > 1;
    }

    /**
     * Set the color used for the expand chevron and the text
     */
    @RemotableViewMethod
    public void setDefaultTextColor(int color) {
        mDefaultTextColor = color;
        updateColors();
    }

    /**
     * Sets the color used to for the expander when there is no number shown
     */
    @RemotableViewMethod
    public void setDefaultPillColor(@ColorInt int color) {
        mDefaultPillColor = color;
        updateColors();
    }

    /**
     * Set the color used for the expand chevron and the text
     */
    @RemotableViewMethod
    public void setHighlightTextColor(int color) {
        mHighlightTextColor = color;
        updateColors();
    }

    /**
     * Sets the color used to highlight the expander when there is a number shown
     */
    @RemotableViewMethod
    public void setHighlightPillColor(@ColorInt int color) {
        mHighlightPillColor = color;
        updateColors();
    }

    /**
     * Sets the number shown inside the expand button.
     * This only appears when the expand button is collapsed, and when greater than 1.
     */
    @RemotableViewMethod
    public void setNumber(int number) {
        mNumber = number;
        updateNumber();
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -14,6 +14,6 @@
  ~ limitations under the License.
  -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20sp" />
    <corners android:radius="@dimen/notification_expand_button_pill_height" />
    <solid android:color="@android:color/white" />
</shape>
 No newline at end of file
+1 −4
Original line number Diff line number Diff line
@@ -21,8 +21,5 @@ Copyright (C) 2020 The Android Open Source Project
    android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M18.59,16.41L20.0,15.0l-8.0,-8.0 -8.0,8.0 1.41,1.41L12.0,9.83"/>
    <path
        android:pathData="M0 0h24v24H0V0z"
        android:fillColor="#00000000"/>
        android:pathData="M18.59,15.41L20.0,14.0l-8.0,-8.0 -8.0,8.0 1.41,1.41L12.0,8.83"/>
</vector>
 No newline at end of file
Loading