2020-08-18 06:31:15 -07:00
|
|
|
package com.topjohnwu.magisk.view
|
2019-08-04 13:47:14 -07:00
|
|
|
|
2019-10-30 01:02:53 -04:00
|
|
|
import android.app.Notification
|
2019-08-04 13:47:14 -07:00
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.content.Context
|
2019-10-30 01:02:53 -04:00
|
|
|
import android.os.Build.VERSION.SDK_INT
|
2019-08-04 13:47:14 -07:00
|
|
|
import androidx.core.app.TaskStackBuilder
|
2019-10-30 01:02:53 -04:00
|
|
|
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.*
|
|
|
|
import com.topjohnwu.magisk.core.Const.ID.PROGRESS_NOTIFICATION_CHANNEL
|
|
|
|
import com.topjohnwu.magisk.core.Const.ID.UPDATE_NOTIFICATION_CHANNEL
|
2020-07-11 05:36:31 -07:00
|
|
|
import com.topjohnwu.magisk.ktx.get
|
|
|
|
import com.topjohnwu.magisk.ktx.getBitmap
|
2019-08-04 13:47:14 -07:00
|
|
|
|
|
|
|
object Notifications {
|
|
|
|
|
2019-10-30 01:02:53 -04:00
|
|
|
val mgr by lazy { get<Context>().getSystemService<NotificationManager>()!! }
|
2019-08-04 13:47:14 -07:00
|
|
|
|
|
|
|
fun setup(context: Context) {
|
2019-10-30 01:02:53 -04:00
|
|
|
if (SDK_INT >= 26) {
|
|
|
|
var channel = NotificationChannel(UPDATE_NOTIFICATION_CHANNEL,
|
2019-08-04 13:47:14 -07:00
|
|
|
context.getString(R.string.update_channel), NotificationManager.IMPORTANCE_DEFAULT)
|
|
|
|
mgr.createNotificationChannel(channel)
|
2019-10-30 01:02:53 -04:00
|
|
|
channel = NotificationChannel(PROGRESS_NOTIFICATION_CHANNEL,
|
2019-08-04 13:47:14 -07:00
|
|
|
context.getString(R.string.progress_channel), NotificationManager.IMPORTANCE_LOW)
|
|
|
|
mgr.createNotificationChannel(channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:02:53 -04:00
|
|
|
private fun updateBuilder(context: Context): Notification.Builder {
|
|
|
|
return Notification.Builder(context).apply {
|
|
|
|
val bitmap = context.getBitmap(R.drawable.ic_magisk_outline)
|
|
|
|
setLargeIcon(bitmap)
|
|
|
|
if (SDK_INT >= 26) {
|
|
|
|
setSmallIcon(bitmap.toIcon())
|
|
|
|
setChannelId(UPDATE_NOTIFICATION_CHANNEL)
|
|
|
|
} else {
|
|
|
|
setSmallIcon(R.drawable.ic_magisk_outline)
|
|
|
|
setVibrate(longArrayOf(0, 100, 100, 100))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-04 13:47:14 -07:00
|
|
|
fun magiskUpdate(context: Context) {
|
2019-11-03 02:55:12 -05:00
|
|
|
val intent = context.intent<SplashActivity>()
|
2019-10-16 04:38:31 -04:00
|
|
|
.putExtra(Const.Key.OPEN_SECTION, "magisk")
|
2019-08-04 13:47:14 -07:00
|
|
|
val stackBuilder = TaskStackBuilder.create(context)
|
2019-10-16 04:38:31 -04:00
|
|
|
stackBuilder.addParentStack(SplashActivity::class.java.cmp(context.packageName))
|
2019-08-04 13:47:14 -07:00
|
|
|
stackBuilder.addNextIntent(intent)
|
2020-01-13 22:01:46 +08:00
|
|
|
val pendingIntent = stackBuilder.getPendingIntent(
|
|
|
|
Const.ID.MAGISK_UPDATE_NOTIFICATION_ID,
|
2019-08-04 13:47:14 -07:00
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT)
|
|
|
|
|
2020-01-13 22:01:46 +08:00
|
|
|
val builder = updateBuilder(
|
|
|
|
context
|
|
|
|
)
|
2019-10-30 01:02:53 -04:00
|
|
|
.setContentTitle(context.getString(R.string.magisk_update_title))
|
|
|
|
.setContentText(context.getString(R.string.manager_download_install))
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setContentIntent(pendingIntent)
|
2019-08-04 13:47:14 -07:00
|
|
|
|
|
|
|
mgr.notify(Const.ID.MAGISK_UPDATE_NOTIFICATION_ID, builder.build())
|
|
|
|
}
|
|
|
|
|
|
|
|
fun managerUpdate(context: Context) {
|
2019-11-03 02:55:12 -05:00
|
|
|
val intent = context.intent<GeneralReceiver>()
|
2019-10-16 04:38:31 -04:00
|
|
|
.setAction(Const.Key.BROADCAST_MANAGER_UPDATE)
|
|
|
|
.putExtra(Const.Key.INTENT_SET_APP, Info.remote.app)
|
2019-08-04 13:47:14 -07:00
|
|
|
|
|
|
|
val pendingIntent = PendingIntent.getBroadcast(context,
|
|
|
|
Const.ID.APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
|
|
|
|
2020-01-13 22:01:46 +08:00
|
|
|
val builder = updateBuilder(
|
|
|
|
context
|
|
|
|
)
|
2019-10-30 01:02:53 -04:00
|
|
|
.setContentTitle(context.getString(R.string.manager_update_title))
|
|
|
|
.setContentText(context.getString(R.string.manager_download_install))
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setContentIntent(pendingIntent)
|
2019-08-04 13:47:14 -07:00
|
|
|
|
|
|
|
mgr.notify(Const.ID.APK_UPDATE_NOTIFICATION_ID, builder.build())
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:02:53 -04:00
|
|
|
fun progress(context: Context, title: CharSequence): Notification.Builder {
|
|
|
|
val builder = if (SDK_INT >= 26) {
|
|
|
|
Notification.Builder(context, PROGRESS_NOTIFICATION_CHANNEL)
|
|
|
|
} else {
|
|
|
|
Notification.Builder(context).setPriority(Notification.PRIORITY_LOW)
|
|
|
|
}
|
|
|
|
builder.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
|
|
|
|
}
|
|
|
|
}
|