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.
This commit is contained in:
Michatec committed 2026-11-15 17:34:40 +01:00
1 parent c3d351f646
commit 0e736db75c
3 files changed
+34 -22

No files matched your search

@@ -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
@@ -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
)
}
}
}
@@ -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