1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
|
package de.pixart.messenger.services;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.media.AudioAttributes;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.DeadSystemException;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat.CarExtender.UnreadConversation;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.Person;
import android.support.v4.app.RemoteInput;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.IconCompat;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Pair;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.entities.Contact;
import de.pixart.messenger.entities.Conversation;
import de.pixart.messenger.entities.Conversational;
import de.pixart.messenger.entities.Message;
import de.pixart.messenger.persistance.FileBackend;
import de.pixart.messenger.ui.ConversationsActivity;
import de.pixart.messenger.ui.EditAccountActivity;
import de.pixart.messenger.ui.TimePreference;
import de.pixart.messenger.utils.AccountUtils;
import de.pixart.messenger.utils.Compatibility;
import de.pixart.messenger.utils.GeoHelper;
import de.pixart.messenger.utils.UIHelper;
import de.pixart.messenger.xmpp.XmppConnection;
public class NotificationService {
public static final Object CATCHUP_LOCK = new Object();
public static final String MESSAGES_CHANNEL_ID = "messages";
public static final String FOREGROUND_CHANNEL_ID = "foreground";
public static final String BACKUP_CHANNEL_ID = "backup";
public static final String UPDATE_CHANNEL_ID = "appupdate";
public static final String VIDEOCOMPRESSION_CHANNEL_ID = "compression";
public static final String ERROR_CHANNEL_ID = "error";
private static final String CONVERSATIONS_GROUP = "de.pixart.messenger";
private static final int NOTIFICATION_ID_MULTIPLIER = 1024 * 1024;
public static final int NOTIFICATION_ID = 2 * NOTIFICATION_ID_MULTIPLIER;
public static final int FOREGROUND_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 4;
public static final int ERROR_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 6;
private static final int LED_COLOR = 0xff0080FF;
private final XmppConnectionService mXmppConnectionService;
private final LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<>();
private final HashMap<Conversation, AtomicInteger> mBacklogMessageCounter = new HashMap<>();
private Conversation mOpenConversation;
private boolean mIsInForeground;
private long mLastNotification;
NotificationService(final XmppConnectionService service) {
this.mXmppConnectionService = service;
}
private static boolean displaySnoozeAction(List<Message> messages) {
int numberOfMessagesWithoutReply = 0;
for (Message message : messages) {
if (message.getStatus() == Message.STATUS_RECEIVED) {
++numberOfMessagesWithoutReply;
} else {
return false;
}
}
return numberOfMessagesWithoutReply >= 3;
}
public static Pattern generateNickHighlightPattern(final String nick) {
return Pattern.compile("(?<=(^|\\s))" + Pattern.quote(nick) + "(?=\\s|$|\\p{Punct})");
}
@RequiresApi(api = Build.VERSION_CODES.O)
void initializeChannels() {
final Context c = mXmppConnectionService;
final NotificationManager notificationManager = c.getSystemService(NotificationManager.class);
if (notificationManager == null) {
return;
}
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("status", c.getString(R.string.notification_group_status_information)));
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("chats", c.getString(R.string.notification_group_messages)));
final NotificationChannel foregroundServiceChannel = new NotificationChannel(FOREGROUND_CHANNEL_ID,
c.getString(R.string.foreground_service_channel_name),
NotificationManager.IMPORTANCE_MIN);
foregroundServiceChannel.setDescription(c.getString(R.string.foreground_service_channel_description));
foregroundServiceChannel.setShowBadge(false);
foregroundServiceChannel.setGroup("status");
notificationManager.createNotificationChannel(foregroundServiceChannel);
final NotificationChannel backupChannel = new NotificationChannel(BACKUP_CHANNEL_ID,
c.getString(R.string.backup_channel_name),
NotificationManager.IMPORTANCE_LOW);
backupChannel.setShowBadge(false);
backupChannel.setGroup("status");
notificationManager.createNotificationChannel(backupChannel);
final NotificationChannel videoCompressionChannel = new NotificationChannel(VIDEOCOMPRESSION_CHANNEL_ID,
c.getString(R.string.video_compression_channel_name),
NotificationManager.IMPORTANCE_LOW);
videoCompressionChannel.setShowBadge(false);
videoCompressionChannel.setGroup("status");
notificationManager.createNotificationChannel(videoCompressionChannel);
final NotificationChannel AppUpdateChannel = new NotificationChannel(UPDATE_CHANNEL_ID,
c.getString(R.string.app_update_channel_name),
NotificationManager.IMPORTANCE_LOW);
AppUpdateChannel.setShowBadge(false);
AppUpdateChannel.setGroup("status");
notificationManager.createNotificationChannel(AppUpdateChannel);
final NotificationChannel errorChannel = new NotificationChannel(ERROR_CHANNEL_ID,
c.getString(R.string.error_channel_name),
NotificationManager.IMPORTANCE_LOW);
errorChannel.setDescription(c.getString(R.string.error_channel_description));
errorChannel.setShowBadge(false);
errorChannel.setGroup("status");
notificationManager.createNotificationChannel(errorChannel);
final NotificationChannel messagesChannel = new NotificationChannel(MESSAGES_CHANNEL_ID,
c.getString(R.string.messages_channel_name),
NotificationManager.IMPORTANCE_HIGH);
messagesChannel.setShowBadge(true);
messagesChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.build());
messagesChannel.setLightColor(LED_COLOR);
final int dat = 70;
final long[] pattern = {0, 3 * dat, dat, dat};
messagesChannel.setVibrationPattern(pattern);
messagesChannel.enableVibration(true);
messagesChannel.enableLights(true);
messagesChannel.setGroup("chats");
notificationManager.createNotificationChannel(messagesChannel);
final NotificationChannel silentMessagesChannel = new NotificationChannel("silent_messages",
c.getString(R.string.silent_messages_channel_name),
NotificationManager.IMPORTANCE_LOW);
silentMessagesChannel.setDescription(c.getString(R.string.silent_messages_channel_description));
silentMessagesChannel.setShowBadge(true);
silentMessagesChannel.setLightColor(LED_COLOR);
silentMessagesChannel.enableLights(true);
silentMessagesChannel.setGroup("chats");
notificationManager.createNotificationChannel(silentMessagesChannel);
final NotificationChannel quietHoursChannel = new NotificationChannel("quiet_hours",
c.getString(R.string.title_pref_quiet_hours),
NotificationManager.IMPORTANCE_LOW);
quietHoursChannel.setShowBadge(true);
quietHoursChannel.setLightColor(LED_COLOR);
quietHoursChannel.enableLights(true);
quietHoursChannel.setGroup("chats");
quietHoursChannel.enableVibration(false);
quietHoursChannel.setSound(null, null);
notificationManager.createNotificationChannel(quietHoursChannel);
}
public boolean notify(final Message message) {
final Conversation conversation = (Conversation) message.getConversation();
return message.getStatus() == Message.STATUS_RECEIVED
&& !conversation.isMuted()
&& (conversation.alwaysNotify() || wasHighlightedOrPrivate(message))
&& (!conversation.isWithStranger() || notificationsFromStrangers());
}
private boolean notificationsFromStrangers() {
return mXmppConnectionService.getBooleanPreference("notifications_from_strangers", R.bool.notifications_from_strangers);
}
private boolean isQuietHours() {
if (!mXmppConnectionService.getBooleanPreference("enable_quiet_hours", R.bool.enable_quiet_hours)) {
return false;
}
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
final long startTime = TimePreference.minutesToTimestamp(preferences.getLong("quiet_hours_start", TimePreference.DEFAULT_VALUE));
final long endTime = TimePreference.minutesToTimestamp(preferences.getLong("quiet_hours_end", TimePreference.DEFAULT_VALUE));
final long nowTime = Calendar.getInstance().getTimeInMillis();
if (endTime < startTime) {
return nowTime > startTime || nowTime < endTime;
} else {
return nowTime > startTime && nowTime < endTime;
}
}
public void pushFromBacklog(final Message message) {
if (notify(message)) {
synchronized (notifications) {
getBacklogMessageCounter((Conversation) message.getConversation()).incrementAndGet();
pushToStack(message);
}
}
}
private AtomicInteger getBacklogMessageCounter(Conversation conversation) {
synchronized (mBacklogMessageCounter) {
if (!mBacklogMessageCounter.containsKey(conversation)) {
mBacklogMessageCounter.put(conversation, new AtomicInteger(0));
}
return mBacklogMessageCounter.get(conversation);
}
}
void pushFromDirectReply(final Message message) {
synchronized (notifications) {
pushToStack(message);
updateNotification(false);
}
}
public void finishBacklog(boolean notify, Account account) {
synchronized (notifications) {
mXmppConnectionService.updateUnreadCountBadge();
if (account == null || !notify) {
updateNotification(notify);
} else {
final int count;
final List<String> conversations;
synchronized (this.mBacklogMessageCounter) {
conversations = getBacklogConversations(account);
count = getBacklogMessageCount(account);
}
updateNotification(count > 0, conversations);
}
}
}
private List<String> getBacklogConversations(Account account) {
final List<String> conversations = new ArrayList<>();
for (Map.Entry<Conversation, AtomicInteger> entry : mBacklogMessageCounter.entrySet()) {
if (entry.getKey().getAccount() == account) {
conversations.add(entry.getKey().getUuid());
}
}
return conversations;
}
private int getBacklogMessageCount(Account account) {
int count = 0;
for (Iterator<Map.Entry<Conversation, AtomicInteger>> it = mBacklogMessageCounter.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Conversation, AtomicInteger> entry = it.next();
if (entry.getKey().getAccount() == account) {
count += entry.getValue().get();
it.remove();
}
}
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": backlog message count=" + count);
return count;
}
void finishBacklog(boolean notify) {
finishBacklog(notify, null);
}
private void pushToStack(final Message message) {
final String conversationUuid = message.getConversationUuid();
if (notifications.containsKey(conversationUuid)) {
notifications.get(conversationUuid).add(message);
} else {
final ArrayList<Message> mList = new ArrayList<>();
mList.add(message);
notifications.put(conversationUuid, mList);
}
}
public void push(final Message message) {
synchronized (CATCHUP_LOCK) {
final XmppConnection connection = message.getConversation().getAccount().getXmppConnection();
if (connection != null && connection.isWaitingForSmCatchup()) {
connection.incrementSmCatchupMessageCounter();
pushFromBacklog(message);
} else {
pushNow(message);
}
}
}
private void pushNow(final Message message) {
mXmppConnectionService.updateUnreadCountBadge();
if (!notify(message)) {
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because turned off");
return;
}
final boolean isScreenOn = mXmppConnectionService.isInteractive();
if (this.mIsInForeground && isScreenOn && this.mOpenConversation == message.getConversation()) {
mXmppConnectionService.vibrate();
Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": suppressing notification because conversation is open");
return;
}
if (this.mIsInForeground && isScreenOn) {
mXmppConnectionService.vibrate();
}
synchronized (notifications) {
pushToStack(message);
final Conversational conversation = message.getConversation();
final Account account = conversation.getAccount();
final boolean doNotify = (!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
&& !account.inGracePeriod()
&& !this.inMiniGracePeriod(account);
updateNotification(doNotify, Collections.singletonList(conversation.getUuid()));
}
}
public void clear() {
synchronized (notifications) {
for (ArrayList<Message> messages : notifications.values()) {
markAsReadIfHasDirectReply(messages);
}
notifications.clear();
updateNotification(false);
}
}
public void clear(final Conversation conversation) {
synchronized (this.mBacklogMessageCounter) {
this.mBacklogMessageCounter.remove(conversation);
}
synchronized (notifications) {
markAsReadIfHasDirectReply(conversation);
if (notifications.remove(conversation.getUuid()) != null) {
cancel(conversation.getUuid(), NOTIFICATION_ID);
updateNotification(false, null, true);
}
}
}
private void markAsReadIfHasDirectReply(final Conversation conversation) {
markAsReadIfHasDirectReply(notifications.get(conversation.getUuid()));
}
private void markAsReadIfHasDirectReply(final ArrayList<Message> messages) {
if (messages != null && messages.size() > 0) {
Message last = messages.get(messages.size() - 1);
if (last.getStatus() != Message.STATUS_RECEIVED) {
if (mXmppConnectionService.markRead((Conversation) last.getConversation(), false)) {
mXmppConnectionService.updateConversationUi();
}
}
}
}
private void setNotificationColor(final Builder mBuilder) {
mBuilder.setColor(ContextCompat.getColor(mXmppConnectionService, R.color.primary));
}
public void updateNotification() {
synchronized (notifications) {
updateNotification(false);
}
}
private void updateNotification(final boolean notify) {
updateNotification(notify, null, false);
}
private void updateNotification(final boolean notify, final List<String> conversations) {
updateNotification(notify, conversations, false);
}
private void updateNotification(final boolean notify, final List<String> conversations, final boolean summaryOnly) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
final boolean quiteHours = isQuietHours();
final boolean notifyOnlyOneChild = notify && conversations != null && conversations.size() == 1; //if this check is changed to > 0 catchup messages will create one notification per conversation
if (notifications.size() == 0) {
cancel(NOTIFICATION_ID);
} else {
if (notify) {
this.markLastNotification();
}
final Builder mBuilder;
if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
mBuilder = buildSingleConversations(notifications.values().iterator().next(), notify, quiteHours);
modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
notify(NOTIFICATION_ID, mBuilder.build());
} else {
mBuilder = buildMultipleConversation(notify, quiteHours);
if (notifyOnlyOneChild) {
mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
}
modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
if (!summaryOnly) {
for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) {
String uuid = entry.getKey();
final boolean notifyThis = notifyOnlyOneChild ? conversations.contains(uuid) : notify;
Builder singleBuilder = buildSingleConversations(entry.getValue(), notifyThis, quiteHours);
if (!notifyOnlyOneChild) {
singleBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
}
modifyForSoundVibrationAndLight(singleBuilder, notifyThis, quiteHours, preferences);
singleBuilder.setGroup(CONVERSATIONS_GROUP);
setNotificationColor(singleBuilder);
notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build());
}
}
notify(NOTIFICATION_ID, mBuilder.build());
}
}
}
private void modifyForSoundVibrationAndLight(Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences) {
final Resources resources = mXmppConnectionService.getResources();
final String ringtone = preferences.getString("notification_ringtone", resources.getString(R.string.notification_ringtone));
final boolean vibrate = preferences.getBoolean("vibrate_on_notification", resources.getBoolean(R.bool.vibrate_on_notification));
final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led));
final boolean headsup = preferences.getBoolean("notification_headsup", resources.getBoolean(R.bool.headsup_notifications));
if (notify && !quietHours) {
if (vibrate) {
final int dat = 70;
final long[] pattern = {0, 3 * dat, dat, dat};
mBuilder.setVibrate(pattern);
} else {
mBuilder.setVibrate(new long[]{0});
}
Uri uri = Uri.parse(ringtone);
try {
mBuilder.setSound(fixRingtoneUri(uri));
} catch (SecurityException e) {
Log.d(Config.LOGTAG, "unable to use custom notification sound " + uri.toString());
}
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setCategory(Notification.CATEGORY_MESSAGE);
}
mBuilder.setPriority(notify ? (headsup ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_DEFAULT) : NotificationCompat.PRIORITY_LOW);
setNotificationColor(mBuilder);
mBuilder.setDefaults(0);
if (led) {
mBuilder.setLights(LED_COLOR, 2000, 3000);
}
}
private Uri fixRingtoneUri(Uri uri) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && "file".equals(uri.getScheme())) {
return FileBackend.getUriForFile(mXmppConnectionService, new File(uri.getPath()));
} else {
return uri;
}
}
private Builder buildMultipleConversation(final boolean notify, final boolean quietHours) {
final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages"));
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle(notifications.size()
+ " "
+ mXmppConnectionService
.getString(R.string.unread_conversations));
final StringBuilder names = new StringBuilder();
Conversation conversation = null;
for (final ArrayList<Message> messages : notifications.values()) {
if (messages.size() > 0) {
conversation = (Conversation) messages.get(0).getConversation();
final String name = conversation.getName().toString();
SpannableString styledString;
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
int count = messages.size();
styledString = new SpannableString(name + ": " + mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString);
} else {
styledString = new SpannableString(name + ": " + UIHelper.getMessagePreview(mXmppConnectionService, messages.get(0)).first);
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString);
}
names.append(name);
names.append(", ");
}
}
if (names.length() >= 2) {
names.delete(names.length() - 2, names.length());
}
mBuilder.setContentTitle(notifications.size()
+ " "
+ mXmppConnectionService
.getString(R.string.unread_conversations));
mBuilder.setContentText(names.toString());
mBuilder.setStyle(style);
if (conversation != null) {
mBuilder.setContentIntent(createOpenConversationsIntent());
}
mBuilder.setGroupSummary(true);
mBuilder.setGroup(CONVERSATIONS_GROUP);
mBuilder.setDeleteIntent(createDeleteIntent(null));
mBuilder.setSmallIcon(R.drawable.ic_notification);
return mBuilder;
}
private Builder buildSingleConversations(final ArrayList<Message> messages, final boolean notify, final boolean quietHours) {
final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService, quietHours ? "quiet_hours" : (notify ? "messages" : "silent_messages"));
if (messages.size() >= 1) {
final Conversation conversation = (Conversation) messages.get(0).getConversation();
final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString());
mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService().get(conversation, AvatarService.getSystemUiAvatarSize(mXmppConnectionService)));
mBuilder.setContentTitle(conversation.getName());
if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) {
int count = messages.size();
mBuilder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
} else {
Message message;
//TODO starting with Android 9 we might want to put images in MessageStyle
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P && (message = getImage(messages)) != null) {
modifyForImage(mBuilder, mUnreadBuilder, message, messages);
} else {
modifyForTextOnly(mBuilder, mUnreadBuilder, messages);
}
RemoteInput remoteInput = new RemoteInput.Builder("text_reply").setLabel(UIHelper.getMessageHint(mXmppConnectionService, conversation)).build();
PendingIntent markAsReadPendingIntent = createReadPendingIntent(conversation);
NotificationCompat.Action markReadAction = new NotificationCompat.Action.Builder(
R.drawable.ic_email_open_outline_white_24dp,
mXmppConnectionService.getString(R.string.mark_as_read),
markAsReadPendingIntent).build();
String replyLabel = mXmppConnectionService.getString(R.string.reply);
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
R.drawable.ic_reply_white_24dp,
replyLabel,
createReplyIntent(conversation, false)).addRemoteInput(remoteInput).build();
NotificationCompat.Action wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_wear_reply,
replyLabel,
createReplyIntent(conversation, true)).addRemoteInput(remoteInput).build();
mBuilder.extend(new NotificationCompat.WearableExtender().addAction(wearReplyAction));
mUnreadBuilder.setReplyAction(createReplyIntent(conversation, true), remoteInput);
mUnreadBuilder.setReadPendingIntent(markAsReadPendingIntent);
mBuilder.extend(new NotificationCompat.CarExtender().setUnreadConversation(mUnreadBuilder.build()));
int addedActionsCount = 1;
mBuilder.addAction(markReadAction);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mBuilder.addAction(replyAction);
++addedActionsCount;
}
if (displaySnoozeAction(messages)) {
String label = mXmppConnectionService.getString(R.string.snooze);
PendingIntent pendingSnoozeIntent = createSnoozeIntent(conversation);
NotificationCompat.Action snoozeAction = new NotificationCompat.Action.Builder(
R.drawable.ic_notifications_paused_white_24dp,
label,
pendingSnoozeIntent).build();
mBuilder.addAction(snoozeAction);
++addedActionsCount;
}
if (addedActionsCount < 3) {
final Message firstLocationMessage = getFirstLocationMessage(messages);
if (firstLocationMessage != null) {
String label = mXmppConnectionService.getResources().getString(R.string.show_location);
PendingIntent pendingShowLocationIntent = createShowLocationIntent(firstLocationMessage);
NotificationCompat.Action locationAction = new NotificationCompat.Action.Builder(
R.drawable.ic_room_white_24dp,
label,
pendingShowLocationIntent).build();
mBuilder.addAction(locationAction);
++addedActionsCount;
}
}
if (addedActionsCount < 3) {
Message firstDownloadableMessage = getFirstDownloadableMessage(messages);
if (firstDownloadableMessage != null) {
String label = mXmppConnectionService.getResources().getString(R.string.download_x_file, UIHelper.getFileDescriptionString(mXmppConnectionService, firstDownloadableMessage));
PendingIntent pendingDownloadIntent = createDownloadIntent(firstDownloadableMessage);
NotificationCompat.Action downloadAction = new NotificationCompat.Action.Builder(
R.drawable.ic_file_download_white_24dp,
label,
pendingDownloadIntent).build();
mBuilder.addAction(downloadAction);
++addedActionsCount;
}
}
}
if (conversation.getMode() == Conversation.MODE_SINGLE) {
Contact contact = conversation.getContact();
Uri systemAccount = contact.getSystemAccount();
if (systemAccount != null) {
mBuilder.addPerson(systemAccount.toString());
}
}
mBuilder.setWhen(conversation.getLatestMessage().getTimeSent());
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setDeleteIntent(createDeleteIntent(conversation));
mBuilder.setContentIntent(createContentIntent(conversation));
}
return mBuilder;
}
private void modifyForImage(final Builder builder, final UnreadConversation.Builder uBuilder,
final Message message, final ArrayList<Message> messages) {
try {
final Bitmap bitmap = mXmppConnectionService.getFileBackend().getThumbnail(message, getPixel(288), false);
final ArrayList<Message> tmp = new ArrayList<>();
for (final Message msg : messages) {
if (msg.getType() == Message.TYPE_TEXT
&& msg.getTransferable() == null) {
tmp.add(msg);
}
}
final BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.bigPicture(bitmap);
if (tmp.size() > 0) {
CharSequence text = getMergedBodies(tmp);
bigPictureStyle.setSummaryText(text);
builder.setContentText(text);
} else {
builder.setContentText(UIHelper.getFileDescriptionString(mXmppConnectionService, message));
}
builder.setStyle(bigPictureStyle);
} catch (final IOException e) {
modifyForTextOnly(builder, uBuilder, messages);
}
}
private Person getPerson(Message message) {
final Contact contact = message.getContact();
final Person.Builder builder = new Person.Builder();
if (contact != null) {
builder.setName(contact.getDisplayName());
final Uri uri = contact.getSystemAccount();
if (uri != null) {
builder.setUri(uri.toString());
}
} else {
builder.setName(UIHelper.getMessageDisplayName(message));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
builder.setIcon(IconCompat.createWithBitmap(mXmppConnectionService.getAvatarService().get(message, AvatarService.getSystemUiAvatarSize(mXmppConnectionService), false)));
}
return builder.build();
}
private void modifyForTextOnly(final Builder builder, final UnreadConversation.Builder uBuilder, final ArrayList<Message> messages) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
final Conversation conversation = (Conversation) messages.get(0).getConversation();
final Person.Builder meBuilder = new Person.Builder().setName(mXmppConnectionService.getString(R.string.me));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
meBuilder.setIcon(IconCompat.createWithBitmap(mXmppConnectionService.getAvatarService().get(conversation.getAccount(), AvatarService.getSystemUiAvatarSize(mXmppConnectionService))));
}
final Person me = meBuilder.build();
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(me);
final boolean multiple = conversation.getMode() == Conversation.MODE_MULTI;
if (multiple) {
messagingStyle.setConversationTitle(conversation.getName());
}
for (Message message : messages) {
final Person sender = message.getStatus() == Message.STATUS_RECEIVED ? getPerson(message) : null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && isImageMessage(message)) {
final Uri dataUri = FileBackend.getMediaUri(mXmppConnectionService, mXmppConnectionService.getFileBackend().getFile(message));
NotificationCompat.MessagingStyle.Message imageMessage = new NotificationCompat.MessagingStyle.Message(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender);
if (dataUri != null) {
imageMessage.setData(message.getMimeType(), dataUri);
}
messagingStyle.addMessage(imageMessage);
} else {
messagingStyle.addMessage(UIHelper.getMessagePreview(mXmppConnectionService, message).first, message.getTimeSent(), sender);
}
}
messagingStyle.setGroupConversation(multiple);
builder.setStyle(messagingStyle);
} else {
if (messages.get(0).getConversation().getMode() == Conversation.MODE_SINGLE) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(getMergedBodies(messages)));
builder.setContentText(UIHelper.getMessagePreview(mXmppConnectionService, messages.get(messages.size() - 1)).first);
builder.setNumber(messages.size());
} else {
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
SpannableString styledString;
for (Message message : messages) {
final String name = UIHelper.getMessageDisplayName(message);
styledString = new SpannableString(name + ": " + message.getBody());
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
style.addLine(styledString);
}
builder.setStyle(style);
int count = messages.size();
if (count == 1) {
final String name = UIHelper.getMessageDisplayName(messages.get(0));
styledString = new SpannableString(name + ": " + messages.get(0).getBody());
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
builder.setContentText(styledString);
} else {
builder.setContentText(mXmppConnectionService.getResources().getQuantityString(R.plurals.x_messages, count, count));
}
}
}
/** message preview for Android Auto **/
for (Message message : messages) {
Pair<CharSequence, Boolean> preview = UIHelper.getMessagePreview(mXmppConnectionService, message);
// only show user written text
if (!preview.second) {
uBuilder.addMessage(preview.first.toString());
uBuilder.setLatestTimestamp(message.getTimeSent());
}
}
}
private Message getImage(final Iterable<Message> messages) {
Message image = null;
for (final Message message : messages) {
if (message.getStatus() != Message.STATUS_RECEIVED) {
return null;
}
if (isImageMessage(message)) {
image = message;
}
}
return image;
}
private static boolean isImageMessage(Message message) {
return message.getType() != Message.TYPE_TEXT
&& message.getTransferable() == null
&& !message.isFileDeleted()
&& message.getEncryption() != Message.ENCRYPTION_PGP
&& message.getFileParams().height > 0;
}
private Message getFirstDownloadableMessage(final Iterable<Message> messages) {
for (final Message message : messages) {
if (message.getTransferable() != null || (message.getType() == Message.TYPE_TEXT && message.treatAsDownloadable())) {
return message;
}
}
return null;
}
private Message getFirstLocationMessage(final Iterable<Message> messages) {
for (final Message message : messages) {
if (message.isGeoUri()) {
return message;
}
}
return null;
}
private CharSequence getMergedBodies(final ArrayList<Message> messages) {
final StringBuilder text = new StringBuilder();
for (Message message : messages) {
if (text.length() != 0) {
text.append("\n");
}
text.append(UIHelper.getMessagePreview(mXmppConnectionService, message).first);
}
return text.toString();
}
private PendingIntent createShowLocationIntent(final Message message) {
Iterable<Intent> intents = GeoHelper.createGeoIntentsFromMessage(mXmppConnectionService, message);
for (Intent intent : intents) {
if (intent.resolveActivity(mXmppConnectionService.getPackageManager()) != null) {
return PendingIntent.getActivity(mXmppConnectionService, generateRequestCode(message.getConversation(), 18), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
return createOpenConversationsIntent();
}
private PendingIntent createContentIntent(final String conversationUuid, final String downloadMessageUuid) {
final Intent viewConversationIntent = new Intent(mXmppConnectionService, ConversationsActivity.class);
viewConversationIntent.setAction(ConversationsActivity.ACTION_VIEW_CONVERSATION);
viewConversationIntent.putExtra(ConversationsActivity.EXTRA_CONVERSATION, conversationUuid);
if (downloadMessageUuid != null) {
viewConversationIntent.putExtra(ConversationsActivity.EXTRA_DOWNLOAD_UUID, downloadMessageUuid);
return PendingIntent.getActivity(mXmppConnectionService,
generateRequestCode(conversationUuid, 8),
viewConversationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
} else {
return PendingIntent.getActivity(mXmppConnectionService,
generateRequestCode(conversationUuid, 10),
viewConversationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
private int generateRequestCode(String uuid, int actionId) {
return (actionId * NOTIFICATION_ID_MULTIPLIER) + (uuid.hashCode() % NOTIFICATION_ID_MULTIPLIER);
}
private int generateRequestCode(Conversational conversation, int actionId) {
return generateRequestCode(conversation.getUuid(), actionId);
}
private PendingIntent createDownloadIntent(final Message message) {
return createContentIntent(message.getConversationUuid(), message.getUuid());
}
private PendingIntent createContentIntent(final Conversational conversation) {
return createContentIntent(conversation.getUuid(), null);
}
private PendingIntent createDeleteIntent(Conversation conversation) {
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_CLEAR_NOTIFICATION);
if (conversation != null) {
intent.putExtra("uuid", conversation.getUuid());
return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 20), intent, 0);
}
return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
}
private PendingIntent createReplyIntent(Conversation conversation, boolean dismissAfterReply) {
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_REPLY_TO_CONVERSATION);
intent.putExtra("uuid", conversation.getUuid());
intent.putExtra("dismiss_notification", dismissAfterReply);
final int id = generateRequestCode(conversation, dismissAfterReply ? 12 : 14);
return PendingIntent.getService(mXmppConnectionService, id, intent, 0);
}
private PendingIntent createReadPendingIntent(Conversation conversation) {
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_MARK_AS_READ);
intent.putExtra("uuid", conversation.getUuid());
intent.setPackage(mXmppConnectionService.getPackageName());
return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 16), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent createSnoozeIntent(Conversation conversation) {
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_SNOOZE);
intent.putExtra("uuid", conversation.getUuid());
intent.setPackage(mXmppConnectionService.getPackageName());
return PendingIntent.getService(mXmppConnectionService, generateRequestCode(conversation, 22), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent createTryAgainIntent() {
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_TRY_AGAIN);
return PendingIntent.getService(mXmppConnectionService, 45, intent, 0);
}
private PendingIntent createDismissErrorIntent() {
final Intent intent = new Intent(mXmppConnectionService, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_DISMISS_ERROR_NOTIFICATIONS);
return PendingIntent.getService(mXmppConnectionService, 69, intent, 0);
}
private boolean wasHighlightedOrPrivate(final Message message) {
if (message.getConversation() instanceof Conversation) {
Conversation conversation = (Conversation) message.getConversation();
final String nick = conversation.getMucOptions().getActualNick();
final Pattern highlight = generateNickHighlightPattern(nick);
if (message.getBody() == null || nick == null) {
return false;
}
final Matcher m = highlight.matcher(message.getBody());
return (m.find() || message.isPrivateMessage());
} else {
return false;
}
}
public void setOpenConversation(final Conversation conversation) {
this.mOpenConversation = conversation;
}
public void setIsInForeground(final boolean foreground) {
this.mIsInForeground = foreground;
}
private int getPixel(final int dp) {
final DisplayMetrics metrics = mXmppConnectionService.getResources()
.getDisplayMetrics();
return ((int) (dp * metrics.density));
}
private void markLastNotification() {
this.mLastNotification = SystemClock.elapsedRealtime();
}
private boolean inMiniGracePeriod(final Account account) {
final int miniGrace = account.getStatus() == Account.State.ONLINE ? Config.MINI_GRACE_PERIOD
: Config.MINI_GRACE_PERIOD * 2;
return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace);
}
Notification createForegroundNotification() {
final Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
Account mAccount = null;
String status;
final List<Account> accounts = mXmppConnectionService.getAccounts();
int enabled = 0;
int connected = 0;
if (accounts != null) {
for (Account account : accounts) {
if (account.isOnlineAndConnected()) {
connected++;
enabled++;
} else if (account.isEnabled()) {
enabled++;
}
}
}
if (accounts.size() == 1) {
mAccount = accounts.get(0);
if (mAccount.getStatus() == Account.State.ONLINE) {
status = "(" + mXmppConnectionService.getString(R.string.account_status_online) + ")";
status = " " + status;
Log.d(Config.LOGTAG, "Status: " + status);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
} else if (mAccount.getStatus() == Account.State.CONNECTING) {
status = "(" + mXmppConnectionService.getString(R.string.account_status_connecting) + ")";
status = " " + status;
Log.d(Config.LOGTAG, "Status: " + status);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
} else {
status = "(" + mXmppConnectionService.getString(R.string.account_status_offline) + ")";
status = " " + status;
Log.d(Config.LOGTAG, "Status: " + status);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
}
} else if (accounts.size() > 1) {
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
} else {
status = "(" + mXmppConnectionService.getString(R.string.account_status_offline) + ")";
status = " " + status;
Log.d(Config.LOGTAG, "Status: " + status);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
}
mBuilder.setContentText(mXmppConnectionService.getString(R.string.connected_accounts, connected, enabled));
mBuilder.setContentIntent(createOpenConversationsIntent());
mBuilder.setWhen(0);
mBuilder.setPriority(Notification.PRIORITY_MIN);
mBuilder.setSmallIcon(connected > 0 ? R.drawable.ic_link_white_24dp : R.drawable.ic_link_off_white_24dp);
if (Compatibility.runsTwentySix()) {
mBuilder.setChannelId(FOREGROUND_CHANNEL_ID);
}
return mBuilder.build();
}
private PendingIntent createOpenConversationsIntent() {
try {
return PendingIntent.getActivity(mXmppConnectionService, 0, new Intent(mXmppConnectionService, ConversationsActivity.class), 0);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
void updateErrorNotification() {
if (Config.SUPPRESS_ERROR_NOTIFICATION) {
cancel(ERROR_NOTIFICATION_ID);
return;
}
final boolean showAllErrors = QuickConversationsService.isConversations();
final List<Account> errors = new ArrayList<>();
for (final Account account : mXmppConnectionService.getAccounts()) {
if (account.hasErrorStatus() && account.showErrorNotification() && (showAllErrors || account.getLastErrorStatus() == Account.State.UNAUTHORIZED)) {
errors.add(account);
}
}
if (mXmppConnectionService.foregroundNotificationNeedsUpdatingWhenErrorStateChanges()) {
notify(FOREGROUND_NOTIFICATION_ID, createForegroundNotification());
}
final Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
if (errors.size() == 0) {
cancel(ERROR_NOTIFICATION_ID);
return;
} else if (errors.size() == 1) {
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_account));
mBuilder.setContentText(errors.get(0).getJid().asBareJid().toString());
} else {
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.problem_connecting_to_accounts));
mBuilder.setContentText(mXmppConnectionService.getString(R.string.touch_to_fix));
}
mBuilder.addAction(R.drawable.ic_autorenew_white_24dp,
mXmppConnectionService.getString(R.string.try_again),
createTryAgainIntent());
mBuilder.setDeleteIntent(createDismissErrorIntent());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
mBuilder.setSmallIcon(R.drawable.ic_warning_white_24dp);
} else {
mBuilder.setSmallIcon(R.drawable.ic_warning_white_24dp);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
mBuilder.setLocalOnly(true);
}
mBuilder.setPriority(Notification.PRIORITY_LOW);
final Intent intent;
if (AccountUtils.MANAGE_ACCOUNT_ACTIVITY != null) {
intent = new Intent(mXmppConnectionService, AccountUtils.MANAGE_ACCOUNT_ACTIVITY);
} else {
intent = new Intent(mXmppConnectionService, EditAccountActivity.class);
intent.putExtra("jid", errors.get(0).getJid().asBareJid().toEscapedString());
intent.putExtra(EditAccountActivity.EXTRA_OPENED_FROM_NOTIFICATION, true);
}
mBuilder.setContentIntent(PendingIntent.getActivity(mXmppConnectionService, 145, intent, PendingIntent.FLAG_UPDATE_CURRENT));
if (Compatibility.runsTwentySix()) {
mBuilder.setChannelId(ERROR_CHANNEL_ID);
}
notify(ERROR_NOTIFICATION_ID, mBuilder.build());
}
void updateFileAddingNotification(int current, Message message) {
Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.transcoding_video));
mBuilder.setProgress(100, current, false);
mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp);
mBuilder.setContentIntent(createContentIntent(message.getConversation()));
mBuilder.setOngoing(true);
if (Compatibility.runsTwentySix()) {
mBuilder.setChannelId(VIDEOCOMPRESSION_CHANNEL_ID);
}
Notification notification = mBuilder.build();
notify(FOREGROUND_NOTIFICATION_ID, notification);
}
void dismissForcedForegroundNotification() {
cancel(FOREGROUND_NOTIFICATION_ID);
}
Notification AppUpdateNotification(PendingIntent intent, String version, String filesize) {
Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.app_name));
mBuilder.setContentText(String.format(mXmppConnectionService.getString(R.string.update_available), version, filesize));
mBuilder.setSmallIcon(R.drawable.ic_update_notification);
mBuilder.setContentIntent(intent);
mBuilder.setOngoing(true);
if (Compatibility.runsTwentySix()) {
mBuilder.setChannelId(UPDATE_CHANNEL_ID);
}
return mBuilder.build();
}
public void AppUpdateServiceNotification(Notification notification) {
notify(FOREGROUND_NOTIFICATION_ID, notification);
}
private void notify(String tag, int id, Notification notification) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.notify(tag, id, notification);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to make notification", e);
}
}
public void notify(int id, Notification notification) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.notify(id, notification);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to make notification", e);
}
}
private void cancel(int id) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.cancel(id);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to cancel notification", e);
}
}
private void cancel(String tag, int id) {
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mXmppConnectionService);
try {
notificationManager.cancel(tag, id);
} catch (RuntimeException e) {
Log.d(Config.LOGTAG, "unable to cancel notification", e);
}
}
}
|