1package eu.siacs.conversations.ui.fragment.settings;
2
3import android.content.Context;
4import android.content.SharedPreferences;
5import android.util.Log;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import androidx.preference.ListPreference;
10import androidx.preference.Preference;
11import androidx.preference.PreferenceFragmentCompat;
12import androidx.preference.Preference;
13
14import com.rarepebble.colorpicker.ColorPreference;
15
16import com.google.common.base.Strings;
17import com.google.common.primitives.Ints;
18
19import eu.siacs.conversations.Config;
20import eu.siacs.conversations.R;
21import eu.siacs.conversations.entities.Account;
22import eu.siacs.conversations.services.XmppConnectionService;
23import eu.siacs.conversations.ui.XmppActivity;
24import eu.siacs.conversations.utils.TimeFrameUtils;
25
26public abstract class XmppPreferenceFragment extends PreferenceFragmentCompat {
27
28 private final SharedPreferences.OnSharedPreferenceChangeListener
29 sharedPreferenceChangeListener =
30 (sharedPreferences, key) -> {
31 if (key == null) {
32 return;
33 }
34 if (isAdded()) {
35 onSharedPreferenceChanged(key);
36 }
37 };
38
39 protected void onSharedPreferenceChanged(@NonNull String key) {
40 Log.d(Config.LOGTAG, "onSharedPreferenceChanged(" + key + ")");
41 }
42
43 public void onBackendConnected() {}
44
45 @Override
46 public void onDisplayPreferenceDialog(Preference preference) {
47 if (preference instanceof ColorPreference) {
48 ((ColorPreference) preference).showDialog(this, 0);
49 } else super.onDisplayPreferenceDialog(preference);
50 }
51
52 @Override
53 public void onResume() {
54 super.onResume();
55 final var sharedPreferences = getPreferenceManager().getSharedPreferences();
56 if (sharedPreferences != null) {
57 sharedPreferences.registerOnSharedPreferenceChangeListener(
58 this.sharedPreferenceChangeListener);
59 }
60 final var xmppActivity = requireXmppActivity();
61 if (xmppActivity.xmppConnectionService != null) {
62 this.onBackendConnected();
63 }
64 }
65
66 @Override
67 public void onPause() {
68 super.onPause();
69 final var sharedPreferences = getPreferenceManager().getSharedPreferences();
70 if (sharedPreferences != null) {
71 sharedPreferences.unregisterOnSharedPreferenceChangeListener(
72 this.sharedPreferenceChangeListener);
73 }
74 }
75
76 protected void reconnectAccounts() {
77 final var service = requireService();
78 for (final Account account : service.getAccounts()) {
79 if (account.isEnabled()) {
80 service.reconnectAccountInBackground(account);
81 }
82 }
83 }
84
85 protected XmppActivity requireXmppActivity() {
86 final var activity = requireActivity();
87 if (activity instanceof XmppActivity xmppActivity) {
88 return xmppActivity;
89 }
90 throw new IllegalStateException();
91 }
92
93 protected XmppConnectionService requireService() {
94 final var xmppActivity = requireXmppActivity();
95 final var service = xmppActivity.xmppConnectionService;
96 if (service != null) {
97 return service;
98 }
99 throw new IllegalStateException();
100 }
101
102 protected void runOnUiThread(final Runnable runnable) {
103 requireActivity().runOnUiThread(runnable);
104 }
105
106 protected static String timeframeValueToName(final Context context, final int value) {
107 if (value == 0) {
108 return context.getString(R.string.never);
109 } else {
110 return TimeFrameUtils.resolve(context, 1000L * value);
111 }
112 }
113
114 protected static class TimeframeSummaryProvider
115 implements Preference.SummaryProvider<ListPreference> {
116
117 @Nullable
118 @Override
119 public CharSequence provideSummary(@NonNull ListPreference preference) {
120 final Integer value = Ints.tryParse(Strings.nullToEmpty(preference.getValue()));
121 return timeframeValueToName(preference.getContext(), value == null ? 0 : value);
122 }
123 }
124}