1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.os; 18 19 import android.util.Log; 20 21 import com.android.internal.os.RuntimeInit; 22 23 import java.lang.ref.WeakReference; 24 import java.util.ArrayList; 25 import java.util.HashMap; 26 27 /** 28 * Monitors files (using <a href="http://en.wikipedia.org/wiki/Inotify">inotify</a>) 29 * to fire an event after files are accessed or changed by by any process on 30 * the device (including this one). FileObserver is an abstract class; 31 * subclasses must implement the event handler {@link #onEvent(int, String)}. 32 * 33 * <p>Each FileObserver instance monitors a single file or directory. 34 * If a directory is monitored, events will be triggered for all files and 35 * subdirectories inside the monitored directory.</p> 36 * 37 * <p>An event mask is used to specify which changes or actions to report. 38 * Event type constants are used to describe the possible changes in the 39 * event mask as well as what actually happened in event callbacks.</p> 40 * 41 * <p class="caution"><b>Warning</b>: If a FileObserver is garbage collected, it 42 * will stop sending events. To ensure you keep receiving events, you must 43 * keep a reference to the FileObserver instance from some other live object.</p> 44 */ 45 public abstract class FileObserver { 46 /** Event type: Data was read from a file */ 47 public static final int ACCESS = 0x00000001; 48 /** Event type: Data was written to a file */ 49 public static final int MODIFY = 0x00000002; 50 /** Event type: Metadata (permissions, owner, timestamp) was changed explicitly */ 51 public static final int ATTRIB = 0x00000004; 52 /** Event type: Someone had a file or directory open for writing, and closed it */ 53 public static final int CLOSE_WRITE = 0x00000008; 54 /** Event type: Someone had a file or directory open read-only, and closed it */ 55 public static final int CLOSE_NOWRITE = 0x00000010; 56 /** Event type: A file or directory was opened */ 57 public static final int OPEN = 0x00000020; 58 /** Event type: A file or subdirectory was moved from the monitored directory */ 59 public static final int MOVED_FROM = 0x00000040; 60 /** Event type: A file or subdirectory was moved to the monitored directory */ 61 public static final int MOVED_TO = 0x00000080; 62 /** Event type: A new file or subdirectory was created under the monitored directory */ 63 public static final int CREATE = 0x00000100; 64 /** Event type: A file was deleted from the monitored directory */ 65 public static final int DELETE = 0x00000200; 66 /** Event type: The monitored file or directory was deleted; monitoring effectively stops */ 67 public static final int DELETE_SELF = 0x00000400; 68 /** Event type: The monitored file or directory was moved; monitoring continues */ 69 public static final int MOVE_SELF = 0x00000800; 70 71 /** Event mask: All valid event types, combined */ 72 public static final int ALL_EVENTS = ACCESS | MODIFY | ATTRIB | CLOSE_WRITE 73 | CLOSE_NOWRITE | OPEN | MOVED_FROM | MOVED_TO | DELETE | CREATE 74 | DELETE_SELF | MOVE_SELF; 75 76 private static final String LOG_TAG = "FileObserver"; 77 78 private static class ObserverThread extends Thread { 79 private HashMap<Integer, WeakReference> m_observers = new HashMap<Integer, WeakReference>(); 80 private int m_fd; 81 82 public ObserverThread() { 83 super("FileObserver"); 84 m_fd = init(); 85 } 86 87 public void run() { 88 observe(m_fd); 89 } 90 91 public int startWatching(String path, int mask, FileObserver observer) { 92 int wfd = startWatching(m_fd, path, mask); 93 94 Integer i = new Integer(wfd); 95 if (wfd >= 0) { 96 synchronized (m_observers) { 97 m_observers.put(i, new WeakReference(observer)); 98 } 99 } 100 101 return i; 102 } 103 104 public void stopWatching(int descriptor) { 105 stopWatching(m_fd, descriptor); 106 } 107 108 public void onEvent(int wfd, int mask, String path) { 109 // look up our observer, fixing up the map if necessary... 110 FileObserver observer = null; 111 112 synchronized (m_observers) { 113 WeakReference weak = m_observers.get(wfd); 114 if (weak != null) { // can happen with lots of events from a dead wfd 115 observer = (FileObserver) weak.get(); 116 if (observer == null) { 117 m_observers.remove(wfd); 118 } 119 } 120 } 121 122 // ...then call out to the observer without the sync lock held 123 if (observer != null) { 124 try { 125 observer.onEvent(mask, path); 126 } catch (Throwable throwable) { 127 Log.wtf(LOG_TAG, "Unhandled exception in FileObserver " + observer, throwable); 128 } 129 } 130 } 131 132 private native int init(); 133 private native void observe(int fd); 134 private native int startWatching(int fd, String path, int mask); 135 private native void stopWatching(int fd, int wfd); 136 } 137 138 private static ObserverThread s_observerThread; 139 140 static { 141 s_observerThread = new ObserverThread(); 142 s_observerThread.start(); 143 } 144 145 // instance 146 private String m_path; 147 private Integer m_descriptor; 148 private int m_mask; 149 150 /** 151 * Equivalent to FileObserver(path, FileObserver.ALL_EVENTS). 152 */ 153 public FileObserver(String path) { 154 this(path, ALL_EVENTS); 155 } 156 157 /** 158 * Create a new file observer for a certain file or directory. 159 * Monitoring does not start on creation! You must call 160 * {@link #startWatching()} before you will receive events. 161 * 162 * @param path The file or directory to monitor 163 * @param mask The event or events (added together) to watch for 164 */ 165 public FileObserver(String path, int mask) { 166 m_path = path; 167 m_mask = mask; 168 m_descriptor = -1; 169 } 170 171 protected void finalize() { 172 stopWatching(); 173 } 174 175 /** 176 * Start watching for events. The monitored file or directory must exist at 177 * this time, or else no events will be reported (even if it appears later). 178 * If monitoring is already started, this call has no effect. 179 */ 180 public void startWatching() { 181 if (m_descriptor < 0) { 182 m_descriptor = s_observerThread.startWatching(m_path, m_mask, this); 183 } 184 } 185 186 /** 187 * Stop watching for events. Some events may be in process, so events 188 * may continue to be reported even after this method completes. If 189 * monitoring is already stopped, this call has no effect. 190 */ 191 public void stopWatching() { 192 if (m_descriptor >= 0) { 193 s_observerThread.stopWatching(m_descriptor); 194 m_descriptor = -1; 195 } 196 } 197 198 /** 199 * The event handler, which must be implemented by subclasses. 200 * 201 * <p class="note">This method is invoked on a special FileObserver thread. 202 * It runs independently of any threads, so take care to use appropriate 203 * synchronization! Consider using {@link Handler#post(Runnable)} to shift 204 * event handling work to the main thread to avoid concurrency problems.</p> 205 * 206 * <p>Event handlers must not throw exceptions.</p> 207 * 208 * @param event The type of event which happened 209 * @param path The path, relative to the main monitored file or directory, 210 * of the file or directory which triggered the event 211 */ 212 public abstract void onEvent(int event, String path); 213 } 214