Go.java 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package go;
  2. import android.content.Context;
  3. import android.os.Looper;
  4. import android.util.Log;
  5. // Go is an entry point for libraries compiled in Go.
  6. // In an app's Application.onCreate, call:
  7. //
  8. // Go.init(getApplicationContext());
  9. //
  10. // When the function returns, it is safe to start calling
  11. // Go code.
  12. public final class Go {
  13. // init loads libgojni.so and starts the runtime.
  14. public static void init(Context context) {
  15. if (Looper.myLooper() != Looper.getMainLooper()) {
  16. Log.wtf("Go", "Go.init must be called from main thread (looper="+Looper.myLooper().toString()+")");
  17. }
  18. if (running) {
  19. return;
  20. }
  21. running = true;
  22. // TODO(crawshaw): setenv TMPDIR to context.getCacheDir().getAbsolutePath()
  23. // TODO(crawshaw): context.registerComponentCallbacks for runtime.GC
  24. System.loadLibrary("gojni");
  25. new Thread("GoMain") {
  26. public void run() {
  27. Go.run();
  28. }
  29. }.start();
  30. Go.waitForRun();
  31. new Thread("GoReceive") {
  32. public void run() { Seq.receive(); }
  33. }.start();
  34. }
  35. private static boolean running = false;
  36. private static native void run();
  37. private static native void waitForRun();
  38. }