-
-
Notifications
You must be signed in to change notification settings - Fork 450
Merge pull request #253 from rainxchzed/feat-shizuku-installer #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ab5dcd1
Merge pull request #253 from rainxchzed/feat-shizuku-installer
rainxchzed 35fc40f
Refactor `ShizukuInstallerServiceImpl` to use reflection for hidden A…
rainxchzed 44fdc2d
Merge branch 'main' into shizuku-integration
rainxchzed 541614a
Refactor Shizuku installer to use `ParcelFileDescriptor` for APK inst…
rainxchzed a6f6b90
Refactor Shizuku installer to use shell commands instead of reflection
rainxchzed fc66218
feat(shizuku): implement timeouts and improve service binding
rainxchzed b4d66b2
Add translations for Shizuku installation method settings across mult…
rainxchzed 68ebb66
docs(readme): update feature structure and technical specifications
rainxchzed 8eba4ca
feat(profile): Add Shizuku-based auto-update functionality
rainxchzed abaa77d
feat(core/data): add AutoUpdateWorker and BootReceiver for Android
rainxchzed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...src/androidMain/aidl/zed/rainxch/core/data/services/shizuku/IShizukuInstallerService.aidl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package zed.rainxch.core.data.services.shizuku; | ||
|
|
||
| interface IShizukuInstallerService { | ||
| int installPackage(in ParcelFileDescriptor pfd, long fileSize); | ||
| int uninstallPackage(String packageName); | ||
| void destroy(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...droidMain/kotlin/zed/rainxch/core/data/services/shizuku/AndroidInstallerStatusProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package zed.rainxch.core.data.services.shizuku | ||
|
|
||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.flow.SharingStarted | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.flow.stateIn | ||
| import zed.rainxch.core.domain.model.ShizukuAvailability | ||
| import zed.rainxch.core.domain.system.InstallerStatusProvider | ||
|
|
||
| /** | ||
| * Android implementation of [InstallerStatusProvider]. | ||
| * Maps [ShizukuServiceManager.status] to the platform-agnostic [ShizukuAvailability] enum. | ||
| */ | ||
| class AndroidInstallerStatusProvider( | ||
| private val shizukuServiceManager: ShizukuServiceManager, | ||
| scope: CoroutineScope | ||
| ) : InstallerStatusProvider { | ||
|
|
||
| override val shizukuAvailability: StateFlow<ShizukuAvailability> = | ||
| shizukuServiceManager.status.map { status -> | ||
| when (status) { | ||
| ShizukuStatus.NOT_INSTALLED -> ShizukuAvailability.UNAVAILABLE | ||
| ShizukuStatus.NOT_RUNNING -> ShizukuAvailability.NOT_RUNNING | ||
| ShizukuStatus.PERMISSION_NEEDED -> ShizukuAvailability.PERMISSION_NEEDED | ||
| ShizukuStatus.READY -> ShizukuAvailability.READY | ||
| } | ||
| }.stateIn( | ||
| scope = scope, | ||
| started = SharingStarted.Eagerly, | ||
| initialValue = ShizukuAvailability.UNAVAILABLE | ||
| ) | ||
|
|
||
| override fun requestShizukuPermission() { | ||
| shizukuServiceManager.requestPermission() | ||
| } | ||
| } | ||
140 changes: 140 additions & 0 deletions
140
.../androidMain/kotlin/zed/rainxch/core/data/services/shizuku/ShizukuInstallerServiceImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package zed.rainxch.core.data.services.shizuku | ||
|
|
||
| import android.os.ParcelFileDescriptor | ||
| import java.io.BufferedReader | ||
| import java.io.InputStreamReader | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| /** | ||
| * Shizuku UserService implementation that runs in a privileged process (shell/root). | ||
| * Provides silent package install/uninstall via `pm` shell commands. | ||
| * | ||
| * This class runs in Shizuku's process, NOT in the app's process. | ||
| * It has shell-level (UID 2000) or root-level (UID 0) privileges. | ||
| * | ||
| * Uses `pm install` with stdin pipe for install, `pm uninstall` for uninstall. | ||
| * This is the most reliable approach — avoids fragile reflection on hidden | ||
| * IPackageInstaller/IPackageInstallerSession/IIntentSender APIs. | ||
| * | ||
| * MUST have a default no-arg constructor for Shizuku's UserService framework. | ||
| */ | ||
| class ShizukuInstallerServiceImpl() : IShizukuInstallerService.Stub() { | ||
|
|
||
| companion object { | ||
| private const val TAG = "ShizukuService" | ||
|
|
||
| private const val STATUS_SUCCESS = 0 | ||
| private const val STATUS_FAILURE = -1 | ||
| private const val INSTALL_TIMEOUT_SECONDS = 120L | ||
| private const val UNINSTALL_TIMEOUT_SECONDS = 30L | ||
|
|
||
| private fun log(msg: String) = android.util.Log.d(TAG, msg) | ||
| private fun logW(msg: String) = android.util.Log.w(TAG, msg) | ||
| private fun logE(msg: String, e: Throwable? = null) = android.util.Log.e(TAG, msg, e) | ||
| } | ||
|
|
||
| override fun installPackage(pfd: ParcelFileDescriptor, fileSize: Long): Int { | ||
| log("installPackage() called — fileSize=$fileSize") | ||
| log("Process UID: ${android.os.Process.myUid()}, PID: ${android.os.Process.myPid()}") | ||
|
|
||
| return try { | ||
| // Use "pm install -S <size>" which reads the APK from stdin | ||
| val command = arrayOf("pm", "install", "-S", fileSize.toString()) | ||
| log("Executing: ${command.joinToString(" ")}") | ||
|
|
||
| val process = Runtime.getRuntime().exec(command) | ||
|
|
||
| // Pipe the APK from the ParcelFileDescriptor to pm's stdin | ||
| val writeThread = Thread { | ||
| try { | ||
| ParcelFileDescriptor.AutoCloseInputStream(pfd).use { input -> | ||
| process.outputStream.use { output -> | ||
| val buffer = ByteArray(65536) | ||
| var bytesWritten = 0L | ||
| var read: Int | ||
| while (input.read(buffer).also { read = it } != -1) { | ||
| output.write(buffer, 0, read) | ||
| bytesWritten += read | ||
| } | ||
| output.flush() | ||
| log("APK piped to pm stdin: $bytesWritten bytes (expected: $fileSize)") | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| logE("Error piping APK to pm stdin", e) | ||
| } | ||
| } | ||
| writeThread.start() | ||
|
|
||
| // Read stdout/stderr for result | ||
| val stdout = BufferedReader(InputStreamReader(process.inputStream)).readText().trim() | ||
| val stderr = BufferedReader(InputStreamReader(process.errorStream)).readText().trim() | ||
|
|
||
| writeThread.join(INSTALL_TIMEOUT_SECONDS * 1000) | ||
| if (writeThread.isAlive) { | ||
| logE("Write thread timed out after ${INSTALL_TIMEOUT_SECONDS}s, aborting install") | ||
| writeThread.interrupt() | ||
| process.destroyForcibly() | ||
| return STATUS_FAILURE | ||
| } | ||
|
|
||
| val finished = process.waitFor(INSTALL_TIMEOUT_SECONDS, TimeUnit.SECONDS) | ||
| if (!finished) { | ||
| logE("pm install timed out after ${INSTALL_TIMEOUT_SECONDS}s, aborting") | ||
| process.destroyForcibly() | ||
| return STATUS_FAILURE | ||
| } | ||
|
|
||
| val exitCode = process.exitValue() | ||
| log("pm install — exitCode=$exitCode, stdout='$stdout', stderr='$stderr'") | ||
|
|
||
| if (exitCode == 0 && stdout.contains("Success")) { | ||
| log("Install SUCCESS") | ||
| STATUS_SUCCESS | ||
| } else { | ||
| logE("Install FAILED — exitCode=$exitCode, stdout='$stdout', stderr='$stderr'") | ||
| STATUS_FAILURE | ||
| } | ||
| } catch (e: Exception) { | ||
| logE("installPackage() exception", e) | ||
| STATUS_FAILURE | ||
| } | ||
| } | ||
|
|
||
| override fun uninstallPackage(packageName: String): Int { | ||
| log("uninstallPackage() called for: $packageName") | ||
| return try { | ||
| val command = arrayOf("pm", "uninstall", packageName) | ||
| log("Executing: ${command.joinToString(" ")}") | ||
|
|
||
| val process = Runtime.getRuntime().exec(command) | ||
| val stdout = BufferedReader(InputStreamReader(process.inputStream)).readText().trim() | ||
| val stderr = BufferedReader(InputStreamReader(process.errorStream)).readText().trim() | ||
|
|
||
| val finished = process.waitFor(UNINSTALL_TIMEOUT_SECONDS, TimeUnit.SECONDS) | ||
| if (!finished) { | ||
| logE("pm uninstall timed out after ${UNINSTALL_TIMEOUT_SECONDS}s, aborting") | ||
| process.destroyForcibly() | ||
| return STATUS_FAILURE | ||
| } | ||
|
|
||
| val exitCode = process.exitValue() | ||
| log("pm uninstall — exitCode=$exitCode, stdout='$stdout', stderr='$stderr'") | ||
|
|
||
| if (exitCode == 0 && stdout.contains("Success")) { | ||
| log("Uninstall SUCCESS") | ||
| STATUS_SUCCESS | ||
| } else { | ||
| logE("Uninstall FAILED — exitCode=$exitCode, stdout='$stdout', stderr='$stderr'") | ||
| STATUS_FAILURE | ||
| } | ||
| } catch (e: Exception) { | ||
| logE("uninstallPackage() exception", e) | ||
| STATUS_FAILURE | ||
| } | ||
| } | ||
|
|
||
| override fun destroy() { | ||
| log("destroy() — service being unbound") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve the
NOT_INSTALLEDstate instead of collapsing it toUNAVAILABLE.Line 23 drops a user-actionable distinction the new UI needs. With this mapping, the profile flow cannot tell “Shizuku is not installed” apart from generic unavailability, so the install-specific status/hint strings added in this PR become unreachable. Please expose a dedicated availability for that case, or pass the raw
ShizukuStatusthrough to the UI layer.