From a4514091969cf6c255f7b89b368a9b32dd606c1f Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 16 Nov 2020 15:46:55 +0100 Subject: [PATCH] Refactoring --- .../java/eu/faircode/email/HtmlHelper.java | 26 +-------- .../java/eu/faircode/email/TextHelper.java | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/TextHelper.java diff --git a/app/src/main/java/eu/faircode/email/HtmlHelper.java b/app/src/main/java/eu/faircode/email/HtmlHelper.java index 156799a41b..d862c3c4db 100644 --- a/app/src/main/java/eu/faircode/email/HtmlHelper.java +++ b/app/src/main/java/eu/faircode/email/HtmlHelper.java @@ -56,9 +56,6 @@ import android.text.style.UnderlineSpan; import android.util.Base64; import android.util.Patterns; import android.view.View; -import android.view.textclassifier.TextClassificationManager; -import android.view.textclassifier.TextClassifier; -import android.view.textclassifier.TextLanguage; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -1686,26 +1683,9 @@ public class HtmlHelper { if (!language_detection) return null; - // Why not ML kit? https://developers.google.com/ml-kit/terms - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - TextClassificationManager tcm = - (TextClassificationManager) context.getSystemService(Context.TEXT_CLASSIFICATION_SERVICE); - if (tcm == null) - return null; - - String text = getPreview(body); - if (text == null) - return null; - - TextLanguage.Request trequest = new TextLanguage.Request.Builder(text).build(); - TextClassifier tc = tcm.getTextClassifier(); - TextLanguage tlanguage = tc.detectLanguage(trequest); - if (tlanguage.getLocaleHypothesisCount() > 0) - return tlanguage.getLocale(0).toLocale().getLanguage(); - } - - return null; + String text = getPreview(body); + Locale locale = TextHelper.detectLanguage(context, text); + return (locale == null ? null : locale.getLanguage()); } catch (Throwable ex) { Log.e(ex); return null; diff --git a/app/src/main/java/eu/faircode/email/TextHelper.java b/app/src/main/java/eu/faircode/email/TextHelper.java new file mode 100644 index 0000000000..14baa33fd0 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/TextHelper.java @@ -0,0 +1,55 @@ +package eu.faircode.email; + +import android.content.Context; +import android.os.Build; +import android.text.TextUtils; +import android.view.textclassifier.TextClassificationManager; +import android.view.textclassifier.TextClassifier; +import android.view.textclassifier.TextLanguage; + +import java.util.Locale; + +public class TextHelper { + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018-2020 by Marcel Bokhorst (M66B) +*/ + + static Locale detectLanguage(Context context, String text) { + // Why not ML kit? https://developers.google.com/ml-kit/terms + if (TextUtils.isEmpty(text)) + return null; + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) + return null; + + // https://issuetracker.google.com/issues/173337263 + TextClassificationManager tcm = + (TextClassificationManager) context.getSystemService(Context.TEXT_CLASSIFICATION_SERVICE); + if (tcm == null) + return null; + + TextLanguage.Request trequest = new TextLanguage.Request.Builder(text).build(); + TextClassifier tc = tcm.getTextClassifier(); + TextLanguage tlanguage = tc.detectLanguage(trequest); + if (tlanguage.getLocaleHypothesisCount() > 0) + return tlanguage.getLocale(0).toLocale(); + + return null; + } +}