DatabaseBackend.java

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