Commit de0b3030 authored by liuyang's avatar liuyang

task run

parent a8ded119
*.iml
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
### Android template
# Built application files
*.apk
......@@ -41,16 +39,6 @@ proguard/
# Android Studio captures folder
captures/
# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
.idea/caches
# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
#*.jks
......
......@@ -6,22 +6,46 @@ import android.support.v4.content.LocalBroadcastManager
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Button
import com.duolebo.blyrobot.service.BylRobotService
import com.duolebo.blyrobot.util.Constants
class MainActivity : AppCompatActivity() {
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i("", "onCreate....")
Log.i(TAG, "onCreate....")
setContentView(R.layout.activity_main)
startRobotService()
val startBtn = findViewById<Button>(R.id.startBtn)
startBtn.setOnClickListener{
startRobot()
}
val stopBtn = findViewById<Button>(R.id.stopBtn)
stopBtn.setOnClickListener{
stopRobot()
}
startBtn.requestFocus()
}
private fun startRobotService() {
startService(Intent(this, BylRobotService::class.java))
}
private fun startRobot() {
val intent = Intent()
Log.i(TAG, "startRobot...")
val intent = Intent(Constants.ACTION_LOCAL_SERVICE)
intent.putExtra(Constants.KEY_CMD, Constants.CMD_START)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
private fun stopRobot() {
val intent = Intent(Constants.ACTION_LOCAL_SERVICE)
intent.putExtra(Constants.KEY_CMD, Constants.CMD_STOP)
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}
......
......@@ -20,7 +20,7 @@ class Task {
private var launchDelay = 15
private var context: Context
private val imageDateFormat = SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.CHINA)
var proc: Process ?= null
var proc: Process? = null
constructor(context: Context) {
this.context = context
......@@ -38,10 +38,14 @@ class Task {
fun start() {
this.launchApp()
stop()
}
fun finish() {
fun stop() {
AdbUtil.stopApp(this.packageName)
this.taskListener?.run {
onComplete()
}
}
fun launchApp(reset: Boolean = true) {
......@@ -49,7 +53,7 @@ class Task {
AdbUtil.resetApp(this.packageName)
}
AdbUtil.launchApp("{$packageName}/{$launcher}")
AdbUtil.launchApp("$packageName/$launcher")
Thread.sleep(launchDelay * 1000L)
}
......@@ -104,7 +108,7 @@ class Task {
fun screenShot() {
val time = imageDateFormat.format(Date())
val imagePath = this.imagePath + "/{$packageName}_$time.png"
val imagePath = this.imagePath + "/${packageName}_$time.png"
AdbUtil.screenShot(imagePath)
}
......@@ -126,4 +130,10 @@ class Task {
}
}
var taskListener: OnTaskListener? = null
interface OnTaskListener {
fun onComplete()
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ import android.os.IBinder
import android.support.v4.content.LocalBroadcastManager
import android.util.Log
import com.duolebo.blyrobot.data.Task
import com.duolebo.blyrobot.tools.TaskManager
import com.duolebo.blyrobot.util.AdbUtil
import com.duolebo.blyrobot.util.AppUtil
import com.duolebo.blyrobot.util.Constants
......@@ -22,17 +23,23 @@ class BylRobotService: Service() {
private val TAG = "BylRobotService"
private var timer: Timer? = null
private var count = 0
private val tasks = ArrayList<Task>()
private val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
override fun onReceive(p0: Context?, intent: Intent?) {
if (intent != null) {
val cmd = intent.getStringExtra(Constants.KEY_CMD)
when (cmd) {
Constants.CMD_START -> loadTask()
Constants.CMD_STOP -> stopTask()
}
loadTask()
}
}
}
override fun onCreate() {
super.onCreate()
initBroadcast()
timer = Timer()
timer!!.schedule(object: TimerTask() {
......@@ -50,16 +57,22 @@ class BylRobotService: Service() {
}
private fun initBroadcast() {
val intentFilter = IntentFilter(Constants.BC_SERVICE)
Log.i(TAG, "initBroadcast...")
val intentFilter = IntentFilter(Constants.ACTION_LOCAL_SERVICE)
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter)
}
private fun loadTask() {
Log.i(TAG, "loadTask...")
val task = Task(this)
task.from(JSONObject(AppUtil.readFromAssert(this, "test.json")))
tasks.add(task)
TaskManager.instance.add(task)
TaskManager.instance.start()
}
private fun stopTask() {
TaskManager.instance.stop()
}
private fun screenShot() {
Thread(Runnable {
......
package com.duolebo.blyrobot.tools
import android.util.Log
import com.duolebo.blyrobot.data.Task
import kotlin.collections.ArrayList
class TaskManager {
private val TAG = "TaskManager"
private val tasks = ArrayList<Task>()
private var isRunning = false
private var currentTask: Task ?= null
private val runnable = Runnable {
while (isRunning && tasks.size > 0) {
currentTask = tasks[0]
currentTask!!.start()
}
}
fun add(task: Task) {
task.taskListener = object : Task.OnTaskListener {
override fun onComplete() {
remove(task)
}
}
this.tasks.add(task)
}
......@@ -29,10 +45,21 @@ class TaskManager {
}
fun start() {
if (this.isRunning) {
Log.i(TAG, "is running")
return
}
this.isRunning = true
val t = Thread(runnable)
t.start()
}
fun stop() {
this.tasks.clear()
this.isRunning = false
}
companion object {
val instance = TaskManager()
}
}
\ No newline at end of file
......@@ -247,11 +247,11 @@ object AdbUtil {
}
fun resetApp(packageName: String) {
exeCmdEcho("pm clear $packageName")
exeCmdEcho("pm clear $packageName", true)
}
fun launchApp(launchInfo: String) {
exeCmdEcho("am start -n $launchInfo")
exeCmdEcho("am start -n $launchInfo", true)
}
fun stopApp(packageName: String) {
......
package com.duolebo.blyrobot.util
object Constants {
val KEY_URL = "url"
val KEY_TIME = "time"
const val KEY_URL = "url"
const val KEY_TIME = "time"
const val KEY_CMD = "cmd"
const val CMD_START = "start"
const val CMD_STOP = "stop"
val KEY_MAP = mapOf(
"0" to "7",
......@@ -34,5 +38,5 @@ object Constants {
const val LAUNCHER = "launcher"
const val LAUNCH_DELAY = "launchDelay"
const val BC_SERVICE = "broadcast_service"
const val ACTION_LOCAL_SERVICE = "action_local_service"
}
\ No newline at end of file
......@@ -92,7 +92,7 @@ class CaptureWebServer : NanoHTTPD {
val appInfo = next.appInfo
if (appInfo != null) {
val appPackageName = appInfo!!.pkgs.getAt(0)
val appPackageName = appInfo.pkgs.getAt(0)
if (packageName == appPackageName) {
iterator.remove()
continue
......
......@@ -15,6 +15,13 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动"/>
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="停止"/>
</LinearLayout>
</RelativeLayout>
\ No newline at end of file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.61'
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
......
#Mon Jul 09 10:52:01 CST 2018
#Tue Sep 25 09:27:36 CST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
android.enableBuildCache = false
\ No newline at end of file
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
android.enableBuildCache=false
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