diff --git a/app/internal/window/Gio.java b/app/internal/window/Gio.java new file mode 100644 index 00000000..9ae812bf --- /dev/null +++ b/app/internal/window/Gio.java @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: Unlicense OR MIT + +package org.gioui; + +import android.content.Context; + +import java.io.UnsupportedEncodingException; + +public class Gio { + private final static Object initLock = new Object(); + private static boolean jniLoaded; + + /** + * init loads and initializes the Go native library and runs + * the Go main function. + * + * It is exported for use by Android apps that need to run Go code + * outside the lifecycle of the Gio activity. + */ + public static synchronized void init(Context appCtx) { + synchronized (initLock) { + if (jniLoaded) { + return; + } + String dataDir = appCtx.getFilesDir().getAbsolutePath(); + byte[] dataDirUTF8; + try { + dataDirUTF8 = dataDir.getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + System.loadLibrary("gio"); + runGoMain(dataDirUTF8, appCtx); + jniLoaded = true; + } + } + + static private native void runGoMain(byte[] dataDir, Context context); +} diff --git a/app/internal/window/GioView.java b/app/internal/window/GioView.java index cf23224e..300e58f7 100644 --- a/app/internal/window/GioView.java +++ b/app/internal/window/GioView.java @@ -42,24 +42,6 @@ public class GioView extends SurfaceView implements Choreographer.FrameCallback private final Handler handler; private long nhandle; - private static synchronized void initialize(Context appCtx) { - synchronized (initLock) { - if (jniLoaded) { - return; - } - String dataDir = appCtx.getFilesDir().getAbsolutePath(); - byte[] dataDirUTF8; - try { - dataDirUTF8 = dataDir.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - System.loadLibrary("gio"); - runGoMain(dataDirUTF8, appCtx); - jniLoaded = true; - } - } - public GioView(Context context) { this(context, null); } @@ -69,7 +51,7 @@ public class GioView extends SurfaceView implements Choreographer.FrameCallback handler = new Handler(); // Late initialization of the Go runtime to wait for a valid context. - initialize(context.getApplicationContext()); + Gio.init(context.getApplicationContext()); nhandle = onCreateView(this); imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); @@ -260,7 +242,6 @@ public class GioView extends SurfaceView implements Choreographer.FrameCallback static private native void onFrameCallback(long handle, long nanos); static private native boolean onBack(long handle); static private native void onFocusChange(long handle, boolean focus); - static private native void runGoMain(byte[] dataDir, Context context); private static class InputConnection extends BaseInputConnection { private final Editable editable; diff --git a/app/internal/window/os_android.go b/app/internal/window/os_android.go index b0b88865..da852f33 100644 --- a/app/internal/window/os_android.go +++ b/app/internal/window/os_android.go @@ -86,8 +86,8 @@ func jniGetStaticMethodID(env *C.JNIEnv, class C.jclass, method, sig string) C.j return C.gio_jni_GetStaticMethodID(env, class, m, s) } -//export Java_org_gioui_GioView_runGoMain -func Java_org_gioui_GioView_runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyteArray, context C.jobject) { +//export Java_org_gioui_Gio_runGoMain +func Java_org_gioui_Gio_runGoMain(env *C.JNIEnv, class C.jclass, jdataDir C.jbyteArray, context C.jobject) { if res := C.gio_jni_GetJavaVM(env, &theJVM); res != 0 { panic("gio: GetJavaVM failed") }