diff options
| author | 2020-10-26 15:52:12 -0400 | |
|---|---|---|
| committer | 2020-10-28 16:04:41 -0400 | |
| commit | 9863a144cf6000435744bc73877062d925321c26 (patch) | |
| tree | 703d37f7366508ecb6ad34bb8bda1d5261cb4701 | |
| parent | defc5d9b1d186280cf67a3f599256d67ed28f270 (diff) | |
Respect the gravity of the NotificationTopLineView.
Test: atest SystemUITests
Test: manually tested within my branch with new templates
Change-Id: I57a12287f1ce712914f985cc0de4df7613c18164
| -rw-r--r-- | core/java/android/view/NotificationTopLineView.java | 74 | ||||
| -rw-r--r-- | core/res/res/layout/notification_template_top_line.xml | 9 |
2 files changed, 73 insertions, 10 deletions
diff --git a/core/java/android/view/NotificationTopLineView.java b/core/java/android/view/NotificationTopLineView.java index 9443ccfc7553..69094b166fdb 100644 --- a/core/java/android/view/NotificationTopLineView.java +++ b/core/java/android/view/NotificationTopLineView.java @@ -19,6 +19,7 @@ package android.view; import android.annotation.Nullable; import android.content.Context; import android.content.res.Resources; +import android.content.res.TypedArray; import android.graphics.Rect; import android.util.AttributeSet; import android.widget.RemoteViews; @@ -36,6 +37,7 @@ import java.util.List; */ @RemoteViews.RemoteView public class NotificationTopLineView extends ViewGroup { + private final int mGravityY; private final int mChildMinWidth; private final int mContentEndMargin; private View mAppName; @@ -48,6 +50,9 @@ public class NotificationTopLineView extends ViewGroup { private int mHeaderTextMarginEnd; private List<View> mIconsAtEnd; + private int mMaxAscent; + private int mMaxDescent; + public NotificationTopLineView(Context context) { this(context, null); } @@ -67,6 +72,20 @@ public class NotificationTopLineView extends ViewGroup { Resources res = getResources(); mChildMinWidth = res.getDimensionPixelSize(R.dimen.notification_header_shrink_min_width); mContentEndMargin = res.getDimensionPixelSize(R.dimen.notification_content_margin_end); + + // NOTE: Implementation only supports TOP, BOTTOM, and CENTER_VERTICAL gravities, + // with CENTER_VERTICAL being the default. + int[] attrIds = {android.R.attr.gravity}; + TypedArray ta = context.obtainStyledAttributes(attrs, attrIds, defStyleAttr, defStyleRes); + int gravity = ta.getInt(0, 0); + ta.recycle(); + if ((gravity & Gravity.BOTTOM) == Gravity.BOTTOM) { + mGravityY = Gravity.BOTTOM; + } else if ((gravity & Gravity.TOP) == Gravity.TOP) { + mGravityY = Gravity.TOP; + } else { + mGravityY = Gravity.CENTER_VERTICAL; + } } @Override @@ -90,6 +109,8 @@ public class NotificationTopLineView extends ViewGroup { MeasureSpec.AT_MOST); int totalWidth = getPaddingStart(); int iconWidth = getPaddingEnd(); + mMaxAscent = -1; + mMaxDescent = -1; for (int i = 0; i < getChildCount(); i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { @@ -108,6 +129,11 @@ public class NotificationTopLineView extends ViewGroup { } else { totalWidth += lp.leftMargin + lp.rightMargin + child.getMeasuredWidth(); } + int childBaseline = child.getBaseline(); + if (childBaseline != -1) { + mMaxAscent = Math.max(mMaxAscent, childBaseline); + mMaxDescent = Math.max(mMaxDescent, child.getMeasuredHeight() - childBaseline); + } } // Ensure that there is at least enough space for the icons @@ -146,7 +172,13 @@ public class NotificationTopLineView extends ViewGroup { int left = getPaddingStart(); int end = getMeasuredWidth(); int childCount = getChildCount(); - int ownHeight = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); + int ownHeight = b - t; + int childSpace = ownHeight - mPaddingTop - mPaddingBottom; + + // Instead of centering the baseline, pick a baseline that centers views which align to it. + // Only used when mGravityY is CENTER_VERTICAL + int baselineY = mPaddingTop + ((childSpace - (mMaxAscent + mMaxDescent)) / 2) + mMaxAscent; + for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() == GONE) { @@ -156,8 +188,42 @@ public class NotificationTopLineView extends ViewGroup { MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams(); int layoutLeft; int layoutRight; - int top = (int) (getPaddingTop() + (ownHeight - childHeight) / 2.0f); - int bottom = top + childHeight; + + // Calculate vertical alignment of the views, accounting for the view baselines + int childTop; + int childBaseline = child.getBaseline(); + switch (mGravityY) { + case Gravity.TOP: + childTop = mPaddingTop + params.topMargin; + if (childBaseline != -1) { + childTop += mMaxAscent - childBaseline; + } + break; + case Gravity.CENTER_VERTICAL: + if (childBaseline != -1) { + // Align baselines vertically only if the child is smaller than us + if (childSpace - childHeight > 0) { + childTop = baselineY - childBaseline; + } else { + childTop = mPaddingTop + (childSpace - childHeight) / 2; + } + } else { + childTop = mPaddingTop + ((childSpace - childHeight) / 2) + + params.topMargin - params.bottomMargin; + } + break; + case Gravity.BOTTOM: + int childBottom = ownHeight - mPaddingBottom; + childTop = childBottom - childHeight - params.bottomMargin; + if (childBaseline != -1) { + int descent = childHeight - childBaseline; + childTop -= (mMaxDescent - descent); + } + break; + default: + childTop = mPaddingTop; + } + // Icons that should go at the end if (mIconsAtEnd.contains(child)) { if (end == getMeasuredWidth()) { @@ -179,7 +245,7 @@ public class NotificationTopLineView extends ViewGroup { layoutLeft = getWidth() - layoutRight; layoutRight = getWidth() - ltrLeft; } - child.layout(layoutLeft, top, layoutRight, bottom); + child.layout(layoutLeft, childTop, layoutRight, childTop + childHeight); } updateTouchListener(); } diff --git a/core/res/res/layout/notification_template_top_line.xml b/core/res/res/layout/notification_template_top_line.xml index 27fab859a045..0786e138559f 100644 --- a/core/res/res/layout/notification_template_top_line.xml +++ b/core/res/res/layout/notification_template_top_line.xml @@ -102,9 +102,8 @@ android:id="@+id/alerted_icon" android:layout_width="@dimen/notification_alerted_size" android:layout_height="@dimen/notification_alerted_size" - android:layout_gravity="center" android:layout_marginStart="4dp" - android:paddingTop="1dp" + android:baseline="10dp" android:scaleType="fitCenter" android:visibility="gone" android:contentDescription="@string/notification_alerted_content_description" @@ -116,8 +115,7 @@ android:layout_height="@dimen/notification_feedback_size" android:layout_marginStart="6dp" android:layout_marginEnd="6dp" - android:paddingTop="2dp" - android:layout_gravity="center" + android:baseline="10dp" android:scaleType="fitCenter" android:src="@drawable/ic_feedback_indicator" android:background="?android:selectableItemBackgroundBorderless" @@ -128,9 +126,8 @@ android:id="@+id/profile_badge" android:layout_width="@dimen/notification_badge_size" android:layout_height="@dimen/notification_badge_size" - android:layout_gravity="center" android:layout_marginStart="4dp" - android:paddingTop="1dp" + android:baseline="10dp" android:scaleType="fitCenter" android:visibility="gone" android:contentDescription="@string/notification_work_profile_content_description" |