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;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.media.MediaPlayer;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
|
import android.support.v4.app.NotificationCompat.BigTextStyle;
|
|
|
|
import android.support.v4.app.NotificationCompat.InboxStyle;
|
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.SpannableStringBuilder;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2013-02-17 11:42:30 -08:00
|
|
|
import org.thoughtcrime.securesms.RoutingActivity;
|
2013-09-09 16:46:03 -07:00
|
|
|
import org.thoughtcrime.securesms.database.PushDatabase;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
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;
|
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;
|
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;
|
2013-07-11 14:58:40 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2013-09-09 16:46:03 -07:00
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles posting system notifications for new messages.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class MessageNotifier {
|
|
|
|
|
|
|
|
public static final int NOTIFICATION_ID = 1338;
|
|
|
|
|
|
|
|
private volatile static long visibleThread = -1;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
sendInThreadNotification(context);
|
|
|
|
} else {
|
2013-02-17 11:42:30 -08:00
|
|
|
Intent intent = new Intent(context, RoutingActivity.class);
|
2013-02-09 15:17:55 -08:00
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
|
|
intent.putExtra("recipients", recipients);
|
|
|
|
intent.putExtra("thread_id", threadId);
|
|
|
|
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
|
|
|
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
|
|
|
builder.setSmallIcon(R.drawable.icon_notification);
|
|
|
|
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
|
2014-06-12 16:27:51 -07:00
|
|
|
R.drawable.ic_action_warning_red));
|
2013-02-09 15:17:55 -08:00
|
|
|
builder.setContentTitle(context.getString(R.string.MessageNotifier_message_delivery_failed));
|
|
|
|
builder.setContentText(context.getString(R.string.MessageNotifier_failed_to_deliver_message));
|
|
|
|
builder.setTicker(context.getString(R.string.MessageNotifier_error_delivering_message));
|
|
|
|
builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));
|
|
|
|
builder.setAutoCancel(true);
|
|
|
|
setNotificationAlarms(context, builder, true);
|
|
|
|
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.notify((int)threadId, builder.build());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
public static void updateNotification(Context context, MasterSecret masterSecret) {
|
2013-07-11 14:58:40 -07:00
|
|
|
if (!TextSecurePreferences.isNotificationsEnabled(context)) {
|
2013-05-06 14:07:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
updateNotification(context, masterSecret, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateNotification(Context context, MasterSecret masterSecret, long threadId) {
|
2013-07-11 14:58:40 -07:00
|
|
|
if (!TextSecurePreferences.isNotificationsEnabled(context)) {
|
2013-05-06 14:07:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
if (visibleThread == threadId) {
|
|
|
|
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
|
|
|
|
sendInThreadNotification(context);
|
|
|
|
} else {
|
|
|
|
updateNotification(context, masterSecret, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void updateNotification(Context context, MasterSecret masterSecret, boolean signal) {
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-09 16:46:03 -07:00
|
|
|
NotificationState notificationState = constructNotificationState(context, masterSecret, telcoCursor);
|
|
|
|
|
|
|
|
appendPushNotificationState(context, masterSecret, notificationState, pushCursor);
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
if (notificationState.hasMultipleThreads()) {
|
2013-05-30 12:39:56 -07:00
|
|
|
sendMultipleThreadNotification(context, masterSecret, 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
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendSingleThreadNotification(Context context,
|
2013-05-30 12:39:56 -07:00
|
|
|
MasterSecret masterSecret,
|
2013-02-08 11:57:54 -08:00
|
|
|
NotificationState notificationState,
|
|
|
|
boolean signal)
|
|
|
|
{
|
2013-09-09 16:46:03 -07:00
|
|
|
if (notificationState.getNotifications().isEmpty()) {
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.cancel(NOTIFICATION_ID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:59:49 -07:00
|
|
|
List<NotificationItem>notifications = notificationState.getNotifications();
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
|
|
|
Recipient recipient = notifications.get(0).getIndividualRecipient();
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
builder.setSmallIcon(R.drawable.icon_notification);
|
2013-04-25 18:59:49 -07:00
|
|
|
builder.setLargeIcon(recipient.getContactPhoto());
|
|
|
|
builder.setContentTitle(recipient.toShortString());
|
2013-02-08 11:57:54 -08:00
|
|
|
builder.setContentText(notifications.get(0).getText());
|
|
|
|
builder.setContentIntent(notifications.get(0).getPendingIntent(context));
|
2013-12-19 02:44:42 +05:30
|
|
|
builder.setContentInfo(String.valueOf(notificationState.getMessageCount()));
|
2014-06-24 03:56:24 +03:00
|
|
|
builder.setNumber(notificationState.getMessageCount());
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2013-05-30 12:39:56 -07:00
|
|
|
if (masterSecret != null) {
|
|
|
|
builder.addAction(R.drawable.check, context.getString(R.string.MessageNotifier_mark_as_read),
|
|
|
|
notificationState.getMarkAsReadIntent(context, masterSecret));
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
SpannableStringBuilder content = new SpannableStringBuilder();
|
|
|
|
|
|
|
|
for (NotificationItem item : notifications) {
|
|
|
|
content.append(item.getBigStyleSummary());
|
|
|
|
content.append('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.setStyle(new BigTextStyle().bigText(content));
|
|
|
|
|
|
|
|
setNotificationAlarms(context, builder, signal);
|
|
|
|
|
|
|
|
if (signal) {
|
|
|
|
builder.setTicker(notifications.get(0).getTickerText());
|
|
|
|
}
|
|
|
|
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.notify(NOTIFICATION_ID, builder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendMultipleThreadNotification(Context context,
|
2013-05-30 12:39:56 -07:00
|
|
|
MasterSecret masterSecret,
|
2013-02-08 11:57:54 -08:00
|
|
|
NotificationState notificationState,
|
|
|
|
boolean signal)
|
|
|
|
{
|
|
|
|
List<NotificationItem> notifications = notificationState.getNotifications();
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
|
|
|
|
|
|
|
builder.setSmallIcon(R.drawable.icon_notification);
|
|
|
|
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
|
|
|
|
R.drawable.icon_notification));
|
|
|
|
builder.setContentTitle(String.format(context.getString(R.string.MessageNotifier_d_new_messages),
|
|
|
|
notificationState.getMessageCount()));
|
|
|
|
builder.setContentText(String.format(context.getString(R.string.MessageNotifier_most_recent_from_s),
|
2013-04-25 18:59:49 -07:00
|
|
|
notifications.get(0).getIndividualRecipientName()));
|
2013-02-17 11:42:30 -08:00
|
|
|
builder.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, RoutingActivity.class), 0));
|
2013-12-19 02:44:42 +05:30
|
|
|
|
|
|
|
builder.setContentInfo(String.valueOf(notificationState.getMessageCount()));
|
2014-06-24 03:56:24 +03:00
|
|
|
builder.setNumber(notificationState.getMessageCount());
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2013-05-30 12:39:56 -07:00
|
|
|
if (masterSecret != null) {
|
|
|
|
builder.addAction(R.drawable.check, context.getString(R.string.MessageNotifier_mark_all_as_read),
|
|
|
|
notificationState.getMarkAsReadIntent(context, masterSecret));
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
InboxStyle style = new InboxStyle();
|
|
|
|
|
|
|
|
for (NotificationItem item : notifications) {
|
|
|
|
style.addLine(item.getTickerText());
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.setStyle(style);
|
|
|
|
|
|
|
|
setNotificationAlarms(context, builder, signal);
|
|
|
|
|
|
|
|
if (signal) {
|
|
|
|
builder.setTicker(notifications.get(0).getTickerText());
|
|
|
|
}
|
|
|
|
|
|
|
|
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
|
|
|
|
.notify(NOTIFICATION_ID, builder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendInThreadNotification(Context context) {
|
|
|
|
try {
|
2013-11-18 15:29:19 -08:00
|
|
|
if (!TextSecurePreferences.isInThreadNotifications(context)) {
|
|
|
|
return;
|
2013-10-08 07:58:40 +00:00
|
|
|
}
|
2013-11-18 15:29:19 -08:00
|
|
|
|
2013-07-11 14:58:40 -07:00
|
|
|
String ringtone = TextSecurePreferences.getNotificationRingtone(context);
|
2013-02-08 11:57:54 -08:00
|
|
|
|
|
|
|
if (ringtone == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Uri uri = Uri.parse(ringtone);
|
|
|
|
MediaPlayer player = new MediaPlayer();
|
|
|
|
player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
|
|
|
|
player.setDataSource(context, uri);
|
|
|
|
player.setLooping(false);
|
|
|
|
player.setVolume(0.25f, 0.25f);
|
|
|
|
player.prepare();
|
|
|
|
|
|
|
|
final AudioManager audioManager = ((AudioManager)context.getSystemService(Context.AUDIO_SERVICE));
|
|
|
|
|
|
|
|
audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION,
|
|
|
|
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
|
|
|
|
|
|
|
|
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
|
|
|
|
@Override
|
|
|
|
public void onCompletion(MediaPlayer mp) {
|
|
|
|
audioManager.abandonAudioFocus(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
player.start();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w("MessageNotifier", ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 16:46:03 -07:00
|
|
|
private static void appendPushNotificationState(Context context,
|
|
|
|
MasterSecret masterSecret,
|
|
|
|
NotificationState notificationState,
|
|
|
|
Cursor cursor)
|
|
|
|
{
|
|
|
|
if (masterSecret != null) return;
|
|
|
|
|
|
|
|
PushDatabase.Reader reader = null;
|
|
|
|
IncomingPushMessage message;
|
|
|
|
|
|
|
|
try {
|
|
|
|
reader = DatabaseFactory.getPushDatabase(context).readerFor(cursor);
|
|
|
|
|
|
|
|
while ((message = reader.getNext()) != null) {
|
|
|
|
Recipient recipient;
|
|
|
|
|
|
|
|
try {
|
|
|
|
recipient = RecipientFactory.getRecipientsFromString(context, message.getSource(), false).getPrimaryRecipient();
|
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("MessageNotifier", e);
|
2014-02-02 19:38:06 -08:00
|
|
|
recipient = Recipient.getUnknownRecipient(context);
|
2013-09-09 16:46:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromMessage(context, message, false);
|
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
|
|
|
SpannableString body = new SpannableString(context.getString(R.string.MessageNotifier_encrypted_message));
|
|
|
|
body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
2014-02-26 12:31:56 -08:00
|
|
|
notificationState.addNotification(new NotificationItem(recipient, recipients, null, threadId, body, null));
|
2013-09-09 16:46:03 -07:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (reader != null)
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:57:54 -08:00
|
|
|
private static NotificationState constructNotificationState(Context context,
|
|
|
|
MasterSecret masterSecret,
|
|
|
|
Cursor cursor)
|
|
|
|
{
|
|
|
|
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) {
|
2014-02-26 12:31:56 -08:00
|
|
|
Recipient recipient = record.getIndividualRecipient();
|
|
|
|
Recipients recipients = record.getRecipients();
|
|
|
|
long threadId = record.getThreadId();
|
|
|
|
SpannableString body = record.getDisplayBody();
|
|
|
|
Uri image = null;
|
|
|
|
Recipients threadRecipients = null;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// XXXX This is so fucked up. FIX ME!
|
|
|
|
if (body.toString().equals(context.getString(R.string.MessageDisplayHelper_decrypting_please_wait))) {
|
|
|
|
body = new SpannableString(context.getString(R.string.MessageNotifier_encrypted_message));
|
|
|
|
body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2014-02-26 12:31:56 -08:00
|
|
|
notificationState.addNotification(new NotificationItem(recipient, recipients, threadRecipients, threadId, body, image));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void setNotificationAlarms(Context context,
|
|
|
|
NotificationCompat.Builder builder,
|
|
|
|
boolean signal)
|
|
|
|
{
|
2013-07-11 14:58:40 -07:00
|
|
|
String ringtone = TextSecurePreferences.getNotificationRingtone(context);
|
|
|
|
boolean vibrate = TextSecurePreferences.isNotificationVibrateEnabled(context);
|
|
|
|
String ledColor = TextSecurePreferences.getNotificationLedColor(context);
|
|
|
|
String ledBlinkPattern = TextSecurePreferences.getNotificationLedPattern(context);
|
|
|
|
String ledBlinkPatternCustom = TextSecurePreferences.getNotificationLedPatternCustom(context);
|
2013-02-08 11:57:54 -08:00
|
|
|
String[] blinkPatternArray = parseBlinkPattern(ledBlinkPattern, ledBlinkPatternCustom);
|
|
|
|
|
|
|
|
builder.setSound(TextUtils.isEmpty(ringtone) || !signal ? null : Uri.parse(ringtone));
|
|
|
|
|
2014-03-04 11:05:56 +01:00
|
|
|
if (signal && vibrate) {
|
2013-02-08 11:57:54 -08:00
|
|
|
builder.setDefaults(Notification.DEFAULT_VIBRATE);
|
2014-03-04 11:05:56 +01:00
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
|
2014-03-04 11:05:56 +01:00
|
|
|
if (!ledColor.equals("none")) {
|
|
|
|
builder.setLights(Color.parseColor(ledColor),
|
|
|
|
Integer.parseInt(blinkPatternArray[0]),
|
|
|
|
Integer.parseInt(blinkPatternArray[1]));
|
|
|
|
}
|
2013-02-08 11:57:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String[] parseBlinkPattern(String blinkPattern, String blinkPatternCustom) {
|
|
|
|
if (blinkPattern.equals("custom"))
|
|
|
|
blinkPattern = blinkPatternCustom;
|
|
|
|
|
|
|
|
return blinkPattern.split(",");
|
|
|
|
}
|
|
|
|
}
|