Search code examples
androidstoragestorage-access-framework

Is there some way to listen for changes under a tree URI on SAF?


My use-case is pretty simple:

  1. List files under a selected folder.
  2. Listen for any changes over this folder (deletion, renaming, creation, etc.). So, for instance, even if the user deletes a child file A using a third-party app (e.g the OS file manager) my app will be able to catch the event and delete the file reference, to avoid ghost files in my recycler view.

To list the files under a selected folder, which is a simple tree URI that my app gains access through openDocumentTree, I'm already using SAF, I can use the tree URI to delete, rename, move child documents, etc. so permission is not a problem here.

But I am not able to find any resource pointing to some way to do the "listening" part. I've already:

I found and tried some SO questions about it (a bit 'older' ~ 3 years), which also doesn't have answers yet:

My question is, do we have access to something that allow us to listen for changes over a folder that we've permission over?

I'm just about to give-up this and add MANAGE_EXTERNAL_STORAGE permission and use the FileObserver API, and if it does not work either, I think I'll just start brute-forcing the user storage searching for changes foff.


Solution

  • As commonsware said, that's not possible. I think the best I can do is just handle when the user try some action in an file that no longer exists by showing a warn then also deleting the file reference from my app.