Commit 0dbc9c45 authored by liuyang's avatar liuyang

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

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