service: try to fix android auto bugs

This commit is contained in:
Alexander Capehart
2026-04-05 13:36:38 -06:00
parent 381deaa001
commit 807f3e6869
3 changed files with 12 additions and 33 deletions
@@ -23,7 +23,6 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.IBinder
import android.support.v4.media.MediaBrowserCompat
import android.support.v4.media.MediaBrowserCompat.MediaItem
import androidx.annotation.StringRes
import androidx.core.app.NotificationChannelCompat
@@ -105,7 +104,7 @@ class AuxioService :
override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaItem>>) {
val maximumRootChildLimit = getRootChildrenLimit()
musicFragment.getChildren(parentId, maximumRootChildLimit, result, null)
musicFragment.getChildren(parentId, maximumRootChildLimit, result)
}
override fun onLoadChildren(
@@ -114,11 +113,11 @@ class AuxioService :
options: Bundle,
) {
val maximumRootChildLimit = getRootChildrenLimit()
musicFragment.getChildren(parentId, maximumRootChildLimit, result, options.getPage())
musicFragment.getChildren(parentId, maximumRootChildLimit, result)
}
override fun onSearch(query: String, extras: Bundle?, result: Result<MutableList<MediaItem>>) {
musicFragment.search(query, result, extras?.getPage())
musicFragment.search(query, result)
}
private fun getRootChildrenLimit(): Int {
@@ -128,13 +127,6 @@ class AuxioService :
) ?: 4
}
private fun Bundle.getPage(): MusicServiceFragment.Page? {
val page = getInt(MediaBrowserCompat.EXTRA_PAGE, -1).takeIf { it >= 0 } ?: return null
val pageSize =
getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1).takeIf { it > 0 } ?: return null
return MusicServiceFragment.Page(page, pageSize)
}
override fun updateForeground(change: ForegroundListener.Change) {
val mediaNotification = playbackFragment.notification
if (mediaNotification != null) {
@@ -131,9 +131,10 @@ private constructor(
}
fun getChildren(parentId: String, maxTabs: Int): List<MediaItem>? {
if (musicRepository.library == null) {
return listOf()
}
// we do not gate by library here since either
// - we are loading tabs, which we want to always load in since some head units dont
// cope well with sending no tabs then trying to update with tabs
// - the downstream code already handles no-library cases
return getMediaItemList(parentId, maxTabs)
}
@@ -202,6 +203,7 @@ private constructor(
homeGenerator.tabs().drop(maxTabs - 1).map { TabNode.Home(it).toMediaItem(context) }
}
is TabNode.Home ->
// homeGenerator returns emptyLists
when (node.type) {
MusicType.SONGS -> homeGenerator.songs().map { it.toMediaItem(context) }
MusicType.ALBUMS -> homeGenerator.albums().map { it.toMediaItem(context) }
@@ -107,15 +107,11 @@ constructor(
fun getItem(mediaId: String, result: Result<MediaItem>) =
result.dispatch { musicBrowser.getItem(mediaId) }
fun getChildren(
mediaId: String,
maxTabs: Int,
result: Result<MutableList<MediaItem>>,
page: Page?,
) = result.dispatch { musicBrowser.getChildren(mediaId, maxTabs)?.expose(page) }
fun getChildren(mediaId: String, maxTabs: Int, result: Result<MutableList<MediaItem>>) =
result.dispatch { musicBrowser.getChildren(mediaId, maxTabs)?.toMutableList() }
fun search(query: String, result: Result<MutableList<MediaItem>>, page: Page?) =
result.dispatchAsync { musicBrowser.search(query).expose(page) }
fun search(query: String, result: Result<MutableList<MediaItem>>) =
result.dispatchAsync { musicBrowser.search(query) }
private fun <T> Result<T>.dispatch(body: () -> T?) {
try {
@@ -145,15 +141,4 @@ constructor(
}
}
}
private fun <T> List<T>.expose(page: Page?): MutableList<T> {
if (page == null) return toMutableList()
val start = page.num * page.size
val end = start + page.size
return if (start >= size) {
mutableListOf()
} else {
subList(start, end.coerceAtMost(size)).toMutableList()
}
}
}