From 0e736db75cf0e2ff295c7c14971f97ce8e22124c Mon Sep 17 00:00:00 2001 From: Michatec Date: Sun, 15 Nov 2026 17:34:40 +0100 Subject: [PATCH] feat: optimize station image retrieval for IPC - Update `ImageHelper.getStationImageAsByteArray` to support downsampling and JPEG compression for IPC transmission. - Add `SIZE_STATION_IMAGE_API` constant for consistent image scaling. - Refactor `PlayerService` to use downsampled images when fetching via `Binder` API. - Improve thread safety in `PlayerService` station image lookup by consolidating I/O operations and avoiding main-thread access when possible. --- app/src/main/java/com/michatec/radio/Keys.kt | 1 + .../java/com/michatec/radio/PlayerService.kt | 33 +++++++++---------- .../com/michatec/radio/helpers/ImageHelper.kt | 22 ++++++++++--- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/michatec/radio/Keys.kt b/app/src/main/java/com/michatec/radio/Keys.kt index 9639cd1..7c7ea5f 100644 --- a/app/src/main/java/com/michatec/radio/Keys.kt +++ b/app/src/main/java/com/michatec/radio/Keys.kt @@ -183,6 +183,7 @@ object Keys { // sizes (in dp) const val SIZE_STATION_IMAGE_CARD: Int = 72 const val SIZE_STATION_IMAGE_MAXIMUM: Int = 640 + const val SIZE_STATION_IMAGE_API: Int = 512 const val BOTTOM_SHEET_PEEK_HEIGHT: Int = 72 // default values diff --git a/app/src/main/java/com/michatec/radio/PlayerService.kt b/app/src/main/java/com/michatec/radio/PlayerService.kt index 23f230f..adc0cdf 100644 --- a/app/src/main/java/com/michatec/radio/PlayerService.kt +++ b/app/src/main/java/com/michatec/radio/PlayerService.kt @@ -280,33 +280,30 @@ class PlayerService : MediaLibraryService(), SharedPreferences.OnSharedPreferenc } override fun getCurrentStationImage(): ByteArray { - val currentUuid = if (::player.isInitialized) { - player.currentMediaItem?.mediaId ?: PreferencesHelper.loadPlayerState().stationUuid - } else { - PreferencesHelper.loadPlayerState().stationUuid + val currentUuid = when { + !::player.isInitialized -> PreferencesHelper.loadPlayerState().stationUuid + Looper.myLooper() == Looper.getMainLooper() -> player.currentMediaItem?.mediaId + ?: PreferencesHelper.loadPlayerState().stationUuid + else -> runBlocking(Main) { + player.currentMediaItem?.mediaId + ?: PreferencesHelper.loadPlayerState().stationUuid + } } return getStationImageByUuid(currentUuid) } override fun getStationImageByUuid(uuid: String): ByteArray { if (uuid.isEmpty()) return byteArrayOf() - if (Looper.myLooper() == Looper.getMainLooper()) { - val imageFile = FileHelper.getStationImageFile(this@PlayerService, uuid) - if (imageFile.exists()) { - return ImageHelper.getStationImageAsByteArray(this@PlayerService, Uri.fromFile(imageFile).toString()) - } - return byteArrayOf() - } val imageUri = runBlocking(Dispatchers.IO) { val imageFile = FileHelper.getStationImageFile(this@PlayerService, uuid) - if (imageFile.exists()) { - Uri.fromFile(imageFile).toString() - } else { - val station = collection.stations.find { it.uuid == uuid } - station?.remoteImageLocation ?: "" - } + if (imageFile.exists()) Uri.fromFile(imageFile).toString() else "" } - return ImageHelper.getStationImageAsByteArray(this@PlayerService, imageUri) + if (imageUri.isEmpty()) return byteArrayOf() + return ImageHelper.getStationImageAsByteArray( + this@PlayerService, + imageUri, + Keys.SIZE_STATION_IMAGE_API + ) } } } diff --git a/app/src/main/java/com/michatec/radio/helpers/ImageHelper.kt b/app/src/main/java/com/michatec/radio/helpers/ImageHelper.kt index d829e4a..e3f469a 100644 --- a/app/src/main/java/com/michatec/radio/helpers/ImageHelper.kt +++ b/app/src/main/java/com/michatec/radio/helpers/ImageHelper.kt @@ -55,11 +55,25 @@ object ImageHelper { } - /* Get an unscaled version of the station image as a ByteArray */ - fun getStationImageAsByteArray(context: Context, imageUriString: String = String()): ByteArray { - val coverBitmap: Bitmap = getStationImage(context, imageUriString) + /* Get the station image as a ByteArray, optionally scaled down to maxSize pixels */ + fun getStationImageAsByteArray( + context: Context, + imageUriString: String = String(), + maxSize: Int = 0 + ): ByteArray { + val coverBitmap: Bitmap = if (maxSize > 0 && imageUriString.isNotEmpty()) { + decodeSampledBitmapFromUri(context, imageUriString.toUri(), maxSize, maxSize) + } else { + getStationImage(context, imageUriString) + } + val stream = ByteArrayOutputStream() - coverBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream) + if (maxSize > 0) { + coverBitmap.compress(Bitmap.CompressFormat.JPEG, 85, stream) + } else { + coverBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream) + } + val coverByteArray: ByteArray = stream.toByteArray() coverBitmap.recycle() return coverByteArray