add handling for the Post Notifications permission on Android 13

This commit is contained in:
tibbi 2022-08-31 21:26:49 +02:00
parent c196d5c1f8
commit f365e92f15
5 changed files with 50 additions and 26 deletions

View File

@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"

View File

@ -118,11 +118,17 @@ class TimerAdapter(
timer_play_pause.applyColorFilter(textColor)
timer_play_pause.setOnClickListener {
when (val state = timer.state) {
is TimerState.Idle -> EventBus.getDefault().post(TimerEvent.Start(timer.id!!, timer.seconds.secondsToMillis))
is TimerState.Paused -> EventBus.getDefault().post(TimerEvent.Start(timer.id!!, state.tick))
is TimerState.Running -> EventBus.getDefault().post(TimerEvent.Pause(timer.id!!, state.tick))
is TimerState.Finished -> EventBus.getDefault().post(TimerEvent.Start(timer.id!!, timer.seconds.secondsToMillis))
(activity as SimpleActivity).handleNotificationPermission {
if (it) {
when (val state = timer.state) {
is TimerState.Idle -> EventBus.getDefault().post(TimerEvent.Start(timer.id!!, timer.seconds.secondsToMillis))
is TimerState.Paused -> EventBus.getDefault().post(TimerEvent.Start(timer.id!!, state.tick))
is TimerState.Running -> EventBus.getDefault().post(TimerEvent.Pause(timer.id!!, state.tick))
is TimerState.Finished -> EventBus.getDefault().post(TimerEvent.Start(timer.id!!, timer.seconds.secondsToMillis))
}
} else {
activity.toast(R.string.no_post_notifications_permissions)
}
}
}

View File

@ -130,20 +130,26 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba
alarm.isEnabled = true
var alarmId = alarm.id
if (alarm.id == 0) {
alarmId = activity.dbHelper.insertAlarm(alarm)
if (alarmId == -1) {
activity.toast(R.string.unknown_error_occurred)
}
} else {
if (!activity.dbHelper.updateAlarm(alarm)) {
activity.toast(R.string.unknown_error_occurred)
activity.handleNotificationPermission {
if (it) {
if (alarm.id == 0) {
alarmId = activity.dbHelper.insertAlarm(alarm)
if (alarmId == -1) {
activity.toast(R.string.unknown_error_occurred)
}
} else {
if (!activity.dbHelper.updateAlarm(alarm)) {
activity.toast(R.string.unknown_error_occurred)
}
}
activity.config.alarmLastConfig = alarm
callback(alarmId)
alertDialog.dismiss()
} else {
activity.toast(R.string.no_post_notifications_permissions)
}
}
activity.config.alarmLastConfig = alarm
callback(alarmId)
alertDialog.dismiss()
}
}
}

View File

@ -122,14 +122,20 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
}
override fun alarmToggled(id: Int, isEnabled: Boolean) {
if (requireContext().dbHelper.updateAlarmEnabledState(id, isEnabled)) {
val alarm = alarms.firstOrNull { it.id == id } ?: return
alarm.isEnabled = isEnabled
checkAlarmState(alarm)
} else {
requireActivity().toast(R.string.unknown_error_occurred)
(activity as SimpleActivity).handleNotificationPermission {
if (it) {
if (requireContext().dbHelper.updateAlarmEnabledState(id, isEnabled)) {
val alarm = alarms.firstOrNull { it.id == id } ?: return@handleNotificationPermission
alarm.isEnabled = isEnabled
checkAlarmState(alarm)
} else {
requireActivity().toast(R.string.unknown_error_occurred)
}
requireContext().updateWidgets()
} else {
activity?.toast(R.string.no_post_notifications_permissions)
}
}
requireContext().updateWidgets()
}
private fun checkAlarmState(alarm: Alarm) {

View File

@ -119,10 +119,15 @@ class StopwatchFragment : Fragment() {
}
private fun togglePlayPause() {
Stopwatch.toggle(true)
(activity as SimpleActivity).handleNotificationPermission {
if (it) {
Stopwatch.toggle(true)
} else {
activity?.toast(R.string.no_post_notifications_permissions)
}
}
}
private fun updateDisplayedText(totalTime: Long, lapTime: Long, useLongerMSFormat: Boolean) {
view.stopwatch_time.text = totalTime.formatStopwatchTime(useLongerMSFormat)
if (Stopwatch.laps.isNotEmpty() && lapTime != -1L) {