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

Commit 2655d8b0 authored by Jiaming Cheng's avatar Jiaming Cheng
Browse files

[QSDetailedView] Revamp the cast list view

Add background to each list item

Bug: 378513588
Flag: com.android.systemui.qs_tile_detailed_view
Test: Only add the background. Existing unit tests still pass.
Change-Id: I62fc227d6a0c073ca3c3d89b877d403bd676a6b5
parent c728be27
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2259,6 +2259,7 @@
    <dimen name="tile_details_button_size">20dp</dimen>
    <dimen name="tile_details_entry_gap">2dp</dimen>
    <dimen name="tile_details_bottom_padding">14dp</dimen>
    <dimen name="tile_details_horizontal_padding">14dp</dimen>
    <!-- Tile Details end -->

</resources>
+70 −1
Original line number Diff line number Diff line
@@ -16,9 +16,12 @@

package com.android.systemui.qs.tiles.dialog

import android.content.Context
import android.database.DataSetObserver
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ListAdapter
import android.widget.ListView
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
@@ -38,6 +41,7 @@ import com.android.compose.ui.graphics.painter.rememberDrawablePainter
import com.android.internal.R
import com.android.internal.app.MediaRouteChooserContentManager
import com.android.internal.app.MediaRouteControllerContentManager
import com.android.systemui.res.R as SystemUiR

private val MAX_CAST_LIST_HEIGHT = 5000.dp

@@ -71,6 +75,9 @@ fun CastDetailsContent(castDetailsViewModel: CastDetailsViewModel) {

@Composable
fun CastChooserView(contentManager: MediaRouteChooserContentManager) {
    var dataObserver: DataSetObserver? = null
    var adapter: ListAdapter? = null

    AndroidView(
        // Use heightIn on this AndroidView to ensure it measures to a non-zero height that works
        // within the scrollable area in the `TileDetails`.
@@ -92,9 +99,25 @@ fun CastChooserView(contentManager: MediaRouteChooserContentManager) {
                listView.layoutParams = this
            }

            customizeView(listView)

            // Listen to the adapter data change and `customizeView` when changes occur.
            adapter = listView.adapter
            dataObserver =
                object : DataSetObserver() {
                    override fun onChanged() {
                        super.onChanged()
                        customizeView(listView)
                    }
                }
            adapter?.registerDataSetObserver(dataObserver)

            view
        },
        onRelease = { contentManager.onDetachedFromWindow() },
        onRelease = {
            contentManager.onDetachedFromWindow()
            adapter?.unregisterDataSetObserver(dataObserver)
        },
    )
}

@@ -125,3 +148,49 @@ fun CastControllerDisconnectButton(contentManager: MediaRouteControllerContentMa
        Text(text = "Disconnect")
    }
}

private fun customizeView(listView: ListView) {
    val context = listView.context
    val entryBackgroundStart =
        context.getDrawable(SystemUiR.drawable.settingslib_entry_bg_off_start)
    val entryBackgroundEnd = context.getDrawable(SystemUiR.drawable.settingslib_entry_bg_off_end)
    val entryBackgroundMiddle =
        context.getDrawable(SystemUiR.drawable.settingslib_entry_bg_off_middle)

    // This code will run after the ListView has had a chance to complete its layout.
    listView.post {
        val visibleChildCount = listView.childCount
        val adapter = listView.adapter
        val totalItemCount = adapter?.count ?: 0

        if (adapter == null || totalItemCount == 0) {
            return@post
        }

        for (i in 0 until visibleChildCount) {
            val child = listView.getChildAt(i) as LinearLayout
            val adapterPosition = listView.getPositionForView(child)
            if (adapterPosition != ListView.INVALID_POSITION) {
                val entry = child.getChildAt(0) as LinearLayout
                entry.background =
                    when (adapterPosition) {
                        0 -> entryBackgroundStart
                        totalItemCount - 1 -> entryBackgroundEnd
                        else -> entryBackgroundMiddle
                    }
                setPadding(context, child)
            }
        }
    }
}

private fun setPadding(context: Context, targetBackgroundView: LinearLayout) {
    val horizontalPadding =
        context.resources.getDimensionPixelSize(SystemUiR.dimen.tile_details_horizontal_padding)
    targetBackgroundView.setPadding(
        horizontalPadding,
        targetBackgroundView.paddingTop,
        horizontalPadding,
        targetBackgroundView.paddingBottom,
    )
}