0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-12-19 06:50:09 +00:00

service tests

This commit is contained in:
X1nto 2020-06-30 14:19:56 +04:00
parent 464596e22e
commit bc2bcb7253
4 changed files with 42 additions and 16 deletions

View file

@ -4,12 +4,15 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log import android.util.Log
import com.downloader.PRDownloader
class DownloadBroadcastReceiver: BroadcastReceiver() { class DownloadBroadcastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
PRDownloader.cancelAll() val type = intent?.getStringExtra("type")
Log.d("VMNotification", "Canceled downloads") when (type) {
"vanced" -> context?.stopService(Intent(context, VancedDownloadService::class.java))
"microg" -> context?.stopService(Intent(context, MicrogDownloadService::class.java))
}
Log.d("VMNotification", "Canceled $type download")
} }
} }

View file

@ -3,10 +3,14 @@ package com.vanced.manager.core.downloader
import android.app.Service import android.app.Service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.widget.Toast import android.widget.Toast
import com.dezlum.codelabs.getjson.GetJson import com.dezlum.codelabs.getjson.GetJson
import com.downloader.* import com.downloader.Error
import com.downloader.OnDownloadListener
import com.downloader.OnStartOrResumeListener
import com.downloader.PRDownloader
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.core.installer.AppInstaller import com.vanced.manager.core.installer.AppInstaller
import com.vanced.manager.utils.InternetTools.getFileNameFromUrl import com.vanced.manager.utils.InternetTools.getFileNameFromUrl
@ -44,13 +48,11 @@ class MicrogDownloadService: Service() {
NotificationHelper.displayDownloadNotif( NotificationHelper.displayDownloadNotif(
channel, channel,
mProgress.toInt(), mProgress.toInt(),
"microg",
getFileNameFromUrl(dwnldUrl), getFileNameFromUrl(dwnldUrl),
this this
) )
} }
.setOnCancelListener { OnCancelListener {
cancelNotif(channel, this)
} }
.start(object : OnDownloadListener { .start(object : OnDownloadListener {
override fun onDownloadComplete() { override fun onDownloadComplete() {
prefs?.edit()?.putBoolean("isMicrogDownloading", false)?.apply() prefs?.edit()?.putBoolean("isMicrogDownloading", false)?.apply()
@ -58,7 +60,10 @@ class MicrogDownloadService: Service() {
val intent = Intent(this@MicrogDownloadService, AppInstaller::class.java) val intent = Intent(this@MicrogDownloadService, AppInstaller::class.java)
intent.putExtra("path", "${filesDir.path}/microg.apk") intent.putExtra("path", "${filesDir.path}/microg.apk")
intent.putExtra("pkg", "com.mgoogle.android.gms") intent.putExtra("pkg", "com.mgoogle.android.gms")
startService(intent) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startForegroundService(intent)
else
startService(intent)
} }
override fun onError(error: Error) { override fun onError(error: Error) {
prefs?.edit()?.putBoolean("isMicrogDownloading", false)?.apply() prefs?.edit()?.putBoolean("isMicrogDownloading", false)?.apply()
@ -67,6 +72,11 @@ class MicrogDownloadService: Service() {
}) })
} }
override fun onDestroy() {
super.onDestroy()
cancelNotif(420, this)
}
override fun onBind(intent: Intent?): IBinder? { override fun onBind(intent: Intent?): IBinder? {
return null return null
} }

View file

@ -9,7 +9,10 @@ import android.widget.Toast
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.dezlum.codelabs.getjson.GetJson import com.dezlum.codelabs.getjson.GetJson
import com.downloader.* import com.downloader.Error
import com.downloader.OnDownloadListener
import com.downloader.OnStartOrResumeListener
import com.downloader.PRDownloader
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.core.installer.RootSplitInstallerService import com.vanced.manager.core.installer.RootSplitInstallerService
import com.vanced.manager.core.installer.SplitInstaller import com.vanced.manager.core.installer.SplitInstaller
@ -68,11 +71,8 @@ class VancedDownloadService: Service() {
.setOnStartOrResumeListener { OnStartOrResumeListener { prefs?.edit()?.putBoolean("isVancedDownloading", true)?.apply() } } .setOnStartOrResumeListener { OnStartOrResumeListener { prefs?.edit()?.putBoolean("isVancedDownloading", true)?.apply() } }
.setOnProgressListener { progress -> .setOnProgressListener { progress ->
val mProgress = progress.currentBytes * 100 / progress.totalBytes val mProgress = progress.currentBytes * 100 / progress.totalBytes
displayDownloadNotif(channel, mProgress.toInt(), getFileNameFromUrl(url), this) displayDownloadNotif(channel, mProgress.toInt(), "vanced", getFileNameFromUrl(url), this)
} }
.setOnCancelListener { OnCancelListener {
cancelNotif(channel, this)
} }
.start(object : OnDownloadListener { .start(object : OnDownloadListener {
override fun onDownloadComplete() { override fun onDownloadComplete() {
when (type) { when (type) {
@ -104,9 +104,21 @@ class VancedDownloadService: Service() {
intent.action = HomeFragment.VANCED_DOWNLOADED intent.action = HomeFragment.VANCED_DOWNLOADED
LocalBroadcastManager.getInstance(this).sendBroadcast(intent) LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
if (variant == "root") if (variant == "root")
startService(Intent(this, RootSplitInstallerService::class.java)) startInstallService(Intent(this, RootSplitInstallerService::class.java))
else else
startService(Intent(this, SplitInstaller::class.java)) startInstallService(Intent(this, SplitInstaller::class.java))
}
private fun startInstallService(intent: Intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startForegroundService(intent)
else
startService(intent)
}
override fun onDestroy() {
super.onDestroy()
cancelNotif(69, this)
} }
override fun onBind(intent: Intent?): IBinder? { override fun onBind(intent: Intent?): IBinder? {

View file

@ -24,7 +24,7 @@ object NotificationHelper {
} }
} }
fun displayDownloadNotif(channel: Int, progress:Int, filename: String, context: Context) { fun displayDownloadNotif(channel: Int, progress:Int, type: String, filename: String, context: Context) {
val notifBuilder = val notifBuilder =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Notification.Builder(context, channel.toString()).setChannelId("69420") Notification.Builder(context, channel.toString()).setChannelId("69420")
@ -32,6 +32,7 @@ object NotificationHelper {
Notification.Builder(context).setPriority(Notification.PRIORITY_HIGH) Notification.Builder(context).setPriority(Notification.PRIORITY_HIGH)
val cancelDownload = Intent(context, DownloadBroadcastReceiver::class.java) val cancelDownload = Intent(context, DownloadBroadcastReceiver::class.java)
cancelDownload.putExtra("type", type)
val cancelPendingIntent = PendingIntent.getBroadcast(context, 0, cancelDownload, PendingIntent.FLAG_UPDATE_CURRENT) val cancelPendingIntent = PendingIntent.getBroadcast(context, 0, cancelDownload, PendingIntent.FLAG_UPDATE_CURRENT)