diff --git a/app/src/main/java/com/vanced/manager/core/downloader/DownloadBroadcastReceiver.kt b/app/src/main/java/com/vanced/manager/core/downloader/DownloadBroadcastReceiver.kt index 7a2e94a4..62d9f83e 100644 --- a/app/src/main/java/com/vanced/manager/core/downloader/DownloadBroadcastReceiver.kt +++ b/app/src/main/java/com/vanced/manager/core/downloader/DownloadBroadcastReceiver.kt @@ -4,12 +4,15 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.util.Log -import com.downloader.PRDownloader class DownloadBroadcastReceiver: BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { - PRDownloader.cancelAll() - Log.d("VMNotification", "Canceled downloads") + val type = intent?.getStringExtra("type") + when (type) { + "vanced" -> context?.stopService(Intent(context, VancedDownloadService::class.java)) + "microg" -> context?.stopService(Intent(context, MicrogDownloadService::class.java)) + } + Log.d("VMNotification", "Canceled $type download") } } \ No newline at end of file diff --git a/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloadService.kt b/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloadService.kt index 3ff2b533..facbf866 100644 --- a/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloadService.kt +++ b/app/src/main/java/com/vanced/manager/core/downloader/MicrogDownloadService.kt @@ -3,10 +3,14 @@ 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 android.widget.Toast 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.core.installer.AppInstaller import com.vanced.manager.utils.InternetTools.getFileNameFromUrl @@ -44,13 +48,11 @@ class MicrogDownloadService: Service() { NotificationHelper.displayDownloadNotif( channel, mProgress.toInt(), + "microg", getFileNameFromUrl(dwnldUrl), this ) } - .setOnCancelListener { OnCancelListener { - cancelNotif(channel, this) - } } .start(object : OnDownloadListener { override fun onDownloadComplete() { prefs?.edit()?.putBoolean("isMicrogDownloading", false)?.apply() @@ -58,7 +60,10 @@ class MicrogDownloadService: Service() { val intent = Intent(this@MicrogDownloadService, AppInstaller::class.java) intent.putExtra("path", "${filesDir.path}/microg.apk") 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) { 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? { return null } diff --git a/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloadService.kt b/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloadService.kt index a023c1ab..c90e78e8 100644 --- a/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloadService.kt +++ b/app/src/main/java/com/vanced/manager/core/downloader/VancedDownloadService.kt @@ -9,7 +9,10 @@ import android.widget.Toast import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.preference.PreferenceManager 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.core.installer.RootSplitInstallerService import com.vanced.manager.core.installer.SplitInstaller @@ -68,11 +71,8 @@ class VancedDownloadService: Service() { .setOnStartOrResumeListener { OnStartOrResumeListener { prefs?.edit()?.putBoolean("isVancedDownloading", true)?.apply() } } .setOnProgressListener { progress -> 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 { override fun onDownloadComplete() { when (type) { @@ -104,9 +104,21 @@ class VancedDownloadService: Service() { intent.action = HomeFragment.VANCED_DOWNLOADED LocalBroadcastManager.getInstance(this).sendBroadcast(intent) if (variant == "root") - startService(Intent(this, RootSplitInstallerService::class.java)) + startInstallService(Intent(this, RootSplitInstallerService::class.java)) 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? { diff --git a/app/src/main/java/com/vanced/manager/utils/NotificationHelper.kt b/app/src/main/java/com/vanced/manager/utils/NotificationHelper.kt index 2b54b5b5..c6b30b07 100644 --- a/app/src/main/java/com/vanced/manager/utils/NotificationHelper.kt +++ b/app/src/main/java/com/vanced/manager/utils/NotificationHelper.kt @@ -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 = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder(context, channel.toString()).setChannelId("69420") @@ -32,6 +32,7 @@ object NotificationHelper { Notification.Builder(context).setPriority(Notification.PRIORITY_HIGH) val cancelDownload = Intent(context, DownloadBroadcastReceiver::class.java) + cancelDownload.putExtra("type", type) val cancelPendingIntent = PendingIntent.getBroadcast(context, 0, cancelDownload, PendingIntent.FLAG_UPDATE_CURRENT)