Skip to content

Commit ea897e2

Browse files
committed
#1637 give option to never show notification permission alert again
1 parent a8cf612 commit ea897e2

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

app/src/main/java/io/github/sds100/keymapper/data/Keys.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ object Keys {
9393
* Whether the user viewed the advanced triggers.
9494
*/
9595
val viewedAdvancedTriggers = booleanPreferencesKey("key_viewed_advanced_triggers")
96+
97+
val neverShowNotificationPermissionAlert =
98+
booleanPreferencesKey("key_never_show_notification_permission_alert")
9699
}

app/src/main/java/io/github/sds100/keymapper/home/ShowHomeScreenAlertsUseCase.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import io.github.sds100.keymapper.system.accessibility.ServiceState
88
import io.github.sds100.keymapper.system.permissions.Permission
99
import io.github.sds100.keymapper.system.permissions.PermissionAdapter
1010
import kotlinx.coroutines.flow.Flow
11+
import kotlinx.coroutines.flow.combine
1112
import kotlinx.coroutines.flow.map
1213

1314
/**
@@ -53,12 +54,21 @@ class ShowHomeScreenAlertsUseCaseImpl(
5354
preferences.set(Keys.log, false)
5455
}
5556

56-
override val isNotificationPermissionGranted: Flow<Boolean> =
57-
permissions.isGrantedFlow(Permission.POST_NOTIFICATIONS)
57+
override val showNotificationPermissionAlert: Flow<Boolean> =
58+
combine(
59+
permissions.isGrantedFlow(Permission.POST_NOTIFICATIONS),
60+
preferences.get(Keys.neverShowNotificationPermissionAlert).map { it ?: false },
61+
) { isGranted, neverShow ->
62+
!isGranted && !neverShow
63+
}
5864

5965
override fun requestNotificationPermission() {
6066
permissions.request(Permission.POST_NOTIFICATIONS)
6167
}
68+
69+
override fun neverShowNotificationPermissionAlert() {
70+
preferences.set(Keys.neverShowNotificationPermissionAlert, true)
71+
}
6272
}
6373

6474
interface ShowHomeScreenAlertsUseCase {
@@ -76,6 +86,7 @@ interface ShowHomeScreenAlertsUseCase {
7686
val isLoggingEnabled: Flow<Boolean>
7787
fun disableLogging()
7888

79-
val isNotificationPermissionGranted: Flow<Boolean>
89+
val showNotificationPermissionAlert: Flow<Boolean>
8090
fun requestNotificationPermission()
91+
fun neverShowNotificationPermissionAlert()
8192
}

app/src/main/java/io/github/sds100/keymapper/mappings/keymaps/KeyMapListViewModel.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ class KeyMapListViewModel(
153153
showAlertsUseCase.accessibilityServiceState,
154154
showAlertsUseCase.hideAlerts,
155155
showAlertsUseCase.isLoggingEnabled,
156-
showAlertsUseCase.isNotificationPermissionGranted,
157-
) { isBatteryOptimised, serviceState, isHidden, isLoggingEnabled, isNotificationPermissionGranted ->
156+
showAlertsUseCase.showNotificationPermissionAlert,
157+
) { isBatteryOptimised, serviceState, isHidden, isLoggingEnabled, showNotificationPermissionAlert ->
158158
if (isHidden) {
159159
return@combine emptyList()
160160
}
@@ -189,7 +189,7 @@ class KeyMapListViewModel(
189189
)
190190
} // don't show a success message for this
191191

192-
if (!isNotificationPermissionGranted) {
192+
if (showNotificationPermissionAlert) {
193193
add(
194194
HomeWarningListItem(
195195
ID_NOTIFICATION_PERMISSION_DENIED_LIST_ITEM,
@@ -683,11 +683,29 @@ class KeyMapListViewModel(
683683

684684
ID_BATTERY_OPTIMISATION_LIST_ITEM -> showAlertsUseCase.disableBatteryOptimisation()
685685
ID_LOGGING_ENABLED_LIST_ITEM -> showAlertsUseCase.disableLogging()
686-
ID_NOTIFICATION_PERMISSION_DENIED_LIST_ITEM -> showAlertsUseCase.requestNotificationPermission()
686+
ID_NOTIFICATION_PERMISSION_DENIED_LIST_ITEM -> showNotificationPermissionAlertDialog()
687687
}
688688
}
689689
}
690690

691+
private suspend fun showNotificationPermissionAlertDialog() {
692+
val dialog = PopupUi.Dialog(
693+
title = getString(R.string.dialog_title_request_notification_permission),
694+
message = getText(R.string.dialog_message_request_notification_permission),
695+
positiveButtonText = getString(R.string.pos_turn_on),
696+
negativeButtonText = getString(R.string.neg_no_thanks),
697+
neutralButtonText = getString(R.string.pos_never_show_again),
698+
)
699+
700+
val dialogResponse = showPopup("notification_permission_alert", dialog)
701+
702+
if (dialogResponse == DialogResponse.POSITIVE) {
703+
showAlertsUseCase.requestNotificationPermission()
704+
} else if (dialogResponse == DialogResponse.NEUTRAL) {
705+
showAlertsUseCase.neverShowNotificationPermissionAlert()
706+
}
707+
}
708+
691709
fun onTogglePausedClick() {
692710
coroutineScope.launch {
693711
if (pauseKeyMaps.isPaused.first()) {

app/src/main/java/io/github/sds100/keymapper/system/permissions/RequestPermissionDelegate.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.provider.Settings
1111
import androidx.activity.result.contract.ActivityResultContracts
1212
import androidx.annotation.RequiresApi
1313
import androidx.appcompat.app.AppCompatActivity
14+
import androidx.core.app.ActivityCompat
1415
import androidx.navigation.NavController
1516
import io.github.sds100.keymapper.Constants
1617
import io.github.sds100.keymapper.NavAppDirections
@@ -98,7 +99,23 @@ class RequestPermissionDelegate(
9899
requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
99100

100101
Permission.POST_NOTIFICATIONS -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
101-
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
102+
val showRationale = ActivityCompat.shouldShowRequestPermissionRationale(
103+
activity,
104+
Manifest.permission.POST_NOTIFICATIONS,
105+
)
106+
107+
// The system will say you have to show a rationale if the user previously
108+
// denied the permission. Therefore, the permission dialog will not show and so
109+
// open the notification settings to turn it on manually.
110+
if (showRationale) {
111+
Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
112+
putExtra(Settings.EXTRA_APP_PACKAGE, Constants.PACKAGE_NAME)
113+
114+
activity.startActivity(this)
115+
}
116+
} else {
117+
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
118+
}
102119
}
103120
}
104121
}

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@
510510
<string name="dialog_title_keycode_to_scancode_trigger_explanation">Unrecognized key code</string>
511511
<string name="dialog_message_keycode_to_scancode_trigger_explanation">The pressed button was not recognized by the input system. In the past Key Mapper detected such buttons as one and the same. Currently the app tries to distinguish the button based on the scan code, which should be more unique. However, this is a makeshift incomplete solution, which doesn\'t guarantee uniqueness.</string>
512512

513+
<string name="dialog_title_request_notification_permission">Turn on notifications</string>
514+
<string name="dialog_message_request_notification_permission">Some actions and options need this permission to work. You can also get notified when there is important news about the app.</string>
515+
513516
<string name="pos_done">Done</string>
514517
<string name="pos_grant_write_secure_settings_guide">Guide</string>
515518
<string name="pos_start_service_with_adb_guide">Guide</string>
@@ -522,11 +525,13 @@
522525
<string name="pos_discard_changes">Discard changes</string>
523526
<string name="pos_save">Save</string>
524527
<string name="pos_understood">Understood</string>
528+
<string name="pos_turn_on">Turn on</string>
525529

526530
<string name="neg_turn_off">Turn off</string>
527531
<string name="neg_cancel">Cancel</string>
528532
<string name="neg_dont_show_again">Don\'t show again</string>
529533
<string name="neg_keep_editing">Keep editing</string>
534+
<string name="neg_no_thanks">No thanks</string>
530535

531536
<string name="neutral_hide">Hide</string>
532537
<string name="neutral_go_to_dont_kill_my_app">Online guide</string>

0 commit comments

Comments
 (0)