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-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-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-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-07 16:51:45 +00:00
|
|
|
import java.io.File
|
2020-06-19 15:48:14 +00:00
|
|
|
|
|
|
|
class VancedDownloadService: Service() {
|
|
|
|
|
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-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-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-06-20 15:11:24 +00:00
|
|
|
type: String = "arch"
|
2020-06-19 15:48:14 +00:00
|
|
|
) {
|
2020-08-08 20:21:08 +00:00
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
File(getExternalFilesDir("apk")?.path as String).deleteRecursively()
|
|
|
|
val defPrefs = PreferenceManager.getDefaultSharedPreferences(this@VancedDownloadService)
|
|
|
|
val installUrl = defPrefs.getString("install_url", baseUrl)
|
|
|
|
val vancedVer = getObjectFromJson("$installUrl/vanced.json", "version")
|
2020-07-01 15:10:34 +00:00
|
|
|
|
2020-08-08 20:21:08 +00:00
|
|
|
val prefs = getSharedPreferences("installPrefs", Context.MODE_PRIVATE)
|
|
|
|
val variant = defPrefs.getString("vanced_variant", "nonroot")
|
|
|
|
val lang = prefs?.getString("lang", "en")?.split(", ")?.toTypedArray()
|
|
|
|
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" -> "$installUrl/apks/v$vancedVer/$variant/Arch/split_config.$arch.apk"
|
|
|
|
"theme" -> "$installUrl/apks/v$vancedVer/$variant/Theme/$theme.apk"
|
|
|
|
"lang" -> "$installUrl/apks/v$vancedVer/$variant/Language/split_config.${lang?.get(count)}.apk"
|
|
|
|
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) {
|
|
|
|
"arch" -> downloadSplits("theme")
|
|
|
|
"theme" -> downloadSplits("lang")
|
|
|
|
"lang" -> {
|
|
|
|
count++
|
|
|
|
if (count < lang?.count()!!)
|
|
|
|
downloadSplits("lang")
|
|
|
|
else
|
|
|
|
prepareInstall(variant!!)
|
2020-06-19 15:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-08 20:21:08 +00:00
|
|
|
}
|
2020-06-21 19:24: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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|