转载请注明出处:http://blog.csdn.net/droyon/article/details/35558637
// FileObserver for external modifications to the database file.
// Note that this is for platform developers only with
// userdebug/eng builds who should be able to tinker with the
// sqlite database out from under the SettingsProvider, which is
// normally the exclusive owner of the database. But we keep this
// enabled all the time to minimize development-vs-user
// differences in testing.
//FileObserver是外部对数据库修改的监听。
//这是userdebug或者eng版本的平台开发者,开发者也是独占数据库的所有者,能够在SettingsProvider中摆弄sqlite数据库。
//这个监听,能够保持长久运行,减小开发者和用户之间的差距。
private static SparseArray<SettingsFileObserver> sObserverInstances
= new SparseArray<SettingsFileObserver>();
private class SettingsFileObserver extends FileObserver {
private final AtomicBoolean mIsDirty = new AtomicBoolean(false);
private final int mUserHandle;
private final String 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;
}
public void onEvent(int event, String path) {
int modsInFlight = sKnownMutationsInFlight.get(mUserHandle).get();
if (modsInFlight > 0) {
// our own modification.
return;
}
Log.d(TAG, "User " + mUserHandle + " external modification to " + mPath
+ "; event=" + event);
if (!mIsDirty.compareAndSet(false, true)) {//isDirty为true,说明在进行fullyPopulateCaches,不需要在做。
// already handled. (we get a few update events
// during an sqlite write)
return;
}
Log.d(TAG, "User " + mUserHandle + " updating our caches for " + mPath);
fullyPopulateCaches(mUserHandle);
mIsDirty.set(false);
}
}