2
0
mirror of synced 2025-02-23 23:08:14 +00:00
mobile/app/Go.java
David Crawshaw b2dee7a880 app, cmd/gomobile: expose an app.State variable
The State can be used in the Start callback (and lazily-initialized
packages) instead of JavaInit. This stores a reference to the
android.context.Context for the (future) keyboard package and for
setting TMPDIR.

Second attempt at https://golang.org/cl/4400.

Change-Id: I673997b26ab25ce5140cb31950593d8c6dbd9c51
Reviewed-on: https://go-review.googlesource.com/5555
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
2015-02-23 17:54:17 +00:00

48 lines
1.1 KiB
Java

package go;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
// Go is an entry point for libraries compiled in Go.
// In an app's Application.onCreate, call:
//
// Go.init(getApplicationContext());
//
// When the function returns, it is safe to start calling
// Go code.
public final class Go {
// init loads libgojni.so and starts the runtime.
public static void init(final Context ctx) {
if (Looper.myLooper() != Looper.getMainLooper()) {
Log.wtf("Go", "Go.init must be called from main thread (looper="+Looper.myLooper().toString()+")");
}
if (running) {
return;
}
running = true;
// TODO(crawshaw): setenv TMPDIR to context.getCacheDir().getAbsolutePath()
// TODO(crawshaw): context.registerComponentCallbacks for runtime.GC
System.loadLibrary("gojni");
new Thread("GoMain") {
public void run() {
Go.run(ctx);
}
}.start();
Go.waitForRun();
new Thread("GoReceive") {
public void run() { Seq.receive(); }
}.start();
}
private static boolean running = false;
private static native void run(Context ctx);
private static native void waitForRun();
}