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