From f1efe2b589d071a5b60e86d16c964896f01cdd20 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Wed, 22 Aug 2018 11:58:41 -0700 Subject: [PATCH] Use correct in-thread message tone on O+. We manually play the ringtone when in-thread notifications are enabled, but we weren't using the sound specified by the channel in the system settings. This fixes that problem by reading the NotificationChannel setting. --- .../RecipientPreferenceActivity.java | 4 ++-- .../notifications/MessageNotifier.java | 6 ++--- .../notifications/NotificationChannels.java | 22 ++++++++++++++++++- .../notifications/NotificationState.java | 4 ++-- .../securesms/recipients/Recipient.java | 6 ++++- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java b/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java index 767f4e9aa9..de1b227088 100644 --- a/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java +++ b/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java @@ -356,7 +356,7 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi mutePreference.setChecked(recipient.isMuted()); - ringtoneMessagePreference.setSummary(getRingtoneSummary(getContext(), recipient.getMessageRingtone())); + ringtoneMessagePreference.setSummary(getRingtoneSummary(getContext(), recipient.getMessageRingtone(getContext()))); ringtoneCallPreference.setSummary(getRingtoneSummary(getContext(), recipient.getCallRingtone())); Pair vibrateMessageSummary = getVibrateSummary(getContext(), recipient.getMessageVibrate()); @@ -496,7 +496,7 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi current = recipient.getCallRingtone(); defaultUri = TextSecurePreferences.getCallNotificationRingtone(getContext()); } else { - current = recipient.getMessageRingtone(); + current = recipient.getMessageRingtone(getContext()); defaultUri = TextSecurePreferences.getNotificationRingtone(getContext()); } diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index a8e4df62e0..8cd445c8c4 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -325,7 +325,7 @@ public class MessageNotifier { } if (signal) { - builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate()); + builder.setAlarms(notificationState.getRingtone(context), notificationState.getVibrate()); builder.setTicker(notifications.get(0).getIndividualRecipient(), notifications.get(0).getText()); } @@ -363,7 +363,7 @@ public class MessageNotifier { } if (signal) { - builder.setAlarms(notificationState.getRingtone(), notificationState.getVibrate()); + builder.setAlarms(notificationState.getRingtone(context), notificationState.getVibrate()); builder.setTicker(notifications.get(0).getIndividualRecipient(), notifications.get(0).getText()); } @@ -378,7 +378,7 @@ public class MessageNotifier { return; } - Uri uri = recipient != null ? recipient.resolve().getMessageRingtone() : null; + Uri uri = recipient != null ? recipient.resolve().getMessageRingtone(context) : null; if (uri == null) { uri = TextSecurePreferences.getNotificationRingtone(context); diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java b/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java index 359bab5858..1dd6b34afd 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationChannels.java @@ -99,7 +99,7 @@ public class NotificationChannels { boolean vibrationEnabled = vibrateState == VibrateState.DEFAULT ? TextSecurePreferences.isNotificationVibrateEnabled(context) : vibrateState == VibrateState.ENABLED; String displayName = getChannelDisplayNameFor(recipient.getName(), recipient.getProfileName(), recipient.getAddress()); - return createChannelFor(context, recipient.getAddress(), displayName, recipient.getMessageRingtone(), vibrationEnabled); + return createChannelFor(context, recipient.getAddress(), displayName, recipient.getMessageRingtone(context), vibrationEnabled); } /** @@ -219,6 +219,26 @@ public class NotificationChannels { notificationManager.createNotificationChannel(channel); } + public static @Nullable Uri getMessageRingtone(@NonNull Context context, @NonNull Recipient recipient) { + if (!supported()) { + return null; + } + + NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + if (notificationManager == null) { + Log.w(TAG, "Unable to retrieve notification manager. Cannot update channel name."); + return null; + } + + NotificationChannel channel = notificationManager.getNotificationChannel(recipient.getNotificationChannel(context)); + if (channel == null) { + Log.w(TAG, "Recipient had an invalid channel. Returning null."); + return null; + } + + return channel.getSound(); + } + @TargetApi(26) private static void onCreate(@NonNull Context context, @NonNull NotificationManager notificationManager) { NotificationChannelGroup messagesGroup = new NotificationChannelGroup(CATEGORY_MESSAGES, context.getResources().getString(R.string.NotificationChannel_group_messages)); diff --git a/src/org/thoughtcrime/securesms/notifications/NotificationState.java b/src/org/thoughtcrime/securesms/notifications/NotificationState.java index eb5e828242..3e3e282b37 100644 --- a/src/org/thoughtcrime/securesms/notifications/NotificationState.java +++ b/src/org/thoughtcrime/securesms/notifications/NotificationState.java @@ -45,12 +45,12 @@ public class NotificationState { notificationCount++; } - public @Nullable Uri getRingtone() { + public @Nullable Uri getRingtone(@NonNull Context context) { if (!notifications.isEmpty()) { Recipient recipient = notifications.getFirst().getRecipient(); if (recipient != null) { - return recipient.resolve().getMessageRingtone(); + return recipient.resolve().getMessageRingtone(context); } } diff --git a/src/org/thoughtcrime/securesms/recipients/Recipient.java b/src/org/thoughtcrime/securesms/recipients/Recipient.java index e7a81ec816..a44f9e2598 100644 --- a/src/org/thoughtcrime/securesms/recipients/Recipient.java +++ b/src/org/thoughtcrime/securesms/recipients/Recipient.java @@ -464,11 +464,15 @@ public class Recipient implements RecipientModifiedListener { if (notify) notifyListeners(); } - public synchronized @Nullable Uri getMessageRingtone() { + public synchronized @Nullable Uri getMessageRingtone(@NonNull Context context) { if (messageRingtone != null && messageRingtone.getScheme() != null && messageRingtone.getScheme().startsWith("file")) { return null; } + if (NotificationChannels.supported()) { + return NotificationChannels.getMessageRingtone(context, this); + } + return messageRingtone; }