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