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