NotificationService.java

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