mirror of
https://github.com/Aviortheking/Neo-Store.git
synced 2025-04-23 19:32:16 +00:00
Optimize Installer
This commit is contained in:
parent
b3a98ee093
commit
96d8c58a0a
@ -13,6 +13,18 @@ import java.io.File
|
|||||||
class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
||||||
|
|
||||||
private val sessionInstaller = context.packageManager.packageInstaller
|
private val sessionInstaller = context.packageManager.packageInstaller
|
||||||
|
private val intent = Intent(context, InstallerService::class.java)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val flags = if (Android.sdk(31)) PendingIntent.FLAG_MUTABLE else 0
|
||||||
|
val sessionParams = SessionParams(SessionParams.MODE_FULL_INSTALL)
|
||||||
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (Android.sdk(31)) {
|
||||||
|
sessionParams.setRequireUserAction(SessionParams.USER_ACTION_NOT_REQUIRED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun install(cacheFileName: String) {
|
override suspend fun install(cacheFileName: String) {
|
||||||
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
|
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
|
||||||
@ -30,11 +42,6 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
|||||||
override suspend fun uninstall(packageName: String) = mDefaultUninstaller(packageName)
|
override suspend fun uninstall(packageName: String) = mDefaultUninstaller(packageName)
|
||||||
|
|
||||||
private fun mDefaultInstaller(cacheFile: File) {
|
private fun mDefaultInstaller(cacheFile: File) {
|
||||||
val sessionParams = SessionParams(SessionParams.MODE_FULL_INSTALL)
|
|
||||||
|
|
||||||
if (Android.sdk(31)) {
|
|
||||||
sessionParams.setRequireUserAction(SessionParams.USER_ACTION_NOT_REQUIRED)
|
|
||||||
}
|
|
||||||
|
|
||||||
val id = sessionInstaller.createSession(sessionParams)
|
val id = sessionInstaller.createSession(sessionParams)
|
||||||
|
|
||||||
@ -47,10 +54,6 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val intent = Intent(context, InstallerService::class.java)
|
|
||||||
|
|
||||||
val flags = if (Android.sdk(31)) PendingIntent.FLAG_MUTABLE else 0
|
|
||||||
|
|
||||||
val pendingIntent = PendingIntent.getService(context, id, intent, flags)
|
val pendingIntent = PendingIntent.getService(context, id, intent, flags)
|
||||||
|
|
||||||
session.commit(pendingIntent.intentSender)
|
session.commit(pendingIntent.intentSender)
|
||||||
@ -59,11 +62,8 @@ class DefaultInstaller(context: Context) : BaseInstaller(context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun mDefaultUninstaller(packageName: String) {
|
private suspend fun mDefaultUninstaller(packageName: String) {
|
||||||
val intent = Intent(context, InstallerService::class.java)
|
|
||||||
intent.putExtra(InstallerService.KEY_ACTION, InstallerService.ACTION_UNINSTALL)
|
intent.putExtra(InstallerService.KEY_ACTION, InstallerService.ACTION_UNINSTALL)
|
||||||
|
|
||||||
val flags = if (Android.sdk(31)) PendingIntent.FLAG_MUTABLE else 0
|
|
||||||
|
|
||||||
val pendingIntent = PendingIntent.getService(context, -1, intent, flags)
|
val pendingIntent = PendingIntent.getService(context, -1, intent, flags)
|
||||||
|
|
||||||
withContext(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
|
@ -8,6 +8,51 @@ import kotlinx.coroutines.withContext
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class RootInstaller(context: Context) : BaseInstaller(context) {
|
class RootInstaller(context: Context) : BaseInstaller(context) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val getCurrentUserState: String =
|
||||||
|
Shell.su("dumpsys activity | grep -E \"mUserLru\"")
|
||||||
|
.exec().out[0].trim()
|
||||||
|
.removePrefix("mUserLru: [").removeSuffix("]")
|
||||||
|
|
||||||
|
private val String.quote
|
||||||
|
get() = "\"${this.replace(Regex("""[\\$"`]""")) { c -> "\\${c.value}" }}\""
|
||||||
|
|
||||||
|
private val getUtilBoxPath: String
|
||||||
|
get() {
|
||||||
|
listOf("toybox", "busybox").forEach {
|
||||||
|
val shellResult = Shell.su("which $it").exec()
|
||||||
|
if (shellResult.out.isNotEmpty()) {
|
||||||
|
val utilBoxPath = shellResult.out.joinToString("")
|
||||||
|
if (utilBoxPath.isNotEmpty()) return utilBoxPath.quote
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
val File.install
|
||||||
|
get() = String.format(
|
||||||
|
ROOT_INSTALL_PACKAGE,
|
||||||
|
absolutePath,
|
||||||
|
getCurrentUserState,
|
||||||
|
length()
|
||||||
|
)
|
||||||
|
|
||||||
|
val String.uninstall
|
||||||
|
get() = String.format(
|
||||||
|
ROOT_UNINSTALL_PACKAGE,
|
||||||
|
getCurrentUserState,
|
||||||
|
this
|
||||||
|
)
|
||||||
|
|
||||||
|
val File.deletePackage
|
||||||
|
get() = String.format(
|
||||||
|
DELETE_PACKAGE,
|
||||||
|
getUtilBoxPath,
|
||||||
|
absolutePath.quote
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun install(cacheFileName: String) {
|
override suspend fun install(cacheFileName: String) {
|
||||||
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
|
val cacheFile = Cache.getReleaseFile(context, cacheFileName)
|
||||||
mRootInstaller(cacheFile)
|
mRootInstaller(cacheFile)
|
||||||
@ -23,47 +68,15 @@ class RootInstaller(context: Context) : BaseInstaller(context) {
|
|||||||
override suspend fun uninstall(packageName: String) = mRootUninstaller(packageName)
|
override suspend fun uninstall(packageName: String) = mRootUninstaller(packageName)
|
||||||
|
|
||||||
private suspend fun mRootInstaller(cacheFile: File) {
|
private suspend fun mRootInstaller(cacheFile: File) {
|
||||||
val installCommand =
|
|
||||||
String.format(
|
|
||||||
ROOT_INSTALL_PACKAGE,
|
|
||||||
cacheFile.absolutePath,
|
|
||||||
getCurrentUserState,
|
|
||||||
cacheFile.length()
|
|
||||||
)
|
|
||||||
val deleteCommand =
|
|
||||||
String.format(
|
|
||||||
DELETE_PACKAGE,
|
|
||||||
getUtilBoxPath,
|
|
||||||
cacheFile.absolutePath.quote
|
|
||||||
)
|
|
||||||
withContext(Dispatchers.Default) {
|
withContext(Dispatchers.Default) {
|
||||||
Shell.su(installCommand).submit { if (it.isSuccess) Shell.su(deleteCommand).submit() }
|
Shell.su(cacheFile.install)
|
||||||
|
.submit { if (it.isSuccess) Shell.su(cacheFile.deletePackage).submit() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun mRootUninstaller(packageName: String) {
|
private suspend fun mRootUninstaller(packageName: String) {
|
||||||
val uninstallCommand =
|
withContext(Dispatchers.Default) {
|
||||||
String.format(ROOT_UNINSTALL_PACKAGE, getCurrentUserState, packageName)
|
Shell.su(packageName.uninstall).submit()
|
||||||
withContext(Dispatchers.Default) { Shell.su(uninstallCommand).submit() }
|
|
||||||
}
|
|
||||||
|
|
||||||
private val getCurrentUserState: String =
|
|
||||||
Shell.su("dumpsys activity | grep -E \"mUserLru\"")
|
|
||||||
.exec().out[0].trim()
|
|
||||||
.removePrefix("mUserLru: [").removeSuffix("]")
|
|
||||||
|
|
||||||
private val String.quote
|
|
||||||
get() = "\"${this.replace(Regex("""[\\$"`]""")) { c -> "\\${c.value}" }}\""
|
|
||||||
|
|
||||||
private val getUtilBoxPath: String
|
|
||||||
get() {
|
|
||||||
listOf("toybox", "busybox").forEach {
|
|
||||||
val shellResult = Shell.su("which $it").exec()
|
|
||||||
if (shellResult.out.isNotEmpty()) {
|
|
||||||
val utilBoxPath = shellResult.out.joinToString("")
|
|
||||||
if (utilBoxPath.isNotEmpty()) return utilBoxPath.quote
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user