Skip to content

Commit

Permalink
Fix some possible clipboard crashes on Android
Browse files Browse the repository at this point in the history
Fixes #1511
  • Loading branch information
andydotxyz committed Nov 9, 2020
1 parent 400654c commit 2efec93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@ More detailed release notes can be found on the [releases page](https://github.c
* iOS compile failed with Go 1.15 (#1497)
* Possible crash when minimising app containing List on Windows
* File chooser dialog ignores drive Z (#1513)
* Entry copy/paste is crashing on android 7.1 (#1511)


## 1.4 - 1 November 2020
Expand Down
18 changes: 16 additions & 2 deletions internal/driver/gomobile/android.c
Expand Up @@ -68,19 +68,30 @@ jobject getClipboard(uintptr_t jni_env, uintptr_t ctx) {
jmethodID getSystemService = find_method(env, ctxClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");

jstring service = (*env)->NewStringUTF(env, "clipboard");
return (jobject)(*env)->CallObjectMethod(env, ctx, getSystemService, service);
jobject ret = (jobject)(*env)->CallObjectMethod(env, ctx, getSystemService, service);
jthrowable err = (*env)->ExceptionOccurred(env);

if (err != NULL) {
LOG_FATAL("cannot lookup clipboard");
(*env)->ExceptionClear(env);
return NULL;
}
return ret;
}

char *getClipboardContent(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx) {
JNIEnv *env = (JNIEnv*)jni_env;
jobject mgr = getClipboard(jni_env, ctx);
if (mgr == NULL) {
return NULL;
}

jclass mgrClass = (*env)->GetObjectClass(env, mgr);
jmethodID getText = find_method(env, mgrClass, "getText", "()Ljava/lang/CharSequence;");

jobject content = (jstring)(*env)->CallObjectMethod(env, mgr, getText);
if (content == NULL) {
return "";
return NULL;
}

jclass clzCharSequence = (*env)->GetObjectClass(env, content);
Expand All @@ -93,6 +104,9 @@ char *getClipboardContent(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx) {
void setClipboardContent(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx, char *content) {
JNIEnv *env = (JNIEnv*)jni_env;
jobject mgr = getClipboard(jni_env, ctx);
if (mgr == NULL) {
return;
}

jclass mgrClass = (*env)->GetObjectClass(env, mgr);
jmethodID setText = find_method(env, mgrClass, "setText", "(Ljava/lang/CharSequence;)V");
Expand Down
4 changes: 4 additions & 0 deletions internal/driver/gomobile/clipboard_android.go
Expand Up @@ -22,6 +22,10 @@ func (c *mobileClipboard) Content() string {
content := ""
app.RunOnJVM(func(vm, env, ctx uintptr) error {
chars := C.getClipboardContent(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))
if chars == nil {
return nil
}

content = C.GoString(chars)
C.free(unsafe.Pointer(chars))
return nil
Expand Down

0 comments on commit 2efec93

Please sign in to comment.