From f2c2834e3fc2b837357f5f90c293c4b173fe5d76 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 5 Aug 2023 10:21:26 +0200 Subject: [PATCH] fix: crash when download file doesn't exist --- .../com/github/libretube/services/OfflinePlayerService.kt | 7 +++++-- .../libretube/ui/activities/OfflinePlayerActivity.kt | 3 ++- .../com/github/libretube/ui/adapters/DownloadsAdapter.kt | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) 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 49d82885e..c82038b88 100644 --- a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt @@ -23,6 +23,7 @@ import com.github.libretube.util.NowPlayingNotification import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kotlin.io.path.exists /** * A service to play downloaded audio in the background @@ -85,9 +86,11 @@ class OfflinePlayerService : LifecycleService() { .build() .loadPlaybackParams(isBackgroundMode = true) - val audioItem = downloadWithItem.downloadItems.firstOrNull { it.type == FileType.AUDIO } + val audioItem = downloadWithItem.downloadItems.filter { it.path.exists() } + .firstOrNull { it.type == FileType.AUDIO } ?: // in some rare cases, video files can contain audio - downloadWithItem.downloadItems.firstOrNull { it.type == FileType.VIDEO } ?: return false + downloadWithItem.downloadItems.firstOrNull { it.type == FileType.VIDEO } + ?: return false val mediaItem = MediaItem.Builder() .setUri(audioItem.path.toAndroidUri()) diff --git a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt index e402fbed5..462827b78 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/OfflinePlayerActivity.kt @@ -40,6 +40,7 @@ import com.github.libretube.util.OfflineTimeFrameReceiver import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import kotlin.io.path.exists @androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class) class OfflinePlayerActivity : BaseActivity() { @@ -133,7 +134,7 @@ class OfflinePlayerActivity : BaseActivity() { val downloadInfo = withContext(Dispatchers.IO) { Database.downloadDao().findById(videoId) } - val downloadFiles = downloadInfo.downloadItems + val downloadFiles = downloadInfo.downloadItems.filter { it.path.exists() } playerBinding.exoTitle.text = downloadInfo.download.title playerBinding.exoTitle.isVisible = true diff --git a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt index b0ab208c4..122cf65d0 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt @@ -25,6 +25,7 @@ import kotlin.io.path.deleteIfExists import kotlin.io.path.fileSize import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking +import kotlin.io.path.exists class DownloadsAdapter( private val context: Context, @@ -50,7 +51,7 @@ class DownloadsAdapter( videoInfo.text = download.uploadDate?.let { TextUtils.localizeDate(it) } val downloadSize = items.sumOf { it.downloadSize } - val currentSize = items.sumOf { it.path.fileSize() } + val currentSize = items.filter { it.path.exists() }.sumOf { it.path.fileSize() } if (downloadSize == -1L) { progressBar.isIndeterminate = true