Skip to content

Commit b2e9352

Browse files
author
Laks Castro
authored
Merge pull request #94 from lakscastro/feat/add-write-to-file-actions
Add `Erase file contents` and `Append file contents` file options
2 parents a94a03b + 8e6d645 commit b2e9352

23 files changed

+953
-488
lines changed

android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/DocumentsContractApi.kt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,31 @@ internal class DocumentsContractApi(private val plugin: SharedStoragePlugin) :
3535
val width = call.argument<Int>("width")!!
3636
val height = call.argument<Int>("height")!!
3737

38-
val bitmap =
39-
DocumentsContract.getDocumentThumbnail(
40-
plugin.context.contentResolver,
41-
uri,
42-
Point(width, height),
43-
null
44-
)
38+
val bitmap = DocumentsContract.getDocumentThumbnail(
39+
plugin.context.contentResolver,
40+
uri,
41+
Point(width, height),
42+
null
43+
)
4544

46-
CoroutineScope(Dispatchers.Default).launch {
47-
if (bitmap != null) {
45+
if (bitmap != null) {
46+
CoroutineScope(Dispatchers.Default).launch {
4847
val base64 = bitmapToBase64(bitmap)
4948

5049
val data =
51-
mapOf(
52-
"base64" to base64,
53-
"uri" to "$uri",
54-
"width" to bitmap.width,
55-
"height" to bitmap.height,
56-
"byteCount" to bitmap.byteCount,
57-
"density" to bitmap.density
58-
)
50+
mapOf(
51+
"base64" to base64,
52+
"uri" to "$uri",
53+
"width" to bitmap.width,
54+
"height" to bitmap.height,
55+
"byteCount" to bitmap.byteCount,
56+
"density" to bitmap.density
57+
)
5958

6059
launch(Dispatchers.Main) { result.success(data) }
6160
}
61+
} else {
62+
result.success(null)
6263
}
6364
} else {
6465
result.notSupported(call.method, API_21)

android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentCommon.kt

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
4844
fun 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
)

android/src/main/kotlin/io/lakscastro/sharedstorage/storageaccessframework/lib/DocumentFileColumn.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ enum class DocumentFileColumn {
1616

1717
enum class DocumentFileColumnType {
1818
LONG,
19-
STRING
19+
STRING,
20+
INT
2021
}
2122

2223
fun parseDocumentFileColumn(column: String): DocumentFileColumn? {
@@ -66,7 +67,8 @@ fun typeOfColumn(column: String): DocumentFileColumnType? {
6667
DocumentsContract.Document.COLUMN_MIME_TYPE to DocumentFileColumnType.STRING,
6768
DocumentsContract.Document.COLUMN_SIZE to DocumentFileColumnType.LONG,
6869
DocumentsContract.Document.COLUMN_SUMMARY to DocumentFileColumnType.STRING,
69-
DocumentsContract.Document.COLUMN_LAST_MODIFIED to DocumentFileColumnType.LONG
70+
DocumentsContract.Document.COLUMN_LAST_MODIFIED to DocumentFileColumnType.LONG,
71+
DocumentsContract.Document.COLUMN_FLAGS to DocumentFileColumnType.INT
7072
)
7173

7274
return values[column]
@@ -76,5 +78,6 @@ fun cursorHandlerOf(type: DocumentFileColumnType): (Cursor, Int) -> Any {
7678
when(type) {
7779
DocumentFileColumnType.LONG -> { return { cursor, index -> cursor.getLong(index) } }
7880
DocumentFileColumnType.STRING -> { return { cursor, index -> cursor.getString(index) } }
81+
DocumentFileColumnType.INT -> { return { cursor, index -> cursor.getInt(index) } }
7982
}
8083
}

0 commit comments

Comments
 (0)