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 {
rocketChatCache.setSelectedRoomId(intent.getStringExtra(PushManager.EXTRA_ROOM_ID));
}
}
PushManager.INSTANCE.clearHostNotifications(hostname);
PushManager.INSTANCE.clearNotificationsByHost(hostname);
} else {
updateHostnameIfNeeded(rocketChatCache.getSelectedServerHostname());
}
......@@ -76,7 +76,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
if (intent.hasExtra(PushManager.EXTRA_NOT_ID)) {
isNotification = true;
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
import android.text.Html
import android.text.Spanned
import android.util.Log
import android.util.SparseArray
import chat.rocket.android.BackgroundLooper
import chat.rocket.android.BuildConfig
import chat.rocket.android.R
......@@ -51,10 +50,6 @@ object PushManager {
private const val REPLY_LABEL = "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.
// This map associates a host to a group id.
private val groupMap = HashMap<String, TupleGroupIdMessageCount>()
......@@ -85,20 +80,39 @@ object PushManager {
Log.d(PushMessage::class.java.simpleName, lastPushMessage.toString())
}
bundleMessage(notId.toInt(), lastPushMessage.message)
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) {
messageStack.delete(notificationId)
fun clearNotificationsByHost(host: String) {
hostToPushMessageList.remove(host)
}
fun clearHostNotifications(host: String) {
hostToPushMessageList.remove(host)
fun clearNotificationsByNotificationId(notificationId: Int) {
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) {
......@@ -110,19 +124,18 @@ object PushManager {
val groupTuple = getGroupForHost(host)
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)) {
val notification = createSingleNotificationForOreo(context, lastPushMessage)
val groupNotification = createGroupNotificationForOreo(context, lastPushMessage)
manager.notify(notId, notification)
manager.notify(groupTuple.first, groupNotification)
} 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 groupNotification = createGroupNotification(context, lastPushMessage)
NotificationManagerCompat.from(context).notify(notId, notification)
......@@ -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 {
with(lastPushMessage) {
val id = lastPushMessage.notificationId.toInt()
......@@ -242,34 +243,16 @@ object PushManager {
pushMessageList?.let {
val messageCount = pushMessageList.size
val summary = summaryText.replace("%n%", messageCount.toString())
.fromHtml()
builder.setNumber(messageCount)
if (messageCount > 1) {
val firstPush = pushMessageList[0]
val singleConversation = pushMessageList.filter {
firstPush.sender.username != it.sender.username
}.isEmpty()
builder.setContentTitle(getTitle(messageCount, title))
val inbox = Notification.InboxStyle()
.setBigContentTitle(if (singleConversation) title else summary)
.setBigContentTitle(getTitle(messageCount, title))
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())
}
inbox.addLine("AAAA")
}
builder.setStyle(inbox)
} else {
val bigText = Notification.BigTextStyle()
.bigText(pushMessageList[0].message.fromHtml())
.setBigContentTitle(pushMessageList[0].title.fromHtml())
builder.setStyle(bigText)
}
}
} else {
val bigText = Notification.BigTextStyle()
......@@ -279,24 +262,6 @@ object PushManager {
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()
}
}
......@@ -373,26 +338,24 @@ object PushManager {
}
if ("inbox" == style) {
val messages = messageStack.get(notificationId.toInt())
val messageCount = messages.size
if (messageCount > 1) {
val summary = summaryText.replace("%n%", messageCount.toString())
.fromHtml()
val pushMessageList = hostToPushMessageList.get(host)
pushMessageList?.let {
val messageCount = pushMessageList.size
val inbox = Notification.InboxStyle()
.setBigContentTitle(title.fromHtml())
.setSummaryText(summary)
messages.forEach { msg ->
inbox.addLine(msg.fromHtml())
val userMessages = pushMessageList.filter {
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)
} else {
val bigText = Notification.BigTextStyle()
.bigText(message.fromHtml())
.setBigContentTitle(title.fromHtml())
builder.setStyle(bigText)
}
} else {
builder.setContentText(message.fromHtml())
......@@ -402,16 +365,8 @@ object PushManager {
}
}
private fun bundleMessage(id: Int, message: CharSequence) {
val existingStack: MutableList<CharSequence>? = messageStack[id]
if (existingStack == null) {
val newStack = arrayListOf<CharSequence>()
newStack.add(message)
messageStack.put(id, newStack)
} else {
existingStack.add(0, message)
}
private fun getTitle(messageCount: Int, title: String): CharSequence {
return if (messageCount > 1) "($messageCount) ${title.fromHtml()}" else title.fromHtml()
}
private fun getDismissIntent(context: Context, pushMessage: PushMessage): PendingIntent {
......@@ -551,13 +506,9 @@ object PushManager {
*/
class DeleteReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val notificationId = intent?.extras?.getInt(EXTRA_NOT_ID)
val host = intent?.extras?.getString(EXTRA_HOSTNAME)
notificationId?.let {
clearNotificationIdStack(notificationId)
}
host?.let {
clearHostNotifications(host)
val notId = intent?.extras?.getInt(EXTRA_NOT_ID)
notId?.let {
clearNotificationsByNotificationId(notId)
}
}
}
......@@ -582,12 +533,16 @@ object PushManager {
pushMessage?.let {
val userNotId = pushMessage.notificationId.toInt()
val groupTuple = groupMap.get(pushMessage.host)
messageStack[userNotId]?.let {
for (msg in messageStack[userNotId]) {
manager.cancel(userNotId)
val pushes = hostToPushMessageList.get(pushMessage.host)
pushes?.let {
val allMessagesFromSameUser = pushes.filter {
it.sender._id == pushMessage.sender._id
}
for (msg in allMessagesFromSameUser) {
manager.cancel(msg.notificationId.toInt())
groupTuple?.second?.decrementAndGet()
}
clearNotificationIdStack(userNotId)
groupTuple?.let {
val groupNotId = groupTuple.first
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