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;
|
|
|
|
import android.media.MediaPlayer;
|
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;
|
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;
|
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 android.text.Spannable;
|
|
|
|
import android.text.SpannableString;
|
2013-02-08 11:57:54 -08:00
|
|
|
import android.text.TextUtils;
|
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 android.text.style.StyleSpan;
|
2013-02-08 11:57:54 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
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;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
|
2014-11-04 15:01:32 -08:00
|
|
|
import org.thoughtcrime.securesms.database.PushDatabase;
|
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;
|
2014-11-04 15:01:32 -08:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
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;
|
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;
|
2014-11-04 15:01:32 -08:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
2014-10-08 21:52:14 +03:00
|
|
|
import java.util.ListIterator;
|
2014-12-11 19:36:46 -08:00
|
|
|
import java.util.concurrent.TimeUnit;
|
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
|
|
|
|
|
|
|
public static final int NOTIFICATION_ID = 1338;
|
|
|
|
|
|
|
|
private volatile static long visibleThread = -1;
|
|
|
|
|
2015-05-19 10:24:08 +02:00
|
|
|
public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
public static void setVisibleThread(long threadId) {
|
|
|
|
visibleThread = threadId;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:00:48 -07:00
|
|
|
updateNotification(context, masterSecret, false, 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
|
|
|
{
|
|
|
|
updateNotification(context, masterSecret, false, threadId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateNotification(@NonNull Context context,
|
|
|
|
@Nullable MasterSecret masterSecret,
|
|
|
|
boolean includePushDatabase,
|
|
|
|
long threadId)
|
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) {
|
|
|
|
threads.setRead(threadId);
|
|
|
|
}
|
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 {
|
2015-07-20 18:00:48 -07:00
|
|
|
updateNotification(context, masterSecret, true, includePushDatabase, 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,
|
|
|
|
boolean includePushDatabase,
|
|
|
|
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()))
|
|
|
|
{
|
2013-02-08 11:57:54 -08:00
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.cancel(NOTIFICATION_ID);
|
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);
|
|
|
|
|
2015-07-20 18:00:48 -07:00
|
|
|
if (includePushDatabase) {
|
|
|
|
appendPushNotificationState(context, notificationState, pushCursor);
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
if (notificationState.hasMultipleThreads()) {
|
2015-07-31 15:05:24 -07:00
|
|
|
sendMultipleThreadNotification(context, notificationState, signal);
|
2013-02-08 11:57:54 -08:00
|
|
|
} else {
|
2013-05-30 12:39:56 -07:00
|
|
|
sendSingleThreadNotification(context, masterSecret, notificationState, signal);
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
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,
|
2013-02-08 11:57:54 -08:00
|
|
|
boolean signal)
|
|
|
|
{
|
2013-09-09 16:46:03 -07:00
|
|
|
if (notificationState.getNotifications().isEmpty()) {
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.cancel(NOTIFICATION_ID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-31 16:46:17 -07:00
|
|
|
SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder(context, masterSecret, TextSecurePreferences.getNotificationPrivacy(context));
|
2015-07-31 15:05:24 -07:00
|
|
|
List<NotificationItem> notifications = notificationState.getNotifications();
|
2015-12-04 15:00:13 -08:00
|
|
|
Recipients recipients = notifications.get(0).getRecipients();
|
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));
|
|
|
|
|
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,
|
|
|
|
notificationState.getMarkAsReadIntent(context),
|
|
|
|
notificationState.getQuickReplyIntent(context, notifications.get(0).getRecipients()),
|
|
|
|
notificationState.getWearableReplyIntent(context, notifications.get(0).getRecipients()));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.notify(NOTIFICATION_ID, builder.build());
|
|
|
|
}
|
|
|
|
|
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());
|
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(notificationState.getMarkAsReadIntent(context));
|
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());
|
|
|
|
builder.setTicker(notifications.get(0).getText());
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.notify(NOTIFICATION_ID, builder.build());
|
|
|
|
}
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
private static void sendInThreadNotification(Context context, Recipients recipients) {
|
2015-12-08 04:40:26 -08:00
|
|
|
if (!TextSecurePreferences.isInThreadNotifications(context)) {
|
|
|
|
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;
|
2015-12-08 04:40:26 -08:00
|
|
|
} else {
|
|
|
|
uri = Uri.parse(ringtone);
|
2015-05-22 14:07:54 -07:00
|
|
|
}
|
2015-12-08 04:40:26 -08:00
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2015-12-08 04:40:26 -08:00
|
|
|
if (uri == null) {
|
|
|
|
Log.w(TAG, "couldn't parse ringtone uri " + TextSecurePreferences.getNotificationRingtone(context));
|
|
|
|
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
|
|
|
|
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-20 18:00:48 -07:00
|
|
|
private static void appendPushNotificationState(@NonNull Context context,
|
|
|
|
@NonNull NotificationState notificationState,
|
|
|
|
@NonNull Cursor cursor)
|
2013-09-09 16:46:03 -07:00
|
|
|
{
|
|
|
|
PushDatabase.Reader reader = null;
|
2014-11-04 15:01:32 -08:00
|
|
|
TextSecureEnvelope envelope;
|
2013-09-09 16:46:03 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
reader = DatabaseFactory.getPushDatabase(context).readerFor(cursor);
|
|
|
|
|
2014-11-04 15:01:32 -08:00
|
|
|
while ((envelope = reader.getNext()) != null) {
|
2015-03-03 11:44:49 -08:00
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, envelope.getSource(), false);
|
|
|
|
Recipient recipient = recipients.getPrimaryRecipient();
|
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
2015-07-06 17:36:49 -07:00
|
|
|
SpannableString body = new SpannableString(context.getString(R.string.MessageNotifier_locked_message));
|
2013-09-09 16:46:03 -07:00
|
|
|
body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
2015-06-09 07:37:20 -07:00
|
|
|
if (!recipients.isMuted()) {
|
2015-07-31 16:46:17 -07:00
|
|
|
notificationState.addNotification(new NotificationItem(recipient, recipients, null, threadId, body, 0, null));
|
2015-06-09 07:37:20 -07:00
|
|
|
}
|
2013-09-09 16:46:03 -07:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (reader != null)
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
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();
|
|
|
|
|
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()) {
|
2015-07-31 16:46:17 -07:00
|
|
|
notificationState.addNotification(new NotificationItem(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 {
|
2015-07-22 12:24:41 -07:00
|
|
|
ShortcutBadger.setBadge(context.getApplicationContext(), 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void clearReminder(Context context) {
|
|
|
|
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);
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret, true, true, reminderCount + 1);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
2014-12-11 19:36:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class DeleteReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
public static final String DELETE_REMINDER_ACTION = "org.thoughtcrime.securesms.MessageNotifier.DELETE_REMINDER_ACTION";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
clearReminder(context);
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|