EnterJidDialog.java

  1package eu.siacs.conversations.ui;
  2import android.util.Log;
  3
  4import android.app.Activity;
  5import android.app.Dialog;
  6import android.content.DialogInterface.OnClickListener;
  7import android.content.DialogInterface;
  8import android.os.Bundle;
  9import android.text.Editable;
 10import android.text.InputType;
 11import android.text.TextWatcher;
 12import android.util.Pair;
 13import android.view.LayoutInflater;
 14import android.view.View;
 15import android.view.ViewGroup;
 16import android.widget.AdapterView;
 17import android.widget.ArrayAdapter;
 18import android.widget.TextView;
 19import android.widget.ToggleButton;
 20
 21import androidx.annotation.NonNull;
 22import androidx.appcompat.app.AlertDialog;
 23import androidx.databinding.DataBindingUtil;
 24import androidx.fragment.app.DialogFragment;
 25import androidx.recyclerview.widget.RecyclerView;
 26import androidx.recyclerview.widget.LinearLayoutManager;
 27
 28import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 29import com.google.common.base.Strings;
 30
 31import java.util.ArrayList;
 32import java.util.Arrays;
 33import java.util.Collection;
 34import java.util.Collections;
 35import java.util.List;
 36import java.util.Map;
 37
 38import io.michaelrocks.libphonenumber.android.NumberParseException;
 39
 40import eu.siacs.conversations.R;
 41import eu.siacs.conversations.databinding.DialogEnterJidBinding;
 42import eu.siacs.conversations.services.XmppConnectionService;
 43import eu.siacs.conversations.entities.Account;
 44import eu.siacs.conversations.entities.Contact;
 45import eu.siacs.conversations.entities.Presence;
 46import eu.siacs.conversations.entities.ServiceDiscoveryResult;
 47import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 48import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
 49import eu.siacs.conversations.ui.util.DelayedHintHelper;
 50import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
 51import eu.siacs.conversations.xmpp.Jid;
 52import eu.siacs.conversations.xmpp.OnGatewayResult;
 53
 54public class EnterJidDialog extends DialogFragment implements OnBackendConnected, TextWatcher {
 55
 56    private static final List<String> SUSPICIOUS_DOMAINS =
 57            Arrays.asList("conference", "muc", "room", "rooms");
 58
 59    private OnEnterJidDialogPositiveListener mListener = null;
 60
 61    private static final String TITLE_KEY = "title";
 62    private static final String POSITIVE_BUTTON_KEY = "positive_button";
 63    private static final String SECONDARY_BUTTON_KEY = "secondary_button";
 64    private static final String PREFILLED_JID_KEY = "prefilled_jid";
 65    private static final String ACCOUNT_KEY = "account";
 66    private static final String ALLOW_EDIT_JID_KEY = "allow_edit_jid";
 67    private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
 68    private static final String SANITY_CHECK_JID = "sanity_check_jid";
 69    private static final String SHOW_BOOKMARK_CHECKBOX = "show_bookmark_checkbox";
 70
 71    private KnownHostsAdapter knownHostsAdapter;
 72    private Collection<String> whitelistedDomains = Collections.emptyList();
 73
 74    private DialogEnterJidBinding binding;
 75    private AlertDialog dialog;
 76    private SanityCheck sanityCheckJid = SanityCheck.NO;
 77
 78    private boolean issuedWarning = false;
 79    private GatewayListAdapter gatewayListAdapter = new GatewayListAdapter();
 80
 81    public static enum SanityCheck {
 82        NO,
 83        YES,
 84        ALLOW_MUC
 85    }
 86
 87    public static EnterJidDialog newInstance(
 88            final ArrayList<String> activatedAccounts,
 89            final String title,
 90            final String positiveButton,
 91            final String secondaryButton,
 92            final String prefilledJid,
 93            final String account,
 94            boolean allowEditJid,
 95            boolean showBookmarkCheckbox,
 96            final SanityCheck sanity_check_jid) {
 97        final EnterJidDialog dialog = new EnterJidDialog();
 98        Bundle bundle = new Bundle();
 99        bundle.putString(TITLE_KEY, title);
100        bundle.putString(POSITIVE_BUTTON_KEY, positiveButton);
101        bundle.putString(SECONDARY_BUTTON_KEY, secondaryButton);
102        bundle.putString(PREFILLED_JID_KEY, prefilledJid);
103        bundle.putString(ACCOUNT_KEY, account);
104        bundle.putBoolean(ALLOW_EDIT_JID_KEY, allowEditJid);
105        bundle.putStringArrayList(ACCOUNTS_LIST_KEY, activatedAccounts);
106        bundle.putInt(SANITY_CHECK_JID, sanity_check_jid.ordinal());
107        bundle.putBoolean(SHOW_BOOKMARK_CHECKBOX, showBookmarkCheckbox);
108        dialog.setArguments(bundle);
109        return dialog;
110    }
111
112    @Override
113    public void onActivityCreated(Bundle savedInstanceState) {
114        super.onActivityCreated(savedInstanceState);
115        setRetainInstance(true);
116    }
117
118    @Override
119    public void onStart() {
120        super.onStart();
121        final Activity activity = getActivity();
122        if (activity instanceof XmppActivity
123                && ((XmppActivity) activity).xmppConnectionService != null) {
124            refreshKnownHosts();
125        }
126    }
127
128    @NonNull
129    @Override
130    public Dialog onCreateDialog(final Bundle savedInstanceState) {
131        final var arguments = getArguments();
132        final MaterialAlertDialogBuilder builder =
133                new MaterialAlertDialogBuilder(requireActivity());
134        builder.setTitle(arguments.getString(TITLE_KEY));
135        binding =
136                DataBindingUtil.inflate(
137                        requireActivity().getLayoutInflater(),
138                        R.layout.dialog_enter_jid,
139                        null,
140                        false);
141        this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.item_autocomplete);
142        binding.jid.setAdapter(this.knownHostsAdapter);
143        binding.jid.addTextChangedListener(this);
144        final String prefilledJid = arguments.getString(PREFILLED_JID_KEY);
145        if (prefilledJid != null) {
146            binding.jid.append(prefilledJid);
147            if (!getArguments().getBoolean(ALLOW_EDIT_JID_KEY)) {
148                binding.jid.setFocusable(false);
149                binding.jid.setFocusableInTouchMode(false);
150                binding.jid.setClickable(false);
151                binding.jid.setCursorVisible(false);
152            }
153        }
154        sanityCheckJid = SanityCheck.values()[getArguments().getInt(SANITY_CHECK_JID, SanityCheck.NO.ordinal())];
155
156        if (!getArguments().getBoolean(SHOW_BOOKMARK_CHECKBOX, false)) {
157            binding.bookmark.setVisibility(View.GONE);
158        }
159
160        DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id, binding.jid);
161
162        final String account = getArguments().getString(ACCOUNT_KEY);
163        if (Strings.isNullOrEmpty(account)) {
164            StartConversationActivity.populateAccountSpinner(
165                    getActivity(),
166                    arguments.getStringArrayList(ACCOUNTS_LIST_KEY),
167                    binding.account);
168        } else {
169            final ArrayAdapter<String> adapter =
170                    new ArrayAdapter<>(
171                            requireActivity(), R.layout.item_autocomplete, new String[] {account});
172            binding.account.setText(account);
173            binding.account.setEnabled(false);
174            adapter.setDropDownViewResource(R.layout.item_autocomplete);
175            binding.account.setAdapter(adapter);
176        }
177
178        binding.gatewayList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
179        binding.gatewayList.setAdapter(gatewayListAdapter);
180        gatewayListAdapter.setOnEmpty(() -> binding.gatewayList.setVisibility(View.GONE));
181        gatewayListAdapter.setOnNonEmpty(() -> binding.gatewayList.setVisibility(View.VISIBLE));
182
183        binding.account.setOnItemClickListener(new AdapterView.OnItemClickListener() {
184            @Override
185            public void onItemClick(AdapterView accountSpinner, View view, int position, long id) {
186                populateGateways();
187            }
188        });
189        populateGateways();
190
191        builder.setView(binding.getRoot());
192        builder.setPositiveButton(getArguments().getString(POSITIVE_BUTTON_KEY), null);
193        if (getArguments().getString(SECONDARY_BUTTON_KEY) == null) {
194            builder.setNegativeButton(R.string.cancel, null);
195        } else {
196            builder.setNegativeButton(getArguments().getString(SECONDARY_BUTTON_KEY), null);
197            builder.setNeutralButton(R.string.cancel, null);
198        }
199        this.dialog = builder.create();
200
201        binding.jid.setOnEditorActionListener(
202                (v, actionId, event) -> {
203                    handleEnter(binding, account, false);
204                    return true;
205                });
206
207        dialog.show();
208        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener((v) -> handleEnter(binding, account, false));
209        if (getArguments().getString(SECONDARY_BUTTON_KEY) != null) {
210            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener((v) -> handleEnter(binding, account, true));
211        }
212        return dialog;
213    }
214
215    protected void populateGateways() {
216        XmppActivity context = (XmppActivity) getActivity();
217        if (context == null || context.xmppConnectionService == null || accountJid() == null) return;
218
219        gatewayListAdapter.clear();
220        final Account account = context.xmppConnectionService.findAccountByJid(accountJid());
221        if (account == null) return;
222
223        for (final Contact contact : account.getRoster().getContacts()) {
224            if (contact.showInRoster() && contact.getPresences().size() > 0 && (contact.getPresences().anyIdentity("gateway", null) || contact.getPresences().anySupport("jabber:iq:gateway"))) {
225                context.xmppConnectionService.fetchFromGateway(account, contact.getJid(), null, (final String prompt, String errorMessage) -> {
226                    if (prompt == null && !contact.getPresences().anyIdentity("gateway", null)) return;
227
228                    context.runOnUiThread(() -> {
229                            gatewayListAdapter.add(contact, prompt);
230                    });
231                });
232            }
233        }
234    }
235
236    protected Jid accountJid() {
237        try {
238            return Jid.of((String) binding.account.getEditableText().toString());
239        } catch (final IllegalArgumentException e) {
240            return null;
241        }
242    }
243
244    private void handleEnter(DialogEnterJidBinding binding, String account, boolean secondary) {
245        if (!binding.account.isEnabled() && account == null) {
246            return;
247        }
248        final Jid accountJid = accountJid();
249        final OnGatewayResult finish = (final String jidString, final String errorMessage) -> {
250            Activity context = getActivity();
251            if (context == null) return; // Race condition, we got the reply after the UI was closed
252
253            context.runOnUiThread(() -> {
254                if (errorMessage != null) {
255                    binding.jidLayout.setError(errorMessage);
256                    return;
257                }
258                if (jidString == null) {
259                    binding.jidLayout.setError(getActivity().getString(R.string.invalid_jid));
260                    return;
261                }
262
263                Jid contactJid = null;
264                try {
265                    contactJid = Jid.of(jidString);
266                } catch (final IllegalArgumentException e) {
267                    binding.jidLayout.setError(getActivity().getString(R.string.invalid_jid));
268                    return;
269                }
270
271                if (!issuedWarning && sanityCheckJid != SanityCheck.NO) {
272                    if (contactJid.isDomainJid()) {
273                        binding.jidLayout.setError(getActivity().getString(R.string.this_looks_like_a_domain));
274                        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway);
275                        issuedWarning = true;
276                        return;
277                    }
278                    if (sanityCheckJid != SanityCheck.ALLOW_MUC && suspiciousSubDomain(contactJid.getDomain().toString())) {
279                        binding.jidLayout.setError(getActivity().getString(R.string.this_looks_like_channel));
280                        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add_anway);
281                        issuedWarning = true;
282                        return;
283                    }
284                }
285
286                if (mListener != null) {
287                    try {
288                        if (mListener.onEnterJidDialogPositive(accountJid, contactJid, secondary, binding.bookmark.isChecked())) {
289                            dialog.dismiss();
290                        }
291                    } catch (JidError error) {
292                        binding.jidLayout.setError(error.toString());
293                        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
294                        issuedWarning = false;
295                    }
296                }
297            });
298        };
299
300        Pair<String,Pair<Jid,Presence>> p = gatewayListAdapter.getSelected();
301        final String type = gatewayListAdapter.getSelectedType();
302
303        // Resolve based on local settings before submission
304        if (type != null && (type.equals("pstn") || type.equals("sms"))) {
305            try {
306                binding.jid.setText(PhoneNumberUtilWrapper.normalize(getActivity(), binding.jid.getText().toString(), true));
307            } catch (NumberParseException | IllegalArgumentException | NullPointerException e) { }
308        }
309
310        if (p == null) {
311            finish.onGatewayResult(binding.jid.getText().toString().trim(), null);
312        } else if (p.first != null) { // Gateway already responsed to jabber:iq:gateway once
313            final Account acct = ((XmppActivity) getActivity()).xmppConnectionService.findAccountByJid(accountJid);
314            ((XmppActivity) getActivity()).xmppConnectionService.fetchFromGateway(acct, p.second.first, binding.jid.getText().toString().trim(), finish);
315        } else if (p.second.first.isDomainJid() && p.second.second.getServiceDiscoveryResult().getFeatures().contains("jid\\20escaping")) {
316            finish.onGatewayResult(Jid.ofLocalAndDomain(binding.jid.getText().toString().trim(), p.second.first.getDomain().toString()).toString(), null);
317        } else if (p.second.first.isDomainJid()) {
318            finish.onGatewayResult(Jid.ofLocalAndDomain(binding.jid.getText().toString().trim().replace("@", "%"), p.second.first.getDomain().toString()).toString(), null);
319        } else {
320            finish.onGatewayResult(null, null);
321        }
322    }
323
324    public void setOnEnterJidDialogPositiveListener(OnEnterJidDialogPositiveListener listener) {
325        this.mListener = listener;
326    }
327
328    @Override
329    public void onBackendConnected() {
330        refreshKnownHosts();
331    }
332
333    private void refreshKnownHosts() {
334        final Activity activity = getActivity();
335        if (activity instanceof XmppActivity) {
336            final XmppConnectionService service = ((XmppActivity) activity).xmppConnectionService;
337            if (service == null) {
338                return;
339            }
340            final Collection<String> hosts = service.getKnownHosts();
341            this.knownHostsAdapter.refresh(hosts);
342            this.whitelistedDomains = hosts;
343        }
344    }
345
346    @Override
347    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
348
349    @Override
350    public void onTextChanged(CharSequence s, int start, int before, int count) {}
351
352    @Override
353    public void afterTextChanged(Editable s) {
354        if (issuedWarning) {
355            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.add);
356            binding.jidLayout.setError(null);
357            issuedWarning = false;
358        }
359    }
360
361    public interface OnEnterJidDialogPositiveListener {
362        boolean onEnterJidDialogPositive(Jid account, Jid contact, boolean secondary, boolean save) throws EnterJidDialog.JidError;
363    }
364
365    public static class JidError extends Exception {
366        final String msg;
367
368        public JidError(final String msg) {
369            this.msg = msg;
370        }
371
372        @NonNull
373        public String toString() {
374            return msg;
375        }
376    }
377
378    @Override
379    public void onDestroyView() {
380        Dialog dialog = getDialog();
381        if (dialog != null && getRetainInstance()) {
382            dialog.setDismissMessage(null);
383        }
384        super.onDestroyView();
385    }
386
387    private boolean suspiciousSubDomain(String domain) {
388        if (this.whitelistedDomains.contains(domain)) {
389            return false;
390        }
391        final String[] parts = domain.split("\\.");
392        return parts.length >= 3 && SUSPICIOUS_DOMAINS.contains(parts[0]);
393    }
394
395    protected class GatewayListAdapter extends RecyclerView.Adapter<GatewayListAdapter.ViewHolder> {
396        protected class ViewHolder extends RecyclerView.ViewHolder {
397            protected ToggleButton button;
398            protected int index;
399
400            public ViewHolder(View view, int i) {
401                super(view);
402                this.button = (ToggleButton) view.findViewById(R.id.button);
403                setIndex(i);
404                button.setOnClickListener(new View.OnClickListener() {
405                    @Override
406                    public void onClick(View v) {
407                        button.setChecked(true); // Force visual not to flap to unchecked
408                        setSelected(index);
409                    }
410                });
411            }
412
413            public void setIndex(int i) {
414                this.index = i;
415                button.setChecked(selected == i);
416            }
417
418            public void useButton(int res) {
419                button.setText(res);
420                button.setTextOff(button.getText());
421                button.setTextOn(button.getText());
422                button.setChecked(selected == this.index);
423                binding.gatewayList.setVisibility(View.VISIBLE);
424                button.setVisibility(View.VISIBLE);
425            }
426
427            public void useButton(String txt) {
428                button.setTextOff(txt);
429                button.setTextOn(txt);
430                button.setChecked(selected == this.index);
431                binding.gatewayList.setVisibility(View.VISIBLE);
432                button.setVisibility(View.VISIBLE);
433            }
434        }
435
436        protected List<Pair<Contact,String>> gateways = new ArrayList();
437        protected int selected = 0;
438        protected Runnable onEmpty = () -> {};
439        protected Runnable onNonEmpty = () -> {};
440
441        @Override
442        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
443            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.enter_jid_dialog_gateway_list_item, null);
444            return new ViewHolder(view, i);
445        }
446
447        @Override
448        public void onBindViewHolder(ViewHolder viewHolder, int i) {
449            viewHolder.setIndex(i);
450
451            if(i == 0) {
452                viewHolder.useButton(R.string.account_settings_jabber_id);
453            } else {
454                viewHolder.useButton(getLabel(i));
455            }
456        }
457
458        @Override
459        public int getItemCount() {
460            return this.gateways.size() + 1;
461        }
462
463        public void setSelected(int i) {
464            int old = this.selected;
465            this.selected = i;
466
467            if(i == 0) {
468                binding.jid.setThreshold(1);
469                binding.jid.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
470                binding.jidLayout.setHint(R.string.account_settings_jabber_id);
471
472                if(binding.jid.hasFocus()) {
473                    binding.jid.setHint(R.string.account_settings_example_jabber_id);
474                } else {
475                    DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id, binding.jid);
476                }
477            } else {
478                binding.jid.setThreshold(999999); // do not autocomplete
479                binding.jid.setHint(null);
480                binding.jid.setOnFocusChangeListener((v, hasFocus) -> {});
481                binding.jidLayout.setHint(this.gateways.get(i-1).second);
482
483                String type = getType(i);
484                if (type == null) type = "";
485                if (type.equals("pstn") || type.equals("sms")) {
486                    binding.jid.setInputType(InputType.TYPE_CLASS_PHONE);
487                } else if (type.equals("email") || type.equals("sip")) {
488                    binding.jid.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
489
490                    if(binding.jid.hasFocus()) {
491                        binding.jid.setHint(R.string.account_settings_example_jabber_id);
492                    } else {
493                        DelayedHintHelper.setHint(R.string.account_settings_example_jabber_id, binding.jid);
494                    }
495                } else {
496                    binding.jid.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
497                }
498            }
499
500            notifyItemChanged(old);
501            notifyItemChanged(i);
502        }
503
504        public String getLabel(Contact gateway) {
505            String type = getType(gateway);
506            if ("pstn".equals(type)) return "📞";
507            if (type != null) return type;
508
509            return gateway.getDisplayName();
510        }
511
512        public String getLabel(int i) {
513            if (i == 0) return null;
514
515            return getLabel(this.gateways.get(i-1).first);
516        }
517
518        public String getType(int i) {
519            if (i == 0) return null;
520
521            return getType(this.gateways.get(i-1).first);
522        }
523
524        public String getType(Contact gateway) {
525            List<String> types = getTypes(gateway);
526            return types.isEmpty() ? null : types.get(0);
527        }
528
529        public List<String> getTypes(Contact gateway) {
530            List<String> types = new ArrayList<>();
531
532            for(Presence p : gateway.getPresences().getPresences()) {
533                if(p.getServiceDiscoveryResult() != null) {
534                    for (ServiceDiscoveryResult.Identity id : p.getServiceDiscoveryResult().getIdentities()) {
535                        if ("gateway".equals(id.getCategory())) types.add(id.getType());
536                    }
537                }
538            }
539
540            return types;
541        }
542
543        public String getSelectedType() {
544            return getType(selected);
545        }
546
547        public Pair<String, Pair<Jid,Presence>> getSelected() {
548            if(this.selected == 0) {
549                return null; // No gateway, just use direct JID entry
550            }
551
552            Pair<Contact,String> gateway = this.gateways.get(this.selected - 1);
553
554            Pair<Jid,Presence> presence = null;
555            for (Map.Entry<String,Presence> e : gateway.first.getPresences().getPresencesMap().entrySet()) {
556                Presence p = e.getValue();
557                if (p.getServiceDiscoveryResult() != null) {
558                    if (p.getServiceDiscoveryResult().getFeatures().contains("jabber:iq:gateway")) {
559                        if (e.getKey().equals("")) {
560                            presence = new Pair<>(gateway.first.getJid(), p);
561                        } else {
562                            presence = new Pair<>(gateway.first.getJid().withResource(e.getKey()), p);
563                        }
564                        break;
565                    }
566                    if (p.getServiceDiscoveryResult().hasIdentity("gateway", null)) {
567                        if (e.getKey().equals("")) {
568                            presence = new Pair<>(gateway.first.getJid(), p);
569                        } else {
570                            presence = new Pair<>(gateway.first.getJid().withResource(e.getKey()), p);
571                        }
572                    }
573                }
574            }
575
576            return presence == null ? null : new Pair(gateway.second, presence);
577        }
578
579        public void setOnEmpty(Runnable r) {
580            onEmpty = r;
581        }
582
583        public void setOnNonEmpty(Runnable r) {
584            onNonEmpty = r;
585        }
586
587        public void clear() {
588            gateways.clear();
589            onEmpty.run();
590            notifyDataSetChanged();
591            setSelected(0);
592        }
593
594        public void add(Contact gateway, String prompt) {
595            if (getItemCount() < 2) onNonEmpty.run();
596            this.gateways.add(new Pair<>(gateway, prompt));
597            Collections.sort(this.gateways, (x, y) -> getLabel(x.first).compareTo(getLabel(y.first)));
598            notifyDataSetChanged();
599        }
600    }
601}