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