diff options
| author | Po Lu | 2023-09-13 09:24:16 +0800 |
|---|---|---|
| committer | Po Lu | 2023-09-13 09:30:14 +0800 |
| commit | 9500fbe231ec32c66e619b7fc8b221d163d5acca (patch) | |
| tree | 9ccf5ca16c278933329fa811c2ba5ceb33c83786 /java | |
| parent | f542a4537e430cee38d8ddb9c64e0bef393ae03a (diff) | |
| download | emacs-9500fbe231ec32c66e619b7fc8b221d163d5acca.tar.gz emacs-9500fbe231ec32c66e619b7fc8b221d163d5acca.zip | |
Facilitate opening content:// files without a linked file name
* java/org/gnu/emacs/EmacsOpenActivity.java
(checkReadableOrCopy): If FILE is NULL, return a matching
content URI if possible, else generate a file name with the URI
as a reference.
(onCreate): Catch SecurityException around calls to
openFileDescriptor.
Diffstat (limited to 'java')
| -rw-r--r-- | java/org/gnu/emacs/EmacsOpenActivity.java | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/java/org/gnu/emacs/EmacsOpenActivity.java b/java/org/gnu/emacs/EmacsOpenActivity.java index ca6d99e20b7..00503b6cbcc 100644 --- a/java/org/gnu/emacs/EmacsOpenActivity.java +++ b/java/org/gnu/emacs/EmacsOpenActivity.java | |||
| @@ -213,12 +213,13 @@ public final class EmacsOpenActivity extends Activity | |||
| 213 | dialog.show (); | 213 | dialog.show (); |
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | /* Check that the specified FILE is readable. If Android 4.4 or | 216 | /* Check that the specified FILE is non-NULL and readable. |
| 217 | later is being used, return URI formatted into a `/content/' file | ||
| 218 | name. | ||
| 219 | 217 | ||
| 220 | If it is not, then copy the file in FD to a location in the | 218 | If it is not, then copy the file in FD to a location in the |
| 221 | system cache directory and return the name of that file. */ | 219 | system cache directory and return the name of that file. |
| 220 | |||
| 221 | Alternatively, return URI formatted into a `/content/' file name | ||
| 222 | if the system runs Android 4.4 or later. */ | ||
| 222 | 223 | ||
| 223 | private String | 224 | private String |
| 224 | checkReadableOrCopy (String file, ParcelFileDescriptor fd, | 225 | checkReadableOrCopy (String file, ParcelFileDescriptor fd, |
| @@ -232,10 +233,19 @@ public final class EmacsOpenActivity extends Activity | |||
| 232 | int read; | 233 | int read; |
| 233 | String content; | 234 | String content; |
| 234 | 235 | ||
| 235 | inFile = new File (file); | 236 | if (file != null) |
| 237 | { | ||
| 238 | inFile = new File (file); | ||
| 239 | |||
| 240 | if (inFile.canRead ()) | ||
| 241 | return file; | ||
| 236 | 242 | ||
| 237 | if (inFile.canRead ()) | 243 | content = inFile.getName (); |
| 238 | return file; | 244 | } |
| 245 | else | ||
| 246 | /* content is the name of this content file if the next branch | ||
| 247 | is not taken. */ | ||
| 248 | content = uri.getLastPathSegment (); | ||
| 239 | 249 | ||
| 240 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) | 250 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) |
| 241 | { | 251 | { |
| @@ -243,8 +253,14 @@ public final class EmacsOpenActivity extends Activity | |||
| 243 | return content; | 253 | return content; |
| 244 | } | 254 | } |
| 245 | 255 | ||
| 256 | /* The file is unnamed if content is NULL. Generate a unique | ||
| 257 | name with the current time as a reference. */ | ||
| 258 | |||
| 259 | if (content == null) | ||
| 260 | content = "content." + System.currentTimeMillis () / 1000; | ||
| 261 | |||
| 246 | /* inFile is now the file being written to. */ | 262 | /* inFile is now the file being written to. */ |
| 247 | inFile = new File (getCacheDir (), inFile.getName ()); | 263 | inFile = new File (getCacheDir (), content); |
| 248 | buffer = new byte[4098]; | 264 | buffer = new byte[4098]; |
| 249 | 265 | ||
| 250 | /* Initialize both streams to NULL. */ | 266 | /* Initialize both streams to NULL. */ |
| @@ -464,6 +480,14 @@ public final class EmacsOpenActivity extends Activity | |||
| 464 | { | 480 | { |
| 465 | /* Do nothing. */ | 481 | /* Do nothing. */ |
| 466 | } | 482 | } |
| 483 | catch (SecurityException exception) | ||
| 484 | { | ||
| 485 | /* This means Emacs lacks the rights to open this | ||
| 486 | file. Display the error message and exit. */ | ||
| 487 | displayFailureDialog ("Error openining file", | ||
| 488 | exception.toString ()); | ||
| 489 | return; | ||
| 490 | } | ||
| 467 | 491 | ||
| 468 | if (fd != null) | 492 | if (fd != null) |
| 469 | { | 493 | { |