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