Commit b89ddb3d authored by liuyang's avatar liuyang

按键改进

parent 3f1fa42c
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
"name": "电视家", "name": "电视家",
"packageName": "com.elinkway.tvlive2", "packageName": "com.elinkway.tvlive2",
"launcher": "com.elinkway.tvlive2.activity.SplashActivity", "launcher": "com.elinkway.tvlive2.activity.SplashActivity",
"launchDelay": "15" "launchDelay": "15",
"keyEvents": "down|3/ok"
} }
\ No newline at end of file
package com.duolebo.blyrobot.data
class EmulateKey {
var key: String = "down"
var delay = "1"
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ package com.duolebo.blyrobot.data ...@@ -3,6 +3,7 @@ package com.duolebo.blyrobot.data
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import com.duolebo.blyrobot.util.AdbUtil import com.duolebo.blyrobot.util.AdbUtil
import com.duolebo.blyrobot.util.AppUtil
import com.duolebo.blyrobot.util.Constants import com.duolebo.blyrobot.util.Constants
import org.json.JSONObject import org.json.JSONObject
import net.gotev.uploadservice.UploadNotificationConfig import net.gotev.uploadservice.UploadNotificationConfig
...@@ -18,6 +19,8 @@ class Task { ...@@ -18,6 +19,8 @@ class Task {
private lateinit var launcher: String private lateinit var launcher: String
private lateinit var imagePath: String private lateinit var imagePath: String
private var launchDelay = 15 private var launchDelay = 15
private lateinit var keyEvent: String
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
...@@ -31,6 +34,7 @@ class Task { ...@@ -31,6 +34,7 @@ class Task {
this.launcher = json.optString(Constants.LAUNCHER) this.launcher = json.optString(Constants.LAUNCHER)
this.launchDelay = json.optInt(Constants.LAUNCH_DELAY, 15) this.launchDelay = json.optInt(Constants.LAUNCH_DELAY, 15)
this.imagePath = this.context.cacheDir.absolutePath + "/" + this.packageName this.imagePath = this.context.cacheDir.absolutePath + "/" + this.packageName
this.keyEvent = json.optString(Constants.KEY_EVENT)
val dir = File(this.imagePath) val dir = File(this.imagePath)
if (!dir.exists()) if (!dir.exists())
dir.mkdirs() dir.mkdirs()
...@@ -38,6 +42,9 @@ class Task { ...@@ -38,6 +42,9 @@ class Task {
fun start() { fun start() {
this.launchApp() this.launchApp()
this.capture()
this.uploadImage()
this.report()
stop() stop()
} }
...@@ -72,6 +79,10 @@ class Task { ...@@ -72,6 +79,10 @@ class Task {
}).start() }).start()
} }
private fun report() {
}
private fun parseCaptureFile(filterUrl: String) { private fun parseCaptureFile(filterUrl: String) {
val file = File("/sdcard/capture.txt") val file = File("/sdcard/capture.txt")
val lines = file.readLines() val lines = file.readLines()
...@@ -112,8 +123,10 @@ class Task { ...@@ -112,8 +123,10 @@ class Task {
fun screenShot() { fun screenShot() {
val time = imageDateFormat.format(Date()) val time = imageDateFormat.format(Date())
val imagePath = this.imagePath + "/${packageName}_$time.png" var pngPath = this.imagePath + "/${packageName}_$time.png"
AdbUtil.screenShot(imagePath) val jpgPath = pngPath.replace(".png", ".jpg")
AdbUtil.screenShot(pngPath)
AppUtil.pngToJpg(pngPath, jpgPath)
} }
fun uploadImage() { fun uploadImage() {
......
...@@ -82,7 +82,7 @@ class BylRobotService: Service() { ...@@ -82,7 +82,7 @@ class BylRobotService: Service() {
private fun testKey() { private fun testKey() {
Thread(Runnable { Thread(Runnable {
AdbUtil.sendMultiKey("down/up/right",0.5f) AdbUtil.sendMultiKey("down/up/right")
}).start() }).start()
} }
......
...@@ -7,7 +7,7 @@ import com.duolebo.blyrobot.web.CaptureWebServer ...@@ -7,7 +7,7 @@ import com.duolebo.blyrobot.web.CaptureWebServer
class BylWebService : Service() { class BylWebService : Service() {
lateinit var webServer: CaptureWebServer private lateinit var webServer: CaptureWebServer
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
......
package com.duolebo.blyrobot.util package com.duolebo.blyrobot.util
import android.util.Log import android.util.Log
import com.duolebo.blyrobot.data.EmulateKey
import java.io.BufferedReader import java.io.BufferedReader
import java.io.DataOutputStream import java.io.DataOutputStream
import java.io.IOException import java.io.IOException
...@@ -47,7 +48,7 @@ object AdbUtil { ...@@ -47,7 +48,7 @@ object AdbUtil {
exeCmdEcho(exeCommands, root) exeCmdEcho(exeCommands, root)
} }
fun exeCmdEcho(commands: ArrayList<String>, root: Boolean = false) { fun exeCmdEcho(commands: ArrayList<String>, root: Boolean = false): List<String>? {
Log.i(TAG, "exe cmd echo: $commands") Log.i(TAG, "exe cmd echo: $commands")
val exeCommands = ArrayList<String>() val exeCommands = ArrayList<String>()
exeCommands.addAll(commands) exeCommands.addAll(commands)
...@@ -55,6 +56,8 @@ object AdbUtil { ...@@ -55,6 +56,8 @@ object AdbUtil {
for (line in results!!) { for (line in results!!) {
Log.i(TAG, "echo line: $line") Log.i(TAG, "echo line: $line")
} }
return results
} }
fun sendCommands(commands: ArrayList<String>, root: Boolean = false): List<String>? { fun sendCommands(commands: ArrayList<String>, root: Boolean = false): List<String>? {
...@@ -85,7 +88,7 @@ object AdbUtil { ...@@ -85,7 +88,7 @@ object AdbUtil {
dos.flush() dos.flush()
} }
dos.writeBytes(EXIT) dos.writeBytes(EXIT + LINE_BREAK)
dos.flush() dos.flush()
status = proc.waitFor() status = proc.waitFor()
...@@ -156,26 +159,38 @@ object AdbUtil { ...@@ -156,26 +159,38 @@ object AdbUtil {
exeCmdEcho(cmdKey, true) exeCmdEcho(cmdKey, true)
} }
fun sendMultiKey(keyPath: String, delay: Float) { fun sendMultiKey(keyPath: String) {
val allKey = ArrayList<String>() val keyEvents = ArrayList<EmulateKey>()
val keys = keyPath.split('/') val keys = keyPath.split(Constants.SLASH)
for (stepKey in keys) { for (stepKey in keys) {
if ('*' in stepKey) { var delay = "1"
val params = stepKey.split('*') if (Constants.VERTICAL_BAR in stepKey) {
for (i in 0..params[1].toInt()) val params = stepKey.split(Constants.VERTICAL_BAR)
allKey.add(params[0]) delay = params[1]
} else }
allKey.add(stepKey)
if (Constants.ASTERISK in stepKey) {
val params = stepKey.split(Constants.ASTERISK)
for (i in 0..params[1].toInt()) {
val keyEvent = EmulateKey()
keyEvent.key = params[0]
keyEvent.delay = delay
keyEvents.add(keyEvent)
}
} else {
val keyEvent = EmulateKey()
keyEvent.key = stepKey
keyEvent.delay = delay
keyEvents.add(keyEvent)
}
} }
val commands = ArrayList<String>() val commands = ArrayList<String>()
for (key in allKey) { for (keyEvent in keyEvents) {
Log.i(TAG, "send key: $key") Log.i(TAG, "send key: $keyEvent")
val cmdKey = "input keyevent ${Constants.KEY_MAP[key]}" val cmdKey = "input keyevent ${Constants.KEY_MAP[keyEvent.key]}"
commands.add(cmdKey) commands.add(cmdKey)
if (delay > 0) { commands.add("sleep ${keyEvent.delay}")
commands.add("sleep $delay")
}
} }
exeCmdEcho(commands, true) exeCmdEcho(commands, true)
......
...@@ -65,7 +65,7 @@ object AppUtil { ...@@ -65,7 +65,7 @@ object AppUtil {
return "" return ""
} }
fun pntToJpg(pngFilePath: String, jpgFilePath: String) { fun pngToJpg(pngFilePath: String, jpgFilePath: String) {
val bitmap = BitmapFactory.decodeFile(pngFilePath) val bitmap = BitmapFactory.decodeFile(pngFilePath)
try { try {
BufferedOutputStream(FileOutputStream(jpgFilePath)).use { bos -> BufferedOutputStream(FileOutputStream(jpgFilePath)).use { bos ->
......
...@@ -37,6 +37,11 @@ object Constants { ...@@ -37,6 +37,11 @@ object Constants {
const val PACKAGE_NAME = "packageName" const val PACKAGE_NAME = "packageName"
const val LAUNCHER = "launcher" const val LAUNCHER = "launcher"
const val LAUNCH_DELAY = "launchDelay" const val LAUNCH_DELAY = "launchDelay"
const val KEY_EVENT = "keyEvent"
const val ASTERISK = "*"
const val SLASH = "/"
const val VERTICAL_BAR = "|"
const val ACTION_LOCAL_SERVICE = "action_local_service" const val ACTION_LOCAL_SERVICE = "action_local_service"
} }
\ 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