FairEmail/app/src/main/java/eu/faircode/email/WorkerFts.java

193 lines
7.6 KiB
Java
Raw Normal View History

2020-01-14 21:39:35 +00:00
package eu.faircode.email;
/*
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)
*/
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
2020-12-28 11:03:34 +00:00
import android.text.TextUtils;
2020-01-14 21:39:35 +00:00
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import java.io.File;
2020-03-10 11:20:27 +00:00
import java.io.FileNotFoundException;
2020-01-15 14:25:33 +00:00
import java.util.ArrayList;
2020-12-28 11:03:34 +00:00
import java.util.Collection;
2020-01-15 14:25:33 +00:00
import java.util.List;
2020-01-14 21:39:35 +00:00
import java.util.concurrent.TimeUnit;
2020-12-28 11:03:34 +00:00
import de.daslaboratorium.machinelearning.classifier.Classification;
import de.daslaboratorium.machinelearning.classifier.bayes.BayesClassifier;
2020-01-14 21:39:35 +00:00
import io.requery.android.database.sqlite.SQLiteDatabase;
2020-01-15 12:16:52 +00:00
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
2020-01-14 21:39:35 +00:00
public class WorkerFts extends Worker {
2020-12-28 11:03:34 +00:00
private static final int INDEX_DELAY = BuildConfig.DEBUG ? 3 : 30; // seconds
2020-10-31 07:13:07 +00:00
private static final int INDEX_BATCH_SIZE = 100;
2020-01-15 09:29:16 +00:00
2020-12-28 11:03:34 +00:00
private static BayesClassifier<String, String> classifier = new BayesClassifier<>();
2020-01-14 21:39:35 +00:00
public WorkerFts(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
Log.i("Instance " + getName());
}
@NonNull
@Override
public Result doWork() {
2020-01-15 12:16:52 +00:00
Thread.currentThread().setPriority(THREAD_PRIORITY_BACKGROUND);
2020-01-14 21:39:35 +00:00
try {
Log.i("FTS index");
int indexed = 0;
2020-01-15 14:25:33 +00:00
List<Long> ids = new ArrayList<>(INDEX_BATCH_SIZE);
2020-01-15 13:48:33 +00:00
DB db = DB.getInstance(getApplicationContext());
SQLiteDatabase sdb = FtsDbHelper.getInstance(getApplicationContext());
2020-03-26 12:29:43 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
2020-01-15 13:48:33 +00:00
try (Cursor cursor = db.message().getMessageFts()) {
while (cursor.moveToNext()) {
2020-03-26 12:29:43 +00:00
boolean fts = prefs.getBoolean("fts", false);
if (!fts)
break;
2020-01-15 14:25:33 +00:00
long id = cursor.getLong(0);
EntityMessage message = db.message().getMessage(id);
2020-01-15 13:48:33 +00:00
if (message != null)
try {
Log.i("FTS index=" + message.id);
2020-01-15 14:25:33 +00:00
2020-01-15 13:48:33 +00:00
File file = message.getFile(getApplicationContext());
2020-02-20 09:35:01 +00:00
String text = HtmlHelper.getFullText(file);
2020-03-26 12:29:43 +00:00
2020-12-28 11:03:34 +00:00
if (BuildConfig.DEBUG) {
EntityFolder folder = db.folder().getFolder(message.folder);
if (folder != null) {
// \\P{L}+
List<String> features = new ArrayList<>();
for (String word : text.trim().toLowerCase().split("\\W+")) {
if (word.matches(".*\\d.*"))
continue;
if (word.endsWith("."))
word = word.substring(0, word.length() - 1);
features.add(word);
}
Collection<Classification<String, String>> classifications = classifier.classifyDetailed(features);
for (Classification<String, String> classification : classifications)
Log.i("MMM folder=" + folder.name +
" classified=" + classification.getCategory() +
" probability=" + classification.getProbability() +
" features=" + TextUtils.join(", ", features.subList(0, Math.min(features.size(), 20))));
classifier.learn(EntityFolder.JUNK.equals(folder.type) ? "spam" : "ham", features);
}
}
2020-01-21 07:17:24 +00:00
try {
sdb.beginTransaction();
FtsDbHelper.insert(sdb, message, text);
sdb.setTransactionSuccessful();
} finally {
sdb.endTransaction();
}
2020-01-15 14:25:33 +00:00
2020-01-15 13:48:33 +00:00
indexed++;
2020-01-15 14:25:33 +00:00
ids.add(id);
if (ids.size() > INDEX_BATCH_SIZE)
markIndexed(db, ids);
2020-01-15 13:48:33 +00:00
} catch (Throwable ex) {
2020-03-10 11:20:27 +00:00
if (ex instanceof FileNotFoundException ||
ex instanceof OutOfMemoryError)
2020-02-14 10:37:39 +00:00
ids.add(id);
2020-01-15 13:48:33 +00:00
Log.e(ex);
}
2020-01-14 21:39:35 +00:00
}
2020-01-15 14:25:33 +00:00
markIndexed(db, ids);
2020-01-14 21:39:35 +00:00
}
Log.i("FTS indexed=" + indexed);
return Result.success();
} catch (Throwable ex) {
Log.e(ex);
return Result.failure();
}
}
2020-01-15 14:25:33 +00:00
private void markIndexed(DB db, List<Long> ids) {
try {
db.beginTransaction();
for (Long id : ids)
db.message().setMessageFts(id, true);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ids.clear();
}
2020-01-15 09:29:16 +00:00
static void init(Context context, boolean immediately) {
2020-01-14 21:39:35 +00:00
try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean fts = prefs.getBoolean("fts", true);
2020-01-16 17:21:03 +00:00
boolean pro = ActivityBilling.isPro(context);
if (fts && pro) {
2020-01-14 21:39:35 +00:00
Log.i("Queuing " + getName());
2020-01-15 09:29:16 +00:00
OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(WorkerFts.class);
if (!immediately)
builder.setInitialDelay(INDEX_DELAY, TimeUnit.SECONDS);
OneTimeWorkRequest workRequest = builder.build();
2020-01-14 21:39:35 +00:00
WorkManager.getInstance(context)
.enqueueUniqueWork(getName(), ExistingWorkPolicy.REPLACE, workRequest);
Log.i("Queued " + getName());
2020-01-15 11:07:45 +00:00
} else if (immediately)
cancel(context);
2020-01-14 21:39:35 +00:00
} catch (IllegalStateException ex) {
// https://issuetracker.google.com/issues/138465476
Log.w(ex);
}
}
2020-01-15 11:07:45 +00:00
static void cancel(Context context) {
2020-08-10 06:33:41 +00:00
try {
Log.i("Cancelling " + getName());
WorkManager.getInstance(context).cancelUniqueWork(getName());
Log.i("Cancelled " + getName());
} catch (IllegalStateException ex) {
Log.w(ex);
}
2020-01-15 11:07:45 +00:00
}
2020-01-14 21:39:35 +00:00
private static String getName() {
return WorkerFts.class.getSimpleName();
}
}