Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Erase file contents and Append file contents file options #94

Merged
merged 6 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,31 @@ internal class DocumentsContractApi(private val plugin: SharedStoragePlugin) :
val width = call.argument<Int>("width")!!
val height = call.argument<Int>("height")!!

val bitmap =
DocumentsContract.getDocumentThumbnail(
plugin.context.contentResolver,
uri,
Point(width, height),
null
)
val bitmap = DocumentsContract.getDocumentThumbnail(
plugin.context.contentResolver,
uri,
Point(width, height),
null
)

CoroutineScope(Dispatchers.Default).launch {
if (bitmap != null) {
if (bitmap != null) {
CoroutineScope(Dispatchers.Default).launch {
val base64 = bitmapToBase64(bitmap)

val data =
mapOf(
"base64" to base64,
"uri" to "$uri",
"width" to bitmap.width,
"height" to bitmap.height,
"byteCount" to bitmap.byteCount,
"density" to bitmap.density
)
mapOf(
"base64" to base64,
"uri" to "$uri",
"width" to bitmap.width,
"height" to bitmap.height,
"byteCount" to bitmap.byteCount,
"density" to bitmap.density
)

launch(Dispatchers.Main) { result.success(data) }
}
} else {
result.success(null)
}
} else {
result.notSupported(call.method, API_21)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,74 +39,58 @@ fun documentFromUri(


/**
* Standard map encoding of a `DocumentFile` and must be used before returning any `DocumentFile`
* from plugin results, like:
* ```dart
* result.success(createDocumentFileMap(documentFile))
* ```
* Convert a [DocumentFile] using the default method for map encoding
*/
fun createDocumentFileMap(documentFile: DocumentFile?): Map<String, Any?>? {
if (documentFile == null) return null

return mapOf(
"isDirectory" to documentFile.isDirectory,
"isFile" to documentFile.isFile,
"isVirtual" to documentFile.isVirtual,
"name" to (documentFile.name ?: ""),
"type" to (documentFile.type ?: ""),
"uri" to "${documentFile.uri}",
"exists" to "${documentFile.exists()}"
return createDocumentFileMap(
DocumentsContract.getDocumentId(documentFile.uri),
parentUri = documentFile.parentFile?.uri,
isDirectory = documentFile.isDirectory,
isFile = documentFile.isFile,
isVirtual = documentFile.isVirtual,
name = documentFile.name,
type = documentFile.type,
uri = documentFile.uri,
exists = documentFile.exists(),
size = documentFile.length(),
lastModified = documentFile.lastModified()
)
}


/**
* Standard map encoding of a row result of a `DocumentFile`
* ```kt
* Standard map encoding of a `DocumentFile` and must be used before returning any `DocumentFile`
* from plugin results, like:
* ```dart
* result.success(createDocumentFileMap(documentFile))
* ```
*
* Example:
* ```py
* input = {
* "last_modified": 2939496, # Key from DocumentsContract.Document.COLUMN_LAST_MODIFIED
* "_display_name": "MyFile" # Key from DocumentsContract.Document.COLUMN_DISPLAY_NAME
* }
*
* output = createCursorRowMap(input)
*
* print(output)
* {
* "lastModified": 2939496,
* "displayName": "MyFile"
* }
* ```
*/
fun createCursorRowMap(
parentUri: Uri,
fun createDocumentFileMap(
id: String?,
parentUri: Uri?,
isDirectory: Boolean?,
isFile: Boolean?,
isVirtual: Boolean?,
name: String?,
type: String?,
uri: Uri,
data: Map<String, Any>,
isDirectory: Boolean?
): Map<String, Any> {
val values = DocumentFileColumn.values()

val formattedData = mutableMapOf<String, Any>()

for (value in values) {
val key = parseDocumentFileColumn(value)

if (data[key] != null) {
formattedData[documentFileColumnToRawString(value)!!] = data[key]!!
}
}

exists: Boolean?,
size: Long?,
lastModified: Long?
): Map<String, Any?> {
return mapOf(
"data" to formattedData,
"metadata" to mapOf(
"parentUri" to "$parentUri",
"isDirectory" to isDirectory,
"uri" to "$uri"
)
"id" to id,
"parentUri" to "$parentUri",
"isDirectory" to isDirectory,
"isFile" to isFile,
"isVirtual" to isVirtual,
"name" to name,
"type" to type,
"uri" to "$uri",
"exists" to exists,
"size" to size,
"lastModified" to lastModified
)
}

Expand All @@ -130,7 +114,7 @@ fun traverseDirectoryEntries(
targetUri: Uri,
columns: Array<String>,
rootOnly: Boolean,
block: (data: Map<String, Any>, isLast: Boolean) -> Unit
block: (data: Map<String, Any?>, isLast: Boolean) -> Unit
): Boolean {
val documentId = try {
DocumentsContract.getDocumentId(targetUri)
Expand Down Expand Up @@ -158,7 +142,10 @@ fun traverseDirectoryEntries(
if (rootOnly) emptyArray() else arrayOf(DocumentsContract.Document.COLUMN_MIME_TYPE)

val intrinsicColumns =
arrayOf(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_FLAGS
)

val projection = arrayOf(
*columns,
Expand Down Expand Up @@ -215,11 +202,22 @@ fun traverseDirectoryEntries(
}

block(
createCursorRowMap(
parent,
uri,
data,
isDirectory = isDirectory
createDocumentFileMap(
parentUri = parent,
uri = uri,
name = data[DocumentsContract.Document.COLUMN_DISPLAY_NAME] as String?,
exists = true,
id = data[DocumentsContract.Document.COLUMN_DOCUMENT_ID] as String,
isDirectory = isDirectory == true,
isFile = isDirectory == false,
isVirtual = if (Build.VERSION.SDK_INT >= API_24) {
(data[DocumentsContract.Document.COLUMN_FLAGS] as Int and DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT) != 0
} else {
false
},
type = data[DocumentsContract.Document.COLUMN_MIME_TYPE] as String?,
size = data[DocumentsContract.Document.COLUMN_SIZE] as Long?,
lastModified = data[DocumentsContract.Document.COLUMN_LAST_MODIFIED] as Long?
),
dirNodes.isEmpty() && cursor.isLast
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ enum class DocumentFileColumn {

enum class DocumentFileColumnType {
LONG,
STRING
STRING,
INT
}

fun parseDocumentFileColumn(column: String): DocumentFileColumn? {
Expand Down Expand Up @@ -66,7 +67,8 @@ fun typeOfColumn(column: String): DocumentFileColumnType? {
DocumentsContract.Document.COLUMN_MIME_TYPE to DocumentFileColumnType.STRING,
DocumentsContract.Document.COLUMN_SIZE to DocumentFileColumnType.LONG,
DocumentsContract.Document.COLUMN_SUMMARY to DocumentFileColumnType.STRING,
DocumentsContract.Document.COLUMN_LAST_MODIFIED to DocumentFileColumnType.LONG
DocumentsContract.Document.COLUMN_LAST_MODIFIED to DocumentFileColumnType.LONG,
DocumentsContract.Document.COLUMN_FLAGS to DocumentFileColumnType.INT
)

return values[column]
Expand All @@ -76,5 +78,6 @@ fun cursorHandlerOf(type: DocumentFileColumnType): (Cursor, Int) -> Any {
when(type) {
DocumentFileColumnType.LONG -> { return { cursor, index -> cursor.getLong(index) } }
DocumentFileColumnType.STRING -> { return { cursor, index -> cursor.getString(index) } }
DocumentFileColumnType.INT -> { return { cursor, index -> cursor.getInt(index) } }
}
}
Loading