2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 22:25:08 +00:00

fix: fix SAF issues before Android 7.0 (SDK Level < 24)

This commit is contained in:
ShellWen Chen
2024-07-04 14:54:38 +08:00
committed by Albert Vaca Cintora
parent e391750e0e
commit e82c0fea84

View File

@@ -71,7 +71,18 @@ class SafFileSystemProvider(
options: Set<OpenOption>, options: Set<OpenOption>,
vararg attrs_: FileAttribute<*> vararg attrs_: FileAttribute<*>
): SeekableByteChannel { ): SeekableByteChannel {
return newFileChannel(path, options, *attrs_) val channel = newFileChannel(path, options, *attrs_)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
val clazz = Class.forName("j$.nio.channels.DesugarChannels")
val method = clazz.getDeclaredMethod(
"convertMaybeLegacyFileChannelFromLibrary",
FileChannel::class.java
)
return method.invoke(null, channel) as SeekableByteChannel
}
return channel
} }
/** /**
@@ -176,6 +187,8 @@ class SafFileSystemProvider(
override fun iterator(): MutableIterator<Path> { override fun iterator(): MutableIterator<Path> {
val documentFile = dir.getDocumentFile(context)!! val documentFile = dir.getDocumentFile(context)!!
return documentFile.listFiles().mapNotNull { return documentFile.listFiles().mapNotNull {
if (it.uri.path?.endsWith(".android_secure") == true) return@mapNotNull null
val newPath = SafPath(dir.fileSystem, it.uri, null, dir.names + it.name!!) val newPath = SafPath(dir.fileSystem, it.uri, null, dir.names + it.name!!)
if (filter.accept(newPath)) newPath else null if (filter.accept(newPath)) newPath else null
}.toMutableList().iterator() }.toMutableList().iterator()
@@ -240,17 +253,22 @@ class SafFileSystemProvider(
val destParentUri = target.parent.getDocumentFile(context)!!.uri val destParentUri = target.parent.getDocumentFile(context)!!.uri
// 1. If dest parent is the same as source parent, rename the file // 1. If dest parent is the same as source parent, rename the file
if (parentUri == destParentUri) { run firstStep@{
try { if (parentUri == destParentUri) {
val newUri = DocumentsContract.renameDocument( try {
context.contentResolver, val newUri = DocumentsContract.renameDocument(
sourceUri, context.contentResolver,
target.names.last() sourceUri,
) target.names.last()
source.safUri = newUri!! )
return if (newUri == null) { // renameDocument returns null on failure
} catch (ignored: FileNotFoundException) { return@firstStep
// no-op: fallback to the next method }
source.safUri = newUri
return
} catch (ignored: FileNotFoundException) {
// no-op: fallback to the next method
}
} }
} }