diff options
| author | Po Lu | 2023-02-10 21:07:14 +0800 |
|---|---|---|
| committer | Po Lu | 2023-02-10 21:07:14 +0800 |
| commit | 2489126e6856bf1b06a26127b73e4bfff857f68f (patch) | |
| tree | dad8edb421f501e0258a7c9a54cc39c508ca32af /java | |
| parent | 9b50500b22ff1f71e80bf6aee98c44493c5ebcce (diff) | |
| download | emacs-2489126e6856bf1b06a26127b73e4bfff857f68f.tar.gz emacs-2489126e6856bf1b06a26127b73e4bfff857f68f.zip | |
Implement more features for the Emacs ``documents provider''
* java/org/gnu/emacs/EmacsDocumentsProvider.java (queryRoots):
Implement isChild.
(getNotificationUri, notifyChange): New functions.
(queryDocument1): Set rename and remove flags.
(queryDocument, queryChildDocuments): Allow the requester to
detect changes in the directory hierarchy.
(createDocument, deleteDocument, removeDocument): Signal changes
to the directory hierarchy.
Diffstat (limited to 'java')
| -rw-r--r-- | java/org/gnu/emacs/EmacsDocumentsProvider.java | 124 |
1 files changed, 101 insertions, 23 deletions
diff --git a/java/org/gnu/emacs/EmacsDocumentsProvider.java b/java/org/gnu/emacs/EmacsDocumentsProvider.java index f12b302ff84..3c3c7ead3c5 100644 --- a/java/org/gnu/emacs/EmacsDocumentsProvider.java +++ b/java/org/gnu/emacs/EmacsDocumentsProvider.java | |||
| @@ -114,7 +114,8 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 114 | 114 | ||
| 115 | /* Add the appropriate flags. */ | 115 | /* Add the appropriate flags. */ |
| 116 | 116 | ||
| 117 | row.add (Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE); | 117 | row.add (Root.COLUMN_FLAGS, (Root.FLAG_SUPPORTS_CREATE |
| 118 | | Root.FLAG_SUPPORTS_IS_CHILD)); | ||
| 118 | row.add (Root.FLAG_LOCAL_ONLY); | 119 | row.add (Root.FLAG_LOCAL_ONLY); |
| 119 | row.add (Root.COLUMN_TITLE, "Emacs"); | 120 | row.add (Root.COLUMN_TITLE, "Emacs"); |
| 120 | row.add (Root.COLUMN_DOCUMENT_ID, baseDir.getAbsolutePath ()); | 121 | row.add (Root.COLUMN_DOCUMENT_ID, baseDir.getAbsolutePath ()); |
| @@ -122,6 +123,36 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 122 | return result; | 123 | return result; |
| 123 | } | 124 | } |
| 124 | 125 | ||
| 126 | private Uri | ||
| 127 | getNotificationUri (File file) | ||
| 128 | { | ||
| 129 | Uri updatedUri; | ||
| 130 | Context context; | ||
| 131 | |||
| 132 | context = getContext (); | ||
| 133 | updatedUri | ||
| 134 | = buildChildDocumentsUri ("org.gnu.emacs", | ||
| 135 | file.getAbsolutePath ()); | ||
| 136 | |||
| 137 | return updatedUri; | ||
| 138 | } | ||
| 139 | |||
| 140 | /* Inform the system that FILE's contents (or FILE itself) has | ||
| 141 | changed. */ | ||
| 142 | |||
| 143 | private void | ||
| 144 | notifyChange (File file) | ||
| 145 | { | ||
| 146 | Uri updatedUri; | ||
| 147 | Context context; | ||
| 148 | |||
| 149 | context = getContext (); | ||
| 150 | updatedUri | ||
| 151 | = buildChildDocumentsUri ("org.gnu.emacs", | ||
| 152 | file.getAbsolutePath ()); | ||
| 153 | context.getContentResolver ().notifyChange (updatedUri, null); | ||
| 154 | } | ||
| 155 | |||
| 125 | /* Return the MIME type of a file FILE. */ | 156 | /* Return the MIME type of a file FILE. */ |
| 126 | 157 | ||
| 127 | private String | 158 | private String |
| @@ -174,6 +205,9 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 174 | { | 205 | { |
| 175 | flags |= Document.FLAG_DIR_SUPPORTS_CREATE; | 206 | flags |= Document.FLAG_DIR_SUPPORTS_CREATE; |
| 176 | flags |= Document.FLAG_SUPPORTS_DELETE; | 207 | flags |= Document.FLAG_SUPPORTS_DELETE; |
| 208 | |||
| 209 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) | ||
| 210 | flags |= Document.FLAG_SUPPORTS_RENAME; | ||
| 177 | } | 211 | } |
| 178 | } | 212 | } |
| 179 | else if (file.canWrite ()) | 213 | else if (file.canWrite ()) |
| @@ -182,13 +216,11 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 182 | flags |= Document.FLAG_SUPPORTS_WRITE; | 216 | flags |= Document.FLAG_SUPPORTS_WRITE; |
| 183 | flags |= Document.FLAG_SUPPORTS_DELETE; | 217 | flags |= Document.FLAG_SUPPORTS_DELETE; |
| 184 | 218 | ||
| 185 | /* TODO: implement these | 219 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) |
| 220 | flags |= Document.FLAG_SUPPORTS_RENAME; | ||
| 186 | 221 | ||
| 187 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) | 222 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) |
| 188 | flags |= Document.FLAG_SUPPORTS_RENAME; | 223 | flags |= Document.FLAG_SUPPORTS_REMOVE; |
| 189 | |||
| 190 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) | ||
| 191 | flags |= Document.FLAG_SUPPORTS_REMOVE; */ | ||
| 192 | } | 224 | } |
| 193 | 225 | ||
| 194 | displayName = file.getName (); | 226 | displayName = file.getName (); |
| @@ -208,12 +240,21 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 208 | throws FileNotFoundException | 240 | throws FileNotFoundException |
| 209 | { | 241 | { |
| 210 | MatrixCursor result; | 242 | MatrixCursor result; |
| 243 | File file; | ||
| 244 | Context context; | ||
| 245 | |||
| 246 | file = new File (documentId); | ||
| 247 | context = getContext (); | ||
| 211 | 248 | ||
| 212 | if (projection == null) | 249 | if (projection == null) |
| 213 | projection = DEFAULT_DOCUMENT_PROJECTION; | 250 | projection = DEFAULT_DOCUMENT_PROJECTION; |
| 214 | 251 | ||
| 215 | result = new MatrixCursor (projection); | 252 | result = new MatrixCursor (projection); |
| 216 | queryDocument1 (result, new File (documentId)); | 253 | queryDocument1 (result, file); |
| 254 | |||
| 255 | /* Now allow interested applications to detect changes. */ | ||
| 256 | result.setNotificationUri (context.getContentResolver (), | ||
| 257 | getNotificationUri (file)); | ||
| 217 | 258 | ||
| 218 | return result; | 259 | return result; |
| 219 | } | 260 | } |
| @@ -225,6 +266,7 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 225 | { | 266 | { |
| 226 | MatrixCursor result; | 267 | MatrixCursor result; |
| 227 | File directory; | 268 | File directory; |
| 269 | Context context; | ||
| 228 | 270 | ||
| 229 | if (projection == null) | 271 | if (projection == null) |
| 230 | projection = DEFAULT_DOCUMENT_PROJECTION; | 272 | projection = DEFAULT_DOCUMENT_PROJECTION; |
| @@ -239,6 +281,12 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 239 | for (File child : directory.listFiles ()) | 281 | for (File child : directory.listFiles ()) |
| 240 | queryDocument1 (result, child); | 282 | queryDocument1 (result, child); |
| 241 | 283 | ||
| 284 | context = getContext (); | ||
| 285 | |||
| 286 | /* Now allow interested applications to detect changes. */ | ||
| 287 | result.setNotificationUri (context.getContentResolver (), | ||
| 288 | getNotificationUri (directory)); | ||
| 289 | |||
| 242 | return result; | 290 | return result; |
| 243 | } | 291 | } |
| 244 | 292 | ||
| @@ -256,12 +304,9 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 256 | createDocument (String documentId, String mimeType, | 304 | createDocument (String documentId, String mimeType, |
| 257 | String displayName) throws FileNotFoundException | 305 | String displayName) throws FileNotFoundException |
| 258 | { | 306 | { |
| 259 | File file; | 307 | File file, parentFile; |
| 260 | boolean rc; | 308 | boolean rc; |
| 261 | Uri updatedUri; | ||
| 262 | Context context; | ||
| 263 | 309 | ||
| 264 | context = getContext (); | ||
| 265 | file = new File (documentId, displayName); | 310 | file = new File (documentId, displayName); |
| 266 | 311 | ||
| 267 | try | 312 | try |
| @@ -293,10 +338,10 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 293 | throw new FileNotFoundException (e.toString ()); | 338 | throw new FileNotFoundException (e.toString ()); |
| 294 | } | 339 | } |
| 295 | 340 | ||
| 296 | updatedUri | 341 | parentFile = file.getParentFile (); |
| 297 | = buildChildDocumentsUri ("org.gnu.emacs", documentId); | 342 | |
| 298 | /* Tell the system about the change. */ | 343 | if (parentFile != null) |
| 299 | context.getContentResolver ().notifyChange (updatedUri, null); | 344 | notifyChange (parentFile); |
| 300 | 345 | ||
| 301 | return file.getAbsolutePath (); | 346 | return file.getAbsolutePath (); |
| 302 | } | 347 | } |
| @@ -333,7 +378,6 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 333 | { | 378 | { |
| 334 | File file, parent; | 379 | File file, parent; |
| 335 | File[] children; | 380 | File[] children; |
| 336 | Uri updatedUri; | ||
| 337 | Context context; | 381 | Context context; |
| 338 | 382 | ||
| 339 | /* Java makes recursively deleting a file hard. File name | 383 | /* Java makes recursively deleting a file hard. File name |
| @@ -347,14 +391,10 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 347 | throw new RuntimeException ("trying to delete file without" | 391 | throw new RuntimeException ("trying to delete file without" |
| 348 | + " parent!"); | 392 | + " parent!"); |
| 349 | 393 | ||
| 350 | updatedUri | ||
| 351 | = buildChildDocumentsUri ("org.gnu.emacs", | ||
| 352 | parent.getAbsolutePath ()); | ||
| 353 | |||
| 354 | if (file.delete ()) | 394 | if (file.delete ()) |
| 355 | { | 395 | { |
| 356 | /* Tell the system about the change. */ | 396 | /* Tell the system about the change. */ |
| 357 | context.getContentResolver ().notifyChange (updatedUri, null); | 397 | notifyChange (parent); |
| 358 | return; | 398 | return; |
| 359 | } | 399 | } |
| 360 | 400 | ||
| @@ -368,7 +408,7 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 368 | 408 | ||
| 369 | if (file.delete ()) | 409 | if (file.delete ()) |
| 370 | /* Tell the system about the change. */ | 410 | /* Tell the system about the change. */ |
| 371 | context.getContentResolver ().notifyChange (updatedUri, null); | 411 | notifyChange (parent); |
| 372 | } | 412 | } |
| 373 | 413 | ||
| 374 | @Override | 414 | @Override |
| @@ -378,4 +418,42 @@ public class EmacsDocumentsProvider extends DocumentsProvider | |||
| 378 | { | 418 | { |
| 379 | deleteDocument (documentId); | 419 | deleteDocument (documentId); |
| 380 | } | 420 | } |
| 421 | |||
| 422 | @Override | ||
| 423 | public String | ||
| 424 | getDocumentType (String documentId) | ||
| 425 | { | ||
| 426 | return getMimeType (new File (documentId)); | ||
| 427 | } | ||
| 428 | |||
| 429 | @Override | ||
| 430 | public String | ||
| 431 | renameDocument (String documentId, String displayName) | ||
| 432 | throws FileNotFoundException | ||
| 433 | { | ||
| 434 | File file, newName; | ||
| 435 | File parent; | ||
| 436 | |||
| 437 | file = new File (documentId); | ||
| 438 | parent = file.getParentFile (); | ||
| 439 | newName = new File (parent, displayName); | ||
| 440 | |||
| 441 | if (parent == null) | ||
| 442 | throw new FileNotFoundException ("parent is null"); | ||
| 443 | |||
| 444 | file = new File (documentId); | ||
| 445 | |||
| 446 | if (!file.renameTo (newName)) | ||
| 447 | return null; | ||
| 448 | |||
| 449 | notifyChange (parent); | ||
| 450 | return newName.getAbsolutePath (); | ||
| 451 | } | ||
| 452 | |||
| 453 | @Override | ||
| 454 | public boolean | ||
| 455 | isChildDocument (String parentDocumentId, String documentId) | ||
| 456 | { | ||
| 457 | return documentId.startsWith (parentDocumentId); | ||
| 458 | } | ||
| 381 | } | 459 | } |