make notification tone picker work

This commit is contained in:
Daniel Gultsch 2024-04-07 20:35:01 +02:00
parent 85771fc26c
commit b30a88e7a5
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 40 additions and 11 deletions

View file

@ -61,6 +61,21 @@ public class AppSettings {
sharedPreferences.edit().putString(RINGTONE, uri == null ? null : uri.toString()).apply();
}
public Uri getNotificationTone() {
final SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
final String incomingCallRingtone =
sharedPreferences.getString(
NOTIFICATION_RINGTONE, context.getString(R.string.notification_ringtone));
return Strings.isNullOrEmpty(incomingCallRingtone) ? null : Uri.parse(incomingCallRingtone);
}
public void setNotificationTone(final Uri uri) {
final SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
sharedPreferences.edit().putString(NOTIFICATION_RINGTONE, uri == null ? null : uri.toString()).apply();
}
public boolean isBTBVEnabled() {
return getBooleanPreference(BTBV, R.bool.btbv);
}

View file

@ -1,6 +1,5 @@
package eu.siacs.conversations.ui.fragment.settings;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
@ -9,9 +8,7 @@ import android.util.Log;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import eu.siacs.conversations.AppSettings;
import eu.siacs.conversations.Config;
@ -21,6 +18,18 @@ import eu.siacs.conversations.utils.Compatibility;
public class NotificationsSettingsFragment extends XmppPreferenceFragment {
private final ActivityResultLauncher<Uri> pickNotificationToneLauncher =
registerForActivityResult(
new PickRingtone(RingtoneManager.TYPE_NOTIFICATION),
result -> {
if (result == null) {
// do nothing. user aborted
return;
}
final Uri uri = PickRingtone.noneToNull(result);
appSettings().setNotificationTone(uri);
Log.i(Config.LOGTAG, "User set notification tone to " + uri);
});
private final ActivityResultLauncher<Uri> pickRingtoneLauncher =
registerForActivityResult(
new PickRingtone(RingtoneManager.TYPE_RINGTONE),
@ -30,7 +39,7 @@ public class NotificationsSettingsFragment extends XmppPreferenceFragment {
return;
}
final Uri uri = PickRingtone.noneToNull(result);
setRingtone(uri);
appSettings().setRingtone(uri);
Log.i(Config.LOGTAG, "User set ringtone to " + uri);
});
@ -79,22 +88,30 @@ public class NotificationsSettingsFragment extends XmppPreferenceFragment {
@Override
public boolean onPreferenceTreeClick(final Preference preference) {
if (AppSettings.RINGTONE.equals(preference.getKey())) {
final var key = preference.getKey();
if (AppSettings.RINGTONE.equals(key)) {
pickRingtone();
return true;
}
if (AppSettings.NOTIFICATION_RINGTONE.equals(key)) {
pickNotificationTone();
return true;
}
return super.onPreferenceTreeClick(preference);
}
private void pickNotificationTone() {
final Uri uri = appSettings().getNotificationTone();
Log.i(Config.LOGTAG, "current notification tone: " + uri);
this.pickNotificationToneLauncher.launch(uri);
}
private void pickRingtone() {
final Uri uri = appSettings().getRingtone();
Log.i(Config.LOGTAG, "current ringtone: " + uri);
this.pickRingtoneLauncher.launch(uri);
}
private void setRingtone(final Uri uri) {
appSettings().setRingtone(uri);
}
private AppSettings appSettings() {
return new AppSettings(requireContext());

View file

@ -1025,9 +1025,6 @@
<string name="delete_and_close">Delete &amp; Archive chat</string>
<string name="start_chat">Start chat</string>
<string name="no_certificate_selected">No client certificate selected!</string>
<string name="pref_title_interface">Interface</string>
<string name="pref_summary_appearance">Theme, Colors, Screenshots, Input</string>
<string name="pref_title_security">Security</string>