Commit da3d16cd authored by Leonardo Aramaki's avatar Leonardo Aramaki

More refactoring

parent 855cc652
...@@ -40,7 +40,7 @@ import java.util.concurrent.atomic.AtomicInteger ...@@ -40,7 +40,7 @@ import java.util.concurrent.atomic.AtomicInteger
import kotlin.collections.HashMap import kotlin.collections.HashMap
typealias TupleRoomUser = Pair<Room, User> typealias TupleRoomUser = Pair<Room, User>
typealias TupleIntAtomicInt = Pair<Int, AtomicInteger> typealias TupleGroupIdMessageCount = Pair<Int, AtomicInteger>
object PushManager { object PushManager {
const val REPLY_LABEL = "REPLY" const val REPLY_LABEL = "REPLY"
...@@ -50,7 +50,7 @@ object PushManager { ...@@ -50,7 +50,7 @@ object PushManager {
private val messageStack = SparseArray<ArrayList<CharSequence>>() private val messageStack = SparseArray<ArrayList<CharSequence>>()
// Notifications received from the same server are grouped in a single bundled notification. // Notifications received from the same server are grouped in a single bundled notification.
// This map associates a host to a group id. // This map associates a host to a group id.
private val groupMap = HashMap<String, TupleIntAtomicInt>() private val groupMap = HashMap<String, TupleGroupIdMessageCount>()
private val randomizer = Random() private val randomizer = Random()
/** /**
...@@ -98,14 +98,14 @@ object PushManager { ...@@ -98,14 +98,14 @@ object PushManager {
val notification: Notification val notification: Notification
if (isAndroidVersionAtLeast(Build.VERSION_CODES.O)) { if (isAndroidVersionAtLeast(Build.VERSION_CODES.O)) {
notification = createNotificationForOreoAndAbove(context, pushMessage) notification = createNotificationForOreoAndAbove(context, pushMessage)
if (groupTuple != null) { groupTuple?.let {
notificationManager.notify(groupTuple.first, groupNotification) notificationManager.notify(groupTuple.first, groupNotification)
groupTuple.second.incrementAndGet() groupTuple.second.incrementAndGet()
} }
notificationManager.notify(notId, notification) notificationManager.notify(notId, notification)
} else { } else {
notification = createCompatNotification(context, pushMessage) notification = createCompatNotification(context, pushMessage)
if (groupTuple != null) { groupTuple?.let {
NotificationManagerCompat.from(context).notify(groupTuple.first, groupNotification) NotificationManagerCompat.from(context).notify(groupTuple.first, groupNotification)
groupTuple.second.incrementAndGet() groupTuple.second.incrementAndGet()
} }
...@@ -117,8 +117,8 @@ object PushManager { ...@@ -117,8 +117,8 @@ object PushManager {
private fun bundleNotificationsToHost(host: String) { private fun bundleNotificationsToHost(host: String) {
val size = groupMap.size val size = groupMap.size
if (groupMap.get(host) == null) { groupMap.get(host)?.let {
groupMap.put(host, TupleIntAtomicInt(size + 1, AtomicInteger(0))) groupMap.put(host, TupleGroupIdMessageCount(size + 1, AtomicInteger(0)))
} }
} }
...@@ -154,8 +154,8 @@ object PushManager { ...@@ -154,8 +154,8 @@ object PushManager {
.setSummaryText(summary) .setSummaryText(summary)
notGroupBuilder.setStyle(inbox) notGroupBuilder.setStyle(inbox)
} else{ } else {
val bigText = NotificationCompat.BigTextStyle() val bigText = NotificationCompat.BigTextStyle()
.bigText(pushMessage.message.fromHtml()) .bigText(pushMessage.message.fromHtml())
.setBigContentTitle(pushMessage.title.fromHtml()) .setBigContentTitle(pushMessage.title.fromHtml())
...@@ -308,7 +308,7 @@ object PushManager { ...@@ -308,7 +308,7 @@ object PushManager {
} }
// CharSequence extensions // CharSequence extensions
fun CharSequence.fromHtml(): Spanned { private fun CharSequence.fromHtml(): Spanned {
return Html.fromHtml(this as String) return Html.fromHtml(this as String)
} }
...@@ -333,13 +333,14 @@ object PushManager { ...@@ -333,13 +333,14 @@ object PushManager {
} }
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
fun Notification.Builder.setMessageNotification(ctx: Context): Notification.Builder { private fun Notification.Builder.setMessageNotification(ctx: Context): Notification.Builder {
val res = ctx.resources val res = ctx.resources
val smallIcon = res.getIdentifier( val smallIcon = res.getIdentifier(
"rocket_chat_notification", "drawable", ctx.packageName) "rocket_chat_notification", "drawable", ctx.packageName)
with(this, { with(this, {
setAutoCancel(true) setAutoCancel(true)
setShowWhen(true) setShowWhen(true)
setColor(res.getColor(R.color.colorRed400, ctx.theme))
setDefaults(Notification.DEFAULT_ALL) setDefaults(Notification.DEFAULT_ALL)
setSmallIcon(smallIcon) setSmallIcon(smallIcon)
}) })
...@@ -366,7 +367,7 @@ object PushManager { ...@@ -366,7 +367,7 @@ object PushManager {
return this return this
} }
fun NotificationCompat.Builder.setMessageNotification(): NotificationCompat.Builder { private fun NotificationCompat.Builder.setMessageNotification(): NotificationCompat.Builder {
val ctx = this.mContext val ctx = this.mContext
val res = ctx.resources val res = ctx.resources
val smallIcon = res.getIdentifier( val smallIcon = res.getIdentifier(
...@@ -374,6 +375,7 @@ object PushManager { ...@@ -374,6 +375,7 @@ object PushManager {
with(this, { with(this, {
setAutoCancel(true) setAutoCancel(true)
setShowWhen(true) setShowWhen(true)
setColor(ctx.resources.getColor(R.color.colorRed400))
setDefaults(Notification.DEFAULT_ALL) setDefaults(Notification.DEFAULT_ALL)
setSmallIcon(smallIcon) setSmallIcon(smallIcon)
}) })
...@@ -419,6 +421,9 @@ object PushManager { ...@@ -419,6 +421,9 @@ object PushManager {
} }
} }
/**
* BroadcastReceiver for dismissed notifications.
*/
class DeleteReceiver : BroadcastReceiver() { class DeleteReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
val notificationId = intent?.extras?.getInt("notId") val notificationId = intent?.extras?.getInt("notId")
...@@ -428,7 +433,11 @@ object PushManager { ...@@ -428,7 +433,11 @@ object PushManager {
} }
} }
// EXPERIMENTAL /**
* *EXPERIMENTAL*
*
* BroadcastReceiver for notifications' replies.
*/
class ReplyReceiver : BroadcastReceiver() { class ReplyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment