Merge pull request #941 from deltachat/notification_review

Notification review
This commit is contained in:
björn petersen
2019-06-05 18:10:31 +02:00
committed by GitHub
9 changed files with 107 additions and 67 deletions
@@ -209,6 +209,7 @@ public class ConversationFragment extends Fragment
public void onResume() {
super.onResume();
dcContext.marknoticedChat(Long.valueOf(chatId).intValue());
if (list.getAdapter() != null) {
list.getAdapter().notifyDataSetChanged();
}
@@ -65,6 +65,7 @@ import org.thoughtcrime.securesms.connect.ApplicationDcContext;
import org.thoughtcrime.securesms.connect.DcChatlistLoader;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.mms.GlideApp;
import org.thoughtcrime.securesms.notifications.MessageNotifierCompat;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.ViewUtil;
import org.thoughtcrime.securesms.util.guava.Optional;
@@ -312,6 +313,7 @@ public class ConversationListFragment extends Fragment
dcContext.marknoticedContact(getListAdapter().getDeaddropContactId());
}
else {
MessageNotifierCompat.removeNotifications((int) chatId);
dcContext.deleteChat((int) chatId);
}
}
@@ -1,11 +1,10 @@
package org.thoughtcrime.securesms.notifications;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
@@ -35,7 +34,7 @@ abstract class AbstractNotificationBuilder extends NotificationCompat.Builder {
private Uri ringtone;
private boolean vibrate;
AbstractNotificationBuilder(Context context, NotificationPrivacyPreference privacy, boolean signal) {
AbstractNotificationBuilder(Context context, NotificationPrivacyPreference privacy) {
super(context, createMsgNotificationChannel(context));
this.context = context;
@@ -56,14 +55,20 @@ abstract class AbstractNotificationBuilder extends NotificationCompat.Builder {
// Alarms are not set in the notification or the notification channel but handled separately
// by the MessageNotifier. It allows us to dynamically turn on and off the sounds and as well as
// to change vibration and sounds during runtime
void setAlarms(@Nullable Uri ringtone, Prefs.VibrateState vibrate) {
Uri defaultRingtone = Prefs.getNotificationRingtone(context);
boolean defaultVibrate = Prefs.isNotificationVibrateEnabled(context);
if (ringtone == null && !TextUtils.isEmpty(defaultRingtone.toString())) this.ringtone = defaultRingtone;
else if (ringtone != null && !ringtone.toString().isEmpty()) this.ringtone = ringtone;
void setAlarms(int systemRingerMode, @Nullable Uri ringtone, Prefs.VibrateState vibrate) {
Uri appDefaultRingtone = Prefs.getNotificationRingtone(context);
boolean appDefaultVibrate = Prefs.isNotificationVibrateEnabled(context);
if (systemRingerMode == AudioManager.RINGER_MODE_NORMAL) {
if (ringtone == null && !TextUtils.isEmpty(appDefaultRingtone.toString())) {
this.ringtone = appDefaultRingtone;
} else if (ringtone != null && !ringtone.toString().isEmpty()) {
this.ringtone = ringtone;
}
}
this.vibrate = (vibrate == Prefs.VibrateState.ENABLED ||
(vibrate == Prefs.VibrateState.DEFAULT && defaultVibrate));
this.vibrate = (systemRingerMode != AudioManager.RINGER_MODE_SILENT) &&
(vibrate == Prefs.VibrateState.ENABLED ||
(vibrate == Prefs.VibrateState.DEFAULT && appDefaultVibrate));
}
private void setLed() {
@@ -111,7 +116,7 @@ abstract class AbstractNotificationBuilder extends NotificationCompat.Builder {
// - the idea is that sound and vibrate are handled outside of the scope of the notification channel
private static String createMsgNotificationChannel(Context context) {
String chBase = "ch_msg2_";
String chBase = "ch_msg3_";
String chId = chBase + "unsupported";
if(notificationChannelsSupported()) {
@@ -44,7 +44,9 @@ abstract class MessageNotifier {
static volatile long lastAudibleNotification = -1;
final NotificationState notificationState;
final Context appContext;
final Object lock;
private final SoundPool soundPool;
private final AudioManager audioManager;
private final int soundIn;
private final int soundOut;
private boolean soundInLoaded;
@@ -53,9 +55,11 @@ abstract class MessageNotifier {
MessageNotifier(Context context) {
appContext = context.getApplicationContext();
soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
audioManager = ServiceUtil.getAudioManager(appContext);
soundIn = soundPool.load(context, R.raw.sound_in, 1);
soundOut = soundPool.load(context, R.raw.sound_out, 1);
notificationState = new NotificationState();
lock = new Object();
soundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
if (status == 0) {
@@ -83,11 +87,6 @@ abstract class MessageNotifier {
public void updateNotification(int chatId, int messageId) {
boolean isVisible = visibleChatId == chatId;
ApplicationDcContext dcContext = DcHelper.getContext(appContext);
if (isVisible) {
dcContext.marknoticedChat(chatId);
}
if (!Prefs.isNotificationsEnabled(appContext) ||
Prefs.isChatMuted(appContext, chatId))
@@ -98,7 +97,7 @@ abstract class MessageNotifier {
if (isVisible) {
sendInChatNotification(chatId);
} else if (visibleChatId != NO_VISIBLE_CHAT_ID) {
//different chat
//different chat is on top
sendNotifications(chatId, messageId, false);
} else {
//app is in background or different Activity is on top
@@ -126,15 +125,20 @@ abstract class MessageNotifier {
public void removeNotifications(int[] chatIds) {
List<NotificationItem> removedItems = new LinkedList<>();
for (int id : chatIds) {
removedItems.addAll(notificationState.removeNotificationsForChat(id));
synchronized (lock) {
for (int id : chatIds) {
removedItems.addAll(notificationState.removeNotificationsForChat(id));
}
}
cancelNotifications(removedItems);
recreateSummaryNotification();
}
public void removeNotifications(int chatId) {
List<NotificationItem> removedItems = notificationState.removeNotificationsForChat(chatId);
List<NotificationItem> removedItems;
synchronized (lock) {
removedItems = notificationState.removeNotificationsForChat(chatId);
}
cancelNotifications(removedItems);
recreateSummaryNotification();
}
@@ -150,13 +154,15 @@ abstract class MessageNotifier {
NotificationManager notifications = ServiceUtil.getNotificationManager(appContext);
notifications.cancel(SUMMARY_NOTIFICATION_ID);
if (notificationState.hasMultipleChats()) {
for (Integer id : notificationState.getChats()) {
sendSingleChatNotification(appContext, new NotificationState(notificationState.getNotificationsForChat(id)), false, true);
synchronized (lock) {
if (notificationState.hasMultipleChats()) {
for (Integer id : notificationState.getChats()) {
sendSingleChatNotification(appContext, new NotificationState(notificationState.getNotificationsForChat(id)), false, true);
}
sendMultipleChatNotification(appContext, notificationState, false);
} else {
sendSingleChatNotification(appContext, notificationState, false, false);
}
sendMultipleChatNotification(appContext, notificationState, false);
} else {
sendSingleChatNotification(appContext, notificationState, false, false);
}
}
@@ -171,26 +177,30 @@ abstract class MessageNotifier {
lastAudibleNotification = System.currentTimeMillis();
}
addMessageToNotificationState(dcContext, chatId, messageId);
if (notificationState.hasMultipleChats()) {
for (int id : notificationState.getChats()) {
sendSingleChatNotification(appContext, new NotificationState(notificationState.getNotificationsForChat(id)), false, true);
synchronized (lock) {
addMessageToNotificationState(dcContext, chatId, messageId);
if (notificationState.hasMultipleChats()) {
for (int id : notificationState.getChats()) {
sendSingleChatNotification(appContext, new NotificationState(notificationState.getNotificationsForChat(id)), false, true);
}
sendMultipleChatNotification(appContext, notificationState, signal);
} else {
sendSingleChatNotification(appContext, notificationState, signal, false);
}
sendMultipleChatNotification(appContext, notificationState, signal);
} else {
sendSingleChatNotification(appContext, notificationState, signal, false);
}
}
boolean isSignalAllowed(boolean signalRequested) {
long now = System.currentTimeMillis();
return signalRequested && (
now - INITIAL_STARTUP) > STARTUP_SILENCE_DELTA &&
return signalRequested &&
(now - INITIAL_STARTUP) > STARTUP_SILENCE_DELTA &&
(now - lastAudibleNotification) > MIN_AUDIBLE_PERIOD_MILLIS;
}
private void clearNotifications() {
notificationState.reset();
synchronized (lock) {
notificationState.reset();
}
cancelActiveNotifications();
}
@@ -222,7 +232,7 @@ abstract class MessageNotifier {
return null;
}
SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context), signal);
SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context));
List<NotificationItem> notifications = notificationState.getNotifications();
NotificationItem firstItem = notifications.get(0);
Recipient recipient = firstItem.getRecipient();
@@ -253,7 +263,9 @@ abstract class MessageNotifier {
}
if (signal) {
builder.setAlarms(notificationState.getRingtone(context), notificationState.getVibrate(context));
builder.setAlarms(audioManager.getRingerMode(),
notificationState.getRingtone(context),
notificationState.getVibrate(context));
builder.setTicker(firstItem.getIndividualRecipient(),
firstItem.getText());
}
@@ -277,11 +289,10 @@ abstract class MessageNotifier {
}
}
protected AbstractNotificationBuilder createMultipleChatNotification(@NonNull Context context,
@NonNull NotificationState notificationState,
boolean signal) {
MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context), signal);
MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context));
List<NotificationItem> notifications = notificationState.getNotifications();
NotificationItem firstItem = notifications.get(0);
@@ -303,7 +314,9 @@ abstract class MessageNotifier {
}
if (signal) {
builder.setAlarms(notificationState.getRingtone(context), notificationState.getVibrate(context));
builder.setAlarms(audioManager.getRingerMode(),
notificationState.getRingtone(context),
notificationState.getVibrate(context));
builder.setTicker(firstItem.getIndividualRecipient(),
firstItem.getText());
}
@@ -320,7 +333,7 @@ abstract class MessageNotifier {
private void sendInChatNotification(int chatId) {
if (!Prefs.isInChatNotifications(appContext) ||
ServiceUtil.getAudioManager(appContext).getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
{
return;
}
@@ -371,7 +384,9 @@ abstract class MessageNotifier {
body = SpanUtil.italic(message, italicLength);
}
notificationState.addNotification(new NotificationItem(id, chatRecipient, individualRecipient, chatId, body, timestamp, slideDeck));
synchronized (lock) {
notificationState.addNotification(new NotificationItem(id, chatRecipient, individualRecipient, chatId, body, timestamp, slideDeck));
}
}
}
@@ -3,10 +3,12 @@ package org.thoughtcrime.securesms.notifications;
import android.content.Context;
import android.os.Build;
import org.thoughtcrime.securesms.util.Util;
public class MessageNotifierCompat {
public static final int NO_VISIBLE_CHAT_ID = -1;
static final int SUMMARY_NOTIFICATION_ID = 1338;
static final int SUMMARY_NOTIFICATION_ID = 1339;
static final String EXTRA_REMOTE_REPLY = "extra_remote_reply";
@@ -29,23 +31,23 @@ public class MessageNotifierCompat {
}
public static void updateNotification(int chatId, int messageId) {
instance.updateNotification(chatId, messageId);
Util.runOnAnyBackgroundThread(() -> instance.updateNotification(chatId, messageId));
}
public static void updateVisibleChat(int chatId) {
instance.updateVisibleChat(chatId);
Util.runOnAnyBackgroundThread(() -> instance.updateVisibleChat(chatId));
}
public static void onNotificationPrivacyChanged() {
instance.onNotificationPrivacyChanged();
Util.runOnAnyBackgroundThread(() -> instance.onNotificationPrivacyChanged());
}
static void removeNotifications(int[] chatIds) {
instance.removeNotifications(chatIds);
Util.runOnAnyBackgroundThread(() -> instance.removeNotifications(chatIds));
}
static void removeNotifications(int chatId) {
instance.removeNotifications(chatId);
public static void removeNotifications(int chatId) {
Util.runOnAnyBackgroundThread(() -> instance.removeNotifications(chatId));
}
@@ -18,14 +18,18 @@ class MessageNotifierPreApi23 extends MessageNotifier {
@Override
public void removeNotifications(int chatId) {
notificationState.removeNotificationsForChat(chatId);
synchronized (lock) {
notificationState.removeNotificationsForChat(chatId);
}
recreateSummaryNotification();
}
@Override
public void removeNotifications(int[] chatIds) {
for (int id : chatIds) {
notificationState.removeNotificationsForChat(id);
synchronized (lock) {
for (int id : chatIds) {
notificationState.removeNotificationsForChat(id);
}
}
recreateSummaryNotification();
}
@@ -43,10 +47,12 @@ class MessageNotifierPreApi23 extends MessageNotifier {
}
addMessageToNotificationState(dcContext, chatId, messageId);
if (notificationState.hasMultipleChats()) {
sendMultipleChatNotification(appContext, notificationState, signal);
} else {
sendSingleChatNotification(appContext, notificationState, signal, false);
synchronized (lock) {
if (notificationState.hasMultipleChats()) {
sendMultipleChatNotification(appContext, notificationState, signal);
} else {
sendSingleChatNotification(appContext, notificationState, signal, false);
}
}
}
@@ -57,11 +63,12 @@ class MessageNotifierPreApi23 extends MessageNotifier {
private void recreateSummaryNotification() {
cancelNotifications();
if (notificationState.hasMultipleChats()) {
sendMultipleChatNotification(appContext, notificationState, false);
} else {
sendSingleChatNotification(appContext, notificationState, false, false);
synchronized (lock) {
if (notificationState.hasMultipleChats()) {
sendMultipleChatNotification(appContext, notificationState, false);
} else {
sendSingleChatNotification(appContext, notificationState, false, false);
}
}
}
}
@@ -39,8 +39,8 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu
}
}
MultipleRecipientNotificationBuilder(Context context, NotificationPrivacyPreference privacy, boolean signal) {
super(context, privacy, signal);
MultipleRecipientNotificationBuilder(Context context, NotificationPrivacyPreference privacy) {
super(context, privacy);
setColor(context.getResources().getColor(R.color.delta_primary));
setSmallIcon(R.drawable.icon_notification);
@@ -46,9 +46,9 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil
private CharSequence contentTitle;
private CharSequence contentText;
SingleRecipientNotificationBuilder(@NonNull Context context, @NonNull NotificationPrivacyPreference privacy, boolean signal)
SingleRecipientNotificationBuilder(@NonNull Context context, @NonNull NotificationPrivacyPreference privacy)
{
super(context, privacy, signal);
super(context, privacy);
setSmallIcon(R.drawable.icon_notification);
setColor(context.getResources().getColor(R.color.delta_primary));
@@ -324,6 +324,14 @@ public class Util {
AsyncTask.THREAD_POOL_EXECUTOR.execute(runnable);
}
public static void runOnAnyBackgroundThread(final @NonNull Runnable runnable) {
if (Util.isMainThread()) {
Util.runOnBackground(runnable);
} else {
runnable.run();
}
}
public static void runOnBackgroundDelayed(final @NonNull Runnable runnable, long delayMillis) {
handler.postDelayed(() -> {
AsyncTask.THREAD_POOL_EXECUTOR.execute(runnable);