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