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-20 14:41:28 +00:00
|
|
|
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
|
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
|
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
|
2020-11-08 13:30:12 +00:00
|
|
|
import com.vanced.manager.utils.DeviceUtils.getArch
|
2020-10-31 19:45:39 +00:00
|
|
|
import com.vanced.manager.utils.DownloadHelper.downloadProgress
|
2020-11-23 16:00:41 +00:00
|
|
|
import com.vanced.manager.utils.DownloadHelper.fuelDownload
|
2020-11-10 12:49:27 +00:00
|
|
|
import com.vanced.manager.utils.Extensions.getInstallUrl
|
2020-11-09 18:26:11 +00:00
|
|
|
import com.vanced.manager.utils.Extensions.getLatestAppVersion
|
2020-06-21 19:24:14 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.getFileNameFromUrl
|
2020-11-05 18:26:27 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.vanced
|
|
|
|
import com.vanced.manager.utils.InternetTools.vancedVersions
|
2020-10-31 19:45:39 +00:00
|
|
|
import com.vanced.manager.utils.LanguageHelper.getDefaultVancedLanguages
|
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 installUrl: String? = null
|
|
|
|
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
|
|
|
|
|
|
|
fun downloadVanced(context: Context) {
|
2020-11-10 12:38:21 +00:00
|
|
|
defPrefs = getDefaultSharedPreferences(context)
|
|
|
|
prefs = context.getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
2020-08-25 09:05:20 +00:00
|
|
|
variant = defPrefs.getString("vanced_variant", "nonroot")
|
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()
|
2020-11-10 12:49:27 +00:00
|
|
|
installUrl = defPrefs.getInstallUrl()
|
2020-11-14 23:28:11 +00:00
|
|
|
prefs.getString("lang", getDefaultVancedLanguages())?.let {
|
|
|
|
lang = it.split(", ").toMutableList()
|
|
|
|
}
|
2020-08-25 09:05:20 +00:00
|
|
|
theme = prefs.getString("theme", "dark")
|
2020-12-16 13:21:45 +00:00
|
|
|
vancedVersion = defPrefs.getString("vanced_version", "latest")?.getLatestAppVersion(vancedVersions.value?.value ?: listOf(""))
|
2020-08-25 09:05:20 +00:00
|
|
|
themePath = "$installUrl/apks/v$vancedVersion/$variant/Theme"
|
|
|
|
hashUrl = "apks/v$vancedVersion/$variant/Theme/hash.json"
|
2020-09-17 10:13:16 +00:00
|
|
|
//newInstaller = defPrefs.getBoolean("new_installer", false)
|
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
|
2020-09-06 10:33:04 +00:00
|
|
|
downloadSplits(context)
|
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"
|
|
|
|
"arch" -> "$installUrl/apks/v$vancedVersion/$variant/Arch/split_config.$arch.apk"
|
|
|
|
"stock" -> "$themePath/stock.apk"
|
|
|
|
"dpi" -> "$themePath/dpi.apk"
|
|
|
|
"lang" -> "$installUrl/apks/v$vancedVersion/$variant/Language/split_config.${lang[count]}.apk"
|
|
|
|
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
|
|
|
|
2020-11-23 16:00:41 +00:00
|
|
|
fuelDownload(url, 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")
|
|
|
|
} else
|
|
|
|
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)
|
2020-09-06 10:33:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 16:00:41 +00:00
|
|
|
}
|
|
|
|
}, onError = {
|
|
|
|
if (type == "lang") {
|
|
|
|
count++
|
|
|
|
when {
|
|
|
|
count < lang.size -> downloadSplits(context, "lang")
|
|
|
|
succesfulLangCount == 0 -> {
|
|
|
|
lang.add("en")
|
|
|
|
downloadSplits(context, "lang")
|
2020-09-06 10:33:04 +00:00
|
|
|
}
|
2020-11-23 16:00:41 +00:00
|
|
|
else -> startVancedInstall(context)
|
|
|
|
}
|
2020-10-31 19:45:39 +00:00
|
|
|
|
2020-11-23 16:00:41 +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
|
|
|
}
|