]> git.eshelyaron.com Git - emacs.git/commitdiff
Housekeeping around androidselect.c
authorPo Lu <luangruo@yahoo.com>
Sat, 18 May 2024 12:45:35 +0000 (20:45 +0800)
committerEshel Yaron <me@eshelyaron.com>
Sat, 18 May 2024 18:56:10 +0000 (20:56 +0200)
* 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.

(cherry picked from commit 8dc00dc22250b383e7692fc761cbca25b014aa8a)

java/org/gnu/emacs/EmacsClipboard.java
java/org/gnu/emacs/EmacsSdk11Clipboard.java
java/org/gnu/emacs/EmacsSdk8Clipboard.java
src/androidselect.c

index 86553f478ed63d0cb303102d5b54ba9010f8529f..2560ef793c2a7229fb4d0ad7f945ac080050d7c9 100644 (file)
@@ -27,10 +27,10 @@ import android.os.Build;
 
 public abstract class EmacsClipboard
 {
-  public abstract void setClipboard (byte[] bytes);
+  public abstract void setClipboard (String string);
   public abstract int ownsClipboard ();
   public abstract boolean clipboardExists ();
-  public abstract byte[] getClipboard ();
+  public abstract String getClipboard ();
 
   public abstract String[] getClipboardTargets ();
   public abstract AssetFileDescriptor getClipboardData (String target);
index dfc714476ecb34f61ed36e9f4dce4d8710a55711..e179551c14dd014307993e763657900905898258 100644 (file)
@@ -86,32 +86,23 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard
       }
   }
 
-  /* Set the clipboard text to CLIPBOARD, a string in UTF-8
-     encoding.  */
+  /* Save the STRING into the clipboard by way of text copied by the
+     user.  */
 
   @Override
   public synchronized void
-  setClipboard (byte[] bytes)
+  setClipboard (String string)
   {
     ClipData data;
-    String string;
 
-    try
-      {
-       string = new String (bytes, "UTF-8");
-       data = ClipData.newPlainText ("Emacs", string);
-       manager.setPrimaryClip (data);
-       ownsClipboard = true;
-
-       /* onPrimaryClipChanged will be called again.  Use this
-          variable to keep track of how many times the clipboard has
-          been changed.  */
-       ++clipboardChangedCount;
-      }
-    catch (UnsupportedEncodingException exception)
-      {
-       Log.w (TAG, "setClipboard: " + exception);
-      }
+    data = ClipData.newPlainText ("Emacs", string);
+    manager.setPrimaryClip (data);
+    ownsClipboard = true;
+
+    /* onPrimaryClipChanged will be called again.  Use this
+       variable to keep track of how many times the clipboard has
+       been changed.  */
+    ++clipboardChangedCount;
   }
 
   /* Return whether or not Emacs owns the clipboard.  Value is 1 if
@@ -141,7 +132,7 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard
      NULL if no content is available.  */
 
   @Override
-  public byte[]
+  public String
   getClipboard ()
   {
     ClipData clip;
@@ -154,18 +145,8 @@ public final class EmacsSdk11Clipboard extends EmacsClipboard
       return null;
 
     context = EmacsService.SERVICE;
-
-    try
-      {
-       text = clip.getItemAt (0).coerceToText (context);
-       return text.toString ().getBytes ("UTF-8");
-      }
-    catch (UnsupportedEncodingException exception)
-      {
-       Log.w (TAG, "getClipboard: " + exception);
-      }
-
-    return null;
+    text = clip.getItemAt (0).coerceToText (context);
+    return text.toString ();
   }
 
   /* Return an array of targets currently provided by the
index 344ec6f799726a4b6d62e391175d687a7ea3c948..afd235babf57571d05ec9e509b2aeb6ec89b8bdc 100644 (file)
@@ -52,21 +52,14 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard
       = (ClipboardManager) context.getSystemService (what);
   }
 
-  /* Set the clipboard text to CLIPBOARD, a string in UTF-8
-     encoding.  */
+  /* Save the STRING into the clipboard by way of text copied by the
+     user.  */
 
   @Override
   public void
-  setClipboard (byte[] bytes)
+  setClipboard (String string)
   {
-    try
-      {
-       manager.setText (new String (bytes, "UTF-8"));
-      }
-    catch (UnsupportedEncodingException exception)
-      {
-       Log.w (TAG, "setClipboard: " + exception);
-      }
+    manager.setText (string);
   }
 
   /* Return whether or not Emacs owns the clipboard.  Value is 1 if
@@ -93,7 +86,7 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard
      NULL if no content is available.  */
 
   @Override
