From ef7d4fca86934953d767830a65b5b0db5f720d32 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 15 May 2020 18:21:45 +0200 Subject: [PATCH] show escaped jid in most of the UI for historical reasons we store unescaped variants in DB and use them in intents. --- .../services/ShortcutService.java | 4 ++-- .../ui/ContactDetailsActivity.java | 6 +++--- .../conversations/ui/EditAccountActivity.java | 8 ++++---- .../conversations/ui/EnterJidDialog.java | 2 +- .../ui/StartConversationActivity.java | 10 +++++----- .../ui/adapter/AccountAdapter.java | 2 +- .../utils/IrregularUnicodeDetector.java | 8 ++++---- .../java/eu/siacs/conversations/xmpp/Jid.java | 20 +++++++++++++++++++ 8 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/ShortcutService.java b/src/main/java/eu/siacs/conversations/services/ShortcutService.java index 956159ad6..9908879e7 100644 --- a/src/main/java/eu/siacs/conversations/services/ShortcutService.java +++ b/src/main/java/eu/siacs/conversations/services/ShortcutService.java @@ -116,13 +116,13 @@ public class ShortcutService { } private static String getShortcutId(Contact contact) { - return contact.getAccount().getJid().asBareJid().toString()+"#"+contact.getJid().asBareJid().toString(); + return contact.getAccount().getJid().asBareJid().toEscapedString()+"#"+contact.getJid().asBareJid().toEscapedString(); } private Intent getShortcutIntent(Contact contact) { Intent intent = new Intent(xmppConnectionService, StartConversationActivity.class); intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse("xmpp:"+contact.getJid().asBareJid().toString())); + intent.setData(Uri.parse("xmpp:"+contact.getJid().asBareJid().toEscapedString())); intent.putExtra("account",contact.getAccount().getJid().asBareJid().toString()); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP); return intent; diff --git a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java index 27add89eb..caeb1e6e2 100644 --- a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -136,7 +136,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp AlertDialog.Builder builder = new AlertDialog.Builder( ContactDetailsActivity.this); builder.setTitle(getString(R.string.action_add_phone_book)); - builder.setMessage(getString(R.string.add_phone_book_text, contact.getJid().toString())); + builder.setMessage(getString(R.string.add_phone_book_text, contact.getJid().toEscapedString())); builder.setNegativeButton(getString(R.string.cancel), null); builder.setPositiveButton(getString(R.string.add), addToPhonebook); builder.create().show(); @@ -411,9 +411,9 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp binding.detailsContactjid.setText(IrregularUnicodeDetector.style(this, contact.getJid())); String account; if (Config.DOMAIN_LOCK != null) { - account = contact.getAccount().getJid().getLocal(); + account = contact.getAccount().getJid().getEscapedLocal(); } else { - account = contact.getAccount().getJid().asBareJid().toString(); + account = contact.getAccount().getJid().asBareJid().toEscapedString(); } binding.detailsAccount.setText(getString(R.string.using_account, account)); AvatarWorkerTask.loadAvatar(contact, binding.detailsContactBadge, R.dimen.avatar_on_details_screen_size); diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index d190a75d5..37c3be53f 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -201,9 +201,9 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat final Jid jid; try { if (mUsernameMode) { - jid = Jid.of(binding.accountJid.getText().toString(), getUserModeDomain(), null); + jid = Jid.ofEscaped(binding.accountJid.getText().toString(), getUserModeDomain(), null); } else { - jid = Jid.of(binding.accountJid.getText().toString()); + jid = Jid.ofEscaped(binding.accountJid.getText().toString()); } } catch (final NullPointerException | IllegalArgumentException e) { if (mUsernameMode) { @@ -959,9 +959,9 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat if (init) { this.binding.accountJid.getEditableText().clear(); if (mUsernameMode) { - this.binding.accountJid.getEditableText().append(this.mAccount.getJid().getLocal()); + this.binding.accountJid.getEditableText().append(this.mAccount.getJid().getEscapedLocal()); } else { - this.binding.accountJid.getEditableText().append(this.mAccount.getJid().asBareJid().toString()); + this.binding.accountJid.getEditableText().append(this.mAccount.getJid().asBareJid().toEscapedString()); } this.binding.accountPassword.getEditableText().clear(); this.binding.accountPassword.getEditableText().append(this.mAccount.getPassword()); diff --git a/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java b/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java index 933022983..6ad8859b9 100644 --- a/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java +++ b/src/main/java/eu/siacs/conversations/ui/EnterJidDialog.java @@ -155,7 +155,7 @@ public class EnterJidDialog extends DialogFragment implements OnBackendConnected } final Jid contactJid; try { - contactJid = Jid.of(binding.jid.getText().toString()); + contactJid = Jid.ofEscaped(binding.jid.getText().toString()); } catch (final IllegalArgumentException e) { binding.jidLayout.setError(getActivity().getString(R.string.invalid_jid)); return; diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index 58e749d62..35484232d 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -855,11 +855,11 @@ public class StartConversationActivity extends XmppActivity implements XmppConne switchToConversationDoNotAppend(muc, invite.getBody()); return true; } else { - showJoinConferenceDialog(invite.getJid().asBareJid().toString()); + showJoinConferenceDialog(invite.getJid().asBareJid().toEscapedString()); return false; } } else if (contacts.size() == 0) { - showCreateContactDialog(invite.getJid().toString(), invite); + showCreateContactDialog(invite.getJid().toEscapedString(), invite); return false; } else if (contacts.size() == 1) { Contact contact = contacts.get(0); @@ -881,10 +881,10 @@ public class StartConversationActivity extends XmppActivity implements XmppConne if (mMenuSearchView != null) { mMenuSearchView.expandActionView(); mSearchEditText.setText(""); - mSearchEditText.append(invite.getJid().toString()); - filter(invite.getJid().toString()); + mSearchEditText.append(invite.getJid().toEscapedString()); + filter(invite.getJid().toEscapedString()); } else { - mInitialSearchValue.push(invite.getJid().toString()); + mInitialSearchValue.push(invite.getJid().toEscapedString()); } return true; } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java index c2d84c162..73e3b4194 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/AccountAdapter.java @@ -58,7 +58,7 @@ public class AccountAdapter extends ArrayAdapter { if (Config.DOMAIN_LOCK != null) { viewHolder.binding.accountJid.setText(account.getJid().getLocal()); } else { - viewHolder.binding.accountJid.setText(account.getJid().asBareJid().toString()); + viewHolder.binding.accountJid.setText(account.getJid().asBareJid().toEscapedString()); } AvatarWorkerTask.loadAvatar(account, viewHolder.binding.accountImage, R.dimen.avatar); viewHolder.binding.accountStatus.setText(getContext().getString(account.getStatus().getReadableId())); diff --git a/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java b/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java index de345f744..ac06e2799 100644 --- a/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java +++ b/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeDetector.java @@ -82,8 +82,8 @@ public class IrregularUnicodeDetector { private static Spannable style(Jid jid, @ColorInt int color) { PatternTuple patternTuple = find(jid); SpannableStringBuilder builder = new SpannableStringBuilder(); - if (jid.getLocal() != null && patternTuple.local != null) { - SpannableString local = new SpannableString(jid.getLocal()); + if (jid.getEscapedLocal() != null && patternTuple.local != null) { + SpannableString local = new SpannableString(jid.getEscapedLocal()); colorize(local, patternTuple.local, color); builder.append(local); builder.append('@'); @@ -258,8 +258,8 @@ public class IrregularUnicodeDetector { private static PatternTuple of(Jid jid) { final Pattern localPattern; - if (jid.getLocal() != null) { - localPattern = create(findIrregularCodePoints(jid.getLocal())); + if (jid.getEscapedLocal() != null) { + localPattern = create(findIrregularCodePoints(jid.getEscapedLocal())); } else { localPattern = null; } diff --git a/src/main/java/eu/siacs/conversations/xmpp/Jid.java b/src/main/java/eu/siacs/conversations/xmpp/Jid.java index e286ee8a5..2f4a31d9c 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/Jid.java +++ b/src/main/java/eu/siacs/conversations/xmpp/Jid.java @@ -25,6 +25,26 @@ public interface Jid extends Comparable, Serializable, CharSequence { } } + static Jid ofEscaped(CharSequence local, CharSequence domain, CharSequence resource) { + try { + if (resource == null) { + return new WrappedJid( + JidCreate.bareFrom( + Localpart.from(local.toString()), + Domainpart.from(domain.toString()) + ) + ); + } + return new WrappedJid(JidCreate.entityFullFrom( + Localpart.from(local.toString()), + Domainpart.from(domain.toString()), + Resourcepart.from(resource.toString()) + )); + } catch (XmppStringprepException e) { + throw new IllegalArgumentException(e); + } + } + /** * Creates a bare JID with only the domain part, e.g. capulet.com *