diff --git a/app/src/main/java/com/duolebo/blyrobot/data/Task.kt b/app/src/main/java/com/duolebo/blyrobot/data/Task.kt index 13ab88f82ae71adc395b382822e5b12129244aa5..3029724bab6d13af02ad61be25423d611138b577 100644 --- a/app/src/main/java/com/duolebo/blyrobot/data/Task.kt +++ b/app/src/main/java/com/duolebo/blyrobot/data/Task.kt @@ -21,19 +21,13 @@ import java.util.* import kotlin.collections.ArrayList -class Task : IAppBaseCallback { +class Task : Thread, IAppBaseCallback { - private val TAG = "RobotTask" + enum class State { + IDLE, RUNNING, INTERRUPT, SCREEN_SHOT_FAILED, UPLOADING, REPORT_FAILED, COMPLETE, MANUAL_STOP + } - val STATUS_MAP = mapOf( - "0" to "未处理", - "1" to "进行中", - "2" to "中断", - "3" to "截图失败", - "4" to "图片上传中", - "5" to "上报失败", - "6" to "处理完成", - "7" to "手动停止") + private val TAG = "RobotTask" lateinit var apkInfo: ApkInfo // 截图存放路径 @@ -45,11 +39,10 @@ class Task : IAppBaseCallback { private var uploadId = "" private var channelIndex = 0 - private var status = "0" - + var status: State = State.IDLE private var reportProtocol: ApkReportProtocol - private var dataHandler:AppBaseHandler + private var dataHandler: AppBaseHandler private lateinit var reportJson: JSONObject private var context: Context @@ -62,6 +55,12 @@ class Task : IAppBaseCallback { this.dataHandler = AppBaseHandler(this) } + fun isRunning(): Boolean { + if (status == State.COMPLETE) + return false + return true + } + fun from(apkInfo: ApkInfo) { this.apkInfo = apkInfo // this.imagePath = this.context.cacheDir.absolutePath + "/" + apkInfo.packageName @@ -81,14 +80,18 @@ class Task : IAppBaseCallback { reportJson.putOpt("channels", JSONArray()) } - fun start() { + override fun run() { + super.run() + execute() + } + private fun execute() { //如果之前的上传任务还在执行,进行取消 if (!uploadId.isNullOrEmpty()) { UploadService.stopUpload(uploadId) } - this.status = "1" + this.status = State.RUNNING this.prepareReport() // 启动应用 this.launchApp(true) @@ -97,8 +100,10 @@ class Task : IAppBaseCallback { var success = this.processChannels(0) // 再次尝试 if (!success) { - if (this.status == "7") + if (this.status == State.MANUAL_STOP) { + Log.i(TAG, "manual exit") return + } this.launchApp(true) // 重新切换到上次崩溃的频道,接着抓取 @@ -107,27 +112,26 @@ class Task : IAppBaseCallback { } success = this.processChannels(this.channelIndex) - if (!success && this.status == "7") + if (!success && this.status == State.MANUAL_STOP) return } // 上传图片 this.uploadImage() - this.status = "4" + this.status = State.UPLOADING } catch (e: java.lang.Exception) { - this.status = "2" + this.status = State.INTERRUPT e.printStackTrace() } + // 退出应用 + AdbUtil.stopApp(this.apkInfo.packageName) // 上报数据,上传图片在后台进行 report() - - // 退出应用 - AdbUtil.stopApp(this.apkInfo.packageName) } fun reset() { - this.status = "0" + this.status = State.IDLE this.channelIndex = 0 this.uploadImages.clear() @@ -138,9 +142,8 @@ class Task : IAppBaseCallback { } } - fun destroy() { + fun release() { // 杀掉tcpdump进程 - this.status = "7" AdbUtil.killTcpdump() // 退出应用 @@ -151,6 +154,11 @@ class Task : IAppBaseCallback { // UploadService.stopUpload(uploadId) } + fun exit() { + this.status = State.MANUAL_STOP + release() + } + private fun finish(result: Boolean) { this.taskListener?.run { onComplete(result) @@ -178,13 +186,13 @@ class Task : IAppBaseCallback { } } - private fun processChannels(index:Int): Boolean { + private fun processChannels(index: Int): Boolean { Log.i(TAG, "processChannels") step() quitCapture() for (i in index + 1 until this.apkInfo.channelCount) { - if (this.status == "7") + if (this.status == State.MANUAL_STOP) return false // 模拟按键事件. 切换频道进行抓取 this.channelIndex = i @@ -340,7 +348,7 @@ class Task : IAppBaseCallback { } // 截图处理,转成jpg - private fun screenShot(absName : String) { + private fun screenShot(absName: String) { val pngPath = "$absName.png" val jpgPath = pngPath.replace(".png", ".jpg") AdbUtil.screenShot(pngPath) @@ -361,7 +369,7 @@ class Task : IAppBaseCallback { val totalUpload = this.uploadImages.size Log.i(TAG, "total upload count : $totalUpload") - return object: UploadStatusDelegate { + return object : UploadStatusDelegate { override fun onCancelled(context: Context?, uploadInfo: UploadInfo?) { } @@ -416,17 +424,17 @@ class Task : IAppBaseCallback { } override fun onProtocolFailed(p0: IProtocol?) { - this.status = "5" + this.status = State.REPORT_FAILED finish(false) } override fun onHttpFailed(p0: IProtocol?) { - this.status = "5" + this.status = State.REPORT_FAILED finish(false) } override fun onProtocolSucceed(p0: IProtocol?) { - this.status = "6" + this.status = State.COMPLETE finish(true) } diff --git a/app/src/main/java/com/duolebo/blyrobot/tools/TaskManager.kt b/app/src/main/java/com/duolebo/blyrobot/tools/TaskManager.kt index af8bfd9fa6ea0ce9b4199fd5b54250b393e315fc..eb0fb2504e6d6563820cbbd607f12fc6746bfcec 100644 --- a/app/src/main/java/com/duolebo/blyrobot/tools/TaskManager.kt +++ b/app/src/main/java/com/duolebo/blyrobot/tools/TaskManager.kt @@ -2,52 +2,36 @@ package com.duolebo.blyrobot.tools import android.util.Log import com.duolebo.blyrobot.data.Task +import java.util.* import kotlin.collections.ArrayList class TaskManager { - private val TAG = "TaskManager" - private val RUN_TASK_PERIOD = 6*60*60*1000 - val tasks = ArrayList() - private val overTasks = ArrayList() + companion object { + val instance = TaskManager() + val TAG = "RobotTaskManager" + val RUN_TASK_PERIOD: Long = 6 * 60 * 60 * 1000 + } + val tasks = ArrayList() var isRunning = false - private var status = IDLE - private var currentTask: Task ?= null - // 处理时间 - private var updateTime = 0L - - private val runnable = Runnable { - while (isRunning) { - - val interval = System.currentTimeMillis() - this.updateTime - if (this.status == IDLE && interval > RUN_TASK_PERIOD) { - this.updateTime = System.currentTimeMillis() - this.status = RUNNING - for (task in tasks) { - task.reset() - - currentTask = task - currentTask!!.start() - } - } - else { - Log.i(TAG, "no task running") - } - - Thread.sleep(3 * 1000) - } - } + private val overTasks = ArrayList() + private var currentTask: Task? = null + private var scheduleTime: Timer? = null fun add(task: Task) { + for (existTask in tasks) { + Log.i(TAG, "exist task app: ${existTask.apkInfo.packageName}") + } task.taskListener = object : Task.OnTaskListener { override fun onComplete(result: Boolean) { Log.i(TAG, "task ${task.apkInfo.name} complete with result: $result") + tasks.remove(task) overTasks.add(task) - if (overTasks.size == tasks.size) { - status = IDLE + if (tasks.size == 0) { + isRunning = false } } } @@ -62,8 +46,9 @@ class TaskManager { } item?.run { + Log.i(TAG, "remove task ${this.apkInfo.packageName}") tasks.remove(this) - destroy() + this.release() } } @@ -76,35 +61,60 @@ class TaskManager { } fun start() { + Log.i(TAG, "start...") if (this.isRunning) { Log.i(TAG, "is running") return } - if (this.tasks.size <= 0) { + if (this.tasks.size == 0) { Log.i(TAG, "no tasks") return } this.isRunning = true - this.status = IDLE - val t = Thread(runnable) - t.start() + scheduleTime?.run { + cancel() + } + scheduleTime = Timer() + scheduleTime!!.schedule(object : TimerTask() { + override fun run() { + while (isRunning) { + + if (currentTask != null && currentTask!!.isRunning()) { + Log.i(TAG, "task ${currentTask!!.apkInfo.packageName} is running") + } + else { + if (tasks.size == 0) { + for (task in overTasks) { + task.reset() + tasks.add(task) + } + } + + if (tasks.size > 0) { + currentTask = tasks[0] + currentTask!!.start() + Log.i(TAG, "task ${currentTask!!.apkInfo.packageName} start") + } + } + + Thread.sleep(3 * 1000) + } + } + }, 0, RUN_TASK_PERIOD) } fun stop() { + this.isRunning = false + scheduleTime?.run { + cancel() + } this.tasks.forEach { - it.destroy() + it.exit() } + this.tasks.clear() this.overTasks.clear() - this.isRunning = false - this.updateTime = 0L - } - - companion object { - val instance = TaskManager() - const val IDLE = 0 - const val RUNNING = 1 } } \ No newline at end of file diff --git a/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt b/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt index 493b8fccce120921e1dfbb4141abe9c46d6368db..aaffe277084998b623c83b7451251551a03112da 100644 --- a/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt +++ b/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt @@ -369,7 +369,11 @@ object AdbUtil { } fun stopApp(packageName: String) { - exeCmdEcho("am force-stop $packageName", true) + try { + exeCmdEcho("am force-stop $packageName", true) + } catch (e:Exception) { + e.printStackTrace() + } } class CmdResult {