11package com .oasisfeng .nevo .decorators .wechat ;
22
33import android .app .Notification ;
4+ import android .service .notification .StatusBarNotification ;
45import android .text .TextUtils ;
56import android .util .Log ;
67
78import com .oasisfeng .nevo .decorators .wechat .ConversationManager .Conversation ;
89
10+ import java .util .List ;
11+
912import androidx .annotation .Nullable ;
1013import androidx .core .app .NotificationCompat .MessagingStyle .Message ;
1114import androidx .core .app .Person ;
@@ -31,17 +34,22 @@ class WeChatMessage {
3134 static final String SENDER_MESSAGE_SEPARATOR = ": " ;
3235 private static final String SELF = "" ;
3336
34- static Message [] buildFromCarConversation (final Conversation conversation , final Notification .CarExtender .UnreadConversation convs ) {
37+ static Message [] buildFromCarConversation (final Conversation conversation , final Notification .CarExtender .UnreadConversation convs , final List < StatusBarNotification > archive ) {
3538 final String [] car_messages = convs .getMessages ();
3639 if (car_messages .length == 0 ) return new Message [] { buildFromBasicFields (conversation ).toMessage () }; // No messages in car conversation
3740
3841 final WeChatMessage basic_msg = buildFromBasicFields (conversation );
3942 final Message [] messages = new Message [car_messages .length ];
43+ final CharSequence [] tickerArray = new CharSequence [car_messages .length ];
44+ for (int i = archive .size () - 1 , diff = archive .size () - car_messages .length ; i >= 0 && i >= diff ; i --) {
45+ tickerArray [i - diff ] = archive .get (i ).getNotification ().tickerText ;
46+ }
4047 int end_of_peers = -1 ;
4148 if (! conversation .isGroupChat ()) for (end_of_peers = car_messages .length - 1 ; end_of_peers >= -1 ; end_of_peers --)
4249 if (end_of_peers >= 0 && TextUtils .equals (basic_msg .text , car_messages [end_of_peers ])) break ; // Find the actual end line which matches basic fields, in case extra lines are sent by self
43- for (int i = 0 , count = car_messages .length ; i < count ; i ++)
44- messages [i ] = buildFromCarMessage (conversation , car_messages [i ], end_of_peers >= 0 && i > end_of_peers ).toMessage ();
50+ for (int i = 0 , count = car_messages .length ; i < count ; i ++) {
51+ messages [i ] = buildFromCarMessage (conversation , car_messages [i ], tickerArray [i ], end_of_peers >= 0 && i > end_of_peers ).toMessage ();
52+ }
4553 return messages ;
4654 }
4755
@@ -130,9 +138,11 @@ private static boolean startsWith(final CharSequence text, final CharSequence ne
130138 && TextUtils .regionMatches (text , needle1_length , needle2 , 0 , needle2_length );
131139 }
132140
133- private static WeChatMessage buildFromCarMessage (final Conversation conversation , final String message , final boolean from_self ) {
141+ private static WeChatMessage buildFromCarMessage (final Conversation conversation , final String message , final @ Nullable CharSequence ticker , final boolean from_self ) {
134142 String text = message , sender = null ;
135- final int pos = from_self ? 0 : TextUtils .indexOf (message , SENDER_MESSAGE_SEPARATOR );
143+ int pos ;
144+ // parse text
145+ pos = from_self ? 0 : TextUtils .indexOf (message , SENDER_MESSAGE_SEPARATOR );
136146 if (pos > 0 ) {
137147 sender = message .substring (0 , pos );
138148 final boolean title_as_sender = TextUtils .equals (sender , conversation .getTitle ());
@@ -141,6 +151,15 @@ private static WeChatMessage buildFromCarMessage(final Conversation conversation
141151 if (conversation .isGroupChat () && title_as_sender ) sender = SELF ; // WeChat incorrectly use group chat title as sender for self-sent messages.
142152 } else sender = null ; // Not really the sender name, revert the parsing result.
143153 }
154+ // parse sender (from ticker)
155+ pos = from_self ? 0 : TextUtils .indexOf (ticker , SENDER_MESSAGE_SEPARATOR );
156+ if (pos > 0 ) {
157+ sender = ticker .toString ().substring (0 , pos );
158+ final boolean title_as_sender = TextUtils .equals (sender , conversation .getTitle ());
159+ if (conversation .isGroupChat () || title_as_sender ) { // Verify the sender with title for non-group conversation
160+ if (conversation .isGroupChat () && title_as_sender ) sender = SELF ; // WeChat incorrectly use group chat title as sender for self-sent messages.
161+ } else sender = null ; // Not really the sender name, revert the parsing result.
162+ }
144163 return new WeChatMessage (conversation , from_self ? SELF : sender , EmojiTranslator .translate (text ), 0 );
145164 }
146165
0 commit comments