107 lines
4.2 KiB
Kotlin
Raw Normal View History

2020-08-18 06:31:15 -07:00
package com.topjohnwu.magisk.view
2019-08-04 13:47:14 -07:00
2022-02-13 18:35:35 -08:00
import android.annotation.SuppressLint
import android.app.Notification
2019-08-04 13:47:14 -07:00
import android.app.NotificationChannel
import android.app.NotificationManager
2022-02-13 18:35:35 -08:00
import android.app.PendingIntent
2019-08-04 13:47:14 -07:00
import android.content.Context
2022-02-13 18:35:35 -08:00
import android.content.Intent
import android.os.Build.VERSION.SDK_INT
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toIcon
2020-01-13 22:01:46 +08:00
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.Subject
2021-04-18 04:46:11 -07:00
import com.topjohnwu.magisk.di.AppContext
2020-07-11 05:36:31 -07:00
import com.topjohnwu.magisk.ktx.getBitmap
2021-11-06 17:45:41 -07:00
import java.util.concurrent.atomic.AtomicInteger
2019-08-04 13:47:14 -07:00
2021-02-11 02:34:00 -08:00
@Suppress("DEPRECATION")
2019-08-04 13:47:14 -07:00
object Notifications {
2021-04-18 04:46:11 -07:00
val mgr by lazy { AppContext.getSystemService<NotificationManager>()!! }
2019-08-04 13:47:14 -07:00
2022-02-13 18:35:35 -08:00
private const val APP_UPDATED_NOTIFICATION_ID = 4
private const val APP_UPDATE_NOTIFICATION_ID = 5
private const val UPDATE_CHANNEL = "update"
private const val PROGRESS_CHANNEL = "progress"
private const val UPDATED_CHANNEL = "updated"
private val nextId = AtomicInteger(APP_UPDATE_NOTIFICATION_ID)
2021-11-06 17:45:41 -07:00
2019-08-04 13:47:14 -07:00
fun setup(context: Context) {
if (SDK_INT >= 26) {
2022-02-13 18:35:35 -08:00
val channel = NotificationChannel(UPDATE_CHANNEL,
2022-01-21 23:52:42 +08:00
context.getString(R.string.update_channel), NotificationManager.IMPORTANCE_DEFAULT)
2022-02-13 18:35:35 -08:00
val channel2 = NotificationChannel(PROGRESS_CHANNEL,
2022-01-21 23:52:42 +08:00
context.getString(R.string.progress_channel), NotificationManager.IMPORTANCE_LOW)
2022-02-13 18:35:35 -08:00
val channel3 = NotificationChannel(UPDATED_CHANNEL,
context.getString(R.string.updated_channel), NotificationManager.IMPORTANCE_HIGH)
mgr.createNotificationChannels(listOf(channel, channel2, channel3))
2019-08-04 13:47:14 -07:00
}
}
2022-02-13 20:16:23 -08:00
fun selfLaunchIntent(context: Context): Intent {
2022-02-13 18:35:35 -08:00
val pm = context.packageManager
2022-02-13 20:16:23 -08:00
val intent = pm.getLaunchIntentForPackage(context.packageName)!!
2022-02-13 18:35:35 -08:00
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
2022-02-13 20:16:23 -08:00
return intent
}
@SuppressLint("InlinedApi")
fun updateDone(context: Context) {
setup(context)
2022-02-13 18:35:35 -08:00
val flag = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
2022-02-13 20:16:23 -08:00
val pending = PendingIntent.getActivity(context, 0, selfLaunchIntent(context), flag)
2022-02-13 18:35:35 -08:00
val builder = if (SDK_INT >= 26) {
Notification.Builder(context, UPDATED_CHANNEL)
.setSmallIcon(context.getBitmap(R.drawable.ic_magisk_outline).toIcon())
} else {
Notification.Builder(context).setPriority(Notification.PRIORITY_HIGH)
.setSmallIcon(R.drawable.ic_magisk_outline)
}
2022-02-13 18:35:35 -08:00
.setContentIntent(pending)
.setContentTitle(context.getText(R.string.updated_title))
.setContentText(context.getText(R.string.updated_text))
.setAutoCancel(true)
mgr.notify(APP_UPDATED_NOTIFICATION_ID, builder.build())
}
2022-02-13 18:35:35 -08:00
fun updateAvailable(context: Context) {
2022-02-03 03:50:40 -08:00
val intent = DownloadService.getPendingIntent(context, Subject.App())
2019-08-04 13:47:14 -07:00
2022-02-13 18:35:35 -08:00
val bitmap = context.getBitmap(R.drawable.ic_magisk_outline)
val builder = if (SDK_INT >= 26) {
Notification.Builder(context, UPDATE_CHANNEL)
.setSmallIcon(bitmap.toIcon())
} else {
Notification.Builder(context)
.setSmallIcon(R.drawable.ic_magisk_outline)
}
.setLargeIcon(bitmap)
2021-02-11 02:34:00 -08:00
.setContentTitle(context.getString(R.string.magisk_update_title))
.setContentText(context.getString(R.string.manager_download_install))
.setAutoCancel(true)
.setContentIntent(intent)
2019-08-04 13:47:14 -07:00
2022-02-13 18:35:35 -08:00
mgr.notify(APP_UPDATE_NOTIFICATION_ID, builder.build())
2019-08-04 13:47:14 -07:00
}
fun progress(context: Context, title: CharSequence): Notification.Builder {
val builder = if (SDK_INT >= 26) {
2022-02-13 18:35:35 -08:00
Notification.Builder(context, PROGRESS_CHANNEL)
} else {
Notification.Builder(context).setPriority(Notification.PRIORITY_LOW)
}
2022-02-13 18:35:35 -08:00
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentTitle(title)
.setProgress(0, 0, true)
.setOngoing(true)
2019-08-04 13:47:14 -07:00
return builder
}
2021-11-06 17:45:41 -07:00
fun nextId() = nextId.incrementAndGet()
2019-08-04 13:47:14 -07:00
}