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