diff --git a/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt b/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt index 18ac5dfa7..789c1d160 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt @@ -22,20 +22,25 @@ import java.util.concurrent.TimeUnit class JobService : BaseJobService() { private var mSession: Session? = null - private var mEngine: DownloadEngine? = null @TargetApi(value = 34) inner class Session( - var params: JobParameters + private var params: JobParameters ) : DownloadEngine.Session { override val context get() = this@JobService + val engine = DownloadEngine(this) - override fun attach(id: Int, builder: Notification.Builder) { + fun updateParams(params: JobParameters) { + this.params = params + engine.reattach() + } + + override fun attachNotification(id: Int, builder: Notification.Builder) { setNotification(params, id, builder.build(), JOB_END_NOTIFICATION_POLICY_REMOVE) } - override fun stop() { + override fun onDownloadComplete() { jobFinished(params, false) } } @@ -59,18 +64,12 @@ class JobService : BaseJobService() { return false val session = mSession?.also { - it.params = params + it.updateParams(params) } ?: run { Session(params).also { mSession = it } } - val engine = mEngine?.also { - it.reattach() - } ?: run { - DownloadEngine(session).also { mEngine = it } - } - - engine.download(subject) + session.engine.download(subject) return true } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Service.kt b/app/src/main/java/com/topjohnwu/magisk/core/Service.kt index 0f3324cac..e68f15548 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Service.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Service.kt @@ -11,30 +11,28 @@ import com.topjohnwu.magisk.core.download.Subject class Service : BaseService(), DownloadEngine.Session { - private lateinit var mEngine: DownloadEngine + private var mEngine: DownloadEngine? = null override val context get() = this - override fun onCreate() { - super.onCreate() - mEngine = DownloadEngine(this) - } - override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { if (intent.action == DownloadEngine.ACTION) { IntentCompat .getParcelableExtra(intent, DownloadEngine.SUBJECT_KEY, Subject::class.java) - ?.let { mEngine.download(it) } + ?.let { subject -> + val engine = mEngine ?: DownloadEngine(this).also { mEngine = it } + engine.download(subject) + } } return START_NOT_STICKY } - override fun attach(id: Int, builder: Notification.Builder) { + override fun attachNotification(id: Int, builder: Notification.Builder) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE) startForeground(id, builder.build()) } - override fun stop() { + override fun onDownloadComplete() { ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) } } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt b/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt index cf6802bd7..69e0c19eb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/download/DownloadEngine.kt @@ -79,8 +79,8 @@ class DownloadEngine( interface Session { val context: Context - fun attach(id: Int, builder: Notification.Builder) - fun stop() + fun attachNotification(id: Int, builder: Notification.Builder) + fun onDownloadComplete() } companion object { @@ -182,18 +182,13 @@ class DownloadEngine( Timber.e(e) notifyFail(subject) } - - synchronized(this@DownloadEngine) { - if (notifications.isEmpty) - session.stop() - } } } @Synchronized fun reattach() { val builder = notifications[attachedId] ?: return - session.attach(attachedId, builder) + session.attachNotification(attachedId, builder) } private val notifications = SparseArrayCompat() @@ -231,7 +226,7 @@ class DownloadEngine( private fun attachNotification(id: Int, notification: Notification.Builder) { attachedId = id - session.attach(id, notification) + session.attachNotification(id, notification) } @Synchronized @@ -265,7 +260,7 @@ class DownloadEngine( } else { // No more notifications left, terminate the session attachedId = -1 - session.stop() + session.onDownloadComplete() } } }