Improve file existence check and copying methods, optimize command execution logic, and fix KPM version display issue (#298)

* fix: Remove the incorrect display of the KPM version and simplify the checking logic

* refactor: Optimize the command execution logic and simplify the code structure.

* fix: Improve the file existence check and file copying methods.
This commit is contained in:
Seyud
2025-07-29 15:16:34 +08:00
committed by GitHub
parent c42b4ffe4b
commit b1ee07fee1
4 changed files with 12 additions and 22 deletions

View File

@@ -19,10 +19,12 @@ public class UltraShellHelper {
} }
public static boolean isPathExists(String path) { public static boolean isPathExists(String path) {
return runCmd("file " + path).contains("No such file or directory"); String result = runCmd("test -f '" + path + "' && echo 'exists'");
return result.contains("exists");
} }
public static void CopyFileTo(String path, String target) { public static boolean CopyFileTo(String path, String target) {
runCmd("cp -f " + path + " " + target); String result = runCmd("cp -f '" + path + "' '" + target + "' 2>&1");
return !result.contains("cp: ");
} }
} }

View File

@@ -59,7 +59,7 @@ fun BottomBar(navController: NavHostController) {
) { ) {
BottomBarDestination.entries.forEach { destination -> BottomBarDestination.entries.forEach { destination ->
if (destination == BottomBarDestination.Kpm) { if (destination == BottomBarDestination.Kpm) {
if (kpmVersion.isNotEmpty() && !kpmVersion.startsWith("Error") && !showKpmInfo && Natives.version >= Natives.MINIMAL_SUPPORTED_KPM) { if (kpmVersion.isNotEmpty() && !showKpmInfo && Natives.version >= Natives.MINIMAL_SUPPORTED_KPM) {
if (!isFullFeatured && destination.rootRequired) return@forEach if (!isFullFeatured && destination.rootRequired) return@forEach
val isCurrentDestOnBackStack by navController.isRouteOnBackStackAsState(destination.direction) val isCurrentDestOnBackStack by navController.isRouteOnBackStackAsState(destination.direction)
NavigationBarItem( NavigationBarItem(

View File

@@ -88,7 +88,7 @@ object AppData {
val version = getKpmVersion() val version = getKpmVersion()
if (version.isEmpty()) "" else version if (version.isEmpty()) "" else version
} catch (e: Exception) { } catch (e: Exception) {
"Error: ${e.message}" ""
} }
} }

View File

@@ -287,16 +287,10 @@ class HorizonKernelWorker(
} }
private fun runCommand(su: Boolean, cmd: String): Int { private fun runCommand(su: Boolean, cmd: String): Int {
val process = ProcessBuilder(if (su) "su" else "sh") val shell = if (su) "su" else "sh"
.redirectErrorStream(true) val process = Runtime.getRuntime().exec(arrayOf(shell, "-c", cmd))
.start()
return try { return try {
process.outputStream.bufferedWriter().use { writer ->
writer.write("$cmd\n")
writer.write("exit\n")
writer.flush()
}
process.waitFor() process.waitFor()
} finally { } finally {
process.destroy() process.destroy()
@@ -304,16 +298,10 @@ class HorizonKernelWorker(
} }
private fun runCommandGetOutput(su: Boolean, cmd: String): String? { private fun runCommandGetOutput(su: Boolean, cmd: String): String? {
val process = ProcessBuilder(if (su) "su" else "sh") val shell = if (su) "su" else "sh"
.redirectErrorStream(true) val process = Runtime.getRuntime().exec(arrayOf(shell, "-c", cmd))
.start()
return try { return try {
process.outputStream.bufferedWriter().use { writer ->
writer.write("$cmd\n")
writer.write("exit\n")
writer.flush()
}
process.inputStream.bufferedReader().use { reader -> process.inputStream.bufferedReader().use { reader ->
reader.readText().trim() reader.readText().trim()
} }
@@ -326,7 +314,7 @@ class HorizonKernelWorker(
private fun rootAvailable(): Boolean { private fun rootAvailable(): Boolean {
return try { return try {
val process = Runtime.getRuntime().exec("su -c id") val process = Runtime.getRuntime().exec("su -c true")
val exitValue = process.waitFor() val exitValue = process.waitFor()
exitValue == 0 exitValue == 0
} catch (_: Exception) { } catch (_: Exception) {