improve preference handling

This commit is contained in:
adbenitez 2025-08-26 15:54:09 +02:00
parent b6ce6d908f
commit 2547a0435c
3 changed files with 33 additions and 22 deletions

View file

@ -30,7 +30,7 @@ import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.connect.KeepAliveService;
import org.thoughtcrime.securesms.util.Prefs;
public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragment {
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;
@ -86,18 +86,12 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
}
reliableService = this.findPreference("pref_reliable_service");
// reliableService is just used for displaying the actual value
// of the reliable service preference that is managed via
// Prefs.setReliableService() and Prefs.reliableService()
reliableService = this.findPreference("pref_reliable_service2");
if (reliableService != null) {
reliableService.setOnPreferenceChangeListener((preference, newValue) -> {
Context context = getContext();
boolean enabled = (Boolean) newValue; // Prefs.reliableService() still has the old value
if (enabled) {
KeepAliveService.startSelf(context);
} else {
context.stopService(new Intent(context, KeepAliveService.class));
}
return true;
});
reliableService.setOnPreferenceChangeListener(this);
}
notificationsEnabled = this.findPreference("pref_enable_notifications");
@ -133,10 +127,11 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
ignoreBattery.setChecked(isIgnoringBatteryOptimizations());
notificationsEnabled.setChecked(!dcContext.isMuted());
mentionNotifEnabled.setChecked(dcContext.isMentionsEnabled());
if (Prefs.reliableService(getActivity())) {
// only alter the "unset" state of the preference if true
reliableService.setChecked(true);
}
// set without altering "unset" state of the preference
reliableService.setOnPreferenceChangeListener(null);
reliableService.setChecked(Prefs.reliableService(getActivity()));
reliableService.setOnPreferenceChangeListener(this);
}
@Override
@ -155,6 +150,19 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
}
}
@Override
public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
Context context = getContext();
boolean enabled = (Boolean) newValue;
Prefs.setReliableService(context, enabled);
if (enabled) {
KeepAliveService.startSelf(context);
} else {
context.stopService(new Intent(context, KeepAliveService.class));
}
return true;
}
private class RingtoneSummaryListener implements Preference.OnPreferenceChangeListener {
@Override
public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {

View file

@ -30,6 +30,7 @@ public class Prefs {
private static final String TAG = Prefs.class.getSimpleName();
public static final String RELIABLE_SERVICE_PREF = "pref_reliable_service";
public static final String DISABLE_PASSPHRASE_PREF = "pref_disable_passphrase";
public static final String THEME_PREF = "pref_theme";
public static final String BACKGROUND_PREF = "pref_chat_background";
@ -214,18 +215,20 @@ public class Prefs {
return result==null? null : Uri.parse(result);
}
public static void setReliableService(Context context, boolean value) {
setBooleanPreference(context, RELIABLE_SERVICE_PREF, value);
}
public static boolean reliableService(Context context) {
final String key = "pref_reliable_service";
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.contains(key)) {
if (prefs.contains(RELIABLE_SERVICE_PREF)) {
try {
return prefs.getBoolean(key, true);
return prefs.getBoolean(RELIABLE_SERVICE_PREF, true);
} catch(Exception e) {}
}
// if the key was unset, then calculate default value
return FcmReceiveService.getToken() == null
|| !DcHelper.getAccounts(context).isAllChatmail();
return isPushEnabled(context) || !DcHelper.getAccounts(context).isAllChatmail();
}
// vibrate

View file

@ -64,7 +64,7 @@
android:summary="@string/pref_background_notifications_explain"/>
<org.thoughtcrime.securesms.components.SwitchPreferenceCompat
android:key="pref_reliable_service"
android:key="pref_reliable_service2"
android:defaultValue="false"
android:title="@string/pref_reliable_service"
android:summary="@string/pref_reliable_service_explain"/>