Skip to content

Commit 1b938e8

Browse files
committed
FIX: Sender avatar is missing when delivered by app-specific mirror with API level >= 28.
1 parent 63eca02 commit 1b938e8

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

src/main/java/com/oasisfeng/nevo/decorators/wechat/ConversationManager.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import java.util.Map;
1010

1111
import androidx.annotation.IntDef;
12+
import androidx.annotation.NonNull;
1213
import androidx.annotation.Nullable;
1314
import androidx.core.app.Person;
15+
import androidx.core.graphics.drawable.IconCompat;
1416

1517
import static java.util.Objects.requireNonNull;
1618

@@ -36,10 +38,12 @@ static class Conversation {
3638
final int id;
3739
@Nullable String key;
3840
int count;
41+
CharSequence title;
3942
CharSequence summary;
4043
CharSequence ticker;
4144
long timestamp;
42-
Person sender = SENDER_PLACEHOLDER;
45+
IconCompat icon;
46+
private @Nullable Person.Builder sender;
4347

4448
int getType() { return mType; }
4549

@@ -48,22 +52,29 @@ int setType(final int type) {
4852
if (type == mType) return type;
4953
final int previous_type = mType;
5054
mType = type;
51-
sender = type == TYPE_UNKNOWN || type == TYPE_GROUP_CHAT ? SENDER_PLACEHOLDER
52-
: sender.toBuilder().setKey(key).setBot(type == TYPE_BOT_MESSAGE).build(); // Always set key as it may change
55+
sender = type == TYPE_UNKNOWN || type == TYPE_GROUP_CHAT ? null : sender().setKey(key).setBot(type == TYPE_BOT_MESSAGE); // Always set key as it may change
5356
if (type != TYPE_GROUP_CHAT) mParticipants.clear();
5457
return previous_type;
5558
}
5659

57-
boolean isGroupChat() { return mType == TYPE_GROUP_CHAT; }
58-
59-
CharSequence getTitle() { return mTitle; }
60-
61-
void setTitle(final CharSequence title) {
62-
if (TextUtils.equals(title, mTitle)) return;
63-
mTitle = title;
64-
sender = sender.toBuilder().setName(title).build(); // Rename the sender
60+
@NonNull Person.Builder sender() {
61+
return sender != null ? sender : new Person.Builder() { @NonNull @Override public Person build() {
62+
switch (mType) {
63+
case TYPE_GROUP_CHAT:
64+
return SENDER_PLACEHOLDER;
65+
case TYPE_BOT_MESSAGE:
66+
setBot(true); // Fall-through
67+
case TYPE_UNKNOWN:
68+
case TYPE_DIRECT_MESSAGE:
69+
setIcon(icon).setName(title != null ? title : " "); // Cannot be empty string, or it will be treated as null.
70+
break;
71+
}
72+
return super.build();
73+
}};
6574
}
6675

76+
boolean isGroupChat() { return mType == TYPE_GROUP_CHAT; }
77+
6778
Person getGroupParticipant(final String key, final String name) {
6879
if (! isGroupChat()) throw new IllegalStateException("Not group chat");
6980
Person.Builder builder = null;
@@ -83,7 +94,6 @@ static CharSequence getOriginalName(final Person person) {
8394
Conversation(final int id) { this.id = id; }
8495

8596
@ConversationType private int mType;
86-
private CharSequence mTitle;
8797
private final Map<String, Person> mParticipants = new ArrayMap<>();
8898
}
8999

src/main/java/com/oasisfeng/nevo/decorators/wechat/MessagingBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private static Message buildMessage(final Conversation conversation, final long
196196
sender = extractSenderFromText(text);
197197
if (sender != null) {
198198
actual_text = text.subSequence(sender.length() + SENDER_MESSAGE_SEPARATOR.length(), text.length());
199-
if (TextUtils.equals(conversation.getTitle(), sender)) sender = null; // In this case, the actual sender is user itself.
199+
if (TextUtils.equals(conversation.title, sender)) sender = null; // In this case, the actual sender is user itself.
200200
}
201201
}
202202
actual_text = EmojiTranslator.translate(actual_text);
@@ -206,7 +206,7 @@ private static Message buildMessage(final Conversation conversation, final long
206206
else if (conversation.isGroupChat()) {
207207
final String ticker_sender = ticker != null ? extractSenderFromText(ticker) : null; // Group nick is used in ticker and content text, while original nick in sender.
208208
person = sender == null ? null : conversation.getGroupParticipant(sender, ticker_sender != null ? ticker_sender : sender);
209-
} else person = conversation.sender;
209+
} else person = conversation.sender().build();
210210
return new Message(actual_text, when, person);
211211
}
212212

src/main/java/com/oasisfeng/nevo/decorators/wechat/WeChatDecorator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.content.SharedPreferences;
2727
import android.content.pm.ApplicationInfo;
2828
import android.content.pm.PackageManager;
29+
import android.graphics.drawable.Icon;
2930
import android.media.AudioAttributes;
3031
import android.net.Uri;
3132
import android.os.Bundle;
@@ -53,6 +54,7 @@
5354
import androidx.annotation.RequiresApi;
5455
import androidx.annotation.StringRes;
5556
import androidx.core.app.NotificationCompat.MessagingStyle;
57+
import androidx.core.graphics.drawable.IconCompat;
5658

5759
import static android.app.Notification.EXTRA_TEXT;
5860
import static android.app.Notification.EXTRA_TITLE;
@@ -120,7 +122,9 @@ public class WeChatDecorator extends NevoDecoratorService {
120122
conversation = mConversationManager.getConversation(title_hash);
121123
} else conversation = mConversationManager.getConversation(evolving.getOriginalId());
122124

123-
conversation.setTitle(title);
125+
final Icon icon = n.getLargeIcon();
126+
conversation.icon = icon != null ? IconCompat.createFromIcon(this, icon) : null;
127+
conversation.title = title;
124128
conversation.summary = content_text;
125129
conversation.ticker = n.tickerText;
126130
conversation.timestamp = n.when;

src/main/java/com/oasisfeng/nevo/decorators/wechat/WeChatMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static int guessConversationType(final Conversation conversation) {
116116
// The content without unread count prefix, may or may not start with sender nick
117117
final CharSequence message = pos > 0 && content.charAt(0) == '[' ? content.subSequence(pos, content.length()) : content;
118118
// message.startsWith(title + SENDER_MESSAGE_SEPARATOR)
119-
if (startsWith(message, conversation.getTitle(), SENDER_MESSAGE_SEPARATOR)) // The title of group chat is group name, not the message sender
119+
if (startsWith(message, conversation.title, SENDER_MESSAGE_SEPARATOR)) // The title of group chat is group name, not the message sender
120120
return Conversation.TYPE_DIRECT_MESSAGE; // Most probably a direct message with more than 1 unread
121121
return Conversation.TYPE_GROUP_CHAT;
122122
} else if (TextUtils.indexOf(ticker, content) >= 0) {
@@ -135,7 +135,7 @@ private static WeChatMessage buildFromCarMessage(final Conversation conversation
135135
final int pos = from_self ? 0 : TextUtils.indexOf(message, SENDER_MESSAGE_SEPARATOR);
136136
if (pos > 0) {
137137
sender = message.substring(0, pos);
138-
final boolean title_as_sender = TextUtils.equals(sender, conversation.getTitle());
138+
final boolean title_as_sender = TextUtils.equals(sender, conversation.title);
139139
if (conversation.isGroupChat() || title_as_sender) { // Verify the sender with title for non-group conversation
140140
text = message.substring(pos + SENDER_MESSAGE_SEPARATOR.length());
141141
if (conversation.isGroupChat() && title_as_sender) sender = SELF; // WeChat incorrectly use group chat title as sender for self-sent messages.
@@ -145,7 +145,7 @@ private static WeChatMessage buildFromCarMessage(final Conversation conversation
145145
}
146146

147147
private Message toMessage() {
148-
final Person person = SELF.equals(sender) ? null : conversation.isGroupChat() ? conversation.getGroupParticipant(sender, nick) : conversation.sender;
148+
final Person person = SELF.equals(sender) ? null : conversation.isGroupChat() ? conversation.getGroupParticipant(sender, nick) : conversation.sender().build();
149149
return new Message(text, time, person);
150150
}
151151

0 commit comments

Comments
 (0)