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