diff options
| author | Po Lu | 2024-05-18 20:45:35 +0800 |
|---|---|---|
| committer | Po Lu | 2024-05-18 20:45:35 +0800 |
| commit | 8dc00dc22250b383e7692fc761cbca25b014aa8a (patch) | |
| tree | a016100ec536856ccbb3c7334b63e0595b8b549f | |
| parent | 5ec4c1a7d3f0d184982f4efa0c01bc9a76197533 (diff) | |
| download | emacs-8dc00dc22250b383e7692fc761cbca25b014aa8a.tar.gz emacs-8dc00dc22250b383e7692fc761cbca25b014aa8a.zip | |
Housekeeping around androidselect.c
* java/org/gnu/emacs/EmacsClipboard.java (setClipboard)
(getClipboard):
* java/org/gnu/emacs/EmacsSdk11Clipboard.java (setClipboard)
(getClipboard):
* java/org/gnu/emacs/EmacsSdk8Clipboard.java (setClipboard)
(getClipboard): Save and return Strings rather than byte arrays.
* src/androidselect.c (android_init_emacs_clipboard)
(Fandroid_set_clipboard, Fandroid_get_clipboard): Adjust to
match.
| -rw-r--r-- | java/org/gnu/emacs/EmacsClipboard.java | 4 | ||||
| -rw-r--r-- | java/org/gnu/emacs/EmacsSdk11Clipboard.java | 47 | ||||
| -rw-r--r-- | java/org/gnu/emacs/EmacsSdk8Clipboard.java | 29 | ||||
| -rw-r--r-- | src/androidselect.c | 56 |
4 files changed, 49 insertions, 87 deletions
diff --git a/java/org/gnu/emacs/EmacsClipboard.java b/java/org/gnu/emacs/EmacsClipboard.java index 86553f478ed..2560ef793c2 100644 --- a/java/org/gnu/emacs/EmacsClipboard.java +++ b/java/org/gnu/emacs/EmacsClipboard.java | |||
| @@ -27,10 +27,10 @@ import android.os.Build; | |||
| 27 | 27 | ||
| 28 | public abstract class EmacsClipboard | 28 | public abstract class EmacsClipboard |
| 29 | { | 29 | { |
| 30 | public abstract void setClipboard (byte[] bytes); | 30 | public abstract void setClipboard (String string); |
| 31 | public abstract int ownsClipboard (); | 31 | public abstract int ownsClipboard (); |
| 32 | public abstract boolean clipboardExists (); | 32 | public abstract boolean clipboardExists (); |
| 33 | public abstract byte[] getClipboard (); | 33 | public abstract String getClipboard (); |
| 34 | 34 | ||
| 35 | public abstract String[] getClipboardTargets (); | 35 | public abstract String[] getClipboardTargets (); |
| 36 | public abstract AssetFileDescriptor getClipboardData (String target); | 36 | public abstract AssetFileDescriptor getClipboardData (String target); |
diff --git a/java/org/gnu/emacs/EmacsSdk11Clipboard.java b/java/org/gnu/emacs/EmacsSdk11Clipboard.java index dfc714476ec..e179551c14d 100644 --- a/java/org/gnu/emacs/EmacsSdk11Clipboard.java +++ b/java/org/gnu/emacs/EmacsSdk11Clipboard.java | |||
| @@ -86,32 +86,23 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard | |||
| 86 | } | 86 | } |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | /* Set the clipboard text to CLIPBOARD, a string in UTF-8 | 89 | /* Save the STRING into the clipboard by way of text copied by the |
| 90 | encoding. */ | 90 | user. */ |
| 91 | 91 | ||
| 92 | @Override | 92 | @Override |
| 93 | public synchronized void | 93 | public synchronized void |
| 94 | setClipboard (byte[] bytes) | 94 | setClipboard (String string) |
| 95 | { | 95 | { |
| 96 | ClipData data; | 96 | ClipData data; |
| 97 | String string; | ||
| 98 | 97 | ||
| 99 | try | 98 | data = ClipData.newPlainText ("Emacs", string); |
| 100 | { | 99 | manager.setPrimaryClip (data); |
| 101 | string = new String (bytes, "UTF-8"); | 100 | ownsClipboard = true; |
| 102 | data = ClipData.newPlainText ("Emacs", string); | 101 | |
| 103 | manager.setPrimaryClip (data); | 102 | /* onPrimaryClipChanged will be called again. Use this |
| 104 | ownsClipboard = true; | 103 | variable to keep track of how many times the clipboard has |
| 105 | 104 | been changed. */ | |
| 106 | /* onPrimaryClipChanged will be called again. Use this | 105 | ++clipboardChangedCount; |
| 107 | variable to keep track of how many times the clipboard has | ||
| 108 | been changed. */ | ||
| 109 | ++clipboardChangedCount; | ||
| 110 | } | ||
| 111 | catch (UnsupportedEncodingException exception) | ||
| 112 | { | ||
| 113 | Log.w (TAG, "setClipboard: " + exception); | ||
| 114 | } | ||
| 115 | } | 106 | } |
| 116 | 107 | ||
| 117 | /* Return whether or not Emacs owns the clipboard. Value is 1 if | 108 | /* Return whether or not Emacs owns the clipboard. Value is 1 if |
| @@ -141,7 +132,7 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard | |||
| 141 | NULL if no content is available. */ | 132 | NULL if no content is available. */ |
| 142 | 133 | ||
| 143 | @Override | 134 | @Override |
| 144 | public byte[] | 135 | public String |
| 145 | getClipboard () | 136 | getClipboard () |
| 146 | { | 137 | { |
| 147 | ClipData clip; | 138 | ClipData clip; |
| @@ -154,18 +145,8 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard | |||
| 154 | return null; | 145 | return null; |
| 155 | 146 | ||
| 156 | context = EmacsService.SERVICE; | 147 | context = EmacsService.SERVICE; |
| 157 | 148 | text = clip.getItemAt (0).coerceToText (context); | |
| 158 | try | 149 | return text.toString (); |
| 159 | { | ||
| 160 | text = clip.getItemAt (0).coerceToText (context); | ||
| 161 | return text.toString ().getBytes ("UTF-8"); | ||
| 162 | } | ||
| 163 | catch (UnsupportedEncodingException exception) | ||
| 164 | { | ||
| 165 | Log.w (TAG, "getClipboard: " + exception); | ||
| 166 | } | ||
| 167 | |||
| 168 | return null; | ||
| 169 | } | 150 | } |
| 170 | 151 | ||
| 171 | /* Return an array of targets currently provided by the | 152 | /* Return an array of targets currently provided by the |
diff --git a/java/org/gnu/emacs/EmacsSdk8Clipboard.java b/java/org/gnu/emacs/EmacsSdk8Clipboard.java index 344ec6f7997..afd235babf5 100644 --- a/java/org/gnu/emacs/EmacsSdk8Clipboard.java +++ b/java/org/gnu/emacs/EmacsSdk8Clipboard.java | |||
| @@ -52,21 +52,14 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard | |||
| 52 | = (ClipboardManager) context.getSystemService (what); | 52 | = (ClipboardManager) context.getSystemService (what); |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | /* Set the clipboard text to CLIPBOARD, a string in UTF-8 | 55 | /* Save the STRING into the clipboard by way of text copied by the |
| 56 | encoding. */ | 56 | user. */ |
| 57 | 57 | ||
| 58 | @Override | 58 | @Override |
| 59 | public void | 59 | public void |
| 60 | setClipboard (byte[] bytes) | 60 | setClipboard (String string) |
| 61 | { | 61 | { |
| 62 | try | 62 | manager.setText (string); |
| 63 | { | ||
| 64 | manager.setText (new String (bytes, "UTF-8")); | ||
| 65 | } | ||
| 66 | catch (UnsupportedEncodingException exception) | ||
| 67 | { | ||
| 68 | Log.w (TAG, "setClipboard: " + exception); | ||
| 69 | } | ||
| 70 | } | 63 | } |
| 71 | 64 | ||
| 72 | /* Return whether or not Emacs owns the clipboard. Value is 1 if | 65 | /* Return whether or not Emacs owns the clipboard. Value is 1 if |
| @@ -93,7 +86,7 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard | |||
| 93 | NULL if no content is available. */ | 86 | NULL if no content is available. */ |
| 94 | 87 | ||
| 95 | @Override | 88 | @Override |
| 96 | public byte[] | 89 | public String |
| 97 | getClipboard () | 90 | getClipboard () |
| 98 | { | 91 | { |
| 99 | String string; | 92 | String string; |
| @@ -105,17 +98,7 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard | |||
| 105 | return null; | 98 | return null; |
| 106 | 99 | ||
| 107 | string = text.toString (); | 100 | string = text.toString (); |
| 108 | 101 | return string; | |
| 109 | try | ||
| 110 | { | ||
| 111 | return string.getBytes ("UTF-8"); | ||
| 112 | } | ||
| 113 | catch (UnsupportedEncodingException exception) | ||
| 114 | { | ||
| 115 | Log.w (TAG, "getClipboard: " + exception); | ||
| 116 | } | ||
| 117 | |||
| 118 | return null; | ||
| 119 | } | 102 | } |
| 120 | 103 | ||
| 121 | /* Return an array of targets currently provided by the | 104 | /* Return an array of targets currently provided by the |
diff --git a/src/androidselect.c b/src/androidselect.c index 7c93607848a..50982738743 100644 --- a/src/androidselect.c +++ b/src/androidselect.c | |||
| @@ -94,10 +94,10 @@ android_init_emacs_clipboard (void) | |||
| 94 | name, signature); \ | 94 | name, signature); \ |
| 95 | eassert (clipboard_class.c_name); | 95 | eassert (clipboard_class.c_name); |
| 96 | 96 | ||
| 97 | FIND_METHOD (set_clipboard, "setClipboard", "([B)V"); | 97 | FIND_METHOD (set_clipboard, "setClipboard", "(Ljava/lang/String;)V"); |
| 98 | FIND_METHOD (owns_clipboard, "ownsClipboard", "()I"); | 98 | FIND_METHOD (owns_clipboard, "ownsClipboard", "()I"); |
| 99 | FIND_METHOD (clipboard_exists, "clipboardExists", "()Z"); | 99 | FIND_METHOD (clipboard_exists, "clipboardExists", "()Z"); |
| 100 | FIND_METHOD (get_clipboard, "getClipboard", "()[B"); | 100 | FIND_METHOD (get_clipboard, "getClipboard", "()Ljava/lang/String;"); |
| 101 | FIND_METHOD (get_clipboard_targets, "getClipboardTargets", | 101 | FIND_METHOD (get_clipboard_targets, "getClipboardTargets", |
| 102 | "()[Ljava/lang/String;"); | 102 | "()[Ljava/lang/String;"); |
| 103 | FIND_METHOD (get_clipboard_data, "getClipboardData", | 103 | FIND_METHOD (get_clipboard_data, "getClipboardData", |
| @@ -151,28 +151,26 @@ DEFUN ("android-set-clipboard", Fandroid_set_clipboard, | |||
| 151 | doc: /* Set the clipboard text to STRING. */) | 151 | doc: /* Set the clipboard text to STRING. */) |
| 152 | (Lisp_Object string) | 152 | (Lisp_Object string) |
| 153 | { | 153 | { |
| 154 | jarray bytes; | 154 | jstring text; |
| 155 | 155 | ||
| 156 | if (!android_init_gui) | 156 | if (!android_init_gui) |
| 157 | error ("Accessing clipboard without display connection"); | 157 | error ("Accessing clipboard without display connection"); |
| 158 | 158 | ||
| 159 | CHECK_STRING (string); | 159 | CHECK_STRING (string); |
| 160 | string = ENCODE_UTF_8 (string); | 160 | string = code_convert_string_norecord (string, Qandroid_jni, |
| 161 | true); | ||
| 161 | 162 | ||
| 162 | bytes = (*android_java_env)->NewByteArray (android_java_env, | 163 | text = (*android_java_env)->NewStringUTF (android_java_env, |
| 163 | SBYTES (string)); | 164 | SSDATA (string)); |
| 164 | android_exception_check (); | 165 | android_exception_check (); |
| 165 | 166 | ||
| 166 | (*android_java_env)->SetByteArrayRegion (android_java_env, bytes, | ||
| 167 | 0, SBYTES (string), | ||
| 168 | (jbyte *) SDATA (string)); | ||
| 169 | (*android_java_env)->CallVoidMethod (android_java_env, | 167 | (*android_java_env)->CallVoidMethod (android_java_env, |
| 170 | clipboard, | 168 | clipboard, |
| 171 | clipboard_class.set_clipboard, | 169 | clipboard_class.set_clipboard, |
| 172 | bytes); | 170 | text); |
| 173 | android_exception_check_1 (bytes); | 171 | android_exception_check_1 (text); |
| 172 | ANDROID_DELETE_LOCAL_REF (text); | ||
| 174 | 173 | ||
| 175 | ANDROID_DELETE_LOCAL_REF (bytes); | ||
| 176 | return Qnil; | 174 | return Qnil; |
| 177 | } | 175 | } |
| 178 | 176 | ||
| @@ -185,39 +183,39 @@ Alternatively, return nil if the clipboard is empty. */) | |||
| 185 | (void) | 183 | (void) |
| 186 | { | 184 | { |
| 187 | Lisp_Object string; | 185 | Lisp_Object string; |
| 188 | jarray bytes; | 186 | jstring text; |
| 189 | jmethodID method; | 187 | jmethodID method; |
| 190 | size_t length; | 188 | jsize length; |
| 191 | jbyte *data; | 189 | const char *data; |
| 192 | 190 | ||
| 193 | if (!android_init_gui) | 191 | if (!android_init_gui) |
| 194 | error ("No Android display connection!"); | 192 | error ("No Android display connection!"); |
| 195 | 193 | ||
| 196 | method = clipboard_class.get_clipboard; | 194 | method = clipboard_class.get_clipboard; |
| 197 | bytes | 195 | text |
| 198 | = (*android_java_env)->CallObjectMethod (android_java_env, | 196 | = (*android_java_env)->CallObjectMethod (android_java_env, |
| 199 | clipboard, | 197 | clipboard, |
| 200 | method); | 198 | method); |
| 201 | android_exception_check (); | 199 | android_exception_check (); |
| 202 | 200 | ||
| 203 | if (!bytes) | 201 | if (!text) |
| 204 | return Qnil; | 202 | return Qnil; |
| 205 | 203 | ||
| 206 | length = (*android_java_env)->GetArrayLength (android_java_env, | 204 | /* Retrieve a pointer to the raw JNI-encoded bytes of the string. */ |
| 207 | bytes); | 205 | length = (*android_java_env)->GetStringUTFLength (android_java_env, |
| 208 | data = (*android_java_env)->GetByteArrayElements (android_java_env, | 206 | text); |
| 209 | bytes, NULL); | 207 | data = (*android_java_env)->GetStringUTFChars (android_java_env, text, |
| 210 | android_exception_check_nonnull (data, bytes); | 208 | NULL); |
| 211 | 209 | android_exception_check_nonnull ((void *) data, text); | |
| 212 | string = make_unibyte_string ((char *) data, length); | ||
| 213 | 210 | ||
| 214 | (*android_java_env)->ReleaseByteArrayElements (android_java_env, | 211 | /* Copy them into a unibyte string for decoding. */ |
| 215 | bytes, data, | 212 | string = make_unibyte_string (data, length); |
| 216 | JNI_ABORT); | 213 | (*android_java_env)->ReleaseStringUTFChars (android_java_env, text, |
| 217 | ANDROID_DELETE_LOCAL_REF (bytes); | 214 | data); |
| 215 | ANDROID_DELETE_LOCAL_REF (text); | ||
| 218 | 216 | ||
| 219 | /* Now decode the resulting string. */ | 217 | /* Now decode the resulting string. */ |
| 220 | return code_convert_string_norecord (string, Qutf_8, false); | 218 | return code_convert_string_norecord (string, Qandroid_jni, false); |
| 221 | } | 219 | } |
| 222 | 220 | ||
| 223 | DEFUN ("android-clipboard-exists-p", Fandroid_clipboard_exists_p, | 221 | DEFUN ("android-clipboard-exists-p", Fandroid_clipboard_exists_p, |