app: add Handle type and PlatformHandle()

On Android, allow access to JVM and Application context.

Signed-off-by: Greg Pomerantz <gmp.gio@wow.st>
This commit is contained in:
Greg Pomerantz
2019-10-18 12:22:55 -04:00
committed by Elias Naur
parent 175144fa99
commit 5ef176af81
6 changed files with 35 additions and 4 deletions
+7
View File
@@ -9,6 +9,13 @@ import (
"gioui.org/app/internal/window" "gioui.org/app/internal/window"
) )
type Handle window.Handle
// PlatformHandle returns the platform specific Handle.
func PlatformHandle() *Handle {
return (*Handle)(window.PlatformHandle)
}
// extraArgs contains extra arguments to append to // extraArgs contains extra arguments to append to
// os.Args. The arguments are separated with |. // os.Args. The arguments are separated with |.
// Useful for running programs on mobiles where the // Useful for running programs on mobiles where the
+2 -2
View File
@@ -46,7 +46,7 @@ public class GioView extends SurfaceView implements Choreographer.FrameCallback
throw new RuntimeException(e); throw new RuntimeException(e);
} }
System.loadLibrary("gio"); System.loadLibrary("gio");
runGoMain(dataDirUTF8); runGoMain(dataDirUTF8, appCtx);
jniLoaded = true; jniLoaded = true;
} }
} }
@@ -219,7 +219,7 @@ public class GioView extends SurfaceView implements Choreographer.FrameCallback
static private native void onFrameCallback(long handle, long nanos); static private native void onFrameCallback(long handle, long nanos);
static private native boolean onBack(long handle); static private native boolean onBack(long handle);
static private native void onFocusChange(long handle, boolean focus); static private native void onFocusChange(long handle, boolean focus);
static private native void runGoMain(byte[] dataDir); static private native void runGoMain(byte[] dataDir, Context context);
private static class InputConnection extends BaseInputConnection { private static class InputConnection extends BaseInputConnection {
private final Editable editable; private final Editable editable;
+7
View File
@@ -0,0 +1,7 @@
// +build !android
package window
var PlatformHandle *Handle
type Handle struct{}
+11
View File
@@ -0,0 +1,11 @@
package window
var PlatformHandle *Handle
type Handle struct {
// JVM is the JNI *JVM pointer.
JVM uintptr
// Context is a global reference to the application's
// android.content.Context instance.
Context uintptr
}
+1 -1
View File
@@ -22,7 +22,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
static const JNINativeMethod methods[] = { static const JNINativeMethod methods[] = {
{ {
.name = "runGoMain", .name = "runGoMain",
.signature = "([B)V", .signature = "([BLandroid/content/Context;)V",
.fnPtr = runGoMain .fnPtr = runGoMain
}, },
{ {
+7 -1
View File
@@ -80,7 +80,7 @@ func jniGetStaticMethodID(env *C.JNIEnv, class C.jclass, method, sig string) C.j
} }
//export runGoMain //export runGoMain
func runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyteArray) { func runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyteArray, context C.jobject) {
dirBytes := C.gio_jni_GetByteArrayElements(env, jdataDir) dirBytes := C.gio_jni_GetByteArrayElements(env, jdataDir)
if dirBytes == nil { if dirBytes == nil {
panic("runGoMain: GetByteArrayElements failed") panic("runGoMain: GetByteArrayElements failed")
@@ -89,6 +89,12 @@ func runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyteArray) {
dataDir := C.GoStringN((*C.char)(unsafe.Pointer(dirBytes)), n) dataDir := C.GoStringN((*C.char)(unsafe.Pointer(dirBytes)), n)
dataDirChan <- dataDir dataDirChan <- dataDir
C.gio_jni_ReleaseByteArrayElements(env, jdataDir, dirBytes) C.gio_jni_ReleaseByteArrayElements(env, jdataDir, dirBytes)
context = C.gio_jni_NewGlobalRef(env, context)
PlatformHandle = &Handle{
JVM: uintptr(unsafe.Pointer(theJVM)),
Context: uintptr(context),
}
runMain() runMain()
} }