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

Commit 895cc9bb authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

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
parent 738c9823
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -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">

            <LinearLayout
                android:id = "@+id/alarm_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">
@@ -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">
+32 −24
Original line number Diff line number Diff line
@@ -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,31 +54,21 @@ 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) {
@@ -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