From 1d2667fb7d39bc0232e1d9ae6301cca7e0b2ccc7 Mon Sep 17 00:00:00 2001 From: X1nto Date: Sat, 5 Dec 2020 17:39:46 +0400 Subject: [PATCH] Improved root package installer code --- .../com/vanced/manager/utils/PackageHelper.kt | 108 +++++++----------- 1 file changed, 41 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt b/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt index 0beeece6..c1cb1149 100644 --- a/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt +++ b/app/src/main/java/com/vanced/manager/utils/PackageHelper.kt @@ -39,6 +39,15 @@ object PackageHelper { private const val apkInstallPath = "/data/adb" private val vancedThemes = arrayOf("black", "dark", "pink", "blue") + init { + Shell.enableVerboseLogging = BuildConfig.DEBUG + Shell.setDefaultBuilder( + Shell.Builder.create() + .setFlags(Shell.FLAG_REDIRECT_STDERR) + .setTimeout(10) + ) + } + fun isPackageInstalled(packageName: String, packageManager: PackageManager): Boolean { return try { packageManager.getPackageInfo(packageName, 0) @@ -96,31 +105,23 @@ object PackageHelper { val files = apksPath.listFiles() if (files?.isNotEmpty() == true) { for (file in files) { - when { vancedThemes.any { file.name == "$it.apk" } && !splitFiles.contains("base") -> splitFiles.add("base") file.name.matches(Regex("split_config\\.(..)\\.apk")) && !splitFiles.contains("lang") -> splitFiles.add("lang") (file.name.startsWith("split_config.arm") || file.name.startsWith("split_config.x86")) && !splitFiles.contains("arch") -> splitFiles.add("arch") } - Log.d("test", splitFiles.joinToString()) - if (splitFiles.size == 3) { return true } - } - } - return false } return false } - fun uninstallRootApk(pkg: String): Boolean { - return Shell.su("pm uninstall $pkg").exec().isSuccess - } + fun uninstallRootApk(pkg: String): Boolean = Shell.su("pm uninstall $pkg").exec().isSuccess fun uninstallApk(pkg: String, context: Context) { val callbackIntent = Intent(context, AppUninstallerService::class.java) @@ -168,32 +169,22 @@ object PackageHelper { return false } - fun installMusicRoot(context: Context) = CoroutineScope(Dispatchers.IO).launch { - Shell.enableVerboseLogging = BuildConfig.DEBUG - Shell.setDefaultBuilder( - Shell.Builder.create() - .setFlags(Shell.FLAG_REDIRECT_STDERR) - .setTimeout(10) - ) - + fun installRootApp(context: Context, app: String, appVerCode: Int, pkg: String, modApkBool: (fileName: String) -> Boolean) = CoroutineScope(Dispatchers.IO).launch { Shell.getShell { - val musicVersionCode = music.get()?.int("versionCode") - val apkFilesPath = context.getExternalFilesDir("music/root")?.path + val apkFilesPath = context.getExternalFilesDir("$app/root")?.path val fileInfoList = apkFilesPath?.let { it1 -> getFileInfoList(it1) } if (fileInfoList != null) { - val modApk: FileInfo? = fileInfoList.lastOrNull { it.name == "root.apk" } + val modApk: FileInfo? = fileInfoList.lastOrNull { modApkBool(it.name) } if (modApk != null) { - if (overwriteBase(modApk, fileInfoList, musicVersionCode!!, musicRootPkg, "music", context)) { + if (overwriteBase(modApk, fileInfoList, appVerCode, pkg, app, context)) { sendRefresh(context) sendCloseDialog(context) } - } - else { + } else { sendFailure(listOf("ModApk_Missing").toMutableList(), context) sendCloseDialog(context) } - } - else { + } else { sendFailure(listOf("Files_Missing_VA").toMutableList(), context) sendCloseDialog(context) } @@ -202,6 +193,28 @@ object PackageHelper { } + fun installMusicRoot(context: Context) { + installRootApp( + context, + "music", + music.get()?.int("versionCode")!!, + musicRootPkg + ) { + it == "root.apk" + } + } + + fun installVancedRoot(context: Context) { + installRootApp( + context, + "vanced", + vanced.get()?.int("versionCode")!!, + vancedRootPkg + ) { fileName -> + vancedThemes.any { fileName == "$it.apk" } + } + } + fun installVanced(context: Context): Int { val apkFolderPath = context.getExternalFilesDir("vanced/nonroot")?.path.toString() + "/" val nameSizeMap = HashMap() @@ -304,43 +317,6 @@ object PackageHelper { } } - fun installVancedRoot(context: Context) = CoroutineScope(Dispatchers.IO).launch { - Shell.enableVerboseLogging = BuildConfig.DEBUG - Shell.setDefaultBuilder( - Shell.Builder.create() - .setFlags(Shell.FLAG_REDIRECT_STDERR) - .setTimeout(10) - ) - - Shell.getShell { - val vancedVersionCode = vanced.get()?.int("versionCode") - val apkFilesPath = context.getExternalFilesDir("vanced/root")?.path - val fileInfoList = apkFilesPath?.let { it1 -> getFileInfoList(it1) } - if (fileInfoList != null) { - val modApk: FileInfo? = fileInfoList.lastOrNull { file -> - vancedThemes.any { file.name == "$it.apk" } - } - if (modApk != null) { - if (overwriteBase(modApk, fileInfoList, vancedVersionCode!!, vancedRootPkg, "vanced", context)) { - sendRefresh(context) - sendCloseDialog(context) - } - } - else { - sendFailure(listOf("ModApk_Missing").toMutableList(), context) - sendCloseDialog(context) - } - } - else { - sendFailure(listOf("Files_Missing_VA").toMutableList(), context) - sendCloseDialog(context) - } - - } - - } - - private fun installSplitApkFiles(apkFiles: ArrayList, context: Context) : Boolean { var sessionId: Int? val filenames = arrayOf("black.apk", "dark.apk", "blue.apk", "pink.apk", "hash.json") @@ -445,20 +421,18 @@ object PackageHelper { val apkFPath = "$apkInstallPath/${app.capitalize(Locale.ROOT)}/base.apk" if (moveAPK(apath, apkFPath, pkg, context)) { if (chConV(apkFPath, context)) { - if (setupScript(apkFPath, path, app)) { + if (setupScript(apkFPath, path, app, pkg)) { return linkApp(apkFPath, pkg, path) } } } - } - } } return false } - private fun setupScript(apkFPath: String, path: String, app: String): Boolean + private fun setupScript(apkFPath: String, path: String, app: String, pkg: String): Boolean { val shellFileZ = SuFile.open("/data/adb/service.d/$app.sh") @@ -468,7 +442,7 @@ object PackageHelper { if (shellFileZ.exists()) { try { SuFileOutputStream(shellFileZ).use { out -> out.write(code.toByteArray())} - Shell.su("""echo "#!/system/bin/sh\nwhile read line; do echo \${"$"}{line} | grep youtube | awk '{print \${'$'}2}' | xargs umount -l; done< /proc/mounts" > /data/adb/post-fs-data.d/$app.sh""").exec() + Shell.su("""echo "#!/system/bin/sh\nwhile read line; do echo \${"$"}{line} | grep $pkg | awk '{print \${'$'}2}' | xargs umount -l; done< /proc/mounts" > /data/adb/post-fs-data.d/$app.sh""").exec() return Shell.su("chmod 744 /data/adb/service.d/$app.sh").exec().isSuccess } catch (e: IOException) { e.printStackTrace()