1package eu.siacs.conversations.services;
2
3import java.util.ArrayList;
4import java.util.LinkedHashMap;
5import java.util.regex.Matcher;
6import java.util.regex.Pattern;
7
8import android.app.Notification;
9import android.app.NotificationManager;
10import android.app.PendingIntent;
11import android.content.Context;
12import android.content.Intent;
13import android.content.SharedPreferences;
14import android.net.Uri;
15import android.os.PowerManager;
16import android.support.v4.app.NotificationCompat;
17import android.support.v4.app.TaskStackBuilder;
18import android.text.Html;
19
20import eu.siacs.conversations.R;
21import eu.siacs.conversations.entities.Conversation;
22import eu.siacs.conversations.entities.Message;
23import eu.siacs.conversations.ui.ConversationActivity;
24
25public class NotificationService {
26
27 private XmppConnectionService mXmppConnectionService;
28 private NotificationManager mNotificationManager;
29
30 private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
31
32 public int NOTIFICATION_ID = 0x2342;
33 private Conversation mOpenConversation;
34 private boolean mIsInForeground;
35
36 public NotificationService(XmppConnectionService service) {
37 this.mXmppConnectionService = service;
38 this.mNotificationManager = (NotificationManager) service
39 .getSystemService(Context.NOTIFICATION_SERVICE);
40 }
41
42 public synchronized void push(Message message) {
43
44 PowerManager pm = (PowerManager) mXmppConnectionService
45 .getSystemService(Context.POWER_SERVICE);
46 boolean isScreenOn = pm.isScreenOn();
47
48 if (this.mIsInForeground && isScreenOn
49 && this.mOpenConversation == message.getConversation()) {
50 return; // simply ignore
51 }
52 String conversationUuid = message.getConversationUuid();
53 if (notifications.containsKey(conversationUuid)) {
54 notifications.get(conversationUuid).add(message);
55 } else {
56 ArrayList<Message> mList = new ArrayList<Message>();
57 mList.add(message);
58 notifications.put(conversationUuid, mList);
59 }
60 updateNotification(!(this.mIsInForeground && this.mOpenConversation == null)
61 || !isScreenOn);
62 }
63
64 public void clear() {
65 notifications.clear();
66 updateNotification(false);
67 }
68
69 public void clear(Conversation conversation) {
70 notifications.remove(conversation.getUuid());
71 updateNotification(false);
72 }
73
74 private void updateNotification(boolean notify) {
75 SharedPreferences preferences = mXmppConnectionService.getPreferences();
76
77 String ringtone = preferences.getString("notification_ringtone", null);
78 boolean vibrate = preferences.getBoolean("vibrate_on_notification",
79 true);
80
81 if (notifications.size() == 0) {
82 mNotificationManager.cancel(NOTIFICATION_ID);
83 } else {
84 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
85 mXmppConnectionService);
86 mBuilder.setSmallIcon(R.drawable.ic_notification);
87 if (notifications.size() == 1) {
88 ArrayList<Message> messages = notifications.values().iterator()
89 .next();
90 if (messages.size() >= 1) {
91 Conversation conversation = messages.get(0)
92 .getConversation();
93 mBuilder.setLargeIcon(conversation.getImage(
94 mXmppConnectionService, 64));
95 mBuilder.setContentTitle(conversation.getName());
96 StringBuilder text = new StringBuilder();
97 for (int i = 0; i < messages.size(); ++i) {
98 text.append(messages.get(i).getReadableBody(
99 mXmppConnectionService));
100 if (i != messages.size() - 1) {
101 text.append("\n");
102 }
103 }
104 mBuilder.setStyle(new NotificationCompat.BigTextStyle()
105 .bigText(text.toString()));
106 mBuilder.setContentText(messages.get(0).getReadableBody(
107 mXmppConnectionService));
108 if (notify) {
109 mBuilder.setTicker(messages.get(messages.size() - 1)
110 .getReadableBody(mXmppConnectionService));
111 }
112 mBuilder.setContentIntent(createContentIntent(conversation
113 .getUuid()));
114 } else {
115 mNotificationManager.cancel(NOTIFICATION_ID);
116 return;
117 }
118 } else {
119 NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
120 style.setBigContentTitle(notifications.size()
121 + " "
122 + mXmppConnectionService
123 .getString(R.string.unread_conversations));
124 StringBuilder names = new StringBuilder();
125 Conversation conversation = null;
126 for (ArrayList<Message> messages : notifications.values()) {
127 if (messages.size() > 0) {
128 conversation = messages.get(0).getConversation();
129 String name = conversation.getName();
130 style.addLine(Html.fromHtml("<b>"
131 + name
132 + "</b> "
133 + messages.get(0).getReadableBody(
134 mXmppConnectionService)));
135 names.append(name);
136 names.append(", ");
137 }
138 }
139 if (names.length() >= 2) {
140 names.delete(names.length() - 2, names.length());
141 }
142 mBuilder.setContentTitle(notifications.size()
143 + " "
144 + mXmppConnectionService
145 .getString(R.string.unread_conversations));
146 mBuilder.setContentText(names.toString());
147 mBuilder.setStyle(style);
148 if (conversation != null) {
149 mBuilder.setContentIntent(createContentIntent(conversation
150 .getUuid()));
151 }
152 }
153 if (notify) {
154 if (vibrate) {
155 int dat = 70;
156 long[] pattern = { 0, 3 * dat, dat, dat };
157 mBuilder.setVibrate(pattern);
158 }
159 if (ringtone != null) {
160 mBuilder.setSound(Uri.parse(ringtone));
161 }
162 }
163 mBuilder.setDeleteIntent(createDeleteIntent());
164 mBuilder.setLights(0xffffffff, 2000, 4000);
165 Notification notification = mBuilder.build();
166 mNotificationManager.notify(NOTIFICATION_ID, notification);
167 }
168 }
169
170 private PendingIntent createContentIntent(String conversationUuid) {
171 TaskStackBuilder stackBuilder = TaskStackBuilder
172 .create(mXmppConnectionService);
173 stackBuilder.addParentStack(ConversationActivity.class);
174
175 Intent viewConversationIntent = new Intent(mXmppConnectionService,
176 ConversationActivity.class);
177 viewConversationIntent.setAction(Intent.ACTION_VIEW);
178 viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
179 conversationUuid);
180 viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
181
182 stackBuilder.addNextIntent(viewConversationIntent);
183
184 PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
185 PendingIntent.FLAG_UPDATE_CURRENT);
186 return resultPendingIntent;
187 }
188
189 private PendingIntent createDeleteIntent() {
190 Intent intent = new Intent(mXmppConnectionService,
191 XmppConnectionService.class);
192 intent.setAction("clear_notification");
193 return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
194 }
195
196 public static boolean wasHighlightedOrPrivate(Message message) {
197 String nick = message.getConversation().getMucOptions().getActualNick();
198 Pattern highlight = generateNickHighlightPattern(nick);
199 if (message.getBody() == null || nick == null) {
200 return false;
201 }
202 Matcher m = highlight.matcher(message.getBody());
203 return (m.find() || message.getType() == Message.TYPE_PRIVATE);
204 }
205
206 private static Pattern generateNickHighlightPattern(String nick) {
207 // We expect a word boundary, i.e. space or start of string, followed by
208 // the
209 // nick (matched in case-insensitive manner), followed by optional
210 // punctuation (for example "bob: i disagree" or "how are you alice?"),
211 // followed by another word boundary.
212 return Pattern.compile("\\b" + nick + "\\p{Punct}?\\b",
213 Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
214 }
215
216 public void setOpenConversation(Conversation conversation) {
217 this.mOpenConversation = conversation;
218 }
219
220 public void setIsInForeground(boolean foreground) {
221 this.mIsInForeground = foreground;
222 }
223
224}