From 895cc9bb755d2832e22eb0c3e96f921ff822f4d2 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Tue, 21 May 2019 10:06:44 -0400 Subject: Fix RTL in QS Header Info When there was only one view to display, we relay on FrameLayout#onLayout. Setting the layout_gravity to center_vertical (which is unneeded here) was removing the "start" by default. Lays out Views from start to end keeping track of offset. As the Views were given their final size in onMeasure, laying out is easy. Fixes: 133221093 Test: manual using Hebrew and English Change-Id: Id57d951bbbacbff7745869fe5b288d620a3c25f1 --- .../res/layout/quick_settings_header_info.xml | 5 +- .../com/android/systemui/qs/QSHeaderInfoLayout.kt | 56 ++++++++++++---------- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/packages/SystemUI/res/layout/quick_settings_header_info.xml b/packages/SystemUI/res/layout/quick_settings_header_info.xml index 3582d391271c..1e16e5d12b83 100644 --- a/packages/SystemUI/res/layout/quick_settings_header_info.xml +++ b/packages/SystemUI/res/layout/quick_settings_header_info.xml @@ -27,14 +27,12 @@ android:id="@+id/status_container" android:layout_width="match_parent" android:layout_weight="1" - android:layout_height="match_parent" - android:gravity="start" > + android:layout_height="match_parent"> @@ -69,7 +67,6 @@ android:id = "@+id/ringer_container" android:layout_width="wrap_content" android:layout_height="match_parent" - android:layout_gravity="center_vertical" android:gravity="center_vertical" android:focusable="true" android:clickable="true"> diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSHeaderInfoLayout.kt b/packages/SystemUI/src/com/android/systemui/qs/QSHeaderInfoLayout.kt index 86f54a97043f..c65462193758 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QSHeaderInfoLayout.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/QSHeaderInfoLayout.kt @@ -41,6 +41,7 @@ class QSHeaderInfoLayout @JvmOverloads constructor( private lateinit var alarmContainer: View private lateinit var ringerContainer: View private lateinit var statusSeparator: View + private val location = Location(0, 0) override fun onFinishInflate() { super.onFinishInflate() @@ -53,33 +54,23 @@ class QSHeaderInfoLayout @JvmOverloads constructor( // At most one view is there if (statusSeparator.visibility == View.GONE) super.onLayout(changed, l, t, r, b) else { - val alarmWidth = alarmContainer.measuredWidth - val separatorWidth = statusSeparator.measuredWidth - val ringerWidth = ringerContainer.measuredWidth - val availableSpace = (r - l) - separatorWidth - var left = l - if (alarmWidth < availableSpace / 2) { - alarmContainer.layout(left, t, left + alarmWidth, b) - left += alarmWidth - statusSeparator.layout(left, t, left + separatorWidth, b) - left += separatorWidth - ringerContainer.layout(left, t, left + Math.min(ringerWidth, r - left), b) - } else if (ringerWidth < availableSpace / 2) { - val alarmAllocation = Math.min(availableSpace - ringerWidth, alarmWidth) - alarmContainer.layout(left, t, left + alarmAllocation, b) - left += alarmWidth - statusSeparator.layout(left, t, left + separatorWidth, b) - left += separatorWidth - ringerContainer.layout(left, t, left + ringerWidth, b) - } else { - alarmContainer.layout(left, t, left + availableSpace / 2, b) - left += availableSpace / 2 - statusSeparator.layout(left, t, left + separatorWidth, b) - ringerContainer.layout(r - availableSpace / 2, t, r, b) - } + val layoutRTL = isLayoutRtl + val width = r - l + val height = b - t + var offset = 0 + + offset += alarmContainer.layoutView(width, height, offset, layoutRTL) + offset += statusSeparator.layoutView(width, height, offset, layoutRTL) + ringerContainer.layoutView(width, height, offset, layoutRTL) } } + private fun View.layoutView(pWidth: Int, pHeight: Int, offset: Int, RTL: Boolean): Int { + location.setLocationFromOffset(pWidth, offset, this.measuredWidth, RTL) + layout(location.left, 0, location.right, pHeight) + return this.measuredWidth + } + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure( MeasureSpec.makeMeasureSpec( @@ -120,4 +111,21 @@ class QSHeaderInfoLayout @JvmOverloads constructor( } setMeasuredDimension(width, measuredHeight) } + + private data class Location(var left: Int, var right: Int) { + /** + * Sets the [left] and [right] with the correct values for laying out the child, respecting + * RTL. Only set the variable through here to prevent concurrency issues. + * This is done to prevent allocation of [Pair] in [onLayout]. + */ + fun setLocationFromOffset(parentWidth: Int, offset: Int, width: Int, RTL: Boolean) { + if (RTL) { + left = parentWidth - offset - width + right = parentWidth - offset + } else { + left = offset + right = offset + width + } + } + } } \ No newline at end of file -- cgit v1.2.3-59-g8ed1b