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