From 6c3b6ba21dc84d320f5a3c9934ebfd6a315bb2a2 Mon Sep 17 00:00:00 2001 From: Michatec Date: Sun, 15 Nov 2026 15:37:29 +0100 Subject: [PATCH] fix: use runBlocking(Main) for thread dispatching in PlayerService binder methods - Replace custom CountDownLatch and Handler synchronization with `runBlocking(Main)` in PlayerService binder methods (`isPlaying`, `getCurrentStationName`, `getCurrentMetadata`). - Add intent query for `IRadioApi` to package visibility configuration in AIDL documentation. --- .../java/com/michatec/radio/PlayerService.kt | 54 +++++-------------- docs/AIDL_API.md | 3 ++ 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/michatec/radio/PlayerService.kt b/app/src/main/java/com/michatec/radio/PlayerService.kt index 83ae490..3681252 100644 --- a/app/src/main/java/com/michatec/radio/PlayerService.kt +++ b/app/src/main/java/com/michatec/radio/PlayerService.kt @@ -67,6 +67,7 @@ import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking import java.util.Date @@ -216,21 +217,12 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc if (!::player.isInitialized) { return PreferencesHelper.loadPlayerState().isPlaying } - return if (Looper.myLooper() == Looper.getMainLooper()) { - player.isPlaying - } else { - val latch = java.util.concurrent.CountDownLatch(1) - var result = false - mainHandler.post { - result = if (::player.isInitialized) player.isPlaying else PreferencesHelper.loadPlayerState().isPlaying - latch.countDown() + if (Looper.myLooper() != Looper.getMainLooper()) { + return runBlocking(Main) { + if (::player.isInitialized) player.isPlaying else PreferencesHelper.loadPlayerState().isPlaying } - try { - latch.await(100, java.util.concurrent.TimeUnit.MILLISECONDS) - } catch (_: Exception) { - } - result } + return player.isPlaying } override fun getCurrentStationName(): String { @@ -242,15 +234,10 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc "" } } - return if (Looper.myLooper() == Looper.getMainLooper()) { - val mediaId = player.currentMediaItem?.mediaId ?: return "" - CollectionHelper.getItem(this@PlayerService, collection, mediaId).mediaMetadata.title?.toString() ?: "" - } else { - val latch = java.util.concurrent.CountDownLatch(1) - var result = "" - mainHandler.post { + if (Looper.myLooper() != Looper.getMainLooper()) { + return runBlocking(Main) { val mediaId = if (::player.isInitialized) player.currentMediaItem?.mediaId else null - result = if (mediaId != null) { + if (mediaId != null) { CollectionHelper.getItem(this@PlayerService, collection, mediaId).mediaMetadata.title?.toString() ?: "" } else { try { @@ -260,33 +247,20 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc "" } } - latch.countDown() } - try { - latch.await(100, java.util.concurrent.TimeUnit.MILLISECONDS) - } catch (_: Exception) { - } - result } + val mediaId = player.currentMediaItem?.mediaId ?: return "" + return CollectionHelper.getItem(this@PlayerService, collection, mediaId).mediaMetadata.title?.toString() ?: "" } override fun getCurrentMetadata(): String { if (!::player.isInitialized) return "" - return if (Looper.myLooper() == Looper.getMainLooper()) { - player.currentMediaItem?.mediaMetadata?.title?.toString() ?: "" - } else { - val latch = java.util.concurrent.CountDownLatch(1) - var result = "" - mainHandler.post { - result = if (::player.isInitialized) player.currentMediaItem?.mediaMetadata?.title?.toString() ?: "" else "" - latch.countDown() + if (Looper.myLooper() != Looper.getMainLooper()) { + return runBlocking(Main) { + if (::player.isInitialized) player.currentMediaItem?.mediaMetadata?.title?.toString() ?: "" else "" } - try { - latch.await(100, java.util.concurrent.TimeUnit.MILLISECONDS) - } catch (_: Exception) { - } - result } + return player.currentMediaItem?.mediaMetadata?.title?.toString() ?: "" } override fun playStationByUuid(uuid: String) { diff --git a/docs/AIDL_API.md b/docs/AIDL_API.md index 7d38784..f031cdc 100644 --- a/docs/AIDL_API.md +++ b/docs/AIDL_API.md @@ -67,6 +67,9 @@ In your client application's `src/main/AndroidManifest.xml`, declare the custom + + +