From c54ac1a4c0f9d5a20cdf16c0f484d82a2e1e7bdd Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Fri, 27 Sep 2024 19:43:05 +0800 Subject: [PATCH] Fix crash when download location is invalid --- .../kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt | 5 +---- app/src/main/kotlin/com/hippo/ehviewer/util/PathUtils.kt | 4 +++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt index ecd14b35c..48bdd5442 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/DownloadScreen.kt @@ -64,7 +64,6 @@ import com.ramcosta.composedestinations.annotation.Destination import com.ramcosta.composedestinations.annotation.RootGraph import com.ramcosta.composedestinations.navigation.DestinationsNavigator import eu.kanade.tachiyomi.util.lang.launchIO -import eu.kanade.tachiyomi.util.lang.launchNonCancellable import eu.kanade.tachiyomi.util.system.logcat import kotlinx.coroutines.launch import moe.tarsin.coroutines.runSuspendCatching @@ -99,10 +98,8 @@ fun AnimatedVisibilityScope.DownloadScreen(navigator: DestinationsNavigator) = c contentResolver.takePersistableUriPermission(treeUri, FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION) val path = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)).toOkioPath() check(path.isDirectory) { "$path is not a directory" } + keepNoMediaFileStatus(path) // Check if the directory is writable downloadLocationState = path - launchNonCancellable { - keepNoMediaFileStatus(downloadLocationState) - } }.onFailure { logcat(it) launchSnackBar(cannotGetDownloadLocation) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/util/PathUtils.kt b/app/src/main/kotlin/com/hippo/ehviewer/util/PathUtils.kt index 370a86e73..73ad48470 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/util/PathUtils.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/util/PathUtils.kt @@ -16,7 +16,9 @@ val Uri.displayPath: String? val context = appCtx if (DocumentsContract.isDocumentUri(context, this)) { - val (type, path) = DocumentsContract.getDocumentId(this).split(":", limit = 2) + val (type, path) = DocumentsContract.getDocumentId(this).split(":", limit = 2).also { + if (it.size < 2) return toString() + } if (authority == "com.android.externalstorage.documents") { if (type == "primary") { return Environment.getExternalStorageDirectory().path + "/" + path