-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider_android.go
126 lines (102 loc) · 2.8 KB
/
provider_android.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// +build android
// +build arm 386 amd64 arm64
package goui
/*
#cgo LDFLAGS: -landroid -llog
#include <jni.h>
#include <dlfcn.h>
#include <stdio.h>
#include <android/log.h>
#include "provider.h"
#define loge(...) __android_log_print(ANDROID_LOG_ERROR, "GoUI", __VA_ARGS__);
#define logd(...) __android_log_print(ANDROID_LOG_DEBUG, "GoUI", __VA_ARGS__);
static void alogd(const char *msg) {
__android_log_print(ANDROID_LOG_DEBUG, "GoUI", "%s",msg);
}
extern void handleClientReq(const char* s);
extern void invokeMain(uintptr_t ptr);
JavaVM* jvm=0;
jobject mainActivity = 0;
jmethodID evalJSID = 0;
jmethodID createWebViewID = 0;
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if ((*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
return JNI_VERSION_1_6;
}
void callCreateWebView(WindowSettings settings) {
logd("url:%s",settings.url);
if(createWebViewID!=NULL) {
JNIEnv *env;
(*jvm)->AttachCurrentThread(jvm,&env,NULL);
(*env)->CallVoidMethod(env,mainActivity, createWebViewID, (*env)->NewStringUTF(env,settings.url));
}
}
JNIEXPORT jboolean JNICALL
Java_org_fipress_goui_android_GoUIActivity_invokeGoMain(
JNIEnv *env,
jobject jobj) {
(*env)->GetJavaVM(env,&jvm);
mainActivity = (*env)->NewGlobalRef(env, jobj);
jclass jcls = (*env)->GetObjectClass(env,jobj);
evalJSID = (*env)->GetMethodID(env,jcls,"evalJavaScript","(Ljava/lang/String;)V");
createWebViewID = (*env)->GetMethodID(env,jcls,"loadWebView","(Ljava/lang/String;)V");
const char *dlsym_error = dlerror();
uintptr_t goMainPtr = (uintptr_t)dlsym(RTLD_DEFAULT, "main.main");
dlsym_error = dlerror();
if(dlsym_error) {
loge("dlsym_error:%s",dlsym_error);
return 0;
}
invokeMain(goMainPtr);
return 1;
}
//to invoke java
void invokeJS(const char* js) {
if(evalJSID!=NULL) {
JNIEnv *env;
(*jvm)->AttachCurrentThread(jvm,&env,NULL);
(*env)->CallVoidMethod(env,mainActivity, evalJSID, (*env)->NewStringUTF(env,js));
}
}
JNIEXPORT void JNICALL
Java_org_fipress_goui_android_ScriptHandler_postMessage(
JNIEnv *env,
jobject jobj,
jstring message) {
const char *msg = (*env)->GetStringUTFChars(env,message,0);
handleClientReq(msg);
(*env)->ReleaseStringUTFChars(env,message,msg);
}
void create(WindowSettings settings) {
callCreateWebView(settings);
}
void exitApp() {
}
*/
import "C"
import (
"unsafe"
)
func cCreate(cs C.WindowSettings, cMenuDefs *C.MenuDef, count C.int) {
C.create(cs)
}
func cActivate() {
}
func cInvokeJS(js *C.char) {
C.invokeJS(js)
}
func cExit() {
C.exitApp()
}
func Log(arg ...interface{}) {
go func() {
msg := fmt.Sprint(arg...)
cMsg := C.CString(msg)
defer C.free(unsafe.Pointer(cMsg))
C.alogd(cMsg)
}()
}