From 6b3e3a0831099c84def024a5bfe6036c2c53b04a Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 7 Apr 2018 22:55:03 +0200 Subject: use scripts instead of blocks on Android >= N --- .../utils/IrregularUnicodeBlockDetector.java | 54 +++++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/de/pixart/messenger/utils/IrregularUnicodeBlockDetector.java b/src/main/java/de/pixart/messenger/utils/IrregularUnicodeBlockDetector.java index df654eeb3..c1e4ba88b 100644 --- a/src/main/java/de/pixart/messenger/utils/IrregularUnicodeBlockDetector.java +++ b/src/main/java/de/pixart/messenger/utils/IrregularUnicodeBlockDetector.java @@ -29,7 +29,9 @@ package de.pixart.messenger.utils; +import android.annotation.TargetApi; import android.content.Context; +import android.os.Build; import android.support.annotation.ColorInt; import android.text.Spannable; import android.text.SpannableString; @@ -98,7 +100,7 @@ public class IrregularUnicodeBlockDetector { return builder; } - private static Map> map(Jid jid) { + private static Map> mapCompat(Jid jid) { Map> map = new HashMap<>(); String local = jid.getLocal(); final int length = local.length(); @@ -118,16 +120,48 @@ public class IrregularUnicodeBlockDetector { return map; } - private static Set eliminateFirstAndGetCodePoints(Map> map) { - Character.UnicodeBlock block = Character.UnicodeBlock.BASIC_LATIN; + @TargetApi(Build.VERSION_CODES.N) + 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.UnicodeScript script = Character.UnicodeScript.of(codePoint); + if (script != Character.UnicodeScript.COMMON) { + List codePoints; + if (map.containsKey(script)) { + codePoints = map.get(script); + } else { + codePoints = new ArrayList<>(); + map.put(script, codePoints); + } + codePoints.add(String.copyValueOf(Character.toChars(codePoint))); + } + offset += Character.charCount(codePoint); + } + return map; + } + + private static Set eliminateFirstAndGetCodePointsCompat(Map> map) { + return eliminateFirstAndGetCodePoints(map, Character.UnicodeBlock.BASIC_LATIN); + } + + @TargetApi(Build.VERSION_CODES.N) + private static Set eliminateFirstAndGetCodePoints(Map> map) { + return eliminateFirstAndGetCodePoints(map, Character.UnicodeScript.COMMON); + } + + private static Set eliminateFirstAndGetCodePoints(Map> map, T defaultPick) { + T pick = defaultPick; int size = 0; - for (Map.Entry> entry : map.entrySet()) { + for (Map.Entry> entry : map.entrySet()) { if (entry.getValue().size() > size) { size = entry.getValue().size(); - block = entry.getKey(); + pick = entry.getKey(); } } - map.remove(block); + map.remove(pick); Set all = new HashSet<>(); for (List codePoints : map.values()) { all.addAll(codePoints); @@ -141,7 +175,13 @@ public class IrregularUnicodeBlockDetector { if (pattern != null) { return pattern; } - pattern = create(eliminateFirstAndGetCodePoints(map(jid))); + Set codePoints; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + codePoints = eliminateFirstAndGetCodePointsCompat(mapCompat(jid)); + } else { + codePoints = eliminateFirstAndGetCodePoints(map(jid)); + } + pattern = create(codePoints); CACHE.put(jid, pattern); return pattern; } -- cgit v1.2.3