Skip to content

Commit

Permalink
Feat[folder_provider]: notify the file manager on file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Dec 12, 2024
1 parent e71be1e commit 011139d
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 011139d

Please sign in to comment.