Minor code improvement

This commit is contained in:
Naveen 2024-01-17 18:55:36 +05:30
parent 9e9eebc5ff
commit 1d675a1be9
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
3 changed files with 14 additions and 15 deletions

View file

@ -97,7 +97,7 @@ class ConversationDetailsActivity : SimpleActivity() {
customNotificationsButton.setOnClickListener { customNotificationsButton.setOnClickListener {
Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply { Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
putExtra(Settings.EXTRA_APP_PACKAGE, packageName) putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
putExtra(Settings.EXTRA_CHANNEL_ID, threadId.hashCode().toString()) putExtra(Settings.EXTRA_CHANNEL_ID, threadId.toString())
startActivity(this) startActivity(this)
} }
} }
@ -113,7 +113,7 @@ class ConversationDetailsActivity : SimpleActivity() {
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION) .setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
.build() .build()
NotificationChannel(threadId.hashCode().toString(), name, NotificationManager.IMPORTANCE_HIGH).apply { NotificationChannel(threadId.toString(), name, NotificationManager.IMPORTANCE_HIGH).apply {
setBypassDnd(false) setBypassDnd(false)
enableLights(true) enableLights(true)
setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes) setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), audioAttributes)
@ -124,7 +124,7 @@ class ConversationDetailsActivity : SimpleActivity() {
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
private fun removeNotificationChannel() { private fun removeNotificationChannel() {
notificationManager.deleteNotificationChannel(threadId.hashCode().toString()) notificationManager.deleteNotificationChannel(threadId.toString())
} }
private fun setupTextViews() { private fun setupTextViews() {

View file

@ -694,7 +694,7 @@ fun Context.deleteConversation(threadId: Long) {
if (config.customNotifications.contains(threadId.toString()) && isOreoPlus()) { if (config.customNotifications.contains(threadId.toString()) && isOreoPlus()) {
config.removeCustomNotificationsByThreadId(threadId) config.removeCustomNotificationsByThreadId(threadId)
notificationManager.deleteNotificationChannel(threadId.hashCode().toString()) notificationManager.deleteNotificationChannel(threadId.toString())
} }
} }

View file

@ -45,14 +45,13 @@ class NotificationHelper(private val context: Context) {
sender: String?, sender: String?,
alertOnlyOnce: Boolean = false alertOnlyOnce: Boolean = false
) { ) {
val notificationId = threadId.hashCode()
val hasCustomNotifications = context.config.customNotifications.contains(threadId.toString()) val hasCustomNotifications = context.config.customNotifications.contains(threadId.toString())
val notificationChannel = if (hasCustomNotifications) notificationId.toString() else NOTIFICATION_CHANNEL val notificationChannelId = if (hasCustomNotifications) threadId.toString() else NOTIFICATION_CHANNEL
if (!hasCustomNotifications) { if (!hasCustomNotifications) {
maybeCreateChannel(notificationChannel, context.getString(R.string.channel_received_sms)) maybeCreateChannel(notificationChannelId, context.getString(R.string.channel_received_sms))
} }
val notificationId = threadId.hashCode()
val contentIntent = Intent(context, ThreadActivity::class.java).apply { val contentIntent = Intent(context, ThreadActivity::class.java).apply {
putExtra(THREAD_ID, threadId) putExtra(THREAD_ID, threadId)
} }
@ -103,7 +102,7 @@ class NotificationHelper(private val context: Context) {
} else { } else {
null null
} }
val builder = NotificationCompat.Builder(context, notificationChannel).apply { val builder = NotificationCompat.Builder(context, notificationChannelId).apply {
when (context.config.lockScreenVisibilitySetting) { when (context.config.lockScreenVisibilitySetting) {
LOCK_SCREEN_SENDER_MESSAGE -> { LOCK_SCREEN_SENDER_MESSAGE -> {
setLargeIcon(largeIcon) setLargeIcon(largeIcon)
@ -134,13 +133,13 @@ class NotificationHelper(private val context: Context) {
} }
builder.addAction(org.fossify.commons.R.drawable.ic_check_vector, context.getString(R.string.mark_as_read), markAsReadPendingIntent) builder.addAction(org.fossify.commons.R.drawable.ic_check_vector, context.getString(R.string.mark_as_read), markAsReadPendingIntent)
.setChannelId(notificationChannel) .setChannelId(notificationChannelId)
if (isNoReplySms) { if (isNoReplySms) {
builder.addAction( builder.addAction(
org.fossify.commons.R.drawable.ic_delete_vector, org.fossify.commons.R.drawable.ic_delete_vector,
context.getString(org.fossify.commons.R.string.delete), context.getString(org.fossify.commons.R.string.delete),
deleteSmsPendingIntent deleteSmsPendingIntent
).setChannelId(notificationChannel) ).setChannelId(notificationChannelId)
} }
notificationManager.notify(notificationId, builder.build()) notificationManager.notify(notificationId, builder.build())
} }
@ -148,9 +147,9 @@ class NotificationHelper(private val context: Context) {
@SuppressLint("NewApi") @SuppressLint("NewApi")
fun showSendingFailedNotification(recipientName: String, threadId: Long) { fun showSendingFailedNotification(recipientName: String, threadId: Long) {
val hasCustomNotifications = context.config.customNotifications.contains(threadId.toString()) val hasCustomNotifications = context.config.customNotifications.contains(threadId.toString())
val notificationChannel = if (hasCustomNotifications) threadId.hashCode().toString() else NOTIFICATION_CHANNEL val notificationChannelId = if (hasCustomNotifications) threadId.toString() else NOTIFICATION_CHANNEL
if (!hasCustomNotifications) { if (!hasCustomNotifications) {
maybeCreateChannel(notificationChannel, context.getString(R.string.message_not_sent_short)) maybeCreateChannel(notificationChannelId, context.getString(R.string.message_not_sent_short))
} }
val notificationId = generateRandomId().hashCode() val notificationId = generateRandomId().hashCode()
@ -161,7 +160,7 @@ class NotificationHelper(private val context: Context) {
val summaryText = String.format(context.getString(R.string.message_sending_error), recipientName) val summaryText = String.format(context.getString(R.string.message_sending_error), recipientName)
val largeIcon = SimpleContactsHelper(context).getContactLetterIcon(recipientName) val largeIcon = SimpleContactsHelper(context).getContactLetterIcon(recipientName)
val builder = NotificationCompat.Builder(context, notificationChannel) val builder = NotificationCompat.Builder(context, notificationChannelId)
.setContentTitle(context.getString(R.string.message_not_sent_short)) .setContentTitle(context.getString(R.string.message_not_sent_short))
.setContentText(summaryText) .setContentText(summaryText)
.setColor(context.getProperPrimaryColor()) .setColor(context.getProperPrimaryColor())
@ -173,7 +172,7 @@ class NotificationHelper(private val context: Context) {
.setDefaults(Notification.DEFAULT_LIGHTS) .setDefaults(Notification.DEFAULT_LIGHTS)
.setCategory(Notification.CATEGORY_MESSAGE) .setCategory(Notification.CATEGORY_MESSAGE)
.setAutoCancel(true) .setAutoCancel(true)
.setChannelId(notificationChannel) .setChannelId(notificationChannelId)
notificationManager.notify(notificationId, builder.build()) notificationManager.notify(notificationId, builder.build())
} }