fix opening ringtone chooser when channel sound was set to null

This commit is contained in:
Daniel Gultsch 2024-05-03 09:41:29 +02:00
parent 5853f57f0a
commit 9bf1e51ac4
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
2 changed files with 8 additions and 4 deletions

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import androidx.activity.result.contract.ActivityResultContract; import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -26,7 +27,7 @@ public class PickRingtone extends ActivityResultContract<Uri, Uri> {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, ringToneType); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, ringToneType);
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);
if (existing != null) { if (noneToNull(existing) != null) {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, existing); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, existing);
} }
return intent; return intent;
@ -37,11 +38,14 @@ public class PickRingtone extends ActivityResultContract<Uri, Uri> {
if (resultCode != Activity.RESULT_OK || data == null) { if (resultCode != Activity.RESULT_OK || data == null) {
return null; return null;
} }
final Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); return nullToNone(data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI));
return pickedUri == null ? NONE : pickedUri;
} }
public static Uri noneToNull(final Uri uri) { public static Uri noneToNull(final Uri uri) {
return uri == null || NONE.equals(uri) ? null : uri; return uri == null || NONE.equals(uri) ? null : uri;
} }
public static @NonNull Uri nullToNone(final Uri uri) {
return uri == null ? NONE : uri;
}
} }

View file

@ -119,7 +119,7 @@ public class NotificationsSettingsFragment extends XmppPreferenceFragment {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelRingtone = channelRingtone =
NotificationService.getCurrentIncomingCallChannel(requireContext()) NotificationService.getCurrentIncomingCallChannel(requireContext())
.transform(NotificationChannel::getSound); .transform(channel -> PickRingtone.nullToNone(channel.getSound()));
} else { } else {
channelRingtone = Optional.absent(); channelRingtone = Optional.absent();
} }