1 Star 8 Fork 4

yxk990407/Ant-Manor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.js 4.70 KB
一键复制 编辑 原始数据 按行查看 历史
TonyJiangWJ 提交于 2020-09-30 23:15 . 优化代码
/*
* @Author: TonyJiangWJ
* @Date: 2019-11-27 09:03:57
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-09-22 19:10:34
* @Description:
*/
let { config } = require('./config.js')(runtime, this)
let singletonRequire = require('./lib/SingletonRequirer.js')(runtime, this)
let resourceMonitor = require('./lib/ResourceMonitor.js')(runtime, this)
let runningQueueDispatcher = singletonRequire('RunningQueueDispatcher')
let { logInfo, errorInfo, warnInfo, debugInfo, infoLog, debugForDev, clearLogFile, flushAllLogs } = singletonRequire('LogUtils')
let commonFunctions = singletonRequire('CommonFunction')
if (config.single_script) {
logInfo('======单脚本运行直接清空任务队列=======')
runningQueueDispatcher.clearAll()
}
logInfo('======加入任务队列,并关闭重复运行的脚本=======')
runningQueueDispatcher.addRunningTask()
let FloatyInstance = singletonRequire('FloatyUtil')
let automator = singletonRequire('Automator')
let tryRequestScreenCapture = singletonRequire('TryRequestScreenCapture')
let callStateListener = config.enable_call_state_control ? singletonRequire('CallStateListener') : { exitIfNotIdle: () => { } }
let unlocker = require('./lib/Unlock.js')
let manorRunner = require('./core/AntManorRunner.js')
callStateListener.exitIfNotIdle()
// 注册自动移除运行中任务
commonFunctions.registerOnEngineRemoved(function () {
// 移除运行中任务
runningQueueDispatcher.removeRunningTask(true, true,
() => {
// 重置自动亮度
if (config.auto_set_brightness) {
device.setBrightnessMode(1)
}
events.removeAllListeners()
events.recycle()
// 保存是否需要重新锁屏
unlocker.saveNeedRelock()
flushAllLogs()
config.isRunning = false
console.clear()
}
)
}, 'main')
/***********************
* 初始化
***********************/
logInfo('======校验无障碍功能======')
// 检查手机是否开启无障碍服务
if (!commonFunctions.checkAccessibilityService()) {
try {
auto.waitFor()
} catch (e) {
warnInfo('auto.waitFor()不可用')
auto()
}
}
logInfo('---前置校验完成;启动系统--->>>>')
// 打印运行环境信息
if (files.exists('version.json')) {
let content = JSON.parse(files.read('version.json'))
logInfo(['版本信息:{} nodeId:{}', content.version, content.nodeId])
} else if (files.exists('project.json')) {
let content = JSON.parse(files.read('project.json'))
logInfo(['版本信息:{}', content.versionName])
} else {
logInfo('无法获取脚本版本信息')
}
logInfo(['AutoJS version: {}', app.autojs.versionName])
logInfo(['device info: {} {} {}', device.brand, device.product, device.release])
logInfo(['设备分辨率:[{}, {}]', config.device_width, config.device_height])
logInfo('======解锁======')
try {
unlocker.exec()
} catch (e) {
let currentDate = new Date()
let sleepTime = 3
if (currentDate.getHours() <= 7 && currentDate.getHours() >= 0) {
// 夜间15分钟
sleepTime = 15
}
errorInfo(['解锁发生异常, {}分钟后重新开始' + e, sleepTime])
commonFunctions.printExceptionStack(e)
commonFunctions.setUpAutoStart(sleepTime)
runningQueueDispatcher.removeRunningTask()
exit()
}
logInfo('解锁成功')
let screenPermission = false
let actionSuccess = commonFunctions.waitFor(function () {
if (config.request_capture_permission) {
screenPermission = tryRequestScreenCapture()
} else {
screenPermission = requestScreenCapture(false)
}
}, 15000)
if (!actionSuccess || !screenPermission) {
errorInfo('请求截图失败, 设置6秒后重启')
runningQueueDispatcher.removeRunningTask()
sleep(6000)
runningQueueDispatcher.executeTargetScript(FileUtils.getRealMainScriptPath())
exit()
} else {
logInfo('请求截屏权限成功')
}
// 初始化悬浮窗
if (!FloatyInstance.init()) {
runningQueueDispatcher.removeRunningTask()
// 悬浮窗初始化失败,6秒后重试
sleep(6000)
runningQueueDispatcher.executeTargetScript(FileUtils.getRealMainScriptPath())
exit()
}
// 自动设置刘海偏移量
commonFunctions.autoSetUpBangOffset()
/************************
* 主程序
***********************/
function mainExec() {
commonFunctions.showDialogAndWait(true)
commonFunctions.listenDelayStart()
manorRunner.start()
}
if (config.develop_mode) {
mainExec()
} else {
try {
mainExec()
} catch (e) {
commonFunctions.printExceptionStack(e)
errorInfo('执行发生异常' + e + ' 三分钟后重启')
commonFunctions.setUpAutoStart(3)
}
}
if (config.auto_lock === true && unlocker.needRelock() === true) {
debugInfo('重新锁定屏幕')
automator.lockScreen()
unlocker.saveNeedRelock(true)
}
FloatyInstance.close()
runningQueueDispatcher.removeRunningTask(true)
exit()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/y9947/Ant-Manor.git
git@gitee.com:y9947/Ant-Manor.git
y9947
Ant-Manor
Ant-Manor
master

搜索帮助