0

I'm working on Android Auto support for my audio app. I'm trying to get the parentId from onLoadChildren() for the browsable media item that is clicked. When Android Auto starts, onLoadChildren() has already been called for the first two of my media items to create the mini UI of the app in the split screen. When I click my app icon to start the app, onLoadChildren() is not triggered for the first two media items when they are clicked since their children have already been loaded. But the function is triggered for the rest of the media items when they are clicked. So, I'm not able to save the parentId of the first two items.

override fun onLoadChildren(parentId: String, result: Result<List<MediaBrowserCompat.MediaItem>>) {
    result.detach()
    if (parentId == EMPTY_MEDIA_ROOT_ID)
        result.sendResult(null)
    else {
        CarConnection(this).type.observeForever(::isConnectedToAndroidAuto)
        CoroutineScope(Dispatchers.IO).launch {
            val children = browseTree?.get(parentId)?.map { item ->
                val mediaDescriptionCompatBuilder = MediaDescriptionCompat.Builder().apply {
                    setMediaId(item.id)
                    setTitle(item.title)
                    setSubtitle(item.subtitle)
                    setIconUri(item.programArtUri)
                    val extras = Bundle().also {
                        if (parentId == HOME_MEDIA_ROOT_ID) {
                            if (item.id == "default" || item.id == "default-original") {
                                it.putString(
                                    MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
                                    "Live Stream"
                                )
                            } else
                                it.putString(
                                    MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
                                    "Favorite Programs"
                                )
                        } else if (parentId == PROGRAMS_MEDIA_ROOT_ID) {
                            if (firstGroupPrograms!!.any { program ->
                                        item.id == program.id
                                    }) {
                                it.putString(
                                    MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
                                    groups!![0].name
                                )
                                it.putString(GlobalConstants.ANDROID_AUTO_PROGRAM_ID, item.id)
                            } else if (secondGroupPrograms != null && secondGroupPrograms!!.any { program ->
                                        item.id == program.id
                                    }) {
                                it.putString(
                                    MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
                                    groups!![1].name
                                )
                                it.putString(GlobalConstants.ANDROID_AUTO_PROGRAM_ID, item.id)
                            } else if (thirdGroupPrograms != null && thirdGroupPrograms!!.any { program ->
                                        item.id == program.id
                                    }) {
                                it.putString(
                                    MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
                                    groups!![2].name
                                )
                                it.putString(GlobalConstants.ANDROID_AUTO_PROGRAM_ID, item.id)
                            } else if (forthGroupPrograms != null && forthGroupPrograms!!.any { program ->
                                        item.id == program.id
                                    }) {
                                it.putString(
                                    MediaConstants.DESCRIPTION_EXTRAS_KEY_CONTENT_STYLE_GROUP_TITLE,
                                    groups!![3].name
                                )
                                it.putString(GlobalConstants.ANDROID_AUTO_PROGRAM_ID, item.id)
                            }
                        }
                    }
                    setExtras(extras)
                }
                MediaBrowserCompat.MediaItem(mediaDescriptionCompatBuilder.build(), item.flag)
            }
            if (children != null)
                result.sendResult(children)
            else
                result.sendResult(emptyList())
            clickedMediaItemId = parentId
        }
    }

Any idea how I can get the parentId, the id of the clicked browsable media item, correctly?

0 Answers0