private static void updateNotification( Context context, MasterSecret masterSecret, boolean signal, int reminderCount) { Cursor telcoCursor = null; Cursor pushCursor = null; try { telcoCursor = DatabaseFactory.getMmsSmsDatabase(context).getUnread(); if ((telcoCursor == null || telcoCursor.isAfterLast()) && (pushCursor == null || pushCursor.isAfterLast())) { ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) .cancel(NOTIFICATION_ID); clearReminder(context); return; } NotificationState notificationState = constructNotificationState(context, masterSecret, telcoCursor); if (notificationState.hasMultipleThreads()) { sendMultipleThreadNotification(context, notificationState, signal); } else { sendSingleThreadNotification(context, masterSecret, notificationState, signal); } if (signal) { scheduleReminder(context, reminderCount); } } finally { if (telcoCursor != null) telcoCursor.close(); if (pushCursor != null) pushCursor.close(); } }
private static NotificationState constructNotificationState( Context context, MasterSecret masterSecret, Cursor cursor) { NotificationState notificationState = new NotificationState(); MessageRecord record; MmsSmsDatabase.Reader reader; if (masterSecret == null) reader = DatabaseFactory.getMmsSmsDatabase(context).readerFor(cursor); else reader = DatabaseFactory.getMmsSmsDatabase(context).readerFor(cursor, masterSecret); while ((record = reader.getNext()) != null) { Recipient recipient = record.getIndividualRecipient(); Recipients recipients = record.getRecipients(); long threadId = record.getThreadId(); CharSequence body = record.getDisplayBody(); Recipients threadRecipients = null; ListenableFutureTask<SlideDeck> slideDeck = null; long timestamp; if (SMSSecurePreferences.showSentTime(context)) timestamp = record.getDateSent(); else timestamp = record.getDateReceived(); if (threadId != -1) { threadRecipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId); } if (SmsDatabase.Types.isDecryptInProgressType(record.getType()) || !record.getBody().isPlaintext()) { body = SpanUtil.italic(context.getString(R.string.MessageNotifier_encrypted_message)); } else if (record.isMms() && TextUtils.isEmpty(body)) { body = SpanUtil.italic(context.getString(R.string.MessageNotifier_media_message)); slideDeck = ((MediaMmsMessageRecord) record).getSlideDeckFuture(); } else if (record.isMms() && !record.isMmsNotification()) { String message = context.getString(R.string.MessageNotifier_media_message_with_text, body); int italicLength = message.length() - body.length(); body = SpanUtil.italic(message, italicLength); slideDeck = ((MediaMmsMessageRecord) record).getSlideDeckFuture(); } if (threadRecipients == null || !threadRecipients.isMuted()) { notificationState.addNotification( new NotificationItem( recipient, recipients, threadRecipients, threadId, body, timestamp, slideDeck)); } } reader.close(); return notificationState; }
private static void sendMultipleThreadNotification( Context context, NotificationState notificationState, boolean signal) { MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder( context, SMSSecurePreferences.getNotificationPrivacy(context)); List<NotificationItem> notifications = notificationState.getNotifications(); builder.setMessageCount( notificationState.getMessageCount(), notificationState.getThreadCount()); builder.setMostRecentSender(notifications.get(0).getIndividualRecipient()); long timestamp = notifications.get(0).getTimestamp(); if (timestamp != 0) builder.setWhen(timestamp); builder.addActions(notificationState.getMarkAsReadIntent(context)); ListIterator<NotificationItem> iterator = notifications.listIterator(0); while (iterator.hasNext()) { NotificationItem item = iterator.next(); builder.addMessageBody(item.getIndividualRecipient(), item.getText()); } if (signal) { builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate()); builder.setTicker(notifications.get(0).getText()); } ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) .notify(NOTIFICATION_ID, builder.build()); }
private static void sendSingleThreadNotification( Context context, MasterSecret masterSecret, NotificationState notificationState, boolean signal) { if (notificationState.getNotifications().isEmpty()) { ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) .cancel(NOTIFICATION_ID); return; } SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder( context, masterSecret, SMSSecurePreferences.getNotificationPrivacy(context)); List<NotificationItem> notifications = notificationState.getNotifications(); builder.setSender(notifications.get(0).getIndividualRecipient()); builder.setMessageCount(notificationState.getMessageCount()); builder.setPrimaryMessageBody( notifications.get(0).getText(), notifications.get(0).getSlideDeck()); builder.setContentIntent(notifications.get(0).getPendingIntent(context)); long timestamp = notifications.get(0).getTimestamp(); if (timestamp != 0) builder.setWhen(timestamp); builder.addActions( masterSecret, notificationState.getMarkAsReadIntent(context), notificationState.getQuickReplyIntent(context, notifications.get(0).getRecipients()), notificationState.getWearableReplyIntent(context, notifications.get(0).getRecipients())); ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size()); while (iterator.hasPrevious()) { builder.addMessageBody(iterator.previous().getText()); } if (signal) { builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate()); builder.setTicker( notifications.get(0).getIndividualRecipient(), notifications.get(0).getText()); } ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) .notify(NOTIFICATION_ID, builder.build()); }