UpSettingsFragment.java

  1package eu.siacs.conversations.ui.fragment.settings;
  2
  3import android.os.Bundle;
  4import android.widget.Toast;
  5
  6import androidx.annotation.NonNull;
  7import androidx.annotation.Nullable;
  8import androidx.preference.EditTextPreference;
  9import androidx.preference.ListPreference;
 10
 11import com.google.common.base.Strings;
 12import com.google.common.collect.ImmutableList;
 13import com.google.common.collect.Lists;
 14
 15import eu.siacs.conversations.R;
 16import eu.siacs.conversations.receiver.UnifiedPushDistributor;
 17import eu.siacs.conversations.xmpp.Jid;
 18
 19import java.net.URI;
 20import java.net.URISyntaxException;
 21import java.util.Arrays;
 22import java.util.List;
 23
 24public class UpSettingsFragment extends XmppPreferenceFragment {
 25
 26    @Override
 27    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
 28        setPreferencesFromResource(R.xml.preferences_up, rootKey);
 29    }
 30
 31    @Override
 32    public void onBackendConnected() {
 33        final ListPreference upAccounts = findPreference(UnifiedPushDistributor.PREFERENCE_ACCOUNT);
 34        final EditTextPreference pushServer = findPreference(UnifiedPushDistributor.PREFERENCE_PUSH_SERVER);
 35        if (upAccounts == null || pushServer == null) {
 36            throw new IllegalStateException();
 37        }
 38        pushServer.setOnPreferenceChangeListener((preference, newValue) -> {
 39            if (newValue instanceof String string) {
 40                if (Strings.isNullOrEmpty(string) || isJidInvalid(string) || isHttpUri(string)) {
 41                    Toast.makeText(requireActivity(),R.string.invalid_jid,Toast.LENGTH_LONG).show();
 42                    return false;
 43                } else {
 44                    return true;
 45                }
 46            } else {
 47                Toast.makeText(requireActivity(),R.string.invalid_jid,Toast.LENGTH_LONG).show();
 48                return false;
 49            }
 50        });
 51        reconfigureUpAccountPreference(upAccounts);
 52    }
 53
 54    private static boolean isJidInvalid(final String input) {
 55        try {
 56            final var jid = Jid.ofEscaped(input);
 57            return !jid.isBareJid();
 58        } catch (final IllegalArgumentException e) {
 59            return true;
 60        }
 61    }
 62
 63    private static boolean isHttpUri(final String input) {
 64        final URI uri;
 65        try {
 66            uri = new URI(input);
 67        } catch (final URISyntaxException e) {
 68            return false;
 69        }
 70        return Arrays.asList("http","https").contains(uri.getScheme());
 71    }
 72
 73
 74    private void reconfigureUpAccountPreference(final ListPreference listPreference) {
 75        final List<CharSequence> accounts =
 76                ImmutableList.copyOf(
 77                        Lists.transform(
 78                                requireService().getAccounts(),
 79                                a -> a.getJid().asBareJid().toEscapedString()));
 80        final ImmutableList.Builder<CharSequence> entries = new ImmutableList.Builder<>();
 81        final ImmutableList.Builder<CharSequence> entryValues = new ImmutableList.Builder<>();
 82        entries.add(getString(R.string.no_account_deactivated));
 83        entryValues.add("none");
 84        entries.addAll(accounts);
 85        entryValues.addAll(accounts);
 86        listPreference.setEntries(entries.build().toArray(new CharSequence[0]));
 87        listPreference.setEntryValues(entryValues.build().toArray(new CharSequence[0]));
 88        if (!accounts.contains(listPreference.getValue())) {
 89            listPreference.setValue("none");
 90        }
 91    }
 92
 93    @Override
 94    protected void onSharedPreferenceChanged(@NonNull String key) {
 95        super.onSharedPreferenceChanged(key);
 96        if (UnifiedPushDistributor.PREFERENCES.contains(key)) {
 97            final var service = requireService();
 98            if (service.reconfigurePushDistributor()) {
 99                service.renewUnifiedPushEndpoints();
100            }
101        }
102    }
103
104    @Override
105    public void onStart() {
106        super.onStart();
107        requireActivity().setTitle(R.string.unified_push_distributor);
108    }
109}