2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-24 09:20:14 +00:00
|
|
|
import android.database.Cursor;
|
2018-08-06 16:22:01 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2018-08-07 16:25:57 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.room.Database;
|
|
|
|
import androidx.room.Room;
|
|
|
|
import androidx.room.RoomDatabase;
|
|
|
|
import androidx.room.TypeConverter;
|
|
|
|
import androidx.room.TypeConverters;
|
|
|
|
import androidx.room.migration.Migration;
|
|
|
|
import androidx.sqlite.db.SupportSQLiteDatabase;
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// https://developer.android.com/topic/libraries/architecture/room.html
|
|
|
|
|
|
|
|
@Database(
|
2018-08-27 07:06:03 +00:00
|
|
|
version = 4,
|
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,
|
|
|
|
EntityAnswer.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 DaoIdentity identity();
|
|
|
|
|
|
|
|
public abstract DaoAccount account();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2018-08-27 07:06:03 +00:00
|
|
|
public abstract DaoAnswer answer();
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
private static DB sInstance;
|
|
|
|
|
2018-08-19 06:53:56 +00:00
|
|
|
private static final String DB_NAME = "email";
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
public static synchronized DB getInstance(Context context) {
|
2018-08-24 12:31:51 +00:00
|
|
|
if (sInstance == null) {
|
2018-08-24 07:58:04 +00:00
|
|
|
sInstance = migrate(Room
|
|
|
|
.databaseBuilder(context.getApplicationContext(), DB.class, DB_NAME)
|
|
|
|
.setJournalMode(JournalMode.WRITE_AHEAD_LOGGING));
|
2018-08-24 10:15:54 +00:00
|
|
|
|
2018-08-24 12:31:51 +00:00
|
|
|
Log.i(Helper.TAG, "sqlite version=" + exec(sInstance, "SELECT sqlite_version() AS sqlite_version"));
|
|
|
|
Log.i(Helper.TAG, "sqlite sync=" + exec(sInstance, "PRAGMA synchronous"));
|
|
|
|
Log.i(Helper.TAG, "sqlite journal=" + exec(sInstance, "PRAGMA journal_mode"));
|
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
2018-08-24 12:31:51 +00:00
|
|
|
static String exec(DB db, String command) {
|
|
|
|
Cursor cursor = null;
|
|
|
|
try {
|
|
|
|
cursor = db.query(command, new Object[0]);
|
|
|
|
if (cursor.moveToNext())
|
|
|
|
return cursor.getString(0);
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
2018-08-03 18:07:12 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
private static DB migrate(RoomDatabase.Builder<DB> builder) {
|
|
|
|
return builder
|
2018-08-08 10:22:12 +00:00
|
|
|
.addCallback(new Callback() {
|
|
|
|
@Override
|
|
|
|
public void onOpen(SupportSQLiteDatabase db) {
|
|
|
|
Log.i(Helper.TAG, "Database version=" + db.getVersion());
|
|
|
|
super.onOpen(db);
|
|
|
|
}
|
|
|
|
})
|
2018-08-23 09:48:27 +00:00
|
|
|
.addMigrations(new Migration(1, 2) {
|
|
|
|
@Override
|
|
|
|
public void migrate(SupportSQLiteDatabase db) {
|
|
|
|
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
|
|
|
|
db.execSQL("ALTER TABLE `account` ADD COLUMN `poll_interval` INTEGER NOT NULL DEFAULT 9");
|
|
|
|
}
|
|
|
|
})
|
2018-08-25 14:24:26 +00:00
|
|
|
.addMigrations(new Migration(2, 3) {
|
|
|
|
@Override
|
|
|
|
public void migrate(SupportSQLiteDatabase db) {
|
|
|
|
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
|
|
|
|
db.execSQL("ALTER TABLE `identity` ADD COLUMN `store_sent` INTEGER NOT NULL DEFAULT 0");
|
|
|
|
}
|
|
|
|
})
|
2018-08-27 07:06:03 +00:00
|
|
|
.addMigrations(new Migration(3, 4) {
|
|
|
|
@Override
|
|
|
|
public void migrate(SupportSQLiteDatabase db) {
|
|
|
|
Log.i(Helper.TAG, "DB migration from version " + startVersion + " to " + endVersion);
|
|
|
|
db.execSQL("CREATE TABLE IF NOT EXISTS `answer` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT NOT NULL, `text` TEXT NOT NULL)");
|
|
|
|
}
|
|
|
|
})
|
2018-08-02 13:33:06 +00:00
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Converters {
|
|
|
|
@TypeConverter
|
2018-08-06 16:22:01 +00:00
|
|
|
public static String[] fromStringArray(String value) {
|
|
|
|
return value.split(",");
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
2018-08-06 16:22:01 +00:00
|
|
|
public static String toStringArray(String[] value) {
|
2018-08-08 06:55:47 +00:00
|
|
|
return TextUtils.join(",", value);
|
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();
|
|
|
|
if (addresses != null)
|
|
|
|
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)
|
2018-08-08 06:55:47 +00:00
|
|
|
jaddress.put("address", a);
|
2018-08-07 16:25:57 +00:00
|
|
|
if (p != null)
|
2018-08-08 06:55:47 +00:00
|
|
|
jaddress.put("personal", p);
|
2018-08-07 16:25:57 +00:00
|
|
|
jaddresses.put(jaddress);
|
|
|
|
} else {
|
|
|
|
JSONObject jaddress = new JSONObject();
|
2018-08-08 06:55:47 +00:00
|
|
|
jaddress.put("address", address.toString());
|
2018-08-07 16:25:57 +00:00
|
|
|
jaddresses.put(jaddress);
|
|
|
|
}
|
|
|
|
} catch (JSONException ex) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
return jaddresses.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
@TypeConverter
|
|
|
|
public static Address[] decodeAddresses(String json) {
|
|
|
|
if (json == null)
|
|
|
|
return null;
|
|
|
|
List<Address> result = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
JSONArray jaddresses = new JSONArray(json);
|
|
|
|
for (int i = 0; i < jaddresses.length(); i++) {
|
|
|
|
JSONObject jaddress = (JSONObject) jaddresses.get(i);
|
|
|
|
if (jaddress.has("personal"))
|
|
|
|
result.add(new InternetAddress(
|
|
|
|
jaddress.getString("address"),
|
|
|
|
jaddress.getString("personal")));
|
|
|
|
else
|
|
|
|
result.add(new InternetAddress(
|
|
|
|
jaddress.getString("address")));
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
return result.toArray(new Address[0]);
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|