From f6f42991e8e52c577928803b9defccffe334f945 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Thu, 19 Feb 2026 02:08:24 +0100 Subject: [PATCH] avoid deprecated `NotificationsPreferenceFragment.onActivityResult()` --- .../NotificationsPreferenceFragment.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java b/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java index e6e282cd7..256ebb010 100644 --- a/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java +++ b/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java @@ -16,6 +16,8 @@ import android.provider.Settings; import android.text.TextUtils; import android.util.Log; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationManagerCompat; @@ -34,17 +36,34 @@ import org.thoughtcrime.securesms.util.Prefs; public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragment implements Preference.OnPreferenceChangeListener { private static final String TAG = NotificationsPreferenceFragment.class.getSimpleName(); - private static final int REQUEST_CODE_NOTIFICATION_SELECTED = 1; private CheckBoxPreference ignoreBattery; private CheckBoxPreference notificationsEnabled; private CheckBoxPreference mentionNotifEnabled; private CheckBoxPreference reliableService; + private ActivityResultLauncher ringtonePickerLauncher; @Override public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); + ringtonePickerLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == RESULT_OK && result.getData() != null) { + Uri uri = result.getData().getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); + + if (Settings.System.DEFAULT_NOTIFICATION_URI.equals(uri)) { + Prefs.removeNotificationRingtone(getContext()); + } else { + Prefs.setNotificationRingtone(getContext(), uri != null ? uri : Uri.EMPTY); + } + + initializeRingtoneSummary(findPreference(Prefs.RINGTONE_PREF)); + } + } + ); + this.findPreference(Prefs.LED_COLOR_PREF) .setOnPreferenceChangeListener(new ListSummaryListener()); this.findPreference(Prefs.RINGTONE_PREF) @@ -65,8 +84,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, current); - - startActivityForResult(intent, REQUEST_CODE_NOTIFICATION_SELECTED); + ringtonePickerLauncher.launch(intent); return true; }); @@ -137,22 +155,6 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme reliableService.setOnPreferenceChangeListener(this); } - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == REQUEST_CODE_NOTIFICATION_SELECTED && resultCode == RESULT_OK && data != null) { - Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); - - if (Settings.System.DEFAULT_NOTIFICATION_URI.equals(uri)) { - Prefs.removeNotificationRingtone(getContext()); - } else { - Prefs.setNotificationRingtone(getContext(), uri != null ? uri : Uri.EMPTY); - } - - initializeRingtoneSummary(findPreference(Prefs.RINGTONE_PREF)); - } - } - @Override public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) { Context context = getContext();