Java Code Examples for android.os.FileObserver#startWatching()

The following examples show how to use android.os.FileObserver#startWatching() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ControlObserver.java    From ClashForMagisk with GNU General Public License v3.0 6 votes vote down vote up
private void restart() {
    //noinspection ResultOfMethodCallIgnored
    dataDir.mkdirs();

    fileObserver = new FileObserver(dataDir.getAbsolutePath(), FileObserver.CREATE | FileObserver.DELETE_SELF | FileObserver.MOVED_FROM | FileObserver.MOVED_TO) {
        @Override
        public void onEvent(int event, String file) {
            Log.d(Constants.TAG, "Control Directory Changed " + file);

            if ((event & FileObserver.DELETE_SELF) != 0) {
                restart();
            } else {
                file = file.toUpperCase();

                switch (file) {
                    case "STOP":
                    case "START":
                    case "RESTART":
                        callback.onUserControl(file);
                }
            }
        }
    };

    fileObserver.startWatching();
}
 
Example 2
Source File: PFileIO.java    From PHONK with GNU General Public License v3.0 6 votes vote down vote up
PFileObserver(AppRunner appRunner, String path) {
    fileObserver = new FileObserver(appRunner.getProject().getFullPathForFile(path), FileObserver.CREATE | FileObserver.MODIFY | FileObserver.DELETE) {

        @Override
        public void onEvent(int event, String file) {
            ReturnObject ret = new ReturnObject();
            if ((FileObserver.CREATE & event) != 0) {
                ret.put("action", "created");
            } else if ((FileObserver.DELETE & event) != 0) {
                ret.put("action", "deleted");
            } else if ((FileObserver.MODIFY & event) != 0) {
                ret.put("action", "modified");
            }
            ret.put("file", file);
            if (callback != null) callback.event(ret);
        }

    };
    fileObserver.startWatching();
    getAppRunner().whatIsRunning.add(this);
}
 
Example 3
Source File: FileObservable.java    From RxFileObserver with Apache License 2.0 6 votes vote down vote up
@Override
public void call(final Subscriber<? super FileEvent> subscriber) {
    final FileObserver observer = new FileObserver(pathToWatch) {
        @Override
        public void onEvent(int event, String file) {
            if(subscriber.isUnsubscribed()) {
                return;
            }

            FileEvent fileEvent = FileEvent.create(event, file);
            subscriber.onNext(fileEvent);

            if(fileEvent.isDeleteSelf()) {
                subscriber.onCompleted();
            }
        }
    };
    observer.startWatching(); //START OBSERVING

    subscriber.add(Subscriptions.create(new Action0() {
        @Override
        public void call() {
            observer.stopWatching();
        }
    }));
}
 
Example 4
Source File: PrefFileManager.java    From MinMinGuard with GNU General Public License v3.0 6 votes vote down vote up
private PrefFileManager(Context context)
{
    mContext = Build.VERSION.SDK_INT >= 24 && !ContextCompat.isDeviceProtectedStorage(context)
            ? ContextCompat.createDeviceProtectedStorageContext(context) : context;

    mFileObserver = new FileObserver(mContext.getFilesDir().getParentFile() + "/shared_prefs", FileObserver.ATTRIB)
    {
        @Override
        public void onEvent(int event, String path)
        {
            if ((event & FileObserver.ATTRIB) != 0)
                onFileAttributesChanged(path);
        }
    };
    mFileObserver.startWatching();
}
 
Example 5
Source File: Enhancement.java    From EdXposedManager with GNU General Public License v3.0 5 votes vote down vote up
private static List<String> getModulesList(final int user) {
    final int index = modulesList.indexOfKey(user);
    if (index >= 0) {
        return modulesList.valueAt(index);
    }

    final String filename = String.format("/data/user_de/%s/%s/conf/enabled_modules.list", user, APPLICATION_ID);
    final FileObserver observer = new FileObserver(filename) {
        @Override
        public void onEvent(int event, @Nullable String path) {
            switch (event) {
                case FileObserver.MODIFY:
                    modulesList.put(user, readModulesList(filename));
                    break;
                case FileObserver.MOVED_FROM:
                case FileObserver.MOVED_TO:
                case FileObserver.MOVE_SELF:
                case FileObserver.DELETE:
                case FileObserver.DELETE_SELF:
                    modulesList.remove(user);
                    modulesListObservers.remove(user);
                    break;
            }
        }
    };
    modulesListObservers.put(user, observer);
    final List<String> list = readModulesList(filename);
    modulesList.put(user, list);
    observer.startWatching();
    return list;
}
 
Example 6
Source File: CameraControlPanel.java    From sandriosCamera with MIT License 5 votes vote down vote up
public void onStartVideoRecord(final File mediaFile) {
    setMediaFilePath(mediaFile);
    if (maxVideoFileSize > 0) {
        recordSizeText.setText("1Mb" + " / " + maxVideoFileSize / (1024 * 1024) + "Mb");
        recordSizeText.setVisibility(VISIBLE);
        try {
            fileObserver = new FileObserver(this.mediaFilePath) {
                private long lastUpdateSize = 0;

                @Override
                public void onEvent(int event, String path) {
                    final long fileSize = mediaFile.length() / (1024 * 1024);
                    if ((fileSize - lastUpdateSize) >= 1) {
                        lastUpdateSize = fileSize;
                        recordSizeText.post(new Runnable() {
                            @Override
                            public void run() {
                                recordSizeText.setText(fileSize + "Mb" + " / " + maxVideoFileSize / (1024 * 1024) + "Mb");
                            }
                        });
                    }
                }
            };
            fileObserver.startWatching();
        } catch (Exception e) {
            Log.e("FileObserver", "setMediaFilePath: ", e);
        }
    }
    countDownTimer.start();
}
 
Example 7
Source File: RecordingActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private void setupFileObserver(File directory) {
    mFileObserver = new FileObserver(directory.getAbsolutePath()) {
        @Override
        public void onEvent(int event, String s) {
            if (s != null && s.equals(mOutputFile.getName()) && event == FileObserver.CLOSE_WRITE) {
                outputFileWrittenLatch.countDown();
            }
        }
    };
    mFileObserver.startWatching();
}
 
Example 8
Source File: RecordingActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
private void setupFileObserver(File directory) {
    mFileObserver = new FileObserver(directory.getAbsolutePath()) {
        @Override
        public void onEvent(int event, String s) {
            if (s != null && s.equals(mOutputFile.getName()) && event == FileObserver.CLOSE_WRITE) {
                outputFileWrittenLatch.countDown();
            }
        }
    };
    mFileObserver.startWatching();
}
 
Example 9
Source File: AssetDefinitionService.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
private FileObserver startFileListener(String path)
{
    FileObserver observer = new FileObserver(path)
    {
        private final String listenerPath = path;
        @Override
        public void onEvent(int event, @Nullable String file)
        {
            //watch for new files and file change
            switch (event)
            {
                case CREATE:
                    //if this file already exists then wait for the modify
                    File checkFile = new File(listenerPath, file);
                    if (checkFile.exists() && checkFile.canRead())
                    {
                        break;
                    }
                case MODIFY:
                    try
                    {
                        if (file.contains(".xml") || file.contains(".tsml"))
                        {
                            System.out.println("FILE: " + file);
                            //form filename
                            TokenScriptFile newTSFile = new TokenScriptFile(context, listenerPath, file);
                            List<ContractLocator> originContracts = addContractAddresses(newTSFile);

                            if (originContracts.size() > 0)
                            {
                                notificationService.DisplayNotification("Definition Updated", file, NotificationCompat.PRIORITY_MAX);
                                cachedDefinition = null;
                                cacheSignature(newTSFile) //update signature data if necessary
                                        .subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe().isDisposed();

                                Intent intent = new Intent(ADDED_TOKEN);
                                intent.putParcelableArrayListExtra(C.EXTRA_TOKENID_LIST, (ArrayList)originContracts);
                                context.sendBroadcast(intent);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        if (homeMessenger != null) homeMessenger.tokenScriptError(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
    };

    observer.startWatching();

    return observer;
}
 
Example 10
Source File: CameraFragment.java    From phoenix with Apache License 2.0 4 votes vote down vote up
protected void onStartVideoRecord(final File mediaFile) {
    setMediaFilePath(mediaFile);
    if (mMaxVideoFileSize > 0) {

        if (mCameraVideoRecordTextListener != null) {
            mCameraVideoRecordTextListener.setRecordSizeText(mMaxVideoFileSize, "1Mb" + " / " + mMaxVideoFileSize / (1024 * 1024) + "Mb");
            mCameraVideoRecordTextListener.setRecordSizeTextVisible(true);
        }
        try {
            mFileObserver = new FileObserver(this.mMediaFilePath) {
                private long lastUpdateSize = 0;

                @Override
                public void onEvent(int event, String path) {
                    final long fileSize = mediaFile.length() / (1024 * 1024);
                    if ((fileSize - lastUpdateSize) >= 1) {
                        lastUpdateSize = fileSize;
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                if (mCameraVideoRecordTextListener != null) {
                                    mCameraVideoRecordTextListener.setRecordSizeText(mMaxVideoFileSize, fileSize + "Mb" + " / " + mMaxVideoFileSize / (1024 * 1024) + "Mb");
                                }
                            }
                        });
                    }
                }
            };
            mFileObserver.startWatching();
        } catch (Exception e) {
            Log.e("FileObserver", "setMediaFilePath: ", e);
        }
    }

    if (mCountDownTimer == null) {
        this.mCountDownTimer = new TimerTask(mTimerCallBack);
    }
    mCountDownTimer.start();

    if (mCameraStateListener != null) {
        mCameraStateListener.onStartVideoRecord(mediaFile);
    }
}
 
Example 11
Source File: BaseAnncaFragment.java    From phoenix with Apache License 2.0 4 votes vote down vote up
protected void onStartVideoRecord(final File mediaFile) {
    setMediaFilePath(mediaFile);
    if (maxVideoFileSize > 0) {

        if (cameraFragmentVideoRecordTextListener != null) {
            cameraFragmentVideoRecordTextListener.setRecordSizeText(maxVideoFileSize, "1Mb" + " / " + maxVideoFileSize / (1024 * 1024) + "Mb");
            cameraFragmentVideoRecordTextListener.setRecordSizeTextVisible(true);
        }
        try {
            fileObserver = new FileObserver(this.mediaFilePath) {
                private long lastUpdateSize = 0;

                @Override
                public void onEvent(int event, String path) {
                    final long fileSize = mediaFile.length() / (1024 * 1024);
                    if ((fileSize - lastUpdateSize) >= 1) {
                        lastUpdateSize = fileSize;
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                if (cameraFragmentVideoRecordTextListener != null) {
                                    cameraFragmentVideoRecordTextListener.setRecordSizeText(maxVideoFileSize, fileSize + "Mb" + " / " + maxVideoFileSize / (1024 * 1024) + "Mb");
                                }
                            }
                        });
                    }
                }
            };
            fileObserver.startWatching();
        } catch (Exception e) {
            Log.e("FileObserver", "setMediaFilePath: ", e);
        }
    }

    if (countDownTimer == null) {
        this.countDownTimer = new TimerTask(timerCallBack);
    }
    countDownTimer.start();

    if (cameraFragmentStateListener != null) {
        cameraFragmentStateListener.onStartVideoRecord(mediaFile);
    }
}
 
Example 12
Source File: MainActivity.java    From open-quartz with Apache License 2.0 4 votes vote down vote up
/**
 * Process picture - from example GDK
 */
private void processPictureWhenReady(final String picturePath) {
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {
        // The picture is ready; process it.
    } 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();
        final FileObserver observer = new FileObserver(parentDirectory.getPath()) {
            // Protect against additional pending events after CLOSE_WRITE 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.
                    final File affectedFile = new File(parentDirectory, path);
                    isFileWritten = (event == FileObserver.CLOSE_WRITE
                        && 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();
    }
}