Skip to content

Commit

Permalink
优化代码,增加通话状态监听功能等
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyJiangWJ committed May 25, 2020
1 parent 858bca6 commit 5b339cb
Show file tree
Hide file tree
Showing 14 changed files with 829 additions and 35 deletions.
8 changes: 4 additions & 4 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-07 19:58:41
* @Last Modified time: 2020-05-11 21:55:40
* @Description:
*/
'ui';
Expand All @@ -26,7 +26,7 @@ let default_config = {
lock_x: 150,
lock_y: 970,
// 锁屏启动关闭提示框
dismissDialogIfLocked: true,
dismiss_dialog_if_locked: true,
request_capture_permission: true,
// 是否保存日志文件,如果设置为保存,则日志文件会按时间分片备份在logback/文件夹下
saveLogFile: true,
Expand Down Expand Up @@ -167,7 +167,7 @@ if (!isRunningMode) {
ui.lockPositionContainer.setVisibility(config.auto_lock && !_hasRootPermission ? View.VISIBLE : View.INVISIBLE)
ui.lockDescNoRoot.setVisibility(!_hasRootPermission ? View.VISIBLE : View.INVISIBLE)

ui.dismissDialogIfLockedChkBox.setChecked(config.dismissDialogIfLocked)
ui.dismissDialogIfLockedChkBox.setChecked(config.dismiss_dialog_if_locked)

ui.timeoutUnlockInpt.text(config.timeout_unlock + '')
ui.timeoutFindOneInpt.text(config.timeout_findOne + '')
Expand Down Expand Up @@ -476,7 +476,7 @@ if (!isRunningMode) {
})

ui.dismissDialogIfLockedChkBox.on('click', () => {
config.dismissDialogIfLocked = ui.dismissDialogIfLockedChkBox.isChecked()
config.dismiss_dialog_if_locked = ui.dismissDialogIfLockedChkBox.isChecked()
})

ui.autoLockChkBox.on('click', () => {
Expand Down
287 changes: 287 additions & 0 deletions lib/ResourceMonitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
/*
* @Author: TonyJiangWJ
* @Date: 2020-05-11 18:28:23
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-05-15 20:21:08
* @Description: 图片资源监听并自动回收
*/
let { config } = require('../config.js')(runtime, this)

if (!config.is_pro) {
importClass(com.stardust.autojs.ScriptEngineService)
importClass(com.stardust.autojs.engine.ScriptEngineManager)
}
let sRequire = require('./SingletonRequirer.js')(runtime, this)
let { debugInfo, debugForDev, infoLog, errorInfo } = sRequire('LogUtils')

module.exports = function (__runtime__, scope) {
if (typeof scope.resourceMonitor === 'undefined' || scope.resourceMonitor === null) {
let _o_images = scope.images
function ResourceMonitor () {
this.images = []
this.writeLock = threads.lock()
this.init()
}

ResourceMonitor.prototype.releaseAll = function () {
if (this.images !== null) {
debugInfo('释放图片,总数:' + this.images.length)
try {
this.writeLock.lock()
this.recycleImages(this.images.splice(0), true)
this.images = null
scope.images = _o_images
scope.__asGlobal__(_o_images, ['captureScreen'])
_o_images = null
} finally {
this.writeLock.unlock()
}
}
}

ResourceMonitor.prototype.addImageToList = function (img) {

try {
this.writeLock.lock()
this.images.push({
img: img,
millis: new Date().getTime()
})
debugForDev('增加图片到监听列表,当前总数:' + this.images.length)
// 达到一定阈值后回收
if (this.images.length > 50) {
if (this.images.length > 100) {
// 大于100张直接回收一半
this.recycleImages(this.images.splice(0, 50))
} else {
let current = new Date().getTime()
// 回收超过5秒钟的图片
let forRecycle = this.images.filter(imageInfo => current - imageInfo.millis > 5000)
this.recycleImages(forRecycle)
this.images.splice(0, forRecycle.length)
}
}
} finally {
this.writeLock.unlock()
}
}

function doRecycleImages (forRecycleList, desc) {
let start = new Date().getTime()
forRecycleList.forEach(imageInfo => {
try {
imageInfo.img.recycle()
} catch (e) {
// console.warn('释放图片异常' + e)
}
})
debugInfo(desc + ',耗时' + (new Date().getTime() - start))
forRecycleList = null
}

ResourceMonitor.prototype.recycleImages = function (forRecycleList, sync) {
if (forRecycleList && forRecycleList.length > 0) {
if (sync) {
doRecycleImages(forRecycleList, '同步释放所有图片')
} else {
threads.start(function () {
// 不太安全,可能没释放完就挂了 脚本结束时最好执行一下releaseAll
doRecycleImages(forRecycleList, '异步释放图片')
})
}
}
}

ResourceMonitor.prototype.init = function () {

let that = this

const M_Images = function () {
_o_images.constructor.call(this)
}
M_Images.prototype = Object.create(_o_images.prototype)
M_Images.prototype.constructor = _o_images

M_Images.prototype.captureScreen = function () {
let img = _o_images.captureScreen()
that.addImageToList(img)
return img
}

M_Images.prototype.copy = function (origialImg) {
let newImg = _o_images.copy(origialImg)
that.addImageToList(newImg)
return newImg
}

M_Images.prototype.read = function (path) {
let newImg = _o_images.read(path)
that.addImageToList(newImg)
return newImg
}

M_Images.prototype.load = function (path) {
let newImg = _o_images.load(path)
that.addImageToList(newImg)
return newImg
}

M_Images.prototype.clip = function (img, x, y, w, h) {
let newImg = _o_images.clip(img, x, y, w, h)
that.addImageToList(newImg)
return newImg
}

M_Images.prototype.interval = function (img, color, threshold) {
let intervalImg = _o_images.interval(img, color, threshold)
that.addImageToList(intervalImg)
return intervalImg
}

M_Images.prototype.grayscale = function (img) {
let grayImg = _o_images.grayscale(img)
that.addImageToList(grayImg)
return grayImg
}

M_Images.prototype.threshold = function (img, threshold, maxVal, type) {
let nImg = _o_images.threshold(img, threshold, maxVal, type)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.inRange = function (img, lowerBound, upperBound) {
let nImg = _o_images.inRange(img, lowerBound, upperBound)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.adaptiveThreshold = function (img, maxValue, adaptiveMethod, thresholdType, blockSize, C) {
let nImg = _o_images.adaptiveThreshold(img, maxValue, adaptiveMethod, thresholdType, blockSize, C)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.blur = function (img, size, point, type) {
let nImg = _o_images.blur(img, size, point, type)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.medianBlur = function (img, size) {
let nImg = _o_images.medianBlur(img, size)
that.addImageToList(nImg)
return nImg
}


M_Images.prototype.gaussianBlur = function (img, size, sigmaX, sigmaY, type) {
let nImg = _o_images.gaussianBlur(img, size, sigmaX, sigmaY, type)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.cvtColor = function (img, code, dstCn) {
let nImg = _o_images.cvtColor(img, code, dstCn)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.resize = function (img, size, interpolation) {
let nImg = _o_images.resize(img, size, interpolation)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.scale = function (img, fx, fy, interpolation) {
let nImg = _o_images.scale(img, fx, fy, interpolation)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.rotate = function (img, degree, x, y) {
let nImg = _o_images.rotate(img, degree, x, y)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.concat = function (img1, img2, direction) {
let nImg = _o_images.concat(img1, img2, direction)
that.addImageToList(nImg)
return nImg
}


M_Images.prototype.fromBase64 = function (base64) {
let nImg = _o_images.fromBase64(base64)
that.addImageToList(nImg)
return nImg
}

M_Images.prototype.fromBytes = function (bytes) {
let nImg = _o_images.fromBytes(bytes)
that.addImageToList(nImg)
return nImg
}


M_Images.prototype.matToImage = function (img) {
let nImg = _o_images.matToImage(img)
that.addImageToList(nImg)
return nImg
}

let mImages = new M_Images()

let newImages = {}
let imageFuncs = Object.getOwnPropertyNames(scope.images)
let newFuncs = Object.getOwnPropertyNames(M_Images.prototype)



for (let idx in imageFuncs) {
let func_name = imageFuncs[idx]
newImages[func_name] = scope.images[func_name]
}

for (let idx in newFuncs) {
let func_name = newFuncs[idx]
if (func_name !== 'constructor' && func_name !== 'init') {
// console.verbose('override function: ' + func_name)
newImages[func_name] = mImages[func_name]
}
}

scope.images = newImages

scope.__asGlobal__(mImages, ['captureScreen'])
}

let resourceMonitor = new ResourceMonitor()

if (!config.is_pro) {
// 监听运行周期,脚本结束后释放图片资源
let engineService = ScriptEngineService.getInstance()

let myEngineId = engines.myEngine().id
engineService.registerEngineLifecycleCallback(
new ScriptEngineManager.EngineLifecycleCallback({
onEngineCreate: function (engine) {
},
onEngineRemove: function (engine) {
if (engine.id === myEngineId) {
infoLog('脚本执行结束, 释放图片资源')
resourceMonitor.releaseAll()
}
}
})
)
} else {
errorInfo('Pro版无法监听生命周期,请尽量使用免费版')
}

scope.resourceMonitor = resourceMonitor
}

return scope.resourceMonitor
}
8 changes: 4 additions & 4 deletions lib/Unlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2019-11-05 09:12:00
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-05-07 12:03:25
* @Last Modified time: 2020-05-08 15:11:58
* @Description:
*/
let { config: _config } = require('../config.js')(runtime, this)
Expand Down Expand Up @@ -105,7 +105,7 @@ function Unlocker () {
this.reTry++
if (this.reTry > 3) {
logInfo('解锁失败达到三次,停止运行')
if (_config.autoSetBrightness) {
if (_config.auto_set_brightness) {
device.setBrightnessMode(1)
}
engines.myEngine().forceStop()
Expand Down Expand Up @@ -164,7 +164,7 @@ function Unlocker () {
}
this.relock = true
logInfo('需要重新锁定屏幕')
if (_config.autoSetBrightness) {
if (_config.auto_set_brightness) {
// 设置最低亮度 同时关闭自动亮度
device.setBrightnessMode(0)
device.setBrightness(0)
Expand All @@ -178,7 +178,7 @@ function Unlocker () {
// 如果解锁失败
this.failed()
} else {
if (_config.dismissDialogIfLocked) {
if (_config.dismiss_dialog_if_locked) {
// 锁屏状态下启动不再弹框倒计时
_commonFunctions.getAndUpdateDismissReason('screen_locked')
}
Expand Down
Binary file removed lib/autojs-tools.dex
Binary file not shown.
Binary file added lib/download.dex
Binary file not shown.
13 changes: 12 additions & 1 deletion lib/prototype/Automator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: TonyJiangWJ
* @Date: 2020-04-25 20:37:31
* @Last Modified by: TonyJiangWJ
* @Last Modified time: 2020-04-28 10:04:53
* @Last Modified time: 2020-05-09 07:46:51
* @Description:
*/
let { config: _config } = require('../../config.js')(runtime, this)
Expand Down Expand Up @@ -46,6 +46,9 @@ module.exports = {
return scrollDown()
}
},
scrollUp: function(speed) {
_automator.scrollUp(speed)
},
scrollUpAndDown: function (speed) {
if (_config.useCustomScrollDown) {
return _automator.scrollUpAndDown(speed)
Expand Down Expand Up @@ -77,6 +80,14 @@ function CommonAutomation () {
this.swipe(x, deviceHeight - bottomHeight, x + 100, parseInt(deviceHeight / 5), millis)
}

this.scrollUp = function (speed) {
let millis = parseInt((speed || _config.scrollDownSpeed || 500) / 2)
let deviceHeight = _config.device_height || 1900
let x = parseInt(_config.device_width / 2)
// 下拉
this.swipe(x, parseInt(deviceHeight / 3), x + 100, parseInt(deviceHeight / 3 * 2), millis)
}

this.scrollUpAndDown = function (speed) {
let millis = parseInt((speed || _config.scrollDownSpeed || 500) / 2)

Expand Down
Loading

0 comments on commit 5b339cb

Please sign in to comment.