mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-04-25 05:10:55 +03:00
Implement basic bitmap loader
This commit is contained in:
parent
4413a31213
commit
0ce65d36d1
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* ArtworkBitmapLoader.kt
|
||||
* Copyright (C) 2009-2022 Ultrasonic developers
|
||||
*
|
||||
* Distributed under terms of the GNU GPLv3 license.
|
||||
*/
|
||||
|
||||
package org.moire.ultrasonic.imageloader
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import androidx.media3.session.BitmapLoader
|
||||
import com.google.common.base.Suppliers
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
import com.google.common.util.concurrent.ListeningExecutorService
|
||||
import com.google.common.util.concurrent.MoreExecutors
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class ArtworkBitmapLoader : BitmapLoader {
|
||||
private val DEFAULT_EXECUTOR_SERVICE = Suppliers.memoize {
|
||||
MoreExecutors.listeningDecorator(
|
||||
Executors.newSingleThreadExecutor()
|
||||
)
|
||||
}
|
||||
|
||||
private val executorService: ListeningExecutorService by lazy {
|
||||
DEFAULT_EXECUTOR_SERVICE.get()
|
||||
}
|
||||
|
||||
override fun decodeBitmap(data: ByteArray): ListenableFuture<Bitmap> {
|
||||
return executorService.submit<Bitmap> {
|
||||
decode(
|
||||
data
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun loadBitmap(uri: Uri): ListenableFuture<Bitmap> {
|
||||
return executorService.submit<Bitmap> {
|
||||
load(uri)
|
||||
}
|
||||
}
|
||||
|
||||
private fun decode(data: ByteArray): Bitmap {
|
||||
val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size)
|
||||
return bitmap ?: throw IllegalArgumentException("Could not decode bitmap")
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun load(uri: Uri): Bitmap {
|
||||
return BitmapFactory.decodeFile(uri.path)
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@ class BitmapUtils {
|
||||
if (track == null) return null
|
||||
val albumArtFile = FileUtil.getAlbumArtFile(track)
|
||||
val bitmap: Bitmap? = null
|
||||
if (albumArtFile != null && File(albumArtFile).exists()) {
|
||||
if (File(albumArtFile).exists()) {
|
||||
return getBitmapFromDisk(albumArtFile, size, bitmap)
|
||||
}
|
||||
return null
|
||||
@ -50,35 +50,6 @@ class BitmapUtils {
|
||||
return null
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
fun getSampledBitmap(bytes: ByteArray, size: Int): Bitmap? {
|
||||
val opt = BitmapFactory.Options()
|
||||
if (size > 0) {
|
||||
// With this flag we only calculate the size first
|
||||
opt.inJustDecodeBounds = true
|
||||
|
||||
// Decode the size
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, opt)
|
||||
|
||||
// Now set the remaining flags
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
||||
opt.inDither = true
|
||||
opt.inPreferQualityOverSpeed = true
|
||||
}
|
||||
|
||||
opt.inSampleSize = Util.calculateInSampleSize(
|
||||
opt,
|
||||
size,
|
||||
Util.getScaledHeight(opt.outHeight.toDouble(), opt.outWidth.toDouble(), size)
|
||||
)
|
||||
|
||||
// Enable real decoding
|
||||
opt.inJustDecodeBounds = false
|
||||
}
|
||||
Timber.i("getSampledBitmap %s", size.toString())
|
||||
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size, opt)
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun getBitmapFromDisk(
|
||||
path: String,
|
||||
|
@ -20,11 +20,12 @@ import androidx.media3.session.SessionCommand
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.inject
|
||||
import org.moire.ultrasonic.R
|
||||
import org.moire.ultrasonic.imageloader.ArtworkBitmapLoader
|
||||
import org.moire.ultrasonic.service.MediaPlayerController
|
||||
|
||||
@UnstableApi
|
||||
class MediaNotificationProvider(context: Context) :
|
||||
DefaultMediaNotificationProvider(context), KoinComponent {
|
||||
DefaultMediaNotificationProvider(context, ArtworkBitmapLoader()), KoinComponent {
|
||||
|
||||
/*
|
||||
* It is currently not possible to edit a MediaItem after creation so the isRated value
|
||||
|
Loading…
x
Reference in New Issue
Block a user