Commit a088ec8b authored by Leonardo Aramaki's avatar Leonardo Aramaki

Update notifications display on Oreo

parent 694084e3
...@@ -68,7 +68,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -68,7 +68,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
rocketChatCache.setSelectedRoomId(intent.getStringExtra(PushManager.EXTRA_ROOM_ID)); rocketChatCache.setSelectedRoomId(intent.getStringExtra(PushManager.EXTRA_ROOM_ID));
} }
} }
PushManager.INSTANCE.clearHostNotifications(hostname); PushManager.INSTANCE.clearNotificationsByHost(hostname);
} else { } else {
updateHostnameIfNeeded(rocketChatCache.getSelectedServerHostname()); updateHostnameIfNeeded(rocketChatCache.getSelectedServerHostname());
} }
...@@ -76,7 +76,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -76,7 +76,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
if (intent.hasExtra(PushManager.EXTRA_NOT_ID)) { if (intent.hasExtra(PushManager.EXTRA_NOT_ID)) {
isNotification = true; isNotification = true;
int notificationId = intent.getIntExtra(PushManager.EXTRA_NOT_ID, 0); int notificationId = intent.getIntExtra(PushManager.EXTRA_NOT_ID, 0);
PushManager.INSTANCE.clearNotificationIdStack(notificationId); PushManager.INSTANCE.clearNotificationsByNotificationId(notificationId);
} }
} }
......
...@@ -17,7 +17,6 @@ import android.support.v4.app.RemoteInput ...@@ -17,7 +17,6 @@ import android.support.v4.app.RemoteInput
import android.text.Html import android.text.Html
import android.text.Spanned import android.text.Spanned
import android.util.Log import android.util.Log
import android.util.SparseArray
import chat.rocket.android.BackgroundLooper import chat.rocket.android.BackgroundLooper
import chat.rocket.android.BuildConfig import chat.rocket.android.BuildConfig
import chat.rocket.android.R import chat.rocket.android.R
...@@ -51,10 +50,6 @@ object PushManager { ...@@ -51,10 +50,6 @@ object PushManager {
private const val REPLY_LABEL = "REPLY" private const val REPLY_LABEL = "REPLY"
private const val REMOTE_INPUT_REPLY = "REMOTE_INPUT_REPLY" private const val REMOTE_INPUT_REPLY = "REMOTE_INPUT_REPLY"
// Map associating a notification id to a list of corresponding messages ie. an id corresponds
// to a user and the corresponding list is all the messages sent by him.
private val messageStack = SparseArray<MutableList<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, TupleGroupIdMessageCount>() private val groupMap = HashMap<String, TupleGroupIdMessageCount>()
...@@ -85,20 +80,39 @@ object PushManager { ...@@ -85,20 +80,39 @@ object PushManager {
Log.d(PushMessage::class.java.simpleName, lastPushMessage.toString()) Log.d(PushMessage::class.java.simpleName, lastPushMessage.toString())
} }
bundleMessage(notId.toInt(), lastPushMessage.message)
showNotification(appContext, lastPushMessage) showNotification(appContext, lastPushMessage)
} }
/** /**
* Clear all messages corresponding to a specific notification id (aka specific user) * Clear all messages received to a given host the user is signed-in.
*/ */
fun clearNotificationIdStack(notificationId: Int) { fun clearNotificationsByHost(host: String) {
messageStack.delete(notificationId) hostToPushMessageList.remove(host)
} }
fun clearHostNotifications(host: String) { fun clearNotificationsByNotificationId(notificationId: Int) {
hostToPushMessageList.remove(host) if (hostToPushMessageList.isNotEmpty()) {
for (entry in hostToPushMessageList.entries) {
println("state: ${entry.value.size} -> ${entry.value}")
println("Removing from ${entry.key}")
entry.value.removeAll {
it.notificationId.toInt() == notificationId
}
println("state: ${entry.value.size} -> ${entry.value}")
}
}
}
private fun isAndroidVersionAtLeast(minVersion: Int) = Build.VERSION.SDK_INT >= minVersion
private fun getGroupForHost(host: String): TupleGroupIdMessageCount {
val size = groupMap.size
var group = groupMap.get(host)
if (group == null) {
group = TupleGroupIdMessageCount(size + 1, AtomicInteger(0))
groupMap.put(host, group)
}
return group
} }
private fun showNotification(context: Context, lastPushMessage: PushMessage) { private fun showNotification(context: Context, lastPushMessage: PushMessage) {
...@@ -110,19 +124,18 @@ object PushManager { ...@@ -110,19 +124,18 @@ object PushManager {
val groupTuple = getGroupForHost(host) val groupTuple = getGroupForHost(host)
groupTuple.second.incrementAndGet() groupTuple.second.incrementAndGet()
val notIdListForHostname: MutableList<PushMessage>? = hostToPushMessageList.get(host)
if (notIdListForHostname == null) {
hostToPushMessageList.put(host, arrayListOf(lastPushMessage))
} else {
notIdListForHostname.add(0, lastPushMessage)
}
if (isAndroidVersionAtLeast(Build.VERSION_CODES.O)) { if (isAndroidVersionAtLeast(Build.VERSION_CODES.O)) {
val notification = createSingleNotificationForOreo(context, lastPushMessage) val notification = createSingleNotificationForOreo(context, lastPushMessage)
val groupNotification = createGroupNotificationForOreo(context, lastPushMessage) val groupNotification = createGroupNotificationForOreo(context, lastPushMessage)
manager.notify(notId, notification) manager.notify(notId, notification)
manager.notify(groupTuple.first, groupNotification) manager.notify(groupTuple.first, groupNotification)
} else { } else {
val notIdListForHostname: MutableList<PushMessage>? = hostToPushMessageList.get(host)
if (notIdListForHostname == null) {
hostToPushMessageList.put(host, arrayListOf(lastPushMessage))
} else {
notIdListForHostname.add(lastPushMessage)
}
val notification = createSingleNotification(context, lastPushMessage) val notification = createSingleNotification(context, lastPushMessage)
val groupNotification = createGroupNotification(context, lastPushMessage) val groupNotification = createGroupNotification(context, lastPushMessage)
NotificationManagerCompat.from(context).notify(notId, notification) NotificationManagerCompat.from(context).notify(notId, notification)
...@@ -130,18 +143,6 @@ object PushManager { ...@@ -130,18 +143,6 @@ object PushManager {
} }
} }
private fun isAndroidVersionAtLeast(minVersion: Int) = Build.VERSION.SDK_INT >= minVersion
private fun getGroupForHost(host: String): TupleGroupIdMessageCount {
val size = groupMap.size
var group = groupMap.get(host)
if (group == null) {
group = TupleGroupIdMessageCount(size + 1, AtomicInteger(0))
groupMap.put(host, group)
}
return group
}
private fun createGroupNotification(context: Context, lastPushMessage: PushMessage): Notification { private fun createGroupNotification(context: Context, lastPushMessage: PushMessage): Notification {
with(lastPushMessage) { with(lastPushMessage) {
val id = lastPushMessage.notificationId.toInt() val id = lastPushMessage.notificationId.toInt()
...@@ -242,34 +243,16 @@ object PushManager { ...@@ -242,34 +243,16 @@ object PushManager {
pushMessageList?.let { pushMessageList?.let {
val messageCount = pushMessageList.size val messageCount = pushMessageList.size
val summary = summaryText.replace("%n%", messageCount.toString()) builder.setContentTitle(getTitle(messageCount, title))
.fromHtml()
builder.setNumber(messageCount)
if (messageCount > 1) {
val firstPush = pushMessageList[0]
val singleConversation = pushMessageList.filter {
firstPush.sender.username != it.sender.username
}.isEmpty()
val inbox = Notification.InboxStyle()
.setBigContentTitle(if (singleConversation) title else summary)
for (push in pushMessageList) {
if (singleConversation) {
inbox.addLine(push.message)
} else {
inbox.addLine("<font color='black'>${push.title}</font> <font color='gray'>${push.message}</font>".fromHtml())
}
}
builder.setStyle(inbox) val inbox = Notification.InboxStyle()
} else { .setBigContentTitle(getTitle(messageCount, title))
val bigText = Notification.BigTextStyle()
.bigText(pushMessageList[0].message.fromHtml())
.setBigContentTitle(pushMessageList[0].title.fromHtml())
builder.setStyle(bigText) for (push in pushMessageList) {
inbox.addLine("AAAA")
} }
builder.setStyle(inbox)
} }
} else { } else {
val bigText = Notification.BigTextStyle() val bigText = Notification.BigTextStyle()
...@@ -279,24 +262,6 @@ object PushManager { ...@@ -279,24 +262,6 @@ object PushManager {
builder.setStyle(bigText) builder.setStyle(bigText)
} }
// val messages = messageStack.get(notificationId.toInt())
// val messageCount = messages.size
//
// if (messageCount > 1) {
// val summary = summaryText.replace("%n%", messageCount.toString())
// val inbox = Notification.InboxStyle()
// .setBigContentTitle(title.fromHtml())
// .setSummaryText(summary)
//
// builder.setStyle(inbox)
// } else {
// val bigText = Notification.BigTextStyle()
// .bigText(message.fromHtml())
// .setBigContentTitle(title.fromHtml())
//
// builder.setStyle(bigText)
// }
return builder.build() return builder.build()
} }
} }
...@@ -373,26 +338,24 @@ object PushManager { ...@@ -373,26 +338,24 @@ object PushManager {
} }
if ("inbox" == style) { if ("inbox" == style) {
val messages = messageStack.get(notificationId.toInt()) val pushMessageList = hostToPushMessageList.get(host)
val messageCount = messages.size
if (messageCount > 1) { pushMessageList?.let {
val summary = summaryText.replace("%n%", messageCount.toString()) val messageCount = pushMessageList.size
.fromHtml()
val inbox = Notification.InboxStyle() val inbox = Notification.InboxStyle()
.setBigContentTitle(title.fromHtml())
.setSummaryText(summary)
messages.forEach { msg -> val userMessages = pushMessageList.filter {
inbox.addLine(msg.fromHtml()) it.notificationId == lastPushMessage.notificationId }
builder.setContentTitle(getTitle(userMessages.size, title))
inbox.setBigContentTitle(getTitle(userMessages.size, title))
for (push in userMessages) {
inbox.addLine(push.message)
} }
builder.setStyle(inbox) builder.setStyle(inbox)
} else {
val bigText = Notification.BigTextStyle()
.bigText(message.fromHtml())
.setBigContentTitle(title.fromHtml())
builder.setStyle(bigText)
} }
} else { } else {
builder.setContentText(message.fromHtml()) builder.setContentText(message.fromHtml())
...@@ -402,16 +365,8 @@ object PushManager { ...@@ -402,16 +365,8 @@ object PushManager {
} }
} }
private fun bundleMessage(id: Int, message: CharSequence) { private fun getTitle(messageCount: Int, title: String): CharSequence {
val existingStack: MutableList<CharSequence>? = messageStack[id] return if (messageCount > 1) "($messageCount) ${title.fromHtml()}" else title.fromHtml()
if (existingStack == null) {
val newStack = arrayListOf<CharSequence>()
newStack.add(message)
messageStack.put(id, newStack)
} else {
existingStack.add(0, message)
}
} }
private fun getDismissIntent(context: Context, pushMessage: PushMessage): PendingIntent { private fun getDismissIntent(context: Context, pushMessage: PushMessage): PendingIntent {
...@@ -551,13 +506,9 @@ object PushManager { ...@@ -551,13 +506,9 @@ object PushManager {
*/ */
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(EXTRA_NOT_ID) val notId = intent?.extras?.getInt(EXTRA_NOT_ID)
val host = intent?.extras?.getString(EXTRA_HOSTNAME) notId?.let {
notificationId?.let { clearNotificationsByNotificationId(notId)
clearNotificationIdStack(notificationId)
}
host?.let {
clearHostNotifications(host)
} }
} }
} }
...@@ -582,12 +533,16 @@ object PushManager { ...@@ -582,12 +533,16 @@ object PushManager {
pushMessage?.let { pushMessage?.let {
val userNotId = pushMessage.notificationId.toInt() val userNotId = pushMessage.notificationId.toInt()
val groupTuple = groupMap.get(pushMessage.host) val groupTuple = groupMap.get(pushMessage.host)
messageStack[userNotId]?.let { val pushes = hostToPushMessageList.get(pushMessage.host)
for (msg in messageStack[userNotId]) { pushes?.let {
manager.cancel(userNotId) val allMessagesFromSameUser = pushes.filter {
it.sender._id == pushMessage.sender._id
}
for (msg in allMessagesFromSameUser) {
manager.cancel(msg.notificationId.toInt())
groupTuple?.second?.decrementAndGet() groupTuple?.second?.decrementAndGet()
} }
clearNotificationIdStack(userNotId)
groupTuple?.let { groupTuple?.let {
val groupNotId = groupTuple.first val groupNotId = groupTuple.first
val totalNot = groupTuple.second.get() val totalNot = groupTuple.second.get()
......
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