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-08-02 16:16:55 +00:00
|
|
|
import com.downloader.Error
|
|
|
|
import com.downloader.OnDownloadListener
|
|
|
|
import com.downloader.PRDownloader
|
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-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-09 18:26:11 +00:00
|
|
|
import com.vanced.manager.utils.Extensions.getLatestAppVersion
|
2020-08-25 01:00:26 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools
|
2020-06-29 16:45:47 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.baseUrl
|
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-08-25 01:00:26 +00:00
|
|
|
import com.vanced.manager.utils.PackageHelper.getPkgVerCode
|
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-08 20:21:08 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
2020-07-11 21:43:43 +00:00
|
|
|
import kotlinx.coroutines.launch
|
2020-08-25 01:00:26 +00:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2020-08-07 16:51:45 +00:00
|
|
|
import java.io.File
|
2020-08-25 01:00:26 +00:00
|
|
|
import java.io.IOException
|
|
|
|
import java.security.MessageDigest
|
2020-09-29 17:42:03 +00:00
|
|
|
import java.util.*
|
2020-06-19 15:48:14 +00:00
|
|
|
|
2020-09-06 10:33:04 +00:00
|
|
|
object VancedDownloader {
|
2020-06-19 15:48:14 +00:00
|
|
|
|
2020-08-25 01:00:26 +00:00
|
|
|
private var sha256Val: String? = null
|
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-10-31 19:45:39 +00:00
|
|
|
private var lang: MutableList<String>? = null
|
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
|
|
|
|
|
|
|
|
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-10-31 19:45:39 +00:00
|
|
|
downloadPath = context.getExternalFilesDir("vanced/$variant")?.path
|
|
|
|
File(downloadPath.toString()).deleteRecursively()
|
2020-11-09 18:26:11 +00:00
|
|
|
installUrl = defPrefs.getString("install_url", baseUrl)
|
2020-11-05 18:26:27 +00:00
|
|
|
lang = prefs.getString("lang", getDefaultVancedLanguages())?.split(", ")?.toMutableList()
|
2020-08-25 09:05:20 +00:00
|
|
|
theme = prefs.getString("theme", "dark")
|
2020-11-09 18:26:11 +00:00
|
|
|
vancedVersion = defPrefs.getString("vanced_version", "latest")?.getLatestAppVersion(vancedVersions.get()?.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-11-05 18:26:27 +00:00
|
|
|
vancedVersionCode = vanced.get()?.int("versionCode") ?: 0
|
2020-09-06 10:33:04 +00:00
|
|
|
downloadSplits(context)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 18:06:29 +00:00
|
|
|
private fun downloadSplits(
|
2020-09-06 10:33:04 +00:00
|
|
|
context: Context,
|
2020-08-25 01:00:26 +00:00
|
|
|
type: String = "theme"
|
2020-06-19 15:48:14 +00:00
|
|
|
) {
|
2020-08-08 20:21:08 +00:00
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
val url =
|
|
|
|
when (type) {
|
2020-08-24 07:47:00 +00:00
|
|
|
"theme" -> "$themePath/$theme.apk"
|
2020-08-25 09:05:20 +00:00
|
|
|
"arch" -> "$installUrl/apks/v$vancedVersion/$variant/Arch/split_config.$arch.apk"
|
2020-09-26 17:09:01 +00:00
|
|
|
"stock" -> "$themePath/stock.apk"
|
2020-08-23 00:48:48 +00:00
|
|
|
"dpi" -> "$themePath/dpi.apk"
|
2020-08-25 09:05:20 +00:00
|
|
|
"lang" -> "$installUrl/apks/v$vancedVersion/$variant/Language/split_config.${lang?.get(count)}.apk"
|
2020-08-08 20:21:08 +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
|
|
|
|
2020-10-31 19:45:39 +00:00
|
|
|
downloadProgress.get()?.currentDownload = PRDownloader.download(url, downloadPath, getFileNameFromUrl(url))
|
2020-09-06 10:33:04 +00:00
|
|
|
.build()
|
2020-10-31 19:45:39 +00:00
|
|
|
.setOnStartOrResumeListener {
|
|
|
|
downloadProgress.get()?.downloadingFile?.set(context.getString(R.string.downloading_file, getFileNameFromUrl(url)))
|
2020-09-06 10:33:04 +00:00
|
|
|
}
|
|
|
|
.setOnProgressListener { progress ->
|
2020-10-31 19:45:39 +00:00
|
|
|
downloadProgress.get()?.downloadProgress?.set((progress.currentBytes * 100 / progress.totalBytes).toInt())
|
2020-09-25 13:14:38 +00:00
|
|
|
}
|
2020-09-06 10:33:04 +00:00
|
|
|
.start(object : OnDownloadListener {
|
|
|
|
override fun onDownloadComplete() {
|
|
|
|
when (type) {
|
|
|
|
"theme" ->
|
2020-09-17 10:13:16 +00:00
|
|
|
if (variant == "root") {
|
2020-09-15 18:06:29 +00:00
|
|
|
if (validateTheme(context)) {
|
2020-09-29 17:42:03 +00:00
|
|
|
if (downloadStockCheck(context))
|
2020-09-06 10:33:04 +00:00
|
|
|
downloadSplits(context, "arch")
|
|
|
|
else
|
2020-10-31 19:45:39 +00:00
|
|
|
startVancedInstall(context)
|
2020-09-06 10:33:04 +00:00
|
|
|
} else
|
|
|
|
downloadSplits(context, "theme")
|
2020-08-25 09:05:20 +00:00
|
|
|
} else
|
2020-09-06 10:33:04 +00:00
|
|
|
downloadSplits(context, "arch")
|
2020-09-17 10:13:16 +00:00
|
|
|
"arch" -> if (variant == "root") downloadSplits(context, "stock") else downloadSplits(context, "lang")
|
2020-09-06 10:33:04 +00:00
|
|
|
"stock" -> downloadSplits(context, "dpi")
|
|
|
|
"dpi" -> downloadSplits(context, "lang")
|
|
|
|
"lang" -> {
|
|
|
|
count++
|
2020-10-31 19:45:39 +00:00
|
|
|
succesfulLangCount++
|
2020-11-05 16:07:13 +00:00
|
|
|
if (count < lang?.size!!)
|
2020-09-06 10:33:04 +00:00
|
|
|
downloadSplits(context, "lang")
|
|
|
|
else
|
2020-10-31 19:45:39 +00:00
|
|
|
startVancedInstall(context)
|
2020-09-06 10:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onError(error: Error?) {
|
|
|
|
if (type == "lang") {
|
2020-08-08 20:21:08 +00:00
|
|
|
count++
|
2020-10-31 19:45:39 +00:00
|
|
|
when {
|
2020-11-05 16:07:13 +00:00
|
|
|
count < lang?.size!! -> downloadSplits(context, "lang")
|
2020-10-31 19:45:39 +00:00
|
|
|
succesfulLangCount == 0 -> {
|
|
|
|
lang?.add("en")
|
|
|
|
downloadSplits(context, "lang")
|
|
|
|
}
|
|
|
|
else -> startVancedInstall(context)
|
|
|
|
}
|
|
|
|
|
2020-09-06 10:33:04 +00:00
|
|
|
} else {
|
2020-10-31 19:45:39 +00:00
|
|
|
downloadProgress.get()?.downloadingFile?.set(context.getString(R.string.error_downloading, getFileNameFromUrl(url)))
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-09-04 19:14:43 +00:00
|
|
|
}
|
2020-09-06 10:33:04 +00:00
|
|
|
})
|
2020-08-02 16:16:55 +00:00
|
|
|
}
|
2020-07-10 19:09:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 19:45:39 +00:00
|
|
|
fun downloadStockCheck(context: Context): Boolean {
|
2020-08-25 09:05:20 +00:00
|
|
|
return try {
|
2020-10-02 20:30:02 +00:00
|
|
|
getPkgVerCode(vancedRootPkg, context.packageManager) != vancedVersionCode
|
2020-08-25 09:05:20 +00:00
|
|
|
} catch (e: Exception) {
|
|
|
|
true
|
|
|
|
}
|
2020-08-25 01:00:26 +00:00
|
|
|
}
|
2020-08-25 09:05:20 +00:00
|
|
|
|
2020-09-15 18:06:29 +00:00
|
|
|
private suspend fun getSha256(obj: String, context: Context) {
|
|
|
|
sha256Val = InternetTools.getJsonString(hashUrl, obj, context)
|
2020-08-25 01:00:26 +00:00
|
|
|
}
|
2020-08-25 09:05:20 +00:00
|
|
|
|
2020-09-15 18:06:29 +00:00
|
|
|
private fun validateTheme(context: Context): Boolean {
|
2020-10-31 19:45:39 +00:00
|
|
|
val themeF = File(downloadPath, "${theme}.apk")
|
2020-09-15 18:06:29 +00:00
|
|
|
runBlocking { getSha256(theme!!, context) }
|
2020-08-25 01:00:26 +00:00
|
|
|
return checkSHA256(sha256Val!!,themeF)
|
|
|
|
}
|
|
|
|
|
2020-10-31 19:45:39 +00:00
|
|
|
fun startVancedInstall(context: Context, variant: String? = this.variant) {
|
|
|
|
downloadProgress.get()?.installing?.set(true)
|
|
|
|
downloadProgress.get()?.reset()
|
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-08-25 01:00:26 +00:00
|
|
|
private fun checkSHA256(sha256: String, updateFile: File?): Boolean {
|
|
|
|
return try {
|
|
|
|
val dataBuffer = updateFile!!.readBytes()
|
|
|
|
// Generate the checksum
|
|
|
|
val sum = generateChecksum(dataBuffer)
|
|
|
|
|
2020-09-29 17:42:03 +00:00
|
|
|
sum.toLowerCase(Locale.ENGLISH) == sha256.toLowerCase(Locale.ENGLISH)
|
2020-08-25 01:00:26 +00:00
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Throws(IOException::class)
|
|
|
|
private fun generateChecksum(data: ByteArray): String {
|
|
|
|
try {
|
|
|
|
val digest: MessageDigest = MessageDigest.getInstance("SHA-256")
|
|
|
|
val hash: ByteArray = digest.digest(data)
|
|
|
|
return printableHexString(hash)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun printableHexString(data: ByteArray): String {
|
|
|
|
// Create Hex String
|
|
|
|
val hexString: StringBuilder = StringBuilder()
|
|
|
|
for (aMessageDigest:Byte in data) {
|
|
|
|
var h: String = Integer.toHexString(0xFF and aMessageDigest.toInt())
|
|
|
|
while (h.length < 2)
|
|
|
|
h = "0$h"
|
|
|
|
hexString.append(h)
|
|
|
|
}
|
|
|
|
return hexString.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-25 09:05:20 +00:00
|
|
|
}
|