mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-25 07:17:38 +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() {
|
class JobService : BaseJobService() {
|
||||||
|
|
||||||
private var mSession: Session? = null
|
private var mSession: Session? = null
|
||||||
private var mEngine: DownloadEngine? = null
|
|
||||||
|
|
||||||
@TargetApi(value = 34)
|
@TargetApi(value = 34)
|
||||||
inner class Session(
|
inner class Session(
|
||||||
var params: JobParameters
|
private var params: JobParameters
|
||||||
) : DownloadEngine.Session {
|
) : DownloadEngine.Session {
|
||||||
|
|
||||||
override val context get() = this@JobService
|
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)
|
setNotification(params, id, builder.build(), JOB_END_NOTIFICATION_POLICY_REMOVE)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stop() {
|
override fun onDownloadComplete() {
|
||||||
jobFinished(params, false)
|
jobFinished(params, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,18 +64,12 @@ class JobService : BaseJobService() {
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
val session = mSession?.also {
|
val session = mSession?.also {
|
||||||
it.params = params
|
it.updateParams(params)
|
||||||
} ?: run {
|
} ?: run {
|
||||||
Session(params).also { mSession = it }
|
Session(params).also { mSession = it }
|
||||||
}
|
}
|
||||||
|
|
||||||
val engine = mEngine?.also {
|
session.engine.download(subject)
|
||||||
it.reattach()
|
|
||||||
} ?: run {
|
|
||||||
DownloadEngine(session).also { mEngine = it }
|
|
||||||
}
|
|
||||||
|
|
||||||
engine.download(subject)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,30 +11,28 @@ import com.topjohnwu.magisk.core.download.Subject
|
|||||||
|
|
||||||
class Service : BaseService(), DownloadEngine.Session {
|
class Service : BaseService(), DownloadEngine.Session {
|
||||||
|
|
||||||
private lateinit var mEngine: DownloadEngine
|
private var mEngine: DownloadEngine? = null
|
||||||
override val context get() = this
|
override val context get() = this
|
||||||
|
|
||||||
override fun onCreate() {
|
|
||||||
super.onCreate()
|
|
||||||
mEngine = DownloadEngine(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
||||||
if (intent.action == DownloadEngine.ACTION) {
|
if (intent.action == DownloadEngine.ACTION) {
|
||||||
IntentCompat
|
IntentCompat
|
||||||
.getParcelableExtra(intent, DownloadEngine.SUBJECT_KEY, Subject::class.java)
|
.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
|
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)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
|
||||||
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
|
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
|
||||||
startForeground(id, builder.build())
|
startForeground(id, builder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun stop() {
|
override fun onDownloadComplete() {
|
||||||
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,8 +79,8 @@ class DownloadEngine(
|
|||||||
interface Session {
|
interface Session {
|
||||||
val context: Context
|
val context: Context
|
||||||
|
|
||||||
fun attach(id: Int, builder: Notification.Builder)
|
fun attachNotification(id: Int, builder: Notification.Builder)
|
||||||
fun stop()
|
fun onDownloadComplete()
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@ -182,18 +182,13 @@ class DownloadEngine(
|
|||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
notifyFail(subject)
|
notifyFail(subject)
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized(this@DownloadEngine) {
|
|
||||||
if (notifications.isEmpty)
|
|
||||||
session.stop()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun reattach() {
|
fun reattach() {
|
||||||
val builder = notifications[attachedId] ?: return
|
val builder = notifications[attachedId] ?: return
|
||||||
session.attach(attachedId, builder)
|
session.attachNotification(attachedId, builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val notifications = SparseArrayCompat<Notification.Builder>()
|
private val notifications = SparseArrayCompat<Notification.Builder>()
|
||||||
@ -231,7 +226,7 @@ class DownloadEngine(
|
|||||||
|
|
||||||
private fun attachNotification(id: Int, notification: Notification.Builder) {
|
private fun attachNotification(id: Int, notification: Notification.Builder) {
|
||||||
attachedId = id
|
attachedId = id
|
||||||
session.attach(id, notification)
|
session.attachNotification(id, notification)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@ -265,7 +260,7 @@ class DownloadEngine(
|
|||||||
} else {
|
} else {
|
||||||
// No more notifications left, terminate the session
|
// No more notifications left, terminate the session
|
||||||
attachedId = -1
|
attachedId = -1
|
||||||
session.stop()
|
session.onDownloadComplete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user