Tom Wayne
Tom Wayne

Reputation: 53

Change Style of Single Item in MediaBrowserService on Android Automotive

I am trying to achive something similar like the Spotify app on android automotive to display list and grid items on the same screen. (See screenshot attached)

Spotify List/Gridview Example

So from what I found on Google Documentation is that we can change the style of of a single item by adding the following to the extras of a specific media item:

extras.putInt(
        MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_SINGLE_ITEM,
        MediaConstants.DESCRIPTION_EXTRAS_VALUE_CONTENT_STYLE_CATEGORY_LIST_ITEM) 

But I cannot get it to work. I created a plain project with the automotive mediaservice sample and create the children like following:

private val ROOT_BROWSEABLE_ID = "Tab1ID"
    override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaItem>>) {

        when (parentId) {
            "root" -> result.sendResult(mutableListOf(generateRootBrowsable()))
            ROOT_BROWSEABLE_ID -> result.sendResult(generateChildren())
            else -> result.sendResult(ArrayList())
        }
    }

    private fun generateRootBrowsable(): MediaItem {
        return MediaItem(
            MediaDescriptionCompat.Builder()
                .setMediaId(ROOT_BROWSEABLE_ID)
                .setTitle("Tab 1")
                .build(),
            FLAG_BROWSABLE
        )
    }

    private fun generateChildren(): MutableList<MediaItem> {
        return mutableListOf(
            generateChild("ListItem 1", false),
            generateChild("ListItem 2", false),
            generateChild("ListItem 3", false),
            generateChild("GridItem 1", true),
            generateChild("GridItem 2", true),
            generateChild("GridItem 3", true),
        )
    }

    private fun generateChild(name: String, isGrid: Boolean): MediaItem {
        return MediaItem(
            MediaDescriptionCompat.Builder()
                .setMediaId(name)
                .setTitle(name)
                .setExtras(Bundle().apply {
                    putString(
                        MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
                        if (isGrid) "GridItem" else "ListItem"
                    )
                    if (isGrid) {
                        putInt(
                            MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_SINGLE_ITEM,
                            MediaConstants.DESCRIPTION_EXTRAS_VALUE_CONTENT_STYLE_GRID_ITEM
                        )
                    }
                })
                .build(),
            FLAG_PLAYABLE
        )
    }

Result: Not working Sample

Thanks for your help!

Upvotes: 1

Views: 190

Answers (0)

Related Questions