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