0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2025-01-06 23:41:00 +00:00

added backup URL fallback

This commit is contained in:
X1nto 2021-01-25 20:54:19 +04:00
parent 77861b400c
commit a12966d8f5
7 changed files with 68 additions and 85 deletions

View file

@ -56,26 +56,6 @@ class SponsorAdapter(
override fun getItemCount(): Int = 2
// fun getCountryFromIP(ipAddress: String?): String? {
// val db = context.assets.open("GeoLite2-Country.mmdb")
// val reader = DatabaseReader.Builder(db).build()
// val inetIp = InetAddress.getByName(ipAddress)
// return reader.country(inetIp).country.isoCode
// }
//
// init {
// json.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
// override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
// val wm = context.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager?
// val ip: String = formatIpAddress(wm!!.connectionInfo.ipAddress)
// val promotedTiers = json.get()?.array<String>("tier2")?.value!! + json.get()?.array<String>("tier3")?.value!!
// if (promotedTiers.any { getCountryFromIP(ip)?.contains(it)!! })
// sponsors.removeAt(1)
// }
//
// })
// }
companion object {
const val BRAVE = "https://vancedapp.com/brave"
const val ADGUARD = "https://adguard.com/?aid=31141&source=manager"

View file

@ -14,13 +14,11 @@ object MicrogDownloader {
fun downloadMicrog(context: Context) {
val url = microg.value?.string("url") ?: ""
context.defPrefs.installUrl?.let {
download(url, "$it/", folderName, fileName, context, onDownloadComplete = {
startMicrogInstall(context)
}, onError = {
downloadProgress.value?.downloadingFile?.postValue(context.getString(R.string.error_downloading, fileName))
})
}
download(url, "$baseInstallUrl/", folderName, fileName, context, onDownloadComplete = {
startMicrogInstall(context)
}, onError = {
downloadProgress.value?.downloadingFile?.postValue(context.getString(R.string.error_downloading, fileName))
})
}

View file

@ -26,7 +26,7 @@ object MusicDownloader {
version = prefs.musicVersion?.getLatestAppVersion(musicVersions.value?.value ?: listOf(""))
versionCode = music.value?.int("versionCode")
variant = prefs.managerVariant
baseurl = "${prefs.installUrl}/music/v$version"
baseurl = "$baseInstallUrl/music/v$version"
folderName = "music/$variant"
downloadPath = context.getExternalFilesDir(folderName)?.path
hashUrl = "$baseurl/hash.json"

View file

@ -22,7 +22,6 @@ object VancedDownloader {
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
private var lang = mutableListOf<String>()
@ -46,13 +45,12 @@ object VancedDownloader {
folderName = "vanced/$variant"
downloadPath = context.getExternalFilesDir(folderName)?.path
File(downloadPath.toString()).deleteRecursively()
installUrl = defPrefs.installUrl
prefs.lang?.let {
lang = it.split(", ").toMutableList()
}
theme = prefs.theme
vancedVersion = defPrefs.vancedVersion?.getLatestAppVersion(vancedVersions.value?.value ?: listOf(""))
themePath = "$installUrl/apks/v$vancedVersion/$variant/Theme"
themePath = "$baseInstallUrl/apks/v$vancedVersion/$variant/Theme"
hashUrl = "apks/v$vancedVersion/$variant/Theme/hash.json"
arch = getArch()
count = 0
@ -70,57 +68,55 @@ object VancedDownloader {
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"
"arch" -> "$baseInstallUrl/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"
"lang" -> "$baseInstallUrl/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?")
}
installUrl?.let {
download(url, "$it/", 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")
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, "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)
}
}
}, onError = {
if (type == "lang") {
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++
when {
count < lang.size -> downloadSplits(context, "lang")
succesfulLangCount == 0 -> {
lang.add("en")
downloadSplits(context, "lang")
}
else -> startVancedInstall(context)
}
} else {
downloadProgress.value?.downloadingFile?.postValue(context.getString(R.string.error_downloading, getFileNameFromUrl(url)))
succesfulLangCount++
if (count < lang.size)
downloadSplits(context, "lang")
else
startVancedInstall(context)
}
})
}
}
}, onError = {
if (type == "lang") {
count++
when {
count < lang.size -> downloadSplits(context, "lang")
succesfulLangCount == 0 -> {
lang.add("en")
downloadSplits(context, "lang")
}
else -> startVancedInstall(context)
}
} else {
downloadProgress.value?.downloadingFile?.postValue(context.getString(R.string.error_downloading, getFileNameFromUrl(url)))
}
})
}
fun startVancedInstall(context: Context, variant: String? = this.variant) {

View file

@ -11,7 +11,6 @@ import com.vanced.manager.core.ui.base.BindingDialogFragment
import com.vanced.manager.databinding.DialogCustomUrlBinding
import com.vanced.manager.utils.baseUrl
import com.vanced.manager.utils.defPrefs
import com.vanced.manager.utils.installUrl
import com.vanced.manager.utils.loadJson
import kotlinx.coroutines.launch

View file

@ -62,6 +62,7 @@ class VancedPreferencesDialog : BindingBottomSheetDialogFragment<DialogVancedPre
showDialog(VancedLanguageSelectionDialog())
}
vancedInstall.setOnClickListener {
if (showLang.isEmpty()) {
installPrefs.lang = "en"
}

View file

@ -13,6 +13,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import com.github.kittinunf.fuel.httpGet
import com.vanced.manager.R
import com.vanced.manager.utils.AppUtils.generateChecksum
import kotlinx.coroutines.Dispatchers
@ -32,7 +33,9 @@ val musicVersions = MutableLiveData<JsonArray<String>>()
val isFetching = MutableLiveData<Boolean>()
//var braveTiers = MutableLiveData<JsonObject?>()
var isMicrogBroken: Boolean = false
var baseInstallUrl = ""
fun openUrl(url: String, color: Int, context: Context) {
try {
@ -57,18 +60,25 @@ fun getFileNameFromUrl(url: String) = url.substring(url.lastIndexOf('/') + 1, ur
suspend fun loadJson(context: Context) = withContext(Dispatchers.IO) {
isFetching.postValue(true)
val installUrl = context.defPrefs.installUrl
if (baseInstallUrl == "" && installUrl != null) {
baseInstallUrl = installUrl
}
baseInstallUrl.httpGet().response { _, response, _ ->
if (response.statusCode / 100 != 2) {
baseInstallUrl = "https://mirror.codebucket.de/vanced"
}
}
val calendar = Calendar.getInstance()
val hour = calendar.get(Calendar.HOUR_OF_DAY)
val minute = calendar.get(Calendar.MINUTE)
val second = calendar.get(Calendar.SECOND)
val fetchTime = "fetchTime=$hour$minute$second"
val latest = getJson("$installUrl/latest.json?$fetchTime")
val versions = getJson("$installUrl/versions.json?$fetchTime")
// braveTiers.apply {
// set(getJson("$installUrl/sponsor.json"))
// notifyChange()
// }
val latest = getJson("$baseInstallUrl/latest.json?$fetchTime")
val versions = getJson("$baseInstallUrl/versions.json?$fetchTime")
isMicrogBroken = latest?.boolean("is_microg_broken") ?: false
vanced.postValue(latest?.obj("vanced"))
vancedVersions.postValue(versions?.array("vanced") )
music.postValue(latest?.obj("music"))
@ -79,9 +89,8 @@ suspend fun loadJson(context: Context) = withContext(Dispatchers.IO) {
}
private suspend fun getJsonString(file: String, obj: String, context: Context): String {
val installUrl = context.defPrefs.installUrl
return try {
getJson("$installUrl/$file")?.string(obj) ?: context.getString(R.string.unavailable)
getJson("$baseInstallUrl/$file")?.string(obj) ?: context.getString(R.string.unavailable)
} catch (e: Exception) {
Log.e(TAG, "Error: ", e)
context.getString(R.string.unavailable)