-
Notifications
You must be signed in to change notification settings - Fork 130
/
可视化配置.js
156 lines (145 loc) · 5.47 KB
/
可视化配置.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
/*
* @Author: TonyJiangWJ
* @Date: 2020-11-29 11:28:15
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2023-04-23 11:27:45
* @Description:
*/
"ui";
const prepareWebView = require('./lib/PrepareWebView.js')
importClass(android.view.View)
importClass(android.view.WindowManager)
// ---修改状态栏颜色 start--
// clear FLAG_TRANSLUCENT_STATUS flag:
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
activity.getWindow().setStatusBarColor(android.R.color.white)
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
// ---修改状态栏颜色 end--
let { config } = require('./config.js')(runtime, global)
let singletonRequire = require('./lib/SingletonRequirer.js')(runtime, global)
let FileUtils = singletonRequire('FileUtils')
config.hasRootPermission = files.exists("/sbin/su") || files.exists("/system/xbin/su") || files.exists("/system/bin/su")
if (config.device_width < 10 || config.device_height < 10) {
toastLog('设备分辨率信息不正确,可能无法正常运行脚本, 请先运行一遍main.js以便自动获取分辨率')
exit()
}
console.log('当前设备分辨率:' + config.device_width + ',' + config.device_height)
ui.layout(
<vertical>
<webview id="loadingWebview" margin="0 0" h="*" w="*" />
<webview id="webview" margin="0 0" h="*" w="*" />
</vertical>
)
let mainScriptPath = FileUtils.getRealMainScriptPath(true)
let indexFilePath = "file://" + mainScriptPath + "/vue_configs/index.html"
let loadingFilePath = "file://" + mainScriptPath + "/vue_configs/loading.html"
let errorFilePath = "file://" + mainScriptPath + "/vue_configs/error.html"
let postMessageToWebView = () => { console.error('function not ready') }
ui.webview.setVisibility(View.GONE)
let clearLocalStorage = false
if (config.clear_webview_cache) {
ui.webview.clearCache(true)
clearLocalStorage = true
config.overwrite('clear_webview_cache', false)
}
prepareWebView(ui.loadingWebview, {
enable_log: config.webview_loging,
mainScriptPath: mainScriptPath,
indexFilePath: loadingFilePath,
// 延迟注册
bridgeHandler: () => { },
onPageFinished: () => {
const getLocalVersion = function () {
let mainPath = FileUtils.getCurrentWorkPath()
let versionFile = files.join(mainPath, 'version.json')
let projectFile = files.join(mainPath, 'project.json')
let versionName = ''
if (files.exists(versionFile)) {
versionName = JSON.parse(files.read(versionFile)).version
} else if (files.exists(projectFile)) {
versionName = JSON.parse(files.read(projectFile)).versionName
}
return versionName
}
ui.loadingWebview.loadUrl('javascript:setVersion("' + getLocalVersion() + '")')
ui.loadingWebview.loadUrl('javascript:setGithubUrl("' + config.github_url + '")')
}
})
/**/
let params = ui.loadingWebview.getLayoutParams();
params.height = config.device_height;
params.width = config.device_width;
ui.loadingWebview.setLayoutParams(params);
let bridgeHandlerBuilder = require('./lib/BridgeHandler.js')
let loadSuccess = false
/**/
postMessageToWebView = prepareWebView(ui.webview, {
enable_log: config.webview_loging,
mainScriptPath: mainScriptPath,
indexFilePath: indexFilePath,
// 延迟注册
bridgeHandler: () => bridgeHandlerBuilder(postMessageToWebView),
onPageFinished: () => {
log('页面加载完毕')
registerSensors()
setTimeout(function () {
ui.loadingWebview.setVisibility(View.GONE)
ui.webview.setVisibility(View.VISIBLE)
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
log('切换webview')
ui.webview.loadUrl('javascript:window.vConsole && window.vConsole.destroy()')
clearLocalStorage && ui.webview.loadUrl('javascript:localStorage.clear()')
loadSuccess = true
setTimeout(function () {
console.log('loadingWebview height:', ui.loadingWebview.getHeight())
console.log('webview height:', ui.webview.getHeight())
postMessageToWebView({ functionName: 'resizeWindow', data: { height: ui.loadingWebview.getHeight(), width: ui.webview.getWidth()} })
}, 100)
ui.loadingWebview.clearView()
}, 1000)
}
})
setTimeout(function () {
if (loadSuccess) {
return
}
toastLog('加载资源异常 请重试')
ui.loadingWebview.loadUrl(errorFilePath)
}, 10000)
// ---------------------
let timeout = null
ui.emitter.on('back_pressed', (e) => {
if (ui.webview.canGoBack()) {
ui.webview.goBack()
e.consumed = true
return
}
// toastLog('触发了返回')
if (timeout == null || timeout < new Date().getTime()) {
e.consumed = true
toastLog('再按一次退出')
// 一秒内再按一次
timeout = new Date().getTime() + 1000
} else {
sensors.unregisterAll()
toastLog('再见~')
}
})
let gravitySensor = null
let distanceSensor = null
function registerSensors () {
gravitySensor = sensors.register('gravity', sensors.delay.ui)
if (gravitySensor) {
gravitySensor.on('change', (event, x, y, z) => {
postMessageToWebView({ functionName: 'gravitySensorChange', data: { x: x, y: y, z: z } })
})
}
distanceSensor = sensors.register('proximity', sensors.delay.ui)
if (distanceSensor) {
distanceSensor.on('change', (event, d) => {
postMessageToWebView({ functionName: 'distanceSensorChange', data: { distance: d } })
})
}
}