Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
BYLAppRobot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liuyang
BYLAppRobot
Commits
733f531c
Commit
733f531c
authored
Nov 08, 2018
by
liuyang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
APK抓包>从频道1开始依次抓取, 修复抓包等待时间停止任务,重新开始后之前的任务仍在执行问题
#BYLSERVER-1693
parent
8caefa37
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
80 deletions
+102
-80
Task.kt
app/src/main/java/com/duolebo/blyrobot/data/Task.kt
+41
-33
TaskManager.kt
app/src/main/java/com/duolebo/blyrobot/tools/TaskManager.kt
+56
-46
AdbUtil.kt
app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt
+5
-1
No files found.
app/src/main/java/com/duolebo/blyrobot/data/Task.kt
View file @
733f531c
...
...
@@ -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
)
}
...
...
app/src/main/java/com/duolebo/blyrobot/tools/TaskManager.kt
View file @
733f531c
...
...
@@ -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
)
{
fun
stop
()
{
this
.
tasks
.
forEach
{
it
.
destroy
()
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
)
}
}
this
.
overTasks
.
clear
()
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
this
.
updateTime
=
0L
scheduleTime
?.
run
{
cancel
()
}
this
.
tasks
.
forEach
{
it
.
exit
()
}
companion
object
{
val
instance
=
TaskManager
()
const
val
IDLE
=
0
const
val
RUNNING
=
1
this
.
tasks
.
clear
()
this
.
overTasks
.
clear
()
}
}
\ No newline at end of file
app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt
View file @
733f531c
...
...
@@ -369,7 +369,11 @@ object AdbUtil {
}
fun
stopApp
(
packageName
:
String
)
{
try
{
exeCmdEcho
(
"am force-stop $packageName"
,
true
)
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
}
}
class
CmdResult
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment