From c56986a9ba6c067677b6d61ca345ebcdb9447889 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 18 Nov 2024 16:22:50 +0100 Subject: [PATCH] fix: crash when starting audio player --- .../github/libretube/helpers/ImageHelper.kt | 27 ++++++++++--------- .../services/OfflinePlayerService.kt | 2 ++ .../ui/fragments/AudioPlayerFragment.kt | 10 ++++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt b/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt index 55b380f6c..4fc9d5e8c 100644 --- a/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/ImageHelper.kt @@ -3,9 +3,11 @@ package com.github.libretube.helpers import android.content.Context import android.graphics.Bitmap import android.graphics.Color +import android.graphics.drawable.Drawable +import android.net.Uri import android.widget.ImageView -import androidx.core.graphics.drawable.toBitmap import androidx.core.graphics.drawable.toBitmapOrNull +import androidx.core.net.toUri import coil.ImageLoader import coil.disk.DiskCache import coil.request.CachePolicy @@ -87,17 +89,12 @@ object ImageHelper { if (DataSaverMode.isEnabled(target.context) || url.isNullOrEmpty()) return val urlToLoad = ProxyHelper.unwrapImageUrl(url) - val request = ImageRequest.Builder(target.context) - .data(urlToLoad) - .listener { _, result -> - // set the background to white for transparent images - if (whiteBackground) target.setBackgroundColor(Color.WHITE) + getImageWithCallback(target.context, urlToLoad) { result -> + // set the background to white for transparent images + if (whiteBackground) target.setBackgroundColor(Color.WHITE) - target.setImageDrawable(result.drawable) - } - .build() - - imageLoader.enqueue(request) + target.setImageDrawable(result) + } } suspend fun downloadImage(context: Context, url: String, path: Path) { @@ -110,6 +107,10 @@ object ImageHelper { } suspend fun getImage(context: Context, url: String?): Bitmap? { + return getImage(context, url?.toUri()) + } + + suspend fun getImage(context: Context, url: Uri?): Bitmap? { val request = ImageRequest.Builder(context) .data(url) .build() @@ -117,11 +118,11 @@ object ImageHelper { return imageLoader.execute(request).drawable?.toBitmapOrNull() } - fun getImageWithCallback(context: Context, url: String?, onBitmap: (Bitmap) -> Unit) { + private fun getImageWithCallback(context: Context, url: String?, onSuccess: (Drawable) -> Unit) { val request = ImageRequest.Builder(context) .data(url) .target { drawable -> - onBitmap(drawable.toBitmap()) + onSuccess(drawable) } .build() diff --git a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt index ed7eefde2..2ac743b16 100644 --- a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt @@ -53,6 +53,8 @@ open class OfflinePlayerService : AbstractPlayerService() { } override suspend fun onServiceCreated(args: Bundle) { + if (args.isEmpty) return + downloadTab = args.serializable(IntentData.downloadTab)!! shuffle = args.getBoolean(IntentData.shuffle, false) noInternetService = args.getBoolean(IntentData.noInternet, false) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt index e21ce07e9..b1eb9265c 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt @@ -2,10 +2,12 @@ package com.github.libretube.ui.fragments import android.annotation.SuppressLint import android.graphics.Color +import android.net.Uri import android.os.Bundle import android.os.Handler import android.os.Looper import android.text.format.DateUtils +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -297,13 +299,13 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { NavigationHelper.navigateChannel(requireContext(), uploaderId) } - metadata.artworkUri?.let { updateThumbnailAsync(it.toString()) } + metadata.artworkUri?.let { updateThumbnailAsync(it) } initializeSeekBar() } - private fun updateThumbnailAsync(thumbnailUrl: String) { - if (DataSaverMode.isEnabled(requireContext())) { + private fun updateThumbnailAsync(thumbnailUri: Uri) { + if (DataSaverMode.isEnabled(requireContext()) && !isOffline) { binding.progress.isVisible = false binding.thumbnail.setImageResource(R.drawable.ic_launcher_monochrome) val primaryColor = ThemeHelper.getThemeColor( @@ -321,7 +323,7 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { lifecycleScope.launch { val binding = _binding ?: return@launch - val bitmap = ImageHelper.getImage(requireContext(), thumbnailUrl) + val bitmap = ImageHelper.getImage(requireContext(), thumbnailUri) binding.thumbnail.setImageBitmap(bitmap) binding.miniPlayerThumbnail.setImageBitmap(bitmap) binding.thumbnail.isVisible = true