mirror of
https://github.com/deltachat/deltachat-android.git
synced 2025-10-03 01:39:18 +02:00
improve preference handling
This commit is contained in:
parent
b6ce6d908f
commit
2547a0435c
3 changed files with 33 additions and 22 deletions
|
@ -30,7 +30,7 @@ import org.thoughtcrime.securesms.connect.DcHelper;
|
||||||
import org.thoughtcrime.securesms.connect.KeepAliveService;
|
import org.thoughtcrime.securesms.connect.KeepAliveService;
|
||||||
import org.thoughtcrime.securesms.util.Prefs;
|
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 String TAG = NotificationsPreferenceFragment.class.getSimpleName();
|
||||||
private static final int REQUEST_CODE_NOTIFICATION_SELECTED = 1;
|
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) {
|
if (reliableService != null) {
|
||||||
reliableService.setOnPreferenceChangeListener((preference, newValue) -> {
|
reliableService.setOnPreferenceChangeListener(this);
|
||||||
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;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
notificationsEnabled = this.findPreference("pref_enable_notifications");
|
notificationsEnabled = this.findPreference("pref_enable_notifications");
|
||||||
|
@ -133,10 +127,11 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
|
||||||
ignoreBattery.setChecked(isIgnoringBatteryOptimizations());
|
ignoreBattery.setChecked(isIgnoringBatteryOptimizations());
|
||||||
notificationsEnabled.setChecked(!dcContext.isMuted());
|
notificationsEnabled.setChecked(!dcContext.isMuted());
|
||||||
mentionNotifEnabled.setChecked(dcContext.isMentionsEnabled());
|
mentionNotifEnabled.setChecked(dcContext.isMentionsEnabled());
|
||||||
if (Prefs.reliableService(getActivity())) {
|
|
||||||
// only alter the "unset" state of the preference if true
|
// set without altering "unset" state of the preference
|
||||||
reliableService.setChecked(true);
|
reliableService.setOnPreferenceChangeListener(null);
|
||||||
}
|
reliableService.setChecked(Prefs.reliableService(getActivity()));
|
||||||
|
reliableService.setOnPreferenceChangeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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 {
|
private class RingtoneSummaryListener implements Preference.OnPreferenceChangeListener {
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
|
public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
|
||||||
|
|
|
@ -30,6 +30,7 @@ public class Prefs {
|
||||||
|
|
||||||
private static final String TAG = Prefs.class.getSimpleName();
|
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 DISABLE_PASSPHRASE_PREF = "pref_disable_passphrase";
|
||||||
public static final String THEME_PREF = "pref_theme";
|
public static final String THEME_PREF = "pref_theme";
|
||||||
public static final String BACKGROUND_PREF = "pref_chat_background";
|
public static final String BACKGROUND_PREF = "pref_chat_background";
|
||||||
|
@ -214,18 +215,20 @@ public class Prefs {
|
||||||
return result==null? null : Uri.parse(result);
|
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) {
|
public static boolean reliableService(Context context) {
|
||||||
final String key = "pref_reliable_service";
|
|
||||||
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
if (prefs.contains(key)) {
|
if (prefs.contains(RELIABLE_SERVICE_PREF)) {
|
||||||
try {
|
try {
|
||||||
return prefs.getBoolean(key, true);
|
return prefs.getBoolean(RELIABLE_SERVICE_PREF, true);
|
||||||
} catch(Exception e) {}
|
} catch(Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the key was unset, then calculate default value
|
// if the key was unset, then calculate default value
|
||||||
return FcmReceiveService.getToken() == null
|
return isPushEnabled(context) || !DcHelper.getAccounts(context).isAllChatmail();
|
||||||
|| !DcHelper.getAccounts(context).isAllChatmail();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// vibrate
|
// vibrate
|
||||||
|
|
|
@ -64,7 +64,7 @@
|
||||||
android:summary="@string/pref_background_notifications_explain"/>
|
android:summary="@string/pref_background_notifications_explain"/>
|
||||||
|
|
||||||
<org.thoughtcrime.securesms.components.SwitchPreferenceCompat
|
<org.thoughtcrime.securesms.components.SwitchPreferenceCompat
|
||||||
android:key="pref_reliable_service"
|
android:key="pref_reliable_service2"
|
||||||
android:defaultValue="false"
|
android:defaultValue="false"
|
||||||
android:title="@string/pref_reliable_service"
|
android:title="@string/pref_reliable_service"
|
||||||
android:summary="@string/pref_reliable_service_explain"/>
|
android:summary="@string/pref_reliable_service_explain"/>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue