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
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.IBinder
|
2020-06-24 19:17:21 +00:00
|
|
|
import android.widget.Toast
|
2020-06-19 15:48:14 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
|
|
|
import androidx.preference.PreferenceManager
|
2020-06-29 16:45:47 +00:00
|
|
|
import com.dezlum.codelabs.getjson.GetJson
|
2020-06-30 10:19:56 +00:00
|
|
|
import com.downloader.Error
|
|
|
|
import com.downloader.OnDownloadListener
|
|
|
|
import com.downloader.OnStartOrResumeListener
|
|
|
|
import com.downloader.PRDownloader
|
2020-06-29 15:44:54 +00:00
|
|
|
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-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-06-29 15:44:54 +00:00
|
|
|
import com.vanced.manager.utils.NotificationHelper.cancelNotif
|
|
|
|
import com.vanced.manager.utils.NotificationHelper.createBasicNotif
|
2020-06-28 14:47:51 +00:00
|
|
|
import com.vanced.manager.utils.NotificationHelper.displayDownloadNotif
|
2020-07-01 11:59:32 +00:00
|
|
|
import java.lang.IllegalStateException
|
2020-06-27 15:03:59 +00:00
|
|
|
import java.util.concurrent.ExecutionException
|
2020-06-19 15:48:14 +00:00
|
|
|
|
|
|
|
class VancedDownloadService: Service() {
|
|
|
|
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
2020-06-24 19:17:21 +00:00
|
|
|
try {
|
|
|
|
downloadSplits()
|
2020-06-28 14:47:51 +00:00
|
|
|
} catch (e: Exception) {
|
|
|
|
when (e) {
|
2020-07-01 11:59:32 +00:00
|
|
|
is ExecutionException, is InterruptedException, is IllegalStateException -> Toast.makeText(this, "Unable to download Vanced", Toast.LENGTH_SHORT).show()
|
2020-06-28 14:47:51 +00:00
|
|
|
else -> throw e
|
|
|
|
}
|
|
|
|
|
2020-06-24 19:17:21 +00:00
|
|
|
}
|
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-06-20 15:11:24 +00:00
|
|
|
type: String = "arch"
|
2020-06-19 15:48:14 +00:00
|
|
|
) {
|
2020-06-29 16:45:47 +00:00
|
|
|
val baseUrl = PreferenceManager.getDefaultSharedPreferences(this).getString("install_url", baseUrl)
|
|
|
|
val vancedVer = GetJson().AsJSONObject("https://x1nto.github.io/VancedFiles/vanced.json").get("version").asString
|
2020-06-19 15:48:14 +00:00
|
|
|
val prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
2020-06-29 16:45:47 +00:00
|
|
|
val variant = PreferenceManager.getDefaultSharedPreferences(this).getString("vanced_variant", "nonroot")
|
2020-06-19 15:48:14 +00:00
|
|
|
val lang = prefs?.getString("lang", "en")
|
|
|
|
val theme = prefs?.getString("theme", "dark")
|
|
|
|
val arch =
|
|
|
|
when {
|
|
|
|
Build.SUPPORTED_ABIS.contains("x86") -> "x86"
|
|
|
|
Build.SUPPORTED_ABIS.contains("arm64-v8a") -> "arm64_v8a"
|
|
|
|
else -> "armeabi_v7a"
|
|
|
|
}
|
|
|
|
val url =
|
|
|
|
when (type) {
|
2020-06-29 16:45:47 +00:00
|
|
|
"arch" -> "$baseUrl/apks/v$vancedVer/$variant/Config/config.$arch.apk"
|
|
|
|
"theme" -> "$baseUrl/apks/v$vancedVer/$variant/Theme/$theme.apk"
|
|
|
|
"lang" -> "$baseUrl/apks/v$vancedVer/$variant/Language/split_config.$lang.apk"
|
|
|
|
"enlang" -> "$baseUrl/apks/v$vancedVer/$variant/Language/split_config.en.apk"
|
2020-06-19 15:48:14 +00:00
|
|
|
else -> throw NotImplementedError("This type of APK is NOT valid. What the hell did you even do?")
|
|
|
|
}
|
|
|
|
|
2020-06-28 14:47:51 +00:00
|
|
|
val channel = 69
|
2020-06-30 08:09:01 +00:00
|
|
|
PRDownloader
|
|
|
|
.download(url, cacheDir.path, getFileNameFromUrl(url))
|
2020-06-21 19:24:14 +00:00
|
|
|
.build()
|
2020-06-29 15:44:54 +00:00
|
|
|
.setOnStartOrResumeListener { OnStartOrResumeListener { prefs?.edit()?.putBoolean("isVancedDownloading", true)?.apply() } }
|
2020-06-21 19:24:14 +00:00
|
|
|
.setOnProgressListener { progress ->
|
|
|
|
val mProgress = progress.currentBytes * 100 / progress.totalBytes
|
2020-06-30 13:58:31 +00:00
|
|
|
displayDownloadNotif(channel, mProgress.toInt(), getFileNameFromUrl(url), this)
|
2020-06-21 19:24:14 +00:00
|
|
|
}
|
|
|
|
.start(object : OnDownloadListener {
|
|
|
|
override fun onDownloadComplete() {
|
2020-06-19 15:48:14 +00:00
|
|
|
when (type) {
|
|
|
|
"arch" -> downloadSplits("theme")
|
|
|
|
"theme" -> downloadSplits("lang")
|
|
|
|
"lang" -> {
|
2020-06-21 20:05:48 +00:00
|
|
|
if (lang == "en") {
|
|
|
|
prepareInstall(variant!!)
|
2020-06-29 15:44:54 +00:00
|
|
|
cancelNotif(channel, this@VancedDownloadService)
|
2020-06-19 15:48:14 +00:00
|
|
|
} else {
|
|
|
|
downloadSplits("enlang")
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 14:47:51 +00:00
|
|
|
"enlang" -> {
|
|
|
|
prepareInstall(variant!!)
|
2020-06-29 15:44:54 +00:00
|
|
|
cancelNotif(channel, this@VancedDownloadService)
|
2020-06-28 14:47:51 +00:00
|
|
|
}
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-06-21 19:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onError(error: Error) {
|
2020-06-29 15:44:54 +00:00
|
|
|
createBasicNotif(getString(R.string.error_downloading, "Vanced"), channel, this@VancedDownloadService)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-06-21 19:24:14 +00:00
|
|
|
})
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-06-21 20:05:48 +00:00
|
|
|
|
|
|
|
private fun prepareInstall(variant: String) {
|
|
|
|
val intent = Intent(HomeFragment.VANCED_DOWNLOADED)
|
|
|
|
intent.action = HomeFragment.VANCED_DOWNLOADED
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
cancelNotif(69, this)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBind(intent: Intent?): IBinder? {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|