UI改进;迁移至AutoJs6

This commit is contained in:
zhbaor 2025-02-06 17:15:00 +08:00
parent 7511a92db5
commit 5d1321724a
2 changed files with 240 additions and 179 deletions

383
main.js
View file

@ -1,136 +1,97 @@
"ui";
storage = storages.create("mower-ng helper"); storage = storages.create("mower-ng helper");
wx = storage.get("wx", 10); wx = storage.get("wx", 10);
wy = storage.get("wy", 10); wy = storage.get("wy", 10);
http_proto = storage.get("http_proto", "http"); http_proto = storage.get("http_proto", "http");
host = storage.get("host", "127.0.0.1:58000"); host = storage.get("host", "127.0.0.1");
port = storage.get("port", "58000");
rawInput( function save_config() {
"连接地址(要写http://或https://,不写token)", http_proto = ui.proto.getText();
`${http_proto}://${host}`, host = ui.host.getText();
(url) => { port = ui.port.getText();
[http_proto, host] = url.split("://"); storage.put("http_proto", http_proto);
storage.put("http_proto", http_proto); storage.put("host", host);
storage.put("host", host); storage.put("port", port);
} }
);
w = floaty.rawWindow( w = null;
<vertical id="container" bg="#000000" padding="8 6 8 6" alpha="0.6">
<horizontal gravity="center_vertical"> ui.layout(
<img id="logo" w="36" h="36" src="file://./logo.png" /> <vertical gravity="center">
<vertical margin="8 0 8 0"> <horizontal gravity="center" margin="0 0 0 6">
<text <img id="logo" w="64" h="64" src="file://logo.png" />
textColor="#FFFFFF" <vertical marginLeft="8">
id="next" <text text="mower-ng helper" textColor="black" textSize="22sp" />
typeface="monospace" <text text="v1.1.0" />
text="下次任务:--:--:--"
/>
<text
textColor="#FFFFFF"
id="remain"
typeface="monospace"
text="剩余时间:--:--:--"
/>
</vertical> </vertical>
<img
w="24"
h="24"
id="exit"
marginLeft="4"
src="file://./stop.png"
tint="#FFFFFF"
/>
</horizontal> </horizontal>
<text <horizontal gravity="center">
id="log" <text text="proto" w="40" />
gravity="bottom" <input id="proto" text="http" w="200" />
h="120" </horizontal>
textColor="#FFFFFF" <horizontal gravity="center">
typeface="monospace" <text text="host" w="40" />
ems="10" <input id="host" text="127.0.0.1" w="200" />
marginTop="6" </horizontal>
textSize="12" <horizontal gravity="center">
visibility="gone" <text text="port" w="40" />
/> <input id="port" text="58000" w="200" />
</horizontal>
<horizontal gravity="center" margin="0 6 0 0">
<button w="130" id="start" text="开启悬浮窗" />
<button w="130" id="stop" text="关闭悬浮窗" />
</horizontal>
</vertical> </vertical>
); );
w.setSize(-2, -2);
w.exitOnClose();
w.setPosition(wx, wy); ui.proto.setText(http_proto);
ui.host.setText(host);
ui.port.setText(port);
w.container.setOnTouchListener((view, event) => { function toggle_ui(running) {
switch (event.getAction()) { group_waiting = [ui.proto, ui.host, ui.port, ui.start];
case event.ACTION_DOWN: group_running = [ui.stop];
X = event.getRawX(); if (running) {
Y = event.getRawY(); to_enable = group_running;
return true; to_disable = group_waiting;
case event.ACTION_MOVE: } else {
dx = event.getRawX() - X; to_enable = group_waiting;
dy = event.getRawY() - Y; to_disable = group_running;
w.setPosition(wx + dx, wy + dy);
return true;
case event.ACTION_UP:
wx += dx;
wy += dy;
storage.put("wx", wx);
storage.put("wy", wy);
return true;
} }
}); for (i of to_enable) {
i.setEnabled(true);
}
for (i of to_disable) {
i.setEnabled(false);
}
}
w.exit.click(() => { toggle_ui(false);
exit();
});
w.logo.click(() => { ws = null;
ui.run(() => { ws_proto = http_proto == "https" ? "wss" : "ws";
visibility = w.log.visibility == 8 ? 0 : 8; reg = /^[0-9].*/;
w.log.visibility = visibility; log = [];
w.container.alpha = w.log.visibility == 8 ? 0.6 : 0.8;
}); no_time = "--:--:--";
}); scheduler = { idle: false, next_time: null };
client = new OkHttpClient.Builder().build();
function pad_num(n) { function pad_num(n) {
return String(n).padStart(2, "0"); return String(n).padStart(2, "0");
} }
function format_time(d) { function format_time(d) {
hh = pad_num(d.getUTCHours()); hh = pad_num(d.getHours());
mm = pad_num(d.getUTCMinutes()); mm = pad_num(d.getMinutes());
ss = pad_num(d.getUTCSeconds()); ss = pad_num(d.getSeconds());
return `${hh}:${mm}:${ss}`; return `${hh}:${mm}:${ss}`;
} }
axios = require("axios");
no_time = "--:--:--";
calculate_remain = false;
function update_tasks() {
Promise.all([
axios.get(`${http_proto}://${host}/idle`),
axios.get(`${http_proto}://${host}/scheduler`),
]).then(([resp1, resp2]) => {
idle = resp1.data;
task_list = resp2.data;
if (idle == "True" && task_list.length > 0) {
task_time = new Date(task_list[0].time.substring(0, 19));
display_time = format_time(task_time);
calculate_remain = true;
} else {
display_time = no_time;
calculate_remain = false;
}
ui.run(() => {
w.next.setText(`下次任务:${display_time}`);
});
});
setTimeout(update_tasks, 5000);
}
update_tasks();
function time_diff(diff) { function time_diff(diff) {
if (diff < 0) { if (diff < 0) {
return no_time; return no_time;
@ -146,47 +107,171 @@ function time_diff(diff) {
return `${hh}:${mm}:${ss}`; return `${hh}:${mm}:${ss}`;
} }
setInterval(() => { function update_tasks() {
ui.run(() => { if (w) {
display_time = calculate_remain request = new Request.Builder()
? time_diff( .url(`${http_proto}://${host}:${port}/scheduler`)
task_time.getTime() + .build();
task_time.getTimezoneOffset() * 60000 - response = client.newCall(request).execute();
Date.now() scheduler = JSON.parse(response.body().string());
) setTimeout(update_tasks, 5000);
: no_time; }
w.remain.setText(`剩余时间:${display_time}`); }
function stop_ws() {
if (ws) {
ws.cancel();
ws = null;
}
}
function stop_floaty() {
if (w) {
w.close();
w = null;
}
stop_ws();
clearInterval(w_timer);
toggle_ui(false);
return;
}
ui.stop.click(() => {
if (w) {
stop_floaty();
}
});
ui.start.click(() => {
toggle_ui(true);
save_config();
w = floaty.rawWindow(
<vertical id="container" bg="#000000" padding="8 6 8 6" alpha="0.8">
<horizontal gravity="center_vertical">
<img id="logo" w="36" h="36" src="file://logo.png" />
<vertical margin="6 0 6 0">
<text
textColor="#FFFFFF"
id="next"
typeface="monospace"
text="下次任务:--:--:--"
/>
<text
textColor="#FFFFFF"
id="remain"
typeface="monospace"
text="剩余时间:--:--:--"
/>
</vertical>
<img
w="24"
h="24"
id="exit"
margin="0 0 2 0"
src="file://stop.png"
tint="#FFFFFF"
/>
</horizontal>
<text
id="log"
gravity="bottom"
line="5"
maxLines="8"
ems="10"
textColor="#FFFFFF"
typeface="monospace"
marginTop="6"
textSize="12"
visibility="gone"
/>
</vertical>
);
w.setSize(-2, -2);
w.setPosition(wx, wy);
w.container.setOnTouchListener((view, event) => {
switch (event.getAction()) {
case event.ACTION_DOWN:
X = event.getRawX();
Y = event.getRawY();
return true;
case event.ACTION_MOVE:
dx = event.getRawX() - X;
dy = event.getRawY() - Y;
w.setPosition(wx + dx, wy + dy);
return true;
case event.ACTION_UP:
wx += dx;
wy += dy;
storage.put("wx", wx);
storage.put("wy", wy);
return true;
}
}); });
}, 1000);
ws_proto = http_proto == "https" ? "wss" : "ws"; w.logo.click(() => {
log = []; ui.run(() => {
visibility = ws ? 8 : 0;
importPackage(Packages["okhttp3"]); w.log.visibility = visibility;
client = new OkHttpClient.Builder().retryOnConnectionFailure(true).build(); if (ws) {
request = new Request.Builder().url(`${ws_proto}://${host}/ws`).build(); stop_ws();
client.dispatcher().cancelAll(); return;
reg = /^[0-9].*/;
client.newWebSocket(
request,
new WebSocketListener({
onMessage: function (ws, data) {
data = JSON.parse(data);
if (data.type == "log") {
for (line of data.data.split("\n")) {
if (line.match(reg)) {
log.push(line.substring(6));
} else {
log.push(line);
}
}
} }
log = log.slice(-5); request = new Request.Builder()
ui.run(() => { .url(`${ws_proto}://${host}:${port}/ws`)
w.log.setText(log.join("\n")); .build();
}); ws = client.newWebSocket(
}, request,
}) new okhttp3.WebSocketListener({
); onMessage: function (ws, data) {
data = JSON.parse(data);
if (data.type == "log") {
for (line of data.data.split("\n")) {
if (line.match(reg)) {
log.push(line.substring(6));
} else {
log.push(line);
}
}
}
log = log.slice(-5);
ui.run(() => {
w.log.setText(log.join("\n"));
});
},
})
);
});
});
w.exit.click(() => {
stop_floaty();
});
w_timer = setInterval(() => {
if (scheduler.idle && scheduler.next_time) {
task_time = new Date(scheduler.next_time.substring(0, 19));
next_time = format_time(task_time);
remain_time = time_diff(task_time.getTime() - Date.now());
} else {
next_time = no_time;
remain_time = no_time;
}
ui.run(() => {
w.next.setText(`下次任务:${next_time}`);
w.remain.setText(`剩余时间:${remain_time}`);
});
}, 1000);
threads.start(() => {
update_tasks();
});
});
events.on("exit", () => {
save_config();
stop_floaty();
});

View file

@ -1,44 +1,20 @@
{ {
"abis": ["arm64-v8a", "armeabi-v7a", "x86", "x86_64"], "abis": [],
"assets": [ "assets": [],
{
"form": "/storage/emulated/0/脚本/helper_script",
"to": "/project"
}
],
"buildDir": "build",
"build": { "build": {
"build_id": null,
"build_number": 0, "build_number": 0,
"build_time": 0 "build_time": 0
}, },
"useFeatures": [], "useFeatures": [],
"icon": "logo.png", "icon": "logo.png",
"ignoredDirs": ["build"],
"launchConfig": { "launchConfig": {
"displaySplash": false, "hideLogs": false
"hideAccessibilityServices": false,
"hideLauncher": false,
"hideLogs": true,
"stableMode": false,
"volumeUpcontrol": false,
"permissions": ["draw_overlay"],
"serviceDesc": "使脚本自动操作(点击、长按、滑动等)所需,若关闭则只能执行不涉及自动操作的脚本。",
"splashIcon": "logo.png",
"splashText": "mower-ng helper"
}, },
"libs": ["libjackpal-androidterm5.so", "libjackpal-termexec2.so"], "libs": [],
"main": "main.js", "main": "main.js",
"name": "mower-ng helper", "name": "mower-ng helper",
"outputPath": "/storage/emulated/0/脚本/helper_script/build",
"packageName": "vip.zhaozuohong.mowernghelper", "packageName": "vip.zhaozuohong.mowernghelper",
"projectDirectory": "/storage/emulated/0/脚本/helper_script",
"scripts": {}, "scripts": {},
"signingConfig": { "versionCode": 2,
"alias": "autoxjs6", "versionName": "1.1.0"
"keystore": "/storage/emulated/0/脚本/.keyStore/AutoX.keystore"
},
"sourcePath": "/storage/emulated/0/脚本/helper_script/main.js",
"versionCode": 1,
"versionName": "1.0.0"
} }