Skip to content

Commit

Permalink
#1219 fix: prompt for notification permission in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Jul 7, 2024
1 parent f5db79a commit 8542cb8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/io/github/sds100/keymapper/data/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Keys {
val hasRootPermission = booleanPreferencesKey("pref_allow_root_features")
val shownAppIntro = booleanPreferencesKey("pref_first_time")
val showImePickerNotification = booleanPreferencesKey("pref_show_ime_notification")
val showToggleKeymapsNotification = booleanPreferencesKey("pref_show_remappings_notification")
val showToggleKeyMapsNotification = booleanPreferencesKey("pref_show_remappings_notification")
val showToggleKeyboardNotification =
booleanPreferencesKey("pref_toggle_key_mapper_keyboard_notification")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.sds100.keymapper.settings
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
import androidx.preference.isEmpty
Expand Down Expand Up @@ -100,10 +101,7 @@ class AutomaticallyChangeImeSettings : BaseSettingsFragment() {
setSummary(R.string.summary_pref_show_toggle_keyboard_notification)

setOnPreferenceClickListener {
NotificationUtils.openChannelSettings(
requireContext(),
NotificationController.CHANNEL_TOGGLE_KEYBOARD,
)
onToggleKeyboardNotificationClick()

true
}
Expand All @@ -123,4 +121,19 @@ class AutomaticallyChangeImeSettings : BaseSettingsFragment() {
}
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun onToggleKeyboardNotificationClick() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
!viewModel.isNotificationPermissionGranted()
) {
viewModel.requestNotificationsPermission()
return
}

NotificationUtils.openChannelSettings(
requireContext(),
NotificationController.CHANNEL_TOGGLE_KEYBOARD,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ class ConfigSettingsUseCaseImpl(
packageManagerAdapter.openApp(ShizukuUtils.SHIZUKU_PACKAGE)
}

override fun requestNotificationsPermission() {
permissionAdapter.request(Permission.POST_NOTIFICATIONS)
}

override fun isNotificationsPermissionGranted(): Boolean =
permissionAdapter.isGranted(Permission.POST_NOTIFICATIONS)

override fun getSoundFiles(): List<SoundFileInfo> = soundsManager.soundFiles.value

override fun deleteSoundFiles(uid: List<String>) {
Expand Down Expand Up @@ -199,6 +206,8 @@ interface ConfigSettingsUseCase {
fun deleteSoundFiles(uid: List<String>)
fun resetDefaultMappingOptions()
fun requestWriteSecureSettingsPermission()
fun requestNotificationsPermission()
fun isNotificationsPermissionGranted(): Boolean
fun requestShizukuPermission()

val connectedInputDevices: StateFlow<State<List<InputDeviceInfo>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.sds100.keymapper.settings
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
import androidx.preference.isEmpty
Expand Down Expand Up @@ -44,10 +45,7 @@ class ImePickerSettingsFragment : BaseSettingsFragment() {
setSummary(R.string.summary_pref_show_ime_picker_notification)

setOnPreferenceClickListener {
NotificationUtils.openChannelSettings(
requireContext(),
NotificationController.CHANNEL_IME_PICKER,
)
onImePickerNotificationClick()

true
}
Expand Down Expand Up @@ -87,4 +85,19 @@ class ImePickerSettingsFragment : BaseSettingsFragment() {
),
)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun onImePickerNotificationClick() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
!viewModel.isNotificationPermissionGranted()
) {
viewModel.requestNotificationsPermission()
return
}

NotificationUtils.openChannelSettings(
requireContext(),
NotificationController.CHANNEL_IME_PICKER,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts.CreateDocument
import androidx.annotation.RequiresApi
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.findNavController
import androidx.preference.DropDownPreference
Expand Down Expand Up @@ -214,17 +215,14 @@ class MainSettingsFragment : BaseSettingsFragment() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// show a preference linking to the notification management screen
Preference(requireContext()).apply {
key = Keys.showToggleKeymapsNotification.name
key = Keys.showToggleKeyMapsNotification.name

setTitle(R.string.title_pref_show_toggle_keymaps_notification)
isSingleLineTitle = false
setSummary(R.string.summary_pref_show_toggle_keymaps_notification)

setOnPreferenceClickListener {
NotificationUtils.openChannelSettings(
requireContext(),
NotificationController.CHANNEL_TOGGLE_KEYMAPS,
)
onToggleKeyMapsNotificationClick()

true
}
Expand All @@ -233,7 +231,7 @@ class MainSettingsFragment : BaseSettingsFragment() {
}
} else {
SwitchPreferenceCompat(requireContext()).apply {
key = Keys.showToggleKeymapsNotification.name
key = Keys.showToggleKeyMapsNotification.name
setDefaultValue(true)

setTitle(R.string.title_pref_show_toggle_keymaps_notification)
Expand Down Expand Up @@ -383,6 +381,21 @@ class MainSettingsFragment : BaseSettingsFragment() {
createLogCategory()
}

@RequiresApi(Build.VERSION_CODES.O)
private fun onToggleKeyMapsNotificationClick() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
!viewModel.isNotificationPermissionGranted()
) {
viewModel.requestNotificationsPermission()
return
}

NotificationUtils.openChannelSettings(
requireContext(),
NotificationController.CHANNEL_TOGGLE_KEYMAPS,
)
}

private fun automaticallyChangeImeSettingsLink() = Preference(requireContext()).apply {
key = KEY_AUTOMATICALLY_CHANGE_IME_LINK

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ class SettingsViewModel(
useCase.openShizukuApp()
}

fun isNotificationPermissionGranted(): Boolean = useCase.isNotificationsPermissionGranted()

fun requestNotificationsPermission() {
useCase.requestNotificationsPermission()
}

fun onEnableCompatibleImeClick() {
useCase.enableCompatibleIme()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ManageNotificationsUseCaseImpl(
}

override val showToggleMappingsNotification: Flow<Boolean> =
preferences.get(Keys.showToggleKeymapsNotification).map {
preferences.get(Keys.showToggleKeyMapsNotification).map {
// always show the notification on Oreo+ because the system/user controls whether notifications are shown
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
true
Expand Down

0 comments on commit 8542cb8

Please sign in to comment.