Fix shizuku installer not updating installed extensions (mihonapp/mihon#2697)

Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>
Co-authored-by: Cuong-Tran <16017808+cuong-tran@users.noreply.github.com>
(cherry picked from commit c3d858a5613a133352f6b140916d05de7f752771)
This commit is contained in:
NGB-Was-Taken 2025-11-23 14:01:38 +05:45 committed by Cuong-Tran
parent 9ade58cc95
commit b7cdc46c0e

View file

@ -50,6 +50,7 @@ import android.os.Build
import android.os.IBinder
import android.os.ParcelFileDescriptor
import android.os.UserHandle
import android.util.Log
import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.extension.installer.ACTION_INSTALL_RESULT
import rikka.shizuku.SystemServiceHelper
@ -75,6 +76,16 @@ class ShellInterface : IShellInterface.Stub() {
.invoke(pmInterface)
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL).apply {
try {
val installFlags = this::class.java.getField("installFlags")
installFlags.set(
this,
installFlags.getInt(this) or REPLACE_EXISTING_INSTALL_FLAG,
)
} catch (e: ReflectiveOperationException) {
Log.w("ShellInterface", "Unable to set installFlags via reflection; continuing without REPLACE_EXISTING_INSTALL_FLAG", e)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
setPackageSource(PackageInstaller.PACKAGE_SOURCE_STORE)
}
@ -105,37 +116,38 @@ class ShellInterface : IShellInterface.Stub() {
.getMethod("openSession", Int::class.java)
.invoke(packageInstaller, sessionId)
(
session::class.java.getMethod(
"openWrite",
String::class.java,
Long::class.java,
Long::class.java,
).invoke(session, "extension", 0L, apk.length) as ParcelFileDescriptor
).let { fd ->
val outputStream = try {
val revocable = Class.forName("android.os.SystemProperties")
.getMethod("getBoolean", String::class.java, Boolean::class.java)
.invoke(null, "fw.revocable_fd", false) as Boolean
session::class.java.getMethod(
"openWrite",
String::class.java,
Long::class.java,
Long::class.java,
)
.invoke(session, "extension", 0L, apk.length)
.let { it as ParcelFileDescriptor }
.let { fd ->
val outputStream = try {
val revocable = Class.forName("android.os.SystemProperties")
.getMethod("getBoolean", String::class.java, Boolean::class.java)
.invoke(null, "fw.revocable_fd", false) as Boolean
if (revocable) {
ParcelFileDescriptor.AutoCloseOutputStream(fd)
} else {
Class.forName($$"android.os.FileBridge$FileBridgeOutputStream")
.getConstructor(ParcelFileDescriptor::class.java)
.newInstance(fd) as OutputStream
if (revocable) {
ParcelFileDescriptor.AutoCloseOutputStream(fd)
} else {
Class.forName($$"android.os.FileBridge$FileBridgeOutputStream")
.getConstructor(ParcelFileDescriptor::class.java)
.newInstance(fd) as OutputStream
}
// KMK -->
} catch (e: Exception) {
fd.close()
throw e
// KMK <--
}
// KMK -->
} catch (e: Exception) {
fd.close()
throw e
// KMK <--
}
outputStream.use { output ->
apk.createInputStream().use { input -> input.copyTo(output) }
outputStream.use { output ->
apk.createInputStream().use { input -> input.copyTo(output) }
}
}
}
val statusIntent = PendingIntent.getBroadcast(
context,
@ -182,3 +194,7 @@ class ShellInterface : IShellInterface.Stub() {
return shellContext.createPackageContext("com.android.shell", 0)
}
}
// Constant hidden from the SDK
// https://cs.android.com/android/platform/superproject/main/+/512046e84bcc51cc241bc6599f83ab345e93ab12:frameworks/base/core/java/android/content/pm/PackageManager.java;l=1682-1689
private const val REPLACE_EXISTING_INSTALL_FLAG = 0x00000002