Skip to content

Commit 866002f

Browse files
committed
Fix: Root detection - explicitly build su shell, better root check with id command
1 parent 9e434fc commit 866002f

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

app/src/main/java/com/appcontrolx/App.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,30 @@ class App : Application() {
1515
private const val TAG = "AppControlX"
1616

1717
init {
18-
// Configure libsu
18+
// Configure libsu for root shell
1919
Shell.enableVerboseLogging = BuildConfig.DEBUG
2020
Shell.setDefaultBuilder(
2121
Shell.Builder.create()
22-
.setFlags(Shell.FLAG_REDIRECT_STDERR)
23-
.setTimeout(30) // Increase timeout for slow devices
22+
.setFlags(Shell.FLAG_REDIRECT_STDERR or Shell.FLAG_NON_ROOT_SHELL)
23+
.setTimeout(30)
2424
)
2525
}
26+
27+
/**
28+
* Get root shell - call this to request su permission
29+
*/
30+
fun getRootShell(): Shell? {
31+
return try {
32+
val shell = Shell.Builder.create()
33+
.setFlags(Shell.FLAG_REDIRECT_STDERR)
34+
.setTimeout(30)
35+
.build("su")
36+
if (shell.isRoot) shell else null
37+
} catch (e: Exception) {
38+
Log.e(TAG, "Failed to get root shell", e)
39+
null
40+
}
41+
}
2642
}
2743

2844
override fun onCreate() {

app/src/main/java/com/appcontrolx/executor/RootExecutor.kt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ class RootExecutor : CommandExecutor {
5858
}
5959

6060
return try {
61-
// Ensure shell is ready
62-
val shell = Shell.getShell()
61+
// Build su shell and execute command
62+
val shell = Shell.Builder.create()
63+
.setFlags(Shell.FLAG_REDIRECT_STDERR)
64+
.setTimeout(30)
65+
.build("su")
66+
6367
if (!shell.isRoot) {
64-
return Result.failure(Exception("Root access not available"))
68+
return Result.failure(Exception("Root access not available - su denied"))
6569
}
6670

67-
val result = Shell.cmd(command).exec()
71+
val result = shell.newJob().add(command).exec()
6872
if (result.isSuccess) {
6973
Result.success(result.out.joinToString("\n"))
7074
} else {
@@ -84,7 +88,20 @@ class RootExecutor : CommandExecutor {
8488
}
8589

8690
return try {
87-
val result = Shell.cmd(*commands.toTypedArray()).exec()
91+
// Build su shell
92+
val shell = Shell.Builder.create()
93+
.setFlags(Shell.FLAG_REDIRECT_STDERR)
94+
.setTimeout(30)
95+
.build("su")
96+
97+
if (!shell.isRoot) {
98+
return Result.failure(Exception("Root access not available - su denied"))
99+
}
100+
101+
val job = shell.newJob()
102+
commands.forEach { job.add(it) }
103+
val result = job.exec()
104+
88105
if (result.isSuccess) {
89106
Result.success(Unit)
90107
} else {

app/src/main/java/com/appcontrolx/service/PermissionBridge.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,27 @@ class PermissionBridge(private val context: Context? = null) {
6464
*/
6565
fun checkRootNow(): Boolean {
6666
return try {
67-
// Get shell instance - this will trigger root request if needed
68-
val shell = Shell.getShell()
67+
// Try to get root shell by executing su
68+
val result = Shell.cmd("id").exec()
6969

70-
// Check if shell is root
71-
if (shell.isRoot) {
72-
return true
70+
if (result.isSuccess) {
71+
val output = result.out.joinToString("\n")
72+
// Check if we're running as root (uid=0)
73+
if (output.contains("uid=0")) {
74+
android.util.Log.d("PermissionBridge", "Root confirmed: $output")
75+
return true
76+
}
7377
}
7478

75-
// Fallback check
76-
Shell.isAppGrantedRoot() == true
79+
// Alternative: try su directly
80+
val suResult = Shell.Builder.create()
81+
.setFlags(Shell.FLAG_REDIRECT_STDERR)
82+
.setTimeout(30)
83+
.build("su")
84+
85+
val isRoot = suResult.isRoot
86+
android.util.Log.d("PermissionBridge", "Su shell isRoot: $isRoot")
87+
isRoot
7788
} catch (e: Exception) {
7889
android.util.Log.e("PermissionBridge", "Root check failed", e)
7990
false

0 commit comments

Comments
 (0)