|
@@ -2,9 +2,13 @@ package com.eastpolar.hookcli.tools
|
|
|
|
|
|
import android.content.Context
|
|
|
import android.util.Log
|
|
|
+import android.widget.Toast
|
|
|
+import java.io.BufferedReader
|
|
|
import java.io.DataOutputStream
|
|
|
import java.io.File
|
|
|
import java.io.IOException
|
|
|
+import java.io.InputStream
|
|
|
+import java.io.InputStreamReader
|
|
|
|
|
|
|
|
|
class AutoCopySO {
|
|
@@ -48,10 +52,13 @@ class AutoCopySO {
|
|
|
val process = Runtime.getRuntime().exec("su")
|
|
|
val os = DataOutputStream(process.outputStream)
|
|
|
|
|
|
+ val inputStream = process.inputStream
|
|
|
+ val errorStream = process.errorStream
|
|
|
+
|
|
|
// 创建目标目录,防止目标目录不存在
|
|
|
os.writeBytes("mkdir -p $targetPath\n")
|
|
|
|
|
|
- os.writeBytes("mkdir -p $targetPath/test\n")
|
|
|
+// os.writeBytes("mkdir -p $targetPath/test\n")
|
|
|
|
|
|
// 复制文件
|
|
|
os.writeBytes("cp $soPath $targetSoFile\n")
|
|
@@ -62,9 +69,17 @@ class AutoCopySO {
|
|
|
// 退出su
|
|
|
os.writeBytes("exit\n")
|
|
|
os.flush()
|
|
|
- process.waitFor()
|
|
|
-
|
|
|
os.close()
|
|
|
+
|
|
|
+ // 读取命令的输出
|
|
|
+ val output = readStream(inputStream)
|
|
|
+ val error = readStream(errorStream)
|
|
|
+ // 输出命令执行结果
|
|
|
+ println("Output: $output")
|
|
|
+ println("Error: $error")
|
|
|
+
|
|
|
+ //等待命令执行完毕
|
|
|
+ process.waitFor()
|
|
|
if (process.exitValue() == 0) {
|
|
|
// 成功复制
|
|
|
println("Copy successful")
|
|
@@ -73,12 +88,22 @@ class AutoCopySO {
|
|
|
println("Copy failed")
|
|
|
}
|
|
|
} catch (e: IOException) {
|
|
|
+ Toast.makeText(context, "没有su权限", Toast.LENGTH_SHORT).show()
|
|
|
e.printStackTrace()
|
|
|
} catch (e: InterruptedException) {
|
|
|
e.printStackTrace()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private fun readStream(inputStream: InputStream): String {
|
|
|
+ val bufferedReader = BufferedReader(InputStreamReader(inputStream))
|
|
|
+ val stringBuilder = StringBuilder()
|
|
|
+ var line: String?
|
|
|
+ while (bufferedReader.readLine().also { line = it } != null) {
|
|
|
+ stringBuilder.append(line).append("\n")
|
|
|
+ }
|
|
|
+ return stringBuilder.toString()
|
|
|
+ }
|
|
|
//单例模式
|
|
|
companion object {
|
|
|
val instance: AutoCopySO by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|