Skip to content

Commit

Permalink
1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
lumkit committed Jun 26, 2024
1 parent fb43ba5 commit 1e8aff3
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class MainActivity : ComponentActivity() {

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
takePersistableUriPermission(0x000002, requestCode, resultCode, data)
takePersistableUriPermission(12138, requestCode, resultCode, data)
}

override fun onDestroy() {
Expand Down
31 changes: 15 additions & 16 deletions file-android/src/main/java/io/github/lumkit/io/impl/ShizukuFile.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.lumkit.io.impl

import com.topjohnwu.superuser.ShellUtils
import io.github.lumkit.io.LintFile
import io.github.lumkit.io.shell.AdbShellPublic

Expand All @@ -11,48 +10,48 @@ class ShizukuFile : LintFile {
constructor(file: LintFile, child: String) : super(file, child)

override fun exists(): Boolean =
AdbShellPublic.doCmdSync("[ -e \"$path\" ] && echo 1 || echo 0") == "1"
AdbShellPublic.doCmdSync("[ -e \"${path.replace("\u200d", "")}\" ] && echo 1 || echo 0") == "1"

override fun getParent(): String = _file.parent ?: ""
override fun getParent(): String = _file.parent?.replace("\u200d", "") ?: ""

override fun getParentFile(): LintFile = ShizukuFile(getParent())

override fun canRead(): Boolean =
AdbShellPublic.doCmdSync("[ -r \"$path\" ] && echo 1 || echo 0") == "1"
AdbShellPublic.doCmdSync("[ -r \"${path.replace("\u200d", "")}\" ] && echo 1 || echo 0") == "1"

override fun canWrite(): Boolean =
AdbShellPublic.doCmdSync("[ -w \"$path\" ] && echo 1 || echo 0") == "1"
AdbShellPublic.doCmdSync("[ -w \"${path.replace("\u200d", "")}\" ] && echo 1 || echo 0") == "1"

override fun isDirectory(): Boolean =
AdbShellPublic.doCmdSync("[ -d \"$path\" ] && echo 1 || echo 0") == "1"
AdbShellPublic.doCmdSync("[ -d \"${path.replace("\u200d", "")}\" ] && echo 1 || echo 0") == "1"

override fun isFile(): Boolean =
AdbShellPublic.doCmdSync("[ -f \"$path\" ] && echo 1 || echo 0") == "1"
AdbShellPublic.doCmdSync("[ -f \"${path.replace("\u200d", "")}\" ] && echo 1 || echo 0") == "1"

override fun lastModified(): Long = try {
AdbShellPublic.doCmdSync("stat -c '%Y' \"$path\"").toLong()
AdbShellPublic.doCmdSync("stat -c '%Y' \"${path.replace("\u200d", "")}\"").toLong()
} catch (e: Exception) {
e.printStackTrace()
0
}

override fun length(): Long = try {
AdbShellPublic.doCmdSync("stat -c '%s' \"$path\"").toLong()
AdbShellPublic.doCmdSync("stat -c '%s' \"${path.replace("\u200d", "")}\"").toLong()
} catch (e: Exception) {
e.printStackTrace()
0
}

override fun createNewFile(): Boolean =
AdbShellPublic.doCmdSync("[ ! -e \"$path\" ] && echo -n > \"$path\" && echo 1 || echo 0") == "1"
AdbShellPublic.doCmdSync("[ ! -e \"${path.replace("\u200d", "")}\" ] && echo -n > \"${path.replace("\u200d", "")}\" && echo 1 || echo 0") == "1"

override fun delete(): Boolean =
AdbShellPublic.doCmdSync("(rm -f \"$path\" || rmdir -f \"$path\") && echo 1 || echo 0") == "1"
AdbShellPublic.doCmdSync("(rm -rf \"${path.replace("\u200d", "")}\") && echo 1 || echo 0") == "1"

override fun list(): Array<String> {
if (!isDirectory())
return arrayOf()
val cmd = "ls -a \"$path\""
val cmd = "ls -a \"${path.replace("\u200d", "")}\""

val list = ArrayList(AdbShellPublic.doCmdSync(cmd).split("\n"))
val iterator = list.listIterator()
Expand All @@ -64,7 +63,7 @@ class ShizukuFile : LintFile {
}
}

return list.map { "$path/$it" }.toTypedArray()
return list.map { "${path.replace("\u200d", "")}/$it" }.toTypedArray()
}

override fun list(filter: (String) -> Boolean): Array<String> = list().filter { filter(it) }.toTypedArray()
Expand All @@ -73,15 +72,15 @@ class ShizukuFile : LintFile {

override fun listFiles(filter: (LintFile) -> Boolean): Array<LintFile> = listFiles().filter { filter(it) }.toTypedArray()

override fun mkdirs(): Boolean = AdbShellPublic.doCmdSync("mkdir -p \"$path\" && echo 1 || echo 0") == "1"
override fun mkdirs(): Boolean = AdbShellPublic.doCmdSync("mkdir -p \"${path.replace("\u200d", "")}\" && echo 1 || echo 0") == "1"

override fun renameTo(dest: String): Boolean {
val cmd = "mv -f \"$path\" \"${getParent()}/$dest\" && echo 1 || echo 0"
val cmd = "mv -f \"${path.replace("\u200d", "")}\" \"${getParent()}/${dest.replace("\u200d", "")}\" && echo 1 || echo 0"
return AdbShellPublic.doCmdSync(cmd) == "1"
}

fun clear(): Boolean {
val cmd = "(echo -n > \"$path\") && echo 1 || echo 0"
val cmd = "(echo -n > \"${path.replace("\u200d", "")}\") && echo 1 || echo 0"
return AdbShellPublic.doCmdSync(cmd) == "1"
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.github.lumkit.io.impl

import android.os.Build
import androidx.documentfile.provider.DocumentFile
import io.github.lumkit.io.LintFile
import io.github.lumkit.io.LintFileConfiguration
import io.github.lumkit.io.absolutePath
import io.github.lumkit.io.androidPath
import io.github.lumkit.io.documentFileUri
import io.github.lumkit.io.primaryChildPath
import io.github.lumkit.io.uri
import io.github.lumkit.io.documentReallyUri
import java.io.File
import java.io.IOException
import java.io.FileNotFoundException

class StorageAccessFrameworkFile : LintFile {

Expand All @@ -18,12 +16,12 @@ class StorageAccessFrameworkFile : LintFile {
constructor(file: LintFile, child: String) : super(file, child)

private val context = LintFileConfiguration.instance.context;
internal val documentFile: DocumentFile? = DocumentFile.fromTreeUri(context, path.documentFileUri(false))
internal val documentFile: DocumentFile? = DocumentFile.fromTreeUri(context, path.replace("\u200d", "").documentReallyUri(false))

override fun exists(): Boolean =
this.documentFile?.exists() ?: false

override fun getParent(): String = this._file.parent ?: ""
override fun getParent(): String = this._file.parent?.replace("\u200d", "") ?: ""

override fun getParentFile(): LintFile = StorageAccessFrameworkFile(getParent())

Expand All @@ -46,32 +44,13 @@ class StorageAccessFrameworkFile : LintFile {
this.documentFile?.length() ?: -1

override fun createNewFile(): Boolean {
val primaryChildPath = getParent().primaryChildPath()
val childPath = primaryChildPath.substring(primaryChildPath.indexOf("/") + 1)
val names = childPath.split("/")
val primaryPath = File(androidPath, names[0]).absolutePath
var treeFile = DocumentFile.fromTreeUri(context, primaryPath.uri()) ?: throw IOException("File does not exist.")
names.subList(1, names.size).forEach { name ->
val findFile = treeFile.findFile(name)
treeFile = if (findFile == null) {
treeFile.createDirectory(name) ?: throw IOException("File does not exist.")
} else {
if (findFile.exists()) {
findFile
} else {
findFile.createDirectory(name) ?: throw IOException("File does not exist.")
}
}
}
var findFile = treeFile.findFile(name)
if (findFile == null) {
findFile = treeFile.createFile("*/*", name)
} else {
if (!findFile.exists()) {
findFile = treeFile.createFile("*/*", name)
}
if (exists())
return true
val parentFile = getParentFile() as StorageAccessFrameworkFile
if (!parentFile.exists()) {
throw FileNotFoundException("No such file or directory: ${parentFile.path}")
}
return findFile?.exists() ?: false
return parentFile.documentFile?.createFile("*/*", name) != null
}

override fun delete(): Boolean =
Expand All @@ -82,7 +61,7 @@ class StorageAccessFrameworkFile : LintFile {
if (this.documentFile != null) {
val listFiles = this.documentFile.listFiles()
listFiles.forEach {
list.add(it.uri.absolutePath())
list.add(it.uri.absolutePath().replace("\u200d", ""))
}
}
return list.toTypedArray()
Expand All @@ -94,7 +73,7 @@ class StorageAccessFrameworkFile : LintFile {
val listFiles = this.documentFile.listFiles()
listFiles.forEach {
if (filter(it.name ?: ""))
list.add(it.uri.absolutePath())
list.add(it.uri.absolutePath().replace("\u200d", ""))
}
}
return list.toTypedArray()
Expand All @@ -119,24 +98,51 @@ class StorageAccessFrameworkFile : LintFile {
}

override fun mkdirs(): Boolean {
val primaryChildPath = path.primaryChildPath()
val childPath = primaryChildPath.substring(primaryChildPath.indexOf("/") + 1)
val names = childPath.split("/")
val primaryPath = File(androidPath, names[0]).absolutePath
var treeFile = DocumentFile.fromTreeUri(context, primaryPath.uri()) ?: return false
names.subList(1, names.size).forEach { name ->
val findFile = treeFile.findFile(name)
treeFile = if (findFile == null) {
treeFile.createDirectory(name) ?: return false
} else {
if (findFile.exists()) {
findFile
} else {
findFile.createDirectory(name) ?: return false
}
if (exists())
return true

var startPath = startPath()
val endPath = endPath()

if (endPath.isEmpty())
return true

val names = endPath.substring(1).split("/")
names.forEach {
val p = File(startPath, it).absolutePath
val safFile = StorageAccessFrameworkFile(p)
if (!safFile.exists()) {
StorageAccessFrameworkFile(startPath).documentFile?.createDirectory(it) ?: throw FileNotFoundException("Cannot write to file $path")
}
startPath = p
}
return StorageAccessFrameworkFile(startPath).exists()
}

private fun startPath(): String {
val path = path.replace("\u200d", "")
val list = (if (path.startsWith("/")) {
path.substring(1)
} else path).split("/")
val builder = StringBuilder()
for (i in 0 until if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) 6 else 5) {
builder.append("/")
.append(list[i])
}
return builder.toString()
}

private fun endPath(): String {
val path = path.replace("\u200d", "")
val list = (if (path.startsWith("/")) {
path.substring(1)
} else path).split("/")
val builder = StringBuilder()
for (i in (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) 6 else 5) until list.size) {
builder.append("/")
.append(list[i])
}
return treeFile.exists()
return builder.toString()
}

override fun renameTo(dest: String): Boolean =
Expand Down
Loading

0 comments on commit 1e8aff3

Please sign in to comment.