1
1
package com .oasisfeng .nevo .decorators .wechat ;
2
2
3
3
import android .app .Notification ;
4
+ import android .service .notification .StatusBarNotification ;
4
5
import android .text .TextUtils ;
5
6
import android .util .Log ;
6
7
7
8
import com .oasisfeng .nevo .decorators .wechat .ConversationManager .Conversation ;
8
9
10
+ import java .util .List ;
11
+
9
12
import androidx .annotation .Nullable ;
10
13
import androidx .core .app .NotificationCompat .MessagingStyle .Message ;
11
14
import androidx .core .app .Person ;
@@ -31,17 +34,22 @@ class WeChatMessage {
31
34
static final String SENDER_MESSAGE_SEPARATOR = ": " ;
32
35
private static final String SELF = "" ;
33
36
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 ) {
35
38
final String [] car_messages = convs .getMessages ();
36
39
if (car_messages .length == 0 ) return new Message [] { buildFromBasicFields (conversation ).toMessage () }; // No messages in car conversation
37
40
38
41
final WeChatMessage basic_msg = buildFromBasicFields (conversation );
39
42
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
+ }
40
47
int end_of_peers = -1 ;
41
48
if (! conversation .isGroupChat ()) for (end_of_peers = car_messages .length - 1 ; end_of_peers >= -1 ; end_of_peers --)
42
49
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
+ }
45
53
return messages ;
46
54
}
47
55
@@ -130,9 +138,11 @@ private static boolean startsWith(final CharSequence text, final CharSequence ne
130
138
&& TextUtils .regionMatches (text , needle1_length , needle2 , 0 , needle2_length );
131
139
}
132
140
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 ) {
134
142
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 );
136
146
if (pos > 0 ) {
137
147
sender = message .substring (0 , pos );
138
148
final boolean title_as_sender = TextUtils .equals (sender , conversation .getTitle ());
@@ -141,6 +151,15 @@ private static WeChatMessage buildFromCarMessage(final Conversation conversation
141
151
if (conversation .isGroupChat () && title_as_sender ) sender = SELF ; // WeChat incorrectly use group chat title as sender for self-sent messages.
142
152
} else sender = null ; // Not really the sender name, revert the parsing result.
143
153
}
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
+ }
144
163
return new WeChatMessage (conversation , from_self ? SELF : sender , EmojiTranslator .translate (text ), 0 );
145
164
}
146
165
0 commit comments