Example usage for android.os FileObserver MOVED_FROM

List of usage examples for android.os FileObserver MOVED_FROM

Introduction

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

Prototype

int MOVED_FROM

To view the source code for android.os FileObserver MOVED_FROM.

Click Source Link

Document

Event type: A file or subdirectory was moved from the monitored directory

Usage

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

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes.//  w  ww .  j  a v a 2 s.  c om
 */
@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 2s.  com*/
 */
@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.ado.minesync.minecraft.MinecraftWorldObserver.java

private String getFileAction(int event) {
    String fileAction = null;//from w  ww.j  a va2  s. c  om
    switch (event) {
    case FileObserver.ACCESS:
        fileAction = "ACCESS";
        break;
    case FileObserver.ALL_EVENTS:
        fileAction = "ALL_EVENTS";
        break;
    case FileObserver.ATTRIB:
        fileAction = "ATTRIB";
        break;
    case FileObserver.CLOSE_NOWRITE:
        fileAction = "CLOSE_NOWRITE";
        break;
    case FileObserver.CLOSE_WRITE:
        fileAction = "CLOSE_WRITE";
        break;
    case FileObserver.CREATE:
        fileAction = "CREATE";
        break;
    case FileObserver.DELETE:
        fileAction = "DELETE";
        break;
    case FileObserver.DELETE_SELF:
        fileAction = "DELETE_SELF";
        break;
    case FileObserver.MODIFY:
        fileAction = "MODIFY";
        break;
    case FileObserver.MOVE_SELF:
        fileAction = "MOVE_SELF";
        break;
    case FileObserver.MOVED_FROM:
        fileAction = "MOVED_FROM";
        break;
    case FileObserver.MOVED_TO:
        fileAction = "MOVED_TO";
        break;
    case FileObserver.OPEN:
        fileAction = "OPEN";
        break;
    }
    return fileAction;
}

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

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes./*from   w  w  w  .j  a v  a 2 s  .c  o m*/
 */
@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.//ww  w .j  av  a 2  s  .com
 */
@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.//w w w.  ja v  a2s  . 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 (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:com.lovejoy777sarootool.rootool.fragments.BrowserFragment.java

@Override
public void onEvent(int event, String path) {
    // this will automatically update the directory when an action like this
    // will be performed
    switch (event & FileObserver.ALL_EVENTS) {
    case FileObserver.CREATE:
    case FileObserver.CLOSE_WRITE:
    case FileObserver.MOVE_SELF:
    case FileObserver.MOVED_TO:
    case FileObserver.MOVED_FROM:
    case FileObserver.ATTRIB:
    case FileObserver.DELETE:
    case FileObserver.DELETE_SELF:
        sHandler.removeCallbacks(mLastRunnable);
        sHandler.post(mLastRunnable = new NavigateRunnable(path));
        break;//  ww w.  j a v a  2  s  .  c o m
    }
}

From source file:com.docd.purefm.adapters.BrowserBaseAdapter.java

/**
 * {@link android.os.FileObserver} event that should be ran only on UI thread
 *
 * @param event The type of event which happened
 * @param file The modified file, relative to the main monitored file or directory,
 *             of the file or directory which triggered the event
 *///ww w  . java  2s  .  c  o m
void onEventUIThread(final int event, @NonNull final GenericFile file) {
    switch (event & FileObserver.ALL_EVENTS) {
    case FileObserver.CREATE:
        //Do nothing. The event is handled in Browser
        break;

    case FileObserver.DELETE:
    case FileObserver.DELETE_SELF:
    case FileObserver.MOVED_FROM:
        onFileDeleted(file);
        break;

    default:
        onFileModified(file);
        break;
    }
}

From source file:com.veniosg.dir.android.fragment.FileListFragment.java

private FileObserver generateFileObserver(String pathToObserve) {
    return new FileObserver(pathToObserve, FileObserver.CREATE | FileObserver.DELETE | FileObserver.CLOSE_WRITE // Removed since in case of continuous modification
                                                                                                                // (copy/compress) we would flood with events.
            | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {
        private static final long MIN_REFRESH_INTERVAL = 2 * 1000;

        private long lastUpdate = 0;

        @Override//w w w . ja v a  2s . c  om
        public void onEvent(int event, String path) {
            if (System.currentTimeMillis() - lastUpdate <= MIN_REFRESH_INTERVAL || event == 32768) { // See https://code.google.com/p/android/issues/detail?id=29546
                return;
            }

            Logger.logV(Logger.TAG_OBSERVER, "Observed event " + event + ", refreshing list..");
            lastUpdate = System.currentTimeMillis();

            if (getActivity() != null) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        refresh();
                    }
                });
            }
        }
    };
}