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