IrregularUnicodeDetector.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.utils;
 31
 32import android.annotation.TargetApi;
 33import android.content.Context;
 34import android.os.Build;
 35import android.support.annotation.ColorInt;
 36import android.text.Spannable;
 37import android.text.SpannableString;
 38import android.text.SpannableStringBuilder;
 39import android.text.style.ForegroundColorSpan;
 40import android.util.LruCache;
 41
 42import java.util.ArrayList;
 43import java.util.Collections;
 44import java.util.HashMap;
 45import java.util.HashSet;
 46import java.util.List;
 47import java.util.Map;
 48import java.util.Set;
 49import java.util.regex.Matcher;
 50import java.util.regex.Pattern;
 51
 52import eu.siacs.conversations.R;
 53import eu.siacs.conversations.ui.util.Color;
 54import rocks.xmpp.addr.Jid;
 55
 56public class IrregularUnicodeDetector {
 57
 58	private static final Map<Character.UnicodeBlock, Character.UnicodeBlock> NORMALIZATION_MAP;
 59	private static final LruCache<Jid, PatternTuple> CACHE = new LruCache<>(4096);
 60
 61	static {
 62		Map<Character.UnicodeBlock, Character.UnicodeBlock> temp = new HashMap<>();
 63		temp.put(Character.UnicodeBlock.LATIN_1_SUPPLEMENT, Character.UnicodeBlock.BASIC_LATIN);
 64		NORMALIZATION_MAP = Collections.unmodifiableMap(temp);
 65	}
 66
 67	private static Character.UnicodeBlock normalize(Character.UnicodeBlock in) {
 68		if (NORMALIZATION_MAP.containsKey(in)) {
 69			return NORMALIZATION_MAP.get(in);
 70		} else {
 71			return in;
 72		}
 73	}
 74
 75	public static Spannable style(Context context, Jid jid) {
 76		return style(jid, Color.get(context, R.attr.color_warning));
 77	}
 78
 79	private static Spannable style(Jid jid, @ColorInt int color) {
 80		PatternTuple patternTuple = find(jid);
 81		SpannableStringBuilder builder = new SpannableStringBuilder();
 82		if (jid.getLocal() != null && patternTuple.local != null) {
 83			SpannableString local = new SpannableString(jid.getLocal());
 84			colorize(local, patternTuple.local, color);
 85			builder.append(local);
 86			builder.append('@');
 87		}
 88		if (jid.getDomain() != null) {
 89			String[] labels = jid.getDomain().split("\\.");
 90			for (int i = 0; i < labels.length; ++i) {
 91				SpannableString spannableString = new SpannableString(labels[i]);
 92				colorize(spannableString, patternTuple.domain.get(i), color);
 93				if (i != 0) {
 94					builder.append('.');
 95				}
 96				builder.append(spannableString);
 97			}
 98		}
 99		if (builder.length() != 0 && jid.getResource() != null) {
100			builder.append('/');
101			builder.append(jid.getResource());
102		}
103		return builder;
104	}
105
106	private static void colorize(SpannableString spannableString, Pattern pattern, @ColorInt int color) {
107		Matcher matcher = pattern.matcher(spannableString);
108		while (matcher.find()) {
109			if (matcher.start() < matcher.end()) {
110				spannableString.setSpan(new ForegroundColorSpan(color), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
111			}
112		}
113	}
114
115	private static Map<Character.UnicodeBlock, List<String>> mapCompat(String word) {
116		Map<Character.UnicodeBlock, List<String>> map = new HashMap<>();
117		final int length = word.length();
118		for (int offset = 0; offset < length; ) {
119			final int codePoint = word.codePointAt(offset);
120			Character.UnicodeBlock block = normalize(Character.UnicodeBlock.of(codePoint));
121			List<String> codePoints;
122			if (map.containsKey(block)) {
123				codePoints = map.get(block);
124			} else {
125				codePoints = new ArrayList<>();
126				map.put(block, codePoints);
127			}
128			codePoints.add(String.copyValueOf(Character.toChars(codePoint)));
129			offset += Character.charCount(codePoint);
130		}
131		return map;
132	}
133
134	@TargetApi(Build.VERSION_CODES.N)
135	private static Map<Character.UnicodeScript, List<String>> map(String word) {
136		Map<Character.UnicodeScript, List<String>> map = new HashMap<>();
137		final int length = word.length();
138		for (int offset = 0; offset < length; ) {
139			final int codePoint = word.codePointAt(offset);
140			Character.UnicodeScript script = Character.UnicodeScript.of(codePoint);
141			if (script != Character.UnicodeScript.COMMON) {
142				List<String> codePoints;
143				if (map.containsKey(script)) {
144					codePoints = map.get(script);
145				} else {
146					codePoints = new ArrayList<>();
147					map.put(script, codePoints);
148				}
149				codePoints.add(String.copyValueOf(Character.toChars(codePoint)));
150			}
151			offset += Character.charCount(codePoint);
152		}
153		return map;
154	}
155
156	private static Set<String> eliminateFirstAndGetCodePointsCompat(Map<Character.UnicodeBlock, List<String>> map) {
157		return eliminateFirstAndGetCodePoints(map, Character.UnicodeBlock.BASIC_LATIN);
158	}
159
160	@TargetApi(Build.VERSION_CODES.N)
161	private static Set<String> eliminateFirstAndGetCodePoints(Map<Character.UnicodeScript, List<String>> map) {
162		return eliminateFirstAndGetCodePoints(map, Character.UnicodeScript.COMMON);
163	}
164
165	private static <T> Set<String> eliminateFirstAndGetCodePoints(Map<T, List<String>> map, T defaultPick) {
166		T pick = defaultPick;
167		int size = 0;
168		for (Map.Entry<T, List<String>> entry : map.entrySet()) {
169			if (entry.getValue().size() > size) {
170				size = entry.getValue().size();
171				pick = entry.getKey();
172			}
173		}
174		map.remove(pick);
175		Set<String> all = new HashSet<>();
176		for (List<String> codePoints : map.values()) {
177			all.addAll(codePoints);
178		}
179		return all;
180	}
181
182	private static Set<String> findIrregularCodePoints(String word) {
183		Set<String> codePoints;
184		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
185			codePoints = eliminateFirstAndGetCodePointsCompat(mapCompat(word));
186		} else {
187			codePoints = eliminateFirstAndGetCodePoints(map(word));
188		}
189		return codePoints;
190	}
191
192	private static PatternTuple find(Jid jid) {
193		synchronized (CACHE) {
194			PatternTuple pattern = CACHE.get(jid);
195			if (pattern != null) {
196				return pattern;
197			}
198			;
199			pattern = PatternTuple.of(jid);
200			CACHE.put(jid, pattern);
201			return pattern;
202		}
203	}
204
205	private static Pattern create(Set<String> codePoints) {
206		final StringBuilder pattern = new StringBuilder();
207		for (String codePoint : codePoints) {
208			if (pattern.length() != 0) {
209				pattern.append('|');
210			}
211			pattern.append(Pattern.quote(codePoint));
212		}
213		return Pattern.compile(pattern.toString());
214	}
215
216	private static class PatternTuple {
217		private final Pattern local;
218		private final List<Pattern> domain;
219
220		private PatternTuple(Pattern local, List<Pattern> domain) {
221			this.local = local;
222			this.domain = domain;
223		}
224
225		private static PatternTuple of(Jid jid) {
226			final Pattern localPattern;
227			if (jid.getLocal() != null) {
228				localPattern = create(findIrregularCodePoints(jid.getLocal()));
229			} else {
230				localPattern = null;
231			}
232			String domain = jid.getDomain();
233			final List<Pattern> domainPatterns = new ArrayList<>();
234			if (domain != null) {
235				for (String label : domain.split("\\.")) {
236					domainPatterns.add(create(findIrregularCodePoints(label)));
237				}
238			}
239			return new PatternTuple(localPattern, domainPatterns);
240		}
241	}
242}