mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-02-15 16:59:18 +01:00
Merge pull request #12461 from blakbin/DocumentProvider
Improve DocumentProvider
This commit is contained in:
commit
2a89a1d27a
@ -9,8 +9,11 @@
|
|||||||
package org.dolphinemu.dolphinemu.features
|
package org.dolphinemu.dolphinemu.features
|
||||||
|
|
||||||
import android.annotation.TargetApi
|
import android.annotation.TargetApi
|
||||||
|
import android.content.res.AssetFileDescriptor
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.database.MatrixCursor
|
import android.database.MatrixCursor
|
||||||
|
import android.graphics.Point
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.CancellationSignal
|
import android.os.CancellationSignal
|
||||||
import android.os.ParcelFileDescriptor
|
import android.os.ParcelFileDescriptor
|
||||||
@ -27,7 +30,7 @@ class DocumentProvider : DocumentsProvider() {
|
|||||||
private var rootDirectory: File? = null
|
private var rootDirectory: File? = null
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
public const val ROOT_ID = "root"
|
const val ROOT_ID = "root"
|
||||||
|
|
||||||
private val DEFAULT_ROOT_PROJECTION = arrayOf(
|
private val DEFAULT_ROOT_PROJECTION = arrayOf(
|
||||||
DocumentsContract.Root.COLUMN_ROOT_ID,
|
DocumentsContract.Root.COLUMN_ROOT_ID,
|
||||||
@ -93,6 +96,8 @@ class DocumentProvider : DocumentsProvider() {
|
|||||||
appendDocument(file, result)
|
appendDocument(file, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.setNotificationUri(context!!.contentResolver, DocumentsContract.buildChildDocumentsUri(
|
||||||
|
"${context!!.packageName}.user", parentDocumentId))
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,6 +112,16 @@ class DocumentProvider : DocumentsProvider() {
|
|||||||
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.parseMode(mode))
|
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(
|
override fun createDocument(
|
||||||
parentDocumentId: String,
|
parentDocumentId: String,
|
||||||
mimeType: String,
|
mimeType: String,
|
||||||
@ -121,6 +136,7 @@ class DocumentProvider : DocumentsProvider() {
|
|||||||
} else {
|
} else {
|
||||||
file.createNewFile()
|
file.createNewFile()
|
||||||
}
|
}
|
||||||
|
refreshDocument(parentDocumentId)
|
||||||
return pathToDocumentId(file)
|
return pathToDocumentId(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +144,9 @@ class DocumentProvider : DocumentsProvider() {
|
|||||||
rootDirectory ?: return
|
rootDirectory ?: return
|
||||||
|
|
||||||
val file = documentIdToPath(documentId)
|
val file = documentIdToPath(documentId)
|
||||||
|
val fileParent = file.parentFile
|
||||||
file.deleteRecursively()
|
file.deleteRecursively()
|
||||||
|
refreshDocument(pathToDocumentId(fileParent!!))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun renameDocument(documentId: String, displayName: String): String? {
|
override fun renameDocument(documentId: String, displayName: String): String? {
|
||||||
@ -137,9 +155,19 @@ class DocumentProvider : DocumentsProvider() {
|
|||||||
val file = documentIdToPath(documentId)
|
val file = documentIdToPath(documentId)
|
||||||
val dest = findFileNameForNewFile(File(file.parentFile, displayName))
|
val dest = findFileNameForNewFile(File(file.parentFile, displayName))
|
||||||
file.renameTo(dest)
|
file.renameTo(dest)
|
||||||
|
refreshDocument(pathToDocumentId(file.parentFile!!))
|
||||||
return pathToDocumentId(dest)
|
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
|
override fun isChildDocument(parentDocumentId: String, documentId: String): Boolean
|
||||||
= documentId.startsWith(parentDocumentId)
|
= documentId.startsWith(parentDocumentId)
|
||||||
|
|
||||||
@ -161,6 +189,10 @@ class DocumentProvider : DocumentsProvider() {
|
|||||||
} else {
|
} else {
|
||||||
file.name
|
file.name
|
||||||
}
|
}
|
||||||
|
val mimeType = getTypeForFile(file)
|
||||||
|
if (file.exists() && mimeType.startsWith("image/")) {
|
||||||
|
flags = flags or DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL
|
||||||
|
}
|
||||||
cursor.newRow().apply {
|
cursor.newRow().apply {
|
||||||
add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, pathToDocumentId(file))
|
add(DocumentsContract.Document.COLUMN_DOCUMENT_ID, pathToDocumentId(file))
|
||||||
add(DocumentsContract.Document.COLUMN_MIME_TYPE, getTypeForFile(file))
|
add(DocumentsContract.Document.COLUMN_MIME_TYPE, getTypeForFile(file))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user