diff --git a/app/src/main/java/com/topjohnwu/magisk/core/App.kt b/app/src/main/java/com/topjohnwu/magisk/core/App.kt index 1c300b7c5..63bae722b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/App.kt @@ -9,6 +9,7 @@ import com.topjohnwu.magisk.StubApk import com.topjohnwu.magisk.core.di.ServiceLocator import com.topjohnwu.magisk.core.utils.* import com.topjohnwu.magisk.ui.surequest.SuRequestActivity +import com.topjohnwu.magisk.view.Notifications import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.internal.UiThreadHandler import com.topjohnwu.superuser.ipc.RootService @@ -70,6 +71,7 @@ open class App() : Application() { refreshLocale() resources.patch() + Notifications.setup() } override fun onConfigurationChanged(newConfig: Configuration) { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt b/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt index 9390a2ded..424176783 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/JobService.kt @@ -33,7 +33,7 @@ class JobService : BaseJobService() { svc.fetchUpdate()?.let { Info.remote = it if (Info.env.isActive && BuildConfig.VERSION_CODE < it.magisk.versionCode) - Notifications.updateAvailable(this) + Notifications.updateAvailable() } } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Receiver.kt b/app/src/main/java/com/topjohnwu/magisk/core/Receiver.kt index c3736f307..a91925590 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Receiver.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Receiver.kt @@ -51,7 +51,7 @@ open class Receiver : BaseReceiver() { @Suppress("DEPRECATION") val installer = context.packageManager.getInstallerPackageName(context.packageName) if (installer == context.packageName) { - Notifications.updateDone(context) + Notifications.updateDone() } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/core/download/NotificationService.kt b/app/src/main/java/com/topjohnwu/magisk/core/download/NotificationService.kt index 35a74d359..7960e557a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/download/NotificationService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/download/NotificationService.kt @@ -87,7 +87,7 @@ open class NotificationService : BaseService() { } protected fun notifyUpdate(id: Int, editor: (Notification.Builder) -> Unit = {}) { - fun create() = Notifications.startProgress(this, "") + fun create() = Notifications.startProgress("") val wasEmpty = !hasNotifications val notification = notifications.getOrPut(id, ::create).also(editor) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt index d0061420a..97932f378 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt @@ -21,7 +21,6 @@ import com.topjohnwu.magisk.core.utils.RootUtils import com.topjohnwu.magisk.ui.theme.Theme import com.topjohnwu.magisk.utils.Utils import com.topjohnwu.magisk.view.MagiskDialog -import com.topjohnwu.magisk.view.Notifications import com.topjohnwu.magisk.view.Shortcuts import com.topjohnwu.superuser.Shell import kotlinx.coroutines.launch @@ -107,7 +106,6 @@ abstract class SplashActivity : NavigationActivity()!! } - private const val APP_UPDATED_NOTIFICATION_ID = 4 - private const val APP_UPDATE_NOTIFICATION_ID = 5 + private const val APP_UPDATED_ID = 4 + private const val APP_UPDATE_AVAILABLE_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) + private val nextId = AtomicInteger(APP_UPDATE_AVAILABLE_ID) - fun setup(context: Context) { - if (SDK_INT >= Build.VERSION_CODES.O) { - val channel = NotificationChannel(UPDATE_CHANNEL, - context.getString(R.string.update_channel), NotificationManager.IMPORTANCE_DEFAULT) - val channel2 = NotificationChannel(PROGRESS_CHANNEL, - context.getString(R.string.progress_channel), NotificationManager.IMPORTANCE_LOW) - val channel3 = NotificationChannel(UPDATED_CHANNEL, - context.getString(R.string.updated_channel), NotificationManager.IMPORTANCE_HIGH) - mgr.createNotificationChannels(listOf(channel, channel2, channel3)) + fun setup() { + AppContext.apply { + if (SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel(UPDATE_CHANNEL, + getString(R.string.update_channel), NotificationManager.IMPORTANCE_DEFAULT) + val channel2 = NotificationChannel(PROGRESS_CHANNEL, + getString(R.string.progress_channel), NotificationManager.IMPORTANCE_LOW) + val channel3 = NotificationChannel(UPDATED_CHANNEL, + getString(R.string.updated_channel), NotificationManager.IMPORTANCE_HIGH) + mgr.createNotificationChannels(listOf(channel, channel2, channel3)) + } } } @SuppressLint("InlinedApi") - fun updateDone(context: Context) { - val flag = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - val pending = PendingIntent.getActivity(context, 0, context.selfLaunchIntent(), flag) - val builder = if (SDK_INT >= Build.VERSION_CODES.O) { - 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) + fun updateDone() { + AppContext.apply { + val flag = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + val pending = PendingIntent.getActivity(this, 0, selfLaunchIntent(), flag) + val builder = if (SDK_INT >= Build.VERSION_CODES.O) { + Notification.Builder(this, UPDATED_CHANNEL) + .setSmallIcon(getBitmap(R.drawable.ic_magisk_outline).toIcon()) + } else { + Notification.Builder(this).setPriority(Notification.PRIORITY_HIGH) + .setSmallIcon(R.drawable.ic_magisk_outline) + } + .setContentIntent(pending) + .setContentTitle(getText(R.string.updated_title)) + .setContentText(getText(R.string.updated_text)) + .setAutoCancel(true) + mgr.notify(APP_UPDATED_ID, builder.build()) } - .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()) } - fun updateAvailable(context: Context) { - val intent = DownloadService.getPendingIntent(context, Subject.App()) - val bitmap = context.getBitmap(R.drawable.ic_magisk_outline) - val builder = if (SDK_INT >= Build.VERSION_CODES.O) { - Notification.Builder(context, UPDATE_CHANNEL) - .setSmallIcon(bitmap.toIcon()) - } else { - Notification.Builder(context) - .setSmallIcon(R.drawable.ic_magisk_outline) - } - .setLargeIcon(bitmap) - .setContentTitle(context.getString(R.string.magisk_update_title)) - .setContentText(context.getString(R.string.manager_download_install)) - .setAutoCancel(true) - .setContentIntent(intent) + fun updateAvailable() { + AppContext.apply { + val intent = DownloadService.getPendingIntent(this, Subject.App()) + val bitmap = getBitmap(R.drawable.ic_magisk_outline) + val builder = if (SDK_INT >= Build.VERSION_CODES.O) { + Notification.Builder(this, UPDATE_CHANNEL) + .setSmallIcon(bitmap.toIcon()) + } else { + Notification.Builder(this) + .setSmallIcon(R.drawable.ic_magisk_outline) + } + .setLargeIcon(bitmap) + .setContentTitle(getString(R.string.magisk_update_title)) + .setContentText(getString(R.string.manager_download_install)) + .setAutoCancel(true) + .setContentIntent(intent) - mgr.notify(APP_UPDATE_NOTIFICATION_ID, builder.build()) + mgr.notify(APP_UPDATE_AVAILABLE_ID, builder.build()) + } } - fun startProgress(context: Context, title: CharSequence): Notification.Builder { + fun startProgress(title: CharSequence): Notification.Builder { val builder = if (SDK_INT >= Build.VERSION_CODES.O) { - Notification.Builder(context, PROGRESS_CHANNEL) + Notification.Builder(AppContext, PROGRESS_CHANNEL) } else { - Notification.Builder(context).setPriority(Notification.PRIORITY_LOW) + Notification.Builder(AppContext).setPriority(Notification.PRIORITY_LOW) } .setSmallIcon(android.R.drawable.stat_sys_download) .setContentTitle(title)