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