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";
}
}
Lets clarify the things First.
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. 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.
- Create the inotify instance by
inotify_init
().- Add all the directories to be monitored to the
inotify
list usinginotify_add_watch
() function.- 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.- 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.- 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With