From 4acedd2a4b962e2fa84bb8848c88b84350a762c7 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Tue, 19 Dec 2017 14:42:00 -0800 Subject: [PATCH] Fix notification ringtone problems Default ringtone was often showing up as a call tone Silent wasn't working correctly globally --- .../RecipientPreferenceActivity.java | 4 +++- .../AbstractNotificationBuilder.java | 8 +++---- .../notifications/MessageNotifier.java | 14 +---------- .../NotificationsPreferenceFragment.java | 23 +++++++++++-------- .../securesms/recipients/Recipient.java | 2 +- .../securesms/util/TextSecurePreferences.java | 14 ++++++++--- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java b/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java index c6e55983cf..9d33817637 100644 --- a/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java +++ b/src/org/thoughtcrime/securesms/RecipientPreferenceActivity.java @@ -58,6 +58,7 @@ import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme; import org.thoughtcrime.securesms.util.DynamicTheme; import org.thoughtcrime.securesms.util.IdentityUtil; +import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.ViewUtil; import org.thoughtcrime.securesms.util.concurrent.ListenableFuture; @@ -372,7 +373,7 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi public boolean onPreferenceChange(Preference preference, Object newValue) { Uri value = (Uri)newValue; - if (Settings.System.DEFAULT_NOTIFICATION_URI.equals(value)) { + if (TextSecurePreferences.getNotificationRingtone(getContext()).equals(value)) { value = null; } else if (value == null) { value = Uri.EMPTY; @@ -403,6 +404,7 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, TextSecurePreferences.getNotificationRingtone(getContext())); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri); diff --git a/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java b/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java index 4d2bb4feda..8b75342dab 100644 --- a/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java +++ b/src/org/thoughtcrime/securesms/notifications/AbstractNotificationBuilder.java @@ -45,11 +45,11 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui } public void setAlarms(@Nullable Uri ringtone, RecipientDatabase.VibrateState vibrate) { - String defaultRingtoneName = TextSecurePreferences.getNotificationRingtone(context); - boolean defaultVibrate = TextSecurePreferences.isNotificationVibrateEnabled(context); + Uri defaultRingtone = TextSecurePreferences.getNotificationRingtone(context); + boolean defaultVibrate = TextSecurePreferences.isNotificationVibrateEnabled(context); - if (ringtone == null && !TextUtils.isEmpty(defaultRingtoneName)) setSound(Uri.parse(defaultRingtoneName)); - else if (ringtone != null && !ringtone.toString().isEmpty()) setSound(ringtone); + if (ringtone == null && !TextUtils.isEmpty(defaultRingtone.toString())) setSound(defaultRingtone); + else if (ringtone != null && !ringtone.toString().isEmpty()) setSound(ringtone); if (vibrate == RecipientDatabase.VibrateState.ENABLED || (vibrate == RecipientDatabase.VibrateState.DEFAULT && defaultVibrate)) diff --git a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java index ffa639fd85..4f05716dc5 100644 --- a/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java +++ b/src/org/thoughtcrime/securesms/notifications/MessageNotifier.java @@ -384,19 +384,7 @@ public class MessageNotifier { Uri uri = recipient != null ? recipient.getRingtone() : null; if (uri == null) { - String ringtone = TextSecurePreferences.getNotificationRingtone(context); - - if (ringtone == null) { - Log.w(TAG, "ringtone preference was null."); - return; - } - - uri = Uri.parse(ringtone); - - if (uri == null) { - Log.w(TAG, "couldn't parse ringtone uri " + ringtone); - return; - } + uri = TextSecurePreferences.getNotificationRingtone(context); } if (uri.toString().isEmpty()) { diff --git a/src/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java b/src/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java index 7b374b59f4..97b81b2b61 100644 --- a/src/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java +++ b/src/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java @@ -3,16 +3,15 @@ package org.thoughtcrime.securesms.preferences; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.provider.Settings; import android.support.annotation.Nullable; import android.support.v7.preference.ListPreference; import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceManager; import android.text.TextUtils; import org.thoughtcrime.securesms.ApplicationPreferencesActivity; @@ -50,13 +49,14 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme this.findPreference(TextSecurePreferences.RINGTONE_PREF) .setOnPreferenceClickListener(preference -> { - String current = TextSecurePreferences.getNotificationRingtone(getContext()); + Uri current = TextSecurePreferences.getNotificationRingtone(getContext()); Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); - intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, current == null ? null : Uri.parse(current)); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, current); startActivityForResult(intent, 1); @@ -87,7 +87,12 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme if (requestCode == 1 && resultCode == RESULT_OK && data != null) { Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); - TextSecurePreferences.setNotificationRingtone(getContext(), uri != null ? uri.toString() : null); + if (Settings.System.DEFAULT_NOTIFICATION_URI.equals(uri)) { + TextSecurePreferences.removeNotificationRingtone(getContext()); + } else { + TextSecurePreferences.setNotificationRingtone(getContext(), uri != null ? uri.toString() : Uri.EMPTY.toString()); + } + initializeRingtoneSummary(findPreference(TextSecurePreferences.RINGTONE_PREF)); } } @@ -97,7 +102,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme public boolean onPreferenceChange(Preference preference, Object newValue) { Uri value = (Uri) newValue; - if (value == null) { + if (value == null || TextUtils.isEmpty(value.toString())) { preference.setSummary(R.string.preferences__silent); } else { Ringtone tone = RingtoneManager.getRingtone(getActivity(), value); @@ -112,10 +117,8 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme } private void initializeRingtoneSummary(Preference pref) { - RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener(); - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); - String encodedUri = sharedPreferences.getString(pref.getKey(), null); - Uri uri = !TextUtils.isEmpty(encodedUri) ? Uri.parse(encodedUri) : null; + RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener(); + Uri uri = TextSecurePreferences.getNotificationRingtone(getContext()); listener.onPreferenceChange(pref, uri); } diff --git a/src/org/thoughtcrime/securesms/recipients/Recipient.java b/src/org/thoughtcrime/securesms/recipients/Recipient.java index f0018b697c..f0a0786c7d 100644 --- a/src/org/thoughtcrime/securesms/recipients/Recipient.java +++ b/src/org/thoughtcrime/securesms/recipients/Recipient.java @@ -456,7 +456,7 @@ public class Recipient implements RecipientModifiedListener { if (ringtone != null && ringtone.getScheme() != null && ringtone.getScheme().startsWith("file")) { return null; } - + return ringtone; } diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index 831d0db040..3910ee71f1 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.util; import android.content.Context; import android.content.SharedPreferences; import android.hardware.Camera.CameraInfo; +import android.net.Uri; import android.os.Build; import android.preference.PreferenceManager; import android.provider.Settings; @@ -10,7 +11,6 @@ import android.support.annotation.ArrayRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; -import android.support.v4.content.SharedPreferencesCompat; import android.util.Log; import org.thoughtcrime.securesms.R; @@ -634,14 +634,18 @@ public class TextSecurePreferences { return getBooleanPreference(context, NOTIFICATION_PREF, true); } - public static String getNotificationRingtone(Context context) { + public static @NonNull Uri getNotificationRingtone(Context context) { String result = getStringPreference(context, RINGTONE_PREF, Settings.System.DEFAULT_NOTIFICATION_URI.toString()); if (result != null && result.startsWith("file:")) { result = Settings.System.DEFAULT_NOTIFICATION_URI.toString(); } - return result; + return Uri.parse(result); + } + + public static void removeNotificationRingtone(Context context) { + removePreference(context, RINGTONE_PREF); } public static void setNotificationRingtone(Context context, String ringtone) { @@ -734,6 +738,10 @@ public class TextSecurePreferences { PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(key, value).apply(); } + private static void removePreference(Context context, String key) { + PreferenceManager.getDefaultSharedPreferences(context).edit().remove(key).apply(); + } + private static Set getStringSetPreference(Context context, String key, Set defaultValues) { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (prefs.contains(key)) {