From 8e284ccc94387c6724a238dcb9353ea496c79753 Mon Sep 17 00:00:00 2001 From: euxane Date: Wed, 18 Sep 2024 23:41:13 +0200 Subject: storageprovider: notify file browsers of file creation and deletion --- .../pacien/tincapp/storageprovider/BrowseFilesIntents.kt | 12 ++++++++---- .../tincapp/storageprovider/FilesDocumentsProvider.kt | 14 +++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'app') diff --git a/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt b/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt index 3ebd501..4705c0b 100644 --- a/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt +++ b/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt @@ -28,10 +28,14 @@ import org.pacien.tincapp.BuildConfig object BrowseFilesIntents { private const val URI_AUTHORITY = BuildConfig.APPLICATION_ID + ".files" - fun openDocumentTree(context: Context, documentId: String) { - val contentUri = DocumentsContract.buildDocumentUri(URI_AUTHORITY, documentId) - openDocumentTree(context, contentUri) - } + fun documentUri(documentId: String) = + DocumentsContract.buildDocumentUri(URI_AUTHORITY, documentId) + + fun childDocumentsUri(parentDocumentId: String) = + DocumentsContract.buildChildDocumentsUri(URI_AUTHORITY, parentDocumentId) + + fun openDocumentTree(context: Context, documentId: String) = + openDocumentTree(context, documentUri(documentId)) fun openDocumentTree(context: Context, contentUri: Uri) { val intent = Intent(Intent.ACTION_VIEW).apply { diff --git a/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt b/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt index 8b8e2a7..688ae27 100644 --- a/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt +++ b/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt @@ -22,6 +22,7 @@ import android.os.CancellationSignal import android.os.ParcelFileDescriptor import android.database.Cursor import android.database.MatrixCursor +import android.net.Uri import android.os.Build import android.provider.DocumentsContract import android.provider.DocumentsContract.Document @@ -92,13 +93,14 @@ class FilesDocumentsProvider : DocumentsProvider() { sortOrder: String? ): Cursor = MatrixCursor(projection ?: DEFAULT_DOCUMENT_PROJECTION).apply { + setNotificationUrl(BrowseFilesIntents.childDocumentsUri(parentDocumentId!!)) when (parentDocumentId) { ROOT_DOCUMENT_ID -> { addVirtualDirRow(VIRTUAL_ROOT_NETWORKS, Document.FLAG_DIR_SUPPORTS_CREATE) addVirtualDirRow(VIRTUAL_ROOT_LOG, Document.FLAG_DIR_SUPPORTS_CREATE) } - else -> fileForDocumentId(parentDocumentId!!).listFiles()?.forEach { + else -> fileForDocumentId(parentDocumentId).listFiles()?.forEach { addFileRow(documentIdForFile(it), it) } } @@ -126,6 +128,7 @@ class FilesDocumentsProvider : DocumentsProvider() { override fun deleteDocument(documentId: String?) { fileForDocumentId(documentId!!).apply { if (!deleteRecursively()) throw FileSystemException(this) + notifyChange(BrowseFilesIntents.childDocumentsUri(documentIdForFile(parentFile))) } } @@ -150,6 +153,7 @@ class FilesDocumentsProvider : DocumentsProvider() { else -> createNewFile() } if (!success) throw FileSystemException(this) + notifyChange(BrowseFilesIntents.childDocumentsUri(parentDocumentId)) }.let { documentIdForFile(it) } @@ -223,4 +227,12 @@ class FilesDocumentsProvider : DocumentsProvider() { val row = newRow() pairs.forEach { row.add(it.first, it.second) } } + + private fun MatrixCursor.setNotificationUrl(uri: Uri) { + setNotificationUri(context!!.contentResolver, uri) + } + + private fun notifyChange(uri: Uri) { + context!!.contentResolver.notifyChange(uri, null) + } } \ No newline at end of file -- cgit v1.2.3