From 6e046f013abff90aff355114ea25d4cf436d2417 Mon Sep 17 00:00:00 2001 From: Daniel Dunn Date: Sun, 12 Jun 2022 21:32:53 -0600 Subject: [PATCH 1/3] Enable listing the contents of a SAF subfolder --- .../lib/DocumentCommon.kt | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt b/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt index 8e49462..ad8c490 100644 --- a/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt +++ b/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt @@ -43,6 +43,8 @@ fun documentFromSingleUri(context: Context, uri: Uri): DocumentFile? { return DocumentFile.fromSingleUri(context, documentUri) } + + /** * Generate the `DocumentFile` reference from string `uri` */ @@ -161,10 +163,24 @@ fun traverseDirectoryEntries( rootOnly: Boolean, block: (data: Map, isLast: Boolean) -> Unit ): Boolean { - val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree( - rootUri, - DocumentsContract.getTreeDocumentId(rootUri) - ) + + + var childrenUritmp: Uri?; + + + //https://stackoverflow.com/questions/41096332/issues-traversing-through-directory-hierarchy-with-android-storage-access-framew + //Credit to user: Foobnix + //If we were to always use getTreeDocumentId, it would apparently always only list the top level folder even if you request a subfolder + + try { + //for childs and sub child dirs + childrenUritmp = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getDocumentId(rootUri)); + } catch (e:Exception) { + // for parent dir + childrenUritmp = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getTreeDocumentId(rootUri)); + } + + val childrenUri = childrenUritmp as Uri; /// Keep track of our directory hierarchy val dirNodes = mutableListOf>(Pair(rootUri, childrenUri)) From 982cbaea009a2a3b88c474482afa61e5ce4d0496 Mon Sep 17 00:00:00 2001 From: Daniel Dunn Date: Sun, 12 Jun 2022 21:50:24 -0600 Subject: [PATCH 2/3] Document ability to list subdirectories. --- docs/Usage/Storage Access Framework.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/Usage/Storage Access Framework.md b/docs/Usage/Storage Access Framework.md index 41cd5d7..1b0c535 100644 --- a/docs/Usage/Storage Access Framework.md +++ b/docs/Usage/Storage Access Framework.md @@ -71,7 +71,7 @@ This method list files lazily **over a granted uri:** > **Note** `DocumentFileColumn.id` is optional. It is required to fetch the file list from native API. So it is enabled regardless if you include this column or not. And this applies only to this API (`listFiles`). ```dart -/// *Must* be a granted uri from `openDocumentTree` +/// *Must* be a granted uri from `openDocumentTree`, or a URI representing a child under such a granted uri. final Uri myGrantedUri = ... final DocumentFile? documentFileOfMyGrantedUri = await myGrantedUri.toDocumentFile(); @@ -598,6 +598,7 @@ This class represents but is not the mirror of the original [`DocumentFile`](htt This class is not intended to be instantiated, and it is only used for typing and convenient purposes. + ### QueryMetadata This class wraps useful metadata of the source queries returned by the `PartialDocumentFile`. From b8a58a5fe145bc484be0b59aa7de6e8c8b62b47f Mon Sep 17 00:00:00 2001 From: Laks Castro Date: Tue, 14 Jun 2022 11:35:59 -0300 Subject: [PATCH 3/3] (#59) Tiny comment improvements --- .../lib/DocumentCommon.kt | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt b/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt index ad8c490..a3d6f9a 100644 --- a/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt +++ b/android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt @@ -164,25 +164,23 @@ fun traverseDirectoryEntries( block: (data: Map, isLast: Boolean) -> Unit ): Boolean { - var childrenUritmp: Uri?; - - //https://stackoverflow.com/questions/41096332/issues-traversing-through-directory-hierarchy-with-android-storage-access-framew - //Credit to user: Foobnix - //If we were to always use getTreeDocumentId, it would apparently always only list the top level folder even if you request a subfolder - + // https://stackoverflow.com/questions/41096332/issues-traversing-through-directory-hierarchy-with-android-storage-access-framew + // Credit to user: Foobnix + // If we were to always use getTreeDocumentId, it would apparently always only list the top level folder even if you request a subfolder try { - //for childs and sub child dirs + // for childs and sub child dirs childrenUritmp = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getDocumentId(rootUri)); - } catch (e:Exception) { + } catch (e: Exception) { // for parent dir childrenUritmp = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, DocumentsContract.getTreeDocumentId(rootUri)); } + // TODO(@EternityForest, @lakscastro): Remove this variable and use: `val childrenUri = try { ... } catch (e: Exception) { ... }` val childrenUri = childrenUritmp as Uri; - /// Keep track of our directory hierarchy + // Keep track of our directory hierarchy val dirNodes = mutableListOf>(Pair(rootUri, childrenUri)) while (dirNodes.isNotEmpty()) { @@ -203,7 +201,7 @@ fun traverseDirectoryEntries( val cursor = contentResolver.query( children, projection, - /// TODO: Add support for `selection`, `selectionArgs` and `sortOrder` + // TODO: Add support for `selection`, `selectionArgs` and `sortOrder` null, null, null @@ -283,7 +281,7 @@ fun bitmapToBase64(bitmap: Bitmap): String { } /** - * Trick to verify if is a tree URi even not in API 26+ + * Trick to verify if is a tree URI even not in API 26+ */ fun isTreeUri(uri: Uri): Boolean { if (Build.VERSION.SDK_INT >= API_24) {