Random Cleanup (#8592)

* Consts and helper functions in NotificationHelper.kt

* remove dead code in RandomId.kt
This commit is contained in:
enzowritescode 2025-02-17 08:23:19 -07:00 committed by GitHub
parent f675761f34
commit e8d72d8a8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 29 deletions

View file

@ -4,10 +4,6 @@ import java.util.UUID
class RandomId {
companion object {
private const val alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
private const val alphabetLength = alphabet.length
private const val idLength = 16
fun generate(): String {
return UUID.randomUUID().toString()
}

View file

@ -14,6 +14,13 @@ object NotificationHelper {
private const val NOTIFICATIONS_IN_GROUP: String = "notificationsInGroup"
private const val VERSION_PREFERENCE = "VERSION_PREFERENCE"
const val MESSAGE_NOTIFICATION_ID: Int = 435345
private const val KEY_ROOT_ID = "root_id"
private const val KEY_CHANNEL_ID = "channel_id"
private const val KEY_POST_ID = "post_id"
private const val KEY_IS_CRT_ENABLED = "is_crt_enabled"
private const val KEY_SERVER_URL = "server_url"
private const val PREF_VERSION = "Version"
fun cleanNotificationPreferencesIfNeeded(context: Context) {
try {
@ -22,13 +29,13 @@ object NotificationHelper {
var storedVersion: String? = null
val pSharedPref = context.getSharedPreferences(VERSION_PREFERENCE, Context.MODE_PRIVATE)
if (pSharedPref != null) {
storedVersion = pSharedPref.getString("Version", "")
storedVersion = pSharedPref.getString(PREF_VERSION, "")
}
if (version != storedVersion) {
if (pSharedPref != null) {
val editor = pSharedPref.edit()
editor.putString("Version", version)
editor.putString(PREF_VERSION, version)
editor.apply()
}
@ -41,8 +48,8 @@ object NotificationHelper {
}
fun getNotificationId(notification: Bundle): Int {
val postId = notification.getString("post_id")
val channelId = notification.getString("channel_id")
val postId = getPostId(notification)
val channelId = getChannelId(notification)
var notificationId: Int = MESSAGE_NOTIFICATION_ID
if (postId != null) {
@ -62,10 +69,10 @@ object NotificationHelper {
fun addNotificationToPreferences(context: Context, notificationId: Int, notification: Bundle): Boolean {
try {
var createSummary = true
val serverUrl = notification.getString("server_url")
val channelId = notification.getString("channel_id")
val rootId = notification.getString("root_id")
val isCRTEnabled = notification.containsKey("is_crt_enabled") && notification.getString("is_crt_enabled") == "true"
val serverUrl = getServerUrl(notification)
val channelId = getChannelId(notification)
val rootId = getRootId(notification)
val isCRTEnabled = isCRTEnabled(notification)
val isThreadNotification = isCRTEnabled && !TextUtils.isEmpty(rootId)
val groupId = if (isThreadNotification) rootId else channelId
@ -103,10 +110,10 @@ object NotificationHelper {
}
fun dismissNotification(context: Context, notification: Bundle) {
val isCRTEnabled = notification.containsKey("is_crt_enabled") && notification.getString("is_crt_enabled") == "true"
val serverUrl = notification.getString("server_url")
val channelId = notification.getString("channel_id")
val rootId = notification.getString("root_id")
val isCRTEnabled = isCRTEnabled(notification)
val serverUrl = getServerUrl(notification)
val channelId = getChannelId(notification)
val rootId = getRootId(notification)
val notificationId = getNotificationId(notification)
@ -130,9 +137,9 @@ object NotificationHelper {
for (status in statusNotifications) {
val bundle = status.notification.extras
hasMore = if (isThreadNotification) {
bundle.containsKey("root_id") && bundle.getString("root_id") == rootId
bundle.containsKey(KEY_ROOT_ID) && getRootId(bundle) == rootId
} else {
bundle.containsKey("channel_id") && bundle.getString("channel_id") == channelId
bundle.containsKey(KEY_CHANNEL_ID) && getChannelId(bundle) == channelId
}
if (hasMore) break
}
@ -167,9 +174,9 @@ object NotificationHelper {
for (sbn in notifications) {
val n = sbn.notification
val bundle = n.extras
val cId = bundle.getString("channel_id")
val rootId = bundle.getString("root_id")
val isCRTEnabled = bundle.containsKey("is_crt_enabled") && bundle.getString("is_crt_enabled") == "true"
val cId = getChannelId(bundle)
val rootId = getRootId(bundle)
val isCRTEnabled = isCRTEnabled(bundle)
val skipThreadNotification = isCRTEnabled && !TextUtils.isEmpty(rootId)
if (cId == channelId && !skipThreadNotification) {
notificationManager.cancel(sbn.id)
@ -186,14 +193,14 @@ object NotificationHelper {
for (sbn in notifications) {
val n = sbn.notification
val bundle = n.extras
val rootId = bundle.getString("root_id")
val postId = bundle.getString("post_id")
val rootId = getRootId(bundle)
val postId = getPostId(bundle)
if (rootId == threadId) {
notificationManager.cancel(sbn.id)
}
if (postId == threadId) {
val channelId = bundle.getString("channel_id")
val channelId = getChannelId(bundle)
val id = sbn.id
if (notificationsInServer != null && channelId != null) {
val notificationsInChannel = notificationsInServer.optJSONObject(channelId)
@ -217,6 +224,8 @@ object NotificationHelper {
}
}
private fun getPostId(notification: Bundle) = notification.getString(KEY_POST_ID)
fun removeServerNotifications(context: Context, serverUrl: String) {
val notificationManager = NotificationManagerCompat.from(context)
val notificationsPerServer = loadMap(context)
@ -226,7 +235,7 @@ object NotificationHelper {
for (sbn in notifications) {
val n = sbn.notification
val bundle = n.extras
val url = bundle.getString("server_url")
val url = getServerUrl(bundle)
if (url == serverUrl) {
notificationManager.cancel(sbn.id)
}
@ -234,11 +243,11 @@ object NotificationHelper {
}
fun clearChannelOrThreadNotifications(context: Context, notification: Bundle) {
val serverUrl = notification.getString("server_url")
val channelId = notification.getString("channel_id")
val rootId = notification.getString("root_id")
val serverUrl = getServerUrl(notification)
val channelId = getChannelId(notification)
val rootId = getRootId(notification)
if (channelId != null) {
val isCRTEnabled = notification.containsKey("is_crt_enabled") && notification.getString("is_crt_enabled") == "true"
val isCRTEnabled = isCRTEnabled(notification)
// rootId is available only when CRT is enabled & clearing the thread
val isClearThread = isCRTEnabled && !TextUtils.isEmpty(rootId)
@ -250,6 +259,15 @@ object NotificationHelper {
}
}
private fun getRootId(notification: Bundle) = notification.getString(KEY_ROOT_ID)
private fun getChannelId(notification: Bundle) = notification.getString(KEY_CHANNEL_ID)
private fun getServerUrl(notification: Bundle) = notification.getString(KEY_SERVER_URL)
private fun isCRTEnabled(bundle: Bundle) =
bundle.containsKey(KEY_IS_CRT_ENABLED) && bundle.getString(KEY_IS_CRT_ENABLED) == "true"
/**
* Map Structure