@@ -56,7 +56,7 @@ abstract class NotificationProcessor(context: Context) {
5656 // 群聊消息
5757 // title: 群名 | 群名 (x条新消息)
5858 // ticker: 昵称(群名):消息内容
59- // text: 昵称: 消息内容
59+ // text: 昵称: 消息内容 //特别关注前缀:[有关注的内容]
6060
6161 /* *
6262 * 匹配群聊消息 Ticker.
@@ -68,6 +68,14 @@ abstract class NotificationProcessor(context: Context) {
6868 @VisibleForTesting
6969 val groupMsgPattern: Pattern = Pattern .compile(" ^(.+?)\\ ((.+)\\ ):([\\ s\\ S]+)$" )
7070
71+ /* *
72+ * 匹配群聊消息 Content.
73+ *
74+ * Group: 1[有关注的内容
75+ */
76+ @VisibleForTesting
77+ val groupMsgContentPattern: Pattern = Pattern .compile(" ^(\\ [有关注的内容])?[\\ s\\ S]+" )
78+
7179 // 私聊消息
7280 // title: 昵称 | 昵称 (x条新消息) //特别关心前缀:[特别关心]
7381 // ticker: 昵称: 消息内容
@@ -250,7 +258,7 @@ abstract class NotificationProcessor(context: Context) {
250258 getNotifyLargeIcon(context, original))
251259 conversation = addMessage(tag, qzoneSpecialTitle, content, null ,
252260 avatarManager.getAvatar(CONVERSATION_NAME_QZONE_SPECIAL .hashCode()), original.contentIntent,
253- original.deleteIntent)
261+ original.deleteIntent, false )
254262 // 由于特别关心动态推送的通知没有显示未读消息个数,所以这里无法提取并删除多余的历史消息。
255263 // Workaround: 在通知删除回调下来匹配并清空特别关心动态历史记录。
256264 Timber .tag(TAG ).d(" [QZoneSpecial] Ticker: $ticker " )
@@ -259,7 +267,7 @@ abstract class NotificationProcessor(context: Context) {
259267 avatarManager.saveAvatar(CONVERSATION_NAME_QZONE .hashCode(), getNotifyLargeIcon(context, original))
260268 conversation = addMessage(tag, qzoneSpecialTitle, content, null ,
261269 avatarManager.getAvatar(CONVERSATION_NAME_QZONE .hashCode()), original.contentIntent,
262- original.deleteIntent)
270+ original.deleteIntent, false )
263271 deleteOldMessage(conversation, num)
264272 Timber .tag(TAG ).d(" [QZone] Ticker: $ticker " )
265273 }
@@ -277,12 +285,16 @@ abstract class NotificationProcessor(context: Context) {
277285 val name = matcher.group(1 ) ? : return null
278286 val groupName = matcher.group(2 ) ? : return null
279287 val text = matcher.group(3 ) ? : return null
288+
289+ val contentMatcher = groupMsgContentPattern.matcher(content!! )
290+ val special = contentMatcher.matches() && contentMatcher.group(1 ) != null
291+
280292 if (! isMulti)
281293 avatarManager.saveAvatar(groupName.hashCode(), getNotifyLargeIcon(context, original))
282- val conversation = addMessage(tag, name, text, groupName,
283- avatarManager.getAvatar(name.hashCode()), original.contentIntent, original.deleteIntent)
294+ val conversation = addMessage(tag, name, text, groupName, avatarManager.getAvatar(name.hashCode()),
295+ original.contentIntent, original.deleteIntent, special )
284296 deleteOldMessage(conversation, if (isMulti) 0 else matchMessageNum(title))
285- Timber .tag(TAG ).d(" [Group] Name: $name ; Group: $groupName ; Text: $text " )
297+ Timber .tag(TAG ).d(" [${ if (special) " GroupS " else " Group" } ] Name: $name ; Group: $groupName ; Text: $text " )
286298 return renewConversionNotification(context, tag, NotifyChannel .GROUP , conversation, sbn, original)
287299 }
288300 }
@@ -297,10 +309,10 @@ abstract class NotificationProcessor(context: Context) {
297309 if (! isMulti)
298310 avatarManager.saveAvatar(name.hashCode(), getNotifyLargeIcon(context, original))
299311 val conversation = addMessage(tag, name, text, null , avatarManager.getAvatar(name.hashCode()),
300- original.contentIntent, original.deleteIntent)
312+ original.contentIntent, original.deleteIntent, special )
301313 deleteOldMessage(conversation, if (isMulti) 0 else matchMessageNum(titleMatcher))
302314 return if (special) {
303- Timber .tag(TAG ).d(" [Special ] Name: $name ; Text: $text " )
315+ Timber .tag(TAG ).d(" [FriendS ] Name: $name ; Text: $text " )
304316 renewConversionNotification(context, tag, NotifyChannel .FRIEND_SPECIAL , conversation, sbn, original)
305317 } else {
306318 Timber .tag(TAG ).d(" [Friend] Name: $name ; Text: $text " )
@@ -422,7 +434,7 @@ abstract class NotificationProcessor(context: Context) {
422434 val style = NotificationCompat .MessagingStyle (Person .Builder ()
423435 .setName(context.getString(R .string.notify_qzone_title)).build())
424436 conversation.messages.forEach { msg ->
425- style.addMessage(msg.content, msg.time, msg.person )
437+ style.addMessage(msg)
426438 }
427439 val num = conversation.messages.size
428440 val subtext = if (num > 1 ) context.getString(R .string.notify_subtext_qzone_num, num) else null
@@ -446,7 +458,7 @@ abstract class NotificationProcessor(context: Context) {
446458 style.isGroupConversation = true
447459 }
448460 conversation.messages.forEach { msg ->
449- style.addMessage(msg.content, msg.time, msg.person )
461+ style.addMessage(msg)
450462 }
451463 val num = conversation.messages.size
452464 val subtext = if (num > 1 ) context.getString(R .string.notify_subtext_message_num, num) else null
@@ -455,6 +467,34 @@ abstract class NotificationProcessor(context: Context) {
455467 avatarManager.getAvatar(conversation.name.hashCode()), original, subtext)
456468 }
457469
470+ private fun NotificationCompat.MessagingStyle.addMessage (message : Message ) {
471+ var name = message.person.name
472+ if (message.special && showSpecialPrefix(ctx)) {
473+ // 添加特别关心或关注前缀
474+ name = if (isGroupConversation)
475+ ctx.getString(R .string.special_group_prefix) + name
476+ else
477+ ctx.getString(R .string.special_prefix) + name
478+ }
479+
480+ val person = if (name == message.person.name) {
481+ message.person
482+ } else {
483+ message.person.clone(name)
484+ }
485+ addMessage(message.content, message.time, person)
486+ }
487+
488+ private fun Person.clone (newName : CharSequence? = null): Person {
489+ return Person .Builder ()
490+ .setBot(this .isBot)
491+ .setIcon(this .icon)
492+ .setImportant(this .isImportant)
493+ .setKey(this .key)
494+ .setName(newName ? : this .name)
495+ .setUri(this .uri)
496+ .build()
497+ }
458498
459499 private fun setIcon (context : Context , builder : NotificationCompat .Builder , tag : Int , isQzone : Boolean ) {
460500 if (isQzone) {
@@ -488,9 +528,14 @@ abstract class NotificationProcessor(context: Context) {
488528
489529 /* *
490530 * 加入历史消息记录。
531+ *
532+ * @param name 发送者昵称。
533+ * @param content 消息内容。
534+ * @param group 群组名。`null` 表示非群组消息。
535+ * @param special 是否来自特别关心或特别关注。
491536 */
492537 private fun addMessage (@SourceTag tag : Int , name : String , content : String , group : String? , icon : Bitmap ? ,
493- contentIntent : PendingIntent , deleteIntent : PendingIntent ): Conversation {
538+ contentIntent : PendingIntent , deleteIntent : PendingIntent , special : Boolean ): Conversation {
494539 var conversation: Conversation ? = null
495540 // 以会话名为标准寻找已存在的会话
496541 for (item in getHistoryMessage(tag)) {
@@ -511,7 +556,7 @@ abstract class NotificationProcessor(context: Context) {
511556 conversation = Conversation (group != null , group ? : name, contentIntent, deleteIntent)
512557 getHistoryMessage(tag).add(conversation)
513558 }
514- conversation.messages.add(Message (name, icon, content))
559+ conversation.messages.add(Message (name, icon, content, special ))
515560 return conversation
516561 }
517562
@@ -544,8 +589,9 @@ abstract class NotificationProcessor(context: Context) {
544589 * @param name 发送者昵称。
545590 * @param icon 头像。
546591 * @param content 消息内容。
592+ * @param special 是否来自特别关心或特别关注。仅在聊天消息中有效。
547593 */
548- protected data class Message (val name : String , val icon : Bitmap ? , val content : String ) {
594+ protected data class Message (val name : String , val icon : Bitmap ? , val content : String , val special : Boolean ) {
549595 val person: Person = Person .Builder ()
550596 .setIcon(icon?.let { IconCompat .createWithBitmap(it) })
551597 .setName(name)
0 commit comments