diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-04-07 22:55:03 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-04-07 22:55:03 +0200 |
commit | 6b3e3a0831099c84def024a5bfe6036c2c53b04a (patch) | |
tree | 2e74f92ccc1a22494133247e6993de5cc4bd5aca /src/main/java/de/pixart | |
parent | 044018d607d7ebdddad94c09b9ada59ce0838be2 (diff) |
use scripts instead of blocks on Android >= N
Diffstat (limited to 'src/main/java/de/pixart')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/IrregularUnicodeBlockDetector.java | 54 |
1 files changed, 47 insertions, 7 deletions
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<Character.UnicodeBlock, List<String>> map(Jid jid) { + private static Map<Character.UnicodeBlock, List<String>> mapCompat(Jid jid) { Map<Character.UnicodeBlock, List<String>> map = new HashMap<>(); String local = jid.getLocal(); final int length = local.length(); @@ -118,16 +120,48 @@ public class IrregularUnicodeBlockDetector { return map; } - private static Set<String> eliminateFirstAndGetCodePoints(Map<Character.UnicodeBlock, List<String>> map) { - Character.UnicodeBlock block = Character.UnicodeBlock.BASIC_LATIN; + @TargetApi(Build.VERSION_CODES.N) + private static Map<Character.UnicodeScript, List<String>> map(Jid jid) { + Map<Character.UnicodeScript, List<String>> 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<String> 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<String> eliminateFirstAndGetCodePointsCompat(Map<Character.UnicodeBlock, List<String>> map) { + return eliminateFirstAndGetCodePoints(map, Character.UnicodeBlock.BASIC_LATIN); + } + + @TargetApi(Build.VERSION_CODES.N) + private static Set<String> eliminateFirstAndGetCodePoints(Map<Character.UnicodeScript, List<String>> map) { + return eliminateFirstAndGetCodePoints(map, Character.UnicodeScript.COMMON); + } + + private static <T> Set<String> eliminateFirstAndGetCodePoints(Map<T, List<String>> map, T defaultPick) { + T pick = defaultPick; int size = 0; - for (Map.Entry<Character.UnicodeBlock, List<String>> entry : map.entrySet()) { + for (Map.Entry<T, List<String>> 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<String> all = new HashSet<>(); for (List<String> codePoints : map.values()) { all.addAll(codePoints); @@ -141,7 +175,13 @@ public class IrregularUnicodeBlockDetector { if (pattern != null) { return pattern; } - pattern = create(eliminateFirstAndGetCodePoints(map(jid))); + Set<String> 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; } |