Skip to content

Commit

Permalink
Random Cleanup (#8592)
Browse files Browse the repository at this point in the history
* Consts and helper functions in NotificationHelper.kt

* remove dead code in RandomId.kt
  • Loading branch information
enzowritescode authored and Willyfrog committed Feb 19, 2025
1 parent 10b5bfe commit 06043e9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
4 changes: 0 additions & 4 deletions android/app/src/main/java/com/mattermost/helpers/RandomId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}

Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -226,19 +235,19 @@ 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)
}
}
}

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)

Expand All @@ -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
Expand Down

0 comments on commit 06043e9

Please sign in to comment.