VancedManager/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloader.kt

149 lines
5.6 KiB
Kotlin
Raw Normal View History

2020-06-19 15:48:14 +00:00
package com.vanced.manager.core.downloader
import android.content.Context
2020-08-25 09:05:20 +00:00
import android.content.SharedPreferences
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.logEvent
2020-08-02 16:16:55 +00:00
import com.vanced.manager.R
2021-01-16 17:00:38 +00:00
import com.vanced.manager.utils.*
2021-02-03 18:24:28 +00:00
import com.vanced.manager.utils.AppUtils.log
2020-11-10 18:16:29 +00:00
import com.vanced.manager.utils.AppUtils.validateTheme
2020-10-02 20:30:02 +00:00
import com.vanced.manager.utils.AppUtils.vancedRootPkg
2021-01-16 14:36:46 +00:00
import com.vanced.manager.utils.DownloadHelper.download
2020-11-10 18:16:29 +00:00
import com.vanced.manager.utils.PackageHelper.downloadStockCheck
import com.vanced.manager.utils.PackageHelper.installSplitApkFiles
2020-09-15 18:06:29 +00:00
import com.vanced.manager.utils.PackageHelper.installVancedRoot
import java.io.File
2020-06-19 15:48:14 +00:00
2020-11-23 16:00:41 +00:00
object VancedDownloader {
2021-04-19 15:57:14 +00:00
2020-08-25 09:05:20 +00:00
private lateinit var prefs: SharedPreferences
private lateinit var defPrefs: SharedPreferences
private lateinit var arch: String
private var variant: String? = null
private var theme: String? = null
2020-11-14 23:28:11 +00:00
private var lang = mutableListOf<String>()
2020-08-25 09:05:20 +00:00
private lateinit var themePath: String
2020-08-02 16:16:55 +00:00
private var count: Int = 0
2020-10-31 19:45:39 +00:00
private var succesfulLangCount: Int = 0
private var hashUrl = ""
2020-09-15 18:06:29 +00:00
private var vancedVersionCode = 0
private var vancedVersion: String? = null
2020-07-01 15:10:34 +00:00
2020-10-31 19:45:39 +00:00
private var downloadPath: String? = null
2020-11-23 16:00:41 +00:00
private var folderName: String? = null
2020-10-31 19:45:39 +00:00
fun downloadVanced(context: Context, version: String?) {
defPrefs = context.defPrefs
prefs = context.installPrefs
variant = defPrefs.managerVariant
2020-11-23 16:00:41 +00:00
folderName = "vanced/$variant"
downloadPath = context.getExternalFilesDir(folderName)?.path
2020-10-31 19:45:39 +00:00
File(downloadPath.toString()).deleteRecursively()
prefs.lang?.let {
2020-11-14 23:28:11 +00:00
lang = it.split(", ").toMutableList()
}
theme = prefs.theme
2021-04-19 15:57:14 +00:00
vancedVersion = version ?: defPrefs.vancedVersion?.getLatestAppVersion(
vancedVersions.value?.value ?: listOf("")
)
2021-01-25 16:54:19 +00:00
themePath = "$baseInstallUrl/apks/v$vancedVersion/$variant/Theme"
2020-08-25 09:05:20 +00:00
hashUrl = "apks/v$vancedVersion/$variant/Theme/hash.json"
2020-11-08 13:30:12 +00:00
arch = getArch()
2020-10-31 19:45:39 +00:00
count = 0
vancedVersionCode = vanced.value?.int("versionCode") ?: 0
try {
downloadSplits(context)
} catch (e: Exception) {
2021-02-03 18:24:28 +00:00
log("VMDownloader", e.stackTraceToString())
2021-02-16 08:26:40 +00:00
downloadingFile.postValue(context.getString(R.string.error_downloading, "Vanced"))
}
2020-06-19 15:48:14 +00:00
}
2020-11-23 16:00:41 +00:00
private fun downloadSplits(context: Context, type: String = "theme") {
val url = when (type) {
"theme" -> "$themePath/$theme.apk"
2021-01-25 16:54:19 +00:00
"arch" -> "$baseInstallUrl/apks/v$vancedVersion/$variant/Arch/split_config.$arch.apk"
2020-11-23 16:00:41 +00:00
"stock" -> "$themePath/stock.apk"
2021-04-19 15:57:14 +00:00
"dpi" -> "$themePath/dpi.apk"
2021-01-25 16:54:19 +00:00
"lang" -> "$baseInstallUrl/apks/v$vancedVersion/$variant/Language/split_config.${lang[count]}.apk"
2020-11-23 16:00:41 +00:00
else -> throw NotImplementedError("This type of APK is NOT valid. What the hell did you even do?")
}
2020-06-19 15:48:14 +00:00
2021-04-19 15:57:14 +00:00
download(
url,
"$baseInstallUrl/",
folderName!!,
getFileNameFromUrl(url),
context,
onDownloadComplete = {
when (type) {
"theme" ->
if (variant == "root") {
if (validateTheme(downloadPath!!, theme!!, hashUrl, context)) {
if (downloadStockCheck(vancedRootPkg, vancedVersionCode, context))
downloadSplits(context, "arch")
else
startVancedInstall(context)
} else
downloadSplits(context, "theme")
2020-11-23 16:00:41 +00:00
} else
2021-04-19 15:57:14 +00:00
downloadSplits(context, "arch")
"arch" -> if (variant == "root") downloadSplits(
context,
"stock"
) else downloadSplits(context, "lang")
"stock" -> downloadSplits(context, "dpi")
"dpi" -> downloadSplits(context, "lang")
"lang" -> {
count++
succesfulLangCount++
if (count < lang.size)
downloadSplits(context, "lang")
else
startVancedInstall(context)
2021-01-25 16:54:19 +00:00
}
2021-04-19 15:57:14 +00:00
2021-01-16 14:36:46 +00:00
}
2021-04-19 15:57:14 +00:00
},
onError = {
if (type == "lang") {
count++
when {
count < lang.size -> downloadSplits(context, "lang")
succesfulLangCount == 0 -> {
lang.add("en")
downloadSplits(context, "lang")
}
else -> startVancedInstall(context)
}
2021-01-25 16:54:19 +00:00
2021-04-19 15:57:14 +00:00
} else {
downloadingFile.postValue(
context.getString(
R.string.error_downloading,
getFileNameFromUrl(url)
)
)
}
})
2020-07-10 19:09:51 +00:00
}
2020-10-31 19:45:39 +00:00
fun startVancedInstall(context: Context, variant: String? = this.variant) {
2021-02-16 08:26:40 +00:00
installing.postValue(true)
postReset()
FirebaseAnalytics.getInstance(context).logEvent(FirebaseAnalytics.Event.SELECT_ITEM) {
2020-10-31 19:45:39 +00:00
variant?.let { param("vanced_variant", it) }
2020-09-30 15:48:28 +00:00
theme?.let { param("vanced_theme", it) }
}
2020-06-21 20:05:48 +00:00
if (variant == "root")
2020-09-06 10:33:04 +00:00
installVancedRoot(context)
2020-06-21 20:05:48 +00:00
else
installSplitApkFiles(context, "vanced")
2020-06-19 15:48:14 +00:00
}
}