Merge pull request #12461 from blakbin/DocumentProvider

Improve DocumentProvider
This commit is contained in:
JosJuice 2023-12-30 12:50:46 +01:00 committed by GitHub
commit 2a89a1d27a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 1 deletions

View File

@ -9,8 +9,11 @@
package org.dolphinemu.dolphinemu.features
import android.annotation.TargetApi
import android.content.res.AssetFileDescriptor
import android.database.Cursor
import android.database.MatrixCursor
import android.graphics.Point
import android.net.Uri
import android.os.Build
import android.os.CancellationSignal
import android.os.ParcelFileDescriptor
@ -27,7 +30,7 @@ class DocumentProvider : DocumentsProvider() {
private var rootDirectory: File? = null
companion object {
public const val ROOT_ID = "root"
const val ROOT_ID = "root"
private val DEFAULT_ROOT_PROJECTION = arrayOf(
DocumentsContract.Root.COLUMN_ROOT_ID,
@ -93,6 +96,8 @@ class DocumentProvider : DocumentsProvider() {
appendDocument(file, result)
}
}
result.setNotificationUri(context!!.contentResolver, DocumentsContract.buildChildDocumentsUri(
"${context!!.packageName}.user", parentDocumentId))
return result
}
@ -107,6 +112,16 @@ class DocumentProvider : DocumentsProvider() {
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.parseMode(mode))
}
override fun openDocumentThumbnail(
documentId: String,
sizeHint: Point,
signal: CancellationSignal
): AssetFileDescriptor {
val file = documentIdToPath(documentId)
val pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
return AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH)
}
override fun createDocument(
parentDocumentId: String,
mimeType: String,
@ -121,6 +136,7 @@ class DocumentProvider : DocumentsProvider() {
} else {
file.createNewFile()
}
refreshDocument(parentDocumentId)
return pathToDocumentId(file)
}
@ -128,7 +144,9 @@ class DocumentProvider : DocumentsProvider() {
rootDirectory ?: return
val file = documentIdToPath(documentId)
val fileParent = file.parentFile
file.deleteRecursively()
refreshDocument(pathToDocumentId(fileParent!!))
}
override fun renameDocument(documentId: String, displayName: String): String? {
@ -137,9 +155,19 @@ class DocumentProvider : DocumentsProvider() {
val file = documentIdToPath(documentId)
val dest = findFileNameForNewFile(File(file.parentFile, displayName))
file.renameTo(dest)
refreshDocument(pathToDocumentId(file.parentFile!!))
return pathToDocumentId(dest)
}
private fun refreshDocument(parentDocumentId: String) {
val parentUri: Uri =
DocumentsContract.buildChildDocumentsUri(
"${context!!.packageName}.user",
parentDocumentId
)
context!!.contentResolver.notifyChange(parentUri, null)
}
override fun isChildDocument(parentDocumentId: String, documentId: String): Boolean
= documentId.startsWith(parentDocumentId)
@ -161,6 +189,10 @@ class DocumentProvider : DocumentsProvider() {
} else {
file.name
}
val mimeType = getTypeForFile(file)
if (file.exists() && mimeType.startsWith("image/")) {
flags = flags or DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL
}
cursor.newRow().apply {
add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, pathToDocumentId(file))
add(DocumentsContract.Document.COLUMN_MIME_TYPE, getTypeForFile(file))