bind/java: reenable asset access.

This is done by moving app.Context to internal/mobileinit,
introducing mobileinit.SetCurrentContext and,
making bind/java depend on it.

TODO: check gomobile bind's proguard rule - context lookup
was implemented through reflection on android.app.AppGlobals class.

Change-Id: Ieb6ad503eeef8c2c1c5836a21c667938c5a701a2
Reviewed-on: https://go-review.googlesource.com/12279
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Hyang-Ah (Hana) Kim
2015-07-16 21:15:37 +00:00
committed by Hyang-Ah Hana Kim
parent c6888a7bae
commit 136fa9bbbb
12 changed files with 131 additions and 39 deletions
+14
View File
@@ -4,6 +4,8 @@
package go;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseIntArray;
@@ -24,6 +26,16 @@ public class Seq {
Log.w("GoSeq", "LoadJNI class not found");
}
try {
// TODO(hyangah): check proguard rule.
Application appl = (Application)Class.forName("android.app.AppGlobals").getMethod("getInitialApplication").invoke(null, (Object[]) null);
Context ctx = appl.getApplicationContext();
setContext(ctx);
} catch (Exception e) {
Log.w("GoSeq", "Global context not found:" + e);
}
initSeq();
new Thread("GoSeq") {
public void run() { Seq.receive(); }
@@ -37,6 +49,8 @@ public class Seq {
ensure(64);
}
static native void setContext(Context ctx);
// Ensure that at least size bytes can be written to the Seq.
// Any existing data in the buffer is preserved.
public native void ensure(int size);
+6
View File
@@ -17,6 +17,12 @@ public class SeqTest extends AndroidTestCase {
public SeqTest() {
}
public void testAssets() {
String want = "Hello, Assets.\n";
String got = Testpkg.ReadAsset();
assertEquals("Asset read", want, got);
}
public void testAdd() {
long res = Testpkg.Add(3, 4);
assertEquals("Unexpected arithmetic failure", 7, res);
+9
View File
@@ -435,3 +435,12 @@ Java_go_Seq_recvRes(JNIEnv *env, jclass clazz, jint handle, jobject out_obj) {
}
RecvRes((int32_t)handle, out->buf, out->len);
}
JNIEXPORT void JNICALL
Java_go_Seq_setContext(JNIEnv* env, jclass clazz, jobject ctx) {
JavaVM* vm;
if ((*env)->GetJavaVM(env, &vm) != 0) {
LOG_FATAL("failed to get JavaVM");
}
setContext(vm, (*env)->NewGlobalRef(env, ctx));
}
+7
View File
@@ -6,6 +6,7 @@ package java // import "golang.org/x/mobile/bind/java"
//#cgo LDFLAGS: -llog
//#include <android/log.h>
//#include <jni.h>
//#include <stdint.h>
//#include <string.h>
//#include "seq_android.h"
@@ -16,6 +17,7 @@ import (
"unsafe"
"golang.org/x/mobile/bind/seq"
"golang.org/x/mobile/internal/mobileinit"
)
const maxSliceLen = 1<<31 - 1
@@ -178,3 +180,8 @@ func init() {
seq.EncString = encodeString
seq.DecString = decodeString
}
//export setContext
func setContext(vm *C.JavaVM, ctx C.jobject) {
mobileinit.SetCurrentContext(unsafe.Pointer(vm), unsafe.Pointer(ctx))
}
+1
View File
@@ -0,0 +1 @@
Hello, Assets.
+18
View File
@@ -11,8 +11,12 @@ package testpkg
import (
"errors"
"fmt"
"io/ioutil"
"log"
"runtime"
"time"
"golang.org/x/mobile/asset"
)
type I interface {
@@ -156,3 +160,17 @@ func Hello(r Receiver, name string) {
func GarbageCollect() {
runtime.GC()
}
func ReadAsset() string {
rc, err := asset.Open("hello.txt")
if err != nil {
log.Fatal(err)
}
defer rc.Close()
b, err := ioutil.ReadAll(rc)
if err != nil {
log.Fatal(err)
}
return string(b)
}