bind/java: move android-specific context code
Change-Id: I7b1dfae97576c7bb2d45e6c2a5732e20df79a77b Reviewed-on: https://go-review.googlesource.com/17251 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
parent
0a581ceb1e
commit
2454c829c1
|
@ -4,8 +4,6 @@
|
|||
|
||||
package go;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.util.SparseArray;
|
||||
import android.util.SparseIntArray;
|
||||
|
||||
|
@ -22,20 +20,15 @@ public class Seq {
|
|||
// Look for the shim class auto-generated by gomobile bind.
|
||||
// Its only purpose is to call System.loadLibrary.
|
||||
try {
|
||||
Class.forName("go.LoadJNI");
|
||||
Class loadJNI = Class.forName("go.LoadJNI");
|
||||
setContext(loadJNI.getDeclaredField("ctx").get(null));
|
||||
} catch (ClassNotFoundException e) {
|
||||
// Ignore, assume the user will load JNI for it.
|
||||
log.warning("LoadJNI class not found");
|
||||
}
|
||||
|
||||
try {
|
||||
// TODO(hyangah): check proguard rule.
|
||||
Application appl = (Application)Class.forName("android.app.AppGlobals").getMethod("getInitialApplication").invoke(null);
|
||||
Context ctx = appl.getApplicationContext();
|
||||
setContext(ctx);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warning("Global context not found: " + e);
|
||||
log.warning("LoadJNI class not found");
|
||||
} catch (NoSuchFieldException e) {
|
||||
log.severe("LoadJNI class missing field: " + e);
|
||||
} catch (IllegalAccessException e) {
|
||||
log.severe("LoadJNI class bad field: " + e);
|
||||
}
|
||||
|
||||
initSeq();
|
||||
|
@ -51,7 +44,8 @@ public class Seq {
|
|||
ensure(64);
|
||||
}
|
||||
|
||||
static native void setContext(Context ctx);
|
||||
// ctx is an android.context.Context.
|
||||
static native void setContext(java.lang.Object ctx);
|
||||
|
||||
// Ensure that at least size bytes can be written to the Seq.
|
||||
// Any existing data in the buffer is preserved.
|
||||
|
|
|
@ -101,9 +101,29 @@ func goAndroidBind(pkgs []*build.Package) error {
|
|||
|
||||
var loadSrc = `package go;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class LoadJNI {
|
||||
private static Logger log = Logger.getLogger("GoLoadJNI");
|
||||
|
||||
public static final Object ctx;
|
||||
|
||||
static {
|
||||
System.loadLibrary("gojni");
|
||||
|
||||
Object androidCtx = null;
|
||||
try {
|
||||
// TODO(hyangah): check proguard rule.
|
||||
Application appl = (Application)Class.forName("android.app.AppGlobals").getMethod("getInitialApplication").invoke(null);
|
||||
androidCtx = appl.getApplicationContext();
|
||||
} catch (Exception e) {
|
||||
log.warning("Global context not found: " + e);
|
||||
} finally {
|
||||
ctx = androidCtx;
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
|
Loading…
Reference in New Issue