diff options
| author | 2023-03-11 00:36:32 -0500 | |
|---|---|---|
| committer | 2023-06-03 00:05:40 -0700 | |
| commit | 2444df2bf495fbac6fe94290cdf7b9b2bbf13378 (patch) | |
| tree | 5cbf056dc9751ad00514c247ef9097fd44f81caf /src/android | |
| parent | android: Convert DirectoryStateReceiver to Kotlin (diff) | |
| download | yuzu-2444df2bf495fbac6fe94290cdf7b9b2bbf13378.tar.gz yuzu-2444df2bf495fbac6fe94290cdf7b9b2bbf13378.tar.xz yuzu-2444df2bf495fbac6fe94290cdf7b9b2bbf13378.zip | |
android: Convert DocumentsTree to Kotlin
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.java | 125 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt | 110 |
2 files changed, 110 insertions, 125 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.java deleted file mode 100644 index beb790ab1..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.java +++ /dev/null | |||
| @@ -1,125 +0,0 @@ | |||
| 1 | package org.yuzu.yuzu_emu.utils; | ||
| 2 | |||
| 3 | import android.content.Context; | ||
| 4 | import android.net.Uri; | ||
| 5 | |||
| 6 | import androidx.annotation.Nullable; | ||
| 7 | import androidx.documentfile.provider.DocumentFile; | ||
| 8 | |||
| 9 | import org.yuzu.yuzu_emu.YuzuApplication; | ||
| 10 | import org.yuzu.yuzu_emu.model.MinimalDocumentFile; | ||
| 11 | |||
| 12 | import java.util.HashMap; | ||
| 13 | import java.util.Map; | ||
| 14 | import java.util.StringTokenizer; | ||
| 15 | |||
| 16 | public class DocumentsTree { | ||
| 17 | private DocumentsNode root; | ||
| 18 | private final Context context; | ||
| 19 | public static final String DELIMITER = "/"; | ||
| 20 | |||
| 21 | public DocumentsTree() { | ||
| 22 | context = YuzuApplication.getAppContext(); | ||
| 23 | } | ||
| 24 | |||
| 25 | public void setRoot(Uri rootUri) { | ||
| 26 | root = null; | ||
| 27 | root = new DocumentsNode(); | ||
| 28 | root.uri = rootUri; | ||
| 29 | root.isDirectory = true; | ||
| 30 | } | ||
| 31 | |||
| 32 | public int openContentUri(String filepath, String openmode) { | ||
| 33 | DocumentsNode node = resolvePath(filepath); | ||
| 34 | if (node == null) { | ||
| 35 | return -1; | ||
| 36 | } | ||
| 37 | return FileUtil.openContentUri(context, node.uri.toString(), openmode); | ||
| 38 | } | ||
| 39 | |||
| 40 | public long getFileSize(String filepath) { | ||
| 41 | DocumentsNode node = resolvePath(filepath); | ||
| 42 | if (node == null || node.isDirectory) { | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | return FileUtil.getFileSize(context, node.uri.toString()); | ||
| 46 | } | ||
| 47 | |||
| 48 | public boolean Exists(String filepath) { | ||
| 49 | return resolvePath(filepath) != null; | ||
| 50 | } | ||
| 51 | |||
| 52 | @Nullable | ||
| 53 | private DocumentsNode resolvePath(String filepath) { | ||
| 54 | StringTokenizer tokens = new StringTokenizer(filepath, DELIMITER, false); | ||
| 55 | DocumentsNode iterator = root; | ||
| 56 | while (tokens.hasMoreTokens()) { | ||
| 57 | String token = tokens.nextToken(); | ||
| 58 | if (token.isEmpty()) continue; | ||
| 59 | iterator = find(iterator, token); | ||
| 60 | if (iterator == null) return null; | ||
| 61 | } | ||
| 62 | return iterator; | ||
| 63 | } | ||
| 64 | |||
| 65 | @Nullable | ||
| 66 | private DocumentsNode find(DocumentsNode parent, String filename) { | ||
| 67 | if (parent.isDirectory && !parent.loaded) { | ||
| 68 | structTree(parent); | ||
| 69 | } | ||
| 70 | return parent.children.get(filename); | ||
| 71 | } | ||
| 72 | |||
| 73 | /** | ||
| 74 | * Construct current level directory tree | ||
| 75 | * @param parent parent node of this level | ||
| 76 | */ | ||
| 77 | private void structTree(DocumentsNode parent) { | ||
| 78 | MinimalDocumentFile[] documents = FileUtil.listFiles(context, parent.uri); | ||
| 79 | for (MinimalDocumentFile document: documents) { | ||
| 80 | DocumentsNode node = new DocumentsNode(document); | ||
| 81 | node.parent = parent; | ||
| 82 | parent.children.put(node.name, node); | ||
| 83 | } | ||
| 84 | parent.loaded = true; | ||
| 85 | } | ||
| 86 | |||
| 87 | public static boolean isNativePath(String path) { | ||
| 88 | if (path.length() > 0) { | ||
| 89 | return path.charAt(0) == '/'; | ||
| 90 | } | ||
| 91 | return false; | ||
| 92 | } | ||
| 93 | |||
| 94 | private static class DocumentsNode { | ||
| 95 | private DocumentsNode parent; | ||
| 96 | private final Map<String, DocumentsNode> children = new HashMap<>(); | ||
| 97 | private String name; | ||
| 98 | private Uri uri; | ||
| 99 | private boolean loaded = false; | ||
| 100 | private boolean isDirectory = false; | ||
| 101 | |||
| 102 | private DocumentsNode() {} | ||
| 103 | private DocumentsNode(MinimalDocumentFile document) { | ||
| 104 | name = document.getFilename(); | ||
| 105 | uri = document.getUri(); | ||
| 106 | isDirectory = document.isDirectory(); | ||
| 107 | loaded = !isDirectory; | ||
| 108 | } | ||
| 109 | private DocumentsNode(DocumentFile document, boolean isCreateDir) { | ||
| 110 | name = document.getName(); | ||
| 111 | uri = document.getUri(); | ||
| 112 | isDirectory = isCreateDir; | ||
| 113 | loaded = true; | ||
| 114 | } | ||
| 115 | |||
| 116 | private void rename(String name) { | ||
| 117 | if (parent == null) { | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | parent.children.remove(this.name); | ||
| 121 | this.name = name; | ||
| 122 | parent.children.put(name, this); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt new file mode 100644 index 000000000..a4795ca22 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | package org.yuzu.yuzu_emu.utils | ||
| 2 | |||
| 3 | import android.net.Uri | ||
| 4 | import androidx.documentfile.provider.DocumentFile | ||
| 5 | import org.yuzu.yuzu_emu.YuzuApplication | ||
| 6 | import org.yuzu.yuzu_emu.model.MinimalDocumentFile | ||
| 7 | import java.io.File | ||
| 8 | import java.util.* | ||
| 9 | |||
| 10 | class DocumentsTree { | ||
| 11 | private var root: DocumentsNode? = null | ||
| 12 | |||
| 13 | fun setRoot(rootUri: Uri?) { | ||
| 14 | root = null | ||
| 15 | root = DocumentsNode() | ||
| 16 | root!!.uri = rootUri | ||
| 17 | root!!.isDirectory = true | ||
| 18 | } | ||
| 19 | |||
| 20 | fun openContentUri(filepath: String, openMode: String?): Int { | ||
| 21 | val node = resolvePath(filepath) ?: return -1 | ||
| 22 | return FileUtil.openContentUri(YuzuApplication.appContext, node.uri.toString(), openMode) | ||
| 23 | } | ||
| 24 | |||
| 25 | fun getFileSize(filepath: String): Long { | ||
| 26 | val node = resolvePath(filepath) | ||
| 27 | return if (node == null || node.isDirectory) { | ||
| 28 | 0 | ||
| 29 | } else FileUtil.getFileSize(YuzuApplication.appContext, node.uri.toString()) | ||
| 30 | } | ||
| 31 | |||
| 32 | fun exists(filepath: String): Boolean { | ||
| 33 | return resolvePath(filepath) != null | ||
| 34 | } | ||
| 35 | |||
| 36 | private fun resolvePath(filepath: String): DocumentsNode? { | ||
| 37 | val tokens = StringTokenizer(filepath, File.separator, false) | ||
| 38 | var iterator = root | ||
| 39 | while (tokens.hasMoreTokens()) { | ||
| 40 | val token = tokens.nextToken() | ||
| 41 | if (token.isEmpty()) continue | ||
| 42 | iterator = find(iterator, token) | ||
| 43 | if (iterator == null) return null | ||
| 44 | } | ||
| 45 | return iterator | ||
| 46 | } | ||
| 47 | |||
| 48 | private fun find(parent: DocumentsNode?, filename: String): DocumentsNode? { | ||
| 49 | if (parent!!.isDirectory && !parent.loaded) { | ||
| 50 | structTree(parent) | ||
| 51 | } | ||
| 52 | return parent.children[filename] | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Construct current level directory tree | ||
| 57 | * @param parent parent node of this level | ||
| 58 | */ | ||
| 59 | private fun structTree(parent: DocumentsNode) { | ||
| 60 | val documents = FileUtil.listFiles(YuzuApplication.appContext, parent.uri!!) | ||
| 61 | for (document in documents) { | ||
| 62 | val node = DocumentsNode(document) | ||
| 63 | node.parent = parent | ||
| 64 | parent.children[node.name] = node | ||
| 65 | } | ||
| 66 | parent.loaded = true | ||
| 67 | } | ||
| 68 | |||
| 69 | private class DocumentsNode { | ||
| 70 | var parent: DocumentsNode? = null | ||
| 71 | val children: MutableMap<String?, DocumentsNode> = HashMap() | ||
| 72 | var name: String? = null | ||
| 73 | var uri: Uri? = null | ||
| 74 | var loaded = false | ||
| 75 | var isDirectory = false | ||
| 76 | |||
| 77 | constructor() | ||
| 78 | constructor(document: MinimalDocumentFile) { | ||
| 79 | name = document.filename | ||
| 80 | uri = document.uri | ||
| 81 | isDirectory = document.isDirectory | ||
| 82 | loaded = !isDirectory | ||
| 83 | } | ||
| 84 | |||
| 85 | private constructor(document: DocumentFile, isCreateDir: Boolean) { | ||
| 86 | name = document.name | ||
| 87 | uri = document.uri | ||
| 88 | isDirectory = isCreateDir | ||
| 89 | loaded = true | ||
| 90 | } | ||
| 91 | |||
| 92 | private fun rename(name: String) { | ||
| 93 | if (parent == null) { | ||
| 94 | return | ||
| 95 | } | ||
| 96 | parent!!.children.remove(this.name) | ||
| 97 | this.name = name | ||
| 98 | parent!!.children[name] = this | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | companion object { | ||
| 103 | @JvmStatic | ||
| 104 | fun isNativePath(path: String): Boolean { | ||
| 105 | return if (path.isNotEmpty()) { | ||
| 106 | path[0] == '/' | ||
| 107 | } else false | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||