• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.UnsupportedAppUsage;
23 import android.util.Log;
24 
25 import java.io.File;
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 import java.lang.ref.WeakReference;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 
33 /**
34  * Monitors files (using <a href="http://en.wikipedia.org/wiki/Inotify">inotify</a>)
35  * to fire an event after files are accessed or changed by by any process on
36  * the device (including this one).  FileObserver is an abstract class;
37  * subclasses must implement the event handler {@link #onEvent(int, String)}.
38  *
39  * <p>Each FileObserver instance can monitor multiple files or directories.
40  * If a directory is monitored, events will be triggered for all files and
41  * subdirectories inside the monitored directory.</p>
42  *
43  * <p>An event mask is used to specify which changes or actions to report.
44  * Event type constants are used to describe the possible changes in the
45  * event mask as well as what actually happened in event callbacks.</p>
46  *
47  * <p class="caution"><b>Warning</b>: If a FileObserver is garbage collected, it
48  * will stop sending events.  To ensure you keep receiving events, you must
49  * keep a reference to the FileObserver instance from some other live object.</p>
50  */
51 public abstract class FileObserver {
52     /** @hide */
53     @IntDef(flag = true, value = {
54             ACCESS,
55             MODIFY,
56             ATTRIB,
57             CLOSE_WRITE,
58             CLOSE_NOWRITE,
59             OPEN,
60             MOVED_FROM,
61             MOVED_TO,
62             CREATE,
63             DELETE,
64             DELETE_SELF,
65             MOVE_SELF
66     })
67     @Retention(RetentionPolicy.SOURCE)
68     public @interface NotifyEventType {}
69 
70     /** Event type: Data was read from a file */
71     public static final int ACCESS = 0x00000001;
72     /** Event type: Data was written to a file */
73     public static final int MODIFY = 0x00000002;
74     /** Event type: Metadata (permissions, owner, timestamp) was changed explicitly */
75     public static final int ATTRIB = 0x00000004;
76     /** Event type: Someone had a file or directory open for writing, and closed it */
77     public static final int CLOSE_WRITE = 0x00000008;
78     /** Event type: Someone had a file or directory open read-only, and closed it */
79     public static final int CLOSE_NOWRITE = 0x00000010;
80     /** Event type: A file or directory was opened */
81     public static final int OPEN = 0x00000020;
82     /** Event type: A file or subdirectory was moved from the monitored directory */
83     public static final int MOVED_FROM = 0x00000040;
84     /** Event type: A file or subdirectory was moved to the monitored directory */
85     public static final int MOVED_TO = 0x00000080;
86     /** Event type: A new file or subdirectory was created under the monitored directory */
87     public static final int CREATE = 0x00000100;
88     /** Event type: A file was deleted from the monitored directory */
89     public static final int DELETE = 0x00000200;
90     /** Event type: The monitored file or directory was deleted; monitoring effectively stops */
91     public static final int DELETE_SELF = 0x00000400;
92     /** Event type: The monitored file or directory was moved; monitoring continues */
93     public static final int MOVE_SELF = 0x00000800;
94 
95     /** Event mask: All valid event types, combined */
96     @NotifyEventType
97     public static final int ALL_EVENTS = ACCESS | MODIFY | ATTRIB | CLOSE_WRITE
98             | CLOSE_NOWRITE | OPEN | MOVED_FROM | MOVED_TO | DELETE | CREATE
99             | DELETE_SELF | MOVE_SELF;
100 
101     private static final String LOG_TAG = "FileObserver";
102 
103     private static class ObserverThread extends Thread {
104         private HashMap<Integer, WeakReference> m_observers = new HashMap<Integer, WeakReference>();
105         private int m_fd;
106 
ObserverThread()107         public ObserverThread() {
108             super("FileObserver");
109             m_fd = init();
110         }
111 
run()112         public void run() {
113             observe(m_fd);
114         }
115 
startWatching(List<File> files, @NotifyEventType int mask, FileObserver observer)116         public int[] startWatching(List<File> files,
117                 @NotifyEventType int mask, FileObserver observer) {
118             final int count = files.size();
119             final String[] paths = new String[count];
120             for (int i = 0; i < count; ++i) {
121                 paths[i] = files.get(i).getAbsolutePath();
122             }
123             final int[] wfds = new int[count];
124             Arrays.fill(wfds, -1);
125 
126             startWatching(m_fd, paths, mask, wfds);
127 
128             final WeakReference<FileObserver> fileObserverWeakReference =
129                     new WeakReference<>(observer);
130             synchronized (m_observers) {
131                 for (int wfd : wfds) {
132                     if (wfd >= 0) {
133                         m_observers.put(wfd, fileObserverWeakReference);
134                     }
135                 }
136             }
137 
138             return wfds;
139         }
140 
stopWatching(int[] descriptors)141         public void stopWatching(int[] descriptors) {
142             stopWatching(m_fd, descriptors);
143         }
144 
145         @UnsupportedAppUsage
onEvent(int wfd, @NotifyEventType int mask, String path)146         public void onEvent(int wfd, @NotifyEventType int mask, String path) {
147             // look up our observer, fixing up the map if necessary...
148             FileObserver observer = null;
149 
150             synchronized (m_observers) {
151                 WeakReference weak = m_observers.get(wfd);
152                 if (weak != null) {  // can happen with lots of events from a dead wfd
153                     observer = (FileObserver) weak.get();
154                     if (observer == null) {
155                         m_observers.remove(wfd);
156                     }
157                 }
158             }
159 
160             // ...then call out to the observer without the sync lock held
161             if (observer != null) {
162                 try {
163                     observer.onEvent(mask, path);
164                 } catch (Throwable throwable) {
165                     Log.wtf(LOG_TAG, "Unhandled exception in FileObserver " + observer, throwable);
166                 }
167             }
168         }
169 
init()170         private native int init();
observe(int fd)171         private native void observe(int fd);
startWatching(int fd, String[] paths, @NotifyEventType int mask, int[] wfds)172         private native void startWatching(int fd, String[] paths,
173                 @NotifyEventType int mask, int[] wfds);
stopWatching(int fd, int[] wfds)174         private native void stopWatching(int fd, int[] wfds);
175     }
176 
177     @UnsupportedAppUsage
178     private static ObserverThread s_observerThread;
179 
180     static {
181         s_observerThread = new ObserverThread();
s_observerThread.start()182         s_observerThread.start();
183     }
184 
185     // instance
186     private final List<File> mFiles;
187     private int[] mDescriptors;
188     private final int mMask;
189 
190     /**
191      * Equivalent to FileObserver(path, FileObserver.ALL_EVENTS).
192      *
193      * @deprecated use {@link #FileObserver(File)} instead.
194      */
195     @Deprecated
FileObserver(String path)196     public FileObserver(String path) {
197         this(new File(path));
198     }
199 
200     /**
201      * Equivalent to FileObserver(file, FileObserver.ALL_EVENTS).
202      */
FileObserver(@onNull File file)203     public FileObserver(@NonNull File file) {
204         this(Arrays.asList(file));
205     }
206 
207     /**
208      * Equivalent to FileObserver(paths, FileObserver.ALL_EVENTS).
209      *
210      * @param files The files or directories to monitor
211      */
FileObserver(@onNull List<File> files)212     public FileObserver(@NonNull List<File> files) {
213         this(files, ALL_EVENTS);
214     }
215 
216     /**
217      * Create a new file observer for a certain file or directory.
218      * Monitoring does not start on creation!  You must call
219      * {@link #startWatching()} before you will receive events.
220      *
221      * @param path The file or directory to monitor
222      * @param mask The event or events (added together) to watch for
223      *
224      * @deprecated use {@link #FileObserver(File, int)} instead.
225      */
226     @Deprecated
FileObserver(String path, @NotifyEventType int mask)227     public FileObserver(String path, @NotifyEventType int mask) {
228         this(new File(path), mask);
229     }
230 
231     /**
232      * Create a new file observer for a certain file or directory.
233      * Monitoring does not start on creation!  You must call
234      * {@link #startWatching()} before you will receive events.
235      *
236      * @param file The file or directory to monitor
237      * @param mask The event or events (added together) to watch for
238      */
FileObserver(@onNull File file, @NotifyEventType int mask)239     public FileObserver(@NonNull File file, @NotifyEventType int mask) {
240         this(Arrays.asList(file), mask);
241     }
242 
243     /**
244      * Version of {@link #FileObserver(File, int)} that allows callers to monitor
245      * multiple files or directories.
246      *
247      * @param files The files or directories to monitor
248      * @param mask The event or events (added together) to watch for
249      */
FileObserver(@onNull List<File> files, @NotifyEventType int mask)250     public FileObserver(@NonNull List<File> files, @NotifyEventType int mask) {
251         mFiles = files;
252         mMask = mask;
253     }
254 
finalize()255     protected void finalize() {
256         stopWatching();
257     }
258 
259     /**
260      * Start watching for events.  The monitored file or directory must exist at
261      * this time, or else no events will be reported (even if it appears later).
262      * If monitoring is already started, this call has no effect.
263      */
startWatching()264     public void startWatching() {
265         if (mDescriptors == null) {
266             mDescriptors = s_observerThread.startWatching(mFiles, mMask, this);
267         }
268     }
269 
270     /**
271      * Stop watching for events.  Some events may be in process, so events
272      * may continue to be reported even after this method completes.  If
273      * monitoring is already stopped, this call has no effect.
274      */
stopWatching()275     public void stopWatching() {
276         if (mDescriptors != null) {
277             s_observerThread.stopWatching(mDescriptors);
278             mDescriptors = null;
279         }
280     }
281 
282     /**
283      * The event handler, which must be implemented by subclasses.
284      *
285      * <p class="note">This method is invoked on a special FileObserver thread.
286      * It runs independently of any threads, so take care to use appropriate
287      * synchronization!  Consider using {@link Handler#post(Runnable)} to shift
288      * event handling work to the main thread to avoid concurrency problems.</p>
289      *
290      * <p>Event handlers must not throw exceptions.</p>
291      *
292      * @param event The type of event which happened
293      * @param path The path, relative to the main monitored file or directory,
294      *     of the file or directory which triggered the event.  This value can
295      *     be {@code null} for certain events, such as {@link #MOVE_SELF}.
296      */
onEvent(int event, @Nullable String path)297     public abstract void onEvent(int event, @Nullable String path);
298 }
299