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
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
import com.vanced.manager.core.installer.RootSplitInstallerService
|
|
|
|
import com.vanced.manager.core.installer.SplitInstaller
|
|
|
|
import com.vanced.manager.ui.fragments.HomeFragment
|
|
|
|
import io.reactivex.disposables.Disposable
|
|
|
|
import io.reactivex.rxkotlin.subscribeBy
|
|
|
|
import io.reactivex.schedulers.Schedulers
|
|
|
|
import zlc.season.rxdownload4.delete
|
|
|
|
import zlc.season.rxdownload4.download
|
|
|
|
import zlc.season.rxdownload4.file
|
|
|
|
import zlc.season.rxdownload4.task.Task
|
|
|
|
import zlc.season.rxdownload4.utils.getFileNameFromUrl
|
|
|
|
|
|
|
|
class VancedDownloadService: Service() {
|
|
|
|
|
|
|
|
private var disposable: Disposable? = null
|
|
|
|
|
|
|
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
2020-06-20 15:11:24 +00:00
|
|
|
downloadSplits()
|
2020-06-19 15:48:14 +00:00
|
|
|
return START_NOT_STICKY
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun downloadSplits(
|
2020-06-20 15:11:24 +00:00
|
|
|
type: String = "arch"
|
2020-06-19 15:48:14 +00:00
|
|
|
) {
|
|
|
|
val prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
|
|
|
prefs?.edit()?.putBoolean("isVancedDownloading", true)?.apply()
|
2020-06-20 15:11:24 +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) {
|
|
|
|
"arch" -> "$baseUrl/$variant/Config/config.$arch.apk"
|
|
|
|
"theme" -> "$baseUrl/$variant/Theme/$theme.apk"
|
|
|
|
"lang" -> "$baseUrl/$variant/Language/split_config.$lang.apk"
|
|
|
|
"enlang" -> "$baseUrl/$variant/Language/split_config.en.apk"
|
|
|
|
else -> throw NotImplementedError("This type of APK is NOT valid. What the hell did you even do?")
|
|
|
|
}
|
|
|
|
|
|
|
|
val task = cacheDir?.path?.let {
|
|
|
|
Task(
|
|
|
|
url = url,
|
|
|
|
saveName = getFileNameFromUrl(url),
|
|
|
|
savePath = it
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (task?.file()?.exists()!!)
|
|
|
|
task.delete()
|
|
|
|
|
|
|
|
disposable = task.download()
|
2020-06-19 18:20:41 +00:00
|
|
|
.observeOn(Schedulers.newThread())
|
2020-06-19 15:48:14 +00:00
|
|
|
.subscribeBy(
|
|
|
|
onNext = { progress ->
|
|
|
|
val intent = Intent(HomeFragment.VANCED_DOWNLOADING)
|
|
|
|
intent.action = HomeFragment.VANCED_DOWNLOADING
|
2020-06-19 18:20:41 +00:00
|
|
|
intent.putExtra("vancedProgress", progress.percent().toInt())
|
2020-06-20 15:11:24 +00:00
|
|
|
intent.putExtra("fileName", "Downloading ${getFileNameFromUrl(url)}...")
|
2020-06-19 15:48:14 +00:00
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
|
|
|
},
|
|
|
|
onComplete = {
|
|
|
|
when (type) {
|
|
|
|
"arch" -> downloadSplits("theme")
|
|
|
|
"theme" -> downloadSplits("lang")
|
|
|
|
"lang" -> {
|
|
|
|
if (lang == "en" || type == "enlang") {
|
2020-06-20 16:30:44 +00:00
|
|
|
val intent = Intent(HomeFragment.VANCED_DOWNLOADED)
|
|
|
|
intent.action = HomeFragment.VANCED_DOWNLOADED
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
2020-06-19 15:48:14 +00:00
|
|
|
if (variant == "root")
|
|
|
|
launchRootInstaller()
|
|
|
|
else
|
|
|
|
launchInstaller()
|
|
|
|
stopSelf()
|
|
|
|
} else {
|
|
|
|
downloadSplits("enlang")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onError = { throwable ->
|
2020-06-19 18:20:41 +00:00
|
|
|
val intent = Intent(HomeFragment.DOWNLOAD_ERROR)
|
|
|
|
intent.action = HomeFragment.DOWNLOAD_ERROR
|
|
|
|
intent.putExtra("DownloadError", throwable.toString())
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
private fun launchInstaller() {
|
|
|
|
SplitInstaller.installSplitApk(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun launchRootInstaller() {
|
|
|
|
startService(Intent(this, RootSplitInstallerService::class.java))
|
|
|
|
}
|
|
|
|
companion object {
|
|
|
|
const val baseUrl = "https://vanced.app/api/v1/apks/v15.05.54"
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBind(intent: Intent?): IBinder? {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|