@@ -39,74 +39,58 @@ fun documentFromUri(
3939
4040
4141/* *
42- * Standard map encoding of a `DocumentFile` and must be used before returning any `DocumentFile`
43- * from plugin results, like:
44- * ```dart
45- * result.success(createDocumentFileMap(documentFile))
46- * ```
42+ * Convert a [DocumentFile] using the default method for map encoding
4743 */
4844fun createDocumentFileMap (documentFile : DocumentFile ? ): Map <String , Any ?>? {
4945 if (documentFile == null ) return null
5046
51- return mapOf (
52- " isDirectory" to documentFile.isDirectory,
53- " isFile" to documentFile.isFile,
54- " isVirtual" to documentFile.isVirtual,
55- " name" to (documentFile.name ? : " " ),
56- " type" to (documentFile.type ? : " " ),
57- " uri" to " ${documentFile.uri} " ,
58- " exists" to " ${documentFile.exists()} "
47+ return createDocumentFileMap(
48+ DocumentsContract .getDocumentId(documentFile.uri),
49+ parentUri = documentFile.parentFile?.uri,
50+ isDirectory = documentFile.isDirectory,
51+ isFile = documentFile.isFile,
52+ isVirtual = documentFile.isVirtual,
53+ name = documentFile.name,
54+ type = documentFile.type,
55+ uri = documentFile.uri,
56+ exists = documentFile.exists(),
57+ size = documentFile.length(),
58+ lastModified = documentFile.lastModified()
5959 )
6060}
6161
62-
6362/* *
64- * Standard map encoding of a row result of a `DocumentFile`
65- * ```kt
63+ * Standard map encoding of a `DocumentFile` and must be used before returning any `DocumentFile`
64+ * from plugin results, like:
65+ * ```dart
6666 * result.success(createDocumentFileMap(documentFile))
6767 * ```
68- *
69- * Example:
70- * ```py
71- * input = {
72- * "last_modified": 2939496, # Key from DocumentsContract.Document.COLUMN_LAST_MODIFIED
73- * "_display_name": "MyFile" # Key from DocumentsContract.Document.COLUMN_DISPLAY_NAME
74- * }
75- *
76- * output = createCursorRowMap(input)
77- *
78- * print(output)
79- * {
80- * "lastModified": 2939496,
81- * "displayName": "MyFile"
82- * }
83- * ```
8468 */
85- fun createCursorRowMap (
86- parentUri : Uri ,
69+ fun createDocumentFileMap (
70+ id : String? ,
71+ parentUri : Uri ? ,
72+ isDirectory : Boolean? ,
73+ isFile : Boolean? ,
74+ isVirtual : Boolean? ,
75+ name : String? ,
76+ type : String? ,
8777 uri : Uri ,
88- data : Map <String , Any >,
89- isDirectory : Boolean?
90- ): Map <String , Any > {
91- val values = DocumentFileColumn .values()
92-
93- val formattedData = mutableMapOf<String , Any >()
94-
95- for (value in values) {
96- val key = parseDocumentFileColumn(value)
97-
98- if (data[key] != null ) {
99- formattedData[documentFileColumnToRawString(value)!! ] = data[key]!!
100- }
101- }
102-
78+ exists : Boolean? ,
79+ size : Long? ,
80+ lastModified : Long?
81+ ): Map <String , Any ?> {
10382 return mapOf (
104- " data" to formattedData,
105- " metadata" to mapOf (
106- " parentUri" to " $parentUri " ,
107- " isDirectory" to isDirectory,
108- " uri" to " $uri "
109- )
83+ " id" to id,
84+ " parentUri" to " $parentUri " ,
85+ " isDirectory" to isDirectory,
86+ " isFile" to isFile,
87+ " isVirtual" to isVirtual,
88+ " name" to name,
89+ " type" to type,
90+ " uri" to " $uri " ,
91+ " exists" to exists,
92+ " size" to size,
93+ " lastModified" to lastModified
11094 )
11195}
11296
@@ -130,7 +114,7 @@ fun traverseDirectoryEntries(
130114 targetUri : Uri ,
131115 columns : Array <String >,
132116 rootOnly : Boolean ,
133- block : (data: Map <String , Any >, isLast: Boolean ) -> Unit
117+ block : (data: Map <String , Any ? >, isLast: Boolean ) -> Unit
134118): Boolean {
135119 val documentId = try {
136120 DocumentsContract .getDocumentId(targetUri)
@@ -158,7 +142,10 @@ fun traverseDirectoryEntries(
158142 if (rootOnly) emptyArray() else arrayOf(DocumentsContract .Document .COLUMN_MIME_TYPE )
159143
160144 val intrinsicColumns =
161- arrayOf(DocumentsContract .Document .COLUMN_DOCUMENT_ID )
145+ arrayOf(
146+ DocumentsContract .Document .COLUMN_DOCUMENT_ID ,
147+ DocumentsContract .Document .COLUMN_FLAGS
148+ )
162149
163150 val projection = arrayOf(
164151 * columns,
@@ -215,11 +202,22 @@ fun traverseDirectoryEntries(
215202 }
216203
217204 block(
218- createCursorRowMap(
219- parent,
220- uri,
221- data,
222- isDirectory = isDirectory
205+ createDocumentFileMap(
206+ parentUri = parent,
207+ uri = uri,
208+ name = data[DocumentsContract .Document .COLUMN_DISPLAY_NAME ] as String? ,
209+ exists = true ,
210+ id = data[DocumentsContract .Document .COLUMN_DOCUMENT_ID ] as String ,
211+ isDirectory = isDirectory == true ,
212+ isFile = isDirectory == false ,
213+ isVirtual = if (Build .VERSION .SDK_INT >= API_24 ) {
214+ (data[DocumentsContract .Document .COLUMN_FLAGS ] as Int and DocumentsContract .Document .FLAG_VIRTUAL_DOCUMENT ) != 0
215+ } else {
216+ false
217+ },
218+ type = data[DocumentsContract .Document .COLUMN_MIME_TYPE ] as String? ,
219+ size = data[DocumentsContract .Document .COLUMN_SIZE ] as Long? ,
220+ lastModified = data[DocumentsContract .Document .COLUMN_LAST_MODIFIED ] as Long?
223221 ),
224222 dirNodes.isEmpty() && cursor.isLast
225223 )
0 commit comments