diff options
| author | Po Lu | 2024-05-01 11:45:53 +0800 |
|---|---|---|
| committer | Po Lu | 2024-05-01 11:46:31 +0800 |
| commit | 2451456695d0e03b89365cbbe64effb2f99af2d5 (patch) | |
| tree | b6bcd3e227454b436ab4ee0fad447d946b5179cf /java/org | |
| parent | 294335b2304028cc97aca036bd37adf2f8e1c508 (diff) | |
| download | emacs-2451456695d0e03b89365cbbe64effb2f99af2d5.tar.gz emacs-2451456695d0e03b89365cbbe64effb2f99af2d5.zip | |
Fix compatibility issues with Android clipboards
* java/org/gnu/emacs/EmacsClipboard.java (getClipboardData):
Return an AssetFileDescriptor.
* java/org/gnu/emacs/EmacsContextMenu.java (onMenuItemClick):
Typo corrections in commentary.
* java/org/gnu/emacs/EmacsOpenActivity.java (onCreate): Raise
minimum version on which to read file descriptors from
ParcelFileDescriptor objects to Honeycomb.
* java/org/gnu/emacs/EmacsSdk11Clipboard.java
(getClipboardData): Return the asset file descriptor.
* java/org/gnu/emacs/EmacsSdk8Clipboard.java (getClipboardData):
Adjust return type to match.
* src/android.h (struct android_parcel_file_descriptor_class):
Move from androidselect.c.
* src/androidselect.c (fd_class): Export function.
(android_init_emacs_clipboard): Adjust signature of
getClipboardData.
(android_init_asset_file_descriptor, close_asset_fd)
(extract_fd_offsets): New functions.
(Fandroid_get_clipboard_data): Extract file descriptor and
offset from the AssetFileDescriptor here, rather than in
getClipboardData.
(init_androidselect): Call android_init_asset_file_descriptor.
* src/androidvfs.c (android_init_fd_class): Export and enable
calling this function more than once.
Diffstat (limited to 'java/org')
| -rw-r--r-- | java/org/gnu/emacs/EmacsClipboard.java | 3 | ||||
| -rw-r--r-- | java/org/gnu/emacs/EmacsContextMenu.java | 4 | ||||
| -rw-r--r-- | java/org/gnu/emacs/EmacsOpenActivity.java | 39 | ||||
| -rw-r--r-- | java/org/gnu/emacs/EmacsSdk11Clipboard.java | 50 | ||||
| -rw-r--r-- | java/org/gnu/emacs/EmacsSdk8Clipboard.java | 11 |
5 files changed, 33 insertions, 74 deletions
diff --git a/java/org/gnu/emacs/EmacsClipboard.java b/java/org/gnu/emacs/EmacsClipboard.java index 9db436ca1e2..f27d96129ef 100644 --- a/java/org/gnu/emacs/EmacsClipboard.java +++ b/java/org/gnu/emacs/EmacsClipboard.java | |||
| @@ -19,6 +19,7 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */ | |||
| 19 | 19 | ||
| 20 | package org.gnu.emacs; | 20 | package org.gnu.emacs; |
| 21 | 21 | ||
| 22 | import android.content.res.AssetFileDescriptor; | ||
| 22 | import android.os.Build; | 23 | import android.os.Build; |
| 23 | 24 | ||
| 24 | /* This class provides helper code for accessing the clipboard, | 25 | /* This class provides helper code for accessing the clipboard, |
| @@ -32,7 +33,7 @@ public abstract class EmacsClipboard | |||
| 32 | public abstract byte[] getClipboard (); | 33 | public abstract byte[] getClipboard (); |
| 33 | 34 | ||
| 34 | public abstract byte[][] getClipboardTargets (); | 35 | public abstract byte[][] getClipboardTargets (); |
| 35 | public abstract long[] getClipboardData (byte[] target); | 36 | public abstract AssetFileDescriptor getClipboardData (byte[] target); |
| 36 | 37 | ||
| 37 | /* Create the correct kind of clipboard for this system. */ | 38 | /* Create the correct kind of clipboard for this system. */ |
| 38 | 39 | ||
diff --git a/java/org/gnu/emacs/EmacsContextMenu.java b/java/org/gnu/emacs/EmacsContextMenu.java index 2bbf2a313d6..0f52d45455f 100644 --- a/java/org/gnu/emacs/EmacsContextMenu.java +++ b/java/org/gnu/emacs/EmacsContextMenu.java | |||
| @@ -108,8 +108,8 @@ public final class EmacsContextMenu | |||
| 108 | will normally confuse Emacs into thinking that the | 108 | will normally confuse Emacs into thinking that the |
| 109 | context menu has been dismissed. Wrong! | 109 | context menu has been dismissed. Wrong! |
| 110 | 110 | ||
| 111 | Setting this flag makes EmacsActivity to only handle | 111 | Setting this flag prompts EmacsActivity to only handle |
| 112 | SubMenuBuilder being closed, which always means the menu | 112 | SubMenuBuilders being closed, which always means the menu |
| 113 | has actually been dismissed. | 113 | has actually been dismissed. |
| 114 | 114 | ||
| 115 | However, these extraneous events aren't sent on devices | 115 | However, these extraneous events aren't sent on devices |
diff --git a/java/org/gnu/emacs/EmacsOpenActivity.java b/java/org/gnu/emacs/EmacsOpenActivity.java index 327a53bc417..cdc68aea2bf 100644 --- a/java/org/gnu/emacs/EmacsOpenActivity.java +++ b/java/org/gnu/emacs/EmacsOpenActivity.java | |||
| @@ -19,29 +19,23 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */ | |||
| 19 | 19 | ||
| 20 | package org.gnu.emacs; | 20 | package org.gnu.emacs; |
| 21 | 21 | ||
| 22 | /* This class makes the Emacs server work reasonably on Android. | 22 | /* Opening external documents on Android. |
| 23 | 23 | ||
| 24 | There is no way to make the Unix socket publicly available on | 24 | This activity is registered as an application capable of opening text |
| 25 | Android. | 25 | files and files in several other formats that Emacs understands, and |
| 26 | assumes responsibility for deriving file names from the files | ||
| 27 | provided to `onCreate', potentially copying them to temporary | ||
| 28 | directories in the process, and invoking `emacsclient' with suitable | ||
| 29 | arguments to open the same. In this respect, it fills the role of | ||
| 30 | `etc/emacs.desktop' on XDG systems. | ||
| 26 | 31 | ||
| 27 | Instead, this activity tries to connect to the Emacs server, to | 32 | It is also registered as a handler for mailto URIs, in which capacity |
| 28 | make it open files the system asks Emacs to open, and to emulate | 33 | it constructs invocations of `emacsclient' so as to start |
| 29 | some reasonable behavior when Emacs has not yet started. | 34 | `message-mailto' with their contents and attachments, much like |
| 35 | `etc/emacs-mail.desktop'. | ||
| 30 | 36 | ||
| 31 | First, Emacs registers itself as an application that can open text | 37 | As with all other activities, it is registered in the package |
| 32 | and image files. | 38 | manifest file. */ |
| 33 | |||
| 34 | Then, when the user is asked to open a file and selects ``Emacs'' | ||
| 35 | as the application that will open the file, the system pops up a | ||
| 36 | window, this activity, and calls the `onCreate' function. | ||
| 37 | |||
| 38 | `onCreate' then tries very to find the file name of the file that | ||
| 39 | was selected, and give it to emacsclient. | ||
| 40 | |||
| 41 | If emacsclient successfully opens the file, then this activity | ||
| 42 | starts EmacsActivity (to bring it on to the screen); otherwise, it | ||
| 43 | displays the output of emacsclient or any error message that occurs | ||
| 44 | and exits. */ | ||
| 45 | 39 | ||
| 46 | import android.app.AlertDialog; | 40 | import android.app.AlertDialog; |
| 47 | import android.app.Activity; | 41 | import android.app.Activity; |
| @@ -628,11 +622,12 @@ public final class EmacsOpenActivity extends Activity | |||
| 628 | 622 | ||
| 629 | if (scheme.equals ("content") | 623 | if (scheme.equals ("content") |
| 630 | /* Retrieving the native file descriptor of a | 624 | /* Retrieving the native file descriptor of a |
| 631 | ParcelFileDescriptor requires Honeycomb, and | 625 | ParcelFileDescriptor requires Honeycomb MR1, and |
| 632 | proceeding without this capability is pointless on | 626 | proceeding without this capability is pointless on |
| 633 | systems before KitKat, since Emacs doesn't support | 627 | systems before KitKat, since Emacs doesn't support |
| 634 | opening content files on those. */ | 628 | opening content files on those. */ |
| 635 | && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) | 629 | && (Build.VERSION.SDK_INT |
| 630 | >= Build.VERSION_CODES.HONEYCOMB_MR1)) | ||
| 636 | { | 631 | { |
| 637 | /* This is one of the annoying Android ``content'' | 632 | /* This is one of the annoying Android ``content'' |
| 638 | URIs. Most of the time, there is actually an | 633 | URIs. Most of the time, there is actually an |
diff --git a/java/org/gnu/emacs/EmacsSdk11Clipboard.java b/java/org/gnu/emacs/EmacsSdk11Clipboard.java index 850bb6c8deb..71381b0f114 100644 --- a/java/org/gnu/emacs/EmacsSdk11Clipboard.java +++ b/java/org/gnu/emacs/EmacsSdk11Clipboard.java | |||
| @@ -207,8 +207,9 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard | |||
| 207 | /* Return the clipboard data for the given target, or NULL if it | 207 | /* Return the clipboard data for the given target, or NULL if it |
| 208 | does not exist. | 208 | does not exist. |
| 209 | 209 | ||
| 210 | Value is normally an array of three longs: the file descriptor, | 210 | Value is normally an asset file descriptor, which in turn holds |
| 211 | the start offset of the data, and its length; length may be | 211 | three important values: the file descriptor, the start offset of |
| 212 | the data, and its length; length may be | ||
| 212 | AssetFileDescriptor.UNKNOWN_LENGTH, meaning that the data extends | 213 | AssetFileDescriptor.UNKNOWN_LENGTH, meaning that the data extends |
| 213 | from that offset to the end of the file. | 214 | from that offset to the end of the file. |
| 214 | 215 | ||
| @@ -217,15 +218,13 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard | |||
| 217 | solely of a URI. */ | 218 | solely of a URI. */ |
| 218 | 219 | ||
| 219 | @Override | 220 | @Override |
| 220 | public long[] | 221 | public AssetFileDescriptor |
| 221 | getClipboardData (byte[] target) | 222 | getClipboardData (byte[] target) |
| 222 | { | 223 | { |
| 223 | ClipData data; | 224 | ClipData data; |
| 224 | String mimeType; | 225 | String mimeType; |
| 225 | int fd; | ||
| 226 | AssetFileDescriptor assetFd; | 226 | AssetFileDescriptor assetFd; |
| 227 | Uri uri; | 227 | Uri uri; |
| 228 | long[] value; | ||
| 229 | 228 | ||
| 230 | /* Decode the target given by Emacs. */ | 229 | /* Decode the target given by Emacs. */ |
| 231 | try | 230 | try |
| @@ -245,8 +244,6 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard | |||
| 245 | if (data == null || data.getItemCount () < 1) | 244 | if (data == null || data.getItemCount () < 1) |
| 246 | return null; | 245 | return null; |
| 247 | 246 | ||
| 248 | fd = -1; | ||
| 249 | |||
| 250 | try | 247 | try |
| 251 | { | 248 | { |
| 252 | uri = data.getItemAt (0).getUri (); | 249 | uri = data.getItemAt (0).getUri (); |
| @@ -257,52 +254,15 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard | |||
| 257 | /* Now open the file descriptor. */ | 254 | /* Now open the file descriptor. */ |
| 258 | assetFd = resolver.openTypedAssetFileDescriptor (uri, mimeType, | 255 | assetFd = resolver.openTypedAssetFileDescriptor (uri, mimeType, |
| 259 | null); | 256 | null); |
| 260 | 257 | return assetFd; | |
| 261 | /* Duplicate the file descriptor. */ | ||
| 262 | fd = assetFd.getParcelFileDescriptor ().getFd (); | ||
| 263 | fd = EmacsNative.dup (fd); | ||
| 264 | |||
| 265 | /* Return the relevant information. */ | ||
| 266 | value = new long[] { fd, assetFd.getStartOffset (), | ||
| 267 | assetFd.getLength (), }; | ||
| 268 | |||
| 269 | /* Close the original offset. */ | ||
| 270 | assetFd.close (); | ||
| 271 | } | 258 | } |
| 272 | catch (SecurityException e) | 259 | catch (SecurityException e) |
| 273 | { | 260 | { |
| 274 | /* Guarantee a file descriptor duplicated or detached is | ||
| 275 | ultimately closed if an error arises. */ | ||
| 276 | |||
| 277 | if (fd != -1) | ||
| 278 | EmacsNative.close (fd); | ||
| 279 | |||
| 280 | return null; | 261 | return null; |
| 281 | } | 262 | } |
| 282 | catch (FileNotFoundException e) | 263 | catch (FileNotFoundException e) |
| 283 | { | 264 | { |
| 284 | /* Guarantee a file descriptor duplicated or detached is | ||
| 285 | ultimately closed if an error arises. */ | ||
| 286 | |||
| 287 | if (fd != -1) | ||
| 288 | EmacsNative.close (fd); | ||
| 289 | |||
| 290 | return null; | 265 | return null; |
| 291 | } | 266 | } |
| 292 | catch (IOException e) | ||
| 293 | { | ||
| 294 | /* Guarantee a file descriptor duplicated or detached is | ||
| 295 | ultimately closed if an error arises. */ | ||
| 296 | |||
| 297 | if (fd != -1) | ||
| 298 | EmacsNative.close (fd); | ||
| 299 | |||
| 300 | return null; | ||
| 301 | } | ||
| 302 | |||
| 303 | /* Don't return value if the file descriptor couldn't be | ||
| 304 | created. */ | ||
| 305 | |||
| 306 | return fd != -1 ? value : null; | ||
| 307 | } | 267 | } |
| 308 | }; | 268 | }; |
diff --git a/java/org/gnu/emacs/EmacsSdk8Clipboard.java b/java/org/gnu/emacs/EmacsSdk8Clipboard.java index 418f55c12c1..3d0504b1924 100644 --- a/java/org/gnu/emacs/EmacsSdk8Clipboard.java +++ b/java/org/gnu/emacs/EmacsSdk8Clipboard.java | |||
| @@ -25,6 +25,8 @@ package org.gnu.emacs; | |||
| 25 | import android.text.*; | 25 | import android.text.*; |
| 26 | 26 | ||
| 27 | import android.content.Context; | 27 | import android.content.Context; |
| 28 | import android.content.res.AssetFileDescriptor; | ||
| 29 | |||
| 28 | import android.util.Log; | 30 | import android.util.Log; |
| 29 | 31 | ||
| 30 | import java.io.UnsupportedEncodingException; | 32 | import java.io.UnsupportedEncodingException; |
| @@ -129,9 +131,10 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard | |||
| 129 | /* Return the clipboard data for the given target, or NULL if it | 131 | /* Return the clipboard data for the given target, or NULL if it |
| 130 | does not exist. | 132 | does not exist. |
| 131 | 133 | ||
| 132 | Value is normally an array of three longs: the file descriptor, | 134 | Value is normally an asset file descriptor, which in turn holds |
| 133 | the start offset of the data, and its length; length may be | 135 | three important values: the file descriptor, the start offset of |
| 134 | AssetFileDescriptor.UNKOWN_LENGTH, meaning that the data extends | 136 | the data, and its length; length may be |
| 137 | AssetFileDescriptor.UNKNOWN_LENGTH, meaning that the data extends | ||
| 135 | from that offset to the end of the file. | 138 | from that offset to the end of the file. |
| 136 | 139 | ||
| 137 | Do not use this function to open text targets; use `getClipboard' | 140 | Do not use this function to open text targets; use `getClipboard' |
| @@ -139,7 +142,7 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard | |||
| 139 | solely of a URI. */ | 142 | solely of a URI. */ |
| 140 | 143 | ||
| 141 | @Override | 144 | @Override |
| 142 | public long[] | 145 | public AssetFileDescriptor |
| 143 | getClipboardData (byte[] target) | 146 | getClipboardData (byte[] target) |
| 144 | { | 147 | { |
| 145 | return null; | 148 | return null; |