From 0efbf8303a39fd06de8a48f8d26a03a776d5e744 Mon Sep 17 00:00:00 2001 From: Hai Zhang Date: Thu, 10 Aug 2023 04:08:53 -0700 Subject: [PATCH] [Refactor] Unify concept of local and remote path. --- .../me/zhanghai/android/files/coil/CoilUtils.kt | 13 ++----------- .../android/files/coil/PathAttributesFetcher.kt | 15 ++++++++------- .../android/files/filelist/FileItemExtensions.kt | 13 ++++++------- .../android/files/filelist/PathExtensions.kt | 9 +++++++++ 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/me/zhanghai/android/files/coil/CoilUtils.kt b/app/src/main/java/me/zhanghai/android/files/coil/CoilUtils.kt index 5e1da34de..4617f40e3 100644 --- a/app/src/main/java/me/zhanghai/android/files/coil/CoilUtils.kt +++ b/app/src/main/java/me/zhanghai/android/files/coil/CoilUtils.kt @@ -14,11 +14,7 @@ import coil.size.Size import coil.size.isOriginal import coil.size.pxOrElse import java8.nio.file.Path -import me.zhanghai.android.files.provider.archive.archiveFile -import me.zhanghai.android.files.provider.archive.isArchivePath -import me.zhanghai.android.files.provider.ftp.isFtpPath -import me.zhanghai.android.files.provider.sftp.isSftpPath -import me.zhanghai.android.files.provider.smb.isSmbPath +import me.zhanghai.android.files.filelist.isRemotePath val Bitmap.Config.isHardware: Boolean get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && this == Bitmap.Config.HARDWARE @@ -26,12 +22,7 @@ val Bitmap.Config.isHardware: Boolean fun Bitmap.Config.toSoftware(): Bitmap.Config = if (isHardware) Bitmap.Config.ARGB_8888 else this val Path.dataSource: DataSource - get() = - when { - isArchivePath -> archiveFile.dataSource - isFtpPath || isSftpPath || isSmbPath -> DataSource.NETWORK - else -> DataSource.DISK - } + get() = if (isRemotePath) DataSource.NETWORK else DataSource.DISK inline fun Size.widthPx(scale: Scale, original: () -> Int): Int = if (isOriginal) original() else width.toPx(scale) diff --git a/app/src/main/java/me/zhanghai/android/files/coil/PathAttributesFetcher.kt b/app/src/main/java/me/zhanghai/android/files/coil/PathAttributesFetcher.kt index 627abe8db..65f9c6a13 100644 --- a/app/src/main/java/me/zhanghai/android/files/coil/PathAttributesFetcher.kt +++ b/app/src/main/java/me/zhanghai/android/files/coil/PathAttributesFetcher.kt @@ -31,6 +31,7 @@ import me.zhanghai.android.files.file.isMedia import me.zhanghai.android.files.file.isPdf import me.zhanghai.android.files.file.isVideo import me.zhanghai.android.files.file.lastModifiedInstant +import me.zhanghai.android.files.filelist.isRemotePath import me.zhanghai.android.files.provider.common.AndroidFileTypeDetector import me.zhanghai.android.files.provider.common.newInputStream import me.zhanghai.android.files.provider.content.resolver.ResolverException @@ -93,13 +94,13 @@ class PathAttributesFetcher( ) } } - val isLocalPath = path.isLinuxPath - || (path.isDocumentPath && DocumentResolver.isLocal(path as DocumentResolver.Path)) - // FTP doesn't support random access and requires one connection per parallel read. - val shouldReadRemotePath = !path.isFtpPath - && Settings.READ_REMOTE_FILES_FOR_THUMBNAIL.valueCompat - if (!(isLocalPath || shouldReadRemotePath)) { - error("Cannot read $path for thumbnail") + if (path.isRemotePath) { + // FTP doesn't support random access and requires one connection per parallel read. + val shouldReadRemotePath = !path.isFtpPath + && Settings.READ_REMOTE_FILES_FOR_THUMBNAIL.valueCompat + if (!shouldReadRemotePath) { + error("Cannot read $path for thumbnail") + } } } val mimeType = AndroidFileTypeDetector.getMimeType(data.first, data.second).asMimeType() diff --git a/app/src/main/java/me/zhanghai/android/files/filelist/FileItemExtensions.kt b/app/src/main/java/me/zhanghai/android/files/filelist/FileItemExtensions.kt index 76b94dfba..374ee7382 100644 --- a/app/src/main/java/me/zhanghai/android/files/filelist/FileItemExtensions.kt +++ b/app/src/main/java/me/zhanghai/android/files/filelist/FileItemExtensions.kt @@ -21,7 +21,6 @@ import me.zhanghai.android.files.file.isPdf import me.zhanghai.android.files.provider.archive.createArchiveRootPath import me.zhanghai.android.files.provider.document.documentSupportsThumbnail import me.zhanghai.android.files.provider.document.isDocumentPath -import me.zhanghai.android.files.provider.document.resolver.DocumentResolver import me.zhanghai.android.files.provider.ftp.isFtpPath import me.zhanghai.android.files.provider.linux.isLinuxPath import me.zhanghai.android.files.settings.Settings @@ -61,12 +60,12 @@ val FileItem.supportsThumbnail: Boolean if (path.isDocumentPath && attributes.documentSupportsThumbnail) { return true } - val isLocalPath = path.isLinuxPath - || (path.isDocumentPath && DocumentResolver.isLocal(path as DocumentResolver.Path)) - val shouldReadRemotePath = !path.isFtpPath - && Settings.READ_REMOTE_FILES_FOR_THUMBNAIL.valueCompat - if (!(isLocalPath || shouldReadRemotePath)) { - return false + if (path.isRemotePath) { + val shouldReadRemotePath = !path.isFtpPath + && Settings.READ_REMOTE_FILES_FOR_THUMBNAIL.valueCompat + if (!shouldReadRemotePath) { + return false + } } return when { mimeType.isApk && path.isGetPackageArchiveInfoCompatible -> true diff --git a/app/src/main/java/me/zhanghai/android/files/filelist/PathExtensions.kt b/app/src/main/java/me/zhanghai/android/files/filelist/PathExtensions.kt index f249f8536..44b03acad 100644 --- a/app/src/main/java/me/zhanghai/android/files/filelist/PathExtensions.kt +++ b/app/src/main/java/me/zhanghai/android/files/filelist/PathExtensions.kt @@ -10,6 +10,8 @@ import me.zhanghai.android.files.file.MimeType import me.zhanghai.android.files.file.isSupportedArchive import me.zhanghai.android.files.provider.archive.archiveFile import me.zhanghai.android.files.provider.archive.isArchivePath +import me.zhanghai.android.files.provider.document.isDocumentPath +import me.zhanghai.android.files.provider.document.resolver.DocumentResolver import me.zhanghai.android.files.provider.linux.isLinuxPath val Path.name: String @@ -18,3 +20,10 @@ val Path.name: String fun Path.toUserFriendlyString(): String = if (isLinuxPath) toFile().path else toUri().toString() fun Path.isArchiveFile(mimeType: MimeType): Boolean = !isArchivePath && mimeType.isSupportedArchive + +val Path.isLocalPath: Boolean + get() = + isLinuxPath || (isDocumentPath && DocumentResolver.isLocal(this as DocumentResolver.Path)) + +val Path.isRemotePath: Boolean + get() = !isLocalPath