| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.util.Log; |
| 24 |
import android.util.SparseArray; |
| 25 |
|
| 26 |
import java.io.File; |
| 27 |
import java.lang.annotation.Retention; |
| 28 |
import java.lang.annotation.RetentionPolicy; |
| 29 |
import java.lang.ref.WeakReference; |
| 30 |
import java.util.Arrays; |
| 31 |
import java.util.HashMap; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public abstract class FileObserver { |
| 53 |
|
| 54 |
@IntDef(flag = true, value = { |
| 55 |
ACCESS, |
| 56 |
MODIFY, |
| 57 |
ATTRIB, |
| 58 |
CLOSE_WRITE, |
| 59 |
CLOSE_NOWRITE, |
| 60 |
OPEN, |
| 61 |
MOVED_FROM, |
| 62 |
MOVED_TO, |
| 63 |
CREATE, |
| 64 |
DELETE, |
| 65 |
DELETE_SELF, |
| 66 |
MOVE_SELF |
| 67 |
}) |
| 68 |
@Retention(RetentionPolicy.SOURCE) |
| 69 |
public @interface NotifyEventType {} |
| 70 |
|
| 71 |
|
| 72 |
public static final int ACCESS = 0x00000001; |
| 73 |
|
| 74 |
public static final int MODIFY = 0x00000002; |
| 75 |
|
| 76 |
public static final int ATTRIB = 0x00000004; |
| 77 |
|
| 78 |
public static final int CLOSE_WRITE = 0x00000008; |
| 79 |
|
| 80 |
public static final int CLOSE_NOWRITE = 0x00000010; |
| 81 |
|
| 82 |
public static final int OPEN = 0x00000020; |
| 83 |
|
| 84 |
public static final int MOVED_FROM = 0x00000040; |
| 85 |
|
| 86 |
public static final int MOVED_TO = 0x00000080; |
| 87 |
|
| 88 |
public static final int CREATE = 0x00000100; |
| 89 |
|
| 90 |
public static final int DELETE = 0x00000200; |
| 91 |
|
| 92 |
public static final int DELETE_SELF = 0x00000400; |
| 93 |
|
| 94 |
public static final int MOVE_SELF = 0x00000800; |
| 95 |
|
| 96 |
|
| 97 |
@NotifyEventType |
| 98 |
public static final int ALL_EVENTS = ACCESS | MODIFY | ATTRIB | CLOSE_WRITE |
| 99 |
| CLOSE_NOWRITE | OPEN | MOVED_FROM | MOVED_TO | DELETE | CREATE |
| 100 |
| DELETE_SELF | MOVE_SELF; |
| 101 |
|
| 102 |
private static final String LOG_TAG = "FileObserver"; |
| 103 |
|
| 104 |
private static class ObserverThread extends Thread { |
| 105 |
|
| 106 |
private HashMap<Integer, WeakReference> m_observers = new HashMap<Integer, WeakReference>(); |
| 107 |
private SparseArray<WeakReference> mRealObservers = new SparseArray<>(); |
| 108 |
private int m_fd; |
| 109 |
|
| 110 |
public ObserverThread() { |
| 111 |
super("FileObserver"); |
| 112 |
m_fd = init(); |
| 113 |
} |
| 114 |
|
| 115 |
public void run() { |
| 116 |
observe(m_fd); |
| 117 |
} |
| 118 |
|
| 119 |
public int[] startWatching(List<File> files, |
| 120 |
@NotifyEventType int mask, FileObserver observer) { |
| 121 |
final int count = files.size(); |
| 122 |
final String[] paths = new String[count]; |
| 123 |
for (int i = 0; i < count; ++i) { |
| 124 |
paths[i] = files.get(i).getAbsolutePath(); |
| 125 |
} |
| 126 |
final int[] wfds = new int[count]; |
| 127 |
Arrays.fill(wfds, -1); |
| 128 |
|
| 129 |
startWatching(m_fd, paths, mask, wfds); |
| 130 |
|
| 131 |
final WeakReference<FileObserver> fileObserverWeakReference = |
| 132 |
new WeakReference<>(observer); |
| 133 |
synchronized (mRealObservers) { |
| 134 |
for (int wfd : wfds) { |
| 135 |
if (wfd >= 0) { |
| 136 |
mRealObservers.put(wfd, fileObserverWeakReference); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return wfds; |
| 142 |
} |
| 143 |
|
| 144 |
public void stopWatching(int[] descriptors) { |
| 145 |
stopWatching(m_fd, descriptors); |
| 146 |
} |
| 147 |
|
| 148 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 149 |
public void onEvent(int wfd, @NotifyEventType int mask, String path) { |
| 150 |
|
| 151 |
FileObserver observer = null; |
| 152 |
|
| 153 |
synchronized (mRealObservers) { |
| 154 |
WeakReference weak = mRealObservers.get(wfd); |
| 155 |
if (weak != null) { |
| 156 |
observer = (FileObserver) weak.get(); |
| 157 |
if (observer == null) { |
| 158 |
mRealObservers.remove(wfd); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
if (observer != null) { |
| 165 |
try { |
| 166 |
observer.onEvent(mask, path); |
| 167 |
} catch (Throwable throwable) { |
| 168 |
Log.wtf(LOG_TAG, "Unhandled exception in FileObserver " + observer, throwable); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
private native int init(); |
| 174 |
private native void observe(int fd); |
| 175 |
private native void startWatching(int fd, String[] paths, |
| 176 |
@NotifyEventType int mask, int[] wfds); |
| 177 |
private native void stopWatching(int fd, int[] wfds); |
| 178 |
} |
| 179 |
|
| 180 |
@UnsupportedAppUsage |
| 181 |
private static ObserverThread s_observerThread; |
| 182 |
|
| 183 |
static { |
| 184 |
s_observerThread = new ObserverThread(); |
| 185 |
s_observerThread.start(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
private final List<File> mFiles; |
| 190 |
private int[] mDescriptors; |
| 191 |
private final int mMask; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@Deprecated |
| 199 |
public FileObserver(String path) { |
| 200 |
this(new File(path)); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public FileObserver(@NonNull File file) { |
| 207 |
this(Arrays.asList(file)); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public FileObserver(@NonNull List<File> files) { |
| 216 |
this(files, ALL_EVENTS); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@Deprecated |
| 230 |
public FileObserver(String path, @NotifyEventType int mask) { |
| 231 |
this(new File(path), mask); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public FileObserver(@NonNull File file, @NotifyEventType int mask) { |
| 243 |
this(Arrays.asList(file), mask); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public FileObserver(@NonNull List<File> files, @NotifyEventType int mask) { |
| 254 |
mFiles = files; |
| 255 |
mMask = mask; |
| 256 |
} |
| 257 |
|
| 258 |
protected void finalize() { |
| 259 |
stopWatching(); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public void startWatching() { |
| 268 |
if (mDescriptors == null) { |
| 269 |
mDescriptors = s_observerThread.startWatching(mFiles, mMask, this); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public void stopWatching() { |
| 279 |
if (mDescriptors != null) { |
| 280 |
s_observerThread.stopWatching(mDescriptors); |
| 281 |
mDescriptors = null; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public abstract void onEvent(int event, @Nullable String path); |
| 301 |
} |