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