From 6944c12186d2c3f00791a09f6fca89d593857b50 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 8 Mar 2018 14:02:48 +0100 Subject: [PATCH] display irregular unicode code points --- .../conversations/entities/Bookmark.java | 10 -- .../siacs/conversations/entities/Contact.java | 9 - .../conversations/entities/ListItem.java | 2 - .../ui/ContactDetailsActivity.java | 15 +- .../conversations/ui/TrustKeysActivity.java | 6 +- .../ui/adapter/ListItemAdapter.java | 8 +- .../utils/IrregularUnicodeBlockDetector.java | 160 ++++++++++++++++++ src/main/res/values/attrs.xml | 1 + src/main/res/values/colors.xml | 2 + src/main/res/values/themes.xml | 2 + 10 files changed, 177 insertions(+), 38 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/utils/IrregularUnicodeBlockDetector.java diff --git a/src/main/java/eu/siacs/conversations/entities/Bookmark.java b/src/main/java/eu/siacs/conversations/entities/Bookmark.java index 8e28fb4c8..e4d441ccc 100644 --- a/src/main/java/eu/siacs/conversations/entities/Bookmark.java +++ b/src/main/java/eu/siacs/conversations/entities/Bookmark.java @@ -63,16 +63,6 @@ public class Bookmark extends Element implements ListItem { } } - @Override - public String getDisplayJid() { - Jid jid = getJid(); - if (jid != null) { - return jid.toString(); - } else { - return getAttribute("jid"); //fallback if jid wasn't parsable - } - } - @Override public Jid getJid() { return this.getAttributeAsJid("jid"); diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java index d9ddc8c45..a2a698f81 100644 --- a/src/main/java/eu/siacs/conversations/entities/Contact.java +++ b/src/main/java/eu/siacs/conversations/entities/Contact.java @@ -127,15 +127,6 @@ public class Contact implements ListItem, Blockable { } } - @Override - public String getDisplayJid() { - if (jid != null) { - return jid.toString(); - } else { - return null; - } - } - public String getProfilePhoto() { return this.photoUri; } diff --git a/src/main/java/eu/siacs/conversations/entities/ListItem.java b/src/main/java/eu/siacs/conversations/entities/ListItem.java index 119ce19d2..d259aa4ab 100644 --- a/src/main/java/eu/siacs/conversations/entities/ListItem.java +++ b/src/main/java/eu/siacs/conversations/entities/ListItem.java @@ -10,8 +10,6 @@ import rocks.xmpp.addr.Jid; public interface ListItem extends Comparable { String getDisplayName(); - String getDisplayJid(); - Jid getJid(); List getTags(Context context); diff --git a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java index 7240d379b..f96574afa 100644 --- a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -38,6 +38,7 @@ import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; +import eu.siacs.conversations.utils.IrregularUnicodeBlockDetector; import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.xml.Namespace; @@ -129,8 +130,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.getDisplayJid())); + builder.setMessage(getString(R.string.add_phone_book_text, contact.getJid().toString())); builder.setNegativeButton(getString(R.string.cancel), null); builder.setPositiveButton(getString(R.string.add), addToPhonebook); builder.create().show(); @@ -235,9 +235,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp break; case R.id.action_delete_contact: builder.setTitle(getString(R.string.action_delete_contact)) - .setMessage( - getString(R.string.remove_contact_text, - contact.getDisplayJid())) + .setMessage(getString(R.string.remove_contact_text, contact.getJid().toString())) .setPositiveButton(getString(R.string.delete), removeFromRoster).create().show(); break; @@ -386,12 +384,7 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp } } - if (contact.getPresences().size() > 1) { - binding.detailsContactjid.setText(contact.getDisplayJid() + " (" - + contact.getPresences().size() + ")"); - } else { - binding.detailsContactjid.setText(contact.getDisplayJid()); - } + binding.detailsContactjid.setText(IrregularUnicodeBlockDetector.style(this,contact.getJid())); String account; if (Config.DOMAIN_LOCK != null) { account = contact.getAccount().getJid().getLocal(); diff --git a/src/main/java/eu/siacs/conversations/ui/TrustKeysActivity.java b/src/main/java/eu/siacs/conversations/ui/TrustKeysActivity.java index 059a9d54f..b37b7292b 100644 --- a/src/main/java/eu/siacs/conversations/ui/TrustKeysActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/TrustKeysActivity.java @@ -35,6 +35,7 @@ import eu.siacs.conversations.databinding.KeysCardBinding; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.utils.CryptoHelper; +import eu.siacs.conversations.utils.IrregularUnicodeBlockDetector; import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.xmpp.OnKeyStatusUpdated; import rocks.xmpp.addr.Jid; @@ -195,9 +196,8 @@ public class TrustKeysActivity extends OmemoActivity implements OnKeyStatusUpdat for (Map.Entry> entry : foreignKeysToTrust.entrySet()) { hasForeignKeys = true; KeysCardBinding keysCardBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.keys_card, binding.foreignKeys,false); - //final LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.keys_card, foreignKeys, false); final Jid jid = entry.getKey(); - keysCardBinding.foreignKeysTitle.setText(jid.toString()); + keysCardBinding.foreignKeysTitle.setText(IrregularUnicodeBlockDetector.style(this,jid)); keysCardBinding.foreignKeysTitle.setOnClickListener(v -> switchToContactDetails(mAccount.getRoster().getContact(jid))); final Map fingerprints = entry.getValue(); for (final String fingerprint : fingerprints.keySet()) { @@ -397,7 +397,7 @@ public class TrustKeysActivity extends OmemoActivity implements OnKeyStatusUpdat fingerprint, FingerprintStatus.createActive(ownKeysToTrust.get(fingerprint))); } - List acceptedTargets = mConversation == null ? new ArrayList() : mConversation.getAcceptedCryptoTargets(); + List acceptedTargets = mConversation == null ? new ArrayList<>() : mConversation.getAcceptedCryptoTargets(); synchronized (this.foreignKeysToTrust) { for (Map.Entry> entry : foreignKeysToTrust.entrySet()) { Jid jid = entry.getKey(); diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java index 3d62ca850..b00cefbe9 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ListItemAdapter.java @@ -1,6 +1,5 @@ package eu.siacs.conversations.ui.adapter; -import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; import android.databinding.DataBindingUtil; @@ -27,8 +26,11 @@ import eu.siacs.conversations.databinding.ContactBinding; import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.ui.SettingsActivity; import eu.siacs.conversations.ui.XmppActivity; +import eu.siacs.conversations.ui.util.Color; import eu.siacs.conversations.utils.EmojiWrapper; +import eu.siacs.conversations.utils.IrregularUnicodeBlockDetector; import eu.siacs.conversations.utils.UIHelper; +import rocks.xmpp.addr.Jid; public class ListItemAdapter extends ArrayAdapter { @@ -108,10 +110,10 @@ public class ListItemAdapter extends ArrayAdapter { viewHolder.tags.addView(tv); } } - final String jid = item.getDisplayJid(); + final Jid jid = item.getJid(); if (jid != null) { viewHolder.jid.setVisibility(View.VISIBLE); - viewHolder.jid.setText(jid); + viewHolder.jid.setText(IrregularUnicodeBlockDetector.style(activity, jid)); } else { viewHolder.jid.setVisibility(View.GONE); } diff --git a/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeBlockDetector.java b/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeBlockDetector.java new file mode 100644 index 000000000..1aaf2021a --- /dev/null +++ b/src/main/java/eu/siacs/conversations/utils/IrregularUnicodeBlockDetector.java @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2018, Daniel Gultsch All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package eu.siacs.conversations.utils; + +import android.content.Context; +import android.support.annotation.ColorInt; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.SpannableStringBuilder; +import android.text.style.ForegroundColorSpan; +import android.util.LruCache; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import eu.siacs.conversations.R; +import eu.siacs.conversations.ui.util.Color; +import rocks.xmpp.addr.Jid; + +public class IrregularUnicodeBlockDetector { + + private static final Map NORMALIZATION_MAP; + + static { + Map temp = new HashMap<>(); + temp.put(Character.UnicodeBlock.LATIN_1_SUPPLEMENT, Character.UnicodeBlock.BASIC_LATIN); + NORMALIZATION_MAP = Collections.unmodifiableMap(temp); + } + + private static Character.UnicodeBlock normalize(Character.UnicodeBlock in) { + if (NORMALIZATION_MAP.containsKey(in)) { + return NORMALIZATION_MAP.get(in); + } else { + return in; + } + } + + private static final LruCache CACHE = new LruCache<>(100); + + public static Spannable style(Context context, Jid jid) { + return style(jid, Color.get(context, R.attr.color_warning)); + } + + private static Spannable style(Jid jid, @ColorInt int color) { + SpannableStringBuilder builder = new SpannableStringBuilder(); + if (jid.getLocal() != null) { + SpannableString local = new SpannableString(jid.getLocal()); + Matcher matcher = find(jid).matcher(local); + while (matcher.find()) { + if (matcher.start() < matcher.end()) { + local.setSpan(new ForegroundColorSpan(color), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } + builder.append(local); + builder.append('@'); + } + if (jid.getDomain() != null) { + builder.append(jid.getDomain()); + } + if (builder.length() != 0 && jid.getResource() != null) { + builder.append('/'); + builder.append(jid.getResource()); + } + return builder; + } + + private static Map> map(Jid jid) { + Map> map = new HashMap<>(); + String local = jid.getLocal(); + final int length = local.length(); + for (int offset = 0; offset < length; ) { + final int codePoint = local.codePointAt(offset); + Character.UnicodeBlock block = normalize(Character.UnicodeBlock.of(codePoint)); + List codePoints; + if (map.containsKey(block)) { + codePoints = map.get(block); + } else { + codePoints = new ArrayList<>(); + map.put(block, codePoints); + } + codePoints.add(String.copyValueOf(Character.toChars(codePoint))); + offset += Character.charCount(codePoint); + } + return map; + } + + private static Set eliminateFirstAndGetCodePoints(Map> map) { + Character.UnicodeBlock block = Character.UnicodeBlock.BASIC_LATIN; + int size = 0; + for (Map.Entry> entry : map.entrySet()) { + if (entry.getValue().size() > size) { + size = entry.getValue().size(); + block = entry.getKey(); + } + } + map.remove(block); + Set all = new HashSet<>(); + for (List codePoints : map.values()) { + all.addAll(codePoints); + } + return all; + } + + private static Pattern find(Jid jid) { + synchronized (CACHE) { + Pattern pattern = CACHE.get(jid); + if (pattern != null) { + return pattern; + } + pattern = create(eliminateFirstAndGetCodePoints(map(jid))); + CACHE.put(jid, pattern); + return pattern; + } + } + + private static Pattern create(Set codePoints) { + final StringBuilder pattern = new StringBuilder(); + for (String codePoint : codePoints) { + if (pattern.length() != 0) { + pattern.append('|'); + } + pattern.append(Pattern.quote(codePoint)); + } + return Pattern.compile(pattern.toString()); + } +} diff --git a/src/main/res/values/attrs.xml b/src/main/res/values/attrs.xml index 775ed9e0c..4fbdd12cb 100644 --- a/src/main/res/values/attrs.xml +++ b/src/main/res/values/attrs.xml @@ -8,6 +8,7 @@ + diff --git a/src/main/res/values/colors.xml b/src/main/res/values/colors.xml index 8e6ffe7f6..1c8d3aa37 100644 --- a/src/main/res/values/colors.xml +++ b/src/main/res/values/colors.xml @@ -19,6 +19,8 @@ #ff424242 #ff282828 #fff44336 + #ffd50000 + #ffff8a80 #ffc62828 #ffff9800 #ff259b24 diff --git a/src/main/res/values/themes.xml b/src/main/res/values/themes.xml index d3f662e60..ea20a7c4d 100644 --- a/src/main/res/values/themes.xml +++ b/src/main/res/values/themes.xml @@ -8,6 +8,7 @@ @color/grey50 @color/grey200 + @color/red_a700 true @color/accent @@ -84,6 +85,7 @@ @color/grey800 @color/grey900 + @color/red_a100 true @color/accent