From 011139db38361334fd5ffc2f9096b557e01f5db2 Mon Sep 17 00:00:00 2001 From: artdeell Date: Thu, 12 Dec 2024 12:32:18 +0300 Subject: [PATCH] Feat[folder_provider]: notify the file manager on file changes --- .../pojavlaunch/scoped/FolderProvider.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/scoped/FolderProvider.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/scoped/FolderProvider.java index 5bd56abeef..e2286e6307 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/scoped/FolderProvider.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/scoped/FolderProvider.java @@ -1,10 +1,12 @@ package net.kdt.pojavlaunch.scoped; import android.annotation.TargetApi; +import android.content.ContentResolver; import android.content.res.AssetFileDescriptor; import android.database.Cursor; import android.database.MatrixCursor; import android.graphics.Point; +import android.net.Uri; import android.os.CancellationSignal; import android.os.ParcelFileDescriptor; import android.provider.DocumentsContract; @@ -47,6 +49,9 @@ public class FolderProvider extends DocumentsProvider { private static final File BASE_DIR = new File(Tools.DIR_GAME_HOME); + private ContentResolver mContentResolver; + + private String mStorageProviderAuthortiy; // The default columns to return information about a root if no specific // columns are requested in a query. @@ -97,6 +102,8 @@ public Cursor queryRoots(String[] projection) { @Override public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException { final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); + // Future-proofing in case if we implement realtime file watching + result.setNotificationUri(mContentResolver, createUriForDocId(documentId)); includeFile(result, documentId, null); return result; } @@ -110,6 +117,8 @@ public Cursor queryChildDocuments(String parentDocumentId, String[] projection, for (File file : children) { includeFile(result, null, file); } + // Set the notification URI as that's what the "Files" app will be listening to in case of file deletion + result.setNotificationUri(mContentResolver, createUriForDocId(parentDocumentId)); return result; } @@ -129,6 +138,8 @@ public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHi @Override public boolean onCreate() { + mContentResolver = getContext().getContentResolver(); + mStorageProviderAuthortiy = getContext().getString(R.string.storageProviderAuthorities); return true; } @@ -152,6 +163,8 @@ public String createDocument(String parentDocumentId, String mimeType, String di } catch (IOException e) { throw new FileNotFoundException("Failed to create document with id " + newFile.getPath()); } + // Notify the file manager that the parent directory has changed + notifyChange(createUriForDocId(parentDocumentId)); return newFile.getPath(); } @@ -196,6 +209,8 @@ public void deleteDocument(String documentId) throws FileNotFoundException { throw new FileNotFoundException("Failed to delete document with id " + documentId); } } + // Notify the file manager that the parent directory has changed + notifyChange(createUriForFile(file.getParentFile())); } @Override @@ -338,4 +353,16 @@ public DocumentsContract.Path findDocumentPath(@Nullable String parentDocumentId Log.i("FolderProvider", pathIds.toString()); return new DocumentsContract.Path(getDocIdForFile(source), pathIds); } + + private Uri createUriForDocId(String documentId) throws FileNotFoundException { + return createUriForFile(getFileForDocId(documentId)); + } + + private Uri createUriForFile(File file) { + return DocumentsContract.buildDocumentUri(mStorageProviderAuthortiy, file.getAbsolutePath()); + } + + private void notifyChange(Uri uri) { + mContentResolver.notifyChange(uri, null); + } } \ No newline at end of file