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
|
2020-09-21 07:53:42 +00:00
|
|
|
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-10-31 19:45:39 +00:00
|
|
|
import com.vanced.manager.utils.DownloadHelper.downloadProgress
|
2020-11-10 18:16:29 +00:00
|
|
|
import com.vanced.manager.utils.PackageHelper.downloadStockCheck
|
2020-09-06 10:33:04 +00:00
|
|
|
import com.vanced.manager.utils.PackageHelper.installVanced
|
2020-09-15 18:06:29 +00:00
|
|
|
import com.vanced.manager.utils.PackageHelper.installVancedRoot
|
2020-08-07 16:51:45 +00:00
|
|
|
import java.io.File
|
2020-06-19 15:48:14 +00:00
|
|
|
|
2020-11-23 16:00:41 +00:00
|
|
|
object VancedDownloader {
|
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-09-30 18:06:21 +00:00
|
|
|
|
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
|
2020-08-25 01:00:26 +00:00
|
|
|
private var hashUrl = ""
|
|
|
|
|
2020-09-15 18:06:29 +00:00
|
|
|
private var vancedVersionCode = 0
|
2020-09-29 10:47:38 +00:00
|
|
|
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
|
|
|
|
2021-01-27 16:58:18 +00:00
|
|
|
fun downloadVanced(context: Context, version: String?) {
|
2021-01-24 18:12:06 +00:00
|
|
|
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()
|
2021-01-24 18:12:06 +00:00
|
|
|
prefs.lang?.let {
|
2020-11-14 23:28:11 +00:00
|
|
|
lang = it.split(", ").toMutableList()
|
|
|
|
}
|
2021-01-24 18:12:06 +00:00
|
|
|
theme = prefs.theme
|
2021-01-27 16:58:18 +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
|
2020-09-29 10:47:38 +00:00
|
|
|
|
2020-12-16 13:21:45 +00:00
|
|
|
vancedVersionCode = vanced.value?.int("versionCode") ?: 0
|
2021-01-17 15:13:30 +00:00
|
|
|
try {
|
|
|
|
downloadSplits(context)
|
|
|
|
} catch (e: Exception) {
|
2021-02-03 18:24:28 +00:00
|
|
|
log("VMDownloader", e.stackTraceToString())
|
2021-01-17 15:13:30 +00:00
|
|
|
downloadProgress.value?.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"
|
|
|
|
"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-01-25 16:54:19 +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)
|
2020-11-23 16:00:41 +00:00
|
|
|
} else
|
2021-01-25 16:54:19 +00:00
|
|
|
downloadSplits(context, "theme")
|
|
|
|
} else
|
|
|
|
downloadSplits(context, "arch")
|
|
|
|
"arch" -> if (variant == "root") downloadSplits(context, "stock") else downloadSplits(context, "lang")
|
|
|
|
"stock" -> downloadSplits(context, "dpi")
|
|
|
|
"dpi" -> downloadSplits(context, "lang")
|
|
|
|
"lang" -> {
|
2021-01-16 14:36:46 +00:00
|
|
|
count++
|
2021-01-25 16:54:19 +00:00
|
|
|
succesfulLangCount++
|
|
|
|
if (count < lang.size)
|
|
|
|
downloadSplits(context, "lang")
|
|
|
|
else
|
|
|
|
startVancedInstall(context)
|
|
|
|
}
|
2020-10-31 19:45:39 +00:00
|
|
|
|
2021-01-25 16:54:19 +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-16 14:36:46 +00:00
|
|
|
}
|
2021-01-25 16:54:19 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
downloadProgress.value?.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) {
|
2020-11-23 16:00:41 +00:00
|
|
|
downloadProgress.value?.installing?.postValue(true)
|
|
|
|
downloadProgress.value?.postReset()
|
2020-09-21 07:53:42 +00:00
|
|
|
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-09-21 07:53:42 +00:00
|
|
|
}
|
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
|
2020-09-15 18:06:29 +00:00
|
|
|
installVanced(context)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-11-15 00:26:07 +00:00
|
|
|
}
|