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