Fix notification ringtone problems

Default ringtone was often showing up as a call tone

Silent wasn't working correctly globally
This commit is contained in:
Moxie Marlinspike 2017-12-19 14:42:00 -08:00
parent d782d3006b
commit 4acedd2a4b
6 changed files with 33 additions and 32 deletions

View File

@ -58,6 +58,7 @@ import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme; import org.thoughtcrime.securesms.util.DynamicNoActionBarTheme;
import org.thoughtcrime.securesms.util.DynamicTheme; import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.IdentityUtil; import org.thoughtcrime.securesms.util.IdentityUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.ViewUtil; import org.thoughtcrime.securesms.util.ViewUtil;
import org.thoughtcrime.securesms.util.concurrent.ListenableFuture; import org.thoughtcrime.securesms.util.concurrent.ListenableFuture;
@ -372,7 +373,7 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
Uri value = (Uri)newValue; Uri value = (Uri)newValue;
if (Settings.System.DEFAULT_NOTIFICATION_URI.equals(value)) { if (TextSecurePreferences.getNotificationRingtone(getContext()).equals(value)) {
value = null; value = null;
} else if (value == null) { } else if (value == null) {
value = Uri.EMPTY; value = Uri.EMPTY;
@ -403,6 +404,7 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, 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_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri);

View File

@ -45,11 +45,11 @@ public abstract class AbstractNotificationBuilder extends NotificationCompat.Bui
} }
public void setAlarms(@Nullable Uri ringtone, RecipientDatabase.VibrateState vibrate) { public void setAlarms(@Nullable Uri ringtone, RecipientDatabase.VibrateState vibrate) {
String defaultRingtoneName = TextSecurePreferences.getNotificationRingtone(context); Uri defaultRingtone = TextSecurePreferences.getNotificationRingtone(context);
boolean defaultVibrate = TextSecurePreferences.isNotificationVibrateEnabled(context); boolean defaultVibrate = TextSecurePreferences.isNotificationVibrateEnabled(context);
if (ringtone == null && !TextUtils.isEmpty(defaultRingtoneName)) setSound(Uri.parse(defaultRingtoneName)); if (ringtone == null && !TextUtils.isEmpty(defaultRingtone.toString())) setSound(defaultRingtone);
else if (ringtone != null && !ringtone.toString().isEmpty()) setSound(ringtone); else if (ringtone != null && !ringtone.toString().isEmpty()) setSound(ringtone);
if (vibrate == RecipientDatabase.VibrateState.ENABLED || if (vibrate == RecipientDatabase.VibrateState.ENABLED ||
(vibrate == RecipientDatabase.VibrateState.DEFAULT && defaultVibrate)) (vibrate == RecipientDatabase.VibrateState.DEFAULT && defaultVibrate))

View File

@ -384,19 +384,7 @@ public class MessageNotifier {
Uri uri = recipient != null ? recipient.getRingtone() : null; Uri uri = recipient != null ? recipient.getRingtone() : null;
if (uri == null) { if (uri == null) {
String ringtone = TextSecurePreferences.getNotificationRingtone(context); uri = 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;
}
} }
if (uri.toString().isEmpty()) { if (uri.toString().isEmpty()) {

View File

@ -3,16 +3,15 @@ package org.thoughtcrime.securesms.preferences;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
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.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.preference.ListPreference; import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity; import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
@ -50,13 +49,14 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
this.findPreference(TextSecurePreferences.RINGTONE_PREF) this.findPreference(TextSecurePreferences.RINGTONE_PREF)
.setOnPreferenceClickListener(preference -> { .setOnPreferenceClickListener(preference -> {
String current = TextSecurePreferences.getNotificationRingtone(getContext()); Uri current = TextSecurePreferences.getNotificationRingtone(getContext());
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); 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); startActivityForResult(intent, 1);
@ -87,7 +87,12 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
if (requestCode == 1 && resultCode == RESULT_OK && data != null) { if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); 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)); initializeRingtoneSummary(findPreference(TextSecurePreferences.RINGTONE_PREF));
} }
} }
@ -97,7 +102,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
Uri value = (Uri) newValue; Uri value = (Uri) newValue;
if (value == null) { if (value == null || TextUtils.isEmpty(value.toString())) {
preference.setSummary(R.string.preferences__silent); preference.setSummary(R.string.preferences__silent);
} else { } else {
Ringtone tone = RingtoneManager.getRingtone(getActivity(), value); Ringtone tone = RingtoneManager.getRingtone(getActivity(), value);
@ -112,10 +117,8 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
} }
private void initializeRingtoneSummary(Preference pref) { private void initializeRingtoneSummary(Preference pref) {
RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener(); RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); Uri uri = TextSecurePreferences.getNotificationRingtone(getContext());
String encodedUri = sharedPreferences.getString(pref.getKey(), null);
Uri uri = !TextUtils.isEmpty(encodedUri) ? Uri.parse(encodedUri) : null;
listener.onPreferenceChange(pref, uri); listener.onPreferenceChange(pref, uri);
} }

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.util;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.hardware.Camera.CameraInfo; import android.hardware.Camera.CameraInfo;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.Settings; import android.provider.Settings;
@ -10,7 +11,6 @@ import android.support.annotation.ArrayRes;
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;
import android.support.v4.content.SharedPreferencesCompat;
import android.util.Log; import android.util.Log;
import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.R;
@ -634,14 +634,18 @@ public class TextSecurePreferences {
return getBooleanPreference(context, NOTIFICATION_PREF, true); 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()); String result = getStringPreference(context, RINGTONE_PREF, Settings.System.DEFAULT_NOTIFICATION_URI.toString());
if (result != null && result.startsWith("file:")) { if (result != null && result.startsWith("file:")) {
result = Settings.System.DEFAULT_NOTIFICATION_URI.toString(); 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) { public static void setNotificationRingtone(Context context, String ringtone) {
@ -734,6 +738,10 @@ public class TextSecurePreferences {
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(key, value).apply(); 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<String> getStringSetPreference(Context context, String key, Set<String> defaultValues) { private static Set<String> getStringSetPreference(Context context, String key, Set<String> defaultValues) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.contains(key)) { if (prefs.contains(key)) {