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