Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.kotlin.parcelize)
alias(libs.plugins.compose)
id("dev.rikka.tools.refine") version "4.4.0"
}

android {
Expand All @@ -18,7 +19,7 @@ android {

defaultConfig {
applicationId = "com.looker.droidify"
minSdk = 23
minSdk = 26
versionName = latestVersionName
versionCode = 730

Expand Down Expand Up @@ -188,6 +189,16 @@ dependencies {
androidTestImplementation(libs.bundles.test.android)
kspAndroidTest(libs.hilt.compiler)

// shizuku_apk_installer dependencies
implementation("dev.rikka.tools.refine:runtime:4.4.0")
implementation("dev.rikka.shizuku:api:13.1.5")
implementation("dev.rikka.shizuku:provider:13.1.5")
implementation("io.github.iamr0s:Dhizuku-API:2.5.4")

implementation("dev.rikka.tools.refine:runtime:4.4.0")
compileOnly("dev.rikka.hidden:stub:4.4.0")
implementation("org.lsposed.hiddenapibypass:hiddenapibypass:6.1")

// debugImplementation(libs.leakcanary)
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@
android:authorities="${applicationId}.androidx-startup"
tools:node="remove" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

<receiver
android:name=".receivers.UnarchivePackageReceiver"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,83 @@
package com.looker.droidify.installer.installers.shizuku

import android.content.Context
import android.content.pm.PackageInstaller
import android.net.Uri
import com.looker.droidify.data.model.PackageName
import com.looker.droidify.installer.installers.Installer
import com.looker.droidify.installer.installers.uninstallPackage
import com.looker.droidify.installer.model.InstallItem
import com.looker.droidify.installer.model.InstallState
import com.looker.droidify.utility.common.SdkCheck
import com.looker.droidify.utility.common.cache.Cache
import com.looker.droidify.utility.common.extension.size
import kotlinx.coroutines.suspendCancellableCoroutine
import java.io.BufferedReader
import java.io.InputStream
import kotlin.coroutines.resume
import dev.re7gog.shizuku_apk_installer.ShizukuWorker
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

class ShizukuInstaller(private val context: Context) : Installer {
class ShizukuWorkerAdapter(private val context: Context) {

private val worker = ShizukuWorker(context.applicationContext)

suspend fun checkPermission(): String = withContext(Dispatchers.IO) {
worker.checkPermission()
}

suspend fun installApkFile(file: File, installerPackageName: String = ""): Int {
val contentUri = file.toContentUri(context)
return withContext(Dispatchers.IO) {
worker.installAPKs(listOf(contentUri.toString()), installerPackageName)
}
}

fun exit() = worker.exit()

companion object {
private val SESSION_ID_REGEX = Regex("(?<=\\[).+?(?=])")
private fun File.toContentUri(context: Context): Uri {
return androidx.core.content.FileProvider.getUriForFile(
context,
"${context.packageName}.fileprovider",
this
)
}
}
}

override suspend fun install(
installItem: InstallItem,
): InstallState = suspendCancellableCoroutine { cont ->
var sessionId: String? = null
val file = Cache.getReleaseFile(context, installItem.installFileName)
try {
val fileSize = file.length()
if (fileSize == 0L) {
cont.cancel()
error("File is not valid: Size ${file.size}")
}
if (cont.isCompleted) return@suspendCancellableCoroutine
val installerPackage = context.packageName
file.inputStream().use {
val createCommand =
if (SdkCheck.isNougat) {
"pm install-create --user current -i $installerPackage -S $fileSize"
} else {
"pm install-create -i $installerPackage -S $fileSize"
}
val createResult = exec(createCommand)
sessionId = SESSION_ID_REGEX.find(createResult.out)?.value
?: run {
cont.cancel()
error("Failed to create install session")
}
if (cont.isCompleted) return@suspendCancellableCoroutine
class ShizukuInstaller(private val context: Context) : Installer {
private val appContext = context.applicationContext
private val adapter = ShizukuWorkerAdapter(appContext)

val writeResult = exec("pm install-write -S $fileSize $sessionId base -", it)
if (writeResult.resultCode != 0) {
cont.cancel()
error("Failed to write APK to session $sessionId")
override suspend fun install(installItem: InstallItem): InstallState {
return withContext(Dispatchers.IO) {
try {
val permissionStatus = adapter.checkPermission()
if (!permissionStatus.startsWith("granted")) {
return@withContext InstallState.Failed
}
if (cont.isCompleted) return@suspendCancellableCoroutine

val commitResult = exec("pm install-commit $sessionId")
if (commitResult.resultCode != 0) {
cont.cancel()
error("Failed to commit install session $sessionId")
val file = Cache.getReleaseFile(appContext, installItem.installFileName)
if (!file.exists() || file.length() == 0L) {
return@withContext InstallState.Failed
}
if (cont.isCompleted) return@suspendCancellableCoroutine
cont.resume(InstallState.Installed)

val result = adapter.installApkFile(
file = file,
installerPackageName = appContext.packageName
)

when (result) {
PackageInstaller.STATUS_SUCCESS -> InstallState.Installed
else -> InstallState.Failed
}
} catch (e: Exception) {
InstallState.Failed
}
} catch (_: Exception) {
if (sessionId != null) exec("pm install-abandon $sessionId")
cont.resume(InstallState.Failed)
}
}

override suspend fun uninstall(packageName: PackageName) =
context.uninstallPackage(packageName)

override fun close() = Unit

private data class ShellResult(val resultCode: Int, val out: String)

private fun exec(command: String, stdin: InputStream? = null): ShellResult {
val process = rikka.shizuku.Shizuku.newProcess(arrayOf("sh", "-c", command), null, null)
if (stdin != null) {
process.outputStream.use { stdin.copyTo(it) }
}
val output = process.inputStream.bufferedReader().use(BufferedReader::readText)
val resultCode = process.waitFor()
return ShellResult(resultCode, output)
override fun close() {
adapter.exit()
}
}
Loading