Example usage for android.os FileObserver FileObserver

List of usage examples for android.os FileObserver FileObserver

Introduction

In this page you can find the example usage for android.os FileObserver FileObserver.

Prototype

public FileObserver(String path, int mask) 

Source Link

Document

Create a new file observer for a certain file or directory.

Usage

From source file:fr.umlv.lastproject.smart.browser.FileLoader.java

@Override
protected void onStartLoading() {
    if (mData != null) {
        deliverResult(mData);//from   ww  w .  ja va2  s .com
    }

    if (mFileObserver == null) {
        mFileObserver = new FileObserver(mPath, FILE_OBSERVER_MASK) {
            @Override
            public void onEvent(int event, String path) {
                onContentChanged();
            }
        };
    }
    mFileObserver.startWatching();

    if (takeContentChanged() || mData == null) {
        forceLoad();
    }
}

From source file:answer.example.answer.view.FileLoader.java

@Override
protected void onStartLoading() {
    if (mData != null)
        deliverResult(mData);/*from  ww w. ja v a2  s. c  o m*/

    if (mFileObserver == null) {
        mFileObserver = new FileObserver(mPath, FILE_OBSERVER_MASK) {
            @Override
            public void onEvent(int event, String path) {
                onContentChanged();
            }
        };
    }
    mFileObserver.startWatching();

    if (takeContentChanged() || mData == null)
        forceLoad();
}

From source file:com.nononsenseapps.filepicker.local.LocalFilePickerFragment.java

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes.//w  w  w .ja  va 2  s  .com
 */
@Override
protected Loader<List<FileSystemObjectInterface>> getLoader() {
    return new AsyncTaskLoader<List<FileSystemObjectInterface>>(getActivity()) {
        FileObserver fileObserver;

        @Override
        public List<FileSystemObjectInterface> loadInBackground() {
            ArrayList<FileSystemObjectInterface> files = new ArrayList<FileSystemObjectInterface>();
            File[] listFiles = ((LocalFileSystemObject) currentPath).getFile().listFiles();

            if (listFiles != null) {
                for (java.io.File f : listFiles) {
                    if ((mode == SelectionMode.MODE_FILE || mode == SelectionMode.MODE_FILE_AND_DIR)
                            || f.isDirectory()) {
                        LocalFileSystemObject obj = new LocalFileSystemObject(f);
                        files.add(obj);
                    }
                }
            }
            return files;
        }

        /**
         * Handles a request to start the Loader.
         */
        @Override
        protected void onStartLoading() {
            super.onStartLoading();

            // handle if directory does not exist. Fall back to root.
            if (currentPath == null || !currentPath.isDir()) {
                currentPath = getRoot();
            }

            // Start watching for changes
            fileObserver = new FileObserver(currentPath.getFullPath(), FileObserver.CREATE | FileObserver.DELETE
                    | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {
                @Override
                public void onEvent(int event, String path) {
                    // Reload
                    onContentChanged();
                }
            };
            fileObserver.startWatching();

            forceLoad();
        }

        /**
         * Handles a request to completely reset the Loader.
         */
        @Override
        protected void onReset() {
            super.onReset();

            // Stop watching
            if (fileObserver != null) {
                fileObserver.stopWatching();
                fileObserver = null;
            }
        }
    };
}

From source file:org.arakhne.afc.ui.android.filechooser.AsyncFileLoader.java

/**
 * {@inheritDoc}/*w  ww. j  a v a 2  s . c om*/
 */
@Override
protected void onStartLoading() {
    // A list is already available. Publish it.
    if (this.discoveredFiles != null)
        deliverResult(this.discoveredFiles);

    if (this.fileObserver == null) {
        this.fileObserver = new FileObserver(this.path.getAbsolutePath(),
                FileObserver.CREATE | FileObserver.DELETE | FileObserver.DELETE_SELF | FileObserver.MOVED_FROM
                        | FileObserver.MOVED_TO | FileObserver.MODIFY | FileObserver.MOVE_SELF) {
            @Override
            public void onEvent(int event, String path) {
                onContentChanged();
            }
        };
    }
    this.fileObserver.startWatching();

    if (takeContentChanged() || this.discoveredFiles == null)
        forceLoad();
}

From source file:org.fourthline.android.feeds.filechooser.FileLoader.java

@Override
protected void onStartLoading() {
    if (data != null)
        deliverResult(data);// w w w  .java2  s  .c o m

    if (fileObserver == null) {
        fileObserver = new FileObserver(path, FILE_OBSERVER_MASK) {
            @Override
            public void onEvent(int event, String path) {
                onContentChanged();
            }
        };
    }
    fileObserver.startWatching();

    if (takeContentChanged() || data == null)
        forceLoad();
}

From source file:org.protocoder.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forfragments);
    c = this;//from   w ww .  j  a  v a  2s. com

    // Create the action bar programmatically

    if (!isWear()) {

        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        setSupportActionBar(toolbar);

        //ActionBar actionBar = getSupportActionBar();
        //actionBar.setHomeButtonEnabled(true);
    }
    mProtocoder = Protocoder.getInstance(this);
    mProtocoder.init();

    /*
     *  Views
     */

    if (savedInstanceState == null) {
        addFragments();
    } else {
        mProtocoder.protoScripts.reinitScriptList();
    }

    // Check when a file is changed in the protocoder dir
    observer = new FileObserver(ProjectManager.FOLDER_USER_PROJECTS,
            FileObserver.CREATE | FileObserver.DELETE) {

        @Override
        public void onEvent(int event, String file) {
            if ((FileObserver.CREATE & event) != 0) {

                MLog.d(TAG, "File created [" + ProjectManager.FOLDER_USER_PROJECTS + "/" + file + "]");

                // check if its a "create" and not equal to probe because
                // thats created every time camera is
                // launched
            } else if ((FileObserver.DELETE & event) != 0) {
                MLog.d(TAG, "File deleted [" + ProjectManager.FOLDER_USER_PROJECTS + "/" + file + "]");

            }
        }
    };

    connectivityChangeReceiver = new ConnectivityChangeReceiver();

}

From source file:com.glasshack.checkmymath.CheckMyMath.java

private void processPictureWhenReady(final String picturePath) {
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {
        // The picture is ready; process it.
        Log.e("Picture Path", picturePath);
        List<NameValuePair> postData = new ArrayList<NameValuePair>();
        postData.add(new BasicNameValuePair("file", picturePath));
        postData.add(new BasicNameValuePair("answer", "1"));
        postData.add(new BasicNameValuePair("submit", "Submit"));
        String postResp = post("http://54.187.58.53/glassmath.php", postData);
        evaluateResponse(postResp);//from   w w w .ja v a 2s  .c  om
    } else {
        // The file does not exist yet. Before starting the file observer, you
        // can update your UI to let the user know that the application is
        // waiting for the picture (for example, by displaying the thumbnail
        // image and a progress indicator).

        final File parentDirectory = pictureFile.getParentFile();
        FileObserver observer = new FileObserver(parentDirectory.getPath(),
                FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
            // Protect against additional pending events after CLOSE_WRITE
            // or MOVED_TO is handled.
            private boolean isFileWritten;

            @Override
            public void onEvent(int event, String path) {
                if (!isFileWritten) {
                    // For safety, make sure that the file that was created in
                    // the directory is actually the one that we're expecting.
                    File affectedFile = new File(parentDirectory, path);
                    isFileWritten = affectedFile.equals(pictureFile);

                    if (isFileWritten) {
                        stopWatching();

                        // Now that the file is ready, recursively call
                        // processPictureWhenReady again (on the UI thread).
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                processPictureWhenReady(picturePath);
                            }
                        });
                    }
                }
            }
        };
        observer.startWatching();
    }
}

From source file:com.ihelp101.instagram.FilePickerFragment.java

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes.//from   www . jav  a2  s  . c om
 */
@Override
public Loader<SortedList<File>> getLoader() {
    return new AsyncTaskLoader<SortedList<File>>(getActivity()) {

        FileObserver fileObserver;

        @Override
        public SortedList<File> loadInBackground() {
            File[] listFiles = mCurrentPath.listFiles();
            final int initCap = listFiles == null ? 0 : listFiles.length;

            SortedList<File> files = new SortedList<>(File.class,
                    new SortedListAdapterCallback<File>(getDummyAdapter()) {
                        @Override
                        public int compare(File lhs, File rhs) {
                            return compareFiles(lhs, rhs);
                        }

                        @Override
                        public boolean areContentsTheSame(File file, File file2) {
                            return file.getAbsolutePath().equals(file2.getAbsolutePath())
                                    && (file.isFile() == file2.isFile());
                        }

                        @Override
                        public boolean areItemsTheSame(File file, File file2) {
                            return areContentsTheSame(file, file2);
                        }
                    }, initCap);

            files.beginBatchedUpdates();
            if (listFiles != null) {
                for (File f : listFiles) {
                    if (isItemVisible(f)) {
                        files.add(f);
                    }
                }
            }
            files.endBatchedUpdates();

            return files;
        }

        /**
         * Handles a request to start the Loader.
         */
        @Override
        protected void onStartLoading() {
            super.onStartLoading();

            // handle if directory does not exist. Fall back to root.
            if (mCurrentPath == null || !mCurrentPath.isDirectory()) {
                mCurrentPath = getRoot();
            }

            // Start watching for changes
            fileObserver = new FileObserver(mCurrentPath.getPath(), FileObserver.CREATE | FileObserver.DELETE
                    | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {

                @Override
                public void onEvent(int event, String path) {
                    // Reload
                    onContentChanged();
                }
            };
            fileObserver.startWatching();

            forceLoad();
        }

        /**
         * Handles a request to completely reset the Loader.
         */
        @Override
        protected void onReset() {
            super.onReset();

            // Stop watching
            if (fileObserver != null) {
                fileObserver.stopWatching();
                fileObserver = null;
            }
        }
    };
}

From source file:com.home.young.filepicker.FilePickerFragment.java

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes./*  w  w w .j  ava 2s .  c o m*/
 */
@NonNull
@Override
public Loader<SortedList<File>> getLoader() {
    return new AsyncTaskLoader<SortedList<File>>(getActivity()) {

        FileObserver fileObserver;

        @Override
        public SortedList<File> loadInBackground() {
            File[] listFiles = mCurrentPath.listFiles();
            final int initCap = listFiles == null ? 0 : listFiles.length;

            SortedList<File> files = new SortedList<>(File.class,
                    new SortedListAdapterCallback<File>(getDummyAdapter()) {
                        @Override
                        public int compare(File lhs, File rhs) {
                            return compareFiles(lhs, rhs);
                        }

                        @Override
                        public boolean areContentsTheSame(File file, File file2) {
                            return file.getAbsolutePath().equals(file2.getAbsolutePath())
                                    && (file.isFile() == file2.isFile());
                        }

                        @Override
                        public boolean areItemsTheSame(File file, File file2) {
                            return areContentsTheSame(file, file2);
                        }
                    }, initCap);

            files.beginBatchedUpdates();
            if (listFiles != null) {
                for (java.io.File f : listFiles) {
                    if (isItemVisible(f)) {
                        files.add(f);
                    }
                }
            }
            files.endBatchedUpdates();

            return files;
        }

        /**
         * Handles a request to start the Loader.
         */
        @Override
        protected void onStartLoading() {
            super.onStartLoading();

            // handle if directory does not exist. Fall back to root.
            if (mCurrentPath == null || !mCurrentPath.isDirectory()) {
                mCurrentPath = getRoot();
            }

            // Start watching for changes
            fileObserver = new FileObserver(mCurrentPath.getPath(), FileObserver.CREATE | FileObserver.DELETE
                    | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {

                @Override
                public void onEvent(int event, String path) {
                    // Reload
                    onContentChanged();
                }
            };
            fileObserver.startWatching();

            forceLoad();
        }

        /**
         * Handles a request to completely reset the Loader.
         */
        @Override
        protected void onReset() {
            super.onReset();

            // Stop watching
            if (fileObserver != null) {
                fileObserver.stopWatching();
                fileObserver = null;
            }
        }
    };
}

From source file:cn.tycoon.lighttrans.fileManager.FilePickerFragment.java

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes.//from  w  ww  .  j av a  2  s  .com
 */
@Override
public Loader<SortedList<File>> getLoader() {
    return new AsyncTaskLoader<SortedList<File>>(getActivity()) {

        FileObserver fileObserver;

        @Override
        public SortedList<File> loadInBackground() {
            File[] listFiles = mCurrentPath.listFiles();
            final int initCap = listFiles == null ? 0 : listFiles.length;

            SortedList<File> files = new SortedList<>(File.class,
                    new SortedListAdapterCallback<File>(getDummyAdapter()) {
                        @Override
                        public int compare(File lhs, File rhs) {
                            return compareFiles(lhs, rhs);
                        }

                        @Override
                        public boolean areContentsTheSame(File file, File file2) {
                            return file.getAbsolutePath().equals(file2.getAbsolutePath())
                                    && (file.isFile() == file2.isFile());
                        }

                        @Override
                        public boolean areItemsTheSame(File file, File file2) {
                            return areContentsTheSame(file, file2);
                        }
                    }, initCap);

            files.beginBatchedUpdates();
            if (listFiles != null) {
                for (java.io.File f : listFiles) {
                    //if (isItemVisible(f)) {
                    files.add(f);
                    //}
                }
            }
            files.endBatchedUpdates();

            return files;
        }

        /**
         * Handles a request to start the Loader.
         */
        @Override
        protected void onStartLoading() {
            super.onStartLoading();

            // handle if directory does not exist. Fall back to root.
            if (mCurrentPath == null || !mCurrentPath.isDirectory()) {
                mCurrentPath = getRoot();
            }

            // Start watching for changes
            fileObserver = new FileObserver(mCurrentPath.getPath(), FileObserver.CREATE | FileObserver.DELETE
                    | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {

                @Override
                public void onEvent(int event, String path) {
                    // Reload
                    onContentChanged();
                }
            };
            fileObserver.startWatching();

            forceLoad();
        }

        /**
         * Handles a request to completely reset the Loader.
         */
        @Override
        protected void onReset() {
            super.onReset();

            // Stop watching
            if (fileObserver != null) {
                fileObserver.stopWatching();
                fileObserver = null;
            }
        }
    };
}