2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2023-11-17 09:07:27 +00:00
|
|
|
/*
|
|
|
|
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-2023 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2021-08-07 15:06:11 +00:00
|
|
|
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
|
|
|
|
|
2021-09-04 18:16:05 +00:00
|
|
|
import android.app.ActivityManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.content.Context;
|
2018-12-23 18:20:08 +00:00
|
|
|
import android.content.SharedPreferences;
|
2018-08-24 09:20:14 +00:00
|
|
|
import android.database.Cursor;
|
2022-09-26 07:01:55 +00:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2022-08-31 08:23:24 +00:00
|
|
|
import android.database.sqlite.SQLiteDatabaseCorruptException;
|
2020-06-25 07:14:05 +00:00
|
|
|
import android.net.Uri;
|
2020-07-12 08:17:50 +00:00
|
|
|
import android.os.Build;
|
2018-08-06 16:22:01 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-05-31 06:57:23 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2020-04-14 06:32:54 +00:00
|
|
|
import androidx.lifecycle.Observer;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
import androidx.room.Database;
|
2020-01-18 19:05:24 +00:00
|
|
|
import androidx.room.DatabaseConfiguration;
|
2019-05-31 06:57:23 +00:00
|
|
|
import androidx.room.InvalidationTracker;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.room.Room;
|
|
|
|
import androidx.room.RoomDatabase;
|
|
|
|
import androidx.room.TypeConverter;
|
|
|
|
import androidx.room.TypeConverters;
|
|
|
|
import androidx.sqlite.db.SupportSQLiteDatabase;
|
|
|
|
|
2018-08-07 16:25:57 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2019-03-14 09:12:19 +00:00
|
|
|
import java.io.File;
|
2020-04-14 08:14:58 +00:00
|
|
|
import java.lang.reflect.Field;
|
2018-08-07 16:25:57 +00:00
|
|
|
import java.util.ArrayList;
|
2021-12-13 16:49:30 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
2021-02-25 13:28:58 +00:00
|
|
|
import java.util.Date;
|
2022-01-09 12:57:07 +00:00
|
|
|
import java.util.HashMap;
|
2018-08-07 16:25:57 +00:00
|
|
|
import java.util.List;
|
2020-04-14 08:14:58 +00:00
|
|
|
import java.util.Map;
|
2021-08-13 14:56:52 +00:00
|
|
|
import java.util.Objects;
|
2019-05-31 06:57:23 +00:00
|
|
|
import java.util.Set;
|
2023-01-05 10:17:00 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
2018-08-07 16:25:57 +00:00
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// https://developer.android.com/topic/libraries/architecture/room.html
|
|
|
|
|
|
|
|
@Database(
|
2023-11-10 09:03:28 +00:00
|
|
|
version = 285,
|
2018-08-02 13:33:06 +00:00
|
|
|
entities = {
|
|
|
|
EntityIdentity.class,
|
|
|
|
EntityAccount.class,
|
|
|
|
EntityFolder.class,
|
|
|
|
EntityMessage.class,
|
2018-08-03 12:07:51 +00:00
|
|
|
EntityAttachment.class,
|
2018-08-27 07:06:03 +00:00
|
|
|
EntityOperation.class,
|
2019-02-13 09:24:06 +00:00
|
|
|
EntityContact.class,
|
2019-12-03 12:59:27 +00:00
|
|
|
EntityCertificate.class,
|
2018-08-27 07:06:03 +00:00
|
|
|
EntityAnswer.class,
|
2019-01-17 13:29:35 +00:00
|
|
|
EntityRule.class,
|
2021-09-24 11:36:06 +00:00
|
|
|
EntitySearch.class,
|
2023-01-17 22:05:56 +00:00
|
|
|
EntityLog.class
|
2019-06-07 08:14:44 +00:00
|
|
|
},
|
|
|
|
views = {
|
2020-01-22 18:53:17 +00:00
|
|
|
TupleAccountView.class,
|
|
|
|
TupleIdentityView.class,
|
2020-11-27 18:09:14 +00:00
|
|
|
TupleFolderView.class
|
2018-08-08 10:22:12 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@TypeConverters({DB.Converters.class})
|
|
|
|
public abstract class DB extends RoomDatabase {
|
|
|
|
public abstract DaoAccount account();
|
|
|
|
|
2019-05-31 06:57:23 +00:00
|
|
|
public abstract DaoIdentity identity();
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
public abstract DaoFolder folder();
|
|
|
|
|
|
|
|
public abstract DaoMessage message();
|
|
|
|
|
2018-08-03 12:07:51 +00:00
|
|
|
public abstract DaoAttachment attachment();
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
public abstract DaoOperation operation();
|
|
|
|
|
2019-02-13 09:24:06 +00:00
|
|
|
public abstract DaoContact contact();
|
|
|
|
|
2019-12-03 12:59:27 +00:00
|
|
|
public abstract DaoCertificate certificate();
|
|
|
|
|
2018-08-27 07:06:03 +00:00
|
|
|
public abstract DaoAnswer answer();
|
|
|
|
|
2019-01-17 13:29:35 +00:00
|
|
|
public abstract DaoRule rule();
|
|
|
|
|
2021-09-24 11:36:06 +00:00
|
|
|
public abstract DaoSearch search();
|
|
|
|
|
2018-09-03 19:11:16 +00:00
|
|
|
public abstract DaoLog log();
|
|
|
|
|
2021-04-25 14:13:26 +00:00
|
|
|
private static int sPid;
|
2021-08-13 14:56:52 +00:00
|
|
|
private static Context sContext;
|
2018-08-02 13:33:06 +00:00
|
|
|
private static DB sInstance;
|
|
|
|
|
2022-06-18 10:18:35 +00:00
|
|
|
static final String DB_NAME = "fairemail";
|
2021-09-11 15:48:48 +00:00
|
|
|
static final int DEFAULT_QUERY_THREADS = 4; // AndroidX default thread count: 4
|
2023-01-07 14:45:16 +00:00
|
|
|
static final int DEFAULT_CACHE_SIZE = 20; // percentage of memory class
|
2022-09-25 06:20:46 +00:00
|
|
|
private static final int DB_JOURNAL_SIZE_LIMIT = 1048576; // requery/sqlite-android default
|
2020-02-01 10:40:29 +00:00
|
|
|
private static final int DB_CHECKPOINT = 1000; // requery/sqlite-android default
|
2020-01-20 19:24:08 +00:00
|
|
|
|
2023-01-05 10:17:00 +00:00
|
|
|
private static ExecutorService executor =
|
2023-01-13 16:38:41 +00:00
|
|
|
Helper.getBackgroundExecutor(0, "db");
|
2023-01-05 10:17:00 +00:00
|
|
|
|
2020-02-01 10:40:29 +00:00
|
|
|
private static final String[] DB_TABLES = new String[]{
|
2021-09-24 11:36:06 +00:00
|
|
|
"identity", "account", "folder", "message", "attachment", "operation", "contact", "certificate", "answer", "rule", "search", "log"};
|
2020-01-22 15:18:21 +00:00
|
|
|
|
2021-12-13 16:49:30 +00:00
|
|
|
private static final List<String> DB_PRAGMAS = Collections.unmodifiableList(Arrays.asList(
|
2021-12-01 07:24:36 +00:00
|
|
|
"synchronous", "journal_mode",
|
|
|
|
"wal_checkpoint", "wal_autocheckpoint", "journal_size_limit",
|
|
|
|
"page_count", "page_size", "max_page_count", "freelist_count",
|
|
|
|
"cache_size", "cache_spill",
|
|
|
|
"soft_heap_limit", "hard_heap_limit", "mmap_size",
|
2022-09-24 11:00:18 +00:00
|
|
|
"foreign_keys", "auto_vacuum",
|
2023-01-19 21:00:37 +00:00
|
|
|
"recursive_triggers",
|
2022-09-24 11:00:18 +00:00
|
|
|
"compile_options"
|
2021-12-13 16:49:30 +00:00
|
|
|
));
|
2021-12-01 07:24:36 +00:00
|
|
|
|
2020-01-20 19:24:08 +00:00
|
|
|
@Override
|
|
|
|
public void init(@NonNull DatabaseConfiguration configuration) {
|
2022-08-31 08:23:24 +00:00
|
|
|
File dbfile = configuration.context.getDatabasePath(DB_NAME);
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(configuration.context);
|
|
|
|
boolean sqlite_integrity_check = prefs.getBoolean("sqlite_integrity_check", true);
|
|
|
|
|
|
|
|
// https://www.sqlite.org/pragma.html#pragma_integrity_check
|
|
|
|
if (sqlite_integrity_check && dbfile.exists()) {
|
2022-09-24 05:59:57 +00:00
|
|
|
String check = (Helper.isRedmiNote() || Helper.isOnePlus() || Helper.isOppo() || BuildConfig.DEBUG
|
2022-08-31 08:23:24 +00:00
|
|
|
? "integrity_check" : "quick_check");
|
|
|
|
try (SQLiteDatabase db = SQLiteDatabase.openDatabase(dbfile.getPath(), null, SQLiteDatabase.OPEN_READWRITE)) {
|
|
|
|
Log.i("PRAGMA " + check);
|
|
|
|
try (Cursor cursor = db.rawQuery("PRAGMA " + check + ";", null)) {
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
String line = cursor.getString(0);
|
|
|
|
if ("ok".equals(line))
|
|
|
|
Log.i("PRAGMA " + check + "=" + line);
|
|
|
|
else
|
|
|
|
Log.e("PRAGMA " + check + "=" + line);
|
2020-01-21 07:16:32 +00:00
|
|
|
}
|
2020-01-20 19:24:08 +00:00
|
|
|
}
|
2022-08-31 08:23:24 +00:00
|
|
|
} catch (SQLiteDatabaseCorruptException ex) {
|
|
|
|
Log.e(ex);
|
2023-11-16 10:10:34 +00:00
|
|
|
Helper.secureDelete(dbfile);
|
2023-05-19 19:28:22 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
/*
|
|
|
|
java.lang.String, java.lang.String, android.os.Bundle)' on a null object reference
|
|
|
|
at android.provider.Settings$NameValueCache.getStringForUser(Settings.java:3002)
|
|
|
|
at android.provider.Settings$Global.getStringForUser(Settings.java:16253)
|
|
|
|
at android.provider.Settings$Global.getString(Settings.java:16241)
|
|
|
|
at android.database.sqlite.SQLiteCompatibilityWalFlags.initIfNeeded(SQLiteCompatibilityWalFlags.java:105)
|
|
|
|
at android.database.sqlite.SQLiteCompatibilityWalFlags.isLegacyCompatibilityWalEnabled(SQLiteCompatibilityWalFlags.java:57)
|
|
|
|
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:321)
|
|
|
|
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:788)
|
|
|
|
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:737)
|
|
|
|
at eu.faircode.email.DB.init(SourceFile:61)
|
|
|
|
at androidx.room.RoomDatabase$Builder.build(SourceFile:274)
|
|
|
|
at eu.faircode.email.DB.getInstance(SourceFile:106)
|
|
|
|
at eu.faircode.email.DB.setupViewInvalidation(SourceFile:1)
|
|
|
|
at eu.faircode.email.ApplicationEx.onCreate(SourceFile:140)
|
|
|
|
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1229)
|
|
|
|
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6719)
|
|
|
|
*/
|
2022-08-31 08:23:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.sqlite.org/pragma.html#pragma_wal_autocheckpoint
|
|
|
|
if (BuildConfig.DEBUG && dbfile.exists()) {
|
|
|
|
try (SQLiteDatabase db = SQLiteDatabase.openDatabase(dbfile.getPath(), null, SQLiteDatabase.OPEN_READWRITE)) {
|
|
|
|
Log.i("Set PRAGMA wal_autocheckpoint=" + DB_CHECKPOINT);
|
|
|
|
try (Cursor cursor = db.rawQuery("PRAGMA wal_autocheckpoint=" + DB_CHECKPOINT + ";", null)) {
|
|
|
|
cursor.moveToNext(); // required
|
|
|
|
}
|
2023-05-19 19:28:22 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2020-01-20 19:24:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
super.init(configuration);
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2020-04-14 06:32:54 +00:00
|
|
|
static void setupViewInvalidation(Context context) {
|
|
|
|
// This needs to be done on a foreground thread
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
db.account().liveAccountView().observeForever(new Observer<List<TupleAccountView>>() {
|
|
|
|
private List<TupleAccountView> last = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(List<TupleAccountView> accounts) {
|
|
|
|
if (accounts == null)
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
|
|
|
|
boolean changed = false;
|
|
|
|
if (last == null || last.size() != accounts.size())
|
|
|
|
changed = true;
|
|
|
|
else
|
|
|
|
for (int i = 0; i < accounts.size(); i++)
|
|
|
|
if (!accounts.get(i).equals(last.get(i))) {
|
|
|
|
changed = true;
|
|
|
|
last = accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
Log.i("Invalidating account view");
|
|
|
|
last = accounts;
|
2020-04-14 08:58:11 +00:00
|
|
|
db.getInvalidationTracker().notifyObserversByTableNames("message");
|
2020-04-14 06:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
db.identity().liveIdentityView().observeForever(new Observer<List<TupleIdentityView>>() {
|
|
|
|
private List<TupleIdentityView> last = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(List<TupleIdentityView> identities) {
|
|
|
|
if (identities == null)
|
|
|
|
identities = new ArrayList<>();
|
|
|
|
|
|
|
|
boolean changed = false;
|
|
|
|
if (last == null || last.size() != identities.size())
|
|
|
|
changed = true;
|
|
|
|
else
|
|
|
|
for (int i = 0; i < identities.size(); i++)
|
|
|
|
if (!identities.get(i).equals(last.get(i))) {
|
|
|
|
changed = true;
|
|
|
|
last = identities;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
Log.i("Invalidating identity view");
|
|
|
|
last = identities;
|
2020-04-14 08:58:11 +00:00
|
|
|
db.getInvalidationTracker().notifyObserversByTableNames("message");
|
2020-04-14 06:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
db.folder().liveFolderView().observeForever(new Observer<List<TupleFolderView>>() {
|
|
|
|
private List<TupleFolderView> last = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onChanged(List<TupleFolderView> folders) {
|
|
|
|
if (folders == null)
|
|
|
|
folders = new ArrayList<>();
|
|
|
|
|
|
|
|
boolean changed = false;
|
|
|
|
if (last == null || last.size() != folders.size())
|
|
|
|
changed = true;
|
|
|
|
else
|
|
|
|
for (int i = 0; i < folders.size(); i++)
|
|
|
|
if (!folders.get(i).equals(last.get(i))) {
|
|
|
|
changed = true;
|
|
|
|
last = folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
Log.i("Invalidating folder view");
|
|
|
|
last = folders;
|
2020-04-26 07:11:21 +00:00
|
|
|
db.getInvalidationTracker().notifyObserversByTableNames("account", "message");
|
2020-04-14 06:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-27 14:31:58 +00:00
|
|
|
static void createEmergencyBackup(Context context) {
|
2023-01-17 23:12:29 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean emergency_file = prefs.getBoolean("emergency_file", true);
|
|
|
|
|
|
|
|
File emergency = new File(context.getFilesDir(), "emergency.json");
|
|
|
|
|
|
|
|
if (emergency_file) {
|
|
|
|
Log.i("Creating emergency backup");
|
|
|
|
try {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
JSONArray jaccounts = new JSONArray();
|
|
|
|
List<EntityAccount> accounts = db.account().getAccounts();
|
|
|
|
for (EntityAccount account : accounts) {
|
|
|
|
JSONObject jaccount = account.toJSON();
|
|
|
|
|
|
|
|
JSONArray jfolders = new JSONArray();
|
|
|
|
List<EntityFolder> folders = db.folder().getFolders(account.id, false, true);
|
|
|
|
for (EntityFolder folder : folders)
|
|
|
|
jfolders.put(folder.toJSON());
|
|
|
|
jaccount.put("folders", jfolders);
|
|
|
|
|
|
|
|
JSONArray jidentities = new JSONArray();
|
|
|
|
List<EntityIdentity> identities = db.identity().getIdentities(account.id);
|
|
|
|
for (EntityIdentity identity : identities)
|
|
|
|
jidentities.put(identity.toJSON());
|
|
|
|
jaccount.put("identities", jidentities);
|
|
|
|
|
|
|
|
jaccounts.put(jaccount);
|
|
|
|
}
|
2021-02-27 14:31:58 +00:00
|
|
|
|
2023-01-17 23:12:29 +00:00
|
|
|
Helper.writeText(emergency, jaccounts.toString(2));
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
} else
|
2023-11-16 10:10:34 +00:00
|
|
|
Helper.secureDelete(emergency);
|
2021-02-27 14:31:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-27 15:03:20 +00:00
|
|
|
private static void checkEmergencyBackup(Context context) {
|
2021-02-27 14:31:58 +00:00
|
|
|
try {
|
|
|
|
File dbfile = context.getDatabasePath(DB_NAME);
|
|
|
|
if (dbfile.exists()) {
|
|
|
|
Log.i("Emergency restore /dbfile");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File emergency = new File(context.getFilesDir(), "emergency.json");
|
|
|
|
if (!emergency.exists()) {
|
|
|
|
Log.i("Emergency restore /json");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
if (db.account().getAccounts().size() > 0) {
|
|
|
|
Log.e("Emergency restore /accounts");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.e("Emergency restore");
|
|
|
|
|
|
|
|
String json = Helper.readText(emergency);
|
|
|
|
JSONArray jaccounts = new JSONArray(json);
|
|
|
|
for (int a = 0; a < jaccounts.length(); a++) {
|
|
|
|
JSONObject jaccount = jaccounts.getJSONObject(a);
|
|
|
|
EntityAccount account = EntityAccount.fromJSON(jaccount);
|
2021-02-28 06:52:15 +00:00
|
|
|
account.created = new Date().getTime();
|
2021-02-27 14:31:58 +00:00
|
|
|
account.id = db.account().insertAccount(account);
|
|
|
|
|
|
|
|
JSONArray jfolders = jaccount.getJSONArray("folders");
|
|
|
|
for (int f = 0; f < jfolders.length(); f++) {
|
|
|
|
EntityFolder folder = EntityFolder.fromJSON(jfolders.getJSONObject(f));
|
|
|
|
folder.account = account.id;
|
|
|
|
db.folder().insertFolder(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONArray jidentities = jaccount.getJSONArray("identities");
|
|
|
|
for (int i = 0; i < jidentities.length(); i++) {
|
|
|
|
EntityIdentity identity = EntityIdentity.fromJSON(jidentities.getJSONObject(i));
|
|
|
|
identity.account = account.id;
|
|
|
|
db.identity().insertIdentity(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 14:28:48 +00:00
|
|
|
public static synchronized DB getInstance(Context context) {
|
2021-08-13 14:56:52 +00:00
|
|
|
int apid = android.os.Process.myPid();
|
2021-04-25 14:13:26 +00:00
|
|
|
Context acontext = context.getApplicationContext();
|
|
|
|
if (sInstance != null &&
|
2021-08-13 14:56:52 +00:00
|
|
|
(sPid != apid || !Objects.equals(sContext, acontext)))
|
2021-04-25 14:13:26 +00:00
|
|
|
try {
|
2021-08-13 14:56:52 +00:00
|
|
|
Log.e("Orphan database instance pid=" + apid + "/" + sPid);
|
2021-04-25 14:13:26 +00:00
|
|
|
sInstance = null;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2021-08-13 14:56:52 +00:00
|
|
|
sPid = apid;
|
2021-04-25 14:13:26 +00:00
|
|
|
sContext = acontext;
|
|
|
|
|
2018-08-24 12:31:51 +00:00
|
|
|
if (sInstance == null) {
|
2021-04-25 14:13:26 +00:00
|
|
|
Log.i("Creating database instance pid=" + sPid);
|
2019-07-22 14:28:48 +00:00
|
|
|
|
2023-11-17 09:07:27 +00:00
|
|
|
sInstance = getBuilder(sContext).build();
|
2019-07-22 14:28:48 +00:00
|
|
|
|
2022-12-13 09:52:39 +00:00
|
|
|
Helper.getSerialExecutor().execute(new Runnable() {
|
2021-02-27 10:22:17 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2021-04-25 14:13:26 +00:00
|
|
|
checkEmergencyBackup(sContext);
|
2021-02-27 10:22:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-14 08:14:58 +00:00
|
|
|
try {
|
|
|
|
Log.i("Disabling view invalidation");
|
|
|
|
|
2020-04-14 08:58:11 +00:00
|
|
|
Field fmViewTables = InvalidationTracker.class.getDeclaredField("mViewTables");
|
2020-04-14 08:14:58 +00:00
|
|
|
fmViewTables.setAccessible(true);
|
|
|
|
|
|
|
|
Map<String, Set<String>> mViewTables = (Map) fmViewTables.get(sInstance.getInvalidationTracker());
|
|
|
|
mViewTables.get("account_view").clear();
|
|
|
|
mViewTables.get("identity_view").clear();
|
|
|
|
mViewTables.get("folder_view").clear();
|
|
|
|
|
|
|
|
Log.i("Disabled view invalidation");
|
|
|
|
} catch (ReflectiveOperationException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
2020-04-14 08:58:11 +00:00
|
|
|
sInstance.getInvalidationTracker().addObserver(new InvalidationTracker.Observer(DB_TABLES) {
|
2019-05-31 06:57:23 +00:00
|
|
|
@Override
|
|
|
|
public void onInvalidated(@NonNull Set<String> tables) {
|
2020-01-23 07:46:31 +00:00
|
|
|
Log.d("ROOM invalidated=" + TextUtils.join(",", tables));
|
2019-05-31 06:57:23 +00:00
|
|
|
}
|
|
|
|
});
|
2018-08-24 12:31:51 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
2019-07-17 10:21:17 +00:00
|
|
|
private static RoomDatabase.Builder<DB> getBuilder(Context context) {
|
2020-06-18 06:03:39 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-02-24 17:10:53 +00:00
|
|
|
boolean wal = prefs.getBoolean("wal", true);
|
2022-12-13 09:52:39 +00:00
|
|
|
Log.i("DB wal=" + wal);
|
2020-06-18 06:03:39 +00:00
|
|
|
|
2022-11-01 07:21:31 +00:00
|
|
|
RoomDatabase.Builder<DB> builder = Room
|
2019-04-16 18:19:50 +00:00
|
|
|
.databaseBuilder(context, DB.class, DB_NAME)
|
2022-09-26 07:01:55 +00:00
|
|
|
//.openHelperFactory(new RequerySQLiteOpenHelperFactory())
|
2022-12-13 09:52:39 +00:00
|
|
|
//.setQueryExecutor()
|
2023-01-05 10:17:00 +00:00
|
|
|
.setTransactionExecutor(executor)
|
2021-02-24 17:10:53 +00:00
|
|
|
.setJournalMode(wal ? JournalMode.WRITE_AHEAD_LOGGING : JournalMode.TRUNCATE) // using the latest sqlite
|
2019-07-24 12:49:55 +00:00
|
|
|
.addCallback(new Callback() {
|
2022-12-14 08:11:29 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(@NonNull SupportSQLiteDatabase db) {
|
|
|
|
defaultSearches(db, context);
|
|
|
|
}
|
|
|
|
|
2019-07-24 12:49:55 +00:00
|
|
|
@Override
|
|
|
|
public void onOpen(@NonNull SupportSQLiteDatabase db) {
|
2022-01-09 13:47:46 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("version", Integer.toString(db.getVersion()));
|
|
|
|
crumb.put("WAL", Boolean.toString(db.isWriteAheadLoggingEnabled()));
|
|
|
|
Log.breadcrumb("Database", crumb);
|
2020-01-18 19:05:24 +00:00
|
|
|
|
2022-07-07 08:52:50 +00:00
|
|
|
// https://www.sqlite.org/pragma.html#pragma_auto_vacuum
|
|
|
|
// https://android.googlesource.com/platform/external/sqlite.git/+/6ab557bdc070f11db30ede0696888efd19800475%5E!/
|
2022-08-31 07:09:42 +00:00
|
|
|
boolean sqlite_auto_vacuum = prefs.getBoolean("sqlite_auto_vacuum", false);
|
2022-07-07 08:52:50 +00:00
|
|
|
String mode = (sqlite_auto_vacuum ? "FULL" : "INCREMENTAL");
|
2022-09-25 06:20:46 +00:00
|
|
|
Log.i("Set PRAGMA auto_vacuum=" + mode);
|
2022-11-08 07:14:01 +00:00
|
|
|
try (Cursor cursor = db.query("PRAGMA auto_vacuum=" + mode + ";")) {
|
2022-07-07 08:52:50 +00:00
|
|
|
cursor.moveToNext(); // required
|
2022-07-04 15:23:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 07:09:42 +00:00
|
|
|
// https://sqlite.org/pragma.html#pragma_synchronous
|
|
|
|
boolean sqlite_sync_extra = prefs.getBoolean("sqlite_sync_extra", true);
|
|
|
|
String sync = (sqlite_sync_extra ? "EXTRA" : "NORMAL");
|
2022-09-25 06:20:46 +00:00
|
|
|
Log.i("Set PRAGMA synchronous=" + sync);
|
2022-11-08 07:14:01 +00:00
|
|
|
try (Cursor cursor = db.query("PRAGMA synchronous=" + sync + ";")) {
|
2022-09-25 06:20:46 +00:00
|
|
|
cursor.moveToNext(); // required
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.i("Set PRAGMA journal_size_limit=" + DB_JOURNAL_SIZE_LIMIT);
|
2022-11-08 07:14:01 +00:00
|
|
|
try (Cursor cursor = db.query("PRAGMA journal_size_limit=" + DB_JOURNAL_SIZE_LIMIT + ";")) {
|
2022-08-31 07:09:42 +00:00
|
|
|
cursor.moveToNext(); // required
|
|
|
|
}
|
|
|
|
|
2021-10-16 18:36:50 +00:00
|
|
|
// https://www.sqlite.org/pragma.html#pragma_cache_size
|
|
|
|
Integer cache_size = getCacheSizeKb(context);
|
|
|
|
if (cache_size != null) {
|
|
|
|
cache_size = -cache_size; // kibibytes
|
|
|
|
Log.i("Set PRAGMA cache_size=" + cache_size);
|
2022-11-08 07:14:01 +00:00
|
|
|
try (Cursor cursor = db.query("PRAGMA cache_size=" + cache_size + ";")) {
|
2021-10-16 18:36:50 +00:00
|
|
|
cursor.moveToNext(); // required
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent long running operations from getting an exclusive lock
|
|
|
|
// https://www.sqlite.org/pragma.html#pragma_cache_spill
|
|
|
|
Log.i("Set PRAGMA cache_spill=0");
|
2022-11-08 07:14:01 +00:00
|
|
|
try (Cursor cursor = db.query("PRAGMA cache_spill=0;")) {
|
2021-10-16 18:36:50 +00:00
|
|
|
cursor.moveToNext(); // required
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:00:37 +00:00
|
|
|
Log.i("Set PRAGMA recursive_triggers=off");
|
|
|
|
try (Cursor cursor = db.query("PRAGMA recursive_triggers=off;")) {
|
|
|
|
cursor.moveToNext(); // required
|
|
|
|
}
|
|
|
|
|
2021-02-23 14:01:59 +00:00
|
|
|
// https://www.sqlite.org/pragma.html
|
2021-12-01 07:24:36 +00:00
|
|
|
for (String pragma : DB_PRAGMAS)
|
2022-09-24 11:00:18 +00:00
|
|
|
if (!"compile_options".equals(pragma) || BuildConfig.DEBUG)
|
|
|
|
try (Cursor cursor = db.query("PRAGMA " + pragma + ";")) {
|
|
|
|
boolean has = false;
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
has = true;
|
|
|
|
Log.i("Get PRAGMA " + pragma + "=" + (cursor.isNull(0) ? "<null>" : cursor.getString(0)));
|
|
|
|
}
|
|
|
|
if (!has)
|
|
|
|
Log.i("Get PRAGMA " + pragma + "=<?>");
|
|
|
|
}
|
2021-02-23 14:01:59 +00:00
|
|
|
|
2023-01-31 06:36:36 +00:00
|
|
|
if (BuildConfig.DEBUG && false)
|
|
|
|
dropTriggers(db);
|
2022-03-05 14:43:53 +00:00
|
|
|
|
2022-03-06 08:49:31 +00:00
|
|
|
createTriggers(db);
|
2019-07-24 12:49:55 +00:00
|
|
|
}
|
|
|
|
});
|
2022-11-01 07:21:31 +00:00
|
|
|
|
|
|
|
if (BuildConfig.DEBUG && false)
|
|
|
|
builder.setQueryCallback(new QueryCallback() {
|
|
|
|
@Override
|
|
|
|
public void onQuery(@NonNull String sqlQuery, @NonNull List<Object> bindArgs) {
|
|
|
|
Log.i("query=" + sqlQuery);
|
|
|
|
}
|
2022-12-13 09:52:39 +00:00
|
|
|
}, Helper.getParallelExecutor());
|
2022-11-01 07:21:31 +00:00
|
|
|
|
2023-11-17 09:07:27 +00:00
|
|
|
DBMigration0.migrate(context, builder);
|
|
|
|
DBMigration1.migrate(context, builder);
|
|
|
|
DBMigration2.migrate(context, builder);
|
|
|
|
|
2022-11-01 07:21:31 +00:00
|
|
|
return builder;
|
2019-01-06 16:31:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 18:36:50 +00:00
|
|
|
static Integer getCacheSizeKb(Context context) {
|
|
|
|
try {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
int sqlite_cache = prefs.getInt("sqlite_cache", DEFAULT_CACHE_SIZE);
|
|
|
|
|
2022-04-13 20:27:33 +00:00
|
|
|
ActivityManager am = Helper.getSystemService(context, ActivityManager.class);
|
2021-10-16 18:36:50 +00:00
|
|
|
int class_mb = am.getMemoryClass();
|
|
|
|
int cache_size = sqlite_cache * class_mb * 1024 / 100;
|
|
|
|
|
|
|
|
return (cache_size > 2000 ? cache_size : null);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 09:07:27 +00:00
|
|
|
static void dropTriggers(@NonNull SupportSQLiteDatabase db) {
|
2023-01-31 06:36:36 +00:00
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
|
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
|
|
|
|
|
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS `account_update`");
|
|
|
|
db.execSQL("DROP TRIGGER IF EXISTS `identity_update`");
|
|
|
|
}
|
|
|
|
|
2023-11-17 09:07:27 +00:00
|
|
|
static void createTriggers(@NonNull SupportSQLiteDatabase db) {
|
2022-04-06 06:39:18 +00:00
|
|
|
List<String> image = new ArrayList<>();
|
|
|
|
for (String img : ImageHelper.IMAGE_TYPES)
|
|
|
|
image.add("'" + img + "'");
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
|
|
|
for (String img : ImageHelper.IMAGE_TYPES8)
|
2021-08-18 17:37:35 +00:00
|
|
|
image.add("'" + img + "'");
|
2022-04-06 06:39:18 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
|
|
|
|
for (String img : ImageHelper.IMAGE_TYPES12)
|
|
|
|
image.add("'" + img + "'");
|
|
|
|
String images = TextUtils.join(",", image);
|
|
|
|
|
|
|
|
db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_insert" +
|
|
|
|
" AFTER INSERT ON attachment" +
|
|
|
|
" BEGIN" +
|
|
|
|
" UPDATE message SET attachments = attachments + 1" +
|
|
|
|
" WHERE message.id = NEW.message" +
|
|
|
|
" AND NEW.encryption IS NULL" +
|
|
|
|
" AND NOT ((NEW.disposition = 'inline' OR (NEW.related IS NOT 0 AND NEW.cid IS NOT NULL)) AND NEW.type IN (" + images + "));" +
|
|
|
|
" END");
|
|
|
|
db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_delete" +
|
|
|
|
" AFTER DELETE ON attachment" +
|
|
|
|
" BEGIN" +
|
|
|
|
" UPDATE message SET attachments = attachments - 1" +
|
|
|
|
" WHERE message.id = OLD.message" +
|
|
|
|
" AND OLD.encryption IS NULL" +
|
|
|
|
" AND NOT ((OLD.disposition = 'inline' OR (OLD.related IS NOT 0 AND OLD.cid IS NOT NULL)) AND OLD.type IN (" + images + "));" +
|
|
|
|
" END");
|
2023-01-19 21:00:37 +00:00
|
|
|
|
|
|
|
db.execSQL("CREATE TRIGGER IF NOT EXISTS account_update" +
|
|
|
|
" AFTER UPDATE ON account" +
|
|
|
|
" BEGIN" +
|
|
|
|
" UPDATE account SET last_modified = strftime('%s') * 1000" +
|
2023-01-21 12:52:57 +00:00
|
|
|
" WHERE OLD.id = NEW.id" +
|
2023-01-20 14:25:38 +00:00
|
|
|
" AND OLD.last_modified = NEW.last_modified" +
|
2023-01-20 08:22:37 +00:00
|
|
|
" AND (NEW.auth_type = " + AUTH_TYPE_PASSWORD + " OR OLD.password = NEW.password)" +
|
|
|
|
" AND OLD.keep_alive_ok IS NEW.keep_alive_ok" +
|
|
|
|
" AND OLD.keep_alive_failed IS NEW.keep_alive_failed" +
|
|
|
|
" AND OLD.keep_alive_succeeded IS NEW.keep_alive_succeeded" +
|
|
|
|
" AND OLD.quota_usage IS NEW.quota_usage" +
|
|
|
|
" AND OLD.thread IS NEW.thread" +
|
|
|
|
" AND OLD.state IS NEW.state" +
|
|
|
|
" AND OLD.warning IS NEW.warning" +
|
|
|
|
" AND OLD.error IS NEW.error" +
|
|
|
|
" AND OLD.last_connected IS NEW.last_connected" +
|
|
|
|
" AND OLD.backoff_until IS NEW.backoff_until;" +
|
2023-01-19 21:00:37 +00:00
|
|
|
" END");
|
|
|
|
|
|
|
|
db.execSQL("CREATE TRIGGER IF NOT EXISTS identity_update" +
|
|
|
|
" AFTER UPDATE ON identity" +
|
|
|
|
" BEGIN" +
|
|
|
|
" UPDATE identity SET last_modified = strftime('%s') * 1000" +
|
2023-01-21 12:52:57 +00:00
|
|
|
" WHERE OLD.id = NEW.id" +
|
2023-01-20 14:25:38 +00:00
|
|
|
" AND OLD.last_modified = NEW.last_modified" +
|
2023-01-20 08:22:37 +00:00
|
|
|
" AND OLD.state IS NEW.state" +
|
|
|
|
" AND OLD.error IS NEW.error" +
|
|
|
|
" AND OLD.last_connected IS NEW.last_connected" +
|
2023-01-20 07:31:12 +00:00
|
|
|
" AND (NEW.auth_type = " + AUTH_TYPE_PASSWORD + " OR OLD.password = NEW.password);" +
|
2023-01-19 21:00:37 +00:00
|
|
|
" END");
|
2020-01-22 08:02:59 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 09:07:27 +00:00
|
|
|
static void logMigration(int startVersion, int endVersion) {
|
2023-11-16 12:17:55 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("startVersion", Integer.toString(startVersion));
|
|
|
|
crumb.put("endVersion", Integer.toString(endVersion));
|
|
|
|
Log.breadcrumb("Migration", crumb);
|
|
|
|
}
|
|
|
|
|
2023-11-17 09:07:27 +00:00
|
|
|
static void defaultSearches(SupportSQLiteDatabase db, Context context) {
|
2022-12-14 08:11:29 +00:00
|
|
|
try {
|
|
|
|
BoundaryCallbackMessages.SearchCriteria criteria;
|
|
|
|
|
|
|
|
criteria = new BoundaryCallbackMessages.SearchCriteria();
|
|
|
|
criteria.with_flagged = true;
|
|
|
|
|
|
|
|
db.execSQL("INSERT INTO `search` (`name`, `order`, `data`) VALUES (?, ?, ?)",
|
|
|
|
new Object[]{
|
|
|
|
context.getString(R.string.title_search_with_flagged),
|
|
|
|
0,
|
|
|
|
criteria.toJsonData().toString()
|
|
|
|
});
|
|
|
|
|
|
|
|
criteria = new BoundaryCallbackMessages.SearchCriteria();
|
|
|
|
criteria.with_unseen = true;
|
|
|
|
db.execSQL("INSERT INTO `search` (`name`, `order`, `data`) VALUES (?, ?, ?)",
|
|
|
|
new Object[]{
|
|
|
|
context.getString(R.string.title_search_with_unseen),
|
|
|
|
0,
|
|
|
|
criteria.toJsonData().toString()
|
|
|
|
});
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 12:06:28 +00:00
|
|
|
public static void checkpoint(Context context) {
|
2021-02-25 13:28:58 +00:00
|
|
|
// https://www.sqlite.org/pragma.html#pragma_wal_checkpoint
|
2021-09-11 12:06:28 +00:00
|
|
|
DB db = getInstance(context);
|
2022-12-27 20:39:27 +00:00
|
|
|
db.getQueryExecutor().execute(new Runnable() {
|
2021-09-11 12:06:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
long start = new Date().getTime();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
SupportSQLiteDatabase sdb = db.getOpenHelper().getWritableDatabase();
|
2022-07-20 14:06:23 +00:00
|
|
|
String mode = (true ? "RESTART" : "PASSIVE");
|
2022-06-24 20:11:53 +00:00
|
|
|
try (Cursor cursor = sdb.query("PRAGMA wal_checkpoint(" + mode + ");")) {
|
2021-09-11 12:06:28 +00:00
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
for (int i = 0; i < cursor.getColumnCount(); i++) {
|
|
|
|
if (i > 0)
|
|
|
|
sb.append(",");
|
|
|
|
sb.append(cursor.getInt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long elapse = new Date().getTime() - start;
|
2022-07-21 07:11:18 +00:00
|
|
|
EntityLog.log(context, "PRAGMA wal_checkpoint(" + mode + ")=" + sb +
|
|
|
|
" elapse=" + elapse + " ms");
|
2021-09-11 12:06:28 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2021-02-25 13:28:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-11 12:06:28 +00:00
|
|
|
});
|
|
|
|
}
|
2021-02-25 13:28:58 +00:00
|
|
|
|
2021-09-11 12:06:28 +00:00
|
|
|
public static void shrinkMemory(Context context) {
|
|
|
|
DB db = getInstance(context);
|
2022-12-27 20:39:27 +00:00
|
|
|
db.getQueryExecutor().execute(new Runnable() {
|
2021-09-11 12:06:28 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
SupportSQLiteDatabase sdb = db.getOpenHelper().getWritableDatabase();
|
|
|
|
try (Cursor cursor = sdb.query("PRAGMA shrink_memory;")) {
|
|
|
|
cursor.moveToNext();
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-02-25 13:28:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 10:57:57 +00:00
|
|
|
@Override
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public void beginTransaction() {
|
|
|
|
super.beginTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public void setTransactionSuccessful() {
|
|
|
|
super.setTransactionSuccessful();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public void endTransaction() {
|
2022-09-26 07:01:55 +00:00
|
|
|
try {
|
|
|
|
super.endTransaction();
|
2023-02-05 07:53:41 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
String msg = ex.getMessage();
|
|
|
|
if (TextUtils.isEmpty(msg))
|
2022-09-26 07:01:55 +00:00
|
|
|
throw ex;
|
2023-02-05 07:53:41 +00:00
|
|
|
|
|
|
|
if (msg.contains("no current transaction")) {
|
|
|
|
// java.lang.IllegalStateException: Cannot perform this operation because there is no current transaction.
|
|
|
|
Log.w(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.contains("no transaction is active")) {
|
|
|
|
// Moto e⁶ plus - Android 9
|
|
|
|
/*
|
|
|
|
android.database.sqlite.SQLiteException: cannot rollback - no transaction is active (code 1 SQLITE_ERROR)
|
|
|
|
at android.database.sqlite.SQLiteConnection.nativeExecute(SQLiteConnection.java:-2)
|
|
|
|
at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:569)
|
|
|
|
at android.database.sqlite.SQLiteSession.endTransactionUnchecked(SQLiteSession.java:439)
|
|
|
|
at android.database.sqlite.SQLiteSession.endTransaction(SQLiteSession.java:401)
|
|
|
|
at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:566)
|
|
|
|
at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.endTransaction(FrameworkSQLiteDatabase:75)
|
|
|
|
at androidx.room.RoomDatabase.internalEndTransaction(RoomDatabase:594)
|
|
|
|
at androidx.room.RoomDatabase.endTransaction(RoomDatabase:584)
|
|
|
|
at eu.faircode.email.DB.endTransaction(DB:2842)
|
|
|
|
at androidx.room.paging.LimitOffsetDataSource.loadInitial(LimitOffsetDataSource:181)
|
|
|
|
at androidx.paging.PositionalDataSource.dispatchLoadInitial(PositionalDataSource:286)
|
|
|
|
at androidx.paging.TiledPagedList.<init>(TiledPagedList:107)
|
|
|
|
at androidx.paging.PagedList.create(PagedList:229)
|
|
|
|
at androidx.paging.PagedList$Builder.build(PagedList:388)
|
|
|
|
at androidx.paging.LivePagedListBuilder$1.compute(LivePagedListBuilder:206)
|
|
|
|
at androidx.paging.LivePagedListBuilder$1.compute(LivePagedListBuilder:171)
|
|
|
|
at androidx.lifecycle.ComputableLiveData$2.run(ComputableLiveData:110)
|
|
|
|
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
|
|
|
|
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
|
|
|
|
at java.lang.Thread.run(Thread.java:764)
|
|
|
|
*/
|
|
|
|
Log.w(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw ex;
|
2022-09-26 07:01:55 +00:00
|
|
|
}
|
2019-05-30 10:57:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
public static class Converters {
|
|
|
|
@TypeConverter
|
2018-11-25 12:34:08 +00:00
|
|
|
public static String[] toStringArray(String value) {
|
|
|
|
if (value == null)
|
|
|
|
return new String[0];
|
2020-06-25 07:14:05 +00:00
|
|
|
else {
|
|
|
|
String[] result = TextUtils.split(value, " ");
|
|
|
|
for (int i = 0; i < result.length; i++)
|
|
|
|
result[i] = Uri.decode(result[i]);
|
|
|
|
return result;
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
2018-11-25 12:34:08 +00:00
|
|
|
public static String fromStringArray(String[] value) {
|
|
|
|
if (value == null || value.length == 0)
|
|
|
|
return null;
|
2020-06-25 07:14:05 +00:00
|
|
|
else {
|
|
|
|
String[] copy = new String[value.length];
|
|
|
|
System.arraycopy(value, 0, copy, 0, value.length);
|
|
|
|
for (int i = 0; i < copy.length; i++)
|
|
|
|
copy[i] = Uri.encode(copy[i]);
|
|
|
|
return TextUtils.join(" ", copy);
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2018-08-07 16:25:57 +00:00
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
public static String encodeAddresses(Address[] addresses) {
|
|
|
|
if (addresses == null)
|
|
|
|
return null;
|
|
|
|
JSONArray jaddresses = new JSONArray();
|
2018-12-09 14:49:43 +00:00
|
|
|
for (Address address : addresses)
|
|
|
|
try {
|
|
|
|
if (address instanceof InternetAddress) {
|
|
|
|
String a = ((InternetAddress) address).getAddress();
|
|
|
|
String p = ((InternetAddress) address).getPersonal();
|
|
|
|
JSONObject jaddress = new JSONObject();
|
|
|
|
if (a != null)
|
|
|
|
jaddress.put("address", a);
|
|
|
|
if (p != null)
|
|
|
|
jaddress.put("personal", p);
|
|
|
|
jaddresses.put(jaddress);
|
|
|
|
} else {
|
|
|
|
JSONObject jaddress = new JSONObject();
|
|
|
|
jaddress.put("address", address.toString());
|
|
|
|
jaddresses.put(jaddress);
|
2018-08-07 16:25:57 +00:00
|
|
|
}
|
2018-12-09 14:49:43 +00:00
|
|
|
} catch (JSONException ex) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.e(ex);
|
2018-12-09 14:49:43 +00:00
|
|
|
}
|
2018-08-07 16:25:57 +00:00
|
|
|
return jaddresses.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
public static Address[] decodeAddresses(String json) {
|
2020-05-07 11:14:21 +00:00
|
|
|
if (json == null)
|
2018-08-07 16:25:57 +00:00
|
|
|
return null;
|
2020-05-08 06:15:39 +00:00
|
|
|
|
2020-05-07 11:14:30 +00:00
|
|
|
List<Address> result = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
JSONArray jroot = new JSONArray(json);
|
|
|
|
for (int i = 0; i < jroot.length(); i++) {
|
|
|
|
Object item = jroot.get(i);
|
|
|
|
if (jroot.get(i) instanceof JSONArray)
|
|
|
|
for (int j = 0; j < ((JSONArray) item).length(); j++)
|
2020-05-08 06:15:39 +00:00
|
|
|
result.add(InternetAddressJson.from((JSONObject) ((JSONArray) item).get(j)));
|
2020-05-07 11:14:30 +00:00
|
|
|
else
|
2020-05-08 06:15:39 +00:00
|
|
|
result.add(InternetAddressJson.from((JSONObject) item));
|
2020-05-07 11:14:30 +00:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
// Compose can store invalid addresses
|
|
|
|
Log.w(ex);
|
2018-08-07 16:25:57 +00:00
|
|
|
}
|
2020-05-07 11:14:30 +00:00
|
|
|
return result.toArray(new Address[0]);
|
2018-08-07 16:25:57 +00:00
|
|
|
}
|
2021-08-16 07:36:23 +00:00
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
public static EntityLog.Type toLogType(int ordinal) {
|
|
|
|
return EntityLog.Type.values()[ordinal];
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
public static int fromLogType(EntityLog.Type type) {
|
2021-08-17 12:21:08 +00:00
|
|
|
if (type == null)
|
|
|
|
type = EntityLog.Type.General;
|
2021-08-16 07:36:23 +00:00
|
|
|
return type.ordinal();
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|