-
Notifications
You must be signed in to change notification settings - Fork 23
/
runtimeAj.js
160 lines (144 loc) · 4.29 KB
/
runtimeAj.js
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
*
* 在 AJ 中 引入
*
* let aj = require("./runtime/runtimeAj.js"); // 引入模块
*
* aj.initUi("./web/index.html"); // 初始化 UI
*/
// 安卓官方接口说明文档 :
// https://developer.android.google.cn/reference/android/webkit/WebViewClient
// AJ 调用 java
// https://p-bakker.github.io/rhino/tutorials/scripting_java/
/**
* 初始化 UI
* @param {string} htmlPath html文件的地址
*/
function initUi(htmlPath) {
ui.layout('<webview id="web" h="*" w="*" />');
webViewExpand_init(ui.web);
let path = "file:" + files.path(htmlPath);
ui.web.loadUrl(path);
}
/**
* 执行 js
* @template T
* @param {*} webViewWidget webview对象
* @param {string} script 脚本
* @param {(data:T)=>void} callback 回调函数
*/
function callJavaScript(webViewWidget, script, callback) {
try {
console.assert(webViewWidget != null, "webView控件为空");
//runtimeLog(script.toString())
webViewWidget.evaluateJavascript(
"javascript:" + script,
new JavaAdapter(android.webkit.ValueCallback, {
onReceiveValue: (val) => {
if (callback) {
callback(val);
}
},
})
);
} catch (e) {
console.error("执行JavaScript失败");
console.trace(e);
}
}
/**
* 执行 js
* @template T
* @param {string} code 欲执行的js代码
* @param {(data:T)=>void} callback 回调函数
*/
function callJs(code, callback) {
callJavaScript(ui.web, code, callback);
}
function bridgeHandler_handle(cmd, args) {
let ret;
// 执行 js代码
if (args == "[code]") {
ret = eval(cmd);
} else {
// 调用方法
/** @type {Function} */
let fun = this[cmd];
if (!fun) {
throw new Error("cmd= " + cmd + " 没有定义实现");
}
// 判断参数类型
if (Array.isArray(args)) {
ret = fun.apply(this, args);
} else {
ret = fun.call(this, args);
}
}
return ret;
}
function webViewExpand_init(webViewWidget) {
webViewWidget.webViewClient = new JavaAdapter(android.webkit.WebViewClient, {
/** 页面开始加载, 此时还没有加载 index.html 中的代码 */
onPageStarted: (webView, url, favicon) => {},
/** 页面加载完成, 在 window.onload 之后触发 */
onPageFinished: (webView, curUrl) => {},
onReceivedError: (webView, webResourceRequest, webResourceError) => {
let url = webResourceRequest.getUrl();
let errorCode = webResourceError.getErrorCode();
let description = webResourceError.getDescription();
console.trace(errorCode + " " + description + " " + url);
},
});
webViewWidget.webChromeClient = new JavaAdapter(android.webkit.WebChromeClient, {
/** 拦截 web console 消息 */
onConsoleMessage: (consoleMessage) => {
/**@type {string} */
let msg = consoleMessage.message();
let sourceId = consoleMessage.sourceId().split("/");
let sourceIdStr = sourceId[sourceId.length - 1];
let lineNumber = consoleMessage.lineNumber();
let msgLevel = consoleMessage.messageLevel();
if (msg.indexOf("jsbridge://") == 0) {
let uris = msg.split("/");
/** @type {{cmd:string,callId:number,args:any}} */
let data = JSON.parse(java.net.URLDecoder.decode(uris[2], "UTF-8"));
let cmd = data.cmd;
let callId = data.callId;
let args = data.args;
runtimeLog("⭐ AJ 收到调用请求:", JSON.stringify(data));
let result = null;
try {
result = bridgeHandler_handle(cmd, args);
} catch (e) {
console.trace(e);
result = {
message: e.message,
};
}
let callbackArgs = {
callId: callId,
args: result,
};
// 调用 web , 完成回调
callJs(["auto.callback(", JSON.stringify(callbackArgs), ")"].join(""));
} else {
runtimeLog("📖 浏览器日志: %s [%s:%s] %s ", msgLevel, sourceIdStr, lineNumber, msg);
}
},
});
}
/**
* 框架 log
* @description 注释方法中的代码就可以关闭所有的框架日志了.
*/
function runtimeLog() {
console.log.apply(this, arguments);
}
module.exports = {
initUi: initUi,
callJavaScript: callJavaScript,
callJs: callJs,
bridgeHandler_handle: bridgeHandler_handle,
webViewExpand_init: webViewExpand_init,
runtimeLog: runtimeLog,
};