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

Commit 9c18d720 authored by Ioana Alexandru's avatar Ioana Alexandru
Browse files

Refactor right icon space calculation for promoted notifs

Promoted notifications don't show the expander, so we need to correctly
calculate the space needed on the right side both with and without a
large icon present.

This was already done, but it was a bit hacky and had a couple of bugs:
- the margin for the right icon itself was set to
  notification_content_margin (20dp) all the time if promoted, but this
  dimension is no longer used if notificationsRedesignTemplates is on -
  it should be notification_2025_margin (16dp) in that case
- the margin for the top line, that needs to be aligned to the right
  icon accordingly, was calculated using
  notification_content_margin_end (16dp), leading to an unintentional
  4dp offset
- mHeadingExtraMarginSet was used for this special top line margin; this
  is normally calculated in setRightIconState, and according to its
  javadoc it shouldn't include the 16dp margin; since this margin was
  needed for the promoted notifs, the "hack" was to re-calculate it
  AFTER setRightIconState to include the margin.

What this change does is:
- consistently use notification_content_margin/notification_2025_margin
  depending on the redesign flag state
- always calculate and apply the end margin for the right icon (in
  bindLargeIcon) when one of the flags is on (this will also be needed
  in a follow-up fix)
- use mHeadingFullMarginSet, since it already includes the margin we
  need, and remove the override to mHeadingExtraMarginSet

Bug: 407819104
Bug: 420643875
Bug: 403536710
Test: check that the margins of both promoted and regular notifications
are correct, both with and without a large icon
Flag: android.app.notifications_redesign_templates
Flag: android.app.ui_rich_ongoing

