Remove "scope"; use "displayName"

This commit is contained in:
Philipp Heckel 2022-12-06 22:06:59 -05:00
parent e43720036a
commit f327c8ffdd
4 changed files with 42 additions and 53 deletions

View file

@ -57,39 +57,34 @@ class NotificationService(val context: Context) {
} }
fun createDefaultNotificationChannels() { fun createDefaultNotificationChannels() {
maybeCreateNotificationGroup(DEFAULT_SCOPE, context.getString(R.string.channel_notifications_group_default_name)) maybeCreateNotificationGroup(DEFAULT_GROUP, 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_GROUP, priority) }
} }
fun createSubscriptionNotificationChannels(subscription: Subscription) { fun createSubscriptionNotificationChannels(subscription: Subscription) {
val notificationScope = dedicatedNotificationScope(subscription) val groupId = subscriptionGroupId(subscription)
val groupId = dedicatedGroupId(subscription) maybeCreateNotificationGroup(groupId, subscriptionGroupName(subscription))
(1..5).forEach { priority -> maybeCreateNotificationChannel(groupId, priority) }
maybeCreateNotificationGroup(groupId, subscriptionTopicShortUrl(subscription))
(1..5).forEach { priority -> maybeCreateNotificationChannel(notificationScope, priority, groupId) }
} }
fun deleteSubscriptionNotificationChannels(subscription: Subscription) { fun deleteSubscriptionNotificationChannels(subscription: Subscription) {
val notificationScope = dedicatedNotificationScope(subscription) val groupId = subscriptionGroupId(subscription)
val groupId = dedicatedGroupId(subscription) (1..5).forEach { priority -> maybeDeleteNotificationChannel(groupId, priority) }
(1..5).forEach { priority -> maybeDeleteNotificationChannel(notificationScope, priority) }
maybeDeleteNotificationGroup(groupId) maybeDeleteNotificationGroup(groupId)
} }
private fun dedicatedNotificationScope(subscription: Subscription): String { private fun subscriptionGroupId(subscription: Subscription): String {
return subscription.id.toString() return subscription.id.toString()
} }
private fun dedicatedGroupId(subscription: Subscription): String { private fun subscriptionGroupName(subscription: Subscription): String {
return subscription.id.toString() return subscription.displayName ?: subscriptionTopicShortUrl(subscription)
} }
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_SCOPE val groupId = if (subscription.dedicatedChannels) subscriptionGroupId(subscription) else DEFAULT_GROUP
val groupId = if (subscription.dedicatedChannels) dedicatedGroupId(subscription) else null val builder = NotificationCompat.Builder(context, toChannelId(groupId, notification.priority))
val builder = NotificationCompat.Builder(context, toChannelId(scope, notification.priority))
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setColor(ContextCompat.getColor(context, Colors.notificationIcon(context))) .setColor(ContextCompat.getColor(context, Colors.notificationIcon(context)))
.setContentTitle(title) .setContentTitle(title)
@ -105,10 +100,8 @@ class NotificationService(val context: Context) {
maybeAddCancelAction(builder, notification) maybeAddCancelAction(builder, notification)
maybeAddUserActions(builder, notification) maybeAddUserActions(builder, notification)
if (groupId != null) { maybeCreateNotificationGroup(groupId, subscriptionGroupName(subscription))
maybeCreateNotificationGroup(groupId, subscriptionTopicShortUrl(subscription)) maybeCreateNotificationChannel(groupId, notification.priority)
}
maybeCreateNotificationChannel(scope, notification.priority, groupId)
notificationManager.notify(notification.notificationId, builder.build()) notificationManager.notify(notification.notificationId, builder.build())
} }
@ -345,11 +338,11 @@ class NotificationService(val context: Context) {
} }
} }
private fun maybeCreateNotificationChannel(scope: String, priority: Int, groupId: String?) { private fun maybeCreateNotificationChannel(group: String, priority: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Note: To change a notification channel, you must delete the old one and create a new one! // Note: To change a notification channel, you must delete the old one and create a new one!
val channelId = toChannelId(scope, priority) val channelId = toChannelId(group, priority)
val pause = 300L val pause = 300L
val channel = when (priority) { val channel = when (priority) {
1 -> NotificationChannel(channelId, context.getString(R.string.channel_notifications_min_name), NotificationManager.IMPORTANCE_MIN) 1 -> NotificationChannel(channelId, context.getString(R.string.channel_notifications_min_name), NotificationManager.IMPORTANCE_MIN)
@ -379,16 +372,14 @@ 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) { channel.group = group
channel.group = groupId
}
notificationManager.createNotificationChannel(channel) notificationManager.createNotificationChannel(channel)
} }
} }
private fun maybeDeleteNotificationChannel(scope: String, priority: Int) { private fun maybeDeleteNotificationChannel(group: String, priority: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.deleteNotificationChannel(toChannelId(scope, priority)) notificationManager.deleteNotificationChannel(toChannelId(group, priority))
} }
} }
@ -404,13 +395,13 @@ class NotificationService(val context: Context) {
} }
} }
private fun toChannelId(scope: String, priority: Int): String { private fun toChannelId(group: String, priority: Int): String {
return when (priority) { return when (priority) {
1 -> scope + SCOPE_SUFFIX_PRIORITY_MIN 1 -> group + GROUP_SUFFIX_PRIORITY_MIN
2 -> scope + SCOPE_SUFFIX_PRIORITY_LOW 2 -> group + GROUP_SUFFIX_PRIORITY_LOW
4 -> scope + SCOPE_SUFFIX_PRIORITY_HIGH 4 -> group + GROUP_SUFFIX_PRIORITY_HIGH
5 -> scope + SCOPE_SUFFIX_PRIORITY_MAX 5 -> group + GROUP_SUFFIX_PRIORITY_MAX
else -> scope + SCOPE_SUFFIX_PRIORITY_DEFAULT else -> group + GROUP_SUFFIX_PRIORITY_DEFAULT
} }
} }
@ -470,14 +461,12 @@ class NotificationService(val context: Context) {
private const val TAG = "NtfyNotifService" private const val TAG = "NtfyNotifService"
private const val DEFAULT_SCOPE = "ntfy"
private const val DEFAULT_GROUP = "ntfy" private const val DEFAULT_GROUP = "ntfy"
private const val GROUP_SUFFIX_PRIORITY_MIN = "-min"
private const val SCOPE_SUFFIX_PRIORITY_MIN = "-min" private const val GROUP_SUFFIX_PRIORITY_LOW = "-low"
private const val SCOPE_SUFFIX_PRIORITY_LOW = "-low" private const val GROUP_SUFFIX_PRIORITY_DEFAULT = ""
private const val SCOPE_SUFFIX_PRIORITY_DEFAULT = "" private const val GROUP_SUFFIX_PRIORITY_HIGH = "-high"
private const val SCOPE_SUFFIX_PRIORITY_HIGH = "-high" private const val GROUP_SUFFIX_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

@ -127,7 +127,6 @@ class DetailSettingsActivity : AppCompatActivity() {
val notificationsHeader: PreferenceCategory? = findPreference(notificationsHeaderId) val notificationsHeader: PreferenceCategory? = findPreference(notificationsHeaderId)
notificationsHeader?.isVisible = false notificationsHeader?.isVisible = false
} }
loadDisplayNamePref() loadDisplayNamePref()
loadTopicUrlPref() loadTopicUrlPref()
} }
@ -168,9 +167,7 @@ class DetailSettingsActivity : AppCompatActivity() {
} else { } else {
notificationService.deleteSubscriptionNotificationChannels(subscription) notificationService.deleteSubscriptionNotificationChannels(subscription)
} }
openChannelsPref.isVisible = value openChannelsPref.isVisible = value
} }
override fun getBoolean(key: String?, defValue: Boolean): Boolean { override fun getBoolean(key: String?, defValue: Boolean): Boolean {
return subscription.dedicatedChannels return subscription.dedicatedChannels
@ -178,9 +175,9 @@ class DetailSettingsActivity : AppCompatActivity() {
} }
pref?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { preference -> pref?.summaryProvider = Preference.SummaryProvider<SwitchPreference> { preference ->
if (preference.isChecked) { if (preference.isChecked) {
getString(R.string.detail_settings_notifications_dedicated_channels_summay_on) getString(R.string.detail_settings_notifications_dedicated_channels_summary_on)
} else { } else {
getString(R.string.detail_settings_notifications_dedicated_channels_summay_off) getString(R.string.detail_settings_notifications_dedicated_channels_summary_off)
} }
} }
} }
@ -349,16 +346,17 @@ class DetailSettingsActivity : AppCompatActivity() {
pref?.dialogMessage = getString(R.string.detail_settings_appearance_display_name_message, topicShortUrl(subscription.baseUrl, subscription.topic)) pref?.dialogMessage = getString(R.string.detail_settings_appearance_display_name_message, topicShortUrl(subscription.baseUrl, subscription.topic))
pref?.preferenceDataStore = object : PreferenceDataStore() { pref?.preferenceDataStore = object : PreferenceDataStore() {
override fun putString(key: String?, value: String?) { override fun putString(key: String?, value: String?) {
val displayName: String? = if (value == "") { val displayName = if (value != "") value else null
null
} else {
value
}
val newSubscription = subscription.copy(displayName = displayName) val newSubscription = subscription.copy(displayName = displayName)
save(newSubscription) save(newSubscription)
// Update activity title
activity?.runOnUiThread { activity?.runOnUiThread {
activity?.title = displayName(newSubscription) activity?.title = displayName(newSubscription)
} }
// Update dedicated notification channel
if (newSubscription.dedicatedChannels) {
notificationService.createSubscriptionNotificationChannels(newSubscription)
}
} }
override fun getString(key: String?, defValue: String?): String { override fun getString(key: String?, defValue: String?): String {
return subscription.displayName ?: "" return subscription.displayName ?: ""

View file

@ -351,9 +351,10 @@
<string name="detail_settings_notifications_instant_summary_on">Notifications are delivered instantly. Requires a foreground service and consumes more battery.</string> <string name="detail_settings_notifications_instant_summary_on">Notifications are delivered instantly. Requires a foreground service and consumes more battery.</string>
<string name="detail_settings_notifications_instant_summary_off">Notifications are delivered using Firebase. Delivery may be delayed, but consumes less battery.</string> <string name="detail_settings_notifications_instant_summary_off">Notifications are delivered using Firebase. Delivery may be delayed, but consumes less battery.</string>
<string name="detail_settings_notifications_dedicated_channels_title">Use dedicated channels</string> <string name="detail_settings_notifications_dedicated_channels_title">Use dedicated channels</string>
<string name="detail_settings_notifications_dedicated_channels_summay_on">Use dedicated notification channels for this subscription</string> <string name="detail_settings_notifications_dedicated_channels_summary_on">Use dedicated notification channels for this subscription</string>
<string name="detail_settings_notifications_dedicated_channels_summay_off">Use default notification channels</string> <string name="detail_settings_notifications_dedicated_channels_summary_off">Use default notification channels</string>
<string name="detail_settings_notifications_open_channels_title">Configure dedicated channels</string> <string name="detail_settings_notifications_open_channels_title">Configure dedicated channels</string>
<string name="detail_settings_notifications_open_channels_summary">Do Not Disturb (DND) override, sounds, etc.</string>
<string name="detail_settings_appearance_header">Appearance</string> <string name="detail_settings_appearance_header">Appearance</string>
<string name="detail_settings_appearance_icon_set_title">Subscription icon</string> <string name="detail_settings_appearance_icon_set_title">Subscription icon</string>
<string name="detail_settings_appearance_icon_set_summary">Set an icon to be displayed in notifications</string> <string name="detail_settings_appearance_icon_set_summary">Set an icon to be displayed in notifications</string>

View file

@ -35,6 +35,7 @@
<Preference <Preference
app:key="@string/detail_settings_notifications_open_channels_key" app:key="@string/detail_settings_notifications_open_channels_key"
app:title="@string/detail_settings_notifications_open_channels_title" app:title="@string/detail_settings_notifications_open_channels_title"
app:summary="@string/detail_settings_notifications_open_channels_summary"
app:isPreferenceVisible="false"/> app:isPreferenceVisible="false"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory