Commit 733f531c authored by liuyang's avatar liuyang

APK抓包>从频道1开始依次抓取, 修复抓包等待时间停止任务,重新开始后之前的任务仍在执行问题

#BYLSERVER-1693
parent 8caefa37
......@@ -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)
}
......
......@@ -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<Task>()
private val overTasks = ArrayList<Task>()
companion object {
val instance = TaskManager()
val TAG = "RobotTaskManager"
val RUN_TASK_PERIOD: Long = 6 * 60 * 60 * 1000
}
val tasks = ArrayList<Task>()
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<Task>()
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
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment