2013-02-08 11:57:54 -08:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011 Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.notifications;
|
|
|
|
|
2014-12-11 19:36:46 -08:00
|
|
|
import android.app.AlarmManager;
|
2013-02-08 11:57:54 -08:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2014-12-11 19:36:46 -08:00
|
|
|
import android.content.BroadcastReceiver;
|
2013-02-08 11:57:54 -08:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
2015-12-08 04:40:26 -08:00
|
|
|
import android.media.AudioAttributes;
|
2013-02-08 11:57:54 -08:00
|
|
|
import android.media.AudioManager;
|
2015-12-08 04:40:26 -08:00
|
|
|
import android.media.Ringtone;
|
|
|
|
import android.media.RingtoneManager;
|
2013-02-08 11:57:54 -08:00
|
|
|
import android.net.Uri;
|
2015-09-04 09:40:59 -07:00
|
|
|
import android.os.AsyncTask;
|
2015-12-08 04:40:26 -08:00
|
|
|
import android.os.Build;
|
2016-12-25 15:27:59 -08:00
|
|
|
import android.service.notification.StatusBarNotification;
|
2015-07-06 17:36:49 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2015-06-09 07:37:20 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2016-12-25 15:27:59 -08:00
|
|
|
import android.support.v4.app.NotificationManagerCompat;
|
2013-02-08 11:57:54 -08:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
import org.thoughtcrime.redphone.ui.NotificationBarManager;
|
2016-11-24 08:09:34 -08:00
|
|
|
import org.thoughtcrime.redphone.util.Util;
|
2014-12-15 12:25:55 -08:00
|
|
|
import org.thoughtcrime.securesms.ConversationActivity;
|
2013-02-08 11:57:54 -08:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2013-02-08 11:57:54 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2016-10-10 11:13:37 -07:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
|
2013-02-08 11:57:54 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
|
2014-12-11 19:36:46 -08:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2015-06-09 07:37:20 -07:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
2015-07-31 16:46:17 -07:00
|
|
|
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
2015-07-31 16:46:17 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
2013-04-25 18:59:49 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2013-02-08 11:57:54 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2014-12-11 19:36:46 -08:00
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
2017-02-20 12:00:03 -08:00
|
|
|
import org.thoughtcrime.securesms.service.MessageRetrievalService;
|
2016-04-14 08:32:21 +02:00
|
|
|
import org.thoughtcrime.securesms.util.ServiceUtil;
|
2015-03-12 11:54:08 -07:00
|
|
|
import org.thoughtcrime.securesms.util.SpanUtil;
|
2013-07-11 14:58:40 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2017-02-22 15:49:05 -08:00
|
|
|
import org.thoughtcrime.securesms.webrtc.CallNotificationBuilder;
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2016-11-24 08:09:34 -08:00
|
|
|
import java.util.HashSet;
|
2013-02-08 11:57:54 -08:00
|
|
|
import java.util.List;
|
2014-10-08 21:52:14 +03:00
|
|
|
import java.util.ListIterator;
|
2016-11-24 08:09:34 -08:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executors;
|
2014-12-11 19:36:46 -08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2016-11-24 08:09:34 -08:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2014-12-11 17:12:10 -08:00
|
|
|
import me.leolin.shortcutbadger.ShortcutBadger;
|
|
|
|
|
2014-12-12 11:49:31 -08:00
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
/**
|
|
|
|
* Handles posting system notifications for new messages.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class MessageNotifier {
|
2015-07-31 15:05:24 -07:00
|
|
|
|
2015-05-22 14:07:54 -07:00
|
|
|
private static final String TAG = MessageNotifier.class.getSimpleName();
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2016-12-25 19:00:33 -08:00
|
|
|
public static final String EXTRA_REMOTE_REPLY = "extra_remote_reply";
|
2016-12-25 15:27:59 -08:00
|
|
|
|
|
|
|
private static final int SUMMARY_NOTIFICATION_ID = 1338;
|
|
|
|
private static final String NOTIFICATION_GROUP = "messages";
|
2016-11-24 08:09:34 -08:00
|
|
|
private static final long MIN_AUDIBLE_PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(2);
|
|
|
|
private static final long DESKTOP_ACTIVITY_PERIOD = TimeUnit.MINUTES.toMillis(1);
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2016-11-24 08:09:34 -08:00
|
|
|
private volatile static long visibleThread = -1;
|
|
|
|
private volatile static long lastDesktopActivityTimestamp = -1;
|
|
|
|
private volatile static long lastAudibleNotification = -1;
|
|
|
|
private static final CancelableExecutor executor = new CancelableExecutor();
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
public static void setVisibleThread(long threadId) {
|
|
|
|
visibleThread = threadId;
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:09:34 -08:00
|
|
|
public static void setLastDesktopActivityTimestamp(long timestamp) {
|
|
|
|
lastDesktopActivityTimestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
2013-02-09 15:17:55 -08:00
|
|
|
public static void notifyMessageDeliveryFailed(Context context, Recipients recipients, long threadId) {
|
|
|
|
if (visibleThread == threadId) {
|
2015-06-09 07:37:20 -07:00
|
|
|
sendInThreadNotification(context, recipients);
|
2013-02-09 15:17:55 -08:00
|
|
|
} else {
|
2014-12-15 12:25:55 -08:00
|
|
|
Intent intent = new Intent(context, ConversationActivity.class);
|
2015-06-13 20:23:30 -07:00
|
|
|
intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.getIds());
|
|
|
|
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId);
|
2015-07-31 15:05:24 -07:00
|
|
|
intent.setData((Uri.parse("custom://" + System.currentTimeMillis())));
|
2013-02-09 15:17:55 -08:00
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
FailedNotificationBuilder builder = new FailedNotificationBuilder(context, TextSecurePreferences.getNotificationPrivacy(context), intent);
|
2013-02-09 15:17:55 -08:00
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.notify((int)threadId, builder.build());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
public static void cancelDelayedNotifications() {
|
|
|
|
executor.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void cancelActiveNotifications(@NonNull Context context) {
|
|
|
|
NotificationManager notifications = ServiceUtil.getNotificationManager(context);
|
|
|
|
notifications.cancel(SUMMARY_NOTIFICATION_ID);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 23) {
|
2017-01-17 20:41:24 -08:00
|
|
|
try {
|
|
|
|
StatusBarNotification[] activeNotifications = notifications.getActiveNotifications();
|
2016-12-25 15:27:59 -08:00
|
|
|
|
2017-01-17 20:41:24 -08:00
|
|
|
for (StatusBarNotification activeNotification : activeNotifications) {
|
|
|
|
if (activeNotification.getId() != NotificationBarManager.RED_PHONE_NOTIFICATION) {
|
|
|
|
notifications.cancel(activeNotification.getId());
|
|
|
|
}
|
2016-12-25 15:27:59 -08:00
|
|
|
}
|
2017-01-17 20:41:24 -08:00
|
|
|
} catch (Throwable e) {
|
|
|
|
// XXX Appears to be a ROM bug, see #6043
|
|
|
|
Log.w(TAG, e);
|
|
|
|
notifications.cancelAll();
|
2016-12-25 15:27:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void cancelOrphanedNotifications(@NonNull Context context, NotificationState notificationState) {
|
|
|
|
if (Build.VERSION.SDK_INT >= 23) {
|
2017-01-17 20:41:24 -08:00
|
|
|
try {
|
|
|
|
NotificationManager notifications = ServiceUtil.getNotificationManager(context);
|
|
|
|
StatusBarNotification[] activeNotifications = notifications.getActiveNotifications();
|
|
|
|
|
|
|
|
for (StatusBarNotification notification : activeNotifications) {
|
|
|
|
boolean validNotification = false;
|
|
|
|
|
2017-02-20 12:00:03 -08:00
|
|
|
if (notification.getId() != SUMMARY_NOTIFICATION_ID &&
|
|
|
|
notification.getId() != NotificationBarManager.RED_PHONE_NOTIFICATION &&
|
2017-02-22 15:49:05 -08:00
|
|
|
notification.getId() != CallNotificationBuilder.WEBRTC_NOTIFICATION &&
|
2017-02-20 12:00:03 -08:00
|
|
|
notification.getId() != KeyCachingService.SERVICE_RUNNING_ID &&
|
|
|
|
notification.getId() != MessageRetrievalService.FOREGROUND_ID)
|
|
|
|
{
|
2017-01-17 20:41:24 -08:00
|
|
|
for (NotificationItem item : notificationState.getNotifications()) {
|
|
|
|
if (notification.getId() == (SUMMARY_NOTIFICATION_ID + item.getThreadId())) {
|
|
|
|
validNotification = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-12-25 15:27:59 -08:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:41:24 -08:00
|
|
|
if (!validNotification) {
|
|
|
|
notifications.cancel(notification.getId());
|
|
|
|
}
|
2016-12-25 15:27:59 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-17 20:41:24 -08:00
|
|
|
} catch (Throwable e) {
|
|
|
|
// XXX Android ROM Bug, see #6043
|
|
|
|
Log.w(TAG, e);
|
2016-12-25 15:27:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
public static void updateNotification(@NonNull Context context, @Nullable MasterSecret masterSecret) {
|
2013-07-11 14:58:40 -07:00
|
|
|
if (!TextSecurePreferences.isNotificationsEnabled(context)) {
|
2013-05-06 14:07:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:38:55 -08:00
|
|
|
updateNotification(context, masterSecret, false, 0);
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
public static void updateNotification(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
long threadId)
|
2015-07-20 18:00:48 -07:00
|
|
|
{
|
2016-11-24 08:09:34 -08:00
|
|
|
if (System.currentTimeMillis() - lastDesktopActivityTimestamp < DESKTOP_ACTIVITY_PERIOD) {
|
|
|
|
Log.w(TAG, "Scheduling delayed notification...");
|
|
|
|
executor.execute(new DelayedNotification(context, masterSecret, threadId));
|
|
|
|
} else {
|
2017-03-08 17:38:55 -08:00
|
|
|
updateNotification(context, masterSecret, threadId, true);
|
2016-11-24 08:09:34 -08:00
|
|
|
}
|
2015-07-20 18:00:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateNotification(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
2015-12-19 13:07:44 +01:00
|
|
|
long threadId,
|
|
|
|
boolean signal)
|
2015-07-06 17:36:49 -07:00
|
|
|
{
|
2015-11-04 12:39:53 -08:00
|
|
|
boolean isVisible = visibleThread == threadId;
|
|
|
|
|
|
|
|
ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context)
|
|
|
|
.getRecipientsForThreadId(threadId);
|
|
|
|
|
|
|
|
if (isVisible) {
|
2017-02-22 15:05:35 -08:00
|
|
|
List<MarkedMessageInfo> messageIds = threads.setRead(threadId, false);
|
2016-10-10 11:13:37 -07:00
|
|
|
MarkReadReceiver.process(context, messageIds);
|
2015-11-04 12:39:53 -08:00
|
|
|
}
|
2015-06-11 13:10:39 -07:00
|
|
|
|
|
|
|
if (!TextSecurePreferences.isNotificationsEnabled(context) ||
|
|
|
|
(recipients != null && recipients.isMuted()))
|
|
|
|
{
|
2013-05-06 14:07:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:39:53 -08:00
|
|
|
if (isVisible) {
|
2015-06-09 07:37:20 -07:00
|
|
|
sendInThreadNotification(context, threads.getRecipientsForThreadId(threadId));
|
2013-02-08 11:57:54 -08:00
|
|
|
} else {
|
2017-03-08 17:38:55 -08:00
|
|
|
updateNotification(context, masterSecret, signal, 0);
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static void updateNotification(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
2015-07-20 18:00:48 -07:00
|
|
|
boolean signal,
|
|
|
|
int reminderCount)
|
2015-07-06 17:36:49 -07:00
|
|
|
{
|
2013-09-09 16:46:03 -07:00
|
|
|
Cursor telcoCursor = null;
|
|
|
|
Cursor pushCursor = null;
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
try {
|
2013-09-09 16:46:03 -07:00
|
|
|
telcoCursor = DatabaseFactory.getMmsSmsDatabase(context).getUnread();
|
|
|
|
pushCursor = DatabaseFactory.getPushDatabase(context).getPending();
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2013-09-09 16:46:03 -07:00
|
|
|
if ((telcoCursor == null || telcoCursor.isAfterLast()) &&
|
|
|
|
(pushCursor == null || pushCursor.isAfterLast()))
|
|
|
|
{
|
2016-12-25 15:27:59 -08:00
|
|
|
cancelActiveNotifications(context);
|
2014-12-11 17:12:10 -08:00
|
|
|
updateBadge(context, 0);
|
2014-12-11 19:36:46 -08:00
|
|
|
clearReminder(context);
|
2013-02-08 11:57:54 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-09 16:46:03 -07:00
|
|
|
NotificationState notificationState = constructNotificationState(context, masterSecret, telcoCursor);
|
|
|
|
|
2016-11-24 08:09:34 -08:00
|
|
|
if (signal && (System.currentTimeMillis() - lastAudibleNotification) < MIN_AUDIBLE_PERIOD_MILLIS) {
|
|
|
|
signal = false;
|
|
|
|
} else if (signal) {
|
|
|
|
lastAudibleNotification = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
if (notificationState.hasMultipleThreads()) {
|
2016-12-25 15:27:59 -08:00
|
|
|
if (Build.VERSION.SDK_INT >= 23) {
|
|
|
|
for (long threadId : notificationState.getThreads()) {
|
|
|
|
sendSingleThreadNotification(context, masterSecret, new NotificationState(notificationState.getNotificationsForThread(threadId)), false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
sendMultipleThreadNotification(context, notificationState, signal);
|
2013-02-08 11:57:54 -08:00
|
|
|
} else {
|
2016-12-25 15:27:59 -08:00
|
|
|
sendSingleThreadNotification(context, masterSecret, notificationState, signal, false);
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
2014-12-11 17:12:10 -08:00
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
cancelOrphanedNotifications(context, notificationState);
|
2014-12-11 17:12:10 -08:00
|
|
|
updateBadge(context, notificationState.getMessageCount());
|
2015-09-09 16:51:55 -07:00
|
|
|
|
|
|
|
if (signal) {
|
|
|
|
scheduleReminder(context, reminderCount);
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
} finally {
|
2013-09-09 16:46:03 -07:00
|
|
|
if (telcoCursor != null) telcoCursor.close();
|
|
|
|
if (pushCursor != null) pushCursor.close();
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static void sendSingleThreadNotification(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull NotificationState notificationState,
|
2016-12-25 15:27:59 -08:00
|
|
|
boolean signal, boolean bundled)
|
2013-02-08 11:57:54 -08:00
|
|
|
{
|
2013-09-09 16:46:03 -07:00
|
|
|
if (notificationState.getNotifications().isEmpty()) {
|
2016-12-25 15:27:59 -08:00
|
|
|
if (!bundled) cancelActiveNotifications(context);
|
2013-09-09 16:46:03 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder(context, masterSecret, TextSecurePreferences.getNotificationPrivacy(context));
|
|
|
|
List<NotificationItem> notifications = notificationState.getNotifications();
|
|
|
|
Recipients recipients = notifications.get(0).getRecipients();
|
|
|
|
int notificationId = (int) (SUMMARY_NOTIFICATION_ID + (bundled ? notifications.get(0).getThreadId() : 0));
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-12-04 15:00:13 -08:00
|
|
|
builder.setThread(notifications.get(0).getRecipients());
|
2015-07-31 15:05:24 -07:00
|
|
|
builder.setMessageCount(notificationState.getMessageCount());
|
2015-12-04 15:00:13 -08:00
|
|
|
builder.setPrimaryMessageBody(recipients, notifications.get(0).getIndividualRecipient(),
|
|
|
|
notifications.get(0).getText(), notifications.get(0).getSlideDeck());
|
2013-02-08 11:57:54 -08:00
|
|
|
builder.setContentIntent(notifications.get(0).getPendingIntent(context));
|
2016-12-25 15:27:59 -08:00
|
|
|
builder.setGroup(NOTIFICATION_GROUP);
|
2017-03-08 17:38:55 -08:00
|
|
|
builder.setDeleteIntent(notificationState.getDeleteIntent(context));
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-03-09 22:27:50 +01:00
|
|
|
long timestamp = notifications.get(0).getTimestamp();
|
|
|
|
if (timestamp != 0) builder.setWhen(timestamp);
|
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
builder.addActions(masterSecret,
|
2016-12-25 15:27:59 -08:00
|
|
|
notificationState.getMarkAsReadIntent(context, notificationId),
|
2015-07-31 15:05:24 -07:00
|
|
|
notificationState.getQuickReplyIntent(context, notifications.get(0).getRecipients()),
|
2016-12-25 19:00:33 -08:00
|
|
|
notificationState.getRemoteReplyIntent(context, notifications.get(0).getRecipients()));
|
|
|
|
|
2016-09-14 23:58:24 +02:00
|
|
|
builder.addAndroidAutoAction(notificationState.getAndroidAutoReplyIntent(context, notifications.get(0).getRecipients()),
|
2016-12-25 15:27:59 -08:00
|
|
|
notificationState.getAndroidAutoHeardIntent(context, notificationId), notifications.get(0).getTimestamp());
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2014-10-08 21:52:14 +03:00
|
|
|
ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size());
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
while(iterator.hasPrevious()) {
|
2015-12-04 15:00:13 -08:00
|
|
|
NotificationItem item = iterator.previous();
|
|
|
|
builder.addMessageBody(item.getRecipients(), item.getIndividualRecipient(), item.getText());
|
2015-07-31 15:05:24 -07:00
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
if (signal) {
|
2015-07-31 15:05:24 -07:00
|
|
|
builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate());
|
|
|
|
builder.setTicker(notifications.get(0).getIndividualRecipient(),
|
|
|
|
notifications.get(0).getText());
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
if (!bundled) {
|
|
|
|
builder.setGroupSummary(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationManagerCompat.from(context).notify(notificationId, builder.build());
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static void sendMultipleThreadNotification(@NonNull Context context,
|
|
|
|
@NonNull NotificationState notificationState,
|
2013-02-08 11:57:54 -08:00
|
|
|
boolean signal)
|
|
|
|
{
|
2015-07-31 15:05:24 -07:00
|
|
|
MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, TextSecurePreferences.getNotificationPrivacy(context));
|
|
|
|
List<NotificationItem> notifications = notificationState.getNotifications();
|
|
|
|
|
|
|
|
builder.setMessageCount(notificationState.getMessageCount(), notificationState.getThreadCount());
|
|
|
|
builder.setMostRecentSender(notifications.get(0).getIndividualRecipient());
|
2016-12-25 15:27:59 -08:00
|
|
|
builder.setGroup(NOTIFICATION_GROUP);
|
2017-03-08 17:38:55 -08:00
|
|
|
builder.setDeleteIntent(notificationState.getDeleteIntent(context));
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-03-09 22:27:50 +01:00
|
|
|
long timestamp = notifications.get(0).getTimestamp();
|
|
|
|
if (timestamp != 0) builder.setWhen(timestamp);
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
builder.addActions(notificationState.getMarkAsReadIntent(context, SUMMARY_NOTIFICATION_ID));
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2014-10-08 21:52:14 +03:00
|
|
|
ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size());
|
2015-07-31 15:05:24 -07:00
|
|
|
|
2014-10-08 21:52:14 +03:00
|
|
|
while(iterator.hasPrevious()) {
|
|
|
|
NotificationItem item = iterator.previous();
|
2015-07-31 15:05:24 -07:00
|
|
|
builder.addMessageBody(item.getIndividualRecipient(), item.getText());
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (signal) {
|
2015-07-31 15:05:24 -07:00
|
|
|
builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate());
|
2016-08-01 00:15:49 +02:00
|
|
|
builder.setTicker(notifications.get(0).getIndividualRecipient(),
|
|
|
|
notifications.get(0).getText());
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
NotificationManagerCompat.from(context).notify(SUMMARY_NOTIFICATION_ID, builder.build());
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
private static void sendInThreadNotification(Context context, Recipients recipients) {
|
2016-04-14 08:32:21 +02:00
|
|
|
if (!TextSecurePreferences.isInThreadNotifications(context) ||
|
|
|
|
ServiceUtil.getAudioManager(context).getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
|
|
|
|
{
|
2015-12-08 04:40:26 -08:00
|
|
|
return;
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-12-08 04:40:26 -08:00
|
|
|
Uri uri = recipients != null ? recipients.getRingtone() : null;
|
2015-05-22 14:07:54 -07:00
|
|
|
|
2015-12-08 04:40:26 -08:00
|
|
|
if (uri == null) {
|
|
|
|
String ringtone = TextSecurePreferences.getNotificationRingtone(context);
|
2015-05-22 14:07:54 -07:00
|
|
|
|
2015-12-08 04:40:26 -08:00
|
|
|
if (ringtone == null) {
|
|
|
|
Log.w(TAG, "ringtone preference was null.");
|
2015-05-22 14:07:54 -07:00
|
|
|
return;
|
2016-01-18 13:39:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uri = Uri.parse(ringtone);
|
|
|
|
|
|
|
|
if (uri == null) {
|
|
|
|
Log.w(TAG, "couldn't parse ringtone uri " + ringtone);
|
|
|
|
return;
|
2015-05-22 14:07:54 -07:00
|
|
|
}
|
2015-12-08 04:40:26 -08:00
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2016-01-18 13:39:13 +01:00
|
|
|
if (uri.toString().isEmpty()) {
|
|
|
|
Log.d(TAG, "ringtone uri is empty");
|
2015-12-08 04:40:26 -08:00
|
|
|
return;
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-12-08 04:40:26 -08:00
|
|
|
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2016-01-18 13:39:13 +01:00
|
|
|
if (ringtone == null) {
|
|
|
|
Log.w(TAG, "ringtone is null");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:40:26 -08:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
|
|
ringtone.setAudioAttributes(new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
|
|
|
|
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
|
|
|
|
.build());
|
|
|
|
} else {
|
|
|
|
ringtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
2015-12-08 04:40:26 -08:00
|
|
|
|
|
|
|
ringtone.play();
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static NotificationState constructNotificationState(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
@NonNull Cursor cursor)
|
2013-02-08 11:57:54 -08:00
|
|
|
{
|
|
|
|
NotificationState notificationState = new NotificationState();
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
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) {
|
2017-03-08 17:38:55 -08:00
|
|
|
long id = record.getId();
|
|
|
|
boolean mms = record.isMms() || record.isMmsNotification();
|
2015-10-21 15:32:19 -07:00
|
|
|
Recipient recipient = record.getIndividualRecipient();
|
|
|
|
Recipients recipients = record.getRecipients();
|
|
|
|
long threadId = record.getThreadId();
|
|
|
|
CharSequence body = record.getDisplayBody();
|
|
|
|
Recipients threadRecipients = null;
|
|
|
|
SlideDeck slideDeck = null;
|
2015-07-06 17:14:57 -05:00
|
|
|
long timestamp = record.getTimestamp();
|
2017-03-08 17:38:55 -08:00
|
|
|
|
2014-02-26 12:31:56 -08:00
|
|
|
|
|
|
|
if (threadId != -1) {
|
|
|
|
threadRecipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
|
|
|
|
}
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
|
2014-03-16 23:16:08 +01:00
|
|
|
if (SmsDatabase.Types.isDecryptInProgressType(record.getType()) || !record.getBody().isPlaintext()) {
|
2015-07-06 17:36:49 -07:00
|
|
|
body = SpanUtil.italic(context.getString(R.string.MessageNotifier_locked_message));
|
2015-03-12 11:54:08 -07:00
|
|
|
} else if (record.isMms() && TextUtils.isEmpty(body)) {
|
|
|
|
body = SpanUtil.italic(context.getString(R.string.MessageNotifier_media_message));
|
2015-10-21 15:32:19 -07:00
|
|
|
slideDeck = ((MediaMmsMessageRecord)record).getSlideDeck();
|
2015-05-11 19:44:25 -07:00
|
|
|
} else if (record.isMms() && !record.isMmsNotification()) {
|
2015-03-12 11:54:08 -07:00
|
|
|
String message = context.getString(R.string.MessageNotifier_media_message_with_text, body);
|
|
|
|
int italicLength = message.length() - body.length();
|
|
|
|
body = SpanUtil.italic(message, italicLength);
|
2015-10-21 15:32:19 -07:00
|
|
|
slideDeck = ((MediaMmsMessageRecord)record).getSlideDeck();
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
if (threadRecipients == null || !threadRecipients.isMuted()) {
|
2017-03-08 17:38:55 -08:00
|
|
|
notificationState.addNotification(new NotificationItem(id, mms, recipient, recipients, threadRecipients, threadId, body, timestamp, slideDeck));
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
2013-04-26 11:23:43 -07:00
|
|
|
reader.close();
|
2013-02-08 11:57:54 -08:00
|
|
|
return notificationState;
|
|
|
|
}
|
|
|
|
|
2014-12-11 17:12:10 -08:00
|
|
|
private static void updateBadge(Context context, int count) {
|
|
|
|
try {
|
2017-01-21 10:06:49 -08:00
|
|
|
if (count == 0) ShortcutBadger.removeCount(context);
|
|
|
|
else ShortcutBadger.applyCount(context, count);
|
2014-12-11 17:12:10 -08:00
|
|
|
} catch (Throwable t) {
|
|
|
|
// NOTE :: I don't totally trust this thing, so I'm catching
|
|
|
|
// everything.
|
|
|
|
Log.w("MessageNotifier", t);
|
|
|
|
}
|
|
|
|
}
|
2014-12-11 19:36:46 -08:00
|
|
|
|
2015-07-31 15:05:24 -07:00
|
|
|
private static void scheduleReminder(Context context, int count) {
|
2014-12-11 19:36:46 -08:00
|
|
|
if (count >= TextSecurePreferences.getRepeatAlertsCount(context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
Intent alarmIntent = new Intent(ReminderReceiver.REMINDER_ACTION);
|
|
|
|
alarmIntent.putExtra("reminder_count", count);
|
|
|
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
2015-04-16 10:52:50 -04:00
|
|
|
long timeout = TimeUnit.MINUTES.toMillis(2);
|
2014-12-11 19:36:46 -08:00
|
|
|
|
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent);
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:38:55 -08:00
|
|
|
public static void clearReminder(Context context) {
|
2014-12-11 19:36:46 -08:00
|
|
|
Intent alarmIntent = new Intent(ReminderReceiver.REMINDER_ACTION);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
alarmManager.cancel(pendingIntent);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ReminderReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
public static final String REMINDER_ACTION = "org.thoughtcrime.securesms.MessageNotifier.REMINDER_ACTION";
|
|
|
|
|
|
|
|
@Override
|
2015-09-04 09:40:59 -07:00
|
|
|
public void onReceive(final Context context, final Intent intent) {
|
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
|
|
|
|
int reminderCount = intent.getIntExtra("reminder_count", 0);
|
2017-03-08 17:38:55 -08:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret, true, reminderCount + 1);
|
2015-09-04 09:40:59 -07:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
2014-12-11 19:36:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:09:34 -08:00
|
|
|
private static class DelayedNotification implements Runnable {
|
|
|
|
|
|
|
|
private static final long DELAY = TimeUnit.SECONDS.toMillis(5);
|
|
|
|
|
|
|
|
private final AtomicBoolean canceled = new AtomicBoolean(false);
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
|
|
|
|
private final long threadId;
|
|
|
|
private final long delayUntil;
|
|
|
|
|
|
|
|
private DelayedNotification(Context context, MasterSecret masterSecret, long threadId) {
|
|
|
|
this.context = context;
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.threadId = threadId;
|
|
|
|
this.delayUntil = System.currentTimeMillis() + DELAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret);
|
|
|
|
|
|
|
|
long delayMillis = delayUntil - System.currentTimeMillis();
|
|
|
|
Log.w(TAG, "Waiting to notify: " + delayMillis);
|
|
|
|
|
|
|
|
if (delayMillis > 0) {
|
|
|
|
Util.sleep(delayMillis);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!canceled.get()) {
|
|
|
|
Log.w(TAG, "Not canceled, notifying...");
|
2017-03-08 17:38:55 -08:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret, threadId, true);
|
2016-11-24 08:09:34 -08:00
|
|
|
MessageNotifier.cancelDelayedNotifications();
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Canceled, not notifying...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
|
|
|
canceled.set(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class CancelableExecutor {
|
|
|
|
|
|
|
|
private final Executor executor = Executors.newSingleThreadExecutor();
|
|
|
|
private final Set<DelayedNotification> tasks = new HashSet<>();
|
|
|
|
|
|
|
|
public void execute(final DelayedNotification runnable) {
|
|
|
|
synchronized (tasks) {
|
|
|
|
tasks.add(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
Runnable wrapper = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
runnable.run();
|
|
|
|
|
|
|
|
synchronized (tasks) {
|
|
|
|
tasks.remove(runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
executor.execute(wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
|
|
|
synchronized (tasks) {
|
|
|
|
for (DelayedNotification task : tasks) {
|
|
|
|
task.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|