introduce AppSettings for easier access to preferences

This commit is contained in:
Daniel Gultsch 2023-03-01 09:05:56 +01:00
parent 86ef179c42
commit 1a924d3efd
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
4 changed files with 58 additions and 34 deletions

View file

@ -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));
}
}

View file

@ -3,6 +3,7 @@ package im.conversations.android.notification;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import im.conversations.android.AppSettings;
import im.conversations.android.R; import im.conversations.android.R;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -13,9 +14,11 @@ public abstract class AbstractNotification {
Executors.newSingleThreadScheduledExecutor(); Executors.newSingleThreadScheduledExecutor();
protected final Context context; protected final Context context;
protected final AppSettings appSettings;
protected AbstractNotification(final Context context) { protected AbstractNotification(final Context context) {
this.context = context; this.context = context;
this.appSettings = new AppSettings(context);
} }
public boolean notificationsFromStrangers() { public boolean notificationsFromStrangers() {

View file

@ -6,22 +6,18 @@ import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.media.Ringtone; import android.media.Ringtone;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Vibrator; import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.util.Log; import android.util.Log;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.common.base.Strings;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection; import eu.siacs.conversations.xmpp.jingle.AbstractJingleConnection;
import eu.siacs.conversations.xmpp.jingle.Media; import eu.siacs.conversations.xmpp.jingle.Media;
@ -120,17 +116,11 @@ public class RtpSessionNotification extends AbstractNotification {
if (currentVibrationFuture != null) { if (currentVibrationFuture != null) {
currentVibrationFuture.cancel(true); currentVibrationFuture.cancel(true);
} }
final SharedPreferences preferences = final Uri uri = appSettings.getRingtone();
PreferenceManager.getDefaultSharedPreferences(context); if (uri == null) {
final Resources resources = context.getResources();
final String ringtonePreference =
preferences.getString(
"call_ringtone", resources.getString(R.string.incoming_call_ringtone));
if (Strings.isNullOrEmpty(ringtonePreference)) {
Log.d(Config.LOGTAG, "ringtone has been set to none"); Log.d(Config.LOGTAG, "ringtone has been set to none");
return; return;
} }
final Uri uri = Uri.parse(ringtonePreference);
this.currentlyPlayingRingtone = RingtoneManager.getRingtone(context, uri); this.currentlyPlayingRingtone = RingtoneManager.getRingtone(context, uri);
if (this.currentlyPlayingRingtone == null) { if (this.currentlyPlayingRingtone == null) {
Log.d(Config.LOGTAG, "unable to find ringtone for uri " + uri); Log.d(Config.LOGTAG, "unable to find ringtone for uri " + uri);

View file

@ -1,14 +1,12 @@
package im.conversations.android.ui.fragment.settings; package im.conversations.android.ui.fragment.settings;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager; import im.conversations.android.AppSettings;
import com.google.common.base.Strings;
import im.conversations.android.R; import im.conversations.android.R;
import im.conversations.android.ui.activity.result.PickRingtone; import im.conversations.android.ui.activity.result.PickRingtone;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -16,8 +14,6 @@ import org.slf4j.LoggerFactory;
public class NotificationsSettingsFragment extends PreferenceFragmentCompat { public class NotificationsSettingsFragment extends PreferenceFragmentCompat {
private static final String RINGTONE_PREFERENCE_KEY = "call_ringtone";
private static final Logger LOGGER = private static final Logger LOGGER =
LoggerFactory.getLogger(NotificationsSettingsFragment.class); LoggerFactory.getLogger(NotificationsSettingsFragment.class);
@ -47,7 +43,7 @@ public class NotificationsSettingsFragment extends PreferenceFragmentCompat {
@Override @Override
public boolean onPreferenceTreeClick(final Preference preference) { public boolean onPreferenceTreeClick(final Preference preference) {
if (RINGTONE_PREFERENCE_KEY.equals(preference.getKey())) { if (AppSettings.PREFERENCE_KEY_RINGTONE.equals(preference.getKey())) {
pickRingtone(); pickRingtone();
return true; return true;
} }
@ -55,26 +51,16 @@ public class NotificationsSettingsFragment extends PreferenceFragmentCompat {
} }
private void pickRingtone() { private void pickRingtone() {
final SharedPreferences sharedPreferences = final Uri uri = appSettings().getRingtone();
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);
LOGGER.info("current ringtone {}", uri); LOGGER.info("current ringtone {}", uri);
this.pickRingtoneLauncher.launch(uri); this.pickRingtoneLauncher.launch(uri);
} }
private void setRingtone(final Uri uri) { private void setRingtone(final Uri uri) {
final SharedPreferences sharedPreferences = appSettings().setRingtone(uri);
PreferenceManager.getDefaultSharedPreferences(requireContext()); }
sharedPreferences
.edit() private AppSettings appSettings() {
.putString(RINGTONE_PREFERENCE_KEY, uri == null ? null : uri.toString()) return new AppSettings(requireContext());
.apply();
} }
} }