Support for notification privacy settings.

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-07-31 15:05:24 -07:00
parent d6d7ca19c1
commit 120cde9917
12 changed files with 450 additions and 221 deletions

View File

@ -180,24 +180,15 @@
<item>2</item> <item>2</item>
</string-array> </string-array>
<string-array name="default_color_choice_values" translatable="false"> <string-array name="pref_notification_privacy_entries">
<item>#ffF44336</item> <item>@string/arrays__name_and_message</item>
<item>#ffE91E63</item> <item>@string/arrays__name_only</item>
<item>#ff9C27B0</item> <item>@string/arrays__neither</item>
<item>#ff673AB7</item> </string-array>
<item>#ff3F51B5</item>
<item>#ff2196F3</item>
<item>#ff03A9F4</item>
<item>#ff00BCD4</item>
<item>#ff009688</item>
<item>#ff4CAF50</item>
<item>#ff8BC34A</item>
<!--<item>#FFCDDC39</item>-->
<item>#FFFFC107</item>
<item>#ffFF9800</item>
<item>#ffFF5722</item>
<item>#ff795548</item>
<item>#ff607D8B</item>
</string-array>
<string-array name="pref_notification_privacy_values">
<item>all</item>
<item>contact</item>
<item>none</item>
</string-array>
</resources> </resources>

View File

