mirror of
https://github.com/M66B/FairEmail.git
synced 2025-01-01 12:44:42 +00:00
Refactoring
This commit is contained in:
parent
cf05e9e683
commit
a451409196
2 changed files with 58 additions and 23 deletions
|
@ -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;
|
||||
|
|
55
app/src/main/java/eu/faircode/email/TextHelper.java
Normal file
55
app/src/main/java/eu/faircode/email/TextHelper.java
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue