Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get notification of any file being deleted from SD card

I want to create Dumpster like app, for this I want notification when user is deleting any file so that I can save it to my app memory.

I used File Observer but it is giving notification after file deletion and in marshmallow it does not notify for deletion also. I referred this link for file observer. Somewhere I read it is possible using native programming language (C), but couldn't get any solution. How can I do this? Thanks in advance.

I have tried this:

@Override
        public void onEvent(int event, String path) {
            if (path == null) {
                return;
            }
            //the monitored file or directory was deleted, monitoring effectively stops
            if ((FileObserver.DELETE_SELF & event)!=0) {
                FileAccessLogStatic.accessLogMsg += absolutePath + "/" + " is deleted\n";
            }       
        }
like image 919
Divy Soni Avatar asked Jun 23 '17 11:06

Divy Soni


1 Answers

Lets clarify the things First.

  1. Dumpster Uses .trash directory which may and may not be present always. This should be noted that Dumpster does not run correctly in many devices as it can be seen through google reviews.
  2. Dumpster uses (i guessed it from the code for educational purpose only) it's own System File Handler that uses a service to check for the onClick event and if its a file onClick it saves the File as well as its path to a separate folder (usually hidden) and also saves it in a database that is local. If it is deleted you know where the file is and if not lets delete that file from that hidden folder. Well that's kinda not worth the pain because you need to almost make your service run for almost all the time which uses CPU resources. It also runs on rooted devices but why root device for this purpose only.
  3. As the Security in devices are increasing it is becoming less possible to perform these tasks. As latest of 1-09-2017 all these of files recycle bin have Negative reviews on latest android versions. Hence, proving my point.

    FileObserver uses the concept for checking any changes on the file or even directories but you cannot influence it meaning you cannot prevent deletion it will notify everything after the user has deleted.

    inotify.h it is the used for NDK purposes for creating application using to check the events on folders and files but if the folder is mentioned the sub sub folder will not be covered in this or notify you any change for the file. Moreover the concept used in inotify is same as FileObserver. you can only receive the notification after the file is deleted. The code used in the inotify is something like this.

  1. Create the inotify instance by inotify_init().
  2. Add all the directories to be monitored to the inotify list using inotify_add_watch() function.
  3. To determine the events occurred, do the read() on the inotify instance. This read will get blocked till the change event occurs. It is recommended to perform selective read on this inotify instance using select() call.
  4. Read returns list of events occurred on the monitored directories. Based on the return value of read(), we will know exactly what kind of changes occurred.
  5. In case of removing the watch on directories / files, call inotify_rm_watch().

The two methods present in inotify is as follow:

IN_DELETE – File/directory deleted from watched directory

IN_DELETE_SELF – Watched file/directory was itself deleted

both of which are almost same as in FileObserver

  1. This Solution can be of help not fully but still can help in creating any Dumpster type Application. It can be said that you need to create your own File Manager where you can Create your own Custom FileV2(Just a cool name File version 2.0) class that extends File and you can override the delete method (and all others) as you like. You can create a custom pop up saying do you want to delete the file with your own backing up the file on yes and dismissing the pop up on no. (Make Sure User uses this File Manager to Delete otherwise it will not work because overriding the system File delete() will just mess up other applications as well).

    class filev2 extends File {
    
    public filev2(@NonNull String pathname) {
        super(pathname);
    }
    
    public filev2(@NonNull URI uri) {
        super(uri);
    }
    
    @Override
    public boolean delete() {
     //   return super.delete();
    //Do as you want and return the boolean.
    }
    
    }
    

But make sure your files will be saved if the user uses your File Manager for this. You can set the intent-filters for the task so that your FileManager comes in ACTION_VIEW for that matter.

Last but I am not sure about this maybe registerContentObserver can be used also. (Not Sure though)

Sources:

Inotify.h help website

registerContentObserver help

Kind of Similar Question

FileObserver Help

Linux Help Deleted Log for Files

I hope it helps and I hope you can now have a start to what you want.

like image 54
Sahil Avatar answered Nov 16 '22 04:11

Sahil