@ -467,7 +467,7 @@
<string name="KeyCachingService_lock">Lock with passphrase</string> <string name="KeyCachingService_lock">Lock with passphrase</string>
<!-- MessageNotifier --> <!-- MessageNotifier -->
<string name="MessageNotifier_d_messages_in_d_conversations">%1$d messages in %2$d conversations</string> <string name="MessageNotifier_d_new_messages_in_d_conversations">%1$d new messages in %2$d conversations</string>
<string name="MessageNotifier_most_recent_from_s">Most recent from: %1$s</string> <string name="MessageNotifier_most_recent_from_s">Most recent from: %1$s</string>
<string name="MessageNotifier_locked_message">Locked message...</string> <string name="MessageNotifier_locked_message">Locked message...</string>
<string name="MessageNotifier_media_message_with_text">Media message: %s</string> <string name="MessageNotifier_media_message_with_text">Media message: %s</string>
@ -484,6 +484,10 @@
<string name="QuickResponseService_quick_response_unavailable_when_TextSecure_is_locked">Quick response unavailable when TextSecure is locked!</string> <string name="QuickResponseService_quick_response_unavailable_when_TextSecure_is_locked">Quick response unavailable when TextSecure is locked!</string>
<string name="QuickResponseService_problem_sending_message">Problem sending message!</string> <string name="QuickResponseService_problem_sending_message">Problem sending message!</string>
<!-- SingleRecipientNotificationBuilder -->
<string name="SingleRecipientNotificationBuilder_new_textsecure_message">New TextSecure message</string>
<string name="SingleRecipientNotificationBuilder_contents_hidden">Contents hidden</string>
<!-- change_passphrase_activity --> <!-- change_passphrase_activity -->
<string name="change_passphrase_activity__old_passphrase">OLD PASSPHRASE:</string> <string name="change_passphrase_activity__old_passphrase">OLD PASSPHRASE:</string>
<string name="change_passphrase_activity__new_passphrase">NEW PASSPHRASE:</string> <string name="change_passphrase_activity__new_passphrase">NEW PASSPHRASE:</string>
@ -754,6 +758,10 @@
<string name="arrays__enabled">Enabled</string> <string name="arrays__enabled">Enabled</string>
<string name="arrays__disabled">Disabled</string> <string name="arrays__disabled">Disabled</string>
<string name="arrays__name_and_message">Name and message</string>
<string name="arrays__name_only">Name only</string>
<string name="arrays__neither">Neither</string>
<!-- plurals.xml --> <!-- plurals.xml -->
<plurals name="hours_ago"> <plurals name="hours_ago">
<item quantity="one">%d hour</item> <item quantity="one">%d hour</item>
@ -858,6 +866,7 @@
<string name="preferences__support_wifi_calling">\'WiFi Calling\' compatibility mode</string> <string name="preferences__support_wifi_calling">\'WiFi Calling\' compatibility mode</string>
<string name="preferences__enable_if_your_device_supports_sms_mms_delivery_over_wifi">Enable if your device uses SMS/MMS delivery over WiFi (only enable when \'WiFi Calling\' is enabled on your device)</string> <string name="preferences__enable_if_your_device_supports_sms_mms_delivery_over_wifi">Enable if your device uses SMS/MMS delivery over WiFi (only enable when \'WiFi Calling\' is enabled on your device)</string>
<string name="preferences_app_protection__blocked_contacts">Blocked contacts</string> <string name="preferences_app_protection__blocked_contacts">Blocked contacts</string>
<string name="preferences_notifications__display_in_notifications">Display in notifications</string>
<!-- **************************************** --> <!-- **************************************** -->
<!-- menus --> <!-- menus -->

View File

@ -36,7 +36,6 @@
android:entries="@array/pref_led_blink_pattern_entries" android:entries="@array/pref_led_blink_pattern_entries"
android:entryValues="@array/pref_led_blink_pattern_values" /> android:entryValues="@array/pref_led_blink_pattern_values" />
<CheckBoxPreference android:key="pref_key_inthread_notifications" <CheckBoxPreference android:key="pref_key_inthread_notifications"
android:title="@string/preferences__inthread_notifications" android:title="@string/preferences__inthread_notifications"
android:summary="@string/preferences__play_inthread_notifications" android:summary="@string/preferences__play_inthread_notifications"
@ -44,10 +43,17 @@
android:defaultValue="true" /> android:defaultValue="true" />
<ListPreference <ListPreference
android:key="pref_repeat_alerts" android:key="pref_repeat_alerts"
android:defaultValue="0" android:defaultValue="0"
android:title="@string/preferences__repeat_alerts" android:title="@string/preferences__repeat_alerts"
android:dependency="pref_key_enable_notifications" android:dependency="pref_key_enable_notifications"
android:entries="@array/pref_repeat_alerts_entries" android:entries="@array/pref_repeat_alerts_entries"
android:entryValues="@array/pref_repeat_alerts_values" /> android:entryValues="@array/pref_repeat_alerts_values" />
<ListPreference android:key="pref_notification_privacy"
android:title="@string/preferences_notifications__display_in_notifications"
android:defaultValue="all"
android:entries="@array/pref_notification_privacy_entries"
android:entryValues="@array/pref_notification_privacy_values"/>
</PreferenceScreen> </PreferenceScreen>

View File

@ -0,0 +1,70 @@
package org.thoughtcrime.securesms.notifications;
import android.app.Notification;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase;
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
public abstract class AbstractNotificationBuilder extends NotificationCompat.Builder {
protected Context context;
protected NotificationPrivacyPreference privacy;
public AbstractNotificationBuilder(Context context, NotificationPrivacyPreference privacy) {
super(context);
this.context = context;
this.privacy = privacy;
}
protected CharSequence getStyledMessage(@NonNull Recipient recipient, @Nullable CharSequence message) {
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(Util.getBoldedString(recipient.toShortString()));
builder.append(": ");
builder.append(message == null ? "" : message);
return builder;
}
public void setAlarms(@Nullable Uri ringtone, RecipientPreferenceDatabase.VibrateState vibrate) {
String defaultRingtoneName = TextSecurePreferences.getNotificationRingtone(context);
boolean defaultVibrate = TextSecurePreferences.isNotificationVibrateEnabled(context);
String ledColor = TextSecurePreferences.getNotificationLedColor(context);
String ledBlinkPattern = TextSecurePreferences.getNotificationLedPattern(context);
String ledBlinkPatternCustom = TextSecurePreferences.getNotificationLedPatternCustom(context);
String[] blinkPatternArray = parseBlinkPattern(ledBlinkPattern, ledBlinkPatternCustom);
if (ringtone != null) setSound(ringtone);
else if (TextUtils.isEmpty(defaultRingtoneName)) setSound(Uri.parse(defaultRingtoneName));
if (vibrate == RecipientPreferenceDatabase.VibrateState.ENABLED ||
(vibrate == RecipientPreferenceDatabase.VibrateState.DEFAULT && defaultVibrate))
{
setDefaults(Notification.DEFAULT_VIBRATE);
}
if (!ledColor.equals("none")) {
setLights(Color.parseColor(ledColor),
Integer.parseInt(blinkPatternArray[0]),
Integer.parseInt(blinkPatternArray[1]));
}
}
private String[] parseBlinkPattern(String blinkPattern, String blinkPatternCustom) {
if (blinkPattern.equals("custom"))
blinkPattern = blinkPatternCustom;
return blinkPattern.split(",");
}
}

View File

@ -0,0 +1,30 @@
package org.thoughtcrime.securesms.notifications;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase;
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
public class FailedNotificationBuilder extends AbstractNotificationBuilder {
public FailedNotificationBuilder(Context context, NotificationPrivacyPreference privacy, Intent intent) {
super(context, privacy);
setSmallIcon(R.drawable.icon_notification);
setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_action_warning_red));
setContentTitle(context.getString(R.string.MessageNotifier_message_delivery_failed));
setContentText(context.getString(R.string.MessageNotifier_failed_to_deliver_message));
setTicker(context.getString(R.string.MessageNotifier_error_delivering_message));
setContentIntent(PendingIntent.getActivity(context, 0, intent, 0));
setAutoCancel(true);
setAlarms(null, RecipientPreferenceDatabase.VibrateState.DEFAULT);
}
}

