Skip to content

Commit

Permalink
Update private extensions privately, even if default installer is used
Browse files Browse the repository at this point in the history
But Shizuku can continue to install regularly, because why not
  • Loading branch information
Jays2Kings committed Oct 13, 2023
1 parent 35949a5 commit 5044a75
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,37 +271,13 @@ internal class ExtensionInstaller(private val context: Context) {
setInstalling(pkgName, uri.hashCode())
shizukuInstaller?.addToQueue(downloadId, pkgName, uri)
}
PRIVATE -> {
PRIVATE -> installPrivately(downloadId, pkgName, uri)
else -> {
val extensionManager = Injekt.get<ExtensionManager>()
val tempFile = File(context.cacheDir, "temp_$downloadId")

pkgName ?: return
if (tempFile.exists() && !tempFile.delete()) {
// Unlikely but just in case
setInstallationResult(pkgName, false)
if (extensionManager.installedExtensionsFlow.value.find { it.pkgName == pkgName }?.isShared == false) {
installPrivately(downloadId, pkgName, uri)
return
}

try {
context.contentResolver.openInputStream(uri)?.use { input ->
tempFile.outputStream().use { output ->
input.copyTo(output)
}
}

if (ExtensionLoader.installPrivateExtensionFile(context, tempFile)) {
setInstallationResult(pkgName, true)
} else {
setInstallationResult(pkgName, false)
}
} catch (e: Exception) {
Timber.e(e, "Failed to read downloaded extension file.")
setInstallationResult(pkgName, false)
}

tempFile.delete()
}
else -> {
val intent =
if (useActivity) {
Intent(context, ExtensionInstallActivity::class.java)
Expand All @@ -320,6 +296,36 @@ internal class ExtensionInstaller(private val context: Context) {
}
}

private fun installPrivately(downloadId: Long, pkgName: String?, uri: Uri) {
val tempFile = File(context.cacheDir, "temp_$downloadId")

pkgName ?: return
if (tempFile.exists() && !tempFile.delete()) {
// Unlikely but just in case
setInstallationResult(pkgName, false)
return
}

try {
context.contentResolver.openInputStream(uri)?.use { input ->
tempFile.outputStream().use { output ->
input.copyTo(output)
}
}

if (ExtensionLoader.installPrivateExtensionFile(context, tempFile)) {
setInstallationResult(pkgName, true)
} else {
setInstallationResult(pkgName, false)
}
} catch (e: Exception) {
Timber.e(e, "Failed to read downloaded extension file.")
setInstallationResult(pkgName, false)
}

tempFile.delete()
}

/**
* Starts an intent to uninstall the extension by the given package name.
*
Expand Down

0 comments on commit 5044a75

Please sign in to comment.