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