Minor changes

This commit is contained in:
Philipp Heckel 2022-12-06 21:42:09 -05:00
parent 6f0bf4d112
commit e43720036a
2 changed files with 30 additions and 41 deletions

View file

@ -51,21 +51,14 @@ class NotificationService(val context: Context) {
fun cancel(notificationId: Int) { fun cancel(notificationId: Int) {
if (notificationId != 0) { if (notificationId != 0) {
Log.d(TAG, "Cancelling notification ${notificationId}") Log.d(TAG, "Cancelling notification $notificationId")
notificationManager.cancel(notificationId) notificationManager.cancel(notificationId)
} }
} }
fun createDefaultNotificationChannels() { fun createDefaultNotificationChannels() {
maybeCreateNotificationGroup(DEFAULT_NOTIFICATION_SCOPE, context.getString(R.string.channel_notifications_group_default_name)) maybeCreateNotificationGroup(DEFAULT_SCOPE, context.getString(R.string.channel_notifications_group_default_name))
(1..5).forEach { priority -> maybeCreateNotificationChannel(DEFAULT_SCOPE, priority,DEFAULT_GROUP) }
(1..5).forEach { priority ->
maybeCreateNotificationChannel(
DEFAULT_NOTIFICATION_SCOPE,
priority,
DEFAULT_NOTIFICATION_SCOPE // use default scope as group id
)
}
} }
fun createSubscriptionNotificationChannels(subscription: Subscription) { fun createSubscriptionNotificationChannels(subscription: Subscription) {
@ -73,7 +66,6 @@ class NotificationService(val context: Context) {
val groupId = dedicatedGroupId(subscription) val groupId = dedicatedGroupId(subscription)
maybeCreateNotificationGroup(groupId, subscriptionTopicShortUrl(subscription)) maybeCreateNotificationGroup(groupId, subscriptionTopicShortUrl(subscription))
(1..5).forEach { priority -> maybeCreateNotificationChannel(notificationScope, priority, groupId) } (1..5).forEach { priority -> maybeCreateNotificationChannel(notificationScope, priority, groupId) }
} }
@ -82,21 +74,20 @@ class NotificationService(val context: Context) {
val groupId = dedicatedGroupId(subscription) val groupId = dedicatedGroupId(subscription)
(1..5).forEach { priority -> maybeDeleteNotificationChannel(notificationScope, priority) } (1..5).forEach { priority -> maybeDeleteNotificationChannel(notificationScope, priority) }
maybeDeleteNotificationGroup(groupId) maybeDeleteNotificationGroup(groupId)
} }
fun dedicatedNotificationScope(subscription: Subscription): String { private fun dedicatedNotificationScope(subscription: Subscription): String {
return "" + subscription.id return subscription.id.toString()
} }
fun dedicatedGroupId(subscription: Subscription): String { private fun dedicatedGroupId(subscription: Subscription): String {
return "" + subscription.id return subscription.id.toString()
} }
private fun displayInternal(subscription: Subscription, notification: Notification, update: Boolean = false) { private fun displayInternal(subscription: Subscription, notification: Notification, update: Boolean = false) {
val title = formatTitle(subscription, notification) val title = formatTitle(subscription, notification)
val scope = if (subscription.dedicatedChannels) dedicatedNotificationScope(subscription) else DEFAULT_NOTIFICATION_SCOPE val scope = if (subscription.dedicatedChannels) dedicatedNotificationScope(subscription) else DEFAULT_SCOPE
val groupId = if (subscription.dedicatedChannels) dedicatedGroupId(subscription) else null val groupId = if (subscription.dedicatedChannels) dedicatedGroupId(subscription) else null
val builder = NotificationCompat.Builder(context, toChannelId(scope, notification.priority)) val builder = NotificationCompat.Builder(context, toChannelId(scope, notification.priority))
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
@ -117,7 +108,6 @@ class NotificationService(val context: Context) {
if (groupId != null) { if (groupId != null) {
maybeCreateNotificationGroup(groupId, subscriptionTopicShortUrl(subscription)) maybeCreateNotificationGroup(groupId, subscriptionTopicShortUrl(subscription))
} }
maybeCreateNotificationChannel(scope, notification.priority, groupId) maybeCreateNotificationChannel(scope, notification.priority, groupId)
notificationManager.notify(notification.notificationId, builder.build()) notificationManager.notify(notification.notificationId, builder.build())
@ -389,11 +379,9 @@ class NotificationService(val context: Context) {
} }
else -> NotificationChannel(channelId, context.getString(R.string.channel_notifications_default_name), NotificationManager.IMPORTANCE_DEFAULT) else -> NotificationChannel(channelId, context.getString(R.string.channel_notifications_default_name), NotificationManager.IMPORTANCE_DEFAULT)
} }
if (groupId != null) { if (groupId != null) {
channel.setGroup(groupId) channel.group = groupId
} }
notificationManager.createNotificationChannel(channel) notificationManager.createNotificationChannel(channel)
} }
} }
@ -416,13 +404,13 @@ class NotificationService(val context: Context) {
} }
} }
fun toChannelId(scope: String, priority: Int): String { private fun toChannelId(scope: String, priority: Int): String {
return when (priority) { return when (priority) {
1 -> scope + PRIORITY_MIN 1 -> scope + SCOPE_SUFFIX_PRIORITY_MIN
2 -> scope + PRIORITY_LOW 2 -> scope + SCOPE_SUFFIX_PRIORITY_LOW
4 -> scope + PRIORITY_HIGH 4 -> scope + SCOPE_SUFFIX_PRIORITY_HIGH
5 -> scope + PRIORITY_MAX 5 -> scope + SCOPE_SUFFIX_PRIORITY_MAX
else -> scope + PRIORITY_DEFAULT else -> scope + SCOPE_SUFFIX_PRIORITY_DEFAULT
} }
} }
@ -482,13 +470,14 @@ class NotificationService(val context: Context) {
private const val TAG = "NtfyNotifService" private const val TAG = "NtfyNotifService"
private const val DEFAULT_NOTIFICATION_SCOPE = "ntfy" private const val DEFAULT_SCOPE = "ntfy"
private const val DEFAULT_GROUP = "ntfy"
private const val PRIORITY_MIN = "-min" private const val SCOPE_SUFFIX_PRIORITY_MIN = "-min"
private const val PRIORITY_LOW = "-low" private const val SCOPE_SUFFIX_PRIORITY_LOW = "-low"
private const val PRIORITY_DEFAULT = "" private const val SCOPE_SUFFIX_PRIORITY_DEFAULT = ""
private const val PRIORITY_HIGH = "-high" private const val SCOPE_SUFFIX_PRIORITY_HIGH = "-high"
private const val PRIORITY_MAX = "-max" private const val SCOPE_SUFFIX_PRIORITY_MAX = "-max"
private const val VIEW_ACTION_EXTRA_URL = "url" private const val VIEW_ACTION_EXTRA_URL = "url"
private const val VIEW_ACTION_EXTRA_NOTIFICATION_ID = "notificationId" private const val VIEW_ACTION_EXTRA_NOTIFICATION_ID = "notificationId"

View file

@ -14,14 +14,6 @@
app:entryValues="@array/settings_notifications_muted_until_values" app:entryValues="@array/settings_notifications_muted_until_values"
app:defaultValue="0" app:defaultValue="0"
app:isPreferenceVisible="false"/> app:isPreferenceVisible="false"/>
<SwitchPreference
app:key="@string/detail_settings_notifications_dedicated_channels_key"
app:title="@string/detail_settings_notifications_dedicated_channels_title"
app:isPreferenceVisible="false"/>
<Preference
app:key="@string/detail_settings_notifications_open_channels_key"
app:title="@string/detail_settings_notifications_open_channels_title"
app:isPreferenceVisible="false"/>
<ListPreference <ListPreference
app:key="@string/detail_settings_notifications_min_priority_key" app:key="@string/detail_settings_notifications_min_priority_key"
app:title="@string/settings_notifications_min_priority_title" app:title="@string/settings_notifications_min_priority_title"
@ -36,6 +28,14 @@
app:entryValues="@array/detail_settings_notifications_auto_delete_values" app:entryValues="@array/detail_settings_notifications_auto_delete_values"
app:defaultValue="-1" app:defaultValue="-1"
app:isPreferenceVisible="false"/> <!-- Same as Repository.AUTO_DELETE_USE_GLOBAL --> app:isPreferenceVisible="false"/> <!-- Same as Repository.AUTO_DELETE_USE_GLOBAL -->
<SwitchPreference
app:key="@string/detail_settings_notifications_dedicated_channels_key"
app:title="@string/detail_settings_notifications_dedicated_channels_title"
app:isPreferenceVisible="false"/>
<Preference
app:key="@string/detail_settings_notifications_open_channels_key"
app:title="@string/detail_settings_notifications_open_channels_title"
app:isPreferenceVisible="false"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
app:key="@string/detail_settings_appearance_header_key" app:key="@string/detail_settings_appearance_header_key"