|
| 1 | +package app.pwhs.universalinstaller.presentation.install.controller |
| 2 | + |
| 3 | +import android.content.pm.PackageManager |
| 4 | +import kotlinx.coroutines.Dispatchers |
| 5 | +import kotlinx.coroutines.withContext |
| 6 | +import rikka.shizuku.Shizuku |
| 7 | +import timber.log.Timber |
| 8 | + |
| 9 | +/** |
| 10 | + * Run privileged `pm` commands via Shizuku's `newProcess()` — same shell UID (2000) that |
| 11 | + * `adb shell` runs as, so per-user uninstall and disable of system apps work without Root. |
| 12 | + * |
| 13 | + * Shizuku v13 moved `newProcess` to private visibility to push callers toward the |
| 14 | + * UserService/AIDL pattern. A UserService would take ~100 LOC for a one-shot shell wrapper, |
| 15 | + * so instead we call via reflection. If a future Shizuku release ever removes the method, |
| 16 | + * [isReady] flips to false and the ViewModel routes users to the "Root required" dialog. |
| 17 | + */ |
| 18 | +object ShizukuShellExecutor { |
| 19 | + |
| 20 | + private val newProcessMethod: java.lang.reflect.Method? = try { |
| 21 | + Shizuku::class.java.getDeclaredMethod( |
| 22 | + "newProcess", |
| 23 | + Array<String>::class.java, |
| 24 | + Array<String>::class.java, |
| 25 | + String::class.java, |
| 26 | + ).apply { isAccessible = true } |
| 27 | + } catch (t: Throwable) { |
| 28 | + Timber.w(t, "Shizuku.newProcess not reachable — Shizuku shell path disabled") |
| 29 | + null |
| 30 | + } |
| 31 | + |
| 32 | + fun isReady(): Boolean { |
| 33 | + if (newProcessMethod == null) return false |
| 34 | + return try { |
| 35 | + Shizuku.pingBinder() && |
| 36 | + !Shizuku.isPreV11() && |
| 37 | + Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED |
| 38 | + } catch (t: Throwable) { |
| 39 | + Timber.w(t, "Shizuku readiness check failed") |
| 40 | + false |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Shells out via Shizuku's remote process. Package name is regex-validated to block |
| 46 | + * shell injection — in practice the caller feeds a PackageManager-sourced string that |
| 47 | + * can't contain metacharacters, but we verify anyway. |
| 48 | + */ |
| 49 | + suspend fun uninstallSystemApp( |
| 50 | + packageName: String, |
| 51 | + method: SystemAppMethod, |
| 52 | + ): Result<String> = withContext(Dispatchers.IO) { |
| 53 | + require(packageName.matches(Regex("^[A-Za-z0-9._]+$"))) { |
| 54 | + "Refusing to shell out with suspicious package name: $packageName" |
| 55 | + } |
| 56 | + val reflectedMethod = newProcessMethod |
| 57 | + ?: return@withContext Result.failure( |
| 58 | + IllegalStateException("Shizuku.newProcess unavailable on this Shizuku build"), |
| 59 | + ) |
| 60 | + val cmd = when (method) { |
| 61 | + SystemAppMethod.UninstallForUser0 -> "pm uninstall --user 0 $packageName" |
| 62 | + SystemAppMethod.Disable -> "pm disable-user --user 0 $packageName" |
| 63 | + } |
| 64 | + runCatching { |
| 65 | + val process = reflectedMethod.invoke( |
| 66 | + null, |
| 67 | + arrayOf("sh", "-c", cmd), |
| 68 | + null, |
| 69 | + null, |
| 70 | + ) as Process |
| 71 | + val stdout = process.inputStream.bufferedReader().use { it.readText() } |
| 72 | + val stderr = process.errorStream.bufferedReader().use { it.readText() } |
| 73 | + val exitCode = process.waitFor() |
| 74 | + // Same token strategy as the Root path — `pm` on some ROMs returns 0 for soft |
| 75 | + // failures like `Failure [NOT_INSTALLED_FOR_USER]`, so we verify both. |
| 76 | + val successToken = when (method) { |
| 77 | + SystemAppMethod.UninstallForUser0 -> "Success" |
| 78 | + SystemAppMethod.Disable -> "new state: disabled" |
| 79 | + } |
| 80 | + if (exitCode != 0 || !stdout.contains(successToken, ignoreCase = true)) { |
| 81 | + throw RuntimeException( |
| 82 | + "shizuku pm failed (exit=$exitCode): ${stdout.ifBlank { stderr }.ifBlank { "no output" }}", |
| 83 | + ) |
| 84 | + } |
| 85 | + stdout |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments