将 su 命令更换为 Magisk 的开源库 libsu

This commit is contained in:
2022-02-07 21:54:28 +08:00
parent 3b338214fc
commit 4914ae20b2
3 changed files with 9 additions and 27 deletions

View File

@@ -45,6 +45,7 @@ android {
dependencies {
compileOnly 'de.robv.android.xposed:api:82'
implementation "com.github.topjohnwu.libsu:core:3.1.2"
// 基础依赖包
implementation 'com.gyf.immersionbar:immersionbar:3.0.0'
// Fragment 快速实现

View File

@@ -208,9 +208,9 @@ class MainActivity : BaseActivity() {
/** 重启系统界面 */
private fun restartSystemUI() =
execShellCmd(cmd = "pgrep systemui").also { pid ->
execShellSu(cmd = "pgrep systemui").also { pid ->
if (pid.isNotBlank())
execShellCmd(cmd = "kill -9 $pid")
execShellSu(cmd = "kill -9 $pid")
else Toast.makeText(this, "ROOT 权限获取失败", Toast.LENGTH_SHORT).show()
}

View File

@@ -33,8 +33,7 @@ import android.provider.Settings
import android.service.notification.StatusBarNotification
import android.util.Base64
import com.fankes.miui.notify.application.MNNApplication.Companion.appContext
import java.io.DataInputStream
import java.io.DataOutputStream
import com.topjohnwu.superuser.Shell
/**
* 系统深色模式是否开启
@@ -268,28 +267,10 @@ fun findPropString(key: String, default: String = "") =
* @param cmd 命令
* @return [String] 执行结果
*/
fun execShellCmd(cmd: String): String {
var result = ""
var dos: DataOutputStream? = null
var dis: DataInputStream? = null
try {
val p = Runtime.getRuntime().exec("su")
dos = DataOutputStream(p.outputStream)
dis = DataInputStream(p.inputStream)
dos.writeBytes("$cmd\n")
dos.flush()
dos.writeBytes("exit\n")
dos.flush()
var line: String
while (dis.readLine().also { line = it } != null) result += line
p.waitFor()
} catch (_: Exception) {
} finally {
try {
dos?.close()
dis?.close()
} catch (_: Exception) {
fun execShellSu(cmd: String) = try {
Shell.su(cmd).exec().out.let {
if (it.isNotEmpty()) it[0].trim() else ""
}
}
return result.trim()
} catch (_: Throwable) {
""
}