Experimental support for transliteration

This commit is contained in:
M66B
2021-06-14 10:49:33 +02:00
parent 27a7c4e86b
commit f48a41128b
6 changed files with 81 additions and 5 deletions

View File

@@ -21,6 +21,8 @@ package eu.faircode.email;
import android.app.Person;
import android.content.Context;
import android.content.SharedPreferences;
import android.icu.text.Transliterator;
import android.os.Build;
import android.text.TextUtils;
import android.view.textclassifier.ConversationAction;
@@ -28,6 +30,9 @@ import android.view.textclassifier.ConversationActions;
import android.view.textclassifier.TextClassificationManager;
import android.view.textclassifier.TextClassifier;
import androidx.preference.PreferenceManager;
import java.text.Normalizer;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
@@ -82,6 +87,42 @@ public class TextHelper {
}
}
static boolean canTransliterate() {
if (!BuildConfig.DEBUG)
return false;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
return false;
try {
Transliterator.getInstance("Any-Latin");
return true;
} catch (Throwable ex) {
return false;
}
}
static String transliterate(Context context, String text) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
return text;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean notify_transliterate = prefs.getBoolean("notify_transliterate", false);
if (!notify_transliterate)
return text;
try {
Transliterator t = Transliterator.getInstance("Any-Latin");
text = t.transliterate(text);
String normalized = Normalizer.normalize(text, Normalizer.Form.NFD);
text = normalized.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
} catch (Throwable ex) {
Log.w(ex);
}
return text;
}
static ConversationActions getConversationActions(
Context context, String[] texts, boolean replies, boolean outgoing, long time) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)