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

Improved root package installer code

This commit is contained in:
X1nto 2020-12-05 17:39:46 +04:00
parent 59193c528c
commit 1d2667fb7d

View file

@ -39,6 +39,15 @@ object PackageHelper {
private const val apkInstallPath = "/data/adb" private const val apkInstallPath = "/data/adb"
private val vancedThemes = arrayOf("black", "dark", "pink", "blue") 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 { fun isPackageInstalled(packageName: String, packageManager: PackageManager): Boolean {
return try { return try {
packageManager.getPackageInfo(packageName, 0) packageManager.getPackageInfo(packageName, 0)
@ -96,31 +105,23 @@ object PackageHelper {
val files = apksPath.listFiles() val files = apksPath.listFiles()
if (files?.isNotEmpty() == true) { if (files?.isNotEmpty() == true) {
for (file in files) { for (file in files) {
when { when {
vancedThemes.any { file.name == "$it.apk" } && !splitFiles.contains("base") -> splitFiles.add("base") 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.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") (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) { if (splitFiles.size == 3) {
return true return true
} }
} }
} }
return false return false
} }
return false return false
} }
fun uninstallRootApk(pkg: String): Boolean { fun uninstallRootApk(pkg: String): Boolean = Shell.su("pm uninstall $pkg").exec().isSuccess
return Shell.su("pm uninstall $pkg").exec().isSuccess
}
fun uninstallApk(pkg: String, context: Context) { fun uninstallApk(pkg: String, context: Context) {
val callbackIntent = Intent(context, AppUninstallerService::class.java) val callbackIntent = Intent(context, AppUninstallerService::class.java)
@ -168,32 +169,22 @@ object PackageHelper {
return false return false
} }
fun installMusicRoot(context: Context) = CoroutineScope(Dispatchers.IO).launch { fun installRootApp(context: Context, app: String, appVerCode: Int, pkg: String, modApkBool: (fileName: String) -> Boolean) = CoroutineScope(Dispatchers.IO).launch {
Shell.enableVerboseLogging = BuildConfig.DEBUG
Shell.setDefaultBuilder(
Shell.Builder.create()
.setFlags(Shell.FLAG_REDIRECT_STDERR)
.setTimeout(10)
)
Shell.getShell { Shell.getShell {
val musicVersionCode = music.get()?.int("versionCode") val apkFilesPath = context.getExternalFilesDir("$app/root")?.path
val apkFilesPath = context.getExternalFilesDir("music/root")?.path
val fileInfoList = apkFilesPath?.let { it1 -> getFileInfoList(it1) } val fileInfoList = apkFilesPath?.let { it1 -> getFileInfoList(it1) }
if (fileInfoList != null) { if (fileInfoList != null) {
val modApk: FileInfo? = fileInfoList.lastOrNull { it.name == "root.apk" } val modApk: FileInfo? = fileInfoList.lastOrNull { modApkBool(it.name) }
if (modApk != null) { if (modApk != null) {
if (overwriteBase(modApk, fileInfoList, musicVersionCode!!, musicRootPkg, "music", context)) { if (overwriteBase(modApk, fileInfoList, appVerCode, pkg, app, context)) {
sendRefresh(context) sendRefresh(context)
sendCloseDialog(context) sendCloseDialog(context)
} }
} } else {
else {
sendFailure(listOf("ModApk_Missing").toMutableList(), context) sendFailure(listOf("ModApk_Missing").toMutableList(), context)
sendCloseDialog(context) sendCloseDialog(context)
} }
} } else {
else {
sendFailure(listOf("Files_Missing_VA").toMutableList(), context) sendFailure(listOf("Files_Missing_VA").toMutableList(), context)
sendCloseDialog(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 { fun installVanced(context: Context): Int {
val apkFolderPath = context.getExternalFilesDir("vanced/nonroot")?.path.toString() + "/" val apkFolderPath = context.getExternalFilesDir("vanced/nonroot")?.path.toString() + "/"
val nameSizeMap = HashMap<String, Long>() val nameSizeMap = HashMap<String, Long>()
@ -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<FileInfo>, context: Context) : Boolean { private fun installSplitApkFiles(apkFiles: ArrayList<FileInfo>, context: Context) : Boolean {
var sessionId: Int? var sessionId: Int?
val filenames = arrayOf("black.apk", "dark.apk", "blue.apk", "pink.apk", "hash.json") 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" val apkFPath = "$apkInstallPath/${app.capitalize(Locale.ROOT)}/base.apk"
if (moveAPK(apath, apkFPath, pkg, context)) { if (moveAPK(apath, apkFPath, pkg, context)) {
if (chConV(apkFPath, context)) { if (chConV(apkFPath, context)) {
if (setupScript(apkFPath, path, app)) { if (setupScript(apkFPath, path, app, pkg)) {
return linkApp(apkFPath, pkg, path) return linkApp(apkFPath, pkg, path)
} }
} }
} }
} }
} }
} }
return false 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") val shellFileZ = SuFile.open("/data/adb/service.d/$app.sh")
@ -468,7 +442,7 @@ object PackageHelper {
if (shellFileZ.exists()) { if (shellFileZ.exists()) {
try { try {
SuFileOutputStream(shellFileZ).use { out -> out.write(code.toByteArray())} 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 return Shell.su("chmod 744 /data/adb/service.d/$app.sh").exec().isSuccess
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()