mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
app: request notification permission for updates
Turning on "Check for updates" didn't request the notification permission, so on Android 13+ the update notifications could never be shown. Instead, every creation of MainActivity, including rotations, requested the permission when the setting was on, and turned the setting off if it was denied, while the settings switch still showed it as on. Request the permission when the setting is turned on, and only enable it when granted. Only request it on startup when the activity is first created, and refresh the switch from the setting when the settings tab resumes. Assisted-by: Claude Opus 5.5
This commit is contained in:
@@ -112,7 +112,8 @@ class MainActivity : ComponentActivity(), SplashScreenHost {
|
||||
override fun onCreateUi(savedInstanceState: Bundle?) {
|
||||
showUnsupportedMessage()
|
||||
|
||||
if (Config.checkUpdate) {
|
||||
// Only ask on startup, not every time the activity is recreated
|
||||
if (savedInstanceState == null && Config.checkUpdate) {
|
||||
extension.withPermission(Manifest.permission.POST_NOTIFICATIONS) {
|
||||
Config.checkUpdate = it
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.topjohnwu.magisk.ui.component
|
||||
|
||||
import android.Manifest.permission.POST_NOTIFICATIONS
|
||||
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
@@ -42,3 +43,35 @@ fun rememberExternalStoragePermissionLauncher(onGranted: () -> Unit): () -> Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a function that runs [onGranted] once the app is allowed to post notifications,
|
||||
* asking for the permission first on Android 13+, and tells the user if it is denied.
|
||||
*/
|
||||
@Composable
|
||||
fun rememberNotificationPermissionLauncher(onGranted: () -> Unit): () -> Unit {
|
||||
val context = LocalContext.current
|
||||
val currentOnGranted by rememberUpdatedState(onGranted)
|
||||
|
||||
val launcher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.RequestPermission()
|
||||
) { granted ->
|
||||
if (granted) {
|
||||
currentOnGranted()
|
||||
} else {
|
||||
context.toast(R.string.post_notifications_denied, Toast.LENGTH_SHORT)
|
||||
}
|
||||
}
|
||||
|
||||
return remember(launcher, context) {
|
||||
{
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU ||
|
||||
ContextCompat.checkSelfPermission(context, POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
currentOnGranted()
|
||||
} else {
|
||||
launcher.launch(POST_NOTIFICATIONS)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ import com.topjohnwu.magisk.ui.component.SettingsSwitch
|
||||
import com.topjohnwu.magisk.ui.component.SmallTitle
|
||||
import com.topjohnwu.magisk.ui.component.onlyHorizontal
|
||||
import com.topjohnwu.magisk.ui.component.rememberBiometricAuth
|
||||
import com.topjohnwu.magisk.ui.component.rememberNotificationPermissionLauncher
|
||||
import com.topjohnwu.magisk.ui.component.verticalScrollbar
|
||||
import com.topjohnwu.magisk.core.R as CoreR
|
||||
|
||||
@@ -56,6 +57,7 @@ fun SettingsScreen(
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
val requireAuth = rememberBiometricAuth()
|
||||
val enableCheckUpdate = rememberNotificationPermissionLauncher { viewModel.setCheckUpdate(true) }
|
||||
|
||||
LifecycleResumeEffect(viewModel) {
|
||||
viewModel.refreshEnvironment()
|
||||
@@ -73,7 +75,10 @@ fun SettingsScreen(
|
||||
onOpenCustomUrlDialog = viewModel::openCustomChannelUrlDialog,
|
||||
onSaveCustomUrl = viewModel::saveCustomChannelUrl,
|
||||
onToggleDoh = viewModel::setDoh,
|
||||
onToggleCheckUpdate = viewModel::setCheckUpdate,
|
||||
// Update notifications need the notification permission
|
||||
onToggleCheckUpdate = { enabled ->
|
||||
if (enabled) enableCheckUpdate() else viewModel.setCheckUpdate(false)
|
||||
},
|
||||
onOpenDownloadPathDialog = viewModel::openDownloadPathDialog,
|
||||
onSaveDownloadPath = viewModel::saveDownloadPath,
|
||||
onToggleRandomPackageName = viewModel::setRandomPackageName,
|
||||
|
||||
@@ -83,6 +83,8 @@ class SettingsViewModel : BaseViewModel() {
|
||||
isDeviceSecure = Info.isDeviceSecure,
|
||||
// Config.suAuth also depends on whether the device is secure
|
||||
suAuthEnabled = Config.suAuth,
|
||||
// Turned off when the notification permission is denied on startup
|
||||
checkUpdateEnabled = Config.checkUpdate,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user