View File

@ -17,43 +17,29 @@
package org.thoughtcrime.securesms.notifications; package org.thoughtcrime.securesms.notifications;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Action;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.InboxStyle;
import android.support.v4.app.RemoteInput;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.util.Log; import android.util.Log;
import org.thoughtcrime.securesms.ConversationActivity; import org.thoughtcrime.securesms.ConversationActivity;
import org.thoughtcrime.securesms.ConversationListActivity;
import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MmsSmsDatabase; import org.thoughtcrime.securesms.database.MmsSmsDatabase;
import org.thoughtcrime.securesms.database.PushDatabase; import org.thoughtcrime.securesms.database.PushDatabase;
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
import org.thoughtcrime.securesms.database.SmsDatabase; import org.thoughtcrime.securesms.database.SmsDatabase;
import org.thoughtcrime.securesms.database.ThreadDatabase; import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.database.model.MessageRecord; import org.thoughtcrime.securesms.database.model.MessageRecord;
@ -61,7 +47,6 @@ import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory; import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.SpanUtil; import org.thoughtcrime.securesms.util.SpanUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope; import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
@ -82,6 +67,7 @@ import me.leolin.shortcutbadger.ShortcutBadger;
*/ */
public class MessageNotifier { public class MessageNotifier {
private static final String TAG = MessageNotifier.class.getSimpleName(); private static final String TAG = MessageNotifier.class.getSimpleName();
public static final int NOTIFICATION_ID = 1338; public static final int NOTIFICATION_ID = 1338;
@ -101,19 +87,9 @@ public class MessageNotifier {
Intent intent = new Intent(context, ConversationActivity.class); Intent intent = new Intent(context, ConversationActivity.class);
intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.getIds()); intent.putExtra(ConversationActivity.RECIPIENTS_EXTRA, recipients.getIds());
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId); intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, threadId);
intent.setData((Uri.parse("custom://"+System.currentTimeMillis()))); 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(),
R.drawable.ic_action_warning_red));
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, null, VibrateState.DEFAULT);
FailedNotificationBuilder builder = new FailedNotificationBuilder(context, TextSecurePreferences.getNotificationPrivacy(context), intent);
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)) ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify((int)threadId, builder.build()); .notify((int)threadId, builder.build());
} }
@ -187,13 +163,13 @@ public class MessageNotifier {
} }
if (notificationState.hasMultipleThreads()) { if (notificationState.hasMultipleThreads()) {
sendMultipleThreadNotification(context, masterSecret, notificationState, signal); sendMultipleThreadNotification(context, notificationState, signal);
} else { } else {
sendSingleThreadNotification(context, masterSecret, notificationState, signal); sendSingleThreadNotification(context, masterSecret, notificationState, signal);
} }
updateBadge(context, notificationState.getMessageCount()); updateBadge(context, notificationState.getMessageCount());
scheduleReminder(context, masterSecret, reminderCount); scheduleReminder(context, reminderCount);
} finally { } finally {
if (telcoCursor != null) telcoCursor.close(); if (telcoCursor != null) telcoCursor.close();
if (pushCursor != null) pushCursor.close(); if (pushCursor != null) pushCursor.close();
@ -211,73 +187,33 @@ public class MessageNotifier {
return; return;
} }
List<NotificationItem> notifications = notificationState.getNotifications(); SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder(context, TextSecurePreferences.getNotificationPrivacy(context));
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); List<NotificationItem> notifications = notificationState.getNotifications();
Recipients recipients = notifications.get(0).getRecipients();
Recipient recipient = notifications.get(0).getIndividualRecipient();
int largeIconTargetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
Drawable recipientPhoto = recipient.getContactPhoto().asDrawable(context, recipients == null ? ContactColors.UNKNOWN_COLOR.toConversationColor(context) :
recipients.getColor().toConversationColor(context));
if (recipientPhoto != null) { builder.setSender(notifications.get(0).getIndividualRecipient());
Bitmap recipientPhotoBitmap = BitmapUtil.createFromDrawable(recipientPhoto, largeIconTargetSize, largeIconTargetSize); builder.setMessageCount(notificationState.getMessageCount());
if (recipientPhotoBitmap != null) builder.setLargeIcon(recipientPhotoBitmap); builder.setPrimaryMessageBody(notifications.get(0).getText());
}
builder.setSmallIcon(R.drawable.icon_notification);
builder.setColor(context.getResources().getColor(R.color.textsecure_primary));
builder.setContentTitle(recipient.toShortString());
builder.setContentText(notifications.get(0).getText());
builder.setContentIntent(notifications.get(0).getPendingIntent(context)); builder.setContentIntent(notifications.get(0).getPendingIntent(context));
builder.setContentInfo(String.valueOf(notificationState.getMessageCount()));
builder.setPriority(NotificationCompat.PRIORITY_HIGH);
builder.setNumber(notificationState.getMessageCount());
builder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
builder.setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(DeleteReceiver.DELETE_REMINDER_ACTION), 0));
if (recipient.getContactUri() != null) builder.addPerson(recipient.getContactUri().toString());
long timestamp = notifications.get(0).getTimestamp(); long timestamp = notifications.get(0).getTimestamp();
if (timestamp != 0) builder.setWhen(timestamp); if (timestamp != 0) builder.setWhen(timestamp);
if (masterSecret != null) { builder.addActions(masterSecret,
Action markAsReadAction = new Action(R.drawable.check, notificationState.getMarkAsReadIntent(context),
context.getString(R.string.MessageNotifier_mark_read), notificationState.getQuickReplyIntent(context, notifications.get(0).getRecipients()),
notificationState.getMarkAsReadIntent(context)); notificationState.getWearableReplyIntent(context, notifications.get(0).getRecipients()));
Action replyAction = new Action(R.drawable.ic_reply_white_36dp,
context.getString(R.string.MessageNotifier_reply),
notificationState.getQuickReplyIntent(context, recipients));
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
context.getString(R.string.MessageNotifier_reply),
notificationState.getWearableReplyIntent(context, recipients))
.addRemoteInput(new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(context.getString(R.string.MessageNotifier_reply)).build())
.build();
builder.addAction(markAsReadAction);
builder.addAction(replyAction);
builder.extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction)
.addAction(wearableReplyAction));
}
SpannableStringBuilder content = new SpannableStringBuilder();
ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size()); ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size());
while(iterator.hasPrevious()) { while(iterator.hasPrevious()) {
NotificationItem item = iterator.previous(); builder.addMessageBody(iterator.previous().getText());
content.append(item.getBigStyleSummary());
content.append('\n');
} }
builder.setStyle(new BigTextStyle().bigText(content));
setNotificationAlarms(context, builder, signal,
notificationState.getRingtone(),
notificationState.getVibrate());
if (signal) { if (signal) {
builder.setTicker(notifications.get(0).getTickerText()); builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate());
builder.setTicker(notifications.get(0).getIndividualRecipient(),
notifications.get(0).getText());
} }
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)) ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
@ -285,59 +221,30 @@ public class MessageNotifier {
} }
private static void sendMultipleThreadNotification(@NonNull Context context, private static void sendMultipleThreadNotification(@NonNull Context context,
@Nullable MasterSecret masterSecret,
@NonNull NotificationState notificationState, @NonNull NotificationState notificationState,
boolean signal) boolean signal)
{ {
List<NotificationItem> notifications = notificationState.getNotifications(); MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, TextSecurePreferences.getNotificationPrivacy(context));
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); List<NotificationItem> notifications = notificationState.getNotifications();
builder.setColor(context.getResources().getColor(R.color.textsecure_primary)); builder.setMessageCount(notificationState.getMessageCount(), notificationState.getThreadCount());
builder.setSmallIcon(R.drawable.icon_notification); builder.setMostRecentSender(notifications.get(0).getIndividualRecipient());
builder.setContentTitle(context.getString(R.string.app_name));
builder.setSubText(context.getString(R.string.MessageNotifier_d_messages_in_d_conversations,
notificationState.getMessageCount(),
notificationState.getThreadCount()));
builder.setContentText(context.getString(R.string.MessageNotifier_most_recent_from_s,
notifications.get(0).getIndividualRecipientName()));
builder.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, ConversationListActivity.class), 0));
builder.setContentInfo(String.valueOf(notificationState.getMessageCount()));
builder.setNumber(notificationState.getMessageCount());
builder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
long timestamp = notifications.get(0).getTimestamp(); long timestamp = notifications.get(0).getTimestamp();
if (timestamp != 0) builder.setWhen(timestamp); if (timestamp != 0) builder.setWhen(timestamp);
builder.setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(DeleteReceiver.DELETE_REMINDER_ACTION), 0)); builder.addActions(notificationState.getMarkAsReadIntent(context));
if (masterSecret != null) {
Action markAllAsReadAction = new Action(R.drawable.check,
context.getString(R.string.MessageNotifier_mark_all_as_read),
notificationState.getMarkAsReadIntent(context));
builder.addAction(markAllAsReadAction);
builder.extend(new NotificationCompat.WearableExtender().addAction(markAllAsReadAction));
}
InboxStyle style = new InboxStyle();
ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size()); ListIterator<NotificationItem> iterator = notifications.listIterator(notifications.size());
while(iterator.hasPrevious()) { while(iterator.hasPrevious()) {
NotificationItem item = iterator.previous(); NotificationItem item = iterator.previous();
style.addLine(item.getTickerText()); builder.addMessageBody(item.getIndividualRecipient(), item.getText());
if (item.getIndividualRecipient().getContactUri() != null) {
builder.addPerson(item.getIndividualRecipient().getContactUri().toString());
}
} }
builder.setStyle(style);
setNotificationAlarms(context, builder, signal,
notificationState.getRingtone(),
notificationState.getVibrate());
if (signal) { if (signal) {
builder.setTicker(notifications.get(0).getTickerText()); builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate());
builder.setTicker(notifications.get(0).getText());
} }
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)) ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE))
@ -411,7 +318,7 @@ public class MessageNotifier {
body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, body.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (!recipients.isMuted()) { if (!recipients.isMuted()) {
notificationState.addNotification(new NotificationItem(recipient, recipients, null, threadId, body, null, 0)); notificationState.addNotification(new NotificationItem(recipient, recipients, null, threadId, body, 0));
} }
} }
} finally { } finally {
@ -436,7 +343,6 @@ public class MessageNotifier {
Recipients recipients = record.getRecipients(); Recipients recipients = record.getRecipients();
long threadId = record.getThreadId(); long threadId = record.getThreadId();
CharSequence body = record.getDisplayBody(); CharSequence body = record.getDisplayBody();
Uri image = null;
Recipients threadRecipients = null; Recipients threadRecipients = null;
long timestamp; long timestamp;
@ -458,7 +364,7 @@ public class MessageNotifier {
} }
if (threadRecipients == null || !threadRecipients.isMuted()) { if (threadRecipients == null || !threadRecipients.isMuted()) {
notificationState.addNotification(new NotificationItem(recipient, recipients, threadRecipients, threadId, body, image, timestamp)); notificationState.addNotification(new NotificationItem(recipient, recipients, threadRecipients, threadId, body, timestamp));
} }
} }
@ -466,42 +372,6 @@ public class MessageNotifier {
return notificationState; return notificationState;
} }
private static void setNotificationAlarms(Context context,
NotificationCompat.Builder builder,
boolean signal,
@Nullable Uri ringtone,
VibrateState vibrate)
{
String defaultRingtoneName = TextSecurePreferences.getNotificationRingtone(context);
boolean defaultVibrate = TextSecurePreferences.isNotificationVibrateEnabled(context);
String ledColor = TextSecurePreferences.getNotificationLedColor(context);
String ledBlinkPattern = TextSecurePreferences.getNotificationLedPattern(context);
String ledBlinkPatternCustom = TextSecurePreferences.getNotificationLedPatternCustom(context);
String[] blinkPatternArray = parseBlinkPattern(ledBlinkPattern, ledBlinkPatternCustom);
if (signal && ringtone != null) builder.setSound(ringtone);
else if (signal && !TextUtils.isEmpty(defaultRingtoneName)) builder.setSound(Uri.parse(defaultRingtoneName));
else builder.setSound(null);
if (signal && (vibrate == VibrateState.ENABLED || (vibrate == VibrateState.DEFAULT && defaultVibrate))) {
builder.setDefaults(Notification.DEFAULT_VIBRATE);
}
if (!ledColor.equals("none")) {
builder.setLights(Color.parseColor(ledColor),
Integer.parseInt(blinkPatternArray[0]),
Integer.parseInt(blinkPatternArray[1]));
}
}
private static String[] parseBlinkPattern(String blinkPattern, String blinkPatternCustom) {
if (blinkPattern.equals("custom"))
blinkPattern = blinkPatternCustom;
return blinkPattern.split(",");
}
private static void updateBadge(Context context, int count) { private static void updateBadge(Context context, int count) {
try { try {
ShortcutBadger.setBadge(context.getApplicationContext(), count); ShortcutBadger.setBadge(context.getApplicationContext(), count);
@ -512,7 +382,7 @@ public class MessageNotifier {
} }
} }
private static void scheduleReminder(Context context, MasterSecret masterSecret, int count) { private static void scheduleReminder(Context context, int count) {
if (count >= TextSecurePreferences.getRepeatAlertsCount(context)) { if (count >= TextSecurePreferences.getRepeatAlertsCount(context)) {
return; return;
} }

View File

@ -0,0 +1,84 @@
package org.thoughtcrime.securesms.notifications;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import org.thoughtcrime.securesms.ConversationListActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.Util;
import java.util.LinkedList;
import java.util.List;
public class MultipleRecipientNotificationBuilder extends AbstractNotificationBuilder {
private final List<CharSequence> messageBodies = new LinkedList<>();
public MultipleRecipientNotificationBuilder(Context context, NotificationPrivacyPreference privacy) {
super(context, privacy);
setColor(context.getResources().getColor(R.color.textsecure_primary));
setSmallIcon(R.drawable.icon_notification);
setContentTitle(context.getString(R.string.app_name));
setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, ConversationListActivity.class), 0));
setCategory(NotificationCompat.CATEGORY_MESSAGE);
setPriority(NotificationCompat.PRIORITY_HIGH);
setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(MessageNotifier.DeleteReceiver.DELETE_REMINDER_ACTION), 0));
}
public void setMessageCount(int messageCount, int threadCount) {
setSubText(context.getString(R.string.MessageNotifier_d_new_messages_in_d_conversations,
messageCount, threadCount));
setContentInfo(String.valueOf(messageCount));
setNumber(messageCount);
}
public void setMostRecentSender(Recipient recipient) {
if (privacy.isDisplayContact()) {
setContentText(context.getString(R.string.MessageNotifier_most_recent_from_s,
recipient.toShortString()));
}
}
public void addActions(PendingIntent markAsReadIntent) {
NotificationCompat.Action markAllAsReadAction = new NotificationCompat.Action(R.drawable.check,
context.getString(R.string.MessageNotifier_mark_all_as_read),
markAsReadIntent);
addAction(markAllAsReadAction);
extend(new NotificationCompat.WearableExtender().addAction(markAllAsReadAction));
}
public void addMessageBody(@NonNull Recipient sender, @Nullable CharSequence body) {
if (privacy.isDisplayMessage()) {
messageBodies.add(getStyledMessage(sender, body));
} else if (privacy.isDisplayContact()) {
messageBodies.add(Util.getBoldedString(sender.toShortString()));
}
if (privacy.isDisplayContact() && sender.getContactUri() != null) {
addPerson(sender.getContactUri().toString());
}
}
@Override
public Notification build() {
if (privacy.isDisplayMessage() || privacy.isDisplayContact()) {
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
for (CharSequence body : messageBodies) {
style.addLine(body);
}
setStyle(style);
}
return super.build();
}
}

