Commit 0dbc9c45 authored by liuyang's avatar liuyang

增加程序前台判断逻辑,修复启动问题

parent eb964c2f
......@@ -65,22 +65,39 @@ class MainActivity : AppCompatActivity(), IAppBaseCallback {
startRobotService()
startBtn.setOnClickListener{
startRobot()
}
stopBtn.setOnClickListener{
stopRobot()
toggle()
}
startBtn.requestFocus()
requestAppList()
refreshBtn.setOnClickListener{
this.appInfoAdapter.clear()
requestAppList()
}
}
private fun toggle() {
// 暂时测试用
if (TaskManager.instance.isRunning) {
stopRobot()
startBtn.isEnabled = false
startBtn.postDelayed({
startBtn.isEnabled = true
startBtn.text = "启动"
}, 3000)
}
else {
startRobot()
startBtn.isEnabled = false
startBtn.postDelayed({
startBtn.isEnabled = true
startBtn.text = "停止"
}, 3000)
}
}
private fun requestAppList() {
apkInfoProtocol = GetAppInfoProtocol(this, Config.instance)
this.dataHandler = AppBaseHandler(this)
......@@ -139,7 +156,7 @@ class MainActivity : AppCompatActivity(), IAppBaseCallback {
override fun onProtocolSucceed(protocol: IProtocol?) {
if (protocol is GetAppInfoProtocol) {
this.appInfoData = protocol.data
this.appInfoData = protocol.data as AppInfoData
this.appInfoAdapter.setData(appInfoData.apkInfos)
this.appInfoAdapter.notifyDataSetChanged()
......@@ -179,6 +196,11 @@ class MainActivity : AppCompatActivity(), IAppBaseCallback {
this.dataList.addAll(dataList)
}
fun clear() {
this.dataList.clear()
notifyDataSetChanged()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var itemView: AppConfigListItem
if (convertView == null) {
......
......@@ -97,7 +97,12 @@ class Task : IAppBaseCallback {
// }
// 频道轮询处理
try {
this.processChannels()
val success = this.processChannels()
// 尝试两次
if (!success) {
this.reset()
this.processChannels()
}
// 上传图片
this.uploadImage()
this.status = "4"
......@@ -162,7 +167,7 @@ class Task : IAppBaseCallback {
}
}
private fun processChannels() {
private fun processChannels(): Boolean {
Log.i(TAG, "processChannels")
step()
for (i in 1 until this.apkInfo.channelCount) {
......@@ -170,8 +175,15 @@ class Task : IAppBaseCallback {
this.channelIndex = i
capture(apkInfo.captureDelay)
AdbUtil.sendMultiKey(this.apkInfo.channelKeyEvent)
if (!AppUtil.isAppBackground(context, this.apkInfo.packageName)) {
return false
}
step()
}
return true
}
private fun step() {
......
......@@ -205,7 +205,7 @@ object AdbUtil {
val keyEvents = ArrayList<EmulateKey>()
val keys = keyPath.split(Constants.SLASH)
for (stepKey in keys) {
var delay = "1"
var delay = "2"
if (Constants.VERTICAL_BAR in stepKey) {
val params = stepKey.split(Constants.VERTICAL_BAR)
delay = params[1]
......
package com.duolebo.blyrobot.util
import android.app.ActivityManager
import android.content.Context
import android.text.TextUtils
import java.net.Inet4Address
......@@ -12,6 +13,9 @@ import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import java.util.regex.Pattern
import android.app.ActivityManager.RunningAppProcessInfo
object AppUtil {
......@@ -168,4 +172,20 @@ object AppUtil {
return sdcardAbsPath
}
fun isAppBackground(context: Context, packageName: String): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return AdbUtil.isAppActive(packageName)
}
val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val appProcesses = activityManager.runningAppProcesses
for (appProcess in appProcesses) {
if (appProcess.processName == packageName) {
return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND
}
}
return false
}
}
\ No newline at end of file
......@@ -27,13 +27,6 @@
android:layout_height="wrap_content"
android:text="启动"/>
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="停止"/>
<Button
android:id="@+id/refreshBtn"
android:layout_width="wrap_content"
......
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