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-21 19:24:14 +00:00
|
|
|
import com.downloader.Error
|
|
|
|
import com.downloader.OnDownloadListener
|
|
|
|
import com.downloader.PRDownloader
|
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-21 19:24:14 +00:00
|
|
|
import com.vanced.manager.utils.InternetTools.getFileNameFromUrl
|
2020-06-25 09:45:39 +00:00
|
|
|
import java.lang.IllegalStateException
|
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-25 09:45:39 +00:00
|
|
|
} catch (e: IllegalStateException) {
|
2020-06-24 19:17:21 +00:00
|
|
|
Toast.makeText(this, "Unable to download Vanced", Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
2020-06-21 20:05:48 +00:00
|
|
|
stopSelf()
|
2020-06-24 19:17:21 +00:00
|
|
|
return START_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
|
|
|
) {
|
|
|
|
val prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
|
|
|
prefs?.edit()?.putBoolean("isVancedDownloading", true)?.apply()
|
2020-06-21 19:24:14 +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?")
|
|
|
|
}
|
|
|
|
|
2020-06-21 19:54:18 +00:00
|
|
|
PRDownloader.download(url, cacheDir.path, getFileNameFromUrl(url))
|
2020-06-21 19:24:14 +00:00
|
|
|
.build()
|
|
|
|
.setOnProgressListener { progress ->
|
|
|
|
val intent = Intent(HomeFragment.VANCED_DOWNLOADING)
|
|
|
|
val mProgress = progress.currentBytes * 100 / progress.totalBytes
|
|
|
|
intent.action = HomeFragment.VANCED_DOWNLOADING
|
|
|
|
intent.putExtra("vancedProgress", mProgress.toInt())
|
|
|
|
intent.putExtra(
|
|
|
|
"fileName",
|
|
|
|
"Downloading ${getFileNameFromUrl(url)}..."
|
|
|
|
)
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
|
|
|
}
|
|
|
|
.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-19 15:48:14 +00:00
|
|
|
} else {
|
|
|
|
downloadSplits("enlang")
|
|
|
|
}
|
|
|
|
}
|
2020-06-21 20:05:48 +00:00
|
|
|
"enlang" -> prepareInstall(variant!!)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
2020-06-21 19:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onError(error: Error) {
|
2020-06-19 18:20:41 +00:00
|
|
|
val intent = Intent(HomeFragment.DOWNLOAD_ERROR)
|
|
|
|
intent.action = HomeFragment.DOWNLOAD_ERROR
|
2020-06-21 19:24:14 +00:00
|
|
|
intent.putExtra("DownloadError", error.toString())
|
|
|
|
LocalBroadcastManager.getInstance(this@VancedDownloadService)
|
|
|
|
.sendBroadcast(intent)
|
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")
|
|
|
|
launchRootInstaller()
|
|
|
|
else
|
|
|
|
launchInstaller()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|