mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
app/internal/window: [Android] use app context for clipboard access
The Activity context may not be available, so it's safer and simpler to use the always available app context. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
package org.gioui;
|
package org.gioui;
|
||||||
|
|
||||||
|
import android.content.ClipboardManager;
|
||||||
|
import android.content.ClipData;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
@@ -36,4 +38,19 @@ public final class Gio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static private native void runGoMain(byte[] dataDir, Context context);
|
static private native void runGoMain(byte[] dataDir, Context context);
|
||||||
|
|
||||||
|
static void writeClipboard(Context ctx, String s) {
|
||||||
|
ClipboardManager m = (ClipboardManager)ctx.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
m.setPrimaryClip(ClipData.newPlainText(null, s));
|
||||||
|
}
|
||||||
|
|
||||||
|
static String readClipboard(Context ctx) {
|
||||||
|
ClipboardManager m = (ClipboardManager)ctx.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
ClipData c = m.getPrimaryClip();
|
||||||
|
if (c == null || c.getItemCount() < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return c.getItemAt(0).coerceToText(ctx).toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ import android.app.Activity;
|
|||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.app.FragmentManager;
|
import android.app.FragmentManager;
|
||||||
import android.app.FragmentTransaction;
|
import android.app.FragmentTransaction;
|
||||||
import android.content.ClipboardManager;
|
|
||||||
import android.content.ClipData;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
@@ -229,20 +227,6 @@ public final class GioView extends SurfaceView implements Choreographer.FrameCal
|
|||||||
ft.commitNow();
|
ft.commitNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeClipboard(String s) {
|
|
||||||
ClipboardManager m = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
|
||||||
m.setPrimaryClip(ClipData.newPlainText(null, s));
|
|
||||||
}
|
|
||||||
|
|
||||||
String readClipboard() {
|
|
||||||
ClipboardManager m = (ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
|
||||||
ClipData c = m.getPrimaryClip();
|
|
||||||
if (c == null || c.getItemCount() < 1) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return c.getItemAt(0).coerceToText(getContext()).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
static private native long onCreateView(GioView view);
|
static private native long onCreateView(GioView view);
|
||||||
static private native void onDestroyView(long handle);
|
static private native void onDestroyView(long handle);
|
||||||
static private native void onStartView(long handle);
|
static private native void onStartView(long handle);
|
||||||
|
|||||||
@@ -82,8 +82,6 @@ type window struct {
|
|||||||
mpostFrameCallback C.jmethodID
|
mpostFrameCallback C.jmethodID
|
||||||
mRegisterFragment C.jmethodID
|
mRegisterFragment C.jmethodID
|
||||||
mwakeupMainThread C.jmethodID
|
mwakeupMainThread C.jmethodID
|
||||||
mwriteClipboard C.jmethodID
|
|
||||||
mreadClipboard C.jmethodID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type jvalue uint64 // The largest JNI type fits in 64 bits.
|
type jvalue uint64 // The largest JNI type fits in 64 bits.
|
||||||
@@ -97,8 +95,13 @@ var android struct {
|
|||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
jvm *C.JavaVM
|
jvm *C.JavaVM
|
||||||
|
|
||||||
// The global Android App context.
|
// appCtx is the global Android App context.
|
||||||
appCtx C.jobject
|
appCtx C.jobject
|
||||||
|
// gioCls is the class of the Gio class.
|
||||||
|
gioCls C.jclass
|
||||||
|
|
||||||
|
mwriteClipboard C.jmethodID
|
||||||
|
mreadClipboard C.jmethodID
|
||||||
}
|
}
|
||||||
|
|
||||||
var views = make(map[C.jlong]*window)
|
var views = make(map[C.jlong]*window)
|
||||||
@@ -153,6 +156,9 @@ func initJVM(env *C.JNIEnv, gio C.jclass, ctx C.jobject) {
|
|||||||
panic("gio: GetJavaVM failed")
|
panic("gio: GetJavaVM failed")
|
||||||
}
|
}
|
||||||
android.appCtx = C.gio_jni_NewGlobalRef(env, ctx)
|
android.appCtx = C.gio_jni_NewGlobalRef(env, ctx)
|
||||||
|
android.gioCls = C.jclass(C.gio_jni_NewGlobalRef(env, C.jobject(gio)))
|
||||||
|
android.mwriteClipboard = getStaticMethodID(env, gio, "writeClipboard", "(Landroid/content/Context;Ljava/lang/String;)V")
|
||||||
|
android.mreadClipboard = getStaticMethodID(env, gio, "readClipboard", "(Landroid/content/Context;)Ljava/lang/String;")
|
||||||
}
|
}
|
||||||
|
|
||||||
func JavaVM() uintptr {
|
func JavaVM() uintptr {
|
||||||
@@ -188,8 +194,6 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j
|
|||||||
mpostFrameCallback: getMethodID(env, class, "postFrameCallback", "()V"),
|
mpostFrameCallback: getMethodID(env, class, "postFrameCallback", "()V"),
|
||||||
mRegisterFragment: getMethodID(env, class, "registerFragment", "(Ljava/lang/String;)V"),
|
mRegisterFragment: getMethodID(env, class, "registerFragment", "(Ljava/lang/String;)V"),
|
||||||
mwakeupMainThread: getMethodID(env, class, "wakeupMainThread", "()V"),
|
mwakeupMainThread: getMethodID(env, class, "wakeupMainThread", "()V"),
|
||||||
mwriteClipboard: getMethodID(env, class, "writeClipboard", "(Ljava/lang/String;)V"),
|
|
||||||
mreadClipboard: getMethodID(env, class, "readClipboard", "()Ljava/lang/String;"),
|
|
||||||
}
|
}
|
||||||
wopts := <-mainWindow.out
|
wopts := <-mainWindow.out
|
||||||
w.callbacks = wopts.window
|
w.callbacks = wopts.window
|
||||||
@@ -624,13 +628,15 @@ func NewWindow(window Callbacks, opts *Options) error {
|
|||||||
func (w *window) WriteClipboard(s string) {
|
func (w *window) WriteClipboard(s string) {
|
||||||
w.runOnMain(func(env *C.JNIEnv) {
|
w.runOnMain(func(env *C.JNIEnv) {
|
||||||
jstr := javaString(env, s)
|
jstr := javaString(env, s)
|
||||||
callVoidMethod(env, w.view, w.mwriteClipboard, jvalue(jstr))
|
callStaticVoidMethod(env, android.gioCls, android.mwriteClipboard,
|
||||||
|
jvalue(android.appCtx), jvalue(jstr))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *window) ReadClipboard() {
|
func (w *window) ReadClipboard() {
|
||||||
w.runOnMain(func(env *C.JNIEnv) {
|
w.runOnMain(func(env *C.JNIEnv) {
|
||||||
c, err := callObjectMethod(env, w.view, w.mreadClipboard)
|
c, err := callStaticObjectMethod(env, android.gioCls, android.mreadClipboard,
|
||||||
|
jvalue(android.appCtx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user