Skip to content

Commit

Permalink
[Refactor] Unify concept of local and remote path.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Aug 10, 2023
1 parent 56bb2e7 commit 0efbf83
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
13 changes: 2 additions & 11 deletions app/src/main/java/me/zhanghai/android/files/coil/CoilUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,15 @@ 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

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 0efbf83

Please sign in to comment.