mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-24 19:08:26 +00:00
Minor cleanup of DownloadEngine
This commit is contained in:
parent
19ea25a9d0
commit
21d374214f
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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<Notification.Builder>()
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user