2020-01-13 15:01:46 +01:00
|
|
|
package com.topjohnwu.magisk.core.download
|
2019-07-09 20:08:00 +02:00
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
2019-10-30 06:02:53 +01:00
|
|
|
import android.app.Notification
|
2019-07-09 20:08:00 +02:00
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
2019-07-10 19:33:44 +02:00
|
|
|
import android.os.Build
|
2020-08-21 16:36:59 +02:00
|
|
|
import androidx.core.net.toFile
|
2021-01-22 11:28:53 +01:00
|
|
|
import com.topjohnwu.magisk.core.download.Action.Flash
|
|
|
|
import com.topjohnwu.magisk.core.download.Subject.Manager
|
|
|
|
import com.topjohnwu.magisk.core.download.Subject.Module
|
2020-01-13 15:01:46 +01:00
|
|
|
import com.topjohnwu.magisk.core.intent
|
2020-03-24 18:07:20 +01:00
|
|
|
import com.topjohnwu.magisk.ui.flash.FlashFragment
|
2019-07-29 09:26:18 +02:00
|
|
|
import com.topjohnwu.magisk.utils.APKInstall
|
2019-07-09 20:08:00 +02:00
|
|
|
import kotlin.random.Random.Default.nextInt
|
|
|
|
|
|
|
|
@SuppressLint("Registered")
|
2020-08-28 13:46:05 +02:00
|
|
|
open class DownloadService : BaseDownloader() {
|
2019-07-09 20:08:00 +02:00
|
|
|
|
|
|
|
private val context get() = this
|
2020-08-21 15:27:13 +02:00
|
|
|
|
2020-08-21 15:45:40 +02:00
|
|
|
override suspend fun onFinish(subject: Subject, id: Int) = when (subject) {
|
2020-08-21 15:27:13 +02:00
|
|
|
is Module -> subject.onFinish(id)
|
|
|
|
is Manager -> subject.onFinish(id)
|
2019-07-09 20:08:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 15:45:40 +02:00
|
|
|
private fun Module.onFinish(id: Int) = when (action) {
|
2021-01-22 11:28:53 +01:00
|
|
|
Flash -> FlashFragment.install(file, id)
|
2019-07-10 15:00:14 +02:00
|
|
|
else -> Unit
|
|
|
|
}
|
2019-07-09 20:08:00 +02:00
|
|
|
|
2020-08-28 13:46:05 +02:00
|
|
|
private fun Manager.onFinish(id: Int) {
|
|
|
|
remove(id)
|
|
|
|
APKInstall.install(context, file.toFile())
|
2019-07-29 09:26:18 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 15:27:13 +02:00
|
|
|
// --- Customize finish notification
|
2019-07-09 20:08:00 +02:00
|
|
|
|
2020-08-21 15:45:40 +02:00
|
|
|
override fun Notification.Builder.setIntent(subject: Subject)
|
2019-08-22 21:05:41 +02:00
|
|
|
= when (subject) {
|
2020-08-21 15:27:13 +02:00
|
|
|
is Module -> setIntent(subject)
|
|
|
|
is Manager -> setIntent(subject)
|
2019-07-09 20:08:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 15:27:13 +02:00
|
|
|
private fun Notification.Builder.setIntent(subject: Module)
|
2020-08-21 15:45:40 +02:00
|
|
|
= when (subject.action) {
|
2021-01-22 11:28:53 +01:00
|
|
|
Flash -> setContentIntent(FlashFragment.installIntent(context, subject.file))
|
2020-08-21 15:27:13 +02:00
|
|
|
else -> setContentIntent(Intent())
|
2019-07-09 20:08:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 15:27:13 +02:00
|
|
|
private fun Notification.Builder.setIntent(subject: Manager)
|
2020-10-17 13:28:20 +02:00
|
|
|
= setContentIntent(APKInstall.installIntent(context, subject.file.toFile()))
|
2019-07-29 09:26:18 +02:00
|
|
|
|
2019-10-30 06:02:53 +01:00
|
|
|
private fun Notification.Builder.setContentIntent(intent: Intent) =
|
2020-01-12 14:52:32 +01:00
|
|
|
setContentIntent(
|
|
|
|
PendingIntent.getActivity(context, nextInt(), intent, PendingIntent.FLAG_ONE_SHOT)
|
|
|
|
)
|
2019-07-10 15:00:14 +02:00
|
|
|
|
2019-07-11 16:25:28 +02:00
|
|
|
// ---
|
|
|
|
|
2019-07-09 20:08:00 +02:00
|
|
|
companion object {
|
|
|
|
|
2020-08-28 13:46:05 +02:00
|
|
|
private fun intent(context: Context, subject: Subject) =
|
|
|
|
context.intent<DownloadService>().putExtra(ACTION_KEY, subject)
|
2019-07-11 16:25:28 +02:00
|
|
|
|
2020-08-28 13:46:05 +02:00
|
|
|
fun pendingIntent(context: Context, subject: Subject): PendingIntent {
|
|
|
|
return if (Build.VERSION.SDK_INT >= 26) {
|
|
|
|
PendingIntent.getForegroundService(context, nextInt(),
|
|
|
|
intent(context, subject), PendingIntent.FLAG_UPDATE_CURRENT)
|
2019-07-11 16:25:28 +02:00
|
|
|
} else {
|
2020-08-28 13:46:05 +02:00
|
|
|
PendingIntent.getService(context, nextInt(),
|
|
|
|
intent(context, subject), PendingIntent.FLAG_UPDATE_CURRENT)
|
2019-07-11 16:25:28 +02:00
|
|
|
}
|
2019-07-10 19:33:44 +02:00
|
|
|
}
|
2019-07-09 20:08:00 +02:00
|
|
|
|
2020-08-28 13:50:46 +02:00
|
|
|
fun start(context: Context, subject: Subject) {
|
2020-08-28 13:46:05 +02:00
|
|
|
val app = context.applicationContext
|
|
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
|
|
app.startForegroundService(intent(app, subject))
|
|
|
|
} else {
|
|
|
|
app.startService(intent(app, subject))
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 20:08:00 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 23:20:55 +02:00
|
|
|
}
|