2020-06-19 15:48:14 +00:00
|
|
|
package com.vanced.manager.core.downloader
|
|
|
|
|
|
|
|
import android.app.Service
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
2020-08-25 09:05:20 +00:00
|
|
|
import android.content.SharedPreferences
|
2020-06-19 15:48:14 +00:00
|
|
|
import android.os.Build
|
|
|
|
import android.os.IBinder
|
2020-08-05 11:52:09 +00:00
|
|
|
import android.widget.Toast
|
2020-06-19 15:48:14 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
|
|
|
import androidx.preference.PreferenceManager
|
2020-08-02 16:16:55 +00:00
|
|
|
import com.downloader.Error
|
|
|
|
import com.downloader.OnDownloadListener
|
|
|
|
import com.downloader.PRDownloader
|
|
|
|
import com.vanced.manager.R
|
2020-06-19 15:48:14 +00:00
|
|
|
import com.vanced.manager.core.installer.RootSplitInstallerService
|
|
|
|
import com.vanced.manager.core.installer.SplitInstaller
|
|
|
|
import com.vanced.manager.ui.fragments.HomeFragment
|
2020-08-08 20:21:08 +00:00
|
|
|
import com.vanced.manager.utils.AppUtils.installing
|
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-07-01 17:52:56 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.getObjectFromJson
|
2020-08-25 01:00:26 +00:00
|
|
|
import com.vanced.manager.utils.PackageHelper.getPkgVerCode
|
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-06-19 15:48:14 +00:00
|
|
|
|
|
|
|
class VancedDownloadService: Service() {
|
|
|
|
|
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
|
|
|
|
private var lang: Array<String>? = null
|
2020-08-26 14:22:36 +00:00
|
|
|
private var newInstaller: Boolean? = null
|
2020-08-25 09:05:20 +00:00
|
|
|
|
|
|
|
private lateinit var themePath: String
|
|
|
|
|
2020-08-02 16:16:55 +00:00
|
|
|
//private var downloadId: Long = 0
|
|
|
|
//private var apkType: String = "arch"
|
|
|
|
private var count: Int = 0
|
2020-08-05 11:52:09 +00:00
|
|
|
private val localBroadcastManager by lazy { LocalBroadcastManager.getInstance(this) }
|
2020-08-25 01:00:26 +00:00
|
|
|
private var hashUrl = ""
|
|
|
|
|
|
|
|
private val yPkg = "com.google.android.youtube"
|
2020-08-31 17:47:23 +00:00
|
|
|
private val vancedVersionCode by lazy {runBlocking { InternetTools.getJsonInt("vanced.json", "versionCode", applicationContext) }}
|
|
|
|
private val vancedVersion by lazy { runBlocking { getObjectFromJson("$installUrl/vanced.json", "version") }}
|
2020-07-01 15:10:34 +00:00
|
|
|
|
2020-07-10 19:09:51 +00:00
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
2020-08-02 16:16:55 +00:00
|
|
|
//registerReceiver(receiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
|
2020-08-25 09:05:20 +00:00
|
|
|
File(getExternalFilesDir("apks")?.path as String).deleteRecursively()
|
|
|
|
defPrefs = PreferenceManager.getDefaultSharedPreferences(this)
|
|
|
|
installUrl = defPrefs.getString("install_url", baseUrl)
|
|
|
|
prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
|
|
|
variant = defPrefs.getString("vanced_variant", "nonroot")
|
|
|
|
lang = prefs.getString("lang", "en")?.split(", ")?.toTypedArray()
|
|
|
|
theme = prefs.getString("theme", "dark")
|
|
|
|
themePath = "$installUrl/apks/v$vancedVersion/$variant/Theme"
|
|
|
|
hashUrl = "apks/v$vancedVersion/$variant/Theme/hash.json"
|
2020-08-26 14:22:36 +00:00
|
|
|
newInstaller = defPrefs.getBoolean("new_installer", false)
|
2020-08-25 09:05:20 +00:00
|
|
|
arch =
|
|
|
|
when {
|
|
|
|
Build.SUPPORTED_ABIS.contains("x86") -> "x86"
|
|
|
|
Build.SUPPORTED_ABIS.contains("arm64-v8a") -> "arm64_v8a"
|
|
|
|
else -> "armeabi_v7a"
|
|
|
|
}
|
2020-07-01 15:10:34 +00:00
|
|
|
downloadSplits()
|
2020-06-21 20:05:48 +00:00
|
|
|
stopSelf()
|
2020-06-30 10:44:04 +00:00
|
|
|
return START_NOT_STICKY
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun downloadSplits(
|
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-08-23 00:48:48 +00:00
|
|
|
"stock" -> "$themePath/stock.apk"
|
|
|
|
"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-08-08 20:21:08 +00:00
|
|
|
//apkType = type
|
|
|
|
//downloadId = download(url, "apks", getFileNameFromUrl(url), this@VancedDownloadService)
|
2020-08-02 16:16:55 +00:00
|
|
|
|
2020-08-08 20:21:08 +00:00
|
|
|
PRDownloader
|
|
|
|
.download(url, getExternalFilesDir("apks")?.path, getFileNameFromUrl(url))
|
|
|
|
.build()
|
|
|
|
.setOnStartOrResumeListener{ installing = true }
|
|
|
|
.setOnProgressListener { progress ->
|
|
|
|
val mProgress = progress.currentBytes * 100 / progress.totalBytes
|
|
|
|
localBroadcastManager.sendBroadcast(Intent(HomeFragment.VANCED_DOWNLOADING).putExtra("progress", mProgress.toInt()).putExtra("file", getFileNameFromUrl(url)))
|
|
|
|
}
|
|
|
|
.start(object : OnDownloadListener {
|
|
|
|
override fun onDownloadComplete() {
|
|
|
|
when (type) {
|
2020-08-25 09:05:20 +00:00
|
|
|
"theme" ->
|
2020-08-26 14:22:36 +00:00
|
|
|
if (variant == "root" && newInstaller == true) {
|
2020-08-25 09:05:20 +00:00
|
|
|
if (ValidateTheme()) {
|
|
|
|
if(downloadStockCheck())
|
|
|
|
downloadSplits("arch")
|
|
|
|
else
|
|
|
|
prepareInstall(variant!!)
|
|
|
|
} else
|
|
|
|
downloadSplits("theme")
|
|
|
|
} else
|
|
|
|
downloadSplits("arch")
|
2020-08-26 14:22:36 +00:00
|
|
|
"arch" -> if (variant == "root" && newInstaller == true) downloadSplits("stock") else downloadSplits("lang")
|
2020-08-22 22:51:49 +00:00
|
|
|
"stock" -> downloadSplits("dpi")
|
2020-08-25 01:00:26 +00:00
|
|
|
"dpi" -> downloadSplits("lang")
|
2020-08-08 20:21:08 +00:00
|
|
|
"lang" -> {
|
|
|
|
count++
|
|
|
|
if (count < lang?.count()!!)
|
|
|
|
downloadSplits("lang")
|
|
|
|
else
|
|
|
|
prepareInstall(variant!!)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-08-22 22:51:49 +00:00
|
|
|
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-08-08 20:21:08 +00:00
|
|
|
}
|
|
|
|
override fun onError(error: Error?) {
|
|
|
|
installing = false
|
|
|
|
Toast.makeText(this@VancedDownloadService, getString(R.string.error_downloading, "Vanced"), Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
})
|
2020-08-02 16:16:55 +00:00
|
|
|
}
|
2020-07-10 19:09:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 09:05:20 +00:00
|
|
|
private fun downloadStockCheck():Boolean {
|
|
|
|
return try {
|
|
|
|
getPkgVerCode(yPkg, packageManager) != vancedVersionCode
|
|
|
|
} catch (e: Exception) {
|
|
|
|
true
|
|
|
|
}
|
2020-08-25 01:00:26 +00:00
|
|
|
}
|
2020-08-25 09:05:20 +00:00
|
|
|
|
2020-08-25 01:00:26 +00:00
|
|
|
suspend fun getSha256(obj: String) {
|
|
|
|
sha256Val = InternetTools.getJsonString(hashUrl,obj,applicationContext)
|
|
|
|
}
|
2020-08-25 09:05:20 +00:00
|
|
|
|
2020-08-25 01:00:26 +00:00
|
|
|
private fun ValidateTheme(): Boolean
|
|
|
|
{
|
|
|
|
val prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
|
|
|
val theme = prefs?.getString("theme", "dark")
|
|
|
|
val themeS = getExternalFilesDir("apks")?.path + "/${theme}.apk"
|
|
|
|
val themeF = File(themeS)
|
|
|
|
runBlocking { getSha256(theme!!) }
|
|
|
|
return checkSHA256(sha256Val!!,themeF)
|
|
|
|
}
|
|
|
|
|
2020-08-02 16:16:55 +00:00
|
|
|
/*
|
2020-07-10 19:09:51 +00:00
|
|
|
private val receiver = object : BroadcastReceiver() {
|
|
|
|
override fun onReceive(context: Context?, intent: Intent?) {
|
2020-07-23 13:35:04 +00:00
|
|
|
val prefs = context?.getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
|
|
|
val variant = PreferenceManager.getDefaultSharedPreferences(this@VancedDownloadService).getString("vanced_variant", "nonroot")
|
|
|
|
val lang = prefs?.getString("lang", "en")
|
2020-07-10 19:09:51 +00:00
|
|
|
if (intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) == downloadId) {
|
|
|
|
when (apkType) {
|
|
|
|
"arch" -> downloadSplits("theme")
|
|
|
|
"theme" -> downloadSplits("lang")
|
|
|
|
"lang" -> {
|
|
|
|
if (lang == "en") {
|
|
|
|
prepareInstall(variant!!)
|
|
|
|
//cancelNotif(channel, this@VancedDownloadService)
|
|
|
|
} else {
|
|
|
|
downloadSplits("enlang")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"enlang" -> {
|
|
|
|
prepareInstall(variant!!)
|
|
|
|
//cancelNotif(channel, this@VancedDownloadService)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-08-02 16:16:55 +00:00
|
|
|
*/
|
2020-06-21 20:05:48 +00:00
|
|
|
|
|
|
|
private fun prepareInstall(variant: String) {
|
2020-08-05 11:52:09 +00:00
|
|
|
localBroadcastManager.sendBroadcast(Intent(HomeFragment.VANCED_INSTALLING))
|
2020-06-21 20:05:48 +00:00
|
|
|
if (variant == "root")
|
2020-06-30 13:58:31 +00:00
|
|
|
startService(Intent(this, RootSplitInstallerService::class.java))
|
2020-06-21 20:05:48 +00:00
|
|
|
else
|
2020-06-30 13:58:31 +00:00
|
|
|
startService(Intent(this, SplitInstaller::class.java))
|
2020-06-30 10:19:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 15:48:14 +00:00
|
|
|
override fun onBind(intent: Intent?): IBinder? {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
sum == sha256
|
|
|
|
} 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
|
|
|
}
|