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;
 20import android.util.DisplayMetrics;
 21
 22import eu.siacs.conversations.Config;
 23import eu.siacs.conversations.R;
 24import eu.siacs.conversations.entities.Account;
 25import eu.siacs.conversations.entities.Conversation;
 26import eu.siacs.conversations.entities.Downloadable;
 27import eu.siacs.conversations.entities.Message;
 28import eu.siacs.conversations.ui.ConversationActivity;
 29
 30public class NotificationService {
 31
 32	private XmppConnectionService mXmppConnectionService;
 33
 34	private LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<String, ArrayList<Message>>();
 35
 36	public static int NOTIFICATION_ID = 0x2342;
 37	private Conversation mOpenConversation;
 38	private boolean mIsInForeground;
 39	private long mLastNotification;
 40
 41	public NotificationService(XmppConnectionService service) {
 42		this.mXmppConnectionService = 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			Account account = message.getConversation().getAccount();
 64			updateNotification((!(this.mIsInForeground && this.mOpenConversation == null) || !isScreenOn)
 65					&& !account.inGracePeriod()
 66					&& !this.inMiniGracePeriod(account));
 67		}
 68
 69	}
 70
 71	public void clear() {
 72		synchronized (notifications) {
 73			notifications.clear();
 74			updateNotification(false);
 75		}
 76	}
 77
 78	public void clear(Conversation conversation) {
 79		synchronized (notifications) {
 80			notifications.remove(conversation.getUuid());
 81			updateNotification(false);
 82		}
 83	}
 84
 85	private void updateNotification(boolean notify) {
 86		NotificationManager notificationManager = (NotificationManager) mXmppConnectionService
 87				.getSystemService(Context.NOTIFICATION_SERVICE);
 88		SharedPreferences preferences = mXmppConnectionService.getPreferences();
 89
 90		String ringtone = preferences.getString("notification_ringtone", null);
 91		boolean vibrate = preferences.getBoolean("vibrate_on_notification",
 92				true);
 93
 94		if (notifications.size() == 0) {
 95			notificationManager.cancel(NOTIFICATION_ID);
 96		} else {
 97			if (notify) {
 98				this.markLastNotification();
 99			}
100			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
101					mXmppConnectionService);
102			mBuilder.setSmallIcon(R.drawable.ic_notification);
103			if (notifications.size() == 1) {
104				ArrayList<Message> messages = notifications.values().iterator()
105						.next();
106				if (messages.size() >= 1) {
107					Conversation conversation = messages.get(0)
108							.getConversation();
109					mBuilder.setLargeIcon(mXmppConnectionService
110							.getAvatarService().get(conversation, getPixel(64)));
111					mBuilder.setContentTitle(conversation.getName());
112					StringBuilder text = new StringBuilder();
113					for (int i = 0; i < messages.size(); ++i) {
114						text.append(getReadableBody(messages.get(i)));
115						if (i != messages.size() - 1) {
116							text.append("\n");
117						}
118					}
119					mBuilder.setStyle(new NotificationCompat.BigTextStyle()
120							.bigText(text.toString()));
121					mBuilder.setContentText(getReadableBody(messages.get(0)));
122					if (notify) {
123						mBuilder.setTicker(getReadableBody(messages
124								.get(messages.size() - 1)));
125					}
126					mBuilder.setContentIntent(createContentIntent(conversation
127							.getUuid()));
128				} else {
129					notificationManager.cancel(NOTIFICATION_ID);
130					return;
131				}
132			} else {
133				NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
134				style.setBigContentTitle(notifications.size()
135						+ " "
136						+ mXmppConnectionService
137								.getString(R.string.unread_conversations));
138				StringBuilder names = new StringBuilder();
139				Conversation conversation = null;
140				for (ArrayList<Message> messages : notifications.values()) {
141					if (messages.size() > 0) {
142						conversation = messages.get(0).getConversation();
143						String name = conversation.getName();
144						style.addLine(Html.fromHtml("<b>" + name + "</b> "
145								+ getReadableBody(messages.get(0))));
146						names.append(name);
147						names.append(", ");
148					}
149				}
150				if (names.length() >= 2) {
151					names.delete(names.length() - 2, names.length());
152				}
153				mBuilder.setContentTitle(notifications.size()
154						+ " "
155						+ mXmppConnectionService
156								.getString(R.string.unread_conversations));
157				mBuilder.setContentText(names.toString());
158				mBuilder.setStyle(style);
159				if (conversation != null) {
160					mBuilder.setContentIntent(createContentIntent(conversation
161							.getUuid()));
162				}
163			}
164			if (notify) {
165				if (vibrate) {
166					int dat = 70;
167					long[] pattern = { 0, 3 * dat, dat, dat };
168					mBuilder.setVibrate(pattern);
169				}
170				if (ringtone != null) {
171					mBuilder.setSound(Uri.parse(ringtone));
172				}
173			}
174			mBuilder.setDeleteIntent(createDeleteIntent());
175			mBuilder.setLights(0xffffffff, 2000, 4000);
176			Notification notification = mBuilder.build();
177			notificationManager.notify(NOTIFICATION_ID, notification);
178		}
179	}
180
181	private String getReadableBody(Message message) {
182		if (message.getDownloadable() != null
183				&& (message.getDownloadable().getStatus() == Downloadable.STATUS_OFFER || message
184						.getDownloadable().getStatus() == Downloadable.STATUS_OFFER_CHECK_FILESIZE)) {
185			return mXmppConnectionService.getText(
186					R.string.image_offered_for_download).toString();
187		} else if (message.getEncryption() == Message.ENCRYPTION_PGP) {
188			return mXmppConnectionService.getText(
189					R.string.encrypted_message_received).toString();
190		} else if (message.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
191			return mXmppConnectionService.getText(R.string.decryption_failed)
192					.toString();
193		} else if (message.getType() == Message.TYPE_IMAGE) {
194			return mXmppConnectionService.getText(R.string.image_file)
195					.toString();
196		} else {
197			return message.getBody().trim();
198		}
199	}
200
201	private PendingIntent createContentIntent(String conversationUuid) {
202		TaskStackBuilder stackBuilder = TaskStackBuilder
203				.create(mXmppConnectionService);
204		stackBuilder.addParentStack(ConversationActivity.class);
205
206		Intent viewConversationIntent = new Intent(mXmppConnectionService,
207				ConversationActivity.class);
208		viewConversationIntent.setAction(Intent.ACTION_VIEW);
209		viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
210				conversationUuid);
211		viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
212
213		stackBuilder.addNextIntent(viewConversationIntent);
214
215		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
216				PendingIntent.FLAG_UPDATE_CURRENT);
217		return resultPendingIntent;
218	}
219
220	private PendingIntent createDeleteIntent() {
221		Intent intent = new Intent(mXmppConnectionService,
222				XmppConnectionService.class);
223		intent.setAction("clear_notification");
224		return PendingIntent.getService(mXmppConnectionService, 0, intent, 0);
225	}
226
227	public static boolean wasHighlightedOrPrivate(Message message) {
228		String nick = message.getConversation().getMucOptions().getActualNick();
229		Pattern highlight = generateNickHighlightPattern(nick);
230		if (message.getBody() == null || nick == null) {
231			return false;
232		}
233		Matcher m = highlight.matcher(message.getBody());
234		return (m.find() || message.getType() == Message.TYPE_PRIVATE);
235	}
236
237	private static Pattern generateNickHighlightPattern(String nick) {
238		// We expect a word boundary, i.e. space or start of string, followed by
239		// the
240		// nick (matched in case-insensitive manner), followed by optional
241		// punctuation (for example "bob: i disagree" or "how are you alice?"),
242		// followed by another word boundary.
243		return Pattern.compile("\\b" + nick + "\\p{Punct}?\\b",
244				Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
245	}
246
247	public void setOpenConversation(Conversation conversation) {
248		this.mOpenConversation = conversation;
249	}
250
251	public void setIsInForeground(boolean foreground) {
252		this.mIsInForeground = foreground;
253	}
254
255	private int getPixel(int dp) {
256		DisplayMetrics metrics = mXmppConnectionService.getResources()
257				.getDisplayMetrics();
258		return ((int) (dp * metrics.density));
259	}
260
261	private void markLastNotification() {
262		this.mLastNotification = SystemClock.elapsedRealtime();
263	}
264
265	private boolean inMiniGracePeriod(Account account) {
266		int miniGrace = account.getStatus() == Account.STATUS_ONLINE ? Config.MINI_GRACE_PERIOD
267				: Config.MINI_GRACE_PERIOD * 2;
268		return SystemClock.elapsedRealtime() < (this.mLastNotification + miniGrace);
269	}
270}