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