Detect message language

pull/177/head
M66B 5 years ago
parent d39235027b
commit 06998e5d6d

File diff suppressed because it is too large Load Diff

@ -375,6 +375,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private TextView tvSentTitle;
private TextView tvReceivedTitle;
private TextView tvSizeExTitle;
private TextView tvLanguageTitle;
private TextView tvSubmitter;
private TextView tvDeliveredTo;
@ -387,6 +388,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private TextView tvSent;
private TextView tvReceived;
private TextView tvSizeEx;
private TextView tvLanguage;
private TextView tvSubjectEx;
private TextView tvFlags;
@ -546,6 +548,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
tvSentTitle = vsBody.findViewById(R.id.tvSentTitle);
tvReceivedTitle = vsBody.findViewById(R.id.tvReceivedTitle);
tvSizeExTitle = vsBody.findViewById(R.id.tvSizeExTitle);
tvLanguageTitle = vsBody.findViewById(R.id.tvLanguageTitle);
tvSubmitter = vsBody.findViewById(R.id.tvSubmitter);
tvDeliveredTo = vsBody.findViewById(R.id.tvDeliveredTo);
@ -558,6 +561,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
tvSent = vsBody.findViewById(R.id.tvSent);
tvReceived = vsBody.findViewById(R.id.tvReceived);
tvSizeEx = vsBody.findViewById(R.id.tvSizeEx);
tvLanguage = vsBody.findViewById(R.id.tvLanguage);
tvSubjectEx = vsBody.findViewById(R.id.tvSubjectEx);
tvFlags = vsBody.findViewById(R.id.tvFlags);
@ -1159,6 +1163,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
tvSentTitle.setVisibility(View.GONE);
tvReceivedTitle.setVisibility(View.GONE);
tvSizeExTitle.setVisibility(View.GONE);
tvLanguageTitle.setVisibility(View.GONE);
tvSubmitter.setVisibility(View.GONE);
tvDeliveredTo.setVisibility(View.GONE);
@ -1171,6 +1176,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
tvSent.setVisibility(View.GONE);
tvReceived.setVisibility(View.GONE);
tvSizeEx.setVisibility(View.GONE);
tvLanguage.setVisibility(View.GONE);
tvSubjectEx.setVisibility(View.GONE);
tvFlags.setVisibility(View.GONE);
tvKeywordsEx.setVisibility(View.GONE);
@ -1416,6 +1423,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
.append(message.total == null ? "-" : Helper.humanReadableByteCount(message.total, true));
tvSizeEx.setText(size.toString());
tvLanguageTitle.setVisibility(show_addresses && message.language != null ? View.VISIBLE : View.GONE);
tvLanguage.setVisibility(show_addresses && message.language != null ? View.VISIBLE : View.GONE);
tvLanguage.setText(message.language == null ? null : new Locale(message.language).getDisplayLanguage());
tvSubjectEx.setVisibility(show_addresses ? View.VISIBLE : View.GONE);
tvSubjectEx.setText(message.subject);
if (subject_italic)
@ -4701,6 +4712,10 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
same = false;
log("content changed", next.id);
}
if (!Objects.equals(prev.language, next.language)) {
same = false;
log("language changed", next.id);
}
if (!Objects.equals(prev.plain_only, next.plain_only)) {
same = false;
log("plain_only changed", next.id);

@ -60,7 +60,7 @@ import io.requery.android.database.sqlite.SQLiteDatabase;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 149,
version = 150,
entities = {
EntityIdentity.class,
EntityAccount.class,
@ -1422,6 +1422,13 @@ public abstract class DB extends RoomDatabase {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `account` ADD COLUMN `leave_deleted` INTEGER NOT NULL DEFAULT 0");
}
})
.addMigrations(new Migration(149, 150) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `language` TEXT");
}
});
}

@ -538,6 +538,9 @@ public interface DaoMessage {
@Query("UPDATE message SET revisions = :revisions WHERE id = :id")
int setMessageRevisions(long id, Integer revisions);
@Query("UPDATE message SET language = :language WHERE id = :id")
int setMessageLanguage(long id, String language);
@Query("UPDATE message" +
" SET content = :content, fts = 0, preview = CASE WHEN :content THEN preview ELSE NULL END" +
" WHERE id = :id")

@ -135,6 +135,7 @@ public class EntityMessage implements Serializable {
public Integer attachments = 0; // performance
@NonNull
public Boolean content = false;
public String language = null; // classified
public Boolean plain_only = null;
public Integer encrypt = null;
public Integer ui_encrypt = null;
@ -311,6 +312,7 @@ public class EntityMessage implements Serializable {
Objects.equals(this.total, other.total) &&
Objects.equals(this.attachments, other.attachments) &&
this.content == other.content &&
Objects.equals(this.language, other.language) &&
Objects.equals(this.plain_only, other.plain_only) &&
Objects.equals(this.encrypt, other.encrypt) &&
Objects.equals(this.ui_encrypt, other.ui_encrypt) &&

@ -22,8 +22,12 @@ package eu.faircode.email;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Build;
import android.view.textclassifier.TextClassificationManager;
import android.view.textclassifier.TextLanguage;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.preference.PreferenceManager;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
@ -62,8 +66,13 @@ public class WorkerFts extends Worker {
List<Long> ids = new ArrayList<>(INDEX_BATCH_SIZE);
DB db = DB.getInstance(getApplicationContext());
SQLiteDatabase sdb = FtsDbHelper.getInstance(getApplicationContext());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
try (Cursor cursor = db.message().getMessageFts()) {
while (cursor.moveToNext()) {
boolean fts = prefs.getBoolean("fts", false);
if (!fts)
break;
long id = cursor.getLong(0);
EntityMessage message = db.message().getMessage(id);
if (message != null)
@ -72,6 +81,11 @@ public class WorkerFts extends Worker {
File file = message.getFile(getApplicationContext());
String text = HtmlHelper.getFullText(file);
if (BuildConfig.DEBUG &&
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
db.message().setMessageLanguage(message.id, getLanguage(text));
try {
sdb.beginTransaction();
FtsDbHelper.insert(sdb, message, text);
@ -103,6 +117,21 @@ public class WorkerFts extends Worker {
}
}
@RequiresApi(api = Build.VERSION_CODES.Q)
private String getLanguage(String text) {
TextClassificationManager tcm = (TextClassificationManager) getApplicationContext()
.getSystemService(Context.TEXT_CLASSIFICATION_SERVICE);
if (tcm == null)
return null;
TextLanguage.Request trequest = new TextLanguage.Request.Builder(text).build();
TextLanguage tlanguage = tcm.getTextClassifier().detectLanguage(trequest);
if (tlanguage.getLocaleHypothesisCount() > 0)
return tlanguage.getLocale(0).toLocale().getLanguage();
return null;
}
private void markIndexed(DB db, List<Long> ids) {
try {
db.beginTransaction();

@ -385,6 +385,31 @@
app:layout_constraintStart_toEndOf="@+id/barrier_addresses"
app:layout_constraintTop_toBottomOf="@id/tvReceived" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvLanguageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:labelFor="@+id/tvSizeEx"
android:text="@string/title_language"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSizeEx" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvLanguage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginEnd="6dp"
android:text="English"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/barrier_addresses"
app:layout_constraintTop_toBottomOf="@id/tvSizeEx" />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier_addresses"
android:layout_width="wrap_content"
@ -396,7 +421,7 @@
tvCcTitle,tvBccTitle,
tvIdentityTitle,
tvSentTitle,tvReceivedTitle,
tvSizeExTitle" />
tvSizeExTitle, tvLanguageTitle" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvSubjectEx"
@ -409,7 +434,7 @@
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSizeEx" />
app:layout_constraintTop_toBottomOf="@id/tvLanguage" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvFlags"

@ -227,6 +227,7 @@
<string name="title_advanced_prefix_once">Lisää aiheeseen etuliite vain kerran vastatessa ja edelleenlähetettäessä</string>
<string name="title_advanced_extended_reply">Käytä laajennettuja tunnistetietoja vastauksessa/edelleenlähetettäessä</string>
<string name="title_advanced_quote_reply">Lainaa vastattu teksti</string>
<string name="title_advanced_resize_reply">Muuta vastatun tekstin kuvien koot</string>
<string name="title_advanced_signature_location">Allekirjoituksen sijainti</string>
<string name="title_advanced_signature_end">Lisää allekirjoitus lainatun/edelleenlähetetyn viestin jälkeen</string>
<string name="title_advanced_plain_only">Lähetä muotoilemattomana tekstinä oletuksena</string>
@ -713,11 +714,19 @@
<string name="title_style_color">Väri</string>
<string name="title_style_clear">Poista muotoilu</string>
<string name="title_style_link">Lisää linkki</string>
<string name="title_add_image">Lisää kuva</string>
<string name="title_add_image_inline">Lisää</string>
<string name="title_add_image_attach">Liitä</string>
<string name="title_add_image_resize">Muuta kokoa</string>
<string name="title_add_resize_pixels">&lt; %1$d kuvapistettä</string>
<string name="title_add_image_select">Valitse tiedosto</string>
<string name="title_add_attachment">Lisää liite</string>
<string name="title_attachment_file">Liitä tiedosto</string>
<string name="title_attachment_photo">Ota kuva</string>
<string name="title_attachment_audio">Tallenna ääntä</string>
<string name="title_show_addresses">Näytä kopio/piilokopio</string>
<string name="title_send_dialog">Näytä lähetysasetukset</string>
<string name="title_image_dialog">Näytä kuva-asetukset</string>
<string name="title_media_toolbar">Mediatyökalupalkki</string>
<string name="title_insert_contact_group">Lisää yhteystietoryhmä</string>
<string name="title_insert_template">Lisää viestipohja</string>
@ -738,8 +747,9 @@
<string name="title_subject_reminder">Aihe puuttuu</string>
<string name="title_text_reminder">Viesti on tyhjä</string>
<string name="title_attachment_keywords">liite,liitteet,liitetty,liitetyt,sisällytetty,lisätty,liitin,sisällytin,lisäsin</string>
<string name="title_attachment_reminder">Oli tarkoituksenasi lisätä liite?</string>
<string name="title_attachment_reminder">Oliko tarkoituksenasi lisätä liite?</string>
<string name="title_attachments_missing">Kaikkia liitteitä ei ole ladattu</string>
<string name="title_dialog_hint">Tämä valintaikkuna voidaan ottaa uudelleen käyttöön yläreunan toimintopalkin kolmen pisteen valikosta</string>
<string name="title_draft_deleted">Luonnos hylätty</string>
<string name="title_draft_saved">Luonnos tallennettu</string>
<string name="title_ask_send_via">Lähetäänkö viesti vastaanottajalle %1$s identiteetillä %2$s?</string>

@ -696,6 +696,7 @@
<string name="title_sent">Inviato:</string>
<string name="title_received">Ricevuto:</string>
<string name="title_size">Dimensioni:</string>
<string name="title_language">Lingua:</string>
<string name="title_subject">Oggetto:</string>
<string name="title_attachment">Allegato:</string>
<string name="title_body_hint">Il tuo messaggio</string>

@ -696,6 +696,7 @@
<string name="title_sent">Verzonden:</string>
<string name="title_received">Ontvangen:</string>
<string name="title_size">Grootte:</string>
<string name="title_language">Taal:</string>
<string name="title_subject">Onderwerp:</string>
<string name="title_attachment">Bijlage:</string>
<string name="title_body_hint">Uw bericht</string>

@ -796,6 +796,7 @@
<string name="title_sent">Sent:</string>
<string name="title_received">Received:</string>
<string name="title_size">Size:</string>
<string name="title_language">Language:</string>
<string name="title_subject">Subject:</string>
<string name="title_attachment">Attachment:</string>
<string name="title_body_hint">Your message</string>

Loading…
Cancel
Save