DatabaseBackend.java

   1package eu.siacs.conversations.persistance;
   2
   3import android.content.ContentValues;
   4import android.content.Context;
   5import android.database.Cursor;
   6import android.database.DatabaseUtils;
   7import android.database.sqlite.SQLiteDatabase;
   8import android.database.sqlite.SQLiteOpenHelper;
   9import android.os.Environment;
  10import android.os.SystemClock;
  11import android.util.Base64;
  12import android.util.Log;
  13
  14import org.json.JSONObject;
  15import org.whispersystems.libsignal.SignalProtocolAddress;
  16import org.whispersystems.libsignal.IdentityKey;
  17import org.whispersystems.libsignal.IdentityKeyPair;
  18import org.whispersystems.libsignal.InvalidKeyException;
  19import org.whispersystems.libsignal.state.PreKeyRecord;
  20import org.whispersystems.libsignal.state.SessionRecord;
  21import org.whispersystems.libsignal.state.SignedPreKeyRecord;
  22
  23import java.io.ByteArrayInputStream;
  24import java.io.File;
  25import java.io.IOException;
  26import java.security.cert.CertificateEncodingException;
  27import java.security.cert.CertificateException;
  28import java.security.cert.CertificateFactory;
  29import java.security.cert.X509Certificate;
  30import java.util.ArrayList;
  31import java.util.HashMap;
  32import java.util.HashSet;
  33import java.util.Iterator;
  34import java.util.List;
  35import java.util.Map;
  36import java.util.Set;
  37import java.util.UUID;
  38import java.util.concurrent.CopyOnWriteArrayList;
  39
  40import org.json.JSONException;
  41
  42import eu.siacs.conversations.Config;
  43import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  44import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
  45import eu.siacs.conversations.crypto.axolotl.SQLiteAxolotlStore;
  46import eu.siacs.conversations.entities.Account;
  47import eu.siacs.conversations.entities.Contact;
  48import eu.siacs.conversations.entities.Conversation;
  49import eu.siacs.conversations.entities.Message;
  50import eu.siacs.conversations.entities.PresenceTemplate;
  51import eu.siacs.conversations.entities.Roster;
  52import eu.siacs.conversations.entities.ServiceDiscoveryResult;
  53import eu.siacs.conversations.services.QuickConversationsService;
  54import eu.siacs.conversations.services.ShortcutService;
  55import eu.siacs.conversations.utils.CryptoHelper;
  56import eu.siacs.conversations.utils.FtsUtils;
  57import eu.siacs.conversations.utils.MimeUtils;
  58import eu.siacs.conversations.utils.Resolver;
  59import eu.siacs.conversations.xmpp.InvalidJid;
  60import eu.siacs.conversations.xmpp.mam.MamReference;
  61import rocks.xmpp.addr.Jid;
  62
  63public class DatabaseBackend extends SQLiteOpenHelper {
  64
  65	private static final String DATABASE_NAME = "history";
  66	private static final int DATABASE_VERSION = 43;
  67	private static DatabaseBackend instance = null;
  68	private static String CREATE_CONTATCS_STATEMENT = "create table "
  69			+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
  70			+ Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT,"
  71			+ Contact.JID + " TEXT," + Contact.KEYS + " TEXT,"
  72			+ Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER,"
  73			+ Contact.SYSTEMACCOUNT + " NUMBER, " + Contact.AVATAR + " TEXT, "
  74			+ Contact.LAST_PRESENCE + " TEXT, " + Contact.LAST_TIME + " NUMBER, "
  75			+ Contact.GROUPS + " TEXT, FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
  76			+ Account.TABLENAME + "(" + Account.UUID
  77			+ ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT + ", "
  78			+ Contact.JID + ") ON CONFLICT REPLACE);";
  79
  80	private static String CREATE_DISCOVERY_RESULTS_STATEMENT = "create table "
  81			+ ServiceDiscoveryResult.TABLENAME + "("
  82			+ ServiceDiscoveryResult.HASH + " TEXT, "
  83			+ ServiceDiscoveryResult.VER + " TEXT, "
  84			+ ServiceDiscoveryResult.RESULT + " TEXT, "
  85			+ "UNIQUE(" + ServiceDiscoveryResult.HASH + ", "
  86			+ ServiceDiscoveryResult.VER + ") ON CONFLICT REPLACE);";
  87
  88	private static String CREATE_PRESENCE_TEMPLATES_STATEMENT = "CREATE TABLE "
  89			+ PresenceTemplate.TABELNAME + "("
  90			+ PresenceTemplate.UUID + " TEXT, "
  91			+ PresenceTemplate.LAST_USED + " NUMBER,"
  92			+ PresenceTemplate.MESSAGE + " TEXT,"
  93			+ PresenceTemplate.STATUS + " TEXT,"
  94			+ "UNIQUE(" + PresenceTemplate.MESSAGE + "," + PresenceTemplate.STATUS + ") ON CONFLICT REPLACE);";
  95
  96	private static String CREATE_PREKEYS_STATEMENT = "CREATE TABLE "
  97			+ SQLiteAxolotlStore.PREKEY_TABLENAME + "("
  98			+ SQLiteAxolotlStore.ACCOUNT + " TEXT,  "
  99			+ SQLiteAxolotlStore.ID + " INTEGER, "
 100			+ SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
 101			+ SQLiteAxolotlStore.ACCOUNT
 102			+ ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
 103			+ "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
 104			+ SQLiteAxolotlStore.ID
 105			+ ") ON CONFLICT REPLACE"
 106			+ ");";
 107
 108	private static String CREATE_SIGNED_PREKEYS_STATEMENT = "CREATE TABLE "
 109			+ SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME + "("
 110			+ SQLiteAxolotlStore.ACCOUNT + " TEXT,  "
 111			+ SQLiteAxolotlStore.ID + " INTEGER, "
 112			+ SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
 113			+ SQLiteAxolotlStore.ACCOUNT
 114			+ ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
 115			+ "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
 116			+ SQLiteAxolotlStore.ID
 117			+ ") ON CONFLICT REPLACE" +
 118			");";
 119
 120	private static String CREATE_SESSIONS_STATEMENT = "CREATE TABLE "
 121			+ SQLiteAxolotlStore.SESSION_TABLENAME + "("
 122			+ SQLiteAxolotlStore.ACCOUNT + " TEXT,  "
 123			+ SQLiteAxolotlStore.NAME + " TEXT, "
 124			+ SQLiteAxolotlStore.DEVICE_ID + " INTEGER, "
 125			+ SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
 126			+ SQLiteAxolotlStore.ACCOUNT
 127			+ ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
 128			+ "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
 129			+ SQLiteAxolotlStore.NAME + ", "
 130			+ SQLiteAxolotlStore.DEVICE_ID
 131			+ ") ON CONFLICT REPLACE"
 132			+ ");";
 133
 134	private static String CREATE_IDENTITIES_STATEMENT = "CREATE TABLE "
 135			+ SQLiteAxolotlStore.IDENTITIES_TABLENAME + "("
 136			+ SQLiteAxolotlStore.ACCOUNT + " TEXT,  "
 137			+ SQLiteAxolotlStore.NAME + " TEXT, "
 138			+ SQLiteAxolotlStore.OWN + " INTEGER, "
 139			+ SQLiteAxolotlStore.FINGERPRINT + " TEXT, "
 140			+ SQLiteAxolotlStore.CERTIFICATE + " BLOB, "
 141			+ SQLiteAxolotlStore.TRUST + " TEXT, "
 142			+ SQLiteAxolotlStore.ACTIVE + " NUMBER, "
 143			+ SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER,"
 144			+ SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
 145			+ SQLiteAxolotlStore.ACCOUNT
 146			+ ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
 147			+ "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
 148			+ SQLiteAxolotlStore.NAME + ", "
 149			+ SQLiteAxolotlStore.FINGERPRINT
 150			+ ") ON CONFLICT IGNORE"
 151			+ ");";
 152
 153	private static String RESOLVER_RESULTS_TABLENAME = "resolver_results";
 154
 155	private static String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
 156			+ Resolver.Result.DOMAIN + " TEXT,"
 157			+ Resolver.Result.HOSTNAME + " TEXT,"
 158			+ Resolver.Result.IP + " BLOB,"
 159			+ Resolver.Result.PRIORITY + " NUMBER,"
 160			+ Resolver.Result.DIRECT_TLS + " NUMBER,"
 161			+ Resolver.Result.AUTHENTICATED + " NUMBER,"
 162			+ Resolver.Result.PORT + " NUMBER,"
 163			+ "UNIQUE(" + Resolver.Result.DOMAIN + ") ON CONFLICT REPLACE"
 164			+ ");";
 165
 166	private static String CREATE_MESSAGE_TIME_INDEX = "create INDEX message_time_index ON " + Message.TABLENAME + "(" + Message.TIME_SENT + ")";
 167	private static String CREATE_MESSAGE_CONVERSATION_INDEX = "create INDEX message_conversation_index ON " + Message.TABLENAME + "(" + Message.CONVERSATION + ")";
 168
 169	private static String CREATE_MESSAGE_INDEX_TABLE = "CREATE VIRTUAL TABLE messages_index USING FTS4(uuid TEXT PRIMARY KEY, body TEXT)";
 170	private static String CREATE_MESSAGE_INSERT_TRIGGER = "CREATE TRIGGER after_message_insert AFTER INSERT ON " + Message.TABLENAME + " BEGIN INSERT INTO messages_index (uuid,body) VALUES (new.uuid,new.body); END;";
 171	private static String CREATE_MESSAGE_UPDATE_TRIGGER = "CREATE TRIGGER after_message_update UPDATE of uuid,body ON " + Message.TABLENAME + " BEGIN update messages_index set body=new.body,uuid=new.uuid WHERE uuid=old.uuid; END;";
 172	private static String COPY_PREEXISTING_ENTRIES = "INSERT into messages_index(uuid,body) select uuid,body FROM " + Message.TABLENAME + ";";
 173
 174	private DatabaseBackend(Context context) {
 175		super(context, DATABASE_NAME, null, DATABASE_VERSION);
 176	}
 177
 178	private static ContentValues createFingerprintStatusContentValues(FingerprintStatus.Trust trust, boolean active) {
 179		ContentValues values = new ContentValues();
 180		values.put(SQLiteAxolotlStore.TRUST, trust.toString());
 181		values.put(SQLiteAxolotlStore.ACTIVE, active ? 1 : 0);
 182		return values;
 183	}
 184
 185	public static synchronized DatabaseBackend getInstance(Context context) {
 186		if (instance == null) {
 187			instance = new DatabaseBackend(context);
 188		}
 189		return instance;
 190	}
 191
 192	@Override
 193	public void onConfigure(SQLiteDatabase db) {
 194		db.execSQL("PRAGMA foreign_keys=ON");
 195		db.rawQuery("PRAGMA secure_delete=ON",null);
 196	}
 197
 198	@Override
 199	public void onCreate(SQLiteDatabase db) {
 200		db.execSQL("create table " + Account.TABLENAME + "(" + Account.UUID + " TEXT PRIMARY KEY,"
 201				+ Account.USERNAME + " TEXT,"
 202				+ Account.SERVER + " TEXT,"
 203				+ Account.PASSWORD + " TEXT,"
 204				+ Account.DISPLAY_NAME + " TEXT, "
 205				+ Account.STATUS + " TEXT,"
 206				+ Account.STATUS_MESSAGE + " TEXT,"
 207				+ Account.ROSTERVERSION + " TEXT,"
 208				+ Account.OPTIONS + " NUMBER, "
 209				+ Account.AVATAR + " TEXT, "
 210				+ Account.KEYS + " TEXT, "
 211				+ Account.HOSTNAME + " TEXT, "
 212				+ Account.RESOURCE + " TEXT,"
 213				+ Account.PORT + " NUMBER DEFAULT 5222)");
 214		db.execSQL("create table " + Conversation.TABLENAME + " ("
 215				+ Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME
 216				+ " TEXT, " + Conversation.CONTACT + " TEXT, "
 217				+ Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID
 218				+ " TEXT, " + Conversation.CREATED + " NUMBER, "
 219				+ Conversation.STATUS + " NUMBER, " + Conversation.MODE
 220				+ " NUMBER, " + Conversation.ATTRIBUTES + " TEXT, FOREIGN KEY("
 221				+ Conversation.ACCOUNT + ") REFERENCES " + Account.TABLENAME
 222				+ "(" + Account.UUID + ") ON DELETE CASCADE);");
 223		db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
 224				+ " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
 225				+ Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
 226				+ " TEXT, " + Message.TRUE_COUNTERPART + " TEXT,"
 227				+ Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, "
 228				+ Message.STATUS + " NUMBER," + Message.TYPE + " NUMBER, "
 229				+ Message.RELATIVE_FILE_PATH + " TEXT, "
 230				+ Message.SERVER_MSG_ID + " TEXT, "
 231				+ Message.FINGERPRINT + " TEXT, "
 232				+ Message.CARBON + " INTEGER, "
 233				+ Message.EDITED + " TEXT, "
 234				+ Message.READ + " NUMBER DEFAULT 1, "
 235				+ Message.OOB + " INTEGER, "
 236				+ Message.ERROR_MESSAGE + " TEXT,"
 237				+ Message.READ_BY_MARKERS + " TEXT,"
 238				+ Message.MARKABLE + " NUMBER DEFAULT 0,"
 239				+ Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY("
 240				+ Message.CONVERSATION + ") REFERENCES "
 241				+ Conversation.TABLENAME + "(" + Conversation.UUID
 242				+ ") ON DELETE CASCADE);");
 243		db.execSQL(CREATE_MESSAGE_TIME_INDEX);
 244		db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
 245		db.execSQL(CREATE_CONTATCS_STATEMENT);
 246		db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
 247		db.execSQL(CREATE_SESSIONS_STATEMENT);
 248		db.execSQL(CREATE_PREKEYS_STATEMENT);
 249		db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
 250		db.execSQL(CREATE_IDENTITIES_STATEMENT);
 251		db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
 252		db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
 253		db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
 254		db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
 255		db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
 256	}
 257
 258	@Override
 259	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 260		if (oldVersion < 2 && newVersion >= 2) {
 261			db.execSQL("update " + Account.TABLENAME + " set "
 262					+ Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
 263		}
 264		if (oldVersion < 3 && newVersion >= 3) {
 265			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 266					+ Message.TYPE + " NUMBER");
 267		}
 268		if (oldVersion < 5 && newVersion >= 5) {
 269			db.execSQL("DROP TABLE " + Contact.TABLENAME);
 270			db.execSQL(CREATE_CONTATCS_STATEMENT);
 271			db.execSQL("UPDATE " + Account.TABLENAME + " SET "
 272					+ Account.ROSTERVERSION + " = NULL");
 273		}
 274		if (oldVersion < 6 && newVersion >= 6) {
 275			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 276					+ Message.TRUE_COUNTERPART + " TEXT");
 277		}
 278		if (oldVersion < 7 && newVersion >= 7) {
 279			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 280					+ Message.REMOTE_MSG_ID + " TEXT");
 281			db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
 282					+ Contact.AVATAR + " TEXT");
 283			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN "
 284					+ Account.AVATAR + " TEXT");
 285		}
 286		if (oldVersion < 8 && newVersion >= 8) {
 287			db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN "
 288					+ Conversation.ATTRIBUTES + " TEXT");
 289		}
 290		if (oldVersion < 9 && newVersion >= 9) {
 291			db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
 292					+ Contact.LAST_TIME + " NUMBER");
 293			db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
 294					+ Contact.LAST_PRESENCE + " TEXT");
 295		}
 296		if (oldVersion < 10 && newVersion >= 10) {
 297			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 298					+ Message.RELATIVE_FILE_PATH + " TEXT");
 299		}
 300		if (oldVersion < 11 && newVersion >= 11) {
 301			db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
 302					+ Contact.GROUPS + " TEXT");
 303			db.execSQL("delete from " + Contact.TABLENAME);
 304			db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
 305		}
 306		if (oldVersion < 12 && newVersion >= 12) {
 307			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 308					+ Message.SERVER_MSG_ID + " TEXT");
 309		}
 310		if (oldVersion < 13 && newVersion >= 13) {
 311			db.execSQL("delete from " + Contact.TABLENAME);
 312			db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
 313		}
 314		if (oldVersion < 14 && newVersion >= 14) {
 315			canonicalizeJids(db);
 316		}
 317		if (oldVersion < 15 && newVersion >= 15) {
 318			recreateAxolotlDb(db);
 319			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 320					+ Message.FINGERPRINT + " TEXT");
 321		}
 322		if (oldVersion < 16 && newVersion >= 16) {
 323			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
 324					+ Message.CARBON + " INTEGER");
 325		}
 326		if (oldVersion < 19 && newVersion >= 19) {
 327			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.DISPLAY_NAME + " TEXT");
 328		}
 329		if (oldVersion < 20 && newVersion >= 20) {
 330			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.HOSTNAME + " TEXT");
 331			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PORT + " NUMBER DEFAULT 5222");
 332		}
 333		if (oldVersion < 26 && newVersion >= 26) {
 334			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS + " TEXT");
 335			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS_MESSAGE + " TEXT");
 336		}
 337		if (oldVersion < 40 && newVersion >= 40) {
 338			db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.RESOURCE + " TEXT");
 339		}
 340		/* Any migrations that alter the Account table need to happen BEFORE this migration, as it
 341		 * depends on account de-serialization.
 342		 */
 343		if (oldVersion < 17 && newVersion >= 17 && newVersion < 31) {
 344			List<Account> accounts = getAccounts(db);
 345			for (Account account : accounts) {
 346				String ownDeviceIdString = account.getKey(SQLiteAxolotlStore.JSONKEY_REGISTRATION_ID);
 347				if (ownDeviceIdString == null) {
 348					continue;
 349				}
 350				int ownDeviceId = Integer.valueOf(ownDeviceIdString);
 351				SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), ownDeviceId);
 352				deleteSession(db, account, ownAddress);
 353				IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
 354				if (identityKeyPair != null) {
 355					String[] selectionArgs = {
 356							account.getUuid(),
 357							CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize())
 358					};
 359					ContentValues values = new ContentValues();
 360					values.put(SQLiteAxolotlStore.TRUSTED, 2);
 361					db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
 362							SQLiteAxolotlStore.ACCOUNT + " = ? AND "
 363									+ SQLiteAxolotlStore.FINGERPRINT + " = ? ",
 364							selectionArgs);
 365				} else {
 366					Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not load own identity key pair");
 367				}
 368			}
 369		}
 370		if (oldVersion < 18 && newVersion >= 18) {
 371			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ + " NUMBER DEFAULT 1");
 372		}
 373
 374		if (oldVersion < 21 && newVersion >= 21) {
 375			List<Account> accounts = getAccounts(db);
 376			for (Account account : accounts) {
 377				account.unsetPgpSignature();
 378				db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
 379						+ "=?", new String[]{account.getUuid()});
 380			}
 381		}
 382
 383		if (oldVersion >= 15 && oldVersion < 22 && newVersion >= 22) {
 384			db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.CERTIFICATE);
 385		}
 386
 387		if (oldVersion < 23 && newVersion >= 23) {
 388			db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
 389		}
 390
 391		if (oldVersion < 24 && newVersion >= 24) {
 392			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.EDITED + " TEXT");
 393		}
 394
 395		if (oldVersion < 25 && newVersion >= 25) {
 396			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.OOB + " INTEGER");
 397		}
 398
 399		if (oldVersion < 26 && newVersion >= 26) {
 400			db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
 401		}
 402
 403		if (oldVersion < 27 && newVersion >= 27) {
 404			db.execSQL("DELETE FROM " + ServiceDiscoveryResult.TABLENAME);
 405		}
 406
 407		if (oldVersion < 28 && newVersion >= 28) {
 408			canonicalizeJids(db);
 409		}
 410
 411		if (oldVersion < 29 && newVersion >= 29) {
 412			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.ERROR_MESSAGE + " TEXT");
 413		}
 414		if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
 415			db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.TRUST + " TEXT");
 416			db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.ACTIVE + " NUMBER");
 417			HashMap<Integer, ContentValues> migration = new HashMap<>();
 418			migration.put(0, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
 419			migration.put(1, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
 420			migration.put(2, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, true));
 421			migration.put(3, createFingerprintStatusContentValues(FingerprintStatus.Trust.COMPROMISED, false));
 422			migration.put(4, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
 423			migration.put(5, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
 424			migration.put(6, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, false));
 425			migration.put(7, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, true));
 426			migration.put(8, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, false));
 427			for (Map.Entry<Integer, ContentValues> entry : migration.entrySet()) {
 428				String whereClause = SQLiteAxolotlStore.TRUSTED + "=?";
 429				String[] where = {String.valueOf(entry.getKey())};
 430				db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, entry.getValue(), whereClause, where);
 431			}
 432
 433		}
 434		if (oldVersion >= 15 && oldVersion < 32 && newVersion >= 32) {
 435			db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER");
 436			ContentValues defaults = new ContentValues();
 437			defaults.put(SQLiteAxolotlStore.LAST_ACTIVATION, System.currentTimeMillis());
 438			db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, defaults, null, null);
 439		}
 440		if (oldVersion >= 15 && oldVersion < 33 && newVersion >= 33) {
 441			String whereClause = SQLiteAxolotlStore.OWN + "=1";
 442			db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED, true), whereClause, null);
 443		}
 444
 445		if (oldVersion < 34 && newVersion >= 34) {
 446			db.execSQL(CREATE_MESSAGE_TIME_INDEX);
 447
 448			final File oldPicturesDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Conversations/");
 449			final File oldFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/");
 450			final File newFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Files/");
 451			final File newVideosDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Videos/");
 452			if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
 453				final File newPicturesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Images/");
 454				newPicturesDirectory.getParentFile().mkdirs();
 455				if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
 456					Log.d(Config.LOGTAG, "moved " + oldPicturesDirectory.getAbsolutePath() + " to " + newPicturesDirectory.getAbsolutePath());
 457				}
 458			}
 459			if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
 460				newFilesDirectory.mkdirs();
 461				newVideosDirectory.mkdirs();
 462				final File[] files = oldFilesDirectory.listFiles();
 463				if (files == null) {
 464					return;
 465				}
 466				for (File file : files) {
 467					if (file.getName().equals(".nomedia")) {
 468						if (file.delete()) {
 469							Log.d(Config.LOGTAG, "deleted nomedia file in " + oldFilesDirectory.getAbsolutePath());
 470						}
 471					} else if (file.isFile()) {
 472						final String name = file.getName();
 473						boolean isVideo = false;
 474						int start = name.lastIndexOf('.') + 1;
 475						if (start < name.length()) {
 476							String mime = MimeUtils.guessMimeTypeFromExtension(name.substring(start));
 477							isVideo = mime != null && mime.startsWith("video/");
 478						}
 479						File dst = new File((isVideo ? newVideosDirectory : newFilesDirectory).getAbsolutePath() + "/" + file.getName());
 480						if (file.renameTo(dst)) {
 481							Log.d(Config.LOGTAG, "moved " + file + " to " + dst);
 482						}
 483					}
 484				}
 485			}
 486		}
 487		if (oldVersion < 35 && newVersion >= 35) {
 488			db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
 489		}
 490		if (oldVersion < 36 && newVersion >= 36) {
 491			List<Account> accounts = getAccounts(db);
 492			for (Account account : accounts) {
 493				account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true);
 494				account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, false);
 495				db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
 496						+ "=?", new String[]{account.getUuid()});
 497			}
 498		}
 499
 500		if (oldVersion < 37 && newVersion >= 37) {
 501			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ_BY_MARKERS + " TEXT");
 502		}
 503
 504		if (oldVersion < 38 && newVersion >= 38) {
 505			db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.MARKABLE + " NUMBER DEFAULT 0");
 506		}
 507
 508		if (oldVersion < 39 && newVersion >= 39) {
 509			db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
 510		}
 511
 512		if (oldVersion < 41 && newVersion >= 41) {
 513			db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
 514			db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
 515			db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
 516			db.execSQL(COPY_PREEXISTING_ENTRIES);
 517		}
 518
 519		if (oldVersion < 42 && newVersion >= 42) {
 520			db.execSQL("DROP TRIGGER IF EXISTS after_message_delete");
 521		}
 522		if (QuickConversationsService.isQuicksy() && oldVersion < 43 && newVersion >= 43) {
 523			List<Account> accounts = getAccounts(db);
 524			for (Account account : accounts) {
 525				account.setOption(Account.OPTION_MAGIC_CREATE, true);
 526				db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
 527						+ "=?", new String[]{account.getUuid()});
 528			}
 529		}
 530	}
 531
 532	private void canonicalizeJids(SQLiteDatabase db) {
 533		// migrate db to new, canonicalized JID domainpart representation
 534
 535		// Conversation table
 536		Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]);
 537		while (cursor.moveToNext()) {
 538			String newJid;
 539			try {
 540				newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))).toString();
 541			} catch (IllegalArgumentException ignored) {
 542				Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID "
 543						+ cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))
 544						+ ": " + ignored + ". Skipping...");
 545				continue;
 546			}
 547
 548			String updateArgs[] = {
 549					newJid,
 550					cursor.getString(cursor.getColumnIndex(Conversation.UUID)),
 551			};
 552			db.execSQL("update " + Conversation.TABLENAME
 553					+ " set " + Conversation.CONTACTJID + " = ? "
 554					+ " where " + Conversation.UUID + " = ?", updateArgs);
 555		}
 556		cursor.close();
 557
 558		// Contact table
 559		cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]);
 560		while (cursor.moveToNext()) {
 561			String newJid;
 562			try {
 563				newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Contact.JID))).toString();
 564			} catch (IllegalArgumentException ignored) {
 565				Log.e(Config.LOGTAG, "Failed to migrate Contact JID "
 566						+ cursor.getString(cursor.getColumnIndex(Contact.JID))
 567						+ ": " + ignored + ". Skipping...");
 568				continue;
 569			}
 570
 571			String updateArgs[] = {
 572					newJid,
 573					cursor.getString(cursor.getColumnIndex(Contact.ACCOUNT)),
 574					cursor.getString(cursor.getColumnIndex(Contact.JID)),
 575			};
 576			db.execSQL("update " + Contact.TABLENAME
 577					+ " set " + Contact.JID + " = ? "
 578					+ " where " + Contact.ACCOUNT + " = ? "
 579					+ " AND " + Contact.JID + " = ?", updateArgs);
 580		}
 581		cursor.close();
 582
 583		// Account table
 584		cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]);
 585		while (cursor.moveToNext()) {
 586			String newServer;
 587			try {
 588				newServer = Jid.of(
 589						cursor.getString(cursor.getColumnIndex(Account.USERNAME)),
 590						cursor.getString(cursor.getColumnIndex(Account.SERVER)),
 591						null
 592				).getDomain();
 593			} catch (IllegalArgumentException ignored) {
 594				Log.e(Config.LOGTAG, "Failed to migrate Account SERVER "
 595						+ cursor.getString(cursor.getColumnIndex(Account.SERVER))
 596						+ ": " + ignored + ". Skipping...");
 597				continue;
 598			}
 599
 600			String updateArgs[] = {
 601					newServer,
 602					cursor.getString(cursor.getColumnIndex(Account.UUID)),
 603			};
 604			db.execSQL("update " + Account.TABLENAME
 605					+ " set " + Account.SERVER + " = ? "
 606					+ " where " + Account.UUID + " = ?", updateArgs);
 607		}
 608		cursor.close();
 609	}
 610
 611	public void createConversation(Conversation conversation) {
 612		SQLiteDatabase db = this.getWritableDatabase();
 613		db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
 614	}
 615
 616	public void createMessage(Message message) {
 617		SQLiteDatabase db = this.getWritableDatabase();
 618		db.insert(Message.TABLENAME, null, message.getContentValues());
 619	}
 620
 621	public void createAccount(Account account) {
 622		SQLiteDatabase db = this.getWritableDatabase();
 623		db.insert(Account.TABLENAME, null, account.getContentValues());
 624	}
 625
 626	public void insertDiscoveryResult(ServiceDiscoveryResult result) {
 627		SQLiteDatabase db = this.getWritableDatabase();
 628		db.insert(ServiceDiscoveryResult.TABLENAME, null, result.getContentValues());
 629	}
 630
 631	public ServiceDiscoveryResult findDiscoveryResult(final String hash, final String ver) {
 632		SQLiteDatabase db = this.getReadableDatabase();
 633		String[] selectionArgs = {hash, ver};
 634		Cursor cursor = db.query(ServiceDiscoveryResult.TABLENAME, null,
 635				ServiceDiscoveryResult.HASH + "=? AND " + ServiceDiscoveryResult.VER + "=?",
 636				selectionArgs, null, null, null);
 637		if (cursor.getCount() == 0) {
 638			cursor.close();
 639			return null;
 640		}
 641		cursor.moveToFirst();
 642
 643		ServiceDiscoveryResult result = null;
 644		try {
 645			result = new ServiceDiscoveryResult(cursor);
 646		} catch (JSONException e) { /* result is still null */ }
 647
 648		cursor.close();
 649		return result;
 650	}
 651
 652	public void saveResolverResult(String domain, Resolver.Result result) {
 653		SQLiteDatabase db = this.getWritableDatabase();
 654		ContentValues contentValues = result.toContentValues();
 655		contentValues.put(Resolver.Result.DOMAIN, domain);
 656		db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
 657	}
 658
 659	public synchronized Resolver.Result findResolverResult(String domain) {
 660		SQLiteDatabase db = this.getReadableDatabase();
 661		String where = Resolver.Result.DOMAIN + "=?";
 662		String[] whereArgs = {domain};
 663		final Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
 664		Resolver.Result result = null;
 665		if (cursor != null) {
 666			try {
 667				if (cursor.moveToFirst()) {
 668					result = Resolver.Result.fromCursor(cursor);
 669				}
 670			} catch (Exception e ) {
 671				Log.d(Config.LOGTAG,"unable to find cached resolver result in database "+e.getMessage());
 672				return null;
 673			} finally {
 674				cursor.close();
 675			}
 676		}
 677		return result;
 678	}
 679
 680	public void insertPresenceTemplate(PresenceTemplate template) {
 681		SQLiteDatabase db = this.getWritableDatabase();
 682		String whereToDelete = PresenceTemplate.MESSAGE + "=?";
 683		String[] whereToDeleteArgs = {template.getStatusMessage()};
 684		db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs);
 685		db.delete(PresenceTemplate.TABELNAME, PresenceTemplate.UUID + " not in (select " + PresenceTemplate.UUID + " from " + PresenceTemplate.TABELNAME + " order by " + PresenceTemplate.LAST_USED + " desc limit 9)", null);
 686		db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
 687	}
 688
 689	public List<PresenceTemplate> getPresenceTemplates() {
 690		ArrayList<PresenceTemplate> templates = new ArrayList<>();
 691		SQLiteDatabase db = this.getReadableDatabase();
 692		Cursor cursor = db.query(PresenceTemplate.TABELNAME, null, null, null, null, null, PresenceTemplate.LAST_USED + " desc");
 693		while (cursor.moveToNext()) {
 694			templates.add(PresenceTemplate.fromCursor(cursor));
 695		}
 696		cursor.close();
 697		return templates;
 698	}
 699
 700	public CopyOnWriteArrayList<Conversation> getConversations(int status) {
 701		CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>();
 702		SQLiteDatabase db = this.getReadableDatabase();
 703		String[] selectionArgs = {Integer.toString(status)};
 704		Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME
 705				+ " where " + Conversation.STATUS + " = ? and " + Conversation.CONTACTJID + " is not null order by "
 706				+ Conversation.CREATED + " desc", selectionArgs);
 707		while (cursor.moveToNext()) {
 708			final Conversation conversation = Conversation.fromCursor(cursor);
 709			if (conversation.getJid() instanceof InvalidJid) {
 710				continue;
 711			}
 712			list.add(conversation);
 713		}
 714		cursor.close();
 715		return list;
 716	}
 717
 718	public ArrayList<Message> getMessages(Conversation conversations, int limit) {
 719		return getMessages(conversations, limit, -1);
 720	}
 721
 722	public ArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
 723		ArrayList<Message> list = new ArrayList<>();
 724		SQLiteDatabase db = this.getReadableDatabase();
 725		Cursor cursor;
 726		if (timestamp == -1) {
 727			String[] selectionArgs = {conversation.getUuid()};
 728			cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
 729					+ "=?", selectionArgs, null, null, Message.TIME_SENT
 730					+ " DESC", String.valueOf(limit));
 731		} else {
 732			String[] selectionArgs = {conversation.getUuid(),
 733					Long.toString(timestamp)};
 734			cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
 735							+ "=? and " + Message.TIME_SENT + "<?", selectionArgs,
 736					null, null, Message.TIME_SENT + " DESC",
 737					String.valueOf(limit));
 738		}
 739		if (cursor.getCount() > 0) {
 740			cursor.moveToLast();
 741			do {
 742				Message message = Message.fromCursor(cursor, conversation);
 743				if (message != null) {
 744					list.add(message);
 745				}
 746			} while (cursor.moveToPrevious());
 747		}
 748		cursor.close();
 749		return list;
 750	}
 751
 752	public Cursor getMessageSearchCursor(List<String> term) {
 753		SQLiteDatabase db = this.getReadableDatabase();
 754		String SQL = "SELECT " + Message.TABLENAME + ".*," + Conversation.TABLENAME + '.' + Conversation.CONTACTJID + ',' + Conversation.TABLENAME + '.' + Conversation.ACCOUNT + ',' + Conversation.TABLENAME + '.' + Conversation.MODE + " FROM " + Message.TABLENAME + " join " + Conversation.TABLENAME + " on " + Message.TABLENAME + '.' + Message.CONVERSATION + '=' + Conversation.TABLENAME + '.' + Conversation.UUID + " join messages_index ON messages_index.uuid=messages.uuid where " + Message.ENCRYPTION + " NOT IN(" + Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE + ',' + Message.ENCRYPTION_PGP + ',' + Message.ENCRYPTION_DECRYPTION_FAILED + ") AND " + Message.TYPE + " IN(" + Message.TYPE_TEXT + ',' + Message.TYPE_PRIVATE + ") AND messages_index.body MATCH ? ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS;
 755		Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term));
 756		return db.rawQuery(SQL, new String[]{FtsUtils.toMatchString(term)});
 757	}
 758
 759	public Iterable<Message> getMessagesIterable(final Conversation conversation) {
 760		return () -> {
 761			class MessageIterator implements Iterator<Message> {
 762				private SQLiteDatabase db = getReadableDatabase();
 763				private String[] selectionArgs = {conversation.getUuid()};
 764				private Cursor cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
 765						+ "=?", selectionArgs, null, null, Message.TIME_SENT
 766						+ " ASC", null);
 767
 768				private MessageIterator() {
 769					cursor.moveToFirst();
 770				}
 771
 772				@Override
 773				public boolean hasNext() {
 774					return !cursor.isAfterLast();
 775				}
 776
 777				@Override
 778				public Message next() {
 779					Message message = Message.fromCursor(cursor, conversation);
 780					cursor.moveToNext();
 781					return message;
 782				}
 783
 784				@Override
 785				public void remove() {
 786					throw new UnsupportedOperationException();
 787				}
 788			}
 789			return new MessageIterator();
 790		};
 791	}
 792
 793	public List<FilePath> getRelativeFilePaths(String account, Jid jid, int limit) {
 794		SQLiteDatabase db = this.getReadableDatabase();
 795		final String SQL = "select uuid,relativeFilePath from messages where type in (1,2) and conversationUuid=(select uuid from conversations where accountUuid=? and (contactJid=? or contactJid like ?)) order by timeSent desc";
 796		final String[] args = {account, jid.toEscapedString(), jid.toEscapedString()+"/%"};
 797		Cursor cursor = db.rawQuery(SQL+(limit > 0 ? " limit "+String.valueOf(limit) : ""), args);
 798		List<FilePath> filesPaths = new ArrayList<>();
 799		while(cursor.moveToNext()) {
 800			filesPaths.add(new FilePath(cursor.getString(0),cursor.getString(1)));
 801		}
 802		cursor.close();
 803		return filesPaths;
 804	}
 805
 806	public static class FilePath {
 807		public final UUID uuid;
 808		public final String path;
 809		private FilePath(String uuid, String path) {
 810			this.uuid = UUID.fromString(uuid);
 811			this.path = path;
 812		}
 813	}
 814
 815	public Conversation findConversation(final Account account, final Jid contactJid) {
 816		SQLiteDatabase db = this.getReadableDatabase();
 817		String[] selectionArgs = {account.getUuid(),
 818				contactJid.asBareJid().toString() + "/%",
 819				contactJid.asBareJid().toString()
 820		};
 821		Cursor cursor = db.query(Conversation.TABLENAME, null,
 822				Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID
 823						+ " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null);
 824		if (cursor.getCount() == 0) {
 825			cursor.close();
 826			return null;
 827		}
 828		cursor.moveToFirst();
 829		Conversation conversation = Conversation.fromCursor(cursor);
 830		cursor.close();
 831		if (conversation.getJid() instanceof InvalidJid) {
 832			return null;
 833		}
 834		return conversation;
 835	}
 836
 837	public void updateConversation(final Conversation conversation) {
 838		final SQLiteDatabase db = this.getWritableDatabase();
 839		final String[] args = {conversation.getUuid()};
 840		db.update(Conversation.TABLENAME, conversation.getContentValues(),
 841				Conversation.UUID + "=?", args);
 842	}
 843
 844	public List<Account> getAccounts() {
 845		SQLiteDatabase db = this.getReadableDatabase();
 846		return getAccounts(db);
 847	}
 848
 849	public List<Jid> getAccountJids() {
 850		SQLiteDatabase db = this.getReadableDatabase();
 851		final List<Jid> jids = new ArrayList<>();
 852		final String[] columns = new String[]{Account.USERNAME, Account.SERVER};
 853		String where = "not options & (1 <<1)";
 854		Cursor cursor = db.query(Account.TABLENAME, columns, where, null, null, null, null);
 855		try {
 856			while (cursor.moveToNext()) {
 857				jids.add(Jid.of(cursor.getString(0), cursor.getString(1), null));
 858			}
 859			return jids;
 860		} catch (Exception e) {
 861			return jids;
 862		} finally {
 863			if (cursor != null) {
 864				cursor.close();
 865			}
 866		}
 867	}
 868
 869	private List<Account> getAccounts(SQLiteDatabase db) {
 870		List<Account> list = new ArrayList<>();
 871		Cursor cursor = db.query(Account.TABLENAME, null, null, null, null,
 872				null, null);
 873		while (cursor.moveToNext()) {
 874			list.add(Account.fromCursor(cursor));
 875		}
 876		cursor.close();
 877		return list;
 878	}
 879
 880	public boolean updateAccount(Account account) {
 881		SQLiteDatabase db = this.getWritableDatabase();
 882		String[] args = {account.getUuid()};
 883		final int rows = db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
 884		return rows == 1;
 885	}
 886
 887	public boolean deleteAccount(Account account) {
 888		SQLiteDatabase db = this.getWritableDatabase();
 889		String[] args = {account.getUuid()};
 890		final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
 891		return rows == 1;
 892	}
 893
 894	public boolean updateMessage(Message message, boolean includeBody) {
 895		SQLiteDatabase db = this.getWritableDatabase();
 896		String[] args = {message.getUuid()};
 897		ContentValues contentValues = message.getContentValues();
 898		contentValues.remove(Message.UUID);
 899		if (!includeBody) {
 900			contentValues.remove(Message.BODY);
 901		}
 902		return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1;
 903	}
 904
 905	public void updateMessage(Message message, String uuid) {
 906		SQLiteDatabase db = this.getWritableDatabase();
 907		String[] args = {uuid};
 908		db.update(Message.TABLENAME, message.getContentValues(), Message.UUID
 909				+ "=?", args);
 910	}
 911
 912	public void readRoster(Roster roster) {
 913		SQLiteDatabase db = this.getReadableDatabase();
 914		Cursor cursor;
 915		String args[] = {roster.getAccount().getUuid()};
 916		cursor = db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?", args, null, null, null);
 917		while (cursor.moveToNext()) {
 918			roster.initContact(Contact.fromCursor(cursor));
 919		}
 920		cursor.close();
 921	}
 922
 923	public void writeRoster(final Roster roster) {
 924		long start = SystemClock.elapsedRealtime();
 925		final Account account = roster.getAccount();
 926		final SQLiteDatabase db = this.getWritableDatabase();
 927		db.beginTransaction();
 928		for (Contact contact : roster.getContacts()) {
 929			if (contact.getOption(Contact.Options.IN_ROSTER) || contact.getAvatarFilename() != null || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
 930				db.insert(Contact.TABLENAME, null, contact.getContentValues());
 931			} else {
 932				String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
 933				String[] whereArgs = {account.getUuid(), contact.getJid().toString()};
 934				db.delete(Contact.TABLENAME, where, whereArgs);
 935			}
 936		}
 937		db.setTransactionSuccessful();
 938		db.endTransaction();
 939		account.setRosterVersion(roster.getVersion());
 940		updateAccount(account);
 941		long duration = SystemClock.elapsedRealtime() - start;
 942		Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": persisted roster in " + duration + "ms");
 943	}
 944
 945	public void deleteMessagesInConversation(Conversation conversation) {
 946		long start = SystemClock.elapsedRealtime();
 947		final SQLiteDatabase db = this.getWritableDatabase();
 948		db.beginTransaction();
 949		String[] args = {conversation.getUuid()};
 950		db.delete("messages_index", "uuid in (select uuid from messages where conversationUuid=?)", args);
 951		int num = db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
 952		db.setTransactionSuccessful();
 953		db.endTransaction();
 954		Log.d(Config.LOGTAG, "deleted " + num + " messages for " + conversation.getJid().asBareJid() + " in " + (SystemClock.elapsedRealtime() - start) + "ms");
 955	}
 956
 957	public void expireOldMessages(long timestamp) {
 958		final String[] args = {String.valueOf(timestamp)};
 959		SQLiteDatabase db = this.getReadableDatabase();
 960		db.beginTransaction();
 961		db.delete("messages_index", "uuid in (select uuid from messages where timeSent<?)", args);
 962		db.delete(Message.TABLENAME, "timeSent<?", args);
 963		db.setTransactionSuccessful();
 964		db.endTransaction();
 965	}
 966
 967	public MamReference getLastMessageReceived(Account account) {
 968		Cursor cursor = null;
 969		try {
 970			SQLiteDatabase db = this.getReadableDatabase();
 971			String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) and (conversations.mode=0 or (messages.serverMsgId not null and messages.type=4)) order by messages.timesent desc limit 1";
 972			String[] args = {account.getUuid()};
 973			cursor = db.rawQuery(sql, args);
 974			if (cursor.getCount() == 0) {
 975				return null;
 976			} else {
 977				cursor.moveToFirst();
 978				return new MamReference(cursor.getLong(0), cursor.getString(1));
 979			}
 980		} catch (Exception e) {
 981			return null;
 982		} finally {
 983			if (cursor != null) {
 984				cursor.close();
 985			}
 986		}
 987	}
 988
 989	public long getLastTimeFingerprintUsed(Account account, String fingerprint) {
 990		String SQL = "select messages.timeSent from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and messages.axolotl_fingerprint=? order by messages.timesent desc limit 1";
 991		String[] args = {account.getUuid(), fingerprint};
 992		Cursor cursor = getReadableDatabase().rawQuery(SQL, args);
 993		long time;
 994		if (cursor.moveToFirst()) {
 995			time = cursor.getLong(0);
 996		} else {
 997			time = 0;
 998		}
 999		cursor.close();
1000		return time;
1001	}
1002
1003	public MamReference getLastClearDate(Account account) {
1004		SQLiteDatabase db = this.getReadableDatabase();
1005		String[] columns = {Conversation.ATTRIBUTES};
1006		String selection = Conversation.ACCOUNT + "=?";
1007		String[] args = {account.getUuid()};
1008		Cursor cursor = db.query(Conversation.TABLENAME, columns, selection, args, null, null, null);
1009		MamReference maxClearDate = new MamReference(0);
1010		while (cursor.moveToNext()) {
1011			try {
1012				final JSONObject o = new JSONObject(cursor.getString(0));
1013				maxClearDate = MamReference.max(maxClearDate, MamReference.fromAttribute(o.getString(Conversation.ATTRIBUTE_LAST_CLEAR_HISTORY)));
1014			} catch (Exception e) {
1015				//ignored
1016			}
1017		}
1018		cursor.close();
1019		return maxClearDate;
1020	}
1021
1022	private Cursor getCursorForSession(Account account, SignalProtocolAddress contact) {
1023		final SQLiteDatabase db = this.getReadableDatabase();
1024		String[] selectionArgs = {account.getUuid(),
1025				contact.getName(),
1026				Integer.toString(contact.getDeviceId())};
1027		return db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1028				null,
1029				SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1030						+ SQLiteAxolotlStore.NAME + " = ? AND "
1031						+ SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1032				selectionArgs,
1033				null, null, null);
1034	}
1035
1036	public SessionRecord loadSession(Account account, SignalProtocolAddress contact) {
1037		SessionRecord session = null;
1038		Cursor cursor = getCursorForSession(account, contact);
1039		if (cursor.getCount() != 0) {
1040			cursor.moveToFirst();
1041			try {
1042				session = new SessionRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1043			} catch (IOException e) {
1044				cursor.close();
1045				throw new AssertionError(e);
1046			}
1047		}
1048		cursor.close();
1049		return session;
1050	}
1051
1052	public List<Integer> getSubDeviceSessions(Account account, SignalProtocolAddress contact) {
1053		final SQLiteDatabase db = this.getReadableDatabase();
1054		return getSubDeviceSessions(db, account, contact);
1055	}
1056
1057	private List<Integer> getSubDeviceSessions(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1058		List<Integer> devices = new ArrayList<>();
1059		String[] columns = {SQLiteAxolotlStore.DEVICE_ID};
1060		String[] selectionArgs = {account.getUuid(),
1061				contact.getName()};
1062		Cursor cursor = db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1063				columns,
1064				SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1065						+ SQLiteAxolotlStore.NAME + " = ?",
1066				selectionArgs,
1067				null, null, null);
1068
1069		while (cursor.moveToNext()) {
1070			devices.add(cursor.getInt(
1071					cursor.getColumnIndex(SQLiteAxolotlStore.DEVICE_ID)));
1072		}
1073
1074		cursor.close();
1075		return devices;
1076	}
1077
1078	public List<String> getKnownSignalAddresses(Account account) {
1079		List<String> addresses = new ArrayList<>();
1080		String[] colums = {"DISTINCT " + SQLiteAxolotlStore.NAME};
1081		String[] selectionArgs = {account.getUuid()};
1082		Cursor cursor = getReadableDatabase().query(SQLiteAxolotlStore.SESSION_TABLENAME,
1083				colums,
1084				SQLiteAxolotlStore.ACCOUNT + " = ?",
1085				selectionArgs,
1086				null, null, null
1087		);
1088		while (cursor.moveToNext()) {
1089			addresses.add(cursor.getString(0));
1090		}
1091		cursor.close();
1092		return addresses;
1093	}
1094
1095	public boolean containsSession(Account account, SignalProtocolAddress contact) {
1096		Cursor cursor = getCursorForSession(account, contact);
1097		int count = cursor.getCount();
1098		cursor.close();
1099		return count != 0;
1100	}
1101
1102	public void storeSession(Account account, SignalProtocolAddress contact, SessionRecord session) {
1103		SQLiteDatabase db = this.getWritableDatabase();
1104		ContentValues values = new ContentValues();
1105		values.put(SQLiteAxolotlStore.NAME, contact.getName());
1106		values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
1107		values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
1108		values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1109		db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
1110	}
1111
1112	public void deleteSession(Account account, SignalProtocolAddress contact) {
1113		SQLiteDatabase db = this.getWritableDatabase();
1114		deleteSession(db, account, contact);
1115	}
1116
1117	private void deleteSession(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1118		String[] args = {account.getUuid(),
1119				contact.getName(),
1120				Integer.toString(contact.getDeviceId())};
1121		db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1122				SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1123						+ SQLiteAxolotlStore.NAME + " = ? AND "
1124						+ SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1125				args);
1126	}
1127
1128	public void deleteAllSessions(Account account, SignalProtocolAddress contact) {
1129		SQLiteDatabase db = this.getWritableDatabase();
1130		String[] args = {account.getUuid(), contact.getName()};
1131		db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1132				SQLiteAxolotlStore.ACCOUNT + "=? AND "
1133						+ SQLiteAxolotlStore.NAME + " = ?",
1134				args);
1135	}
1136
1137	private Cursor getCursorForPreKey(Account account, int preKeyId) {
1138		SQLiteDatabase db = this.getReadableDatabase();
1139		String[] columns = {SQLiteAxolotlStore.KEY};
1140		String[] selectionArgs = {account.getUuid(), Integer.toString(preKeyId)};
1141		Cursor cursor = db.query(SQLiteAxolotlStore.PREKEY_TABLENAME,
1142				columns,
1143				SQLiteAxolotlStore.ACCOUNT + "=? AND "
1144						+ SQLiteAxolotlStore.ID + "=?",
1145				selectionArgs,
1146				null, null, null);
1147
1148		return cursor;
1149	}
1150
1151	public PreKeyRecord loadPreKey(Account account, int preKeyId) {
1152		PreKeyRecord record = null;
1153		Cursor cursor = getCursorForPreKey(account, preKeyId);
1154		if (cursor.getCount() != 0) {
1155			cursor.moveToFirst();
1156			try {
1157				record = new PreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1158			} catch (IOException e) {
1159				throw new AssertionError(e);
1160			}
1161		}
1162		cursor.close();
1163		return record;
1164	}
1165
1166	public boolean containsPreKey(Account account, int preKeyId) {
1167		Cursor cursor = getCursorForPreKey(account, preKeyId);
1168		int count = cursor.getCount();
1169		cursor.close();
1170		return count != 0;
1171	}
1172
1173	public void storePreKey(Account account, PreKeyRecord record) {
1174		SQLiteDatabase db = this.getWritableDatabase();
1175		ContentValues values = new ContentValues();
1176		values.put(SQLiteAxolotlStore.ID, record.getId());
1177		values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1178		values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1179		db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
1180	}
1181
1182	public void deletePreKey(Account account, int preKeyId) {
1183		SQLiteDatabase db = this.getWritableDatabase();
1184		String[] args = {account.getUuid(), Integer.toString(preKeyId)};
1185		db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1186				SQLiteAxolotlStore.ACCOUNT + "=? AND "
1187						+ SQLiteAxolotlStore.ID + "=?",
1188				args);
1189	}
1190
1191	private Cursor getCursorForSignedPreKey(Account account, int signedPreKeyId) {
1192		SQLiteDatabase db = this.getReadableDatabase();
1193		String[] columns = {SQLiteAxolotlStore.KEY};
1194		String[] selectionArgs = {account.getUuid(), Integer.toString(signedPreKeyId)};
1195		Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1196				columns,
1197				SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
1198				selectionArgs,
1199				null, null, null);
1200
1201		return cursor;
1202	}
1203
1204	public SignedPreKeyRecord loadSignedPreKey(Account account, int signedPreKeyId) {
1205		SignedPreKeyRecord record = null;
1206		Cursor cursor = getCursorForSignedPreKey(account, signedPreKeyId);
1207		if (cursor.getCount() != 0) {
1208			cursor.moveToFirst();
1209			try {
1210				record = new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1211			} catch (IOException e) {
1212				throw new AssertionError(e);
1213			}
1214		}
1215		cursor.close();
1216		return record;
1217	}
1218
1219	public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
1220		List<SignedPreKeyRecord> prekeys = new ArrayList<>();
1221		SQLiteDatabase db = this.getReadableDatabase();
1222		String[] columns = {SQLiteAxolotlStore.KEY};
1223		String[] selectionArgs = {account.getUuid()};
1224		Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1225				columns,
1226				SQLiteAxolotlStore.ACCOUNT + "=?",
1227				selectionArgs,
1228				null, null, null);
1229
1230		while (cursor.moveToNext()) {
1231			try {
1232				prekeys.add(new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT)));
1233			} catch (IOException ignored) {
1234			}
1235		}
1236		cursor.close();
1237		return prekeys;
1238	}
1239
1240	public int getSignedPreKeysCount(Account account) {
1241		String[] columns = {"count(" + SQLiteAxolotlStore.KEY + ")"};
1242		String[] selectionArgs = {account.getUuid()};
1243		SQLiteDatabase db = this.getReadableDatabase();
1244		Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1245				columns,
1246				SQLiteAxolotlStore.ACCOUNT + "=?",
1247				selectionArgs,
1248				null, null, null);
1249		final int count;
1250		if (cursor.moveToFirst()) {
1251			count = cursor.getInt(0);
1252		} else {
1253			count = 0;
1254		}
1255		cursor.close();
1256		return count;
1257	}
1258
1259	public boolean containsSignedPreKey(Account account, int signedPreKeyId) {
1260		Cursor cursor = getCursorForPreKey(account, signedPreKeyId);
1261		int count = cursor.getCount();
1262		cursor.close();
1263		return count != 0;
1264	}
1265
1266	public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
1267		SQLiteDatabase db = this.getWritableDatabase();
1268		ContentValues values = new ContentValues();
1269		values.put(SQLiteAxolotlStore.ID, record.getId());
1270		values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1271		values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1272		db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
1273	}
1274
1275	public void deleteSignedPreKey(Account account, int signedPreKeyId) {
1276		SQLiteDatabase db = this.getWritableDatabase();
1277		String[] args = {account.getUuid(), Integer.toString(signedPreKeyId)};
1278		db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1279				SQLiteAxolotlStore.ACCOUNT + "=? AND "
1280						+ SQLiteAxolotlStore.ID + "=?",
1281				args);
1282	}
1283
1284	private Cursor getIdentityKeyCursor(Account account, String name, boolean own) {
1285		final SQLiteDatabase db = this.getReadableDatabase();
1286		return getIdentityKeyCursor(db, account, name, own);
1287	}
1288
1289	private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, boolean own) {
1290		return getIdentityKeyCursor(db, account, name, own, null);
1291	}
1292
1293	private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
1294		final SQLiteDatabase db = this.getReadableDatabase();
1295		return getIdentityKeyCursor(db, account, fingerprint);
1296	}
1297
1298	private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String fingerprint) {
1299		return getIdentityKeyCursor(db, account, null, null, fingerprint);
1300	}
1301
1302	private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, Boolean own, String fingerprint) {
1303		String[] columns = {SQLiteAxolotlStore.TRUST,
1304				SQLiteAxolotlStore.ACTIVE,
1305				SQLiteAxolotlStore.LAST_ACTIVATION,
1306				SQLiteAxolotlStore.KEY};
1307		ArrayList<String> selectionArgs = new ArrayList<>(4);
1308		selectionArgs.add(account.getUuid());
1309		String selectionString = SQLiteAxolotlStore.ACCOUNT + " = ?";
1310		if (name != null) {
1311			selectionArgs.add(name);
1312			selectionString += " AND " + SQLiteAxolotlStore.NAME + " = ?";
1313		}
1314		if (fingerprint != null) {
1315			selectionArgs.add(fingerprint);
1316			selectionString += " AND " + SQLiteAxolotlStore.FINGERPRINT + " = ?";
1317		}
1318		if (own != null) {
1319			selectionArgs.add(own ? "1" : "0");
1320			selectionString += " AND " + SQLiteAxolotlStore.OWN + " = ?";
1321		}
1322		Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1323				columns,
1324				selectionString,
1325				selectionArgs.toArray(new String[selectionArgs.size()]),
1326				null, null, null);
1327
1328		return cursor;
1329	}
1330
1331	public IdentityKeyPair loadOwnIdentityKeyPair(Account account) {
1332		SQLiteDatabase db = getReadableDatabase();
1333		return loadOwnIdentityKeyPair(db, account);
1334	}
1335
1336	private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
1337		String name = account.getJid().asBareJid().toString();
1338		IdentityKeyPair identityKeyPair = null;
1339		Cursor cursor = getIdentityKeyCursor(db, account, name, true);
1340		if (cursor.getCount() != 0) {
1341			cursor.moveToFirst();
1342			try {
1343				identityKeyPair = new IdentityKeyPair(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1344			} catch (InvalidKeyException e) {
1345				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1346			}
1347		}
1348		cursor.close();
1349
1350		return identityKeyPair;
1351	}
1352
1353	public Set<IdentityKey> loadIdentityKeys(Account account, String name) {
1354		return loadIdentityKeys(account, name, null);
1355	}
1356
1357	public Set<IdentityKey> loadIdentityKeys(Account account, String name, FingerprintStatus status) {
1358		Set<IdentityKey> identityKeys = new HashSet<>();
1359		Cursor cursor = getIdentityKeyCursor(account, name, false);
1360
1361		while (cursor.moveToNext()) {
1362			if (status != null && !FingerprintStatus.fromCursor(cursor).equals(status)) {
1363				continue;
1364			}
1365			try {
1366				String key = cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY));
1367				if (key != null) {
1368					identityKeys.add(new IdentityKey(Base64.decode(key, Base64.DEFAULT), 0));
1369				} else {
1370					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Missing key (possibly preverified) in database for account" + account.getJid().asBareJid() + ", address: " + name);
1371				}
1372			} catch (InvalidKeyException e) {
1373				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1374			}
1375		}
1376		cursor.close();
1377
1378		return identityKeys;
1379	}
1380
1381	public long numTrustedKeys(Account account, String name) {
1382		SQLiteDatabase db = getReadableDatabase();
1383		String[] args = {
1384				account.getUuid(),
1385				name,
1386				FingerprintStatus.Trust.TRUSTED.toString(),
1387				FingerprintStatus.Trust.VERIFIED.toString(),
1388				FingerprintStatus.Trust.VERIFIED_X509.toString()
1389		};
1390		return DatabaseUtils.queryNumEntries(db, SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1391				SQLiteAxolotlStore.ACCOUNT + " = ?"
1392						+ " AND " + SQLiteAxolotlStore.NAME + " = ?"
1393						+ " AND (" + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ?)"
1394						+ " AND " + SQLiteAxolotlStore.ACTIVE + " > 0",
1395				args
1396		);
1397	}
1398
1399	private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized, FingerprintStatus status) {
1400		SQLiteDatabase db = this.getWritableDatabase();
1401		ContentValues values = new ContentValues();
1402		values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1403		values.put(SQLiteAxolotlStore.NAME, name);
1404		values.put(SQLiteAxolotlStore.OWN, own ? 1 : 0);
1405		values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1406		values.put(SQLiteAxolotlStore.KEY, base64Serialized);
1407		values.putAll(status.toContentValues());
1408		String where = SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.NAME + "=? AND " + SQLiteAxolotlStore.FINGERPRINT + " =?";
1409		String[] whereArgs = {account.getUuid(), name, fingerprint};
1410		int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, where, whereArgs);
1411		if (rows == 0) {
1412			db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1413		}
1414	}
1415
1416	public void storePreVerification(Account account, String name, String fingerprint, FingerprintStatus status) {
1417		SQLiteDatabase db = this.getWritableDatabase();
1418		ContentValues values = new ContentValues();
1419		values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1420		values.put(SQLiteAxolotlStore.NAME, name);
1421		values.put(SQLiteAxolotlStore.OWN, 0);
1422		values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1423		values.putAll(status.toContentValues());
1424		db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1425	}
1426
1427	public FingerprintStatus getFingerprintStatus(Account account, String fingerprint) {
1428		Cursor cursor = getIdentityKeyCursor(account, fingerprint);
1429		final FingerprintStatus status;
1430		if (cursor.getCount() > 0) {
1431			cursor.moveToFirst();
1432			status = FingerprintStatus.fromCursor(cursor);
1433		} else {
1434			status = null;
1435		}
1436		cursor.close();
1437		return status;
1438	}
1439
1440	public boolean setIdentityKeyTrust(Account account, String fingerprint, FingerprintStatus fingerprintStatus) {
1441		SQLiteDatabase db = this.getWritableDatabase();
1442		return setIdentityKeyTrust(db, account, fingerprint, fingerprintStatus);
1443	}
1444
1445	private boolean setIdentityKeyTrust(SQLiteDatabase db, Account account, String fingerprint, FingerprintStatus status) {
1446		String[] selectionArgs = {
1447				account.getUuid(),
1448				fingerprint
1449		};
1450		int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, status.toContentValues(),
1451				SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1452						+ SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1453				selectionArgs);
1454		return rows == 1;
1455	}
1456
1457	public boolean setIdentityKeyCertificate(Account account, String fingerprint, X509Certificate x509Certificate) {
1458		SQLiteDatabase db = this.getWritableDatabase();
1459		String[] selectionArgs = {
1460				account.getUuid(),
1461				fingerprint
1462		};
1463		try {
1464			ContentValues values = new ContentValues();
1465			values.put(SQLiteAxolotlStore.CERTIFICATE, x509Certificate.getEncoded());
1466			return db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
1467					SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1468							+ SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1469					selectionArgs) == 1;
1470		} catch (CertificateEncodingException e) {
1471			Log.d(Config.LOGTAG, "could not encode certificate");
1472			return false;
1473		}
1474	}
1475
1476	public X509Certificate getIdentityKeyCertifcate(Account account, String fingerprint) {
1477		SQLiteDatabase db = this.getReadableDatabase();
1478		String[] selectionArgs = {
1479				account.getUuid(),
1480				fingerprint
1481		};
1482		String[] colums = {SQLiteAxolotlStore.CERTIFICATE};
1483		String selection = SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ";
1484		Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME, colums, selection, selectionArgs, null, null, null);
1485		if (cursor.getCount() < 1) {
1486			return null;
1487		} else {
1488			cursor.moveToFirst();
1489			byte[] certificate = cursor.getBlob(cursor.getColumnIndex(SQLiteAxolotlStore.CERTIFICATE));
1490			cursor.close();
1491			if (certificate == null || certificate.length == 0) {
1492				return null;
1493			}
1494			try {
1495				CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
1496				return (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(certificate));
1497			} catch (CertificateException e) {
1498				Log.d(Config.LOGTAG, "certificate exception " + e.getMessage());
1499				return null;
1500			}
1501		}
1502	}
1503
1504	public void storeIdentityKey(Account account, String name, IdentityKey identityKey, FingerprintStatus status) {
1505		storeIdentityKey(account, name, false, CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()), Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT), status);
1506	}
1507
1508	public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
1509		storeIdentityKey(account, account.getJid().asBareJid().toString(), true, CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), FingerprintStatus.createActiveVerified(false));
1510	}
1511
1512
1513	private void recreateAxolotlDb(SQLiteDatabase db) {
1514		Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<");
1515		db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME);
1516		db.execSQL(CREATE_SESSIONS_STATEMENT);
1517		db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.PREKEY_TABLENAME);
1518		db.execSQL(CREATE_PREKEYS_STATEMENT);
1519		db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME);
1520		db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
1521		db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.IDENTITIES_TABLENAME);
1522		db.execSQL(CREATE_IDENTITIES_STATEMENT);
1523	}
1524
1525	public void wipeAxolotlDb(Account account) {
1526		String accountName = account.getUuid();
1527		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ">>> WIPING AXOLOTL DATABASE FOR ACCOUNT " + accountName + " <<<");
1528		SQLiteDatabase db = this.getWritableDatabase();
1529		String[] deleteArgs = {
1530				accountName
1531		};
1532		db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1533				SQLiteAxolotlStore.ACCOUNT + " = ?",
1534				deleteArgs);
1535		db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1536				SQLiteAxolotlStore.ACCOUNT + " = ?",
1537				deleteArgs);
1538		db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1539				SQLiteAxolotlStore.ACCOUNT + " = ?",
1540				deleteArgs);
1541		db.delete(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1542				SQLiteAxolotlStore.ACCOUNT + " = ?",
1543				deleteArgs);
1544	}
1545
1546	public List<ShortcutService.FrequentContact> getFrequentContacts(int days) {
1547		SQLiteDatabase db = this.getReadableDatabase();
1548		final String SQL = "select " + Conversation.TABLENAME + "." + Conversation.ACCOUNT + "," + Conversation.TABLENAME + "." + Conversation.CONTACTJID + " from " + Conversation.TABLENAME + " join " + Message.TABLENAME + " on conversations.uuid=messages.conversationUuid where messages.status!=0 and carbon==0  and conversations.mode=0 and messages.timeSent>=? group by conversations.uuid order by count(body) desc limit 4;";
1549		String[] whereArgs = new String[]{String.valueOf(System.currentTimeMillis() - (Config.MILLISECONDS_IN_DAY * days))};
1550		Cursor cursor = db.rawQuery(SQL, whereArgs);
1551		ArrayList<ShortcutService.FrequentContact> contacts = new ArrayList<>();
1552		while (cursor.moveToNext()) {
1553			try {
1554				contacts.add(new ShortcutService.FrequentContact(cursor.getString(0), Jid.of(cursor.getString(1))));
1555			} catch (Exception e) {
1556				Log.d(Config.LOGTAG, e.getMessage());
1557			}
1558		}
1559		cursor.close();
1560		return contacts;
1561	}
1562}