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