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.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() {

View file

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

View file

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