2

Is it possible to override File.delete() function? Whenever i delete any file from sdcard from other apps (not using by my application) i need a notification like this file is going to be delete. A sample code snippet i tried is,

public class ExtendFile extends File
{
    public ExtendFile(File dir, String name) 
    {
         super(dir, name);
         // TODO Auto-generated constructor stub
    }

    @Override
    public boolean delete() 
    {    
        System.out.println("to be deleted");
        return super.delete();
     }
}  

and

File file = new File("/mnt/sdcard/tmp.txt");
ExtendFile f = new ExtendFile(file, tempPath);

i implemented above two statements in a service class in FileObsever. Now i deleted tmp.txt file from other app, but i didn't get any notification. How can i get the notifications from other apps ? Is it possible or not ?

Thanks in advance

user2791281
  • 149
  • 3
  • 12
  • Why do you need to `@Override` it? Can't you put the delete part in a Thread and run this Thread only if the user confirms the deletion (via a Dialog confirmation for example)? – g00dy Sep 19 '13 at 07:12
  • @g00dy Thanks for your reply. First i implemented like that (using dialog box confirmation) only. But its working only for my own application not for other apps.How can i get the dialog box confirmation from other apps ? – user2791281 Sep 19 '13 at 07:18
  • Using a fileObserver (see my answer), you are now only influencing your own code by wrapping File. – RvdK Sep 19 '13 at 07:25
  • The fileObserver is an option, as mentioned, but it doesn't solve the "issue" you have, becasue it's simply not possible. For this case, you would want to write an Android Mod, but that's a lot of work for the thing you want. – g00dy Sep 19 '13 at 07:37

1 Answers1

3

No, this is not possible. Not only will this cause problems with other applications but also with the Android OS.

With the FileObserver you can only see what has happened to a file. You cannot influence it. To conclude: If you only want to know what has happened to "/mnt/sdcard/tmp.txt" then use a FileObserver, but you cannot prevent deletion.

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • Thanks for the reply.I am trying to do a application like [Dumpster(Recycle bin for android)](http://www.dumpsterapp.mobi/).I tried with Fileobserver and Content observer to get notifications, but those are giving notifications after file deletion only. Thats why i tried to override File.delete() function . How can i get alert/notification whenever user is going to delete any file from sdcard ? – user2791281 Sep 19 '13 at 07:26
  • As you said that we can store the name and path of the file that is deleted. But How to get the physical address of the corresponding file in android? – user2791281 Sep 19 '13 at 07:47
  • Hmm I looked into the obfuscated code, they are using the .trash directory. So disregard my previous comment. – RvdK Sep 19 '13 at 07:59