From 1a924d3efde134cf0e0e72781ee2a3d536d1da1c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 1 Mar 2023 09:05:56 +0100 Subject: [PATCH] introduce AppSettings for easier access to preferences --- .../im/conversations/android/AppSettings.java | 45 +++++++++++++++++++ .../notification/AbstractNotification.java | 3 ++ .../notification/RtpSessionNotification.java | 14 +----- .../NotificationsSettingsFragment.java | 30 ++++--------- 4 files changed, 58 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/im/conversations/android/AppSettings.java diff --git a/app/src/main/java/im/conversations/android/AppSettings.java b/app/src/main/java/im/conversations/android/AppSettings.java new file mode 100644 index 000000000..e4afd409a --- /dev/null +++ b/app/src/main/java/im/conversations/android/AppSettings.java @@ -0,0 +1,45 @@ +package im.conversations.android; + +import android.content.Context; +import android.content.SharedPreferences; +import android.net.Uri; +import androidx.preference.PreferenceManager; +import com.google.common.base.Strings; + +public class AppSettings { + + public static final String PREFERENCE_KEY_RINGTONE = "call_ringtone"; + public static final String PREFERENCE_KEY_BTBV = "btbv"; + + private final Context context; + + public AppSettings(final Context context) { + this.context = context; + } + + public Uri getRingtone() { + final SharedPreferences sharedPreferences = + PreferenceManager.getDefaultSharedPreferences(context); + final String incomingCallRingtone = + sharedPreferences.getString( + PREFERENCE_KEY_RINGTONE, + context.getString(R.string.incoming_call_ringtone)); + return Strings.isNullOrEmpty(incomingCallRingtone) ? null : Uri.parse(incomingCallRingtone); + } + + public void setRingtone(final Uri uri) { + final SharedPreferences sharedPreferences = + PreferenceManager.getDefaultSharedPreferences(context); + sharedPreferences + .edit() + .putString(PREFERENCE_KEY_RINGTONE, uri == null ? null : uri.toString()) + .apply(); + } + + public boolean isBtbv() { + final SharedPreferences sharedPreferences = + PreferenceManager.getDefaultSharedPreferences(context); + return sharedPreferences.getBoolean( + PREFERENCE_KEY_BTBV, context.getResources().getBoolean(R.bool.btbv)); + } +} diff --git a/app/src/main/java/im/conversations/android/notification/AbstractNotification.java b/app/src/main/java/im/conversations/android/notification/AbstractNotification.java index 8e8b74fce..65029cf7a 100644 --- a/app/src/main/java/im/conversations/android/notification/AbstractNotification.java +++ b/app/src/main/java/im/conversations/android/notification/AbstractNotification.java @@ -3,6 +3,7 @@ package im.conversations.android.notification; import android.content.Context; import android.content.SharedPreferences; import androidx.preference.PreferenceManager; +import im.conversations.android.AppSettings; import im.conversations.android.R; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -13,9 +14,11 @@ public abstract class AbstractNotification { Executors.newSingleThreadScheduledExecutor(); protected final Context context; + protected final AppSettings appSettings; protected AbstractNotification(final Context context) { this.context = context; + this.appSettings = new AppSettings(context); } public boolean notificationsFromStrangers() { diff --git a/app/src/main/java/im/conversations/android/notification/RtpSessionNotification.java b/app/src/main/java/im/conversations/android/notification/RtpSessionNotification.java index 1269862d0..255bd410e 100644 --- a/app/src/main/java/im/conversations/android/notification/RtpSessionNotification.java +++ b/app/src/main/java/im/conversations/android/notification/RtpSessionNotification.java @@ -6,22 +6,18 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.content.res.Resources; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Build; import android.os.Vibrator; -import android.preference.PreferenceManager; import android.service.notification.StatusBarNotification; import android.util.Log; import androidx.core.app.ActivityCompat; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.content.ContextCompat; -import com.google.common.base.Strings; import eu.siacs.conversations.Config; import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection; import eu.siacs.conversations.xmpp.jingle.Media; @@ -120,17 +116,11 @@ public class RtpSessionNotification extends AbstractNotification { if (currentVibrationFuture != null) { currentVibrationFuture.cancel(true); } - final SharedPreferences preferences = - PreferenceManager.getDefaultSharedPreferences(context); - final Resources resources = context.getResources(); - final String ringtonePreference = - preferences.getString( - "call_ringtone", resources.getString(R.string.incoming_call_ringtone)); - if (Strings.isNullOrEmpty(ringtonePreference)) { + final Uri uri = appSettings.getRingtone(); + if (uri == null) { Log.d(Config.LOGTAG, "ringtone has been set to none"); return; } - final Uri uri = Uri.parse(ringtonePreference); this.currentlyPlayingRingtone = RingtoneManager.getRingtone(context, uri); if (this.currentlyPlayingRingtone == null) { Log.d(Config.LOGTAG, "unable to find ringtone for uri " + uri); diff --git a/app/src/main/java/im/conversations/android/ui/fragment/settings/NotificationsSettingsFragment.java b/app/src/main/java/im/conversations/android/ui/fragment/settings/NotificationsSettingsFragment.java index 709de54cf..acf4b07d2 100644 --- a/app/src/main/java/im/conversations/android/ui/fragment/settings/NotificationsSettingsFragment.java +++ b/app/src/main/java/im/conversations/android/ui/fragment/settings/NotificationsSettingsFragment.java @@ -1,14 +1,12 @@ package im.conversations.android.ui.fragment.settings; -import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import androidx.activity.result.ActivityResultLauncher; import androidx.annotation.Nullable; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.PreferenceManager; -import com.google.common.base.Strings; +import im.conversations.android.AppSettings; import im.conversations.android.R; import im.conversations.android.ui.activity.result.PickRingtone; import org.slf4j.Logger; @@ -16,8 +14,6 @@ import org.slf4j.LoggerFactory; public class NotificationsSettingsFragment extends PreferenceFragmentCompat { - private static final String RINGTONE_PREFERENCE_KEY = "call_ringtone"; - private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsSettingsFragment.class); @@ -47,7 +43,7 @@ public class NotificationsSettingsFragment extends PreferenceFragmentCompat { @Override public boolean onPreferenceTreeClick(final Preference preference) { - if (RINGTONE_PREFERENCE_KEY.equals(preference.getKey())) { + if (AppSettings.PREFERENCE_KEY_RINGTONE.equals(preference.getKey())) { pickRingtone(); return true; } @@ -55,26 +51,16 @@ public class NotificationsSettingsFragment extends PreferenceFragmentCompat { } private void pickRingtone() { - final SharedPreferences sharedPreferences = - PreferenceManager.getDefaultSharedPreferences(requireContext()); - final String incomingCallRingtone = - sharedPreferences.getString( - RINGTONE_PREFERENCE_KEY, - requireContext().getString(R.string.incoming_call_ringtone)); - final Uri uri = - Strings.isNullOrEmpty(incomingCallRingtone) - ? null - : Uri.parse(incomingCallRingtone); + final Uri uri = appSettings().getRingtone(); LOGGER.info("current ringtone {}", uri); this.pickRingtoneLauncher.launch(uri); } private void setRingtone(final Uri uri) { - final SharedPreferences sharedPreferences = - PreferenceManager.getDefaultSharedPreferences(requireContext()); - sharedPreferences - .edit() - .putString(RINGTONE_PREFERENCE_KEY, uri == null ? null : uri.toString()) - .apply(); + appSettings().setRingtone(uri); + } + + private AppSettings appSettings() { + return new AppSettings(requireContext()); } }