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

天涯浪子 提交于 2019-12-05 05:48:00

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!