aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/services/NotificationService.java
blob: 831ce51ff5ca0670e9c56ab5140b8dff77d1dc23 (plain)
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
package eu.siacs.conversations.services;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.text.Html;
import android.util.Log;

import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.ui.ConversationActivity;

public class NotificationService {

	private XmppConnectionService mXmppConnectionService;
	private NotificationManager mNotificationManager;

	private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();

	public int NOTIFICATION_ID = 0x2342;
	private Conversation mOpenConversation;
	private boolean mIsInForeground;

	public NotificationService(XmppConnectionService service) {
		this.mXmppConnectionService = service;
		this.mNotificationManager = (NotificationManager) service
				.getSystemService(Context.NOTIFICATION_SERVICE);
	}

	public synchronized void push(Message message) {
		if (this.mIsInForeground
				&& this.mOpenConversation == message.getConversation()) {
			Log.d(Config.LOGTAG,"ignoring notification because foreground and conv matches");
			return; // simply ignore
		} else {
			Log.d(Config.LOGTAG,"pushed new notification");
		}
		String conversationUuid = message.getConversationUuid();
		if (notifications.containsKey(conversationUuid)) {
			notifications.get(conversationUuid).add(message);
		} else {
			ArrayList<Message> mList = new ArrayList<Message>();
			mList.add(message);
			notifications.put(conversationUuid, mList);
		}
		updateNotification(!(this.mIsInForeground && this.mOpenConversation == null));
	}

	public void clear() {
		notifications.clear();
		updateNotification(false);
	}

	public void clear(Conversation conversation) {
		notifications.remove(conversation.getUuid());
		updateNotification(false);
	}

	private void updateNotification(boolean notify) {
		SharedPreferences preferences = mXmppConnectionService.getPreferences();

		String ringtone = preferences.getString("notification_ringtone", null);
		boolean vibrate = preferences.getBoolean("vibrate_on_notification",
				true);

		if (notifications.size() == 0) {
			mNotificationManager.cancel(NOTIFICATION_ID);
		} else {
			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
					mXmppConnectionService);
			mBuilder.setSmallIcon(R.drawable.ic_notification);
			if (notifications.size() == 1) {
				ArrayList<Message> messages = notifications.values().iterator()
						.next();
				if (messages.size() >= 1) {
					Conversation conversation = messages.get(0)
							.getConversation();
					mBuilder.setLargeIcon(conversation.getImage(
							mXmppConnectionService, 64));
					mBuilder.setContentTitle(conversation.getName());
					StringBuilder text = new StringBuilder();
					for (int i = 0; i < messages.size(); ++i) {
						text.append(messages.get(i).getReadableBody(
								mXmppConnectionService));
						if (i != messages.size() - 1) {
							text.append("\n");
						}
					}
					mBuilder.setStyle(new NotificationCompat.BigTextStyle()
							.bigText(text.toString()));
					mBuilder.setContentText(messages.get(0).getReadableBody(
							mXmppConnectionService));
					if (notify) {
						mBuilder.setTicker(messages.get(messages.size() - 1)
								.getReadableBody(mXmppConnectionService));
					}
					mBuilder.setContentIntent(createContentIntent(conversation
							.getUuid()));
				} else {
					mNotificationManager.cancel(NOTIFICATION_ID);
					return;
				}
			} else {
				NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
				style.setBigContentTitle(notifications.size()
						+ " "
						+ mXmppConnectionService
								.getString(R.string.unread_conversations));
				StringBuilder names = new StringBuilder();
				for (ArrayList<Message> messages : notifications.values()) {
					if (messages.size() > 0) {
						String name = messages.get(0).getConversation()
								.getName();
						style.addLine(Html.fromHtml("<b>"
								+ name
								+ "</b> "
								+ messages.get(0).getReadableBody(
										mXmppConnectionService)));
						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 (notify) {
				if (vibrate) {
					int dat = 70;
					long[] pattern = { 0, 3 * dat, dat, dat };
					mBuilder.setVibrate(pattern);
				}
				if (ringtone != null) {
					mBuilder.setSound(Uri.parse(ringtone));
				}
			}
			mBuilder.setLights(0xffffffff, 2000, 4000);
			Notification notification = mBuilder.build();
			mNotificationManager.notify(NOTIFICATION_ID, notification);
		}
	}

	private PendingIntent createContentIntent(String conversationUuid) {
		TaskStackBuilder stackBuilder = TaskStackBuilder
				.create(mXmppConnectionService);
		stackBuilder.addParentStack(ConversationActivity.class);

		Intent viewConversationIntent = new Intent(mXmppConnectionService,
				ConversationActivity.class);
		viewConversationIntent.setAction(Intent.ACTION_VIEW);
		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
				conversationUuid);
		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);

		stackBuilder.addNextIntent(viewConversationIntent);

		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
				PendingIntent.FLAG_UPDATE_CURRENT);
		return resultPendingIntent;
	}

	public static boolean wasHighlightedOrPrivate(Message message) {
		String nick = message.getConversation().getMucOptions().getActualNick();
		Pattern highlight = generateNickHighlightPattern(nick);
		Matcher m = highlight.matcher(message.getBody());
		return (m.find() || message.getType() == Message.TYPE_PRIVATE);
	}

	private static Pattern generateNickHighlightPattern(String nick) {
		// We expect a word boundary, i.e. space or start of string, followed by
		// the
		// nick (matched in case-insensitive manner), followed by optional
		// punctuation (for example "bob: i disagree" or "how are you alice?"),
		// followed by another word boundary.
		return Pattern.compile("\\b" + nick + "\\p{Punct}?\\b",
				Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
	}

	public void setOpenConversation(Conversation conversation) {
		this.mOpenConversation = conversation;
	}

	public void setIsInForeground(boolean foreground) {
		this.mIsInForeground = foreground;
	}

}