|
1 | 1 | package com.oasisfeng.nevo.decorators.wechat;
|
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint; |
3 | 4 | import android.app.Notification;
|
4 | 5 | import android.app.Notification.Action;
|
5 | 6 | import android.app.NotificationManager;
|
|
32 | 33 |
|
33 | 34 | import androidx.annotation.NonNull;
|
34 | 35 | import androidx.annotation.Nullable;
|
| 36 | +import androidx.annotation.RequiresApi; |
35 | 37 | import androidx.core.app.NotificationCompat.MessagingStyle;
|
36 | 38 | import androidx.core.app.NotificationCompat.MessagingStyle.Message;
|
37 | 39 | import androidx.core.app.Person;
|
|
44 | 46 | import static android.os.Build.VERSION_CODES.N;
|
45 | 47 | import static android.os.Build.VERSION_CODES.O;
|
46 | 48 | import static android.os.Build.VERSION_CODES.P;
|
| 49 | +import static androidx.core.app.NotificationCompat.EXTRA_CONVERSATION_TITLE; |
| 50 | +import static androidx.core.app.NotificationCompat.EXTRA_IS_GROUP_CONVERSATION; |
| 51 | +import static androidx.core.app.NotificationCompat.EXTRA_MESSAGES; |
| 52 | +import static androidx.core.app.NotificationCompat.EXTRA_SELF_DISPLAY_NAME; |
47 | 53 | import static com.oasisfeng.nevo.decorators.wechat.WeChatDecorator.SENDER_MESSAGE_SEPARATOR;
|
48 | 54 |
|
49 | 55 | /**
|
@@ -77,6 +83,14 @@ class MessagingBuilder {
|
77 | 83 | private static final String CAR_KEY_TEXT = "text";
|
78 | 84 | private static final String CAR_KEY_TIMESTAMP = "timestamp";
|
79 | 85 |
|
| 86 | + private static final String KEY_TEXT = "text"; |
| 87 | + private static final String KEY_TIMESTAMP = "time"; |
| 88 | + private static final String KEY_SENDER = "sender"; |
| 89 | + @RequiresApi(P) private static final String KEY_SENDER_PERSON = "sender_person"; |
| 90 | + private static final String KEY_DATA_MIME_TYPE = "type"; |
| 91 | + private static final String KEY_DATA_URI= "uri"; |
| 92 | + private static final String KEY_EXTRAS_BUNDLE = "extras"; |
| 93 | + |
80 | 94 | private static final String KEY_USERNAME = "key_username";
|
81 | 95 | private static final String MENTION_SEPARATOR = " "; // Separator between @nick and text. It's not a regular white space, but U+2005.
|
82 | 96 |
|
@@ -335,6 +349,46 @@ void markRead(final String key) {
|
335 | 349 | return new Intent().addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES).setPackage(action.getCreatorPackage());
|
336 | 350 | }
|
337 | 351 |
|
| 352 | + static void flatIntoExtras(final MessagingStyle messaging, final Bundle extras) { |
| 353 | + final Person user = messaging.getUser(); |
| 354 | + if (user != null) { |
| 355 | + extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, user.getName()); |
| 356 | + if (SDK_INT >= P) extras.putParcelable(Notification.EXTRA_MESSAGING_PERSON, toAndroidPerson(user)); // Not included in NotificationCompat |
| 357 | + } |
| 358 | + if (messaging.getConversationTitle() != null) extras.putCharSequence(EXTRA_CONVERSATION_TITLE, messaging.getConversationTitle()); |
| 359 | + final List<Message> messages = messaging.getMessages(); |
| 360 | + if (! messages.isEmpty()) extras.putParcelableArray(EXTRA_MESSAGES, getBundleArrayForMessages(messages)); |
| 361 | + //if (! mHistoricMessages.isEmpty()) extras.putParcelableArray(Notification.EXTRA_HISTORIC_MESSAGES, MessagingBuilder.getBundleArrayForMessages(mHistoricMessages)); |
| 362 | + extras.putBoolean(EXTRA_IS_GROUP_CONVERSATION, messaging.isGroupConversation()); |
| 363 | + } |
| 364 | + |
| 365 | + private static Bundle[] getBundleArrayForMessages(final List<Message> messages) { |
| 366 | + final int N = messages.size(); |
| 367 | + final Bundle[] bundles = new Bundle[N]; |
| 368 | + for (int i = 0; i < N; i ++) bundles[i] = toBundle(messages.get(i)); |
| 369 | + return bundles; |
| 370 | + } |
| 371 | + |
| 372 | + private static Bundle toBundle(final Message message) { |
| 373 | + final Bundle bundle = new Bundle(); |
| 374 | + bundle.putCharSequence(KEY_TEXT, message.getText()); |
| 375 | + bundle.putLong(KEY_TIMESTAMP, message.getTimestamp()); // Must be included even for 0 |
| 376 | + final Person sender = message.getPerson(); |
| 377 | + if (sender != null) { |
| 378 | + bundle.putCharSequence(KEY_SENDER, sender.getName()); // Legacy listeners need this |
| 379 | + if (SDK_INT >= P) bundle.putParcelable(KEY_SENDER_PERSON, toAndroidPerson(sender)); |
| 380 | + } |
| 381 | + if (message.getDataMimeType() != null) bundle.putString(KEY_DATA_MIME_TYPE, message.getDataMimeType()); |
| 382 | + if (message.getDataUri() != null) bundle.putParcelable(KEY_DATA_URI, message.getDataUri()); |
| 383 | + if (SDK_INT >= O && ! message.getExtras().isEmpty()) bundle.putBundle(KEY_EXTRAS_BUNDLE, message.getExtras()); |
| 384 | + //if (message.isRemoteInputHistory()) bundle.putBoolean(KEY_REMOTE_INPUT_HISTORY, message.isRemoteInputHistory()); |
| 385 | + return bundle; |
| 386 | + } |
| 387 | + |
| 388 | + @RequiresApi(P) @SuppressLint("RestrictedApi") private static android.app.Person toAndroidPerson(final Person user) { |
| 389 | + return user.toAndroidPerson(); |
| 390 | + } |
| 391 | + |
338 | 392 | interface Controller { void recastNotification(String key, Bundle addition); }
|
339 | 393 |
|
340 | 394 | MessagingBuilder(final Context context, final Controller controller) {
|
|
0 commit comments