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        SQLiteDatabase db = this.getWritableDatabase();
1008        String[] args = {account.getUuid()};
1009        final int rows = db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
1010        return rows == 1;
1011    }
1012
1013    public boolean deleteAccount(Account account) {
1014        SQLiteDatabase db = this.getWritableDatabase();
1015        String[] args = {account.getUuid()};
1016        final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
1017        return rows == 1;
1018    }
1019
1020    public boolean updateMessage(Message message, boolean includeBody) {
1021        SQLiteDatabase db = this.getWritableDatabase();
1022        String[] args = {message.getUuid()};
1023        ContentValues contentValues = message.getContentValues();
1024        contentValues.remove(Message.UUID);
1025        if (!includeBody) {
1026            contentValues.remove(Message.BODY);
1027        }
1028        return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1;
1029    }
1030
1031    public boolean updateMessage(Message message, String uuid) {
1032        SQLiteDatabase db = this.getWritableDatabase();
1033        String[] args = {uuid};
1034        return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1;
1035    }
1036
1037    public void readRoster(Roster roster) {
1038        final SQLiteDatabase db = this.getReadableDatabase();
1039        final String[] args = {roster.getAccount().getUuid()};
1040        try (final Cursor cursor =
1041                db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?", args, null, null, null)) {
1042            while (cursor.moveToNext()) {
1043                roster.initContact(Contact.fromCursor(cursor));
1044            }
1045        }
1046    }
1047
1048    public void writeRoster(final Roster roster) {
1049        long start = SystemClock.elapsedRealtime();
1050        final Account account = roster.getAccount();
1051        final SQLiteDatabase db = this.getWritableDatabase();
1052        db.beginTransaction();
1053        for (Contact contact : roster.getContacts()) {
1054            if (contact.getOption(Contact.Options.IN_ROSTER) || contact.hasAvatarOrPresenceName() || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
1055                db.insert(Contact.TABLENAME, null, contact.getContentValues());
1056            } else {
1057                String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
1058                String[] whereArgs = {account.getUuid(), contact.getJid().toString()};
1059                db.delete(Contact.TABLENAME, where, whereArgs);
1060            }
1061        }
1062        db.setTransactionSuccessful();
1063        db.endTransaction();
1064        account.setRosterVersion(roster.getVersion());
1065        updateAccount(account);
1066        long duration = SystemClock.elapsedRealtime() - start;
1067        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": persisted roster in " + duration + "ms");
1068    }
1069
1070    public void deleteMessagesInConversation(Conversation conversation) {
1071        long start = SystemClock.elapsedRealtime();
1072        final SQLiteDatabase db = this.getWritableDatabase();
1073        db.beginTransaction();
1074        final String[] args = {conversation.getUuid()};
1075        int num = db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
1076        db.setTransactionSuccessful();
1077        db.endTransaction();
1078        Log.d(Config.LOGTAG, "deleted " + num + " messages for " + conversation.getJid().asBareJid() + " in " + (SystemClock.elapsedRealtime() - start) + "ms");
1079    }
1080
1081    public void expireOldMessages(long timestamp) {
1082        final String[] args = {String.valueOf(timestamp)};
1083        SQLiteDatabase db = this.getReadableDatabase();
1084        db.beginTransaction();
1085        db.delete(Message.TABLENAME, "timeSent<?", args);
1086        db.setTransactionSuccessful();
1087        db.endTransaction();
1088    }
1089
1090    public MamReference getLastMessageReceived(Account account) {
1091        Cursor cursor = null;
1092        try {
1093            SQLiteDatabase db = this.getReadableDatabase();
1094            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";
1095            String[] args = {account.getUuid()};
1096            cursor = db.rawQuery(sql, args);
1097            if (cursor.getCount() == 0) {
1098                return null;
1099            } else {
1100                cursor.moveToFirst();
1101                return new MamReference(cursor.getLong(0), cursor.getString(1));
1102            }
1103        } catch (Exception e) {
1104            return null;
1105        } finally {
1106            if (cursor != null) {
1107                cursor.close();
1108            }
1109        }
1110    }
1111
1112    public long getLastTimeFingerprintUsed(Account account, String fingerprint) {
1113        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";
1114        String[] args = {account.getUuid(), fingerprint};
1115        Cursor cursor = getReadableDatabase().rawQuery(SQL, args);
1116        long time;
1117        if (cursor.moveToFirst()) {
1118            time = cursor.getLong(0);
1119        } else {
1120            time = 0;
1121        }
1122        cursor.close();
1123        return time;
1124    }
1125
1126    public MamReference getLastClearDate(Account account) {
1127        SQLiteDatabase db = this.getReadableDatabase();
1128        String[] columns = {Conversation.ATTRIBUTES};
1129        String selection = Conversation.ACCOUNT + "=?";
1130        String[] args = {account.getUuid()};
1131        Cursor cursor = db.query(Conversation.TABLENAME, columns, selection, args, null, null, null);
1132        MamReference maxClearDate = new MamReference(0);
1133        while (cursor.moveToNext()) {
1134            try {
1135                final JSONObject o = new JSONObject(cursor.getString(0));
1136                maxClearDate = MamReference.max(maxClearDate, MamReference.fromAttribute(o.getString(Conversation.ATTRIBUTE_LAST_CLEAR_HISTORY)));
1137            } catch (Exception e) {
1138                //ignored
1139            }
1140        }
1141        cursor.close();
1142        return maxClearDate;
1143    }
1144
1145    private Cursor getCursorForSession(Account account, SignalProtocolAddress contact) {
1146        final SQLiteDatabase db = this.getReadableDatabase();
1147        String[] selectionArgs = {account.getUuid(),
1148                contact.getName(),
1149                Integer.toString(contact.getDeviceId())};
1150        return db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1151                null,
1152                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1153                        + SQLiteAxolotlStore.NAME + " = ? AND "
1154                        + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1155                selectionArgs,
1156                null, null, null);
1157    }
1158
1159    public SessionRecord loadSession(Account account, SignalProtocolAddress contact) {
1160        SessionRecord session = null;
1161        Cursor cursor = getCursorForSession(account, contact);
1162        if (cursor.getCount() != 0) {
1163            cursor.moveToFirst();
1164            try {
1165                session = new SessionRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1166            } catch (IOException e) {
1167                cursor.close();
1168                throw new AssertionError(e);
1169            }
1170        }
1171        cursor.close();
1172        return session;
1173    }
1174
1175    public List<Integer> getSubDeviceSessions(Account account, SignalProtocolAddress contact) {
1176        final SQLiteDatabase db = this.getReadableDatabase();
1177        return getSubDeviceSessions(db, account, contact);
1178    }
1179
1180    private List<Integer> getSubDeviceSessions(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1181        List<Integer> devices = new ArrayList<>();
1182        String[] columns = {SQLiteAxolotlStore.DEVICE_ID};
1183        String[] selectionArgs = {account.getUuid(),
1184                contact.getName()};
1185        Cursor cursor = db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1186                columns,
1187                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1188                        + SQLiteAxolotlStore.NAME + " = ?",
1189                selectionArgs,
1190                null, null, null);
1191
1192        while (cursor.moveToNext()) {
1193            devices.add(cursor.getInt(
1194                    cursor.getColumnIndex(SQLiteAxolotlStore.DEVICE_ID)));
1195        }
1196
1197        cursor.close();
1198        return devices;
1199    }
1200
1201    public List<String> getKnownSignalAddresses(Account account) {
1202        List<String> addresses = new ArrayList<>();
1203        String[] colums = {"DISTINCT " + SQLiteAxolotlStore.NAME};
1204        String[] selectionArgs = {account.getUuid()};
1205        Cursor cursor = getReadableDatabase().query(SQLiteAxolotlStore.SESSION_TABLENAME,
1206                colums,
1207                SQLiteAxolotlStore.ACCOUNT + " = ?",
1208                selectionArgs,
1209                null, null, null
1210        );
1211        while (cursor.moveToNext()) {
1212            addresses.add(cursor.getString(0));
1213        }
1214        cursor.close();
1215        return addresses;
1216    }
1217
1218    public boolean containsSession(Account account, SignalProtocolAddress contact) {
1219        Cursor cursor = getCursorForSession(account, contact);
1220        int count = cursor.getCount();
1221        cursor.close();
1222        return count != 0;
1223    }
1224
1225    public void storeSession(Account account, SignalProtocolAddress contact, SessionRecord session) {
1226        SQLiteDatabase db = this.getWritableDatabase();
1227        ContentValues values = new ContentValues();
1228        values.put(SQLiteAxolotlStore.NAME, contact.getName());
1229        values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
1230        values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
1231        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1232        db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
1233    }
1234
1235    public void deleteSession(Account account, SignalProtocolAddress contact) {
1236        SQLiteDatabase db = this.getWritableDatabase();
1237        deleteSession(db, account, contact);
1238    }
1239
1240    private void deleteSession(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1241        String[] args = {account.getUuid(),
1242                contact.getName(),
1243                Integer.toString(contact.getDeviceId())};
1244        db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1245                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1246                        + SQLiteAxolotlStore.NAME + " = ? AND "
1247                        + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1248                args);
1249    }
1250
1251    public void deleteAllSessions(Account account, SignalProtocolAddress contact) {
1252        SQLiteDatabase db = this.getWritableDatabase();
1253        String[] args = {account.getUuid(), contact.getName()};
1254        db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1255                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1256                        + SQLiteAxolotlStore.NAME + " = ?",
1257                args);
1258    }
1259
1260    private Cursor getCursorForPreKey(Account account, int preKeyId) {
1261        SQLiteDatabase db = this.getReadableDatabase();
1262        String[] columns = {SQLiteAxolotlStore.KEY};
1263        String[] selectionArgs = {account.getUuid(), Integer.toString(preKeyId)};
1264        Cursor cursor = db.query(SQLiteAxolotlStore.PREKEY_TABLENAME,
1265                columns,
1266                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1267                        + SQLiteAxolotlStore.ID + "=?",
1268                selectionArgs,
1269                null, null, null);
1270
1271        return cursor;
1272    }
1273
1274    public PreKeyRecord loadPreKey(Account account, int preKeyId) {
1275        PreKeyRecord record = null;
1276        Cursor cursor = getCursorForPreKey(account, preKeyId);
1277        if (cursor.getCount() != 0) {
1278            cursor.moveToFirst();
1279            try {
1280                record = new PreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1281            } catch (IOException e) {
1282                throw new AssertionError(e);
1283            }
1284        }
1285        cursor.close();
1286        return record;
1287    }
1288
1289    public boolean containsPreKey(Account account, int preKeyId) {
1290        Cursor cursor = getCursorForPreKey(account, preKeyId);
1291        int count = cursor.getCount();
1292        cursor.close();
1293        return count != 0;
1294    }
1295
1296    public void storePreKey(Account account, PreKeyRecord record) {
1297        SQLiteDatabase db = this.getWritableDatabase();
1298        ContentValues values = new ContentValues();
1299        values.put(SQLiteAxolotlStore.ID, record.getId());
1300        values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1301        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1302        db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
1303    }
1304
1305    public int deletePreKey(Account account, int preKeyId) {
1306        SQLiteDatabase db = this.getWritableDatabase();
1307        String[] args = {account.getUuid(), Integer.toString(preKeyId)};
1308        return db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1309                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1310                        + SQLiteAxolotlStore.ID + "=?",
1311                args);
1312    }
1313
1314    private Cursor getCursorForSignedPreKey(Account account, int signedPreKeyId) {
1315        SQLiteDatabase db = this.getReadableDatabase();
1316        String[] columns = {SQLiteAxolotlStore.KEY};
1317        String[] selectionArgs = {account.getUuid(), Integer.toString(signedPreKeyId)};
1318        Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1319                columns,
1320                SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
1321                selectionArgs,
1322                null, null, null);
1323
1324        return cursor;
1325    }
1326
1327    public SignedPreKeyRecord loadSignedPreKey(Account account, int signedPreKeyId) {
1328        SignedPreKeyRecord record = null;
1329        Cursor cursor = getCursorForSignedPreKey(account, signedPreKeyId);
1330        if (cursor.getCount() != 0) {
1331            cursor.moveToFirst();
1332            try {
1333                record = new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1334            } catch (IOException e) {
1335                throw new AssertionError(e);
1336            }
1337        }
1338        cursor.close();
1339        return record;
1340    }
1341
1342    public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
1343        List<SignedPreKeyRecord> prekeys = new ArrayList<>();
1344        SQLiteDatabase db = this.getReadableDatabase();
1345        String[] columns = {SQLiteAxolotlStore.KEY};
1346        String[] selectionArgs = {account.getUuid()};
1347        Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1348                columns,
1349                SQLiteAxolotlStore.ACCOUNT + "=?",
1350                selectionArgs,
1351                null, null, null);
1352
1353        while (cursor.moveToNext()) {
1354            try {
1355                prekeys.add(new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT)));
1356            } catch (IOException ignored) {
1357            }
1358        }
1359        cursor.close();
1360        return prekeys;
1361    }
1362
1363    public int getSignedPreKeysCount(Account account) {
1364        String[] columns = {"count(" + SQLiteAxolotlStore.KEY + ")"};
1365        String[] selectionArgs = {account.getUuid()};
1366        SQLiteDatabase db = this.getReadableDatabase();
1367        Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1368                columns,
1369                SQLiteAxolotlStore.ACCOUNT + "=?",
1370                selectionArgs,
1371                null, null, null);
1372        final int count;
1373        if (cursor.moveToFirst()) {
1374            count = cursor.getInt(0);
1375        } else {
1376            count = 0;
1377        }
1378        cursor.close();
1379        return count;
1380    }
1381
1382    public boolean containsSignedPreKey(Account account, int signedPreKeyId) {
1383        Cursor cursor = getCursorForPreKey(account, signedPreKeyId);
1384        int count = cursor.getCount();
1385        cursor.close();
1386        return count != 0;
1387    }
1388
1389    public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
1390        SQLiteDatabase db = this.getWritableDatabase();
1391        ContentValues values = new ContentValues();
1392        values.put(SQLiteAxolotlStore.ID, record.getId());
1393        values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1394        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1395        db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
1396    }
1397
1398    public void deleteSignedPreKey(Account account, int signedPreKeyId) {
1399        SQLiteDatabase db = this.getWritableDatabase();
1400        String[] args = {account.getUuid(), Integer.toString(signedPreKeyId)};
1401        db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1402                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1403                        + SQLiteAxolotlStore.ID + "=?",
1404                args);
1405    }
1406
1407    private Cursor getIdentityKeyCursor(Account account, String name, boolean own) {
1408        final SQLiteDatabase db = this.getReadableDatabase();
1409        return getIdentityKeyCursor(db, account, name, own);
1410    }
1411
1412    private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, boolean own) {
1413        return getIdentityKeyCursor(db, account, name, own, null);
1414    }
1415
1416    private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
1417        final SQLiteDatabase db = this.getReadableDatabase();
1418        return getIdentityKeyCursor(db, account, fingerprint);
1419    }
1420
1421    private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String fingerprint) {
1422        return getIdentityKeyCursor(db, account, null, null, fingerprint);
1423    }
1424
1425    private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, Boolean own, String fingerprint) {
1426        String[] columns = {SQLiteAxolotlStore.TRUST,
1427                SQLiteAxolotlStore.ACTIVE,
1428                SQLiteAxolotlStore.LAST_ACTIVATION,
1429                SQLiteAxolotlStore.KEY};
1430        ArrayList<String> selectionArgs = new ArrayList<>(4);
1431        selectionArgs.add(account.getUuid());
1432        String selectionString = SQLiteAxolotlStore.ACCOUNT + " = ?";
1433        if (name != null) {
1434            selectionArgs.add(name);
1435            selectionString += " AND " + SQLiteAxolotlStore.NAME + " = ?";
1436        }
1437        if (fingerprint != null) {
1438            selectionArgs.add(fingerprint);
1439            selectionString += " AND " + SQLiteAxolotlStore.FINGERPRINT + " = ?";
1440        }
1441        if (own != null) {
1442            selectionArgs.add(own ? "1" : "0");
1443            selectionString += " AND " + SQLiteAxolotlStore.OWN + " = ?";
1444        }
1445        Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1446                columns,
1447                selectionString,
1448                selectionArgs.toArray(new String[selectionArgs.size()]),
1449                null, null, null);
1450
1451        return cursor;
1452    }
1453
1454    public IdentityKeyPair loadOwnIdentityKeyPair(Account account) {
1455        SQLiteDatabase db = getReadableDatabase();
1456        return loadOwnIdentityKeyPair(db, account);
1457    }
1458
1459    private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
1460        String name = account.getJid().asBareJid().toString();
1461        IdentityKeyPair identityKeyPair = null;
1462        Cursor cursor = getIdentityKeyCursor(db, account, name, true);
1463        if (cursor.getCount() != 0) {
1464            cursor.moveToFirst();
1465            try {
1466                identityKeyPair = new IdentityKeyPair(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1467            } catch (InvalidKeyException e) {
1468                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1469            }
1470        }
1471        cursor.close();
1472
1473        return identityKeyPair;
1474    }
1475
1476    public Set<IdentityKey> loadIdentityKeys(Account account, String name) {
1477        return loadIdentityKeys(account, name, null);
1478    }
1479
1480    public Set<IdentityKey> loadIdentityKeys(Account account, String name, FingerprintStatus status) {
1481        Set<IdentityKey> identityKeys = new HashSet<>();
1482        Cursor cursor = getIdentityKeyCursor(account, name, false);
1483
1484        while (cursor.moveToNext()) {
1485            if (status != null && !FingerprintStatus.fromCursor(cursor).equals(status)) {
1486                continue;
1487            }
1488            try {
1489                String key = cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY));
1490                if (key != null) {
1491                    identityKeys.add(new IdentityKey(Base64.decode(key, Base64.DEFAULT), 0));
1492                } else {
1493                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Missing key (possibly preverified) in database for account" + account.getJid().asBareJid() + ", address: " + name);
1494                }
1495            } catch (InvalidKeyException e) {
1496                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1497            }
1498        }
1499        cursor.close();
1500
1501        return identityKeys;
1502    }
1503
1504    public long numTrustedKeys(Account account, String name) {
1505        SQLiteDatabase db = getReadableDatabase();
1506        String[] args = {
1507                account.getUuid(),
1508                name,
1509                FingerprintStatus.Trust.TRUSTED.toString(),
1510                FingerprintStatus.Trust.VERIFIED.toString(),
1511                FingerprintStatus.Trust.VERIFIED_X509.toString()
1512        };
1513        return DatabaseUtils.queryNumEntries(db, SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1514                SQLiteAxolotlStore.ACCOUNT + " = ?"
1515                        + " AND " + SQLiteAxolotlStore.NAME + " = ?"
1516                        + " AND (" + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ?)"
1517                        + " AND " + SQLiteAxolotlStore.ACTIVE + " > 0",
1518                args
1519        );
1520    }
1521
1522    private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized, FingerprintStatus status) {
1523        SQLiteDatabase db = this.getWritableDatabase();
1524        ContentValues values = new ContentValues();
1525        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1526        values.put(SQLiteAxolotlStore.NAME, name);
1527        values.put(SQLiteAxolotlStore.OWN, own ? 1 : 0);
1528        values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1529        values.put(SQLiteAxolotlStore.KEY, base64Serialized);
1530        values.putAll(status.toContentValues());
1531        String where = SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.NAME + "=? AND " + SQLiteAxolotlStore.FINGERPRINT + " =?";
1532        String[] whereArgs = {account.getUuid(), name, fingerprint};
1533        int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, where, whereArgs);
1534        if (rows == 0) {
1535            db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1536        }
1537    }
1538
1539    public void storePreVerification(Account account, String name, String fingerprint, FingerprintStatus status) {
1540        SQLiteDatabase db = this.getWritableDatabase();
1541        ContentValues values = new ContentValues();
1542        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1543        values.put(SQLiteAxolotlStore.NAME, name);
1544        values.put(SQLiteAxolotlStore.OWN, 0);
1545        values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1546        values.putAll(status.toContentValues());
1547        db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1548    }
1549
1550    public FingerprintStatus getFingerprintStatus(Account account, String fingerprint) {
1551        Cursor cursor = getIdentityKeyCursor(account, fingerprint);
1552        final FingerprintStatus status;
1553        if (cursor.getCount() > 0) {
1554            cursor.moveToFirst();
1555            status = FingerprintStatus.fromCursor(cursor);
1556        } else {
1557            status = null;
1558        }
1559        cursor.close();
1560        return status;
1561    }
1562
1563    public boolean setIdentityKeyTrust(Account account, String fingerprint, FingerprintStatus fingerprintStatus) {
1564        SQLiteDatabase db = this.getWritableDatabase();
1565        return setIdentityKeyTrust(db, account, fingerprint, fingerprintStatus);
1566    }
1567
1568    private boolean setIdentityKeyTrust(SQLiteDatabase db, Account account, String fingerprint, FingerprintStatus status) {
1569        String[] selectionArgs = {
1570                account.getUuid(),
1571                fingerprint
1572        };
1573        int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, status.toContentValues(),
1574                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1575                        + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1576                selectionArgs);
1577        return rows == 1;
1578    }
1579
1580    public boolean setIdentityKeyCertificate(Account account, String fingerprint, X509Certificate x509Certificate) {
1581        SQLiteDatabase db = this.getWritableDatabase();
1582        String[] selectionArgs = {
1583                account.getUuid(),
1584                fingerprint
1585        };
1586        try {
1587            ContentValues values = new ContentValues();
1588            values.put(SQLiteAxolotlStore.CERTIFICATE, x509Certificate.getEncoded());
1589            return db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
1590                    SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1591                            + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1592                    selectionArgs) == 1;
1593        } catch (CertificateEncodingException e) {
1594            Log.d(Config.LOGTAG, "could not encode certificate");
1595            return false;
1596        }
1597    }
1598
1599    public X509Certificate getIdentityKeyCertifcate(Account account, String fingerprint) {
1600        SQLiteDatabase db = this.getReadableDatabase();
1601        String[] selectionArgs = {
1602                account.getUuid(),
1603                fingerprint
1604        };
1605        String[] colums = {SQLiteAxolotlStore.CERTIFICATE};
1606        String selection = SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ";
1607        Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME, colums, selection, selectionArgs, null, null, null);
1608        if (cursor.getCount() < 1) {
1609            return null;
1610        } else {
1611            cursor.moveToFirst();
1612            byte[] certificate = cursor.getBlob(cursor.getColumnIndex(SQLiteAxolotlStore.CERTIFICATE));
1613            cursor.close();
1614            if (certificate == null || certificate.length == 0) {
1615                return null;
1616            }
1617            try {
1618                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
1619                return (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(certificate));
1620            } catch (CertificateException e) {
1621                Log.d(Config.LOGTAG, "certificate exception " + e.getMessage());
1622                return null;
1623            }
1624        }
1625    }
1626
1627    public void storeIdentityKey(Account account, String name, IdentityKey identityKey, FingerprintStatus status) {
1628        storeIdentityKey(account, name, false, CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()), Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT), status);
1629    }
1630
1631    public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
1632        storeIdentityKey(account, account.getJid().asBareJid().toString(), true, CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), FingerprintStatus.createActiveVerified(false));
1633    }
1634
1635
1636    private void recreateAxolotlDb(SQLiteDatabase db) {
1637        Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<");
1638        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME);
1639        db.execSQL(CREATE_SESSIONS_STATEMENT);
1640        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.PREKEY_TABLENAME);
1641        db.execSQL(CREATE_PREKEYS_STATEMENT);
1642        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME);
1643        db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
1644        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.IDENTITIES_TABLENAME);
1645        db.execSQL(CREATE_IDENTITIES_STATEMENT);
1646    }
1647
1648    public void wipeAxolotlDb(Account account) {
1649        String accountName = account.getUuid();
1650        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ">>> WIPING AXOLOTL DATABASE FOR ACCOUNT " + accountName + " <<<");
1651        SQLiteDatabase db = this.getWritableDatabase();
1652        String[] deleteArgs = {
1653                accountName
1654        };
1655        db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1656                SQLiteAxolotlStore.ACCOUNT + " = ?",
1657                deleteArgs);
1658        db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1659                SQLiteAxolotlStore.ACCOUNT + " = ?",
1660                deleteArgs);
1661        db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1662                SQLiteAxolotlStore.ACCOUNT + " = ?",
1663                deleteArgs);
1664        db.delete(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1665                SQLiteAxolotlStore.ACCOUNT + " = ?",
1666                deleteArgs);
1667    }
1668
1669    public List<ShortcutService.FrequentContact> getFrequentContacts(int days) {
1670        SQLiteDatabase db = this.getReadableDatabase();
1671        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;";
1672        String[] whereArgs = new String[]{String.valueOf(System.currentTimeMillis() - (Config.MILLISECONDS_IN_DAY * days))};
1673        Cursor cursor = db.rawQuery(SQL, whereArgs);
1674        ArrayList<ShortcutService.FrequentContact> contacts = new ArrayList<>();
1675        while (cursor.moveToNext()) {
1676            try {
1677                contacts.add(new ShortcutService.FrequentContact(cursor.getString(0), Jid.of(cursor.getString(1))));
1678            } catch (Exception e) {
1679                Log.d(Config.LOGTAG, e.getMessage());
1680            }
1681        }
1682        cursor.close();
1683        return contacts;
1684    }
1685}