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.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.Account;
 24import eu.siacs.conversations.entities.Conversation;
 25import eu.siacs.conversations.entities.Message;
 26import eu.siacs.conversations.ui.ConversationActivity;
 27
 28public class NotificationService {
 29
 30	private XmppConnectionService mXmppConnectionService;
 31	private NotificationManager mNotificationManager;
 32
 33	private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
 34
 35	public int NOTIFICATION_ID = 0x2342;
 36	private Conversation mOpenConversation;
 37	private boolean mIsInForeground;
 38	
 39	public NotificationService(XmppConnectionService service) {
 40		this.mXmppConnectionService = service;
 41		this.mNotificationManager = (NotificationManager) service
 42				.getSystemService(Context.NOTIFICATION_SERVICE);
 43	}
 44
 45	public void push(Message message) {
 46		PowerManager pm = (PowerManager) mXmppConnectionService
 47				.getSystemService(Context.POWER_SERVICE);
 48		boolean isScreenOn = pm.isScreenOn();
 49
 50		if (this.mIsInForeground && isScreenOn
 51				&& this.mOpenConversation == message.getConversation()) {
 52			return;
 53		}
 54		synchronized (notifications) {
 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		}
 64		Account account = message.getConversation().getAccount();
 65		updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
 66				&& !account.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			mBuilder.setLights(0xffffffff, 2000, 4000);
174			Notification notification = mBuilder.build();
175			mNotificationManager.notify(NOTIFICATION_ID, notification);
176		}
177	}
178
179	private PendingIntent createContentIntent(String conversationUuid) {
180		TaskStackBuilder stackBuilder = TaskStackBuilder
181				.create(mXmppConnectionService);
182		stackBuilder.addParentStack(ConversationActivity.class);
183
184		Intent viewConversationIntent = new Intent(mXmppConnectionService,
185				ConversationActivity.class);
186		viewConversationIntent.setAction(Intent.ACTION_VIEW);
187		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
188				conversationUuid);
189		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
190
191		stackBuilder.addNextIntent(viewConversationIntent);
192
193		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
194				PendingIntent.FLAG_UPDATE_CURRENT);
195		return resultPendingIntent;
196	}
197
198	private PendingIntent createDeleteIntent() {
199		Intent intent = new Intent(mXmppConnectionService,
200				XmppConnectionService.class);
201		intent.setAction("clear_notification");
202		return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
203	}
204
205	public static boolean wasHighlightedOrPrivate(Message message) {
206		String nick = message.getConversation().getMucOptions().getActualNick();
207		Pattern highlight = generateNickHighlightPattern(nick);
208		if (message.getBody() == null || nick == null) {
209			return false;
210		}
211		Matcher m = highlight.matcher(message.getBody());
212		return (m.find() || message.getType() == Message.TYPE_PRIVATE);
213	}
214
215	private static Pattern generateNickHighlightPattern(String nick) {
216		// We expect a word boundary, i.e. space or start of string, followed by
217		// the
218		// nick (matched in case-insensitive manner), followed by optional
219		// punctuation (for example "bob: i disagree" or "how are you alice?"),
220		// followed by another word boundary.
221		return Pattern.compile("\\b" + nick + "\\p{Punct}?\\b",
222				Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
223	}
224
225	public void setOpenConversation(Conversation conversation) {
226		this.mOpenConversation = conversation;
227	}
228
229	public void setIsInForeground(boolean foreground) {
230		this.mIsInForeground = foreground;
231	}
232}