Skip to content

Commit

Permalink
日志写入文件的方法改为异步执行
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyJiangWJ committed Sep 17, 2020
1 parent 852d556 commit 3db3bb0
Show file tree
Hide file tree
Showing 6 changed files with 320 additions and 154 deletions.
13 changes: 10 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-12-09 20:42:08
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-05-11 21:55:40
* @Last Modified time: 2020-09-17 20:05:38
* @Description:
*/
'ui';
Expand Down Expand Up @@ -47,7 +47,9 @@ let default_config = {
device_width: device.width,
device_height: device.height,
// 是否是AutoJS Pro 需要屏蔽部分功能,暂时无法实现:生命周期监听等 包括通话监听
is_pro: is_pro
is_pro: is_pro,
auto_set_bang_offset: true,
bang_offset: 0
}
// 不同项目需要设置不同的storageName,不然会导致配置信息混乱
let CONFIG_STORAGE_NAME = 'autoscript_version'
Expand Down Expand Up @@ -249,7 +251,12 @@ if (!isRunningMode) {
<button id="changeDeviceSizeBtn" >修改</button>
</horizontal>
<horizontal w="*" h="1sp" bg="#cccccc" margin="5 5"></horizontal>

<text text="刘海屏或者挖孔屏悬浮窗显示位置和实际目测位置不同,需要施加一个偏移量一般是负值,脚本运行时会自动设置:" textSize="12sp" margin="10 5"/>
<horizontal padding="10 10" gravity="center">
<text text="当前自动设置的刘海偏移量为:" textSize="12sp" layout_weight="60" />
<text id="bangOffsetText" textSize="12sp" layout_weight="40" />
</horizontal>
<horizontal w="*" h="1sp" bg="#cccccc" margin="5 5"></horizontal>
{/* 脚本延迟启动 */}
<horizontal gravity="center">
<text text="延迟启动时间(秒):" />
Expand Down
49 changes: 49 additions & 0 deletions core/MainExecutor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function mainLoop () {
for (let left = 5; left > 0; left--) {
FloatyInstance.setFloatyInfo({ x: 100, y: config.device_height / 2 }, 'Hello, this will dismiss in ' + left + ' second', { textSize: 20 - left })
FloatyInstance.setFloatyTextColor(colors.toString(Math.random() * 0xFFFFFF & 0xFFFFFF))
sleep(1000)
}
FloatyInstance.setFloatyInfo({ x: config.device_width / 2, y: config.device_height / 2 }, 'GoodBye')
FloatyInstance.setFloatyTextColor(colors.toString(Math.random() * 0xFFFFFF & 0xFFFFFF))
sleep(3000)
}

function testLogs () {
let length = 1000
let start = new Date().getTime()
let thread1 = threads.start(function () {
let count = 0
while(count++ < length) {
logInfo('this is thread 1, count: '+ count)
}
})

let thread2 = threads.start(function () {
let count = 0
while(count++ < length) {
logInfo('this is thread 2, count: '+ count)
}
})

thread1.join()
thread2.join()
infoLog(['all threads done! cost: {}ms', new Date().getTime() - start])

debugForDev('this is a develop log')
debugInfo('this is a debug log')
logInfo('this is a log log')
infoLog('this is a info log')
warnInfo('this is a warn log')
errorInfo('this is a error log')
}
function MainExecutor() {

this.exec = function () {
// 执行主要业务逻辑
// 演示功能,主流程自行封装
testLogs()
mainLoop()
}
}
module.exports = new MainExecutor()
43 changes: 42 additions & 1 deletion lib/prototype/CommonFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2020-04-25 20:16:09
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-05-28 11:06:28
* @Last Modified time: 2020-09-17 20:13:27
* @Description: 通用工具
*/
importClass(android.content.Context)
Expand Down Expand Up @@ -33,6 +33,47 @@ if (!_config.is_pro) {

function CommonFunctions () {

/**
* 自动设置刘海的偏移量
*/
this.autoSetUpBangOffset = function () {
if (_config.auto_set_bang_offset) {
let DETECT_COLOR = '#10FF1F'
let window = floaty.window(
<frame id="container" gravity="center" bg="#10FF1F">
<horizontal margin="10 0" gravity="center">
<text id="text" text="TEXT FLOATY" textSize="10sp" />
</horizontal>
</frame>
)
window.setPosition(100, 0)
// 等待悬浮窗初始化
sleep(300)
let offset = null
while (!offset && offset !== 0) {
let screen = this.checkCaptureScreenPermission()
if (screen) {
let point = images.findColor(screen, DETECT_COLOR, { region: [80, 0, 100, 300], threshold: 1 })
if (point && images.detectsColor(screen, DETECT_COLOR, point.x + 20, point.y) && images.detectsColor(screen, DETECT_COLOR, point.x + 30, point.y)) {
offset = point.y
ui.run(function () {
window.text.setText('刘海偏移量为:' + offset + ' 自动关闭悬浮窗')
})
_logUtils.debugInfo(['自动设置刘海偏移量为:{}', offset])
sleep(500)
debugInfo('关闭悬浮窗')
window.close()
let configStorage = storages.create(_storage_name)
// 设为负值
_config.bang_offset = -offset
configStorage.put('bang_offset', _config.bang_offset)
configStorage.put('auto_set_bang_offset', false)
}
}
}
}
}

/**
* 脚本引擎退出成功后回调,将callback从生命周期回调中移除
*
Expand Down
5 changes: 3 additions & 2 deletions lib/prototype/FloatyUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* @Author: TonyJiangWJ
* @Date: 2019-12-02 19:05:01
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-04-29 14:55:51
* @Last Modified time: 2020-09-17 20:08:24
* @Description: 悬浮窗工具,单独提出来作为单例使用
*/

let { config: _config } = require('../../config.js')(runtime, this)
let FloatyUtil = function () {
this.floatyWindow = null
this.floatyInitStatus = false
Expand Down Expand Up @@ -69,7 +70,7 @@ FloatyUtil.prototype.setFloatyInfo = function (position, text, option) {
ui.run(function () {
_this.floatyLock.lock()
if (position && isFinite(position.x) && isFinite(position.y)) {
_this.floatyWindow.setPosition(parseInt(position.x), parseInt(position.y))
_this.floatyWindow.setPosition(parseInt(position.x), parseInt(position.y) + _config.bang_offset)
}
if (text) {
_this.floatyWindow.content.text(text)
Expand Down
Loading

0 comments on commit 3db3bb0

Please sign in to comment.