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