1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-24 07:20:37 +00:00

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.
This commit is contained in:
Po Lu 2024-05-18 20:45:35 +08:00
parent 5ec4c1a7d3
commit 8dc00dc222
4 changed files with 48 additions and 86 deletions

View File

@ -27,10 +27,10 @@
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);

View 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;
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);
}
/* 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

View 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

View 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);
/* 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);
string = make_unibyte_string ((char *) data, length);
(*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,