-  public byte[]
+  public String
   getClipboard ()
   {
     String string;
@@ -105,17 +98,7 @@ public final class EmacsSdk8Clipboard extends EmacsClipboard
       return null;
 
     string = text.toString ();
-
-    try
-      {
-       return string.getBytes ("UTF-8");
-      }
-    catch (UnsupportedEncodingException exception)
-      {
-       Log.w (TAG, "getClipboard: " + exception);
-      }
-
-    return null;
+    return string;
   }
 
   /* Return an array of targets currently provided by the
index 7c93607848a03e9168d0ed7cc0858abad0f4c8c5..50982738743e0e790258347bdf2ff363c7b91942 100644 (file)
@@ -94,10 +94,10 @@ android_init_emacs_clipboard (void)
                                        name, signature);       \
   eassert (clipboard_class.c_name);
 
-  FIND_METHOD (set_clipboard, "setClipboard", "([B)V");
+  FIND_METHOD (set_clipboard, "setClipboard", "(Ljava/lang/String;)V");
   FIND_METHOD (owns_clipboard, "ownsClipboard", "()I");
   FIND_METHOD (clipboard_exists, "clipboardExists", "()Z");
-  FIND_METHOD (get_clipboard, "getClipboard", "()[B");
+  FIND_METHOD (get_clipboard, "getClipboard", "()Ljava/lang/String;");
   FIND_METHOD (get_clipboard_targets, "getClipboardTargets",
               "()[Ljava/lang/String;");
   FIND_METHOD (get_clipboard_data, "getClipboardData",
@@ -151,28 +151,26 @@ DEFUN ("android-set-clipboard", Fandroid_set_clipboard,
        doc: /* Set the clipboard text to STRING.  */)
   (Lisp_Object string)
 {
-  jarray bytes;
+  jstring text;
 
   if (!android_init_gui)
     error ("Accessing clipboard without display connection");
 
   CHECK_STRING (string);
-  string = ENCODE_UTF_8 (string);
+  string = code_convert_string_norecord (string, Qandroid_jni,
+                                        true);
 
-  bytes = (*android_java_env)->NewByteArray (android_java_env,
-                                            SBYTES (string));
+  text = (*android_java_env)->NewStringUTF (android_java_env,
+                                           SSDATA (string));
   android_exception_check ();
 
-  (*android_java_env)->SetByteArrayRegion (android_java_env, bytes,
-                                          0, SBYTES (string),
-                                          (jbyte *) SDATA (string));
   (*android_java_env)->CallVoidMethod (android_java_env,
                                       clipboard,
                                       clipboard_class.set_clipboard,
-                                      bytes);
-  android_exception_check_1 (bytes);
+                                      text);
+  android_exception_check_1 (text);
+  ANDROID_DELETE_LOCAL_REF (text);
 
-  ANDROID_DELETE_LOCAL_REF (bytes);
   return Qnil;
 }
 
@@ -185,39 +183,39 @@ Alternatively, return nil if the clipboard is empty.  */)
   (void)
 {
   Lisp_Object string;
-  jarray bytes;
+  jstring text;
   jmethodID method;
-  size_t length;
-  jbyte *data;
+  jsize length;
+  const char *data;
 
   if (!android_init_gui)
     error ("No Android display connection!");
 
   method = clipboard_class.get_clipboard;
-  bytes
+  text
     = (*android_java_env)->CallObjectMethod (android_java_env,
                                             clipboard,
                                             method);
   android_exception_check ();
 
-  if (!bytes)
+  if (!text)
     return Qnil;
 
-  length = (*android_java_env)->GetArrayLength (android_java_env,
-                                               bytes);
-  data = (*android_java_env)->GetByteArrayElements (android_java_env,
-                                                   bytes, NULL);
-  android_exception_check_nonnull (data, bytes);
-
-  string = make_unibyte_string ((char *) data, length);
+  /* Retrieve a pointer to the raw JNI-encoded bytes of the string.  */
+  length = (*android_java_env)->GetStringUTFLength (android_java_env,
+                                                   text);
+  data = (*android_java_env)->GetStringUTFChars (android_java_env, text,
+                                                NULL);
+  android_exception_check_nonnull ((void *) data, text);
 
-  (*android_java_env)->ReleaseByteArrayElements (android_java_env,
-                                                bytes, data,
-                                                JNI_ABORT);
-  ANDROID_DELETE_LOCAL_REF (bytes);
+  /* Copy them into a unibyte string for decoding.  */
+  string = make_unibyte_string (data, length);
+  (*android_java_env)->ReleaseStringUTFChars (android_java_env, text,
+                                             data);
+  ANDROID_DELETE_LOCAL_REF (text);
 
   /* Now decode the resulting string.  */
-  return code_convert_string_norecord (string, Qutf_8, false);
+  return code_convert_string_norecord (string, Qandroid_jni, false);
 }
 
 DEFUN ("android-clipboard-exists-p", Fandroid_clipboard_exists_p,