NotificationsSettingsFragment.java

  1package eu.siacs.conversations.ui.fragment.settings;
  2
  3import android.content.SharedPreferences;
  4import android.media.RingtoneManager;
  5import android.net.Uri;
  6import android.os.Bundle;
  7import android.util.Log;
  8
  9import androidx.activity.result.ActivityResultLauncher;
 10import androidx.annotation.NonNull;
 11import androidx.annotation.Nullable;
 12import androidx.preference.ListPreference;
 13import androidx.preference.Preference;
 14import androidx.preference.PreferenceFragmentCompat;
 15
 16import eu.siacs.conversations.AppSettings;
 17import eu.siacs.conversations.Config;
 18import eu.siacs.conversations.R;
 19import eu.siacs.conversations.ui.activity.result.PickRingtone;
 20import eu.siacs.conversations.utils.Compatibility;
 21
 22public class NotificationsSettingsFragment extends XmppPreferenceFragment {
 23
 24    private final ActivityResultLauncher<Uri> pickRingtoneLauncher =
 25            registerForActivityResult(
 26                    new PickRingtone(RingtoneManager.TYPE_RINGTONE),
 27                    result -> {
 28                        if (result == null) {
 29                            // do nothing. user aborted
 30                            return;
 31                        }
 32                        final Uri uri = PickRingtone.noneToNull(result);
 33                        setRingtone(uri);
 34                        Log.i(Config.LOGTAG, "User set ringtone to " + uri);
 35                    });
 36
 37    @Override
 38    public void onCreatePreferences(
 39            @Nullable final Bundle savedInstanceState, final @Nullable String rootKey) {
 40        setPreferencesFromResource(R.xml.preferences_notifications, rootKey);
 41        final var messageNotificationSettings = findPreference("message_notification_settings");
 42        final var notificationRingtone = findPreference(AppSettings.NOTIFICATION_RINGTONE);
 43        final var notificationHeadsUp = findPreference(AppSettings.NOTIFICATION_HEADS_UP);
 44        final var notificationVibrate = findPreference(AppSettings.NOTIFICATION_VIBRATE);
 45        final var notificationLed = findPreference(AppSettings.NOTIFICATION_LED);
 46        final var foregroundService = findPreference(AppSettings.KEEP_FOREGROUND_SERVICE);
 47        if (messageNotificationSettings == null
 48                || notificationRingtone == null
 49                || notificationHeadsUp == null
 50                || notificationVibrate == null
 51                || notificationLed == null
 52                || foregroundService == null) {
 53            throw new IllegalStateException("The preference resource file is missing preferences");
 54        }
 55        if (Compatibility.runsTwentySix()) {
 56            notificationRingtone.setVisible(false);
 57            notificationHeadsUp.setVisible(false);
 58            notificationVibrate.setVisible(false);
 59            notificationLed.setVisible(false);
 60            foregroundService.setVisible(false);
 61        } else {
 62            messageNotificationSettings.setVisible(false);
 63        }
 64    }
 65
 66    @Override
 67    protected void onSharedPreferenceChanged(@NonNull String key) {
 68        super.onSharedPreferenceChanged(key);
 69        if (key.equals(AppSettings.KEEP_FOREGROUND_SERVICE)) {
 70            requireService().toggleForegroundService();
 71        }
 72    }
 73
 74    @Override
 75    public void onStart() {
 76        super.onStart();
 77        requireActivity().setTitle(R.string.notifications);
 78    }
 79
 80    @Override
 81    public boolean onPreferenceTreeClick(final Preference preference) {
 82        if (AppSettings.RINGTONE.equals(preference.getKey())) {
 83            pickRingtone();
 84            return true;
 85        }
 86        return super.onPreferenceTreeClick(preference);
 87    }
 88
 89    private void pickRingtone() {
 90        final Uri uri = appSettings().getRingtone();
 91        Log.i(Config.LOGTAG, "current ringtone: " + uri);
 92        this.pickRingtoneLauncher.launch(uri);
 93    }
 94
 95    private void setRingtone(final Uri uri) {
 96        appSettings().setRingtone(uri);
 97    }
 98
 99    private AppSettings appSettings() {
100        return new AppSettings(requireContext());
101    }
102}