Change-Id: If7c65d83bbf3de7b1ee2ea0eb46a72b1d6c05e54
parent 65110ddf
Loading
Loading
Loading
Loading
+42 −29
Original line number Diff line number Diff line
@@ -6266,9 +6266,14 @@ public class Notification implements Parcelable
                result = new TemplateBindResult();
            }
            bindLargeIcon(contentView, p, result);
            if (!p.mHeaderless) {
                // views in states with a header (big states)
                result.mHeadingExtraMarginSet.applyToView(contentView, R.id.notification_header);
            if (!p.mHeaderless) { // views in states with a header (expanded states)
                if (mN.isPromotedOngoing()) {
                    // When promoted, the expander is hidden but we need to include the notif margin
                    result.mHeadingFullMarginSet.applyToView(contentView, R.id.notification_header);
                } else {
                    result.mHeadingExtraMarginSet.applyToView(contentView,
                            R.id.notification_header);
                }
                result.mTitleMarginSet.applyToView(contentView, R.id.title);
                // If there is no title, the text (or big_text) needs to wrap around the image
                result.mTitleMarginSet.applyToView(contentView, p.mTextViewId);
@@ -6310,7 +6315,8 @@ public class Notification implements Parcelable
            final float contentMarginDp = resources.getDimension(
                    R.dimen.notification_content_margin_end) / density;
            float spaceForExpanderDp;
            if (mN.isPromotedOngoing()) {
            if (Flags.uiRichOngoing() && mN.isPromotedOngoing() && !mParams.mHeaderless) {
                // No expander is shown in promoted notifications
                spaceForExpanderDp = 0;
            } else {
                final int expanderSizeRes;
@@ -6339,16 +6345,11 @@ public class Notification implements Parcelable
                    }
                }
            }
            result.setRightIconState(rightIcon != null /* visible */, viewWidthDp, viewHeightDp);
            // Margin needed for the header to accommodate the icon when shown
            final float extraMarginEndDpIfVisible = viewWidthDp + iconMarginDp;
            result.setRightIconState(rightIcon != null /* visible */, viewWidthDp, viewHeightDp,
                    extraMarginEndDpIfVisible, spaceForExpanderDp, notificationMarginDp);
            if (mN.isPromotedOngoing() && !mParams.mHeaderless) {
                result.mHeadingExtraMarginSet.setValues(
                        /* valueIfGone = */ contentMarginDp,
                        /* valueIfVisible = */ extraMarginEndDpIfVisible + contentMarginDp);
            }
            result.calculateMargins(extraMarginEndDpIfVisible, spaceForExpanderDp,
                    notificationMarginDp);
        }
        /**
@@ -6391,20 +6392,9 @@ public class Notification implements Parcelable
                contentView.setImageViewIcon(R.id.right_icon, rightIcon);
                contentView.setIntTag(R.id.right_icon, R.id.tag_keep_when_showing_left_icon,
                        isPromotedPicture ? 1 : 0);
                if (Flags.uiRichOngoing() && !p.mHeaderless) {
                    final int largeIconMarginEnd;
                    if (mN.isPromotedOngoing()) {
                        largeIconMarginEnd = R.dimen.notification_content_margin;
                    } else {
                        if (notificationsRedesignTemplates()) {
                            largeIconMarginEnd =
                                    R.dimen.notification_2025_right_icon_expanded_margin_end;
                        } else {
                            largeIconMarginEnd = R.dimen.notification_header_expand_icon_size;
                        }
                    }
                    contentView.setViewLayoutMarginDimen(
                            R.id.right_icon, RemoteViews.MARGIN_END, largeIconMarginEnd);
                if ((notificationsRedesignTemplates() || Flags.uiRichOngoing()) && !p.mHeaderless) {
                    contentView.setViewLayoutMargin(R.id.right_icon,
                            RemoteViews.MARGIN_END, getLargeIconMarginEnd(p), COMPLEX_UNIT_PX);
                }
                processLargeLegacyIcon(rightIcon, contentView, p);
@@ -6417,6 +6407,26 @@ public class Notification implements Parcelable
            }
        }
        int getLargeIconMarginEnd(@NonNull StandardTemplateParams p) {
            Resources res = mContext.getResources();
            if (Flags.uiRichOngoing() && mN.isPromotedOngoing() && !p.mHeaderless) {
                // Promoted notifications don't need space for the expand button
                if (notificationsRedesignTemplates()) {
                    return res.getDimensionPixelSize(R.dimen.notification_2025_margin);
                } else {
                    return res.getDimensionPixelSize(R.dimen.notification_content_margin);
                }
            }
            if (notificationsRedesignTemplates()) {
                return res.getDimensionPixelSize(
                        R.dimen.notification_2025_right_icon_expanded_margin_end);
            } else {
                return res.getDimensionPixelSize(R.dimen.notification_header_expand_icon_size);
            }
        }
        private void bindNotificationHeader(RemoteViews contentView, StandardTemplateParams p) {
            bindSmallIcon(contentView, p);
            // Populate text left-to-right so that separators are only shown between strings
@@ -14945,7 +14955,7 @@ public class Notification implements Parcelable
        /**
         * The margin end that needs to be added to the heading so that it won't overlap
         * with the large icon. This value includes the space required to accommodate the large
         * icon as well as the expander. This DOES include the 16dp content margin.
         * icon as well as the expander (when present). This DOES include the 16dp content margin.
         */
        public final MarginSet mHeadingFullMarginSet = new MarginSet();
@@ -14956,11 +14966,14 @@ public class Notification implements Parcelable
         */
        public final MarginSet mTitleMarginSet = new MarginSet();
        public void setRightIconState(boolean visible, float widthDp, float heightDp,
                float marginEndDpIfVisible, float spaceForExpanderDp, float notificationMarginDp) {
        public void setRightIconState(boolean visible, float widthDp, float heightDp) {
            mRightIconVisible = visible;
            mRightIconWidthDp = widthDp;
            mRightIconHeightDp = heightDp;
        }
        public void calculateMargins(float marginEndDpIfVisible, float spaceForExpanderDp,
                float notificationMarginDp) {
            mHeadingExtraMarginSet.setValues(
                    /* valueIfGone = */ 0,
                    /* valueIfVisible = */ marginEndDpIfVisible);