0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-25 12:45:12 +00:00

packagemanager code cleanup

This commit is contained in:
X1nto 2022-03-11 19:53:34 +04:00
parent a1662bab5a
commit 9b21751957
4 changed files with 128 additions and 93 deletions

View file

@ -4,7 +4,7 @@ import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFile
import com.topjohnwu.superuser.io.SuFileOutputStream
import com.vanced.manager.repository.manager.PackageManagerResult
import com.vanced.manager.repository.manager.PackageManagerStatus
import com.vanced.manager.repository.manager.PackageManagerError
import com.vanced.manager.repository.manager.getEnumForInstallFailed
import com.vanced.manager.util.errString
import com.vanced.manager.util.outString
@ -17,7 +17,7 @@ object PMRoot {
val apk = File(apkPath)
val tmpApk = copyApkToTemp(apk).getOrElse { exception ->
return PackageManagerResult.Error(
PackageManagerStatus.SESSION_FAILED_COPY,
PackageManagerError.SESSION_FAILED_COPY,
exception.stackTraceToString()
)
}
@ -38,18 +38,18 @@ object PMRoot {
val installCreate = Shell.su("pm", "install-create", "-r").exec()
if (!installCreate.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.SESSION_FAILED_CREATE, installCreate.errString)
return PackageManagerResult.Error(PackageManagerError.SESSION_FAILED_CREATE, installCreate.errString)
val sessionId = installCreate.outString
if (sessionId.toIntOrNull() == null)
return PackageManagerResult.Error(PackageManagerStatus.SESSION_INVALID_ID, installCreate.errString)
return PackageManagerResult.Error(PackageManagerError.SESSION_INVALID_ID, installCreate.errString)
for (apkPath in apkPaths) {
val apk = File(apkPath)
val tmpApk = copyApkToTemp(apk).getOrElse { exception ->
return PackageManagerResult.Error(
PackageManagerStatus.SESSION_FAILED_COPY,
PackageManagerError.SESSION_FAILED_COPY,
exception.stackTraceToString()
)
}
@ -61,7 +61,7 @@ object PMRoot {
tmpApk.delete()
if (!installWrite.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.SESSION_FAILED_WRITE, installWrite.errString)
return PackageManagerResult.Error(PackageManagerError.SESSION_FAILED_WRITE, installWrite.errString)
}
val installCommit = Shell.su("pm", "install-commit", sessionId).exec()
@ -78,7 +78,7 @@ object PMRoot {
val uninstall = Shell.su("pm", "uninstall", pkg).exec()
if (!uninstall.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.UNINSTALL_FAILED, uninstall.errString)
return PackageManagerResult.Error(PackageManagerError.UNINSTALL_FAILED, uninstall.errString)
return PackageManagerResult.Success(null)
}
@ -89,7 +89,7 @@ object PMRoot {
if (!setInstaller.isSuccess)
return PackageManagerResult.Error(
PackageManagerStatus.SET_FAILED_INSTALLER,
PackageManagerError.SET_FAILED_INSTALLER,
setInstaller.errString
)
@ -100,7 +100,7 @@ object PMRoot {
val stopApp = Shell.su("am", "force-stop", pkg).exec()
if (!stopApp.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.APP_FAILED_FORCE_STOP, stopApp.errString)
return PackageManagerResult.Error(PackageManagerError.APP_FAILED_FORCE_STOP, stopApp.errString)
return PackageManagerResult.Success(null)
}
@ -111,7 +111,7 @@ object PMRoot {
if (!dumpsys.isSuccess)
return PackageManagerResult.Error(
PackageManagerStatus.GET_FAILED_PACKAGE_VERSION_NAME,
PackageManagerError.GET_FAILED_PACKAGE_VERSION_NAME,
dumpsys.errString
)
@ -124,7 +124,7 @@ object PMRoot {
if (!dumpsys.isSuccess)
return PackageManagerResult.Error(
PackageManagerStatus.GET_FAILED_PACKAGE_VERSION_CODE,
PackageManagerError.GET_FAILED_PACKAGE_VERSION_CODE,
dumpsys.errString
)
@ -139,7 +139,7 @@ object PMRoot {
val dumpsys = Shell.su("dumpsys", "package", pkg, "|", "grep", keyword).exec()
if (!dumpsys.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.GET_FAILED_PACKAGE_DIR, dumpsys.errString)
return PackageManagerResult.Error(PackageManagerError.GET_FAILED_PACKAGE_DIR, dumpsys.errString)
return PackageManagerResult.Success(dumpsys.outString.removePrefix(keyword))
}

View file

@ -6,7 +6,7 @@ import com.topjohnwu.superuser.io.SuFileOutputStream
import com.vanced.manager.io.ManagerSuFile
import com.vanced.manager.io.SUIOException
import com.vanced.manager.repository.manager.PackageManagerResult
import com.vanced.manager.repository.manager.PackageManagerStatus
import com.vanced.manager.repository.manager.PackageManagerError
import com.vanced.manager.util.errString
import java.io.File
import java.io.IOException
@ -27,14 +27,14 @@ object Patcher {
val copyServiceDScript = copyScriptToDestination(postFsDataScript, postFsDataScriptPath)
if (copyServiceDScript.isFailure)
return PackageManagerResult.Error(
PackageManagerStatus.SCRIPT_FAILED_SETUP_POST_FS,
PackageManagerError.SCRIPT_FAILED_SETUP_POST_FS,
copyServiceDScript.exceptionOrNull()!!.stackTraceToString()
)
val copyPostFsDataScript = copyScriptToDestination(serviceDScript, serviceDScriptPath)
if (copyPostFsDataScript.isFailure)
return PackageManagerResult.Error(
PackageManagerStatus.SCRIPT_FAILED_SETUP_SERVICE_D,
PackageManagerError.SCRIPT_FAILED_SETUP_SERVICE_D,
copyPostFsDataScript.exceptionOrNull()!!.stackTraceToString()
)
@ -55,16 +55,16 @@ object Patcher {
try {
patchApk.copyTo(newPatchApk)
} catch (e: IOException) {
return PackageManagerResult.Error(PackageManagerStatus.PATCH_FAILED_COPY, e.stackTraceToString())
return PackageManagerResult.Error(PackageManagerError.PATCH_FAILED_COPY, e.stackTraceToString())
}
val chmod = Shell.su("chmod", "644", newPatchPath).exec()
if (!chmod.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.PATCH_FAILED_CHMOD, chmod.errString)
return PackageManagerResult.Error(PackageManagerError.PATCH_FAILED_CHMOD, chmod.errString)
val chown = Shell.su("chown", "system:system", newPatchPath).exec()
if (!chmod.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.PATCH_FAILED_CHOWN, chown.errString)
return PackageManagerResult.Error(PackageManagerError.PATCH_FAILED_CHOWN, chown.errString)
return PackageManagerResult.Success(null)
}
@ -72,7 +72,7 @@ object Patcher {
fun chconPatch(app: String): PackageManagerResult<Nothing> {
val chcon = Shell.su("chcon u:object_r:apk_data_file:s0 ${getAppPatchPath(app)}").exec()
if (!chcon.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.PATCH_FAILED_CHCON, chcon.errString)
return PackageManagerResult.Error(PackageManagerError.PATCH_FAILED_CHCON, chcon.errString)
return PackageManagerResult.Success(null)
}
@ -82,13 +82,13 @@ object Patcher {
Shell.su("""for i in ${'$'}(ls /data/app/ | grep $stockPackage | tr " "); do umount -l "/data/app/${"$"}i/base.apk"; done """)
.exec()
if (!umount.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.LINK_FAILED_UNMOUNT, umount.errString)
return PackageManagerResult.Error(PackageManagerError.LINK_FAILED_UNMOUNT, umount.errString)
val mount =
Shell.su("su", "-mm", "-c", """"mount -o bind ${getAppPatchPath(app)} $stockPath"""")
.exec()
if (!mount.isSuccess)
return PackageManagerResult.Error(PackageManagerStatus.LINK_FAILED_MOUNT, mount.errString)
return PackageManagerResult.Error(PackageManagerError.LINK_FAILED_MOUNT, mount.errString)
return PackageManagerResult.Success(null)
}
@ -136,9 +136,9 @@ private fun cleanPatchFiles(
patchPath: String,
): PackageManagerResult<Nothing> {
val files = mapOf(
postFsPath to PackageManagerStatus.SCRIPT_FAILED_DESTROY_POST_FS,
serviceDPath to PackageManagerStatus.SCRIPT_FAILED_DESTROY_SERVICE_D,
patchPath to PackageManagerStatus.PATCH_FAILED_DESTROY,
postFsPath to PackageManagerError.SCRIPT_FAILED_DESTROY_POST_FS,
serviceDPath to PackageManagerError.SCRIPT_FAILED_DESTROY_SERVICE_D,
patchPath to PackageManagerError.PATCH_FAILED_DESTROY,
)
for ((filePath, errorStatusType) in files) {

View file

@ -12,8 +12,7 @@ import com.topjohnwu.superuser.io.SuFileInputStream
import com.topjohnwu.superuser.io.SuFileOutputStream
import com.vanced.manager.installer.service.AppInstallService
import com.vanced.manager.installer.service.AppUninstallService
import com.vanced.manager.util.SuException
import com.vanced.manager.util.awaitOutputOrThrow
import com.vanced.manager.util.*
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
@ -53,10 +52,11 @@ class NonrootPackageManager(
} else {
packageInfo.versionCode
}
PackageManagerResult.Success(versionCode)
} catch (e: android.content.pm.PackageManager.NameNotFoundException) {
PackageManagerResult.Error(
status = PackageManagerStatus.GET_FAILED_PACKAGE_VERSION_CODE,
error = PackageManagerError.GET_FAILED_PACKAGE_VERSION_CODE,
message = e.stackTraceToString()
)
}
@ -68,10 +68,11 @@ class NonrootPackageManager(
val versionName = context.packageManager
.getPackageInfo(packageName, FLAG_NOTHING)
.versionName
PackageManagerResult.Success(versionName)
} catch (e: android.content.pm.PackageManager.NameNotFoundException) {
PackageManagerResult.Error(
status = PackageManagerStatus.GET_FAILED_PACKAGE_VERSION_NAME,
error = PackageManagerError.GET_FAILED_PACKAGE_VERSION_NAME,
message = e.stackTraceToString()
)
}
@ -84,10 +85,11 @@ class NonrootPackageManager(
.getPackageInfo(packageName, FLAG_NOTHING)
.applicationInfo
.sourceDir
PackageManagerResult.Success(installationDir)
} catch (e: android.content.pm.PackageManager.NameNotFoundException) {
PackageManagerResult.Error(
status = PackageManagerStatus.GET_FAILED_PACKAGE_DIR,
error = PackageManagerError.GET_FAILED_PACKAGE_DIR,
message = e.stackTraceToString()
)
}
@ -98,14 +100,14 @@ class NonrootPackageManager(
installerPackage: String
): PackageManagerResult<Nothing> {
return PackageManagerResult.Error(
status = PackageManagerStatus.SET_FAILED_INSTALLER,
error = PackageManagerError.SET_FAILED_INSTALLER,
message = "Unsupported"
)
}
override suspend fun forceStop(packageName: String): PackageManagerResult<Nothing> {
return PackageManagerResult.Error(
status = PackageManagerStatus.APP_FAILED_FORCE_STOP,
error = PackageManagerError.APP_FAILED_FORCE_STOP,
message = "Unsupported"
)
}
@ -154,30 +156,27 @@ class NonrootPackageManager(
intentFlags
).intentSender
val sessionId: Int
try {
sessionId = packageInstaller.createSession(sessionParams)
} catch (e: IOException) {
val sessionId = tripleUnionTryCatch<IOException, SecurityException, IllegalArgumentException, Int>(
onCatch = {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_CREATE,
message = e.stackTraceToString()
error = PackageManagerError.SESSION_FAILED_CREATE,
message = it.stackTraceToString()
)
}
) {
packageInstaller.createSession(sessionParams)
}
val session: PackageInstaller.Session
try {
session = packageInstaller.openSession(sessionId)
} catch (e: IOException) {
val session = doubleUnionTryCatch<IOException, SecurityException, PackageInstaller.Session>(
onCatch = {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_OPEN,
message = e.stackTraceToString()
)
} catch (e: SecurityException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_OPEN,
message = e.stackTraceToString()
error = PackageManagerError.SESSION_FAILED_CREATE,
message = it.stackTraceToString()
)
}
) {
packageInstaller.openSession(sessionId)
}
try {
session.use {
@ -186,12 +185,12 @@ class NonrootPackageManager(
}
} catch (e: IOException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_WRITE,
error = PackageManagerError.SESSION_FAILED_WRITE,
message = e.stackTraceToString()
)
} catch (e: SecurityException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_COMMIT,
error = PackageManagerError.SESSION_FAILED_COMMIT,
message = e.stackTraceToString()
)
}
@ -236,12 +235,12 @@ class RootPackageManager : PackageManager {
PackageManagerResult.Success(versionCode)
} catch (e: SuException) {
PackageManagerResult.Error(
status = PackageManagerStatus.GET_FAILED_PACKAGE_VERSION_CODE,
error = PackageManagerError.GET_FAILED_PACKAGE_VERSION_CODE,
message = e.stderrOut
)
} catch (e: java.lang.NumberFormatException) {
PackageManagerResult.Error(
status = PackageManagerStatus.GET_FAILED_PACKAGE_VERSION_CODE,
error = PackageManagerError.GET_FAILED_PACKAGE_VERSION_CODE,
message = e.stackTraceToString()
)
}
@ -256,7 +255,7 @@ class RootPackageManager : PackageManager {
PackageManagerResult.Success(versionName)
} catch (e: SuException) {
PackageManagerResult.Error(
status = PackageManagerStatus.GET_FAILED_PACKAGE_VERSION_NAME,
error = PackageManagerError.GET_FAILED_PACKAGE_VERSION_NAME,
message = e.stderrOut
)
}
@ -271,7 +270,7 @@ class RootPackageManager : PackageManager {
PackageManagerResult.Success(installationDir)
} catch (e: SuException) {
PackageManagerResult.Error(
status = PackageManagerStatus.GET_FAILED_PACKAGE_DIR,
error = PackageManagerError.GET_FAILED_PACKAGE_DIR,
message = e.stderrOut
)
}
@ -281,25 +280,26 @@ class RootPackageManager : PackageManager {
targetPackage: String,
installerPackage: String
): PackageManagerResult<Nothing> {
try {
return try {
Shell.su("pm", "set-installer", targetPackage, installerPackage).awaitOutputOrThrow()
PackageManagerResult.Success(null)
} catch (e: SuException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SET_FAILED_INSTALLER,
PackageManagerResult.Error(
error = PackageManagerError.SET_FAILED_INSTALLER,
message = e.stderrOut
)
}
return PackageManagerResult.Success(null)
}
override suspend fun forceStop(packageName: String): PackageManagerResult<Nothing> {
return try {
Shell.su("am", "force-stop", packageName).awaitOutputOrThrow()
PackageManagerResult.Success(null)
} catch (e: SuException) {
PackageManagerResult.Error(
status = PackageManagerStatus.APP_FAILED_FORCE_STOP,
error = PackageManagerError.APP_FAILED_FORCE_STOP,
message = e.stderrOut
)
}
@ -307,38 +307,39 @@ class RootPackageManager : PackageManager {
override suspend fun installApp(apk: File): PackageManagerResult<Nothing> {
var tempApk: File? = null
try {
return try {
tempApk = copyApkToTemp(apk)
Shell.su("pm", "install", "-r", tempApk.absolutePath).awaitOutputOrThrow()
PackageManagerResult.Success(null)
} catch (e: IOException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_COPY,
PackageManagerResult.Error(
error = PackageManagerError.SESSION_FAILED_COPY,
message = e.stackTraceToString()
)
} catch (e: SuException) {
return PackageManagerResult.Error(
status = getEnumForInstallFailed(e.stderrOut),
PackageManagerResult.Error(
error = getEnumForInstallFailed(e.stderrOut),
message = e.stderrOut
)
} finally {
tempApk?.delete()
}
return PackageManagerResult.Success(null)
}
override suspend fun installSplitApp(apks: Array<File>): PackageManagerResult<Nothing> {
val sessionId: Int
try {
val sessionId = try {
val installCreate = Shell.su("pm", "install-create", "-r").awaitOutputOrThrow()
sessionId = installCreate.toInt()
installCreate.toInt()
} catch (e: SuException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_CREATE,
error = PackageManagerError.SESSION_FAILED_CREATE,
message = e.stderrOut
)
} catch (e: NumberFormatException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_INVALID_ID,
error = PackageManagerError.SESSION_INVALID_ID,
message = e.stackTraceToString()
)
}
@ -350,12 +351,12 @@ class RootPackageManager : PackageManager {
Shell.su("pm", "install-write", sessionId.toString(), tempApk.name, tempApk.absolutePath).awaitOutputOrThrow()
} catch (e: SuException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_WRITE,
error = PackageManagerError.SESSION_FAILED_WRITE,
message = e.stderrOut
)
} catch (e: IOException) {
return PackageManagerResult.Error(
status = PackageManagerStatus.SESSION_FAILED_COPY,
error = PackageManagerError.SESSION_FAILED_COPY,
message = e.stackTraceToString()
)
} finally {
@ -363,24 +364,26 @@ class RootPackageManager : PackageManager {
}
}
try {
return try {
Shell.su("pm", "install-commit", sessionId.toString()).awaitOutputOrThrow()
PackageManagerResult.Success(null)
} catch (e: SuException) {
return PackageManagerResult.Error(
status = getEnumForInstallFailed(e.stderrOut),
PackageManagerResult.Error(
error = getEnumForInstallFailed(e.stderrOut),
message = e.stderrOut
)
}
return PackageManagerResult.Success(null)
}
override suspend fun uninstallApp(packageName: String): PackageManagerResult<Nothing> {
return try {
Shell.su("pm", "uninstall", packageName).awaitOutputOrThrow()
PackageManagerResult.Success(null)
} catch (e: SuException) {
PackageManagerResult.Error(
status = PackageManagerStatus.UNINSTALL_FAILED,
error = PackageManagerError.UNINSTALL_FAILED,
message = e.stderrOut
)
}
@ -409,7 +412,7 @@ class RootPackageManager : PackageManager {
}
enum class PackageManagerStatus {
enum class PackageManagerError {
SET_FAILED_INSTALLER,
GET_FAILED_PACKAGE_DIR,
GET_FAILED_PACKAGE_VERSION_NAME,
@ -450,22 +453,22 @@ enum class PackageManagerStatus {
SCRIPT_FAILED_DESTROY_SERVICE_D,
}
fun getEnumForInstallFailed(outString: String): PackageManagerStatus {
fun getEnumForInstallFailed(outString: String): PackageManagerError {
return when {
outString.contains("INSTALL_FAILED_ABORTED") -> PackageManagerStatus.INSTALL_FAILED_ABORTED
outString.contains("INSTALL_FAILED_ALREADY_EXISTS") -> PackageManagerStatus.INSTALL_FAILED_ALREADY_EXISTS
outString.contains("INSTALL_FAILED_CPU_ABI_INCOMPATIBLE") -> PackageManagerStatus.INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
outString.contains("INSTALL_FAILED_INSUFFICIENT_STORAGE") -> PackageManagerStatus.INSTALL_FAILED_INSUFFICIENT_STORAGE
outString.contains("INSTALL_FAILED_INVALID_APK") -> PackageManagerStatus.INSTALL_FAILED_INVALID_APK
outString.contains("INSTALL_FAILED_VERSION_DOWNGRADE") -> PackageManagerStatus.INSTALL_FAILED_VERSION_DOWNGRADE
outString.contains("INSTALL_PARSE_FAILED_NO_CERTIFICATES") -> PackageManagerStatus.INSTALL_FAILED_PARSE_NO_CERTIFICATES
else -> PackageManagerStatus.INSTALL_FAILED_UNKNOWN
outString.contains("INSTALL_FAILED_ABORTED") -> PackageManagerError.INSTALL_FAILED_ABORTED
outString.contains("INSTALL_FAILED_ALREADY_EXISTS") -> PackageManagerError.INSTALL_FAILED_ALREADY_EXISTS
outString.contains("INSTALL_FAILED_CPU_ABI_INCOMPATIBLE") -> PackageManagerError.INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
outString.contains("INSTALL_FAILED_INSUFFICIENT_STORAGE") -> PackageManagerError.INSTALL_FAILED_INSUFFICIENT_STORAGE
outString.contains("INSTALL_FAILED_INVALID_APK") -> PackageManagerError.INSTALL_FAILED_INVALID_APK
outString.contains("INSTALL_FAILED_VERSION_DOWNGRADE") -> PackageManagerError.INSTALL_FAILED_VERSION_DOWNGRADE
outString.contains("INSTALL_PARSE_FAILED_NO_CERTIFICATES") -> PackageManagerError.INSTALL_FAILED_PARSE_NO_CERTIFICATES
else -> PackageManagerError.INSTALL_FAILED_UNKNOWN
}
}
sealed class PackageManagerResult<out V> {
data class Success<out V>(val value: V?) : PackageManagerResult<V>()
data class Error(val status: PackageManagerStatus, val message: String) : PackageManagerResult<Nothing>()
data class Error(val error: PackageManagerError, val message: String) : PackageManagerResult<Nothing>()
fun getValueOrNull(): V? = getOrElse { null }

View file

@ -0,0 +1,32 @@
package com.vanced.manager.util
//Dear reader, welcome to HELL.
//We don't kink-shame here.
inline fun <reified E1 : Exception, reified E2 : Exception, R> doubleUnionTryCatch(
onCatch: (Exception) -> R,
onTry: () -> R
): R {
return try {
onTry()
} catch (e: Exception) {
when (e) {
is E1, is E2 -> onCatch(e)
else -> throw e
}
}
}
inline fun <reified E1 : Exception, reified E2 : Exception, reified E3 : Exception, R> tripleUnionTryCatch(
onCatch: (Exception) -> R,
onTry: () -> R
): R {
return try {
onTry()
} catch (e: Exception) {
when (e) {
is E1, is E2, is E3 -> onCatch(e)
else -> throw e
}
}
}