Commit 30946a2f authored by liuyang's avatar liuyang

流程串联

parent 7359fe33
......@@ -8,7 +8,6 @@ import android.util.Log
import com.duolebo.blyrobot.service.BylRobotService
import com.duolebo.blyrobot.util.AdbUtil
import com.duolebo.blyrobot.util.Constants
import com.duolebo.blyrobot.util.ShellUtils
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
......@@ -20,8 +19,6 @@ class MainActivity : AppCompatActivity() {
Log.i(TAG, "onCreate....")
setContentView(R.layout.activity_main)
// val result = ShellUtils.execCommand("killall", true)
// Log.i(TAG, result.errorMsg + " " + result.errorMsg)
val hasRoot = AdbUtil.checkRootPermission()
if (hasRoot) {
Log.i(TAG, "rooted...")
......
......@@ -19,6 +19,7 @@ class Task {
private lateinit var launcher: String
private lateinit var imagePath: String
private var launchDelay = 15
private var captureDelay = 15
private lateinit var keyEvent: String
private var context: Context
......@@ -69,14 +70,14 @@ class Task {
}
private fun capture() {
proc?.destroy()
Thread(Runnable {
AdbUtil.killTcpdump()
}).start()
Thread.sleep(3000)
Thread(Runnable {
proc = AdbUtil.tcpCapture("")
}).start()
Thread.sleep(captureDelay * 1000L)
proc?.destroy()
AdbUtil.killTcpdump()
Thread.sleep(3000L)
parseCaptureFile("")
}
private fun report() {
......@@ -86,7 +87,7 @@ class Task {
private fun parseCaptureFile(filterUrl: String) {
val file = File("/sdcard/capture.txt")
val lines = file.readLines()
val playUrlItems = JSONArray()
val playUrlItems = ArrayList<PlayInfo>()
var partUrl = ""
var timeStr = ""
......@@ -107,17 +108,23 @@ class Task {
val start = it.indexOf(' ') + 1
val host = it.substring(start)
val url = "http://$host$partUrl"
val item = JSONObject()
item.put(Constants.KEY_URL, url)
item.put(Constants.KEY_TIME, timeStr)
playUrlItems.put(item)
val item = PlayInfo()
item.url = url
item.time = timeStr
playUrlItems.add(item)
partUrl = ""
}
}
for (i in 0..playUrlItems.length()) {
val jo = playUrlItems.get(i) as JSONObject
val url = jo.optString(Constants.KEY_URL)
for (i in 0..playUrlItems.size) {
val item = playUrlItems.get(i)
if (filterUrl.isNullOrEmpty()) {
}
else {
}
}
}
......@@ -153,4 +160,9 @@ class Task {
interface OnTaskListener {
fun onComplete()
}
class PlayInfo {
var time = ""
var url = ""
}
}
\ No newline at end of file
......@@ -42,35 +42,38 @@ object AdbUtil {
return proc
}
fun exeCmdEcho(cmd: String, root: Boolean = false) {
fun exeCmdEcho(cmd: String, root: Boolean = false): CmdResult {
val exeCommands = ArrayList<String>()
exeCommands.add(cmd)
exeCmdEcho(exeCommands, root)
return exeCmdEcho(exeCommands, root)
}
fun exeCmdEcho(commands: ArrayList<String>, root: Boolean = false): List<String>? {
fun exeCmdEcho(commands: ArrayList<String>, root: Boolean = false): CmdResult {
Log.i(TAG, "exe cmd echo: $commands")
val exeCommands = ArrayList<String>()
exeCommands.addAll(commands)
val results = sendCommands(exeCommands, root)
for (line in results!!) {
Log.i(TAG, "echo line: $line")
val result = sendCommands(exeCommands, root)
for (line in result.successLines) {
Log.i(TAG, "echo success line: $line")
}
return results
for (line in result.errorLines) {
Log.i(TAG, "echo error line: $line")
}
return result
}
fun sendCommands(commands: ArrayList<String>, root: Boolean = false): List<String>? {
val results = ArrayList<String>()
fun sendCommands(commands: ArrayList<String>, root: Boolean = false): CmdResult {
val result = CmdResult(-1, "", "")
var status = -1
if (commands.isEmpty()) {
return null
return result
}
Log.i(TAG, "exe commands : $commands")
var proc: Process? = null
var successBufReader: BufferedReader? = null
var errorBufReader: BufferedReader? = null
var errorMsg: StringBuilder? = null
var dos: DataOutputStream? = null
try {
......@@ -93,7 +96,6 @@ object AdbUtil {
status = proc.waitFor()
errorMsg = StringBuilder()
successBufReader = BufferedReader(InputStreamReader(proc.inputStream))
errorBufReader = BufferedReader(InputStreamReader(proc.errorStream))
var lineStr: String? = null
......@@ -103,11 +105,10 @@ object AdbUtil {
break
lineStr = trimLine(lineStr)
if (lineStr !in commands)
results.add(lineStr)
result.successLines.add(lineStr)
Log.i(TAG, " success line echo : $lineStr")
} while (true)
results.add("----------")
do {
lineStr = errorBufReader.readLine()
......@@ -115,7 +116,7 @@ object AdbUtil {
break
lineStr = trimLine(lineStr)
if (lineStr !in commands)
results.add(lineStr)
result.errorLines.add(lineStr)
Log.i(TAG, " error line echo : $lineStr")
} while (true)
......@@ -134,8 +135,13 @@ object AdbUtil {
proc?.destroy()
}
Log.i(TAG, "error message:$errorMsg and status $status")
return results
Log.i(TAG, "error message:${result.errorLines} and status $status")
if (result.errorLines.size > 0 && result.successLines.size <= 0)
status = -1
result.status = status
return result
}
private fun trimLine(line: String): String {
......@@ -266,8 +272,24 @@ object AdbUtil {
}
fun killTcpdump() {
exeCmdEcho("killall", true)
// exeCmdEcho("killall tcpdump", true)
val result = exeCmdEcho("killall", true)
if (result.status != -1)
exeCmdEcho("killall tcpdump", true)
else {
val psResult = exeCmdEcho("ps | grep tcpdump", true)
for (line in psResult.successLines) {
val arr = line.split(' ')
val items = ArrayList<String>()
arr.forEach {
if (!it.isNullOrEmpty())
items.add(it)
}
if (items.isNotEmpty()) {
exeCmdEcho("kill " + items[1], true)
}
}
}
}
fun resetApp(packageName: String) {
......@@ -282,4 +304,22 @@ object AdbUtil {
exeCmdEcho("am force-stop $packageName", true)
}
class CmdResult {
var status: Int = 0
var successLines = ArrayList<String>()
var errorLines = ArrayList<String>()
constructor(status: Int) {
this.status = status
}
constructor(result: Int, successMsg: String, errorMsg: String) {
this.status = result
if (!successMsg.isNullOrEmpty())
this.successLines.add(successMsg)
if (!errorMsg.isNullOrEmpty())
this.errorLines.add(errorMsg)
}
}
}
\ No newline at end of file
package com.duolebo.blyrobot.util;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
/**
* ShellUtils
* <ul>
* <strong>Check root</strong>
* <li>{@link ShellUtils#checkRootPermission()}</li>
* </ul>
* <ul>
* <strong>Execte command</strong>
* <li>{@link ShellUtils#execCommand(String, boolean)}</li>
* <li>{@link ShellUtils#execCommand(String, boolean, boolean)}</li>
* <li>{@link ShellUtils#execCommand(List, boolean)}</li>
* <li>{@link ShellUtils#execCommand(List, boolean, boolean)}</li>
* <li>{@link ShellUtils#execCommand(String[], boolean)}</li>
* <li>{@link ShellUtils#execCommand(String[], boolean, boolean)}</li>
* </ul>
*/
public class ShellUtils {
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
private ShellUtils() {
throw new AssertionError();
}
/**
* check whether has root permission
*
* @return
*/
public static boolean checkRootPermission() {
return execCommand("echo root", true, false).result == 0;
}
/**
* execute shell command, default return result msg
*
* @param command command
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] {command}, isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command list
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List<String> commands, boolean isRoot) {
return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command array
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String[] commands, boolean isRoot) {
return execCommand(commands, isRoot, true);
}
/**
* execute shell command
*
* @param command command
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(new String[] {command}, isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command list
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command array
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return <ul>
* <li>if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and
* {@link CommandResult#errorMsg} is null.</li>
* <li>if {@link CommandResult#result} is -1, there maybe some excepiton.</li>
* </ul>
*/
public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
// get command result
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}
/**
* result of command
* <ul>
* <li>{@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in
* linux shell</li>
* <li>{@link CommandResult#successMsg} means success message of command result</li>
* <li>{@link CommandResult#errorMsg} means error message of command result</li>
* </ul>
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-16
*/
public static class CommandResult {
/** result of command **/
public int result;
/** success message of command result **/
public String successMsg;
/** error message of command result **/
public String errorMsg;
public CommandResult(int result) {
this.result = result;
}
public CommandResult(int result, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
}
\ 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