View File

@ -4,14 +4,10 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.SpannableStringBuilder;
import org.thoughtcrime.securesms.ConversationActivity; import org.thoughtcrime.securesms.ConversationActivity;
import org.thoughtcrime.securesms.ConversationPopupActivity;
import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.Util;
public class NotificationItem { public class NotificationItem {
@ -20,18 +16,16 @@ public class NotificationItem {
private final Recipients threadRecipients; private final Recipients threadRecipients;
private final long threadId; private final long threadId;
private final CharSequence text; private final CharSequence text;
private final Uri image;
private final long timestamp; private final long timestamp;
public NotificationItem(Recipient individualRecipient, Recipients recipients, public NotificationItem(Recipient individualRecipient, Recipients recipients,
Recipients threadRecipients, long threadId, Recipients threadRecipients, long threadId,
CharSequence text, Uri image, long timestamp) CharSequence text, long timestamp)
{ {
this.individualRecipient = individualRecipient; this.individualRecipient = individualRecipient;
this.recipients = recipients; this.recipients = recipients;
this.threadRecipients = threadRecipients; this.threadRecipients = threadRecipients;
this.text = text; this.text = text;
this.image = image;
this.threadId = threadId; this.threadId = threadId;
this.timestamp = timestamp; this.timestamp = timestamp;
} }
@ -44,10 +38,6 @@ public class NotificationItem {
return individualRecipient; return individualRecipient;
} }
public String getIndividualRecipientName() {
return individualRecipient.toShortString();
}
public CharSequence getText() { public CharSequence getText() {
return text; return text;
} }
@ -56,31 +46,10 @@ public class NotificationItem {
return timestamp; return timestamp;
} }
public Uri getImage() {
return image;
}
public boolean hasImage() {
return image != null;
}
public long getThreadId() { public long getThreadId() {
return threadId; return threadId;
} }
public CharSequence getBigStyleSummary() {
return (text == null) ? "" : text;
}
public CharSequence getTickerText() {
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(Util.getBoldedString(getIndividualRecipientName()));
builder.append(": ");
builder.append(getText());
return builder;
}
public PendingIntent getPendingIntent(Context context) { public PendingIntent getPendingIntent(Context context) {
Intent intent = new Intent(context, ConversationActivity.class); Intent intent = new Intent(context, ConversationActivity.class);
Recipients notifyRecipients = threadRecipients != null ? threadRecipients : recipients; Recipients notifyRecipients = threadRecipients != null ? threadRecipients : recipients;

View File

@ -0,0 +1,149 @@
package org.thoughtcrime.securesms.notifications;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Action;
import android.support.v4.app.RemoteInput;
import android.text.SpannableStringBuilder;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.BitmapUtil;
import java.util.LinkedList;
import java.util.List;
public class SingleRecipientNotificationBuilder extends AbstractNotificationBuilder {
private final List<CharSequence> messageBodies = new LinkedList<>();
public SingleRecipientNotificationBuilder(@NonNull Context context, @NonNull NotificationPrivacyPreference privacy) {
super(context, privacy);
setSmallIcon(R.drawable.icon_notification);
setColor(context.getResources().getColor(R.color.textsecure_primary));
setPriority(NotificationCompat.PRIORITY_HIGH);
setCategory(NotificationCompat.CATEGORY_MESSAGE);
setDeleteIntent(PendingIntent.getBroadcast(context, 0, new Intent(MessageNotifier.DeleteReceiver.DELETE_REMINDER_ACTION), 0));
}
public void setSender(@NonNull Recipient recipient) {
if (privacy.isDisplayContact()) {
setContentTitle(recipient.toShortString());
if (recipient.getContactUri() != null) {
addPerson(recipient.getContactUri().toString());
}
setLargeIcon(recipient.getContactPhoto()
.asDrawable(context, recipient.getColor()
.toConversationColor(context)));
} else {
setContentTitle(context.getString(R.string.SingleRecipientNotificationBuilder_new_textsecure_message));
setLargeIcon(Recipient.getUnknownRecipient()
.getContactPhoto()
.asDrawable(context, Recipient.getUnknownRecipient()
.getColor()
.toConversationColor(context)));
}
}
public void setMessageCount(int messageCount) {
setContentInfo(String.valueOf(messageCount));
setNumber(messageCount);
}
public void setPrimaryMessageBody(CharSequence message) {
if (privacy.isDisplayMessage()) {
setContentText(message);
} else {
setContentText(context.getString(R.string.SingleRecipientNotificationBuilder_contents_hidden));
}
}
public void addActions(@Nullable MasterSecret masterSecret,
@NonNull PendingIntent markReadIntent,
@NonNull PendingIntent quickReplyIntent,
@NonNull PendingIntent wearableReplyIntent)
{
Action markAsReadAction = new Action(R.drawable.check,
context.getString(R.string.MessageNotifier_mark_read),
markReadIntent);
if (masterSecret != null) {
Action replyAction = new Action(R.drawable.ic_reply_white_36dp,
context.getString(R.string.MessageNotifier_reply),
quickReplyIntent);
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
context.getString(R.string.MessageNotifier_reply),
wearableReplyIntent)
.addRemoteInput(new RemoteInput.Builder(MessageNotifier.EXTRA_VOICE_REPLY)
.setLabel(context.getString(R.string.MessageNotifier_reply)).build())
.build();
addAction(markAsReadAction);
addAction(replyAction);
extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction)
.addAction(wearableReplyAction));
} else {
addAction(markAsReadAction);
extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction));
}
}
public void addMessageBody(@Nullable CharSequence messageBody) {
if (privacy.isDisplayMessage()) {
messageBodies.add(messageBody == null ? "" : messageBody);
}
}
public void setTicker(@NonNull Recipient recipient, @Nullable CharSequence message) {
if (privacy.isDisplayMessage()) {
setTicker(getStyledMessage(recipient, message));
} else if (privacy.isDisplayContact()) {
setTicker(getStyledMessage(recipient, context.getString(R.string.SingleRecipientNotificationBuilder_new_textsecure_message)));
} else {
setTicker(context.getString(R.string.SingleRecipientNotificationBuilder_new_textsecure_message));
}
}
@Override
public Notification build() {
if (privacy.isDisplayMessage()) {
SpannableStringBuilder content = new SpannableStringBuilder();
for (CharSequence message : messageBodies) {
content.append(message);
content.append('\n');
}
setStyle(new NotificationCompat.BigTextStyle().bigText(content));
}
return super.build();
}
private void setLargeIcon(@Nullable Drawable drawable) {
if (drawable != null) {
int largeIconTargetSize = context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size);
Bitmap recipientPhotoBitmap = BitmapUtil.createFromDrawable(drawable, largeIconTargetSize, largeIconTargetSize);
if (recipientPhotoBitmap != null) {
setLargeIcon(recipientPhotoBitmap);
}
}
}
}

View File

@ -0,0 +1,19 @@
package org.thoughtcrime.securesms.preferences;
public class NotificationPrivacyPreference {
private final String preference;
public NotificationPrivacyPreference(String preference) {
this.preference = preference;
}
public boolean isDisplayContact() {
return "all".equals(preference) || "contact".equals(preference);
}
public boolean isDisplayMessage() {
return "all".equals(preference);
}
}

View File

@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.media.Ringtone; import android.media.Ringtone;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
@ -14,13 +15,18 @@ import android.text.TextUtils;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity; import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragment { public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragment {
private MasterSecret masterSecret;
@Override @Override
public void onCreate(Bundle paramBundle) { public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle); super.onCreate(paramBundle);
masterSecret = getArguments().getParcelable("master_secret");
addPreferencesFromResource(R.xml.preferences_notifications); addPreferencesFromResource(R.xml.preferences_notifications);
this.findPreference(TextSecurePreferences.LED_COLOR_PREF) this.findPreference(TextSecurePreferences.LED_COLOR_PREF)
@ -31,10 +37,13 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
.setOnPreferenceChangeListener(new RingtoneSummaryListener()); .setOnPreferenceChangeListener(new RingtoneSummaryListener());
this.findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF) this.findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF)
.setOnPreferenceChangeListener(new ListSummaryListener()); .setOnPreferenceChangeListener(new ListSummaryListener());
this.findPreference(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF)
.setOnPreferenceChangeListener(new NotificationPrivacyListener());
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_COLOR_PREF)); initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_COLOR_PREF));
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_BLINK_PREF)); initializeListSummary((ListPreference) findPreference(TextSecurePreferences.LED_BLINK_PREF));
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF)); initializeListSummary((ListPreference) findPreference(TextSecurePreferences.REPEAT_ALERTS_PREF));
initializeListSummary((ListPreference) findPreference(TextSecurePreferences.NOTIFICATION_PRIVACY_PREF));
initializeRingtoneSummary((RingtonePreference) findPreference(TextSecurePreferences.RINGTONE_PREF)); initializeRingtoneSummary((RingtonePreference) findPreference(TextSecurePreferences.RINGTONE_PREF));
} }
@ -76,4 +85,20 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
return context.getString(TextSecurePreferences.isNotificationsEnabled(context) ? onCapsResId : offCapsResId); return context.getString(TextSecurePreferences.isNotificationsEnabled(context) ? onCapsResId : offCapsResId);
} }
private class NotificationPrivacyListener extends ListSummaryListener {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
MessageNotifier.updateNotification(getActivity(), masterSecret);
return null;
}
}.execute();
return super.onPreferenceChange(preference, value);
}
}
} }

View File

@ -6,6 +6,8 @@ import android.preference.PreferenceManager;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
import java.io.IOException; import java.io.IOException;
public class TextSecurePreferences { public class TextSecurePreferences {
@ -70,6 +72,11 @@ public class TextSecurePreferences {
private static final String RATING_ENABLED_PREF = "pref_rating_enabled"; private static final String RATING_ENABLED_PREF = "pref_rating_enabled";
public static final String REPEAT_ALERTS_PREF = "pref_repeat_alerts"; public static final String REPEAT_ALERTS_PREF = "pref_repeat_alerts";
public static final String NOTIFICATION_PRIVACY_PREF = "pref_notification_privacy";
public static NotificationPrivacyPreference getNotificationPrivacy(Context context) {
return new NotificationPrivacyPreference(getStringPreference(context, NOTIFICATION_PRIVACY_PREF, "all"));
}
public static long getRatingLaterTimestamp(Context context) { public static long getRatingLaterTimestamp(Context context) {
return getLongPreference(context, RATING_LATER_PREF, 0); return getLongPreference(context, RATING_LATER_PREF, 0);