下面列出了android.os.FileObserver#CLOSE_WRITE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onEvent(int eventType, @Nullable String relativePath) {
if (relativePath == null || eventType != FileObserver.CLOSE_WRITE) {
return;
}
logger.log(
SentryLevel.DEBUG,
"onEvent fired for EnvelopeFileObserver with event type %d on path: %s for file %s.",
eventType,
this.rootPath,
relativePath);
// TODO: Only some event types should be pass through?
final CachedEnvelopeHint hint = new CachedEnvelopeHint(flushTimeoutMillis, logger);
envelopeSender.processEnvelopeFile(this.rootPath + File.separator + relativePath, hint);
}
private void startWatching() {
final String pathToWatch = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
observer = new RecursiveFileObserver(pathToWatch, FileObserver.CLOSE_WRITE| FileObserver.MOVED_TO) { // set up a file observer to watch this directory on sd card
@Override
public void onEvent(int event, final String mediaPath) {
if (mediaPath != null && (!mediaPath.equals(".probe"))) { // check that it's not equal to .probe because thats created every time camera is launched
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (mediaPath.endsWith(".mp4"))
handleNewVideo(mediaPath);
}
});
}
}
};
observer.startWatching();
}
@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((AbstractBrowserActivity) getActivity(), path));
break;
}
}
/**
* Receives and processes events about updates of the monitor folder and its children files.
*
* @param event Kind of event occurred.
* @param path Relative path of the file referred by the event.
*/
@Override
public void onEvent(int event, String path) {
Log_OC.d(TAG, "Got event " + event + " on FOLDER " + mPath + " about "
+ ((path != null) ? path : ""));
boolean shouldSynchronize = false;
synchronized(mObservedChildren) {
if (path != null && path.length() > 0 && mObservedChildren.containsKey(path)) {
if ( ((event & FileObserver.MODIFY) != 0) ||
((event & FileObserver.ATTRIB) != 0) ||
((event & FileObserver.MOVED_TO) != 0) ) {
if (!mObservedChildren.get(path)) {
mObservedChildren.put(path, Boolean.valueOf(true));
}
}
if ((event & FileObserver.CLOSE_WRITE) != 0 && mObservedChildren.get(path)) {
mObservedChildren.put(path, Boolean.valueOf(false));
shouldSynchronize = true;
}
}
}
if (shouldSynchronize) {
startSyncOperation(path);
}
if ((event & IN_IGNORE) != 0 &&
(path == null || path.length() == 0)) {
Log_OC.d(TAG, "Stopping the observance on " + mPath);
}
}
public SettingsFileObserver(int userHandle, String path) {
super(path, FileObserver.CLOSE_WRITE |
FileObserver.CREATE | FileObserver.DELETE |
FileObserver.MOVED_TO | FileObserver.MODIFY);
mUserHandle = userHandle;
mPath = path;
}
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();
}
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();
}
public static String toString(final int event) {
switch (event) {
case FileObserver.ACCESS:
return "ACCESS";
case FileObserver.MODIFY:
return "MODIFY";
case FileObserver.ATTRIB:
return "ATTRIB";
case FileObserver.CLOSE_WRITE:
return "CLOSE_WRITE";
case FileObserver.CLOSE_NOWRITE:
return "CLOSE_NOWRITE";
case FileObserver.OPEN:
return "OPEN";
case FileObserver.MOVED_FROM:
return "MOVED_FROM";
case FileObserver.MOVED_TO:
return "MOVED_TO";
case FileObserver.CREATE:
return "CREATE";
case FileObserver.DELETE:
return "DELETE";
case FileObserver.DELETE_SELF:
return "DELETE_SELF";
case FileObserver.MOVE_SELF:
return "MOVE_SELF";
default:
return "0x" + Integer.toHexString(event);
}
}
public FileInitializedObserver(File directory, String ... filenames) {
// MOVED_TO is required for the Tor case where the Tor process creates <target>.tmp,
// writes to that file, then renames to <target>. There's no CLOSE_WRITE event for <target>.
super(
directory.getAbsolutePath(),
FileObserver.MOVED_TO | FileObserver.CLOSE_WRITE);
mTargetFilenames = new ArrayList<String>(Arrays.asList(filenames));
mLatch = new CountDownLatch(mTargetFilenames.size());
}
@Override
public void onEvent(int event, final String path) {
if (path == null) {
return;
}
switch (event & FileObserver.ALL_EVENTS) {
case FileObserver.CLOSE_WRITE:
// Download complete, or paused when wifi is disconnected. Possibly reported more than once in a row.
// Useful for noticing when a download has been paused. For completions, register a receiver for
// DownloadManager.ACTION_DOWNLOAD_COMPLETE.
break;
case FileObserver.OPEN:
// Called for both read and write modes.
// Useful for noticing a download has been started or resumed.
break;
case FileObserver.DELETE:
case FileObserver.MOVED_FROM:
// This video is lost never to return. Remove it.
handler.post(new Runnable() {
@Override
public void run() {
DatabaseHelper helper = dataService.getHelper();
helper.removeDownloadFromDownloadManager(helper.getVideoForFilename(path));
}
});
break;
case FileObserver.MODIFY:
// Called very frequently while a download is ongoing (~1 per ms).
// This could be used to trigger a progress update, but that should probably be done less often than this.
shouldPoll = true;
break;
}
}
@Override
public void onEvent(int event, String path) {
String parse = null;
switch(event) {
case FileObserver.CREATE:
parse = "file created";
break;
case FileObserver.MODIFY:
parse = "file modified";
break;
case FileObserver.CLOSE_WRITE:
parse = "file closed/writen";
break;
case FileObserver.ACCESS:
//parse = "file accessed";
break;
case FileObserver.DELETE:
parse = "file deleted";
break;
case FileObserver.OPEN:
//parse = "file opened";
break;
case FileObserver.CLOSE_NOWRITE:
//parse = "file closed/not writen";
break;
case FileObserver.ATTRIB:
parse = "file attribs changed";
break;
}
if(parse != null) {
Logger.d(LOG, String.format("EVENT %d ON %s: %s", event, path, parse));
//lsof_r1(path);
ILogPack logPack = new ILogPack();
logPack.put(Keys.FILE_EFFECTED, path);
logPack.put(Keys.ACCESS_TYPE, parse);
logPack.put(Keys.ACCESS_CODE, event);
DeviceSucker.this.sendToBuffer(logPack);
}
}
/**
* 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();
}
}