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.nononsenseapps.filepicker.local.LocalFilePickerFragment.java

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

private String getFileAction(int event) {
    String fileAction = null;//  w w  w .  j a va2s.  c  o m
    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.owncloud.android.services.observer.InstantUploadsObserver.java

/**
 * Receives and processes events about updates of the monitored folder.
 *
 * This is almost heuristic. Do no expect it works magically with any camera.
 *
 * For instance, Google Camera creates a new video file when the user enters in "video mode", before
 * start to record, and saves it empty if the user leaves recording nothing. True store. Life is magic.
 *
 * @param event     Kind of event occurred.
 * @param path      Relative path of the file referred by the event.
 *///from  w w w  .  j  a va  2  s  .  c  om
@Override
public void onEvent(int event, String path) {
    Log_OC.d(TAG, "Got event " + event + " on FOLDER " + mConfiguration.getSourcePath() + " about "
            + ((path != null) ? path : "") + " (in thread '" + Thread.currentThread().getName() + "')");

    if (path != null && path.length() > 0) {
        synchronized (mLock) {
            if ((event & FileObserver.CREATE) != 0) {
                // new file created, let's watch it; false -> not modified yet
                mObservedChildren.put(path, false);
            }
            if (((event & FileObserver.MODIFY) != 0) && mObservedChildren.containsKey(path)
                    && !mObservedChildren.get(path)) {
                // watched file was written for the first time after creation
                mObservedChildren.put(path, true);
            }
            if ((event & FileObserver.CLOSE_WRITE) != 0 && mObservedChildren.containsKey(path)
                    && mObservedChildren.get(path)) {
                // a file that was previously created and written has been closed;
                // testing for FileObserver.MODIFY is needed because some apps
                // close the video file right after creating it when the recording
                // is started, and reopen it to write with the first chunk of video
                // to save; for instance, Camera MX does so.
                mObservedChildren.remove(path);
                handleNewFile(path);
            }
            if ((event & FileObserver.MOVED_TO) != 0) {
                // a file has been moved or renamed into the folder;
                // for instance, Google Camera does so right after
                // saving a video recording
                handleNewFile(path);
            }
        }
    }

    if ((event & IN_IGNORE) != 0 && (path == null || path.length() == 0)) {
        Log_OC.d(TAG, "Stopping the observance on " + mConfiguration.getSourcePath());
    }
}

From source file:org.protocoder.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_forfragments);
    c = this;//from  www.  j ava2  s  .  c o  m

    // 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.ihelp101.instagram.FilePickerFragment.java

/**
 * Get a loader that lists the Files in the current path,
 * and monitors changes.//from   ww w  .  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  a va2  s  . c om
 */
@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 w w.  j  ava  2  s. co 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 (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;//from  ww w . jav a  2 s  . c o m
    }
}