DatabaseBackend.java

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