From 7f6823a650f3da5c4ea02e41ffa027b57fd74e82 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 28 May 2019 15:40:27 +0200 Subject: [PATCH] silence notification channels if needed --- .../AbstractNotificationBuilder.java | 24 ++++++++++++------- .../notifications/MessageNotifier.java | 6 ++--- .../MultipleRecipientNotificationBuilder.java | 7 ++---- .../SingleRecipientNotificationBuilder.java | 7 ++---- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java index 8a9436165..ca8b1ecd5 100644 --- a/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java @@ -32,8 +32,8 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui protected Context context; protected NotificationPrivacyPreference privacy; - AbstractNotificationBuilder(Context context, NotificationPrivacyPreference privacy) { - super(context, createMsgNotificationChannel(context)); + AbstractNotificationBuilder(Context context, NotificationPrivacyPreference privacy, boolean signal) { + super(context, createMsgNotificationChannel(context, signal)); this.context = context; this.privacy = privacy; @@ -109,7 +109,7 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui // - the idea is that sound, led, vibrate is edited by the user // via the ACTION_CHANNEL_NOTIFICATION_SETTINGS intent that takes the channelId - static String createMsgNotificationChannel(Context context) { + private static String createMsgNotificationChannel(Context context, boolean signal) { String chBase = "ch_msg2_"; String chId = chBase + "unsupported"; @@ -128,6 +128,7 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui md.update(ledColor.getBytes()); md.update(defaultVibrate ? (byte) 1 : (byte) 0); md.update(ringtone.toString().getBytes()); + md.update(signal ? (byte) 1 : (byte) 0); hash = String.format("%X", new BigInteger(1, md.digest())).substring(0, 16); // get channel name @@ -173,13 +174,18 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui channel.enableLights(false); } - channel.enableVibration(defaultVibrate); + if (signal) { + channel.enableVibration(defaultVibrate); - if (!TextUtils.isEmpty(ringtone.toString())) { - channel.setSound(ringtone, - new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) - .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) - .build()); + if (!TextUtils.isEmpty(ringtone.toString())) { + channel.setSound(ringtone, + new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN) + .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT) + .build()); + } + } else { + channel.setSound(null, null); + channel.enableVibration(false); } notificationManager.createNotificationChannel(channel); diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index 6e31aba42..4065f2b95 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -209,7 +209,7 @@ class MessageNotifier implements IMessageNotifier { void sendNotifications(int chatId, int messageId, boolean signal) { ApplicationDcContext dcContext = DcHelper.getContext(appContext); if (signal = isSignalAllowed(signal)) { - lastAudibleNotification = System.currentTimeMillis();; + lastAudibleNotification = System.currentTimeMillis(); } addMessageToNotificationState(dcContext, chatId, messageId); @@ -245,7 +245,7 @@ class MessageNotifier implements IMessageNotifier { return; } - SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context)); + SingleRecipientNotificationBuilder builder = new SingleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context), signal); List notifications = notificationState.getNotifications(); NotificationItem firstItem = notifications.get(0); Recipient recipient = firstItem.getRecipient(); @@ -291,7 +291,7 @@ class MessageNotifier implements IMessageNotifier { @NonNull NotificationState notificationState, boolean signal) { - MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context)); + MultipleRecipientNotificationBuilder builder = new MultipleRecipientNotificationBuilder(context, Prefs.getNotificationPrivacy(context), signal); List notifications = notificationState.getNotifications(); NotificationItem firstItem = notifications.get(0); diff --git a/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java index abf007a06..17d4e1fe8 100644 --- a/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/MultipleRecipientNotificationBuilder.java @@ -39,14 +39,11 @@ public class MultipleRecipientNotificationBuilder extends AbstractNotificationBu } } - MultipleRecipientNotificationBuilder(Context context, NotificationPrivacyPreference privacy) { - super(context, privacy); + MultipleRecipientNotificationBuilder(Context context, NotificationPrivacyPreference privacy, boolean signal) { + super(context, privacy, signal); setColor(context.getResources().getColor(R.color.delta_primary)); setSmallIcon(R.drawable.icon_notification); - if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) { - setChannelId(createMsgNotificationChannel(context)); - } setContentTitle(context.getString(R.string.app_name)); setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, ConversationListActivity.class), 0)); setCategory(NotificationCompat.CATEGORY_MESSAGE); diff --git a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java index 203575a75..838e1c8c6 100644 --- a/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/SingleRecipientNotificationBuilder.java @@ -46,14 +46,11 @@ public class SingleRecipientNotificationBuilder extends AbstractNotificationBuil private CharSequence contentTitle; private CharSequence contentText; - SingleRecipientNotificationBuilder(@NonNull Context context, @NonNull NotificationPrivacyPreference privacy) + SingleRecipientNotificationBuilder(@NonNull Context context, @NonNull NotificationPrivacyPreference privacy, boolean signal) { - super(context, privacy); + super(context, privacy, signal); setSmallIcon(R.drawable.icon_notification); - if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) { - setChannelId(createMsgNotificationChannel(context)); - } setColor(context.getResources().getColor(R.color.delta_primary)); setPriority(Prefs.getNotificationPriority(context)); setCategory(NotificationCompat.CATEGORY_MESSAGE);