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