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

173 lines
5.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/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2020-01-14 21:39:35 +00:00
*/
2022-02-11 09:44:59 +00:00
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
2020-01-14 21:39:35 +00:00
import android.content.Context;
import android.content.SharedPreferences;
2022-02-15 09:17:38 +00:00
import android.database.Cursor;
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-01-15 14:25:33 +00:00
import java.util.ArrayList;
import java.util.List;
2020-01-14 21:39:35 +00:00
import java.util.concurrent.TimeUnit;
import io.requery.android.database.sqlite.SQLiteDatabase;
public class WorkerFts extends Worker {
2021-01-02 14:57:24 +00:00
private static final int INDEX_DELAY = 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-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");
2020-12-28 18:10:06 +00:00
Context context = getApplicationContext();
2020-01-14 21:39:35 +00:00
2021-09-11 13:12:56 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2022-03-09 20:11:46 +00:00
boolean checkpoints = prefs.getBoolean("sqlite_checkpoints", false);
2021-09-11 12:06:28 +00:00
2020-01-14 21:39:35 +00:00
int indexed = 0;
2020-01-15 14:25:33 +00:00
List<Long> ids = new ArrayList<>(INDEX_BATCH_SIZE);
2020-12-28 18:10:06 +00:00
DB db = DB.getInstance(context);
2021-02-25 13:28:58 +00:00
2020-12-28 18:10:06 +00:00
SQLiteDatabase sdb = FtsDbHelper.getInstance(context);
2021-04-06 09:58:54 +00:00
2022-02-15 09:17:38 +00:00
Cursor cursor = db.message().getMessageFts();
while (cursor != null && cursor.moveToNext())
2021-04-06 09:58:54 +00:00
try {
2022-02-15 09:17:38 +00:00
long id = cursor.getLong(0);
2021-04-06 09:58:54 +00:00
Log.i("FTS index=" + id);
ids.add(id);
EntityMessage message = db.message().getMessage(id);
if (message == null) {
Log.i("FTS gone");
continue;
}
File file = message.getFile(context);
String text = HtmlHelper.getFullText(file);
2021-06-04 17:59:08 +00:00
if (text == null)
text = "";
2021-04-06 09:58:54 +00:00
boolean fts = prefs.getBoolean("fts", false);
if (!fts)
break;
2020-12-28 18:10:06 +00:00
try {
2021-04-06 09:58:54 +00:00
sdb.beginTransaction();
FtsDbHelper.insert(sdb, message, text);
sdb.setTransactionSuccessful();
} finally {
sdb.endTransaction();
2020-12-28 18:10:06 +00:00
}
2021-04-06 09:58:54 +00:00
indexed++;
if (ids.size() > INDEX_BATCH_SIZE)
markIndexed(db, ids);
} catch (Throwable ex) {
Log.e(ex);
}
markIndexed(db, ids);
2020-01-14 21:39:35 +00:00
2021-09-11 13:12:56 +00:00
if (checkpoints)
DB.checkpoint(context);
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();
}
}