Commit de0b3030 authored by liuyang's avatar liuyang

task run

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