androidbootmountfileobserver

Elegant way to use Android FileObserver on booting the device


I'm developing an application which starts automatically when the device boots and it needs to monitorize some special directories in the memory of the device, for this I use FileObservers, a snippet of my code is:

 if (folder == null)
    throw new FailedExecutionException(
            "Trying to check the limit of a null directory");

Log.d(TAG, "Setting a limit for " + folder.getAbsolutePath());

if (!folder.isDirectory())
    throw new FailedExecutionException(
            "FolderLimit should be checked on directories, "
                    + folder.getAbsolutePath() + " is not a directory");

    //Then create the FileObserver...

When I launch this application with the device running it works, the partition where is the folder that I'm observing is mounted and I can monitorize it, the problem is when I reboot the device, then this code is executed before the system mounts the partition and it doesn't recognize it as a folder:

com.mycompany.android.helpers.util.FailedExecutionException: FolderLimit should be checked on directories, /mnt/sdcard1/mycompany/photo/white_list is not a directory

Of course I can do some kind of while(!created){ attempt() } but I want to ask if there is a more elegant way to notify this application that the partitions are already mounted and the system is ready.

What I have if I execute mount in the shell is:

127|root@android:/ # mount | busybox grep sdcard1                              
/dev/block/vold/179:4 /mnt/sdcard1 vfat        rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime= 0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
tmpfs /mnt/sdcard1/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0

Thank you

EDIT I forgot to say, I can't use Environment.getExternalStorage() for this, it is a modified device and this is a partition which is not guaranteed to be the external storage path in the release version


Solution

  • Finally I did this by brute force...

    private void attemptToCreateFolderLimit(final File file,
        final long limitCount, final long recycle) {
    
    ThreadFactory.startNewThread(TAG, new Runnable() {
    
        @Override
        public void run() {
        for (int i = 0; i < FOLDER_LIMIT_CREATION_ATTEMPTS; i++) {
            Log.d(TAG,
                "creating folder limit for "
                    + file.getAbsoluteFile());
    
            try {
            FolderLimit limit = new FolderLimit(file, limitCount,
                recycle);
            folderLimits.add(limit);
            } catch (FailedExecutionException e) {
            Log.e(TAG, "Limit not created");
            Log.e(TAG, Log.getStackTraceString(e));
            }
            try {
            Thread.sleep(FOLDER_LIMIT_TIME_BETWEEN_ATTEMPTS);
            } catch (InterruptedException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            }
    
        }
        }
    });
    
    }