Android檔案監控FileObserver介紹

來源:互聯網
上載者:User

在前面的Linux檔案系統Inotify機制中介紹了Linux對檔案變更監控過程。Android系統在此基礎上封裝了一個FileObserver類來方便使用Inotify機制。FileObserver是一個抽象類別,需要定義子類實現該類的onEvent抽象方法,當被監控的檔案或者目錄發生變更事件時,將回調FileObserver的onEvent()函數來處理檔案或目錄的變更事件。

事件監控過程

在FileObserver類中定義了一個靜態內部類ObserverThread,該線程類才是真正實現檔案或目錄監控過程。各種類型的FileObserver都擁有一個ObserverThread執行個體:

frameworks\base\core\java\android\os\FileObserver.java

public abstract class FileObserver {    //可監控的事件類型public static final int ALL_EVENTS = ACCESS | MODIFY | ATTRIB | CLOSE_WRITE            | CLOSE_NOWRITE | OPEN | MOVED_FROM | MOVED_TO | DELETE | CREATE | DELETE_SELF | MOVE_SELF;//靜態建立並啟動一個檔案監控線程private static ObserverThread s_observerThread;    static {        s_observerThread = new ObserverThread();        s_observerThread.start();    }    // instance    private String m_path;    private Integer m_descriptor;    private int m_mask;}

FileObserver類通過靜態方式構造了一個ObserverThread對象:

public ObserverThread() {super("FileObserver");m_fd = init();//初始化一個inotify執行個體,Observer線程就是對該inotify執行個體進行監控}

frameworks\base\core\jni\android_util_FileObserver.cpp

static jint android_os_fileobserver_init(JNIEnv* env, jobject object){#ifdef HAVE_INOTIFY    return (jint)inotify_init();//初始化一個inotify執行個體   #else // HAVE_INOTIFY    return -1;#endif // HAVE_INOTIFY}

inotify_init()函數實現在Linux檔案系統Inotify機制有詳細介紹,然後啟動ObserverThread線程,ObserverThread線程運行體:

frameworks\base\core\java\android\os\FileObserve$ObserverThread

public void run() {observe(m_fd);//監控inotify執行個體控制代碼}

frameworks\base\core\jni\android_util_FileObserver.cpp

static void android_os_fileobserver_observe(JNIEnv* env, jobject object, jint fd){#ifdef HAVE_INOTIFY    char event_buf[512];//定義事件數目組    struct inotify_event* event;    while (1)    {        int event_pos = 0;//從inotify執行個體控制代碼中讀取事件        int num_bytes = read(fd, event_buf, sizeof(event_buf));        if (num_bytes < (int)sizeof(*event))        {            if (errno == EINTR)                continue;            ALOGE("***** ERROR! android_os_fileobserver_observe() got a short event!");            return;        }    //迴圈處理讀取到的事件        while (num_bytes >= (int)sizeof(*event))        {            int event_size;            event = (struct inotify_event *)(event_buf + event_pos);            jstring path = NULL;            if (event->len > 0)            {                path = env->NewStringUTF(event->name);            }//調用ObserverThread的onEvent函數通知上層響應            env->CallVoidMethod(object, method_onEvent, event->wd, event->mask, path);            if (env->ExceptionCheck()) {                env->ExceptionDescribe();                env->ExceptionClear();            }            if (path != NULL)            {                env->DeleteLocalRef(path);            }            event_size = sizeof(*event) + event->len;            num_bytes -= event_size;            event_pos += event_size;        }    }#endif // HAVE_INOTIFY}

ObserverThread線程迴圈從inotify執行個體控制代碼中讀取事件,然後回調ObserverThread的onEvent函數來處理事件。

frameworks\base\core\java\android\os\FileObserve$ObserverThread

public void onEvent(int wfd, int mask, String path) {// look up our observer, fixing up the map if necessary...FileObserver observer = null;synchronized (m_observers) {//根據wfd控制代碼從m_observers表中尋找出註冊的FileObserver對象WeakReference weak = m_observers.get(wfd);if (weak != null) {  // can happen with lots of events from a dead wfdobserver = (FileObserver) weak.get();if (observer == null) {m_observers.remove(wfd);}}}// ...then call out to the observer without the sync lock heldif (observer != null) {try {//調用對應的FileObserver對象的onEvent函數來處理事件observer.onEvent(mask, path);} catch (Throwable throwable) {Log.wtf(LOG_TAG, "Unhandled exception in FileObserver " + observer, throwable);}}}
註冊監控watch

FileObserver類提供了startWatching()函數來開機檔案監控

frameworks\base\core\java\android\os\FileObserver.java

public void startWatching() {if (m_descriptor < 0) {m_descriptor = s_observerThread.startWatching(m_path, m_mask, this);}}

由ObserverThread線程對象啟動監控

frameworks\base\core\java\android\os\FileObserver$ObserverThread

public int startWatching(String path, int mask, FileObserver observer) {//在Inotify執行個體中添加一個watch對象,並得到一個watch物件控點int wfd = startWatching(m_fd, path, mask);Integer i = new Integer(wfd);if (wfd >= 0) {//將watch物件控點和響應該watch事件的FileObserver以索引值對的形式儲存在m_observers成員變數中synchronized (m_observers) {m_observers.put(i, new WeakReference(observer));}}return i;}

ObserverThread又調用native方法android_os_fileobserver_startWatching()來添加一個watch

frameworks\base\core\jni\android_util_FileObserver.cpp

static jint android_os_fileobserver_startWatching(JNIEnv* env, jobject object, jint fd, jstring pathString, jint mask){    int res = -1;#ifdef HAVE_INOTIFY    if (fd >= 0)    {        const char* path = env->GetStringUTFChars(pathString, NULL);//在Inotify執行個體上添加一個watch對象        res = inotify_add_watch(fd, path, mask);        env->ReleaseStringUTFChars(pathString, path);    }#endif // HAVE_INOTIFY    return res;}
登出監控watch

FileObserver類提供了使用stopWatching()函數來停止檔案監控。
frameworks\base\core\java\android\os\FileObserver$ObserverThread

public void stopWatching() {if (m_descriptor >= 0) {s_observerThread.stopWatching(m_descriptor);m_descriptor = -1;}}

frameworks\base\core\java\android\os\FileObserve$ObserverThread

public void stopWatching(int descriptor) {stopWatching(m_fd, descriptor);}

frameworks\base\core\jni\android_util_FileObserver.cpp

static void android_os_fileobserver_stopWatching(JNIEnv* env, jobject object, jint fd, jint wfd){#ifdef HAVE_INOTIFY    inotify_rm_watch((int)fd, (uint32_t)wfd);#endif // HAVE_INOTIFY}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.