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 createConversation(Conversation conversation) {
 805        SQLiteDatabase db = this.getWritableDatabase();
 806        db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
 807    }
 808
 809    public void createMessage(Message message) {
 810        SQLiteDatabase db = this.getWritableDatabase();
 811        db.insert(Message.TABLENAME, null, message.getContentValues());
 812        db.insert("cheogram." + Message.TABLENAME, null, message.getCheogramContentValues());
 813    }
 814
 815    public void createAccount(Account account) {
 816        SQLiteDatabase db = this.getWritableDatabase();
 817        db.insert(Account.TABLENAME, null, account.getContentValues());
 818    }
 819
 820    public void insertDiscoveryResult(ServiceDiscoveryResult result) {
 821        SQLiteDatabase db = this.getWritableDatabase();
 822        db.insert(ServiceDiscoveryResult.TABLENAME, null, result.getContentValues());
 823    }
 824
 825    public ServiceDiscoveryResult findDiscoveryResult(final String hash, final String ver) {
 826        SQLiteDatabase db = this.getReadableDatabase();
 827        String[] selectionArgs = {hash, ver};
 828        Cursor cursor = db.query(ServiceDiscoveryResult.TABLENAME, null,
 829                ServiceDiscoveryResult.HASH + "=? AND " + ServiceDiscoveryResult.VER + "=?",
 830                selectionArgs, null, null, null);
 831        if (cursor.getCount() == 0) {
 832            cursor.close();
 833            return null;
 834        }
 835        cursor.moveToFirst();
 836
 837        ServiceDiscoveryResult result = null;
 838        try {
 839            result = new ServiceDiscoveryResult(cursor);
 840        } catch (JSONException e) { /* result is still null */ }
 841
 842        cursor.close();
 843        return result;
 844    }
 845
 846    public void saveResolverResult(String domain, Resolver.Result result) {
 847        SQLiteDatabase db = this.getWritableDatabase();
 848        ContentValues contentValues = result.toContentValues();
 849        contentValues.put(Resolver.Result.DOMAIN, domain);
 850        db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
 851    }
 852
 853    public synchronized Resolver.Result findResolverResult(String domain) {
 854        SQLiteDatabase db = this.getReadableDatabase();
 855        String where = Resolver.Result.DOMAIN + "=?";
 856        String[] whereArgs = {domain};
 857        final Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
 858        Resolver.Result result = null;
 859        if (cursor != null) {
 860            try {
 861                if (cursor.moveToFirst()) {
 862                    result = Resolver.Result.fromCursor(cursor);
 863                }
 864            } catch (Exception e) {
 865                Log.d(Config.LOGTAG, "unable to find cached resolver result in database " + e.getMessage());
 866                return null;
 867            } finally {
 868                cursor.close();
 869            }
 870        }
 871        return result;
 872    }
 873
 874    public void insertPresenceTemplate(PresenceTemplate template) {
 875        SQLiteDatabase db = this.getWritableDatabase();
 876        String whereToDelete = PresenceTemplate.MESSAGE + "=?";
 877        String[] whereToDeleteArgs = {template.getStatusMessage()};
 878        db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs);
 879        db.delete(PresenceTemplate.TABELNAME, PresenceTemplate.UUID + " not in (select " + PresenceTemplate.UUID + " from " + PresenceTemplate.TABELNAME + " order by " + PresenceTemplate.LAST_USED + " desc limit 9)", null);
 880        db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
 881    }
 882
 883    public List<PresenceTemplate> getPresenceTemplates() {
 884        ArrayList<PresenceTemplate> templates = new ArrayList<>();
 885        SQLiteDatabase db = this.getReadableDatabase();
 886        Cursor cursor = db.query(PresenceTemplate.TABELNAME, null, null, null, null, null, PresenceTemplate.LAST_USED + " desc");
 887        while (cursor.moveToNext()) {
 888            templates.add(PresenceTemplate.fromCursor(cursor));
 889        }
 890        cursor.close();
 891        return templates;
 892    }
 893
 894    public CopyOnWriteArrayList<Conversation> getConversations(int status) {
 895        CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>();
 896        SQLiteDatabase db = this.getReadableDatabase();
 897        String[] selectionArgs = {Integer.toString(status)};
 898        Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME
 899                + " where " + Conversation.STATUS + " = ? and " + Conversation.CONTACTJID + " is not null order by "
 900                + Conversation.CREATED + " desc", selectionArgs);
 901        while (cursor.moveToNext()) {
 902            final Conversation conversation = Conversation.fromCursor(cursor);
 903            if (conversation.getJid() instanceof InvalidJid) {
 904                continue;
 905            }
 906            list.add(conversation);
 907        }
 908        cursor.close();
 909        return list;
 910    }
 911
 912    public ArrayList<Message> getMessages(Conversation conversations, int limit) {
 913        return getMessages(conversations, limit, -1);
 914    }
 915
 916    public ArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
 917        ArrayList<Message> list = new ArrayList<>();
 918        SQLiteDatabase db = this.getReadableDatabase();
 919        Cursor cursor;
 920        if (timestamp == -1) {
 921            String[] selectionArgs = {conversation.getUuid()};
 922            cursor = db.rawQuery(
 923                "SELECT * FROM " + Message.TABLENAME + " " +
 924                "LEFT JOIN cheogram." + Message.TABLENAME +
 925                "  USING (" + Message.UUID + ")" +
 926                "WHERE " + Message.CONVERSATION + "=? " +
 927                "ORDER BY " + Message.TIME_SENT + " DESC " +
 928                "LIMIT " + String.valueOf(limit),
 929                selectionArgs
 930            );
 931        } else {
 932            String[] selectionArgs = {conversation.getUuid(),
 933                    Long.toString(timestamp)};
 934            cursor = db.rawQuery(
 935                "SELECT * FROM " + Message.TABLENAME + " " +
 936                "LEFT JOIN cheogram." + Message.TABLENAME +
 937                "  USING (" + Message.UUID + ")" +
 938                "WHERE " + Message.CONVERSATION + "=? AND " +
 939                Message.TIME_SENT + "<? " +
 940                "ORDER BY " + Message.TIME_SENT + " DESC " +
 941                "LIMIT " + String.valueOf(limit),
 942                selectionArgs
 943            );
 944        }
 945        CursorUtils.upgradeCursorWindowSize(cursor);
 946        while (cursor.moveToNext()) {
 947            try {
 948                list.add(0, Message.fromCursor(cursor, conversation));
 949            } catch (Exception e) {
 950                Log.e(Config.LOGTAG, "unable to restore message");
 951            }
 952        }
 953        cursor.close();
 954        return list;
 955    }
 956
 957    public Cursor getMessageSearchCursor(final List<String> term, final String uuid) {
 958        final SQLiteDatabase db = this.getReadableDatabase();
 959        final StringBuilder SQL = new StringBuilder();
 960        final String[] selectionArgs;
 961        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 ?");
 962        if (uuid == null) {
 963            selectionArgs = new String[]{FtsUtils.toMatchString(term)};
 964        } else {
 965            selectionArgs = new String[]{FtsUtils.toMatchString(term), uuid};
 966            SQL.append(" AND " + Conversation.TABLENAME + '.' + Conversation.UUID + "=?");
 967        }
 968        SQL.append(" ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS);
 969        Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term));
 970        return db.rawQuery(SQL.toString(), selectionArgs);
 971    }
 972
 973    public List<String> markFileAsDeleted(final File file, final boolean internal) {
 974        SQLiteDatabase db = this.getReadableDatabase();
 975        String selection;
 976        String[] selectionArgs;
 977        if (internal) {
 978            final String name = file.getName();
 979            if (name.endsWith(".pgp")) {
 980                selection = "(" + Message.RELATIVE_FILE_PATH + " IN(?,?) OR (" + Message.RELATIVE_FILE_PATH + "=? and encryption in(1,4))) and type in (1,2,5)";
 981                selectionArgs = new String[]{file.getAbsolutePath(), name, name.substring(0, name.length() - 4)};
 982            } else {
 983                selection = Message.RELATIVE_FILE_PATH + " IN(?,?) and type in (1,2,5)";
 984                selectionArgs = new String[]{file.getAbsolutePath(), name};
 985            }
 986        } else {
 987            selection = Message.RELATIVE_FILE_PATH + "=? and type in (1,2,5)";
 988            selectionArgs = new String[]{file.getAbsolutePath()};
 989        }
 990        final List<String> uuids = new ArrayList<>();
 991        Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID}, selection, selectionArgs, null, null, null);
 992        while (cursor != null && cursor.moveToNext()) {
 993            uuids.add(cursor.getString(0));
 994        }
 995        if (cursor != null) {
 996            cursor.close();
 997        }
 998        markFileAsDeleted(uuids);
 999        return uuids;
1000    }
1001
1002    public void markFileAsDeleted(List<String> uuids) {
1003        SQLiteDatabase db = this.getReadableDatabase();
1004        final ContentValues contentValues = new ContentValues();
1005        final String where = Message.UUID + "=?";
1006        contentValues.put(Message.DELETED, 1);
1007        db.beginTransaction();
1008        for (String uuid : uuids) {
1009            db.update(Message.TABLENAME, contentValues, where, new String[]{uuid});
1010        }
1011        db.setTransactionSuccessful();
1012        db.endTransaction();
1013    }
1014
1015    public void markFilesAsChanged(List<FilePathInfo> files) {
1016        SQLiteDatabase db = this.getReadableDatabase();
1017        final String where = Message.UUID + "=?";
1018        db.beginTransaction();
1019        for (FilePathInfo info : files) {
1020            final ContentValues contentValues = new ContentValues();
1021            contentValues.put(Message.DELETED, info.deleted ? 1 : 0);
1022            db.update(Message.TABLENAME, contentValues, where, new String[]{info.uuid.toString()});
1023        }
1024        db.setTransactionSuccessful();
1025        db.endTransaction();
1026    }
1027
1028    public List<FilePathInfo> getFilePathInfo() {
1029        final SQLiteDatabase db = this.getReadableDatabase();
1030        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);
1031        final List<FilePathInfo> list = new ArrayList<>();
1032        while (cursor != null && cursor.moveToNext()) {
1033            list.add(new FilePathInfo(cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0));
1034        }
1035        if (cursor != null) {
1036            cursor.close();
1037        }
1038        return list;
1039    }
1040
1041    public List<FilePath> getRelativeFilePaths(String account, Jid jid, int limit) {
1042        SQLiteDatabase db = this.getReadableDatabase();
1043        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";
1044        final String[] args = {account, jid.toString(), jid.toString() + "/%"};
1045        Cursor cursor = db.rawQuery(SQL + (limit > 0 ? " limit " + limit : ""), args);
1046        List<FilePath> filesPaths = new ArrayList<>();
1047        while (cursor.moveToNext()) {
1048            filesPaths.add(new FilePath(cursor.getString(0), cursor.getString(1)));
1049        }
1050        cursor.close();
1051        return filesPaths;
1052    }
1053
1054    public static class FilePath {
1055        public final UUID uuid;
1056        public final String path;
1057
1058        private FilePath(String uuid, String path) {
1059            this.uuid = UUID.fromString(uuid);
1060            this.path = path;
1061        }
1062    }
1063
1064    public static class FilePathInfo extends FilePath {
1065        public boolean deleted;
1066
1067        private FilePathInfo(String uuid, String path, boolean deleted) {
1068            super(uuid, path);
1069            this.deleted = deleted;
1070        }
1071
1072        public boolean setDeleted(boolean deleted) {
1073            final boolean changed = deleted != this.deleted;
1074            this.deleted = deleted;
1075            return changed;
1076        }
1077    }
1078
1079    public Conversation findConversation(final Account account, final Jid contactJid) {
1080        SQLiteDatabase db = this.getReadableDatabase();
1081        String[] selectionArgs = {account.getUuid(),
1082                contactJid.asBareJid().toString() + "/%",
1083                contactJid.asBareJid().toString()
1084        };
1085        try(final Cursor cursor = db.query(Conversation.TABLENAME, null,
1086                Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID
1087                        + " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null)) {
1088            if (cursor.getCount() == 0) {
1089                return null;
1090            }
1091            cursor.moveToFirst();
1092            final Conversation conversation = Conversation.fromCursor(cursor);
1093            if (conversation.getJid() instanceof InvalidJid) {
1094                return null;
1095            }
1096            return conversation;
1097        }
1098    }
1099
1100    public void updateConversation(final Conversation conversation) {
1101        final SQLiteDatabase db = this.getWritableDatabase();
1102        final String[] args = {conversation.getUuid()};
1103        db.update(Conversation.TABLENAME, conversation.getContentValues(),
1104                Conversation.UUID + "=?", args);
1105    }
1106
1107    public List<Account> getAccounts() {
1108        SQLiteDatabase db = this.getReadableDatabase();
1109        return getAccounts(db);
1110    }
1111
1112    public List<Jid> getAccountJids(final boolean enabledOnly) {
1113        final SQLiteDatabase db = this.getReadableDatabase();
1114        final List<Jid> jids = new ArrayList<>();
1115        final String[] columns = new String[]{Account.USERNAME, Account.SERVER};
1116        final String where = enabledOnly ? "not options & (1 <<1)" : null;
1117        try (final Cursor cursor = db.query(Account.TABLENAME, columns, where, null, null, null, null)) {
1118            while (cursor != null && cursor.moveToNext()) {
1119                jids.add(Jid.of(cursor.getString(0), cursor.getString(1), null));
1120            }
1121        } catch (final Exception e) {
1122            return jids;
1123        }
1124        return jids;
1125    }
1126
1127    private List<Account> getAccounts(SQLiteDatabase db) {
1128        final List<Account> list = new ArrayList<>();
1129        try (final Cursor cursor =
1130                db.query(Account.TABLENAME, null, null, null, null, null, null)) {
1131            while (cursor != null && cursor.moveToNext()) {
1132                list.add(Account.fromCursor(cursor));
1133            }
1134        }
1135        return list;
1136    }
1137
1138    public boolean updateAccount(Account account) {
1139        SQLiteDatabase db = this.getWritableDatabase();
1140        String[] args = {account.getUuid()};
1141        final int rows = db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
1142        return rows == 1;
1143    }
1144
1145    public boolean deleteAccount(Account account) {
1146        SQLiteDatabase db = this.getWritableDatabase();
1147        String[] args = {account.getUuid()};
1148        final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
1149        return rows == 1;
1150    }
1151
1152    public boolean updateMessage(Message message, boolean includeBody) {
1153        SQLiteDatabase db = this.getWritableDatabase();
1154        String[] args = {message.getUuid()};
1155        ContentValues contentValues = message.getContentValues();
1156        contentValues.remove(Message.UUID);
1157        if (!includeBody) {
1158            contentValues.remove(Message.BODY);
1159        }
1160        return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
1161               db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
1162    }
1163
1164    public boolean updateMessage(Message message, String uuid) {
1165        SQLiteDatabase db = this.getWritableDatabase();
1166        String[] args = {uuid};
1167        return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
1168               db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
1169    }
1170
1171    public void readRoster(Roster roster) {
1172        final SQLiteDatabase db = this.getReadableDatabase();
1173        final String[] args = {roster.getAccount().getUuid()};
1174        try (final Cursor cursor =
1175                db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?", args, null, null, null)) {
1176            while (cursor.moveToNext()) {
1177                roster.initContact(Contact.fromCursor(cursor));
1178            }
1179        }
1180    }
1181
1182    public void writeRoster(final Roster roster) {
1183        long start = SystemClock.elapsedRealtime();
1184        final Account account = roster.getAccount();
1185        final SQLiteDatabase db = this.getWritableDatabase();
1186        db.beginTransaction();
1187        for (Contact contact : roster.getContacts()) {
1188            if (contact.getOption(Contact.Options.IN_ROSTER) || contact.hasAvatarOrPresenceName() || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
1189                db.insert(Contact.TABLENAME, null, contact.getContentValues());
1190            } else {
1191                String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
1192                String[] whereArgs = {account.getUuid(), contact.getJid().toString()};
1193                db.delete(Contact.TABLENAME, where, whereArgs);
1194            }
1195        }
1196        db.setTransactionSuccessful();
1197        db.endTransaction();
1198        account.setRosterVersion(roster.getVersion());
1199        updateAccount(account);
1200        long duration = SystemClock.elapsedRealtime() - start;
1201        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": persisted roster in " + duration + "ms");
1202    }
1203
1204    public void deleteMessagesInConversation(Conversation conversation) {
1205        long start = SystemClock.elapsedRealtime();
1206        final SQLiteDatabase db = this.getWritableDatabase();
1207        db.beginTransaction();
1208        final String[] args = {conversation.getUuid()};
1209        int num = db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
1210        db.setTransactionSuccessful();
1211        db.endTransaction();
1212        Log.d(Config.LOGTAG, "deleted " + num + " messages for " + conversation.getJid().asBareJid() + " in " + (SystemClock.elapsedRealtime() - start) + "ms");
1213    }
1214
1215    public void expireOldMessages(long timestamp) {
1216        final String[] args = {String.valueOf(timestamp)};
1217        SQLiteDatabase db = this.getReadableDatabase();
1218        db.beginTransaction();
1219        db.delete(Message.TABLENAME, "timeSent<?", args);
1220        db.setTransactionSuccessful();
1221        db.endTransaction();
1222    }
1223
1224    public MamReference getLastMessageReceived(Account account) {
1225        Cursor cursor = null;
1226        try {
1227            SQLiteDatabase db = this.getReadableDatabase();
1228            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";
1229            String[] args = {account.getUuid()};
1230            cursor = db.rawQuery(sql, args);
1231            if (cursor.getCount() == 0) {
1232                return null;
1233            } else {
1234                cursor.moveToFirst();
1235                return new MamReference(cursor.getLong(0), cursor.getString(1));
1236            }
1237        } catch (Exception e) {
1238            return null;
1239        } finally {
1240            if (cursor != null) {
1241                cursor.close();
1242            }
1243        }
1244    }
1245
1246    public long getLastTimeFingerprintUsed(Account account, String fingerprint) {
1247        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";
1248        String[] args = {account.getUuid(), fingerprint};
1249        Cursor cursor = getReadableDatabase().rawQuery(SQL, args);
1250        long time;
1251        if (cursor.moveToFirst()) {
1252            time = cursor.getLong(0);
1253        } else {
1254            time = 0;
1255        }
1256        cursor.close();
1257        return time;
1258    }
1259
1260    public MamReference getLastClearDate(Account account) {
1261        SQLiteDatabase db = this.getReadableDatabase();
1262        String[] columns = {Conversation.ATTRIBUTES};
1263        String selection = Conversation.ACCOUNT + "=?";
1264        String[] args = {account.getUuid()};
1265        Cursor cursor = db.query(Conversation.TABLENAME, columns, selection, args, null, null, null);
1266        MamReference maxClearDate = new MamReference(0);
1267        while (cursor.moveToNext()) {
1268            try {
1269                final JSONObject o = new JSONObject(cursor.getString(0));
1270                maxClearDate = MamReference.max(maxClearDate, MamReference.fromAttribute(o.getString(Conversation.ATTRIBUTE_LAST_CLEAR_HISTORY)));
1271            } catch (Exception e) {
1272                //ignored
1273            }
1274        }
1275        cursor.close();
1276        return maxClearDate;
1277    }
1278
1279    private Cursor getCursorForSession(Account account, SignalProtocolAddress contact) {
1280        final SQLiteDatabase db = this.getReadableDatabase();
1281        String[] selectionArgs = {account.getUuid(),
1282                contact.getName(),
1283                Integer.toString(contact.getDeviceId())};
1284        return db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1285                null,
1286                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1287                        + SQLiteAxolotlStore.NAME + " = ? AND "
1288                        + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1289                selectionArgs,
1290                null, null, null);
1291    }
1292
1293    public SessionRecord loadSession(Account account, SignalProtocolAddress contact) {
1294        SessionRecord session = null;
1295        Cursor cursor = getCursorForSession(account, contact);
1296        if (cursor.getCount() != 0) {
1297            cursor.moveToFirst();
1298            try {
1299                session = new SessionRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1300            } catch (IOException e) {
1301                cursor.close();
1302                throw new AssertionError(e);
1303            }
1304        }
1305        cursor.close();
1306        return session;
1307    }
1308
1309    public List<Integer> getSubDeviceSessions(Account account, SignalProtocolAddress contact) {
1310        final SQLiteDatabase db = this.getReadableDatabase();
1311        return getSubDeviceSessions(db, account, contact);
1312    }
1313
1314    private List<Integer> getSubDeviceSessions(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1315        List<Integer> devices = new ArrayList<>();
1316        String[] columns = {SQLiteAxolotlStore.DEVICE_ID};
1317        String[] selectionArgs = {account.getUuid(),
1318                contact.getName()};
1319        Cursor cursor = db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1320                columns,
1321                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1322                        + SQLiteAxolotlStore.NAME + " = ?",
1323                selectionArgs,
1324                null, null, null);
1325
1326        while (cursor.moveToNext()) {
1327            devices.add(cursor.getInt(
1328                    cursor.getColumnIndex(SQLiteAxolotlStore.DEVICE_ID)));
1329        }
1330
1331        cursor.close();
1332        return devices;
1333    }
1334
1335    public List<String> getKnownSignalAddresses(Account account) {
1336        List<String> addresses = new ArrayList<>();
1337        String[] colums = {"DISTINCT " + SQLiteAxolotlStore.NAME};
1338        String[] selectionArgs = {account.getUuid()};
1339        Cursor cursor = getReadableDatabase().query(SQLiteAxolotlStore.SESSION_TABLENAME,
1340                colums,
1341                SQLiteAxolotlStore.ACCOUNT + " = ?",
1342                selectionArgs,
1343                null, null, null
1344        );
1345        while (cursor.moveToNext()) {
1346            addresses.add(cursor.getString(0));
1347        }
1348        cursor.close();
1349        return addresses;
1350    }
1351
1352    public boolean containsSession(Account account, SignalProtocolAddress contact) {
1353        Cursor cursor = getCursorForSession(account, contact);
1354        int count = cursor.getCount();
1355        cursor.close();
1356        return count != 0;
1357    }
1358
1359    public void storeSession(Account account, SignalProtocolAddress contact, SessionRecord session) {
1360        SQLiteDatabase db = this.getWritableDatabase();
1361        ContentValues values = new ContentValues();
1362        values.put(SQLiteAxolotlStore.NAME, contact.getName());
1363        values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
1364        values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
1365        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1366        db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
1367    }
1368
1369    public void deleteSession(Account account, SignalProtocolAddress contact) {
1370        SQLiteDatabase db = this.getWritableDatabase();
1371        deleteSession(db, account, contact);
1372    }
1373
1374    private void deleteSession(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1375        String[] args = {account.getUuid(),
1376                contact.getName(),
1377                Integer.toString(contact.getDeviceId())};
1378        db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1379                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1380                        + SQLiteAxolotlStore.NAME + " = ? AND "
1381                        + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1382                args);
1383    }
1384
1385    public void deleteAllSessions(Account account, SignalProtocolAddress contact) {
1386        SQLiteDatabase db = this.getWritableDatabase();
1387        String[] args = {account.getUuid(), contact.getName()};
1388        db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1389                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1390                        + SQLiteAxolotlStore.NAME + " = ?",
1391                args);
1392    }
1393
1394    private Cursor getCursorForPreKey(Account account, int preKeyId) {
1395        SQLiteDatabase db = this.getReadableDatabase();
1396        String[] columns = {SQLiteAxolotlStore.KEY};
1397        String[] selectionArgs = {account.getUuid(), Integer.toString(preKeyId)};
1398        Cursor cursor = db.query(SQLiteAxolotlStore.PREKEY_TABLENAME,
1399                columns,
1400                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1401                        + SQLiteAxolotlStore.ID + "=?",
1402                selectionArgs,
1403                null, null, null);
1404
1405        return cursor;
1406    }
1407
1408    public PreKeyRecord loadPreKey(Account account, int preKeyId) {
1409        PreKeyRecord record = null;
1410        Cursor cursor = getCursorForPreKey(account, preKeyId);
1411        if (cursor.getCount() != 0) {
1412            cursor.moveToFirst();
1413            try {
1414                record = new PreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1415            } catch (IOException e) {
1416                throw new AssertionError(e);
1417            }
1418        }
1419        cursor.close();
1420        return record;
1421    }
1422
1423    public boolean containsPreKey(Account account, int preKeyId) {
1424        Cursor cursor = getCursorForPreKey(account, preKeyId);
1425        int count = cursor.getCount();
1426        cursor.close();
1427        return count != 0;
1428    }
1429
1430    public void storePreKey(Account account, PreKeyRecord record) {
1431        SQLiteDatabase db = this.getWritableDatabase();
1432        ContentValues values = new ContentValues();
1433        values.put(SQLiteAxolotlStore.ID, record.getId());
1434        values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1435        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1436        db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
1437    }
1438
1439    public int deletePreKey(Account account, int preKeyId) {
1440        SQLiteDatabase db = this.getWritableDatabase();
1441        String[] args = {account.getUuid(), Integer.toString(preKeyId)};
1442        return db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1443                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1444                        + SQLiteAxolotlStore.ID + "=?",
1445                args);
1446    }
1447
1448    private Cursor getCursorForSignedPreKey(Account account, int signedPreKeyId) {
1449        SQLiteDatabase db = this.getReadableDatabase();
1450        String[] columns = {SQLiteAxolotlStore.KEY};
1451        String[] selectionArgs = {account.getUuid(), Integer.toString(signedPreKeyId)};
1452        Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1453                columns,
1454                SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
1455                selectionArgs,
1456                null, null, null);
1457
1458        return cursor;
1459    }
1460
1461    public SignedPreKeyRecord loadSignedPreKey(Account account, int signedPreKeyId) {
1462        SignedPreKeyRecord record = null;
1463        Cursor cursor = getCursorForSignedPreKey(account, signedPreKeyId);
1464        if (cursor.getCount() != 0) {
1465            cursor.moveToFirst();
1466            try {
1467                record = new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1468            } catch (IOException e) {
1469                throw new AssertionError(e);
1470            }
1471        }
1472        cursor.close();
1473        return record;
1474    }
1475
1476    public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
1477        List<SignedPreKeyRecord> prekeys = new ArrayList<>();
1478        SQLiteDatabase db = this.getReadableDatabase();
1479        String[] columns = {SQLiteAxolotlStore.KEY};
1480        String[] selectionArgs = {account.getUuid()};
1481        Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1482                columns,
1483                SQLiteAxolotlStore.ACCOUNT + "=?",
1484                selectionArgs,
1485                null, null, null);
1486
1487        while (cursor.moveToNext()) {
1488            try {
1489                prekeys.add(new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT)));
1490            } catch (IOException ignored) {
1491            }
1492        }
1493        cursor.close();
1494        return prekeys;
1495    }
1496
1497    public int getSignedPreKeysCount(Account account) {
1498        String[] columns = {"count(" + SQLiteAxolotlStore.KEY + ")"};
1499        String[] selectionArgs = {account.getUuid()};
1500        SQLiteDatabase db = this.getReadableDatabase();
1501        Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1502                columns,
1503                SQLiteAxolotlStore.ACCOUNT + "=?",
1504                selectionArgs,
1505                null, null, null);
1506        final int count;
1507        if (cursor.moveToFirst()) {
1508            count = cursor.getInt(0);
1509        } else {
1510            count = 0;
1511        }
1512        cursor.close();
1513        return count;
1514    }
1515
1516    public boolean containsSignedPreKey(Account account, int signedPreKeyId) {
1517        Cursor cursor = getCursorForPreKey(account, signedPreKeyId);
1518        int count = cursor.getCount();
1519        cursor.close();
1520        return count != 0;
1521    }
1522
1523    public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
1524        SQLiteDatabase db = this.getWritableDatabase();
1525        ContentValues values = new ContentValues();
1526        values.put(SQLiteAxolotlStore.ID, record.getId());
1527        values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1528        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1529        db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
1530    }
1531
1532    public void deleteSignedPreKey(Account account, int signedPreKeyId) {
1533        SQLiteDatabase db = this.getWritableDatabase();
1534        String[] args = {account.getUuid(), Integer.toString(signedPreKeyId)};
1535        db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1536                SQLiteAxolotlStore.ACCOUNT + "=? AND "
1537                        + SQLiteAxolotlStore.ID + "=?",
1538                args);
1539    }
1540
1541    private Cursor getIdentityKeyCursor(Account account, String name, boolean own) {
1542        final SQLiteDatabase db = this.getReadableDatabase();
1543        return getIdentityKeyCursor(db, account, name, own);
1544    }
1545
1546    private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, boolean own) {
1547        return getIdentityKeyCursor(db, account, name, own, null);
1548    }
1549
1550    private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
1551        final SQLiteDatabase db = this.getReadableDatabase();
1552        return getIdentityKeyCursor(db, account, fingerprint);
1553    }
1554
1555    private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String fingerprint) {
1556        return getIdentityKeyCursor(db, account, null, null, fingerprint);
1557    }
1558
1559    private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, Boolean own, String fingerprint) {
1560        String[] columns = {SQLiteAxolotlStore.TRUST,
1561                SQLiteAxolotlStore.ACTIVE,
1562                SQLiteAxolotlStore.LAST_ACTIVATION,
1563                SQLiteAxolotlStore.KEY};
1564        ArrayList<String> selectionArgs = new ArrayList<>(4);
1565        selectionArgs.add(account.getUuid());
1566        String selectionString = SQLiteAxolotlStore.ACCOUNT + " = ?";
1567        if (name != null) {
1568            selectionArgs.add(name);
1569            selectionString += " AND " + SQLiteAxolotlStore.NAME + " = ?";
1570        }
1571        if (fingerprint != null) {
1572            selectionArgs.add(fingerprint);
1573            selectionString += " AND " + SQLiteAxolotlStore.FINGERPRINT + " = ?";
1574        }
1575        if (own != null) {
1576            selectionArgs.add(own ? "1" : "0");
1577            selectionString += " AND " + SQLiteAxolotlStore.OWN + " = ?";
1578        }
1579        Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1580                columns,
1581                selectionString,
1582                selectionArgs.toArray(new String[selectionArgs.size()]),
1583                null, null, null);
1584
1585        return cursor;
1586    }
1587
1588    public IdentityKeyPair loadOwnIdentityKeyPair(Account account) {
1589        SQLiteDatabase db = getReadableDatabase();
1590        return loadOwnIdentityKeyPair(db, account);
1591    }
1592
1593    private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
1594        String name = account.getJid().asBareJid().toString();
1595        IdentityKeyPair identityKeyPair = null;
1596        Cursor cursor = getIdentityKeyCursor(db, account, name, true);
1597        if (cursor.getCount() != 0) {
1598            cursor.moveToFirst();
1599            try {
1600                identityKeyPair = new IdentityKeyPair(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1601            } catch (InvalidKeyException e) {
1602                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1603            }
1604        }
1605        cursor.close();
1606
1607        return identityKeyPair;
1608    }
1609
1610    public Set<IdentityKey> loadIdentityKeys(Account account, String name) {
1611        return loadIdentityKeys(account, name, null);
1612    }
1613
1614    public Set<IdentityKey> loadIdentityKeys(Account account, String name, FingerprintStatus status) {
1615        Set<IdentityKey> identityKeys = new HashSet<>();
1616        Cursor cursor = getIdentityKeyCursor(account, name, false);
1617
1618        while (cursor.moveToNext()) {
1619            if (status != null && !FingerprintStatus.fromCursor(cursor).equals(status)) {
1620                continue;
1621            }
1622            try {
1623                String key = cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY));
1624                if (key != null) {
1625                    identityKeys.add(new IdentityKey(Base64.decode(key, Base64.DEFAULT), 0));
1626                } else {
1627                    Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Missing key (possibly preverified) in database for account" + account.getJid().asBareJid() + ", address: " + name);
1628                }
1629            } catch (InvalidKeyException e) {
1630                Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1631            }
1632        }
1633        cursor.close();
1634
1635        return identityKeys;
1636    }
1637
1638    public long numTrustedKeys(Account account, String name) {
1639        SQLiteDatabase db = getReadableDatabase();
1640        String[] args = {
1641                account.getUuid(),
1642                name,
1643                FingerprintStatus.Trust.TRUSTED.toString(),
1644                FingerprintStatus.Trust.VERIFIED.toString(),
1645                FingerprintStatus.Trust.VERIFIED_X509.toString()
1646        };
1647        return DatabaseUtils.queryNumEntries(db, SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1648                SQLiteAxolotlStore.ACCOUNT + " = ?"
1649                        + " AND " + SQLiteAxolotlStore.NAME + " = ?"
1650                        + " AND (" + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ?)"
1651                        + " AND " + SQLiteAxolotlStore.ACTIVE + " > 0",
1652                args
1653        );
1654    }
1655
1656    private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized, FingerprintStatus status) {
1657        SQLiteDatabase db = this.getWritableDatabase();
1658        ContentValues values = new ContentValues();
1659        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1660        values.put(SQLiteAxolotlStore.NAME, name);
1661        values.put(SQLiteAxolotlStore.OWN, own ? 1 : 0);
1662        values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1663        values.put(SQLiteAxolotlStore.KEY, base64Serialized);
1664        values.putAll(status.toContentValues());
1665        String where = SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.NAME + "=? AND " + SQLiteAxolotlStore.FINGERPRINT + " =?";
1666        String[] whereArgs = {account.getUuid(), name, fingerprint};
1667        int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, where, whereArgs);
1668        if (rows == 0) {
1669            db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1670        }
1671    }
1672
1673    public void storePreVerification(Account account, String name, String fingerprint, FingerprintStatus status) {
1674        SQLiteDatabase db = this.getWritableDatabase();
1675        ContentValues values = new ContentValues();
1676        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1677        values.put(SQLiteAxolotlStore.NAME, name);
1678        values.put(SQLiteAxolotlStore.OWN, 0);
1679        values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1680        values.putAll(status.toContentValues());
1681        db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1682    }
1683
1684    public FingerprintStatus getFingerprintStatus(Account account, String fingerprint) {
1685        Cursor cursor = getIdentityKeyCursor(account, fingerprint);
1686        final FingerprintStatus status;
1687        if (cursor.getCount() > 0) {
1688            cursor.moveToFirst();
1689            status = FingerprintStatus.fromCursor(cursor);
1690        } else {
1691            status = null;
1692        }
1693        cursor.close();
1694        return status;
1695    }
1696
1697    public boolean setIdentityKeyTrust(Account account, String fingerprint, FingerprintStatus fingerprintStatus) {
1698        SQLiteDatabase db = this.getWritableDatabase();
1699        return setIdentityKeyTrust(db, account, fingerprint, fingerprintStatus);
1700    }
1701
1702    private boolean setIdentityKeyTrust(SQLiteDatabase db, Account account, String fingerprint, FingerprintStatus status) {
1703        String[] selectionArgs = {
1704                account.getUuid(),
1705                fingerprint
1706        };
1707        int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, status.toContentValues(),
1708                SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1709                        + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1710                selectionArgs);
1711        return rows == 1;
1712    }
1713
1714    public boolean setIdentityKeyCertificate(Account account, String fingerprint, X509Certificate x509Certificate) {
1715        SQLiteDatabase db = this.getWritableDatabase();
1716        String[] selectionArgs = {
1717                account.getUuid(),
1718                fingerprint
1719        };
1720        try {
1721            ContentValues values = new ContentValues();
1722            values.put(SQLiteAxolotlStore.CERTIFICATE, x509Certificate.getEncoded());
1723            return db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
1724                    SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1725                            + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1726                    selectionArgs) == 1;
1727        } catch (CertificateEncodingException e) {
1728            Log.d(Config.LOGTAG, "could not encode certificate");
1729            return false;
1730        }
1731    }
1732
1733    public X509Certificate getIdentityKeyCertifcate(Account account, String fingerprint) {
1734        SQLiteDatabase db = this.getReadableDatabase();
1735        String[] selectionArgs = {
1736                account.getUuid(),
1737                fingerprint
1738        };
1739        String[] colums = {SQLiteAxolotlStore.CERTIFICATE};
1740        String selection = SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ";
1741        Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME, colums, selection, selectionArgs, null, null, null);
1742        if (cursor.getCount() < 1) {
1743            return null;
1744        } else {
1745            cursor.moveToFirst();
1746            byte[] certificate = cursor.getBlob(cursor.getColumnIndex(SQLiteAxolotlStore.CERTIFICATE));
1747            cursor.close();
1748            if (certificate == null || certificate.length == 0) {
1749                return null;
1750            }
1751            try {
1752                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
1753                return (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(certificate));
1754            } catch (CertificateException e) {
1755                Log.d(Config.LOGTAG, "certificate exception " + e.getMessage());
1756                return null;
1757            }
1758        }
1759    }
1760
1761    public void storeIdentityKey(Account account, String name, IdentityKey identityKey, FingerprintStatus status) {
1762        storeIdentityKey(account, name, false, CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()), Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT), status);
1763    }
1764
1765    public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
1766        storeIdentityKey(account, account.getJid().asBareJid().toString(), true, CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), FingerprintStatus.createActiveVerified(false));
1767    }
1768
1769
1770    private void recreateAxolotlDb(SQLiteDatabase db) {
1771        Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<");
1772        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME);
1773        db.execSQL(CREATE_SESSIONS_STATEMENT);
1774        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.PREKEY_TABLENAME);
1775        db.execSQL(CREATE_PREKEYS_STATEMENT);
1776        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME);
1777        db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
1778        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.IDENTITIES_TABLENAME);
1779        db.execSQL(CREATE_IDENTITIES_STATEMENT);
1780    }
1781
1782    public void wipeAxolotlDb(Account account) {
1783        String accountName = account.getUuid();
1784        Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ">>> WIPING AXOLOTL DATABASE FOR ACCOUNT " + accountName + " <<<");
1785        SQLiteDatabase db = this.getWritableDatabase();
1786        String[] deleteArgs = {
1787                accountName
1788        };
1789        db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1790                SQLiteAxolotlStore.ACCOUNT + " = ?",
1791                deleteArgs);
1792        db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1793                SQLiteAxolotlStore.ACCOUNT + " = ?",
1794                deleteArgs);
1795        db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1796                SQLiteAxolotlStore.ACCOUNT + " = ?",
1797                deleteArgs);
1798        db.delete(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1799                SQLiteAxolotlStore.ACCOUNT + " = ?",
1800                deleteArgs);
1801    }
1802
1803    public List<ShortcutService.FrequentContact> getFrequentContacts(int days) {
1804        SQLiteDatabase db = this.getReadableDatabase();
1805        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;";
1806        String[] whereArgs = new String[]{String.valueOf(System.currentTimeMillis() - (Config.MILLISECONDS_IN_DAY * days))};
1807        Cursor cursor = db.rawQuery(SQL, whereArgs);
1808        ArrayList<ShortcutService.FrequentContact> contacts = new ArrayList<>();
1809        while (cursor.moveToNext()) {
1810            try {
1811                contacts.add(new ShortcutService.FrequentContact(cursor.getString(0), Jid.of(cursor.getString(1))));
1812            } catch (Exception e) {
1813                Log.d(Config.LOGTAG, e.getMessage());
1814            }
1815        }
1816        cursor.close();
1817        return contacts;
1818    }
1819}