Example usage for android.os FileObserver CREATE

List of usage examples for android.os FileObserver CREATE

Introduction

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

Prototype

int CREATE

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

Click Source Link

Document

Event type: A new file or subdirectory was created under the monitored directory

Usage

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
 *//*from   w  w  w. ja  v a 2  s  .  co  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:org.protocoderrunner.apprunner.api.PFileIO.java

@ProtoMethod(description = "Observer file changes in a folder", example = "")
@ProtoMethodParam(params = { "path", "function(action, file" })
public void observeFolder(String path, final FileObserverCB callback) {

    fileObserver = new FileObserver(
            ProjectManager.getInstance().getCurrentProject().getStoragePath() + "/" + path,
            FileObserver.CREATE | FileObserver.MODIFY | FileObserver.DELETE) {

        @Override/*  ww w.  j a v a2 s  .  c om*/
        public void onEvent(int event, String file) {

            if ((FileObserver.CREATE & event) != 0) {
                callback.event("create", file);
            } else if ((FileObserver.DELETE & event) != 0) {
                callback.event("delete", file);
            } else if ((FileObserver.MODIFY & event) != 0) {
                callback.event("modify", file);
            }
        }

    };
    fileObserver.startWatching();
}

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/*from  w w w .ja v  a2s.  co  m*/
        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();
                    }
                });
            }
        }
    };
}

From source file:com.dnielfe.manager.Browser.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(mCurrentPath));
        break;/*www  .  ja  v  a 2  s.co m*/
    }
}

From source file:com.alexlionne.apps.avatars.Utils.DirectoryChooserFragment.java

/**
 * Sets up a FileObserver to watch the current directory.
 *//*ww  w .  ja v a2 s . co  m*/
private FileObserver createFileObserver(String path) {
    return new FileObserver(path,
            FileObserver.CREATE | FileObserver.DELETE | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {

        @Override
        public void onEvent(int event, String path) {
            debug("FileObserver received event %d", event);
            final Activity activity = getActivity();

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

From source file:com.andrada.sitracker.ui.fragment.DirectoryChooserFragment.java

/**
 * Sets up a FileObserver to watch the current directory.
 *///from www  .  j a v  a2s.com
@NotNull
private FileObserver createFileObserver(String path) {
    return new FileObserver(path,
            FileObserver.CREATE | FileObserver.DELETE | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {

        @Override
        public void onEvent(int event, String path) {
            debug("FileObserver received event %d", event);
            final Activity activity = getActivity();

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

From source file:org.protocoderrunner.apprunner.AppRunnerFragment.java

public void startFileObserver() {

    if (mIsProjectLoaded) {

        // set up mContext file observer to watch this directory on sd card
        fileObserver = new FileObserver(mCurrentProject.getStoragePath(),
                FileObserver.CREATE | FileObserver.DELETE) {

            @Override/*w w  w .  j a  va2s. c om*/
            public void onEvent(int event, String file) {
                JSONObject msg = new JSONObject();
                String action = null;

                if ((FileObserver.CREATE & event) != 0) {
                    MLog.d(TAG, "created " + file);
                    action = "new_files_in_project";

                } else if ((FileObserver.DELETE & event) != 0) {
                    MLog.d(TAG, "deleted file " + file);
                    action = "deleted_files_in_project";
                }

                try {
                    msg.put("action", action);
                    msg.put("type", "ide");
                    IDEcommunication.getInstance(mActivity).send(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        };
    }

}