From 5bd0dcd564e377cbcd3ef956208acc4bddb6d0a1 Mon Sep 17 00:00:00 2001 From: biroder <107300789+biroder@users.noreply.github.com> Date: Fri, 3 Mar 2023 17:46:00 +0200 Subject: [PATCH] DocumentsProvider refreshing after changing files (#197) * DocumentsProvider refreshing after changing files * Add a flags for notifyChange function --- .../DoukutsuDocumentsProvider.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/app/app/src/main/java/io/github/doukutsu_rs/DoukutsuDocumentsProvider.java b/app/app/src/main/java/io/github/doukutsu_rs/DoukutsuDocumentsProvider.java index d06b8fc..9802e54 100644 --- a/app/app/src/main/java/io/github/doukutsu_rs/DoukutsuDocumentsProvider.java +++ b/app/app/src/main/java/io/github/doukutsu_rs/DoukutsuDocumentsProvider.java @@ -1,11 +1,14 @@ package io.github.doukutsu_rs; +import android.content.ContentResolver; import android.database.Cursor; import android.database.MatrixCursor; import android.database.MatrixCursor.RowBuilder; +import android.net.Uri; import android.os.Build; import android.os.CancellationSignal; import android.os.ParcelFileDescriptor; +import android.provider.DocumentsContract; import android.provider.DocumentsContract.Document; import android.provider.DocumentsContract.Root; import android.provider.DocumentsProvider; @@ -98,7 +101,9 @@ public class DoukutsuDocumentsProvider extends DocumentsProvider { pushFile(result, file); } } - + + result.setNotificationUri(getContext().getContentResolver(), DocumentsContract.buildDocumentUri(BuildConfig.DOCUMENTS_AUTHORITY, parentDocumentId)); + return result; } @@ -137,7 +142,16 @@ public class DoukutsuDocumentsProvider extends DocumentsProvider { } catch (IOException e) { throw new FileNotFoundException("Couldn't create file: " + e.getMessage()); } - + + Uri uri = DocumentsContract.buildDocumentUri(BuildConfig.DOCUMENTS_AUTHORITY, file.getParent()); + + if (SDK_INT >= Build.VERSION_CODES.R) { + getContext().getContentResolver().notifyChange(uri, null, ContentResolver.NOTIFY_INSERT); + } else { + getContext().getContentResolver().notifyChange(uri, null); + } + + return file.getAbsolutePath(); } @@ -150,8 +164,15 @@ public class DoukutsuDocumentsProvider extends DocumentsProvider { } deleteRecursive(file); - // todo refresh this shit - // getContext().getContentResolver().refresh() + + + Uri uri = DocumentsContract.buildDocumentUri(BuildConfig.DOCUMENTS_AUTHORITY, file.getParent()); + + if (SDK_INT >= Build.VERSION_CODES.R) { + getContext().getContentResolver().notifyChange(uri, null, ContentResolver.NOTIFY_DELETE); + } else { + getContext().getContentResolver().notifyChange(uri, null); + } } @Override @@ -201,6 +222,15 @@ public class DoukutsuDocumentsProvider extends DocumentsProvider { throw new FileNotFoundException(e.getMessage()); } + + Uri uri = DocumentsContract.buildDocumentUri(BuildConfig.DOCUMENTS_AUTHORITY, file.getParent()); + + if (SDK_INT >= Build.VERSION_CODES.R) { + getContext().getContentResolver().notifyChange(uri, null, ContentResolver.NOTIFY_UPDATE); + } else { + getContext().getContentResolver().notifyChange(uri, null); + } + return newPath.getAbsolutePath(); }