DatabaseBackend.java

   1package eu.siacs.conversations.persistance;
   2
   3import android.content.ContentValues;
   4import android.content.Context;
   5import android.database.Cursor;
   6import android.database.DatabaseUtils;
   7import android.database.sqlite.SQLiteAbortException;
   8import android.database.sqlite.SQLiteDatabase;
   9import android.database.sqlite.SQLiteException;
  10import android.database.sqlite.SQLiteOpenHelper;
  11import android.text.TextUtils;
  12import android.os.Build;
  13import android.os.Environment;
  14import android.os.SystemClock;
  15import android.util.Base64;
  16import android.util.Log;
  17import com.cheogram.android.WebxdcUpdate;
  18
  19import com.google.common.base.Stopwatch;
  20import com.google.common.collect.Multimap;
  21import com.google.common.collect.HashMultimap;
  22
  23import org.json.JSONException;
  24import org.json.JSONObject;
  25import org.jxmpp.jid.parts.Localpart;
  26import org.jxmpp.stringprep.XmppStringprepException;
  27import org.whispersystems.libsignal.IdentityKey;
  28import org.whispersystems.libsignal.IdentityKeyPair;
  29import org.whispersystems.libsignal.InvalidKeyException;
  30import org.whispersystems.libsignal.SignalProtocolAddress;
  31import org.whispersystems.libsignal.state.PreKeyRecord;
  32import org.whispersystems.libsignal.state.SessionRecord;
  33import org.whispersystems.libsignal.state.SignedPreKeyRecord;
  34
  35import java.io.ByteArrayInputStream;
  36import java.io.File;
  37import java.io.IOException;
  38import java.nio.charset.StandardCharsets;
  39import java.security.cert.CertificateEncodingException;
  40import java.security.cert.CertificateException;
  41import java.security.cert.CertificateFactory;
  42import java.security.cert.X509Certificate;
  43import java.util.ArrayList;
  44import java.util.Collection;
  45import java.util.HashMap;
  46import java.util.Hashtable;
  47import java.util.HashSet;
  48import java.util.List;
  49import java.util.Map;
  50import java.util.Set;
  51import java.util.UUID;
  52import java.util.concurrent.CopyOnWriteArrayList;
  53
  54import im.conversations.android.xmpp.model.occupant.OccupantId;
  55import io.ipfs.cid.Cid;
  56
  57import com.google.common.collect.ImmutableMap;
  58import eu.siacs.conversations.Config;
  59import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  60import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
  61import eu.siacs.conversations.crypto.axolotl.SQLiteAxolotlStore;
  62import eu.siacs.conversations.entities.Account;
  63import eu.siacs.conversations.entities.Contact;
  64import eu.siacs.conversations.entities.Conversation;
  65import eu.siacs.conversations.entities.DownloadableFile;
  66import eu.siacs.conversations.entities.Message;
  67import eu.siacs.conversations.entities.MucOptions;
  68import eu.siacs.conversations.entities.PresenceTemplate;
  69import eu.siacs.conversations.services.QuickConversationsService;
  70import eu.siacs.conversations.services.ShortcutService;
  71import eu.siacs.conversations.utils.CryptoHelper;
  72import eu.siacs.conversations.utils.CursorUtils;
  73import eu.siacs.conversations.utils.FtsUtils;
  74import eu.siacs.conversations.utils.MimeUtils;
  75import eu.siacs.conversations.utils.Resolver;
  76import eu.siacs.conversations.xml.Namespace;
  77import eu.siacs.conversations.xmpp.Jid;
  78import eu.siacs.conversations.xmpp.mam.MamReference;
  79import im.conversations.android.xml.XmlElementReader;
  80import im.conversations.android.xmpp.EntityCapabilities;
  81import im.conversations.android.xmpp.EntityCapabilities2;
  82import im.conversations.android.xmpp.model.disco.info.InfoQuery;
  83
  84public class DatabaseBackend extends SQLiteOpenHelper {
  85
  86    private static final String DATABASE_NAME = "history";
  87    private static final int DATABASE_VERSION = 54;
  88
  89    private static boolean requiresMessageIndexRebuild = false;
  90    private static DatabaseBackend instance = null;
  91    private static final String CREATE_CONTACTS_STATEMENT =
  92            "create table "
  93                    + Contact.TABLENAME
  94                    + "("
  95                    + Contact.ACCOUNT
  96                    + " TEXT, "
  97                    + Contact.SERVERNAME
  98                    + " TEXT, "
  99                    + Contact.SYSTEMNAME
 100                    + " TEXT,"
 101                    + Contact.PRESENCE_NAME
 102                    + " TEXT,"
 103                    + Contact.JID
 104                    + " TEXT,"
 105                    + Contact.KEYS
 106                    + " TEXT,"
 107                    + Contact.PHOTOURI
 108                    + " TEXT,"
 109                    + Contact.OPTIONS
 110                    + " NUMBER,"
 111                    + Contact.SYSTEMACCOUNT
 112                    + " NUMBER, "
 113                    + Contact.AVATAR
 114                    + " TEXT, "
 115                    + Contact.LAST_PRESENCE
 116                    + " TEXT, "
 117                    + Contact.LAST_TIME
 118                    + " NUMBER, "
 119                    + Contact.RTP_CAPABILITY
 120                    + " TEXT,"
 121                    + Contact.GROUPS
 122                    + " TEXT, FOREIGN KEY("
 123                    + Contact.ACCOUNT
 124                    + ") REFERENCES "
 125                    + Account.TABLENAME
 126                    + "("
 127                    + Account.UUID
 128                    + ") ON DELETE CASCADE, UNIQUE("
 129                    + Contact.ACCOUNT
 130                    + ", "
 131                    + Contact.JID
 132                    + ") ON CONFLICT REPLACE);";
 133
 134    private static final String CREATE_PRESENCE_TEMPLATES_STATEMENT =
 135            "CREATE TABLE "
 136                    + PresenceTemplate.TABELNAME
 137                    + "("
 138                    + PresenceTemplate.UUID
 139                    + " TEXT, "
 140                    + PresenceTemplate.LAST_USED
 141                    + " NUMBER,"
 142                    + PresenceTemplate.MESSAGE
 143                    + " TEXT,"
 144                    + PresenceTemplate.STATUS
 145                    + " TEXT,"
 146                    + "UNIQUE("
 147                    + PresenceTemplate.MESSAGE
 148                    + ","
 149                    + PresenceTemplate.STATUS
 150                    + ") ON CONFLICT REPLACE);";
 151
 152    private static final String CREATE_PREKEYS_STATEMENT =
 153            "CREATE TABLE "
 154                    + SQLiteAxolotlStore.PREKEY_TABLENAME
 155                    + "("
 156                    + SQLiteAxolotlStore.ACCOUNT
 157                    + " TEXT,  "
 158                    + SQLiteAxolotlStore.ID
 159                    + " INTEGER, "
 160                    + SQLiteAxolotlStore.KEY
 161                    + " TEXT, FOREIGN KEY("
 162                    + SQLiteAxolotlStore.ACCOUNT
 163                    + ") REFERENCES "
 164                    + Account.TABLENAME
 165                    + "("
 166                    + Account.UUID
 167                    + ") ON DELETE CASCADE, "
 168                    + "UNIQUE( "
 169                    + SQLiteAxolotlStore.ACCOUNT
 170                    + ", "
 171                    + SQLiteAxolotlStore.ID
 172                    + ") ON CONFLICT REPLACE"
 173                    + ");";
 174
 175    private static final String CREATE_SIGNED_PREKEYS_STATEMENT =
 176            "CREATE TABLE "
 177                    + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME
 178                    + "("
 179                    + SQLiteAxolotlStore.ACCOUNT
 180                    + " TEXT,  "
 181                    + SQLiteAxolotlStore.ID
 182                    + " INTEGER, "
 183                    + SQLiteAxolotlStore.KEY
 184                    + " TEXT, FOREIGN KEY("
 185                    + SQLiteAxolotlStore.ACCOUNT
 186                    + ") REFERENCES "
 187                    + Account.TABLENAME
 188                    + "("
 189                    + Account.UUID
 190                    + ") ON DELETE CASCADE, "
 191                    + "UNIQUE( "
 192                    + SQLiteAxolotlStore.ACCOUNT
 193                    + ", "
 194                    + SQLiteAxolotlStore.ID
 195                    + ") ON CONFLICT REPLACE"
 196                    + ");";
 197
 198    private static final String CREATE_SESSIONS_STATEMENT =
 199            "CREATE TABLE "
 200                    + SQLiteAxolotlStore.SESSION_TABLENAME
 201                    + "("
 202                    + SQLiteAxolotlStore.ACCOUNT
 203                    + " TEXT,  "
 204                    + SQLiteAxolotlStore.NAME
 205                    + " TEXT, "
 206                    + SQLiteAxolotlStore.DEVICE_ID
 207                    + " INTEGER, "
 208                    + SQLiteAxolotlStore.KEY
 209                    + " TEXT, FOREIGN KEY("
 210                    + SQLiteAxolotlStore.ACCOUNT
 211                    + ") REFERENCES "
 212                    + Account.TABLENAME
 213                    + "("
 214                    + Account.UUID
 215                    + ") ON DELETE CASCADE, "
 216                    + "UNIQUE( "
 217                    + SQLiteAxolotlStore.ACCOUNT
 218                    + ", "
 219                    + SQLiteAxolotlStore.NAME
 220                    + ", "
 221                    + SQLiteAxolotlStore.DEVICE_ID
 222                    + ") ON CONFLICT REPLACE"
 223                    + ");";
 224
 225    private static final String CREATE_IDENTITIES_STATEMENT =
 226            "CREATE TABLE "
 227                    + SQLiteAxolotlStore.IDENTITIES_TABLENAME
 228                    + "("
 229                    + SQLiteAxolotlStore.ACCOUNT
 230                    + " TEXT,  "
 231                    + SQLiteAxolotlStore.NAME
 232                    + " TEXT, "
 233                    + SQLiteAxolotlStore.OWN
 234                    + " INTEGER, "
 235                    + SQLiteAxolotlStore.FINGERPRINT
 236                    + " TEXT, "
 237                    + SQLiteAxolotlStore.CERTIFICATE
 238                    + " BLOB, "
 239                    + SQLiteAxolotlStore.TRUST
 240                    + " TEXT, "
 241                    + SQLiteAxolotlStore.ACTIVE
 242                    + " NUMBER, "
 243                    + SQLiteAxolotlStore.LAST_ACTIVATION
 244                    + " NUMBER,"
 245                    + SQLiteAxolotlStore.KEY
 246                    + " TEXT, FOREIGN KEY("
 247                    + SQLiteAxolotlStore.ACCOUNT
 248                    + ") REFERENCES "
 249                    + Account.TABLENAME
 250                    + "("
 251                    + Account.UUID
 252                    + ") ON DELETE CASCADE, "
 253                    + "UNIQUE( "
 254                    + SQLiteAxolotlStore.ACCOUNT
 255                    + ", "
 256                    + SQLiteAxolotlStore.NAME
 257                    + ", "
 258                    + SQLiteAxolotlStore.FINGERPRINT
 259                    + ") ON CONFLICT IGNORE"
 260                    + ");";
 261
 262    private static final String CREATE_CAPS_CACHE_TABLE =
 263            "CREATE TABLE caps_cache (caps TEXT, caps2 TEXT, disco_info TEXT, UNIQUE (caps), UNIQUE"
 264                    + " (caps2));";
 265    private static final String CREATE_CAPS_CACHE_INDEX_CAPS =
 266            "CREATE INDEX idx_caps ON caps_cache(caps);";
 267    private static final String CREATE_CAPS_CACHE_INDEX_CAPS2 =
 268            "CREATE INDEX idx_caps2 ON caps_cache(caps2);";
 269
 270    private static final String RESOLVER_RESULTS_TABLENAME = "resolver_results";
 271
 272    private static final String CREATE_RESOLVER_RESULTS_TABLE =
 273            "create table "
 274                    + RESOLVER_RESULTS_TABLENAME
 275                    + "("
 276                    + Resolver.Result.DOMAIN
 277                    + " TEXT,"
 278                    + Resolver.Result.HOSTNAME
 279                    + " TEXT,"
 280                    + Resolver.Result.IP
 281                    + " BLOB,"
 282                    + Resolver.Result.PRIORITY
 283                    + " NUMBER,"
 284                    + Resolver.Result.DIRECT_TLS
 285                    + " NUMBER,"
 286                    + Resolver.Result.AUTHENTICATED
 287                    + " NUMBER,"
 288                    + Resolver.Result.PORT
 289                    + " NUMBER,"
 290                    + "UNIQUE("
 291                    + Resolver.Result.DOMAIN
 292                    + ") ON CONFLICT REPLACE"
 293                    + ");";
 294
 295    private static final String CREATE_MESSAGE_TIME_INDEX =
 296            "CREATE INDEX message_time_index ON "
 297                    + Message.TABLENAME
 298                    + "("
 299                    + Message.TIME_SENT
 300                    + ")";
 301    private static final String CREATE_MESSAGE_CONVERSATION_INDEX =
 302            "CREATE INDEX message_conversation_index ON "
 303                    + Message.TABLENAME
 304                    + "("
 305                    + Message.CONVERSATION
 306                    + ")";
 307    private static final String CREATE_MESSAGE_DELETED_INDEX =
 308            "CREATE INDEX message_deleted_index ON "
 309                    + Message.TABLENAME
 310                    + "("
 311                    + Message.DELETED
 312                    + ")";
 313    private static final String CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX =
 314            "CREATE INDEX message_file_path_index ON "
 315                    + Message.TABLENAME
 316                    + "("
 317                    + Message.RELATIVE_FILE_PATH
 318                    + ")";
 319    private static final String CREATE_MESSAGE_TYPE_INDEX =
 320            "CREATE INDEX message_type_index ON " + Message.TABLENAME + "(" + Message.TYPE + ")";
 321
 322    private static final String CREATE_MESSAGE_INDEX_TABLE =
 323            "CREATE VIRTUAL TABLE messages_index USING fts4"
 324                    + " (uuid,body,notindexed=\"uuid\",content=\""
 325                    + Message.TABLENAME
 326                    + "\",tokenize='unicode61')";
 327    private static final String CREATE_MESSAGE_INSERT_TRIGGER =
 328            "CREATE TRIGGER after_message_insert AFTER INSERT ON "
 329                    + Message.TABLENAME
 330                    + " BEGIN INSERT INTO messages_index(rowid,uuid,body)"
 331                    + " VALUES(NEW.rowid,NEW.uuid,NEW.body); END;";
 332    private static final String CREATE_MESSAGE_UPDATE_TRIGGER =
 333            "CREATE TRIGGER after_message_update UPDATE OF uuid,body ON "
 334                    + Message.TABLENAME
 335                    + " BEGIN UPDATE messages_index SET body=NEW.body,uuid=NEW.uuid WHERE"
 336                    + " rowid=OLD.rowid; END;";
 337    private static final String CREATE_MESSAGE_DELETE_TRIGGER =
 338            "CREATE TRIGGER after_message_delete AFTER DELETE ON "
 339                    + Message.TABLENAME
 340                    + " BEGIN DELETE FROM messages_index WHERE rowid=OLD.rowid; END;";
 341    private static final String COPY_PREEXISTING_ENTRIES =
 342            "INSERT INTO messages_index(messages_index) VALUES('rebuild');";
 343
 344    protected Context context;
 345
 346    private DatabaseBackend(Context context) {
 347        super(context, DATABASE_NAME, null, DATABASE_VERSION);
 348        this.context = context;
 349        setWriteAheadLoggingEnabled(true);
 350    }
 351
 352    private static ContentValues createFingerprintStatusContentValues(
 353            FingerprintStatus.Trust trust, boolean active) {
 354        ContentValues values = new ContentValues();
 355        values.put(SQLiteAxolotlStore.TRUST, trust.toString());
 356        values.put(SQLiteAxolotlStore.ACTIVE, active ? 1 : 0);
 357        return values;
 358    }
 359
 360    public static boolean requiresMessageIndexRebuild() {
 361        return requiresMessageIndexRebuild;
 362    }
 363
 364    public void rebuildMessagesIndex() {
 365        final SQLiteDatabase db = getWritableDatabase();
 366        final Stopwatch stopwatch = Stopwatch.createStarted();
 367        db.execSQL(COPY_PREEXISTING_ENTRIES);
 368        Log.d(Config.LOGTAG, "rebuilt message index in " + stopwatch.stop());
 369    }
 370
 371    public static synchronized DatabaseBackend getInstance(Context context) {
 372        if (instance == null) {
 373            instance = new DatabaseBackend(context);
 374        }
 375        return instance;
 376    }
 377
 378    protected void cheogramMigrate(SQLiteDatabase db) {
 379        db.beginTransaction();
 380
 381        try {
 382            Cursor cursor = db.rawQuery("PRAGMA cheogram.user_version", null);
 383            cursor.moveToNext();
 384            int cheogramVersion = cursor.getInt(0);
 385            cursor.close();
 386
 387            if(cheogramVersion < 1) {
 388                // No cross-DB foreign keys unfortunately
 389                db.execSQL(
 390                    "CREATE TABLE cheogram." + Message.TABLENAME + "(" +
 391                    Message.UUID + " TEXT PRIMARY KEY, " +
 392                    "subject TEXT" +
 393                    ")"
 394                );
 395                db.execSQL("PRAGMA cheogram.user_version = 1");
 396            }
 397
 398            if(cheogramVersion < 2) {
 399                db.execSQL(
 400                    "ALTER TABLE cheogram." + Message.TABLENAME + " " +
 401                    "ADD COLUMN oobUri TEXT"
 402                );
 403                db.execSQL(
 404                    "ALTER TABLE cheogram." + Message.TABLENAME + " " +
 405                    "ADD COLUMN fileParams TEXT"
 406                );
 407                db.execSQL("PRAGMA cheogram.user_version = 2");
 408            }
 409
 410            if(cheogramVersion < 3) {
 411                db.execSQL(
 412                    "ALTER TABLE cheogram." + Message.TABLENAME + " " +
 413                    "ADD COLUMN payloads TEXT"
 414                );
 415                db.execSQL("PRAGMA cheogram.user_version = 3");
 416            }
 417
 418            if(cheogramVersion < 4) {
 419                db.execSQL(
 420                    "CREATE TABLE cheogram.cids (" +
 421                    "cid TEXT NOT NULL PRIMARY KEY," +
 422                    "path TEXT NOT NULL" +
 423                    ")"
 424                );
 425                db.execSQL("PRAGMA cheogram.user_version = 4");
 426            }
 427
 428            if(cheogramVersion < 5) {
 429                db.execSQL(
 430                    "ALTER TABLE cheogram." + Message.TABLENAME + " " +
 431                    "ADD COLUMN timeReceived NUMBER"
 432                );
 433                db.execSQL("CREATE INDEX cheogram.message_time_received_index ON " + Message.TABLENAME + " (timeReceived)");
 434                db.execSQL("PRAGMA cheogram.user_version = 5");
 435            }
 436
 437            if(cheogramVersion < 6) {
 438                db.execSQL(
 439                    "CREATE TABLE cheogram.blocked_media (" +
 440                    "cid TEXT NOT NULL PRIMARY KEY" +
 441                    ")"
 442                );
 443                db.execSQL("PRAGMA cheogram.user_version = 6");
 444            }
 445
 446            if(cheogramVersion < 7) {
 447                db.execSQL(
 448                    "ALTER TABLE cheogram.cids " +
 449                    "ADD COLUMN url TEXT"
 450                );
 451                db.execSQL("PRAGMA cheogram.user_version = 7");
 452            }
 453
 454            if(cheogramVersion < 8) {
 455                db.execSQL(
 456                    "CREATE TABLE cheogram.webxdc_updates (" +
 457                    "serial INTEGER PRIMARY KEY AUTOINCREMENT, " +
 458                    Message.CONVERSATION + " TEXT NOT NULL, " +
 459                    "sender TEXT NOT NULL, " +
 460                    "thread TEXT NOT NULL, " +
 461                    "threadParent TEXT, " +
 462                    "info TEXT, " +
 463                    "document TEXT, " +
 464                    "summary TEXT, " +
 465                    "payload TEXT" +
 466                    ")"
 467                );
 468                db.execSQL("CREATE INDEX cheogram.webxdc_index ON webxdc_updates (" + Message.CONVERSATION + ", thread)");
 469                db.execSQL("PRAGMA cheogram.user_version = 8");
 470				}
 471
 472            if(cheogramVersion < 9) {
 473                db.execSQL(
 474                    "ALTER TABLE cheogram.webxdc_updates " +
 475                    "ADD COLUMN message_id TEXT"
 476                );
 477                db.execSQL("CREATE UNIQUE INDEX cheogram.webxdc_message_id_index ON webxdc_updates (" + Message.CONVERSATION + ", message_id)");
 478                db.execSQL("PRAGMA cheogram.user_version = 9");
 479            }
 480
 481            if(cheogramVersion < 10) {
 482                db.execSQL(
 483                    "CREATE TABLE cheogram.muted_participants (" +
 484                    "muc_jid TEXT NOT NULL, " +
 485                    "occupant_id TEXT NOT NULL, " +
 486                    "nick TEXT NOT NULL," +
 487                    "PRIMARY KEY (muc_jid, occupant_id)" +
 488                    ")"
 489                );
 490                db.execSQL(
 491                    "ALTER TABLE cheogram." + Message.TABLENAME + " " +
 492                    "ADD COLUMN occupant_id TEXT"
 493                );
 494                db.execSQL("PRAGMA cheogram.user_version = 10");
 495            }
 496
 497            if(cheogramVersion < 11) {
 498                if (Build.VERSION.SDK_INT >= 34) {
 499                    db.execSQL(
 500                        "ALTER TABLE cheogram.muted_participants " +
 501                        "DROP COLUMN nick"
 502                    );
 503                } else {
 504                    db.execSQL("DROP TABLE cheogram.muted_participants");
 505                    db.execSQL(
 506                        "CREATE TABLE cheogram.muted_participants (" +
 507                        "muc_jid TEXT NOT NULL, " +
 508                        "occupant_id TEXT NOT NULL, " +
 509                        "PRIMARY KEY (muc_jid, occupant_id)" +
 510                        ")"
 511                    );
 512                }
 513                db.execSQL("PRAGMA cheogram.user_version = 11");
 514            }
 515
 516            if(cheogramVersion < 12) {
 517                db.execSQL(
 518                    "ALTER TABLE cheogram." + Message.TABLENAME + " " +
 519                    "ADD COLUMN notificationDismissed NUMBER DEFAULT 0"
 520                );
 521                db.execSQL("PRAGMA cheogram.user_version = 12");
 522            }
 523
 524            if (cheogramVersion < 13) {
 525                db.execSQL(
 526                        "CREATE TABLE cheogram." + MucOptions.User.CacheEntry.TABLENAME + " (" +
 527                                "conversation_uuid TEXT NOT NULL, " +
 528                                "occupant_id TEXT NOT NULL, " +
 529                                "nick TEXT, " +
 530                                "avatar TEXT, " +
 531                                "PRIMARY KEY (conversation_uuid, occupant_id)" +
 532                                ")"
 533                );
 534                db.execSQL("PRAGMA cheogram.user_version = 13");
 535            }
 536
 537            db.setTransactionSuccessful();
 538        } finally {
 539            db.endTransaction();
 540        }
 541    }
 542
 543    @Override
 544    public void onConfigure(SQLiteDatabase db) {
 545        db.setForeignKeyConstraintsEnabled(true);
 546        db.rawQuery("PRAGMA secure_delete=ON", null).close();
 547        db.execSQL("ATTACH DATABASE ? AS cheogram", new Object[]{context.getDatabasePath("cheogram").getPath()});
 548        cheogramMigrate(db);
 549    }
 550
 551    @Override
 552    public void onCreate(SQLiteDatabase db) {
 553        db.execSQL(
 554                "create table "
 555                        + Account.TABLENAME
 556                        + "("
 557                        + Account.UUID
 558                        + " TEXT PRIMARY KEY,"
 559                        + Account.USERNAME
 560                        + " TEXT,"
 561                        + Account.SERVER
 562                        + " TEXT,"
 563                        + Account.PASSWORD
 564                        + " TEXT,"
 565                        + Account.DISPLAY_NAME
 566                        + " TEXT, "
 567                        + Account.STATUS
 568                        + " TEXT,"
 569                        + Account.STATUS_MESSAGE
 570                        + " TEXT,"
 571                        + Account.ROSTERVERSION
 572                        + " TEXT,"
 573                        + Account.OPTIONS
 574                        + " NUMBER, "
 575                        + Account.AVATAR
 576                        + " TEXT, "
 577                        + Account.KEYS
 578                        + " TEXT, "
 579                        + Account.HOSTNAME
 580                        + " TEXT, "
 581                        + Account.RESOURCE
 582                        + " TEXT,"
 583                        + Account.PINNED_MECHANISM
 584                        + " TEXT,"
 585                        + Account.PINNED_CHANNEL_BINDING
 586                        + " TEXT,"
 587                        + Account.FAST_MECHANISM
 588                        + " TEXT,"
 589                        + Account.FAST_TOKEN
 590                        + " TEXT,"
 591                        + Account.PORT
 592                        + " NUMBER DEFAULT 5222)");
 593        db.execSQL(
 594                "create table "
 595                        + Conversation.TABLENAME
 596                        + " ("
 597                        + Conversation.UUID
 598                        + " TEXT PRIMARY KEY, "
 599                        + Conversation.NAME
 600                        + " TEXT, "
 601                        + Conversation.CONTACT
 602                        + " TEXT, "
 603                        + Conversation.ACCOUNT
 604                        + " TEXT, "
 605                        + Conversation.CONTACTJID
 606                        + " TEXT, "
 607                        + Conversation.CREATED
 608                        + " NUMBER, "
 609                        + Conversation.STATUS
 610                        + " NUMBER, "
 611                        + Conversation.MODE
 612                        + " NUMBER, "
 613                        + Conversation.ATTRIBUTES
 614                        + " TEXT, FOREIGN KEY("
 615                        + Conversation.ACCOUNT
 616                        + ") REFERENCES "
 617                        + Account.TABLENAME
 618                        + "("
 619                        + Account.UUID
 620                        + ") ON DELETE CASCADE);");
 621        db.execSQL(
 622                "create table "
 623                        + Message.TABLENAME
 624                        + "( "
 625                        + Message.UUID
 626                        + " TEXT PRIMARY KEY, "
 627                        + Message.CONVERSATION
 628                        + " TEXT, "
 629                        + Message.TIME_SENT
 630                        + " NUMBER, "
 631                        + Message.COUNTERPART
 632                        + " TEXT, "
 633                        + Message.TRUE_COUNTERPART
 634                        + " TEXT,"
 635                        + Message.BODY
 636                        + " TEXT, "
 637                        + Message.ENCRYPTION
 638                        + " NUMBER, "
 639                        + Message.STATUS
 640                        + " NUMBER,"
 641                        + Message.TYPE
 642                        + " NUMBER, "
 643                        + Message.RELATIVE_FILE_PATH
 644                        + " TEXT, "
 645                        + Message.SERVER_MSG_ID
 646                        + " TEXT, "
 647                        + Message.FINGERPRINT
 648                        + " TEXT, "
 649                        + Message.CARBON
 650                        + " INTEGER, "
 651                        + Message.EDITED
 652                        + " TEXT, "
 653                        + Message.READ
 654                        + " NUMBER DEFAULT 1, "
 655                        + Message.OOB
 656                        + " INTEGER, "
 657                        + Message.ERROR_MESSAGE
 658                        + " TEXT,"
 659                        + Message.READ_BY_MARKERS
 660                        + " TEXT,"
 661                        + Message.MARKABLE
 662                        + " NUMBER DEFAULT 0,"
 663                        + Message.DELETED
 664                        + " NUMBER DEFAULT 0,"
 665                        + Message.BODY_LANGUAGE
 666                        + " TEXT,"
 667                        + Message.OCCUPANT_ID
 668                        + " TEXT,"
 669                        + Message.REACTIONS
 670                        + " TEXT,"
 671                        + Message.REMOTE_MSG_ID
 672                        + " TEXT, FOREIGN KEY("
 673                        + Message.CONVERSATION
 674                        + ") REFERENCES "
 675                        + Conversation.TABLENAME
 676                        + "("
 677                        + Conversation.UUID
 678                        + ") ON DELETE CASCADE);");
 679        db.execSQL(CREATE_MESSAGE_TIME_INDEX);
 680        db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
 681        db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
 682        db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
 683        db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
 684        db.execSQL(CREATE_CONTACTS_STATEMENT);
 685        db.execSQL(CREATE_SESSIONS_STATEMENT);
 686        db.execSQL(CREATE_PREKEYS_STATEMENT);
 687        db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
 688        db.execSQL(CREATE_IDENTITIES_STATEMENT);
 689        db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
 690        db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
 691        db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
 692        db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
 693        db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
 694        db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
 695        db.execSQL(CREATE_CAPS_CACHE_TABLE);
 696        db.execSQL(CREATE_CAPS_CACHE_INDEX_CAPS);
 697        db.execSQL(CREATE_CAPS_CACHE_INDEX_CAPS2);
 698    }
 699
 700    @Override
 701    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 702        if (oldVersion < 2 && newVersion >= 2) {
 703            db.execSQL(
 704                    "update "
 705                            + Account.TABLENAME
 706                            + " set "
 707                            + Account.OPTIONS
 708                            + " = "
 709                            + Account.OPTIONS
 710                            + " | 8");
 711        }
 712        if (oldVersion < 3 && newVersion >= 3) {
 713            db.execSQL(
 714                    "ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.TYPE + " NUMBER");
 715        }
 716        if (oldVersion < 5 && newVersion >= 5) {
 717            db.execSQL("DROP TABLE " + Contact.TABLENAME);
 718            db.execSQL(CREATE_CONTACTS_STATEMENT);
 719            db.execSQL("UPDATE " + Account.TABLENAME + " SET " + Account.ROSTERVERSION + " = NULL");
 720        }
 721        if (oldVersion < 6 && newVersion >= 6) {
 722            db.execSQL(
 723                    "ALTER TABLE "
 724                            + Message.TABLENAME
 725                            + " ADD COLUMN "
 726                            + Message.TRUE_COUNTERPART
 727                            + " TEXT");
 728        }
 729        if (oldVersion < 7 && newVersion >= 7) {
 730            db.execSQL(
 731                    "ALTER TABLE "
 732                            + Message.TABLENAME
 733                            + " ADD COLUMN "
 734                            + Message.REMOTE_MSG_ID
 735                            + " TEXT");
 736            db.execSQL(
 737                    "ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.AVATAR + " TEXT");
 738            db.execSQL(
 739                    "ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.AVATAR + " TEXT");
 740        }
 741        if (oldVersion < 8 && newVersion >= 8) {
 742            db.execSQL(
 743                    "ALTER TABLE "
 744                            + Conversation.TABLENAME
 745                            + " ADD COLUMN "
 746                            + Conversation.ATTRIBUTES
 747                            + " TEXT");
 748        }
 749        if (oldVersion < 9 && newVersion >= 9) {
 750            db.execSQL(
 751                    "ALTER TABLE "
 752                            + Contact.TABLENAME
 753                            + " ADD COLUMN "
 754                            + Contact.LAST_TIME
 755                            + " NUMBER");
 756            db.execSQL(
 757                    "ALTER TABLE "
 758                            + Contact.TABLENAME
 759                            + " ADD COLUMN "
 760                            + Contact.LAST_PRESENCE
 761                            + " TEXT");
 762        }
 763        if (oldVersion < 10 && newVersion >= 10) {
 764            db.execSQL(
 765                    "ALTER TABLE "
 766                            + Message.TABLENAME
 767                            + " ADD COLUMN "
 768                            + Message.RELATIVE_FILE_PATH
 769                            + " TEXT");
 770        }
 771        if (oldVersion < 11 && newVersion >= 11) {
 772            db.execSQL(
 773                    "ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.GROUPS + " TEXT");
 774            db.execSQL("delete from " + Contact.TABLENAME);
 775            db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
 776        }
 777        if (oldVersion < 12 && newVersion >= 12) {
 778            db.execSQL(
 779                    "ALTER TABLE "
 780                            + Message.TABLENAME
 781                            + " ADD COLUMN "
 782                            + Message.SERVER_MSG_ID
 783                            + " TEXT");
 784        }
 785        if (oldVersion < 13 && newVersion >= 13) {
 786            db.execSQL("delete from " + Contact.TABLENAME);
 787            db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
 788        }
 789        if (oldVersion < 14 && newVersion >= 14) {
 790            canonicalizeJids(db);
 791        }
 792        if (oldVersion < 15 && newVersion >= 15) {
 793            recreateAxolotlDb(db);
 794            db.execSQL(
 795                    "ALTER TABLE "
 796                            + Message.TABLENAME
 797                            + " ADD COLUMN "
 798                            + Message.FINGERPRINT
 799                            + " TEXT");
 800        }
 801        if (oldVersion < 16 && newVersion >= 16) {
 802            db.execSQL(
 803                    "ALTER TABLE "
 804                            + Message.TABLENAME
 805                            + " ADD COLUMN "
 806                            + Message.CARBON
 807                            + " INTEGER");
 808        }
 809        if (oldVersion < 19 && newVersion >= 19) {
 810            db.execSQL(
 811                    "ALTER TABLE "
 812                            + Account.TABLENAME
 813                            + " ADD COLUMN "
 814                            + Account.DISPLAY_NAME
 815                            + " TEXT");
 816        }
 817        if (oldVersion < 20 && newVersion >= 20) {
 818            db.execSQL(
 819                    "ALTER TABLE "
 820                            + Account.TABLENAME
 821                            + " ADD COLUMN "
 822                            + Account.HOSTNAME
 823                            + " TEXT");
 824            db.execSQL(
 825                    "ALTER TABLE "
 826                            + Account.TABLENAME
 827                            + " ADD COLUMN "
 828                            + Account.PORT
 829                            + " NUMBER DEFAULT 5222");
 830        }
 831        if (oldVersion < 26 && newVersion >= 26) {
 832            db.execSQL(
 833                    "ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS + " TEXT");
 834            db.execSQL(
 835                    "ALTER TABLE "
 836                            + Account.TABLENAME
 837                            + " ADD COLUMN "
 838                            + Account.STATUS_MESSAGE
 839                            + " TEXT");
 840        }
 841        if (oldVersion < 40 && newVersion >= 40) {
 842            db.execSQL(
 843                    "ALTER TABLE "
 844                            + Account.TABLENAME
 845                            + " ADD COLUMN "
 846                            + Account.RESOURCE
 847                            + " TEXT");
 848        }
 849        /* Any migrations that alter the Account table need to happen BEFORE this migration, as it
 850         * depends on account de-serialization.
 851         */
 852        if (oldVersion < 17 && newVersion >= 17 && newVersion < 31) {
 853            List<Account> accounts = getAccounts(db);
 854            for (Account account : accounts) {
 855                String ownDeviceIdString =
 856                        account.getKey(SQLiteAxolotlStore.JSONKEY_REGISTRATION_ID);
 857                if (ownDeviceIdString == null) {
 858                    continue;
 859                }
 860                int ownDeviceId = Integer.valueOf(ownDeviceIdString);
 861                SignalProtocolAddress ownAddress =
 862                        new SignalProtocolAddress(
 863                                account.getJid().asBareJid().toString(), ownDeviceId);
 864                deleteSession(db, account, ownAddress);
 865                IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
 866                if (identityKeyPair != null) {
 867                    String[] selectionArgs = {
 868                        account.getUuid(),
 869                        CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize())
 870                    };
 871                    ContentValues values = new ContentValues();
 872                    values.put(SQLiteAxolotlStore.TRUSTED, 2);
 873                    db.update(
 874                            SQLiteAxolotlStore.IDENTITIES_TABLENAME,
 875                            values,
 876                            SQLiteAxolotlStore.ACCOUNT
 877                                    + " = ? AND "
 878                                    + SQLiteAxolotlStore.FINGERPRINT
 879                                    + " = ? ",
 880                            selectionArgs);
 881                } else {
 882                    Log.d(
 883                            Config.LOGTAG,
 884                            account.getJid().asBareJid()
 885                                    + ": could not load own identity key pair");
 886                }
 887            }
 888        }
 889        if (oldVersion < 18 && newVersion >= 18) {
 890            db.execSQL(
 891                    "ALTER TABLE "
 892                            + Message.TABLENAME
 893                            + " ADD COLUMN "
 894                            + Message.READ
 895                            + " NUMBER DEFAULT 1");
 896        }
 897
 898        if (oldVersion < 21 && newVersion >= 21) {
 899            List<Account> accounts = getAccounts(db);
 900            for (Account account : accounts) {
 901                account.unsetPgpSignature();
 902                db.update(
 903                        Account.TABLENAME,
 904                        account.getContentValues(),
 905                        Account.UUID + "=?",
 906                        new String[] {account.getUuid()});
 907            }
 908        }
 909
 910        if (oldVersion >= 15 && oldVersion < 22 && newVersion >= 22) {
 911            db.execSQL(
 912                    "ALTER TABLE "
 913                            + SQLiteAxolotlStore.IDENTITIES_TABLENAME
 914                            + " ADD COLUMN "
 915                            + SQLiteAxolotlStore.CERTIFICATE);
 916        }
 917
 918        if (oldVersion < 24 && newVersion >= 24) {
 919            db.execSQL(
 920                    "ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.EDITED + " TEXT");
 921        }
 922
 923        if (oldVersion < 25 && newVersion >= 25) {
 924            db.execSQL(
 925                    "ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.OOB + " INTEGER");
 926        }
 927
 928        if (oldVersion < 26 && newVersion >= 26) {
 929            db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
 930        }
 931
 932        if (oldVersion < 28 && newVersion >= 28) {
 933            canonicalizeJids(db);
 934        }
 935
 936        if (oldVersion < 29 && newVersion >= 29) {
 937            db.execSQL(
 938                    "ALTER TABLE "
 939                            + Message.TABLENAME
 940                            + " ADD COLUMN "
 941                            + Message.ERROR_MESSAGE
 942                            + " TEXT");
 943        }
 944        if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
 945            db.execSQL(
 946                    "ALTER TABLE "
 947                            + SQLiteAxolotlStore.IDENTITIES_TABLENAME
 948                            + " ADD COLUMN "
 949                            + SQLiteAxolotlStore.TRUST
 950                            + " TEXT");
 951            db.execSQL(
 952                    "ALTER TABLE "
 953                            + SQLiteAxolotlStore.IDENTITIES_TABLENAME
 954                            + " ADD COLUMN "
 955                            + SQLiteAxolotlStore.ACTIVE
 956                            + " NUMBER");
 957            HashMap<Integer, ContentValues> migration = new HashMap<>();
 958            migration.put(
 959                    0, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
 960            migration.put(
 961                    1, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
 962            migration.put(
 963                    2,
 964                    createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, true));
 965            migration.put(
 966                    3,
 967                    createFingerprintStatusContentValues(
 968                            FingerprintStatus.Trust.COMPROMISED, false));
 969            migration.put(
 970                    4,
 971                    createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
 972            migration.put(
 973                    5,
 974                    createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
 975            migration.put(
 976                    6,
 977                    createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, false));
 978            migration.put(
 979                    7,
 980                    createFingerprintStatusContentValues(
 981                            FingerprintStatus.Trust.VERIFIED_X509, true));
 982            migration.put(
 983                    8,
 984                    createFingerprintStatusContentValues(
 985                            FingerprintStatus.Trust.VERIFIED_X509, false));
 986            for (Map.Entry<Integer, ContentValues> entry : migration.entrySet()) {
 987                String whereClause = SQLiteAxolotlStore.TRUSTED + "=?";
 988                String[] where = {String.valueOf(entry.getKey())};
 989                db.update(
 990                        SQLiteAxolotlStore.IDENTITIES_TABLENAME,
 991                        entry.getValue(),
 992                        whereClause,
 993                        where);
 994            }
 995        }
 996        if (oldVersion >= 15 && oldVersion < 32 && newVersion >= 32) {
 997            db.execSQL(
 998                    "ALTER TABLE "
 999                            + SQLiteAxolotlStore.IDENTITIES_TABLENAME
1000                            + " ADD COLUMN "
1001                            + SQLiteAxolotlStore.LAST_ACTIVATION
1002                            + " NUMBER");
1003            ContentValues defaults = new ContentValues();
1004            defaults.put(SQLiteAxolotlStore.LAST_ACTIVATION, System.currentTimeMillis());
1005            db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, defaults, null, null);
1006        }
1007        if (oldVersion >= 15 && oldVersion < 33 && newVersion >= 33) {
1008            String whereClause = SQLiteAxolotlStore.OWN + "=1";
1009            db.update(
1010                    SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1011                    createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED, true),
1012                    whereClause,
1013                    null);
1014        }
1015
1016        if (oldVersion < 34 && newVersion >= 34) {
1017            db.execSQL(CREATE_MESSAGE_TIME_INDEX);
1018
1019            final File oldPicturesDirectory =
1020                    new File(
1021                            Environment.getExternalStoragePublicDirectory(
1022                                            Environment.DIRECTORY_PICTURES)
1023                                    + "/Conversations/");
1024            final File oldFilesDirectory =
1025                    new File(Environment.getExternalStorageDirectory() + "/Conversations/");
1026            final File newFilesDirectory =
1027                    new File(
1028                            Environment.getExternalStorageDirectory()
1029                                    + "/Conversations/Media/Conversations Files/");
1030            final File newVideosDirectory =
1031                    new File(
1032                            Environment.getExternalStorageDirectory()
1033                                    + "/Conversations/Media/Conversations Videos/");
1034            if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
1035                final File newPicturesDirectory =
1036                        new File(
1037                                Environment.getExternalStorageDirectory()
1038                                        + "/Conversations/Media/Conversations Images/");
1039                newPicturesDirectory.getParentFile().mkdirs();
1040                if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
1041                    Log.d(
1042                            Config.LOGTAG,
1043                            "moved "
1044                                    + oldPicturesDirectory.getAbsolutePath()
1045                                    + " to "
1046                                    + newPicturesDirectory.getAbsolutePath());
1047                }
1048            }
1049            if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
1050                newFilesDirectory.mkdirs();
1051                newVideosDirectory.mkdirs();
1052                final File[] files = oldFilesDirectory.listFiles();
1053                if (files == null) {
1054                    return;
1055                }
1056                for (File file : files) {
1057                    if (file.getName().equals(".nomedia")) {
1058                        if (file.delete()) {
1059                            Log.d(
1060                                    Config.LOGTAG,
1061                                    "deleted nomedia file in "
1062                                            + oldFilesDirectory.getAbsolutePath());
1063                        }
1064                    } else if (file.isFile()) {
1065                        final String name = file.getName();
1066                        boolean isVideo = false;
1067                        int start = name.lastIndexOf('.') + 1;
1068                        if (start < name.length()) {
1069                            String mime =
1070                                    MimeUtils.guessMimeTypeFromExtension(name.substring(start));
1071                            isVideo = mime != null && mime.startsWith("video/");
1072                        }
1073                        File dst =
1074                                new File(
1075                                        (isVideo ? newVideosDirectory : newFilesDirectory)
1076                                                        .getAbsolutePath()
1077                                                + "/"
1078                                                + file.getName());
1079                        if (file.renameTo(dst)) {
1080                            Log.d(Config.LOGTAG, "moved " + file + " to " + dst);
1081                        }
1082                    }
1083                }
1084            }
1085        }
1086        if (oldVersion < 35 && newVersion >= 35) {
1087            db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
1088        }
1089        if (oldVersion < 36 && newVersion >= 36) {
1090            List<Account> accounts = getAccounts(db);
1091            for (Account account : accounts) {
1092                account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true);
1093                account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, false);
1094                db.update(
1095                        Account.TABLENAME,
1096                        account.getContentValues(),
1097                        Account.UUID + "=?",
1098                        new String[] {account.getUuid()});
1099            }
1100        }
1101
1102        if (oldVersion < 37 && newVersion >= 37) {
1103            db.execSQL(
1104                    "ALTER TABLE "
1105                            + Message.TABLENAME
1106                            + " ADD COLUMN "
1107                            + Message.READ_BY_MARKERS
1108                            + " TEXT");
1109        }
1110
1111        if (oldVersion < 38 && newVersion >= 38) {
1112            db.execSQL(
1113                    "ALTER TABLE "
1114                            + Message.TABLENAME
1115                            + " ADD COLUMN "
1116                            + Message.MARKABLE
1117                            + " NUMBER DEFAULT 0");
1118        }
1119
1120        if (oldVersion < 39 && newVersion >= 39) {
1121            db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
1122        }
1123
1124        if (QuickConversationsService.isQuicksy() && oldVersion < 43 && newVersion >= 43) {
1125            List<Account> accounts = getAccounts(db);
1126            for (Account account : accounts) {
1127                account.setOption(Account.OPTION_MAGIC_CREATE, true);
1128                db.update(
1129                        Account.TABLENAME,
1130                        account.getContentValues(),
1131                        Account.UUID + "=?",
1132                        new String[] {account.getUuid()});
1133            }
1134        }
1135
1136        if (oldVersion < 44 && newVersion >= 44) {
1137            db.execSQL(
1138                    "ALTER TABLE "
1139                            + Message.TABLENAME
1140                            + " ADD COLUMN "
1141                            + Message.DELETED
1142                            + " NUMBER DEFAULT 0");
1143            db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
1144            db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
1145            db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
1146        }
1147
1148        if (oldVersion < 45 && newVersion >= 45) {
1149            db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE);
1150        }
1151
1152        if (oldVersion < 46 && newVersion >= 46) {
1153            final long start = SystemClock.elapsedRealtime();
1154            db.rawQuery("PRAGMA secure_delete = FALSE", null).close();
1155            db.execSQL("update " + Message.TABLENAME + " set " + Message.EDITED + "=NULL");
1156            db.rawQuery("PRAGMA secure_delete=ON", null).close();
1157            final long diff = SystemClock.elapsedRealtime() - start;
1158            Log.d(Config.LOGTAG, "deleted old edit information in " + diff + "ms");
1159        }
1160        if (oldVersion < 47 && newVersion >= 47) {
1161            db.execSQL(
1162                    "ALTER TABLE "
1163                            + Contact.TABLENAME
1164                            + " ADD COLUMN "
1165                            + Contact.PRESENCE_NAME
1166                            + " TEXT");
1167        }
1168        if (oldVersion < 48 && newVersion >= 48) {
1169            db.execSQL(
1170                    "ALTER TABLE "
1171                            + Contact.TABLENAME
1172                            + " ADD COLUMN "
1173                            + Contact.RTP_CAPABILITY
1174                            + " TEXT");
1175        }
1176        if (oldVersion < 49 && newVersion >= 49) {
1177            db.beginTransaction();
1178            db.execSQL("DROP TRIGGER IF EXISTS after_message_insert;");
1179            db.execSQL("DROP TRIGGER IF EXISTS after_message_update;");
1180            db.execSQL("DROP TRIGGER IF EXISTS after_message_delete;");
1181            db.execSQL("DROP TABLE IF EXISTS messages_index;");
1182            // a hack that should not be necessary, but
1183            // there was at least one occurence when SQLite failed at this
1184            db.execSQL("DROP TABLE IF EXISTS messages_index_docsize;");
1185            db.execSQL("DROP TABLE IF EXISTS messages_index_segdir;");
1186            db.execSQL("DROP TABLE IF EXISTS messages_index_segments;");
1187            db.execSQL("DROP TABLE IF EXISTS messages_index_stat;");
1188            db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
1189            db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
1190            db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
1191            db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
1192            db.setTransactionSuccessful();
1193            db.endTransaction();
1194            requiresMessageIndexRebuild = true;
1195        }
1196        if (oldVersion < 50 && newVersion >= 50) {
1197            db.execSQL(
1198                    "ALTER TABLE "
1199                            + Account.TABLENAME
1200                            + " ADD COLUMN "
1201                            + Account.PINNED_MECHANISM
1202                            + " TEXT");
1203            db.execSQL(
1204                    "ALTER TABLE "
1205                            + Account.TABLENAME
1206                            + " ADD COLUMN "
1207                            + Account.PINNED_CHANNEL_BINDING
1208                            + " TEXT");
1209        }
1210        if (oldVersion < 51 && newVersion >= 51) {
1211            db.execSQL(
1212                    "ALTER TABLE "
1213                            + Account.TABLENAME
1214                            + " ADD COLUMN "
1215                            + Account.FAST_MECHANISM
1216                            + " TEXT");
1217            db.execSQL(
1218                    "ALTER TABLE "
1219                            + Account.TABLENAME
1220                            + " ADD COLUMN "
1221                            + Account.FAST_TOKEN
1222                            + " TEXT");
1223        }
1224        if (oldVersion < 52 && newVersion >= 52) {
1225            db.execSQL(
1226                    "ALTER TABLE "
1227                            + Message.TABLENAME
1228                            + " ADD COLUMN "
1229                            + Message.OCCUPANT_ID
1230                            + " TEXT");
1231            db.execSQL(
1232                    "ALTER TABLE "
1233                            + Message.TABLENAME
1234                            + " ADD COLUMN "
1235                            + Message.REACTIONS
1236                            + " TEXT");
1237        }
1238        if (oldVersion < 53 && newVersion >= 53) {
1239            try (final Cursor cursor =
1240                    db.query(
1241                            Account.TABLENAME,
1242                            new String[] {Account.UUID, Account.USERNAME},
1243                            null,
1244                            null,
1245                            null,
1246                            null,
1247                            null)) {
1248                while (cursor != null && cursor.moveToNext()) {
1249                    final var uuid = cursor.getString(0);
1250                    final var username = cursor.getString(1);
1251                    final Localpart localpart;
1252                    try {
1253                        localpart = Localpart.fromUnescaped(username);
1254                    } catch (final XmppStringprepException e) {
1255                        Log.d(Config.LOGTAG, "unable to parse jid");
1256                        continue;
1257                    }
1258                    final var contentValues = new ContentValues();
1259                    contentValues.putNull(Account.ROSTERVERSION);
1260                    contentValues.put(Account.USERNAME, localpart.toString());
1261                    db.update(
1262                            Account.TABLENAME,
1263                            contentValues,
1264                            Account.UUID + "=?",
1265                            new String[] {uuid});
1266                }
1267            }
1268        }
1269        if (oldVersion < 54 && newVersion >= 54) {
1270            db.execSQL("DROP TABLE discovery_results");
1271            db.execSQL(CREATE_CAPS_CACHE_TABLE);
1272            db.execSQL(CREATE_CAPS_CACHE_INDEX_CAPS);
1273            db.execSQL(CREATE_CAPS_CACHE_INDEX_CAPS2);
1274        }
1275    }
1276
1277    private void canonicalizeJids(SQLiteDatabase db) {
1278        // migrate db to new, canonicalized JID domainpart representation
1279
1280        // Conversation table
1281        Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]);
1282        while (cursor.moveToNext()) {
1283            String newJid;
1284            try {
1285                newJid =
1286                        Jid.of(
1287                                        cursor.getString(
1288                                                cursor.getColumnIndexOrThrow(
1289                                                        Conversation.CONTACTJID)))
1290                                .toString();
1291            } catch (final IllegalArgumentException e) {
1292                Log.e(
1293                        Config.LOGTAG,
1294                        "Failed to migrate Conversation CONTACTJID "
1295                                + cursor.getString(
1296                                        cursor.getColumnIndexOrThrow(Conversation.CONTACTJID))
1297                                + ". Skipping...",
1298                        e);
1299                continue;
1300            }
1301
1302            final String[] updateArgs = {
1303                newJid, cursor.getString(cursor.getColumnIndexOrThrow(Conversation.UUID)),
1304            };
1305            db.execSQL(
1306                    "update "
1307                            + Conversation.TABLENAME
1308                            + " set "
1309                            + Conversation.CONTACTJID
1310                            + " = ? "
1311                            + " where "
1312                            + Conversation.UUID
1313                            + " = ?",
1314                    updateArgs);
1315        }
1316        cursor.close();
1317
1318        // Contact table
1319        cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]);
1320        while (cursor.moveToNext()) {
1321            String newJid;
1322            try {
1323                newJid =
1324                        Jid.of(cursor.getString(cursor.getColumnIndexOrThrow(Contact.JID)))
1325                                .toString();
1326            } catch (final IllegalArgumentException e) {
1327                Log.e(
1328                        Config.LOGTAG,
1329                        "Failed to migrate Contact JID "
1330                                + cursor.getString(cursor.getColumnIndexOrThrow(Contact.JID))
1331                                + ":  Skipping...",
1332                        e);
1333                continue;
1334            }
1335
1336            final String[] updateArgs = {
1337                newJid,
1338                cursor.getString(cursor.getColumnIndexOrThrow(Contact.ACCOUNT)),
1339                cursor.getString(cursor.getColumnIndexOrThrow(Contact.JID)),
1340            };
1341            db.execSQL(
1342                    "update "
1343                            + Contact.TABLENAME
1344                            + " set "
1345                            + Contact.JID
1346                            + " = ? "
1347                            + " where "
1348                            + Contact.ACCOUNT
1349                            + " = ? "
1350                            + " AND "
1351                            + Contact.JID
1352                            + " = ?",
1353                    updateArgs);
1354        }
1355        cursor.close();
1356
1357        // Account table
1358        cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]);
1359        while (cursor.moveToNext()) {
1360            String newServer;
1361            try {
1362                newServer =
1363                        Jid.of(
1364                                        cursor.getString(
1365                                                cursor.getColumnIndexOrThrow(Account.USERNAME)),
1366                                        cursor.getString(
1367                                                cursor.getColumnIndexOrThrow(Account.SERVER)),
1368                                        null)
1369                                .getDomain()
1370                                .toString();
1371            } catch (final IllegalArgumentException e) {
1372                Log.e(
1373                        Config.LOGTAG,
1374                        "Failed to migrate Account SERVER "
1375                                + cursor.getString(cursor.getColumnIndexOrThrow(Account.SERVER))
1376                                + ". Skipping...",
1377                        e);
1378                continue;
1379            }
1380
1381            String[] updateArgs = {
1382                newServer, cursor.getString(cursor.getColumnIndexOrThrow(Account.UUID)),
1383            };
1384            db.execSQL(
1385                    "update "
1386                            + Account.TABLENAME
1387                            + " set "
1388                            + Account.SERVER
1389                            + " = ? "
1390                            + " where "
1391                            + Account.UUID
1392                            + " = ?",
1393                    updateArgs);
1394        }
1395        cursor.close();
1396    }
1397
1398    public DownloadableFile getFileForCid(Cid cid) {
1399        if (cid == null) return null;
1400
1401        SQLiteDatabase db = this.getReadableDatabase();
1402        Cursor cursor = db.query("cheogram.cids", new String[]{"path"}, "cid=?", new String[]{cid.toString()}, null, null, null);
1403        DownloadableFile f = null;
1404        if (cursor.moveToNext()) {
1405            f = new DownloadableFile(cursor.getString(0));
1406        }
1407        cursor.close();
1408        return f;
1409    }
1410
1411    public String getUrlForCid(Cid cid) {
1412        SQLiteDatabase db = this.getReadableDatabase();
1413        Cursor cursor = db.query("cheogram.cids", new String[]{"url"}, "cid=?", new String[]{cid.toString()}, null, null, null);
1414        String url = null;
1415        if (cursor.moveToNext()) {
1416            url = cursor.getString(0);
1417        }
1418        cursor.close();
1419        return url;
1420    }
1421
1422    public void saveCid(Cid cid, File file) {
1423        saveCid(cid, file, null);
1424    }
1425
1426    public void saveCid(Cid cid, File file, String url) {
1427        SQLiteDatabase db = this.getWritableDatabase();
1428        ContentValues cv = new ContentValues();
1429        cv.put("cid", cid.toString());
1430        if (file != null) cv.put("path", file.getAbsolutePath());
1431        if (url != null) cv.put("url", url);
1432        if (db.update("cheogram.cids", cv, "cid=?", new String[]{cid.toString()}) < 1) {
1433            db.insertWithOnConflict("cheogram.cids", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
1434        }
1435    }
1436
1437    public void blockMedia(Cid cid) {
1438        SQLiteDatabase db = this.getWritableDatabase();
1439        ContentValues cv = new ContentValues();
1440        cv.put("cid", cid.toString());
1441        db.insertWithOnConflict("cheogram.blocked_media", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
1442    }
1443
1444    public boolean isBlockedMedia(Cid cid) {
1445        SQLiteDatabase db = this.getReadableDatabase();
1446        Cursor cursor = db.query("cheogram.blocked_media", new String[]{"count(*)"}, "cid=?", new String[]{cid.toString()}, null, null, null);
1447        boolean is = false;
1448        if (cursor.moveToNext()) {
1449            is = cursor.getInt(0) > 0;
1450        }
1451        cursor.close();
1452        return is;
1453    }
1454
1455    public void clearBlockedMedia() {
1456        SQLiteDatabase db = this.getWritableDatabase();
1457        db.execSQL("DELETE FROM cheogram.blocked_media");
1458    }
1459
1460    public Multimap<String, String> loadMutedMucUsers() {
1461        Multimap<String, String> result = HashMultimap.create();
1462        SQLiteDatabase db = this.getReadableDatabase();
1463        Cursor cursor = db.query("cheogram.muted_participants", new String[]{"muc_jid", "occupant_id"}, null, null, null, null, null);
1464        while (cursor.moveToNext()) {
1465            result.put(cursor.getString(0), cursor.getString(1));
1466        }
1467        cursor.close();
1468        return result;
1469    }
1470
1471    public boolean muteMucUser(MucOptions.User user) {
1472        if (user.getMuc() == null || user.getOccupantId() == null) return false;
1473
1474        SQLiteDatabase db = this.getWritableDatabase();
1475        ContentValues cv = new ContentValues();
1476        cv.put("muc_jid", user.getMuc().toString());
1477        cv.put("occupant_id", user.getOccupantId());
1478        db.insertWithOnConflict("cheogram.muted_participants", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
1479
1480        return true;
1481    }
1482
1483    public boolean unmuteMucUser(MucOptions.User user) {
1484        if (user.getMuc() == null || user.getOccupantId() == null) return false;
1485
1486        SQLiteDatabase db = this.getWritableDatabase();
1487        String where = "muc_jid=? AND occupant_id=?";
1488        String[] whereArgs = {user.getMuc().toString(), user.getOccupantId()};
1489        db.delete("cheogram.muted_participants", where, whereArgs);
1490
1491        return true;
1492    }
1493
1494    public void insertWebxdcUpdate(final WebxdcUpdate update) {
1495        SQLiteDatabase db = this.getWritableDatabase();
1496        db.insertWithOnConflict("cheogram.webxdc_updates", null, update.getContentValues(), SQLiteDatabase.CONFLICT_IGNORE);
1497    }
1498
1499    private int deleteOversizedWebxdcUpdates(SQLiteDatabase db, String conversationUuid, String thread) {
1500        int deleted = db.delete("cheogram.webxdc_updates",
1501                Message.CONVERSATION + "=? AND thread=? AND length(payload) > 131072",
1502                new String[]{conversationUuid, thread});
1503        if (deleted > 0) {
1504            Log.w(Config.LOGTAG, "Deleted " + deleted + " oversized webxdc update(s) for thread " + thread);
1505        }
1506        return deleted;
1507    }
1508
1509    public WebxdcUpdate findLastWebxdcUpdate(Message message) {
1510        if (message.getThread() == null) {
1511            Log.w(Config.LOGTAG, "WebXDC message with no thread!");
1512            return null;
1513        }
1514
1515        SQLiteDatabase db = this.getReadableDatabase();
1516        String[] selectionArgs = {message.getConversation().getUuid(), message.getThread().getContent()};
1517        Cursor cursor = db.query("cheogram.webxdc_updates", null,
1518                Message.CONVERSATION + "=? AND thread=?",
1519                selectionArgs, null, null, "serial ASC");
1520        try {
1521            WebxdcUpdate update = null;
1522            if (cursor.moveToLast()) {
1523                update = new WebxdcUpdate(cursor, cursor.getLong(cursor.getColumnIndex("serial")));
1524            }
1525            return update;
1526        } catch (final android.database.sqlite.SQLiteBlobTooBigException e) {
1527            Log.w(Config.LOGTAG, "findLastWebxdcUpdate: blob too big, deleting oversized rows", e);
1528            deleteOversizedWebxdcUpdates(db, message.getConversation().getUuid(), message.getThread().getContent());
1529            return findLastWebxdcUpdate(message);
1530        } finally {
1531            cursor.close();
1532        }
1533    }
1534
1535    public List<WebxdcUpdate> findWebxdcUpdates(Message message, long serial) {
1536        SQLiteDatabase db = this.getReadableDatabase();
1537        String[] selectionArgs = {message.getConversation().getUuid(), message.getThread().getContent(), String.valueOf(serial)};
1538        Cursor cursor = db.query("cheogram.webxdc_updates", null,
1539                Message.CONVERSATION + "=? AND thread=? AND serial>?",
1540                selectionArgs, null, null, "serial ASC");
1541        try {
1542            long maxSerial = 0;
1543            if (cursor.moveToLast()) {
1544                maxSerial = cursor.getLong(cursor.getColumnIndex("serial"));
1545            }
1546            cursor.moveToFirst();
1547            cursor.moveToPrevious();
1548
1549            List<WebxdcUpdate> updates = new ArrayList<>();
1550            while (cursor.moveToNext()) {
1551                updates.add(new WebxdcUpdate(cursor, maxSerial));
1552            }
1553            return updates;
1554        } catch (final android.database.sqlite.SQLiteBlobTooBigException e) {
1555            Log.w(Config.LOGTAG, "findWebxdcUpdates: blob too big, deleting oversized rows", e);
1556            deleteOversizedWebxdcUpdates(db, message.getConversation().getUuid(), message.getThread().getContent());
1557            return findWebxdcUpdates(message, serial);
1558        } finally {
1559            cursor.close();
1560        }
1561    }
1562
1563    public void createConversation(Conversation conversation) {
1564        SQLiteDatabase db = this.getWritableDatabase();
1565        db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
1566    }
1567
1568    public void createMessage(Message message) {
1569        SQLiteDatabase db = this.getWritableDatabase();
1570        db.insert(Message.TABLENAME, null, message.getContentValues());
1571        db.insert("cheogram." + Message.TABLENAME, null, message.getCheogramContentValues());
1572    }
1573
1574    public void createAccount(Account account) {
1575        SQLiteDatabase db = this.getWritableDatabase();
1576        db.insert(Account.TABLENAME, null, account.getContentValues());
1577    }
1578
1579    public void saveResolverResult(String domain, Resolver.Result result) {
1580        SQLiteDatabase db = this.getWritableDatabase();
1581        ContentValues contentValues = result.toContentValues();
1582        contentValues.put(Resolver.Result.DOMAIN, domain);
1583        db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
1584    }
1585
1586    public synchronized Resolver.Result findResolverResult(String domain) {
1587        SQLiteDatabase db = this.getReadableDatabase();
1588        String where = Resolver.Result.DOMAIN + "=?";
1589        String[] whereArgs = {domain};
1590        final Cursor cursor =
1591                db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
1592        Resolver.Result result = null;
1593        if (cursor != null) {
1594            try {
1595                if (cursor.moveToFirst()) {
1596                    result = Resolver.Result.fromCursor(cursor);
1597                }
1598            } catch (Exception e) {
1599                Log.d(
1600                        Config.LOGTAG,
1601                        "unable to find cached resolver result in database " + e.getMessage());
1602                return null;
1603            } finally {
1604                cursor.close();
1605            }
1606        }
1607        return result;
1608    }
1609
1610    public void insertPresenceTemplate(PresenceTemplate template) {
1611        SQLiteDatabase db = this.getWritableDatabase();
1612        String whereToDelete = PresenceTemplate.MESSAGE + "=?";
1613        String[] whereToDeleteArgs = {template.getStatusMessage()};
1614        db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs);
1615        db.delete(
1616                PresenceTemplate.TABELNAME,
1617                PresenceTemplate.UUID
1618                        + " not in (select "
1619                        + PresenceTemplate.UUID
1620                        + " from "
1621                        + PresenceTemplate.TABELNAME
1622                        + " order by "
1623                        + PresenceTemplate.LAST_USED
1624                        + " desc limit 9)",
1625                null);
1626        db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
1627    }
1628
1629    public List<PresenceTemplate> getPresenceTemplates() {
1630        ArrayList<PresenceTemplate> templates = new ArrayList<>();
1631        SQLiteDatabase db = this.getReadableDatabase();
1632        Cursor cursor =
1633                db.query(
1634                        PresenceTemplate.TABELNAME,
1635                        null,
1636                        null,
1637                        null,
1638                        null,
1639                        null,
1640                        PresenceTemplate.LAST_USED + " desc");
1641        while (cursor.moveToNext()) {
1642            templates.add(PresenceTemplate.fromCursor(cursor));
1643        }
1644        cursor.close();
1645        return templates;
1646    }
1647
1648    public HashMap<MucOptions.User.OccupantId, MucOptions.User.CacheEntry>
1649    getMucUsersForConversation(Conversation conversation)
1650    {
1651        HashMap<MucOptions.User.OccupantId, MucOptions.User.CacheEntry> cache = new HashMap<>();
1652        Cursor cursor = null;
1653        try {
1654            SQLiteDatabase db = this.getReadableDatabase();
1655            String[] selectionArgs = {conversation.getUuid()};
1656            cursor = db.rawQuery(
1657                    "select * from "
1658                            + MucOptions.User.CacheEntry.TABLENAME
1659                            + " where "
1660                            + MucOptions.User.CacheEntry.CONVERSATION_UUID
1661                            + "= ?",
1662                        selectionArgs
1663            );
1664            while (cursor.moveToNext()) {
1665                final var avatar = cursor.getString(cursor.getColumnIndexOrThrow(MucOptions.User.CacheEntry.AVATAR));
1666                final var nick = cursor.getString(cursor.getColumnIndexOrThrow(MucOptions.User.CacheEntry.NICK));
1667                final var occupantId = cursor.getString(cursor.getColumnIndexOrThrow(MucOptions.User.CacheEntry.OCCUPANT_ID));
1668                cache.put(
1669                    new MucOptions.User.OccupantId(occupantId),
1670                    new MucOptions.User.CacheEntry(avatar, nick)
1671                );
1672            }
1673        } finally {
1674            if (cursor != null) cursor.close();
1675        }
1676
1677        return cache;
1678    }
1679
1680    public CopyOnWriteArrayList<Conversation> getConversations(int status) {
1681        CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>();
1682        SQLiteDatabase db = this.getReadableDatabase();
1683        String[] selectionArgs = {Integer.toString(status)};
1684        Cursor cursor =
1685                db.rawQuery(
1686                        "select " + String.join(", ", Conversation.ALL_COLUMNS) + " from "
1687                                + Conversation.TABLENAME
1688                                + " where "
1689                                + Conversation.STATUS
1690                                + " = ? and "
1691                                + Conversation.CONTACTJID
1692                                + " is not null order by "
1693                                + Conversation.CREATED
1694                                + " desc",
1695                        selectionArgs);
1696        while (cursor.moveToNext()) {
1697            final Conversation conversation = Conversation.fromCursor(cursor);
1698            if (conversation.getJid() instanceof Jid.Invalid) {
1699                continue;
1700            }
1701            conversation.putAllInMucOccupantCache(getMucUsersForConversation(conversation));
1702            list.add(conversation);
1703        }
1704        cursor.close();
1705        return list;
1706    }
1707
1708    public Message getMessage(Conversation conversation, String uuid) {
1709        ArrayList<Message> list = new ArrayList<>();
1710        SQLiteDatabase db = this.getReadableDatabase();
1711        Cursor cursor;
1712        cursor = db.rawQuery(
1713            "SELECT * FROM " + Message.TABLENAME + " " +
1714            "LEFT JOIN cheogram." + Message.TABLENAME +
1715            "  USING (" + Message.UUID + ")" +
1716            "WHERE " + Message.UUID + "=?",
1717            new String[]{uuid}
1718        );
1719        while (cursor.moveToNext()) {
1720            try {
1721                return Message.fromCursor(cursor, conversation);
1722            } catch (Exception e) {
1723                Log.e(Config.LOGTAG, "unable to restore message");
1724            }
1725        }
1726        cursor.close();
1727        return null;
1728    }
1729
1730    public ArrayList<Message> getMessages(Conversation conversations, int limit) {
1731        return getMessages(conversations, limit, -1);
1732    }
1733
1734    public Map<String, Message> getMessageFuzzyIds(Conversation conversation, Collection<String> ids) {
1735        final var result = new Hashtable<String, Message>();
1736        if (ids.size() < 1) return result;
1737        final ArrayList<String> params = new ArrayList<>();
1738        final ArrayList<String> template = new ArrayList<>();
1739        for (final var id : ids) {
1740            template.add("?");
1741        }
1742        params.addAll(ids);
1743        params.addAll(ids);
1744        params.addAll(ids);
1745        ArrayList<Message> list = new ArrayList<>();
1746        SQLiteDatabase db = this.getReadableDatabase();
1747        Cursor cursor;
1748        cursor = db.rawQuery(
1749            "SELECT * FROM " + Message.TABLENAME + " " +
1750            "LEFT JOIN cheogram." + Message.TABLENAME +
1751            "  USING (" + Message.UUID + ")" +
1752            "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) + ")",
1753            params.toArray(new String[0])
1754        );
1755
1756        while (cursor.moveToNext()) {
1757            try {
1758                final var m = Message.fromCursor(cursor, conversation);
1759                if (ids.contains(m.getUuid())) result.put(m.getUuid(), m);
1760                if (ids.contains(m.getServerMsgId())) result.put(m.getServerMsgId(), m);
1761                if (ids.contains(m.getRemoteMsgId())) result.put(m.getRemoteMsgId(), m);
1762            } catch (Exception e) {
1763                Log.e(Config.LOGTAG, "unable to restore message");
1764            }
1765        }
1766        cursor.close();
1767        return result;
1768    }
1769
1770    public ArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
1771        ArrayList<Message> list = new ArrayList<>();
1772        SQLiteDatabase db = this.getReadableDatabase();
1773        Cursor cursor;
1774        if (timestamp == -1) {
1775            String[] selectionArgs = {conversation.getUuid()};
1776            cursor = db.rawQuery(
1777                "SELECT * FROM " + Message.TABLENAME + " " +
1778                "LEFT JOIN cheogram." + Message.TABLENAME +
1779                "  USING (" + Message.UUID + ")" +
1780                " WHERE " + Message.UUID + " IN (" +
1781                "SELECT " + Message.UUID + " FROM " + Message.TABLENAME +
1782                " WHERE " + Message.CONVERSATION + "=? " +
1783                "ORDER BY " + Message.TIME_SENT + " DESC " +
1784                "LIMIT " + String.valueOf(limit) + ") " +
1785                "ORDER BY " + Message.TIME_SENT + " DESC ",
1786                selectionArgs
1787            );
1788        } else {
1789            String[] selectionArgs = {conversation.getUuid(),
1790                    Long.toString(timestamp)};
1791            cursor = db.rawQuery(
1792                "SELECT * FROM " + Message.TABLENAME + " " +
1793                "LEFT JOIN cheogram." + Message.TABLENAME +
1794                "  USING (" + Message.UUID + ")" +
1795                " WHERE " + Message.UUID + " IN (" +
1796                "SELECT " + Message.UUID + " FROM " + Message.TABLENAME +
1797                " WHERE " + Message.CONVERSATION + "=? AND " +
1798                Message.TIME_SENT + "<? " +
1799                "ORDER BY " + Message.TIME_SENT + " DESC " +
1800                "LIMIT " + String.valueOf(limit) + ") " +
1801                "ORDER BY " + Message.TIME_SENT + " DESC ",
1802                selectionArgs
1803            );
1804        }
1805        CursorUtils.upgradeCursorWindowSize(cursor);
1806        final Multimap<String, Message> waitingForReplies = HashMultimap.create();
1807        final var replyIds = new HashSet<String>();
1808        while (cursor.moveToNext()) {
1809            try {
1810                final var m = Message.fromCursor(cursor, conversation);
1811                final var reply = m.getReply();
1812                if (reply != null && reply.getAttribute("id") != null) { // Guard against busted replies
1813                    replyIds.add(reply.getAttribute("id"));
1814                    waitingForReplies.put(reply.getAttribute("id"), m);
1815                }
1816                list.add(0, m);
1817            } catch (Exception e) {
1818                Log.e(Config.LOGTAG, "unable to restore message", e);
1819            }
1820        }
1821        for (final var parent : getMessageFuzzyIds(conversation, replyIds).entrySet()) {
1822            for (final var m : waitingForReplies.get(parent.getKey())) {
1823                m.setInReplyTo(parent.getValue());
1824            }
1825        }
1826        cursor.close();
1827        return list;
1828    }
1829
1830    public Cursor getMessageSearchCursor(final List<String> term, final String uuid) {
1831        final SQLiteDatabase db = this.getReadableDatabase();
1832        final StringBuilder SQL = new StringBuilder();
1833        final String[] selectionArgs;
1834        SQL.append(
1835                "SELECT "
1836                        + Message.TABLENAME
1837                        + ".*,"
1838                        + Conversation.TABLENAME
1839                        + "."
1840                        + Conversation.CONTACTJID
1841                        + ","
1842                        + Conversation.TABLENAME
1843                        + "."
1844                        + Conversation.ACCOUNT
1845                        + ","
1846                        + Conversation.TABLENAME
1847                        + "."
1848                        + Conversation.MODE
1849                        + " FROM "
1850                        + Message.TABLENAME
1851                        + " JOIN "
1852                        + Conversation.TABLENAME
1853                        + " ON "
1854                        + Message.TABLENAME
1855                        + "."
1856                        + Message.CONVERSATION
1857                        + "="
1858                        + Conversation.TABLENAME
1859                        + "."
1860                        + Conversation.UUID
1861                        + " JOIN messages_index ON messages_index.rowid=messages.rowid WHERE "
1862                        + Message.ENCRYPTION
1863                        + " NOT IN("
1864                        + Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE
1865                        + ","
1866                        + Message.ENCRYPTION_PGP
1867                        + ","
1868                        + Message.ENCRYPTION_DECRYPTION_FAILED
1869                        + ","
1870                        + Message.ENCRYPTION_AXOLOTL_FAILED
1871                        + ") AND "
1872                        + Message.TYPE
1873                        + " IN("
1874                        + Message.TYPE_TEXT
1875                        + ","
1876                        + Message.TYPE_PRIVATE
1877                        + ") AND messages_index.body MATCH ?");
1878        if (uuid == null) {
1879            selectionArgs = new String[] {FtsUtils.toMatchString(term)};
1880        } else {
1881            selectionArgs = new String[] {FtsUtils.toMatchString(term), uuid};
1882            SQL.append(" AND " + Conversation.TABLENAME + '.' + Conversation.UUID + "=?");
1883        }
1884        SQL.append(" ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS);
1885        Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term));
1886        return db.rawQuery(SQL.toString(), selectionArgs);
1887    }
1888
1889    public List<String> markFileAsDeleted(final File file, final boolean internal) {
1890        SQLiteDatabase db = this.getReadableDatabase();
1891        String selection;
1892        String[] selectionArgs;
1893        if (internal) {
1894            final String name = file.getName();
1895            if (name.endsWith(".pgp")) {
1896                selection =
1897                        "("
1898                                + Message.RELATIVE_FILE_PATH
1899                                + " IN(?,?) OR ("
1900                                + Message.RELATIVE_FILE_PATH
1901                                + "=? and encryption in(1,4))) and type in (1,2,5)";
1902                selectionArgs =
1903                        new String[] {
1904                            file.getAbsolutePath(), name, name.substring(0, name.length() - 4)
1905                        };
1906            } else {
1907                selection = Message.RELATIVE_FILE_PATH + " IN(?,?) and type in (1,2,5)";
1908                selectionArgs = new String[] {file.getAbsolutePath(), name};
1909            }
1910        } else {
1911            selection = Message.RELATIVE_FILE_PATH + "=? and type in (1,2,5)";
1912            selectionArgs = new String[] {file.getAbsolutePath()};
1913        }
1914        final List<String> uuids = new ArrayList<>();
1915        Cursor cursor =
1916                db.query(
1917                        Message.TABLENAME,
1918                        new String[] {Message.UUID},
1919                        selection,
1920                        selectionArgs,
1921                        null,
1922                        null,
1923                        null);
1924        while (cursor != null && cursor.moveToNext()) {
1925            uuids.add(cursor.getString(0));
1926        }
1927        if (cursor != null) {
1928            cursor.close();
1929        }
1930        markFileAsDeleted(uuids);
1931        return uuids;
1932    }
1933
1934    public void markFileAsDeleted(List<String> uuids) {
1935        SQLiteDatabase db = this.getReadableDatabase();
1936        final ContentValues contentValues = new ContentValues();
1937        final String where = Message.UUID + "=?";
1938        contentValues.put(Message.DELETED, 1);
1939        db.beginTransaction();
1940        for (String uuid : uuids) {
1941            db.update(Message.TABLENAME, contentValues, where, new String[] {uuid});
1942        }
1943        db.setTransactionSuccessful();
1944        db.endTransaction();
1945    }
1946
1947    public void markFilesAsChanged(List<FilePathInfo> files) {
1948        SQLiteDatabase db = this.getReadableDatabase();
1949        final String where = Message.UUID + "=?";
1950        db.beginTransaction();
1951        for (FilePathInfo info : files) {
1952            final ContentValues contentValues = new ContentValues();
1953            contentValues.put(Message.DELETED, info.deleted ? 1 : 0);
1954            db.update(Message.TABLENAME, contentValues, where, new String[] {info.uuid.toString()});
1955        }
1956        db.setTransactionSuccessful();
1957        db.endTransaction();
1958    }
1959
1960    public List<FilePathInfo> getFilePathInfo() {
1961        final SQLiteDatabase db = this.getReadableDatabase();
1962        final Cursor cursor =
1963                db.query(
1964                        Message.TABLENAME,
1965                        new String[] {Message.UUID, Message.RELATIVE_FILE_PATH, Message.DELETED},
1966                        "type in (1,2,5) and " + Message.RELATIVE_FILE_PATH + " is not null",
1967                        null,
1968                        null,
1969                        null,
1970                        null);
1971        final List<FilePathInfo> list = new ArrayList<>();
1972        while (cursor != null && cursor.moveToNext()) {
1973            list.add(
1974                    new FilePathInfo(
1975                            cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0));
1976        }
1977        if (cursor != null) {
1978            cursor.close();
1979        }
1980        return list;
1981    }
1982
1983    public List<FilePath> getRelativeFilePaths(String account, Jid jid, int limit) {
1984        SQLiteDatabase db = this.getReadableDatabase();
1985        final String SQL =
1986                "select uuid,relativeFilePath from messages where type in (1,2,5) and deleted=0 and"
1987                        + " "
1988                        + Message.RELATIVE_FILE_PATH
1989                        + " is not null and conversationUuid=(select uuid from conversations where"
1990                        + " accountUuid=? and (contactJid=? or contactJid like ?)) order by"
1991                        + " timeSent desc";
1992        final String[] args = {account, jid.toString(), jid + "/%"};
1993        Cursor cursor = db.rawQuery(SQL + (limit > 0 ? " limit " + limit : ""), args);
1994        List<FilePath> filesPaths = new ArrayList<>();
1995        while (cursor.moveToNext()) {
1996            filesPaths.add(new FilePath(cursor.getString(0), cursor.getString(1)));
1997        }
1998        cursor.close();
1999        return filesPaths;
2000    }
2001
2002    public Message getMessageWithServerMsgId(
2003            final Conversation conversation, final String messageId) {
2004        final var db = this.getReadableDatabase();
2005        final String sql =
2006                "select * from messages LEFT JOIN cheogram.messages USING (uuid) where conversationUuid=? and serverMsgId=? LIMIT 1";
2007        final String[] args = {conversation.getUuid(), messageId};
2008        final Cursor cursor = db.rawQuery(sql, args);
2009        if (cursor == null) {
2010            return null;
2011        }
2012        Message message = null;
2013        try {
2014            if (cursor.moveToFirst()) {
2015                message = Message.fromCursor(cursor, conversation);
2016            }
2017        } catch (final IOException e) { }
2018        cursor.close();
2019        return message;
2020    }
2021
2022    public Message getMessageWithUuidOrRemoteId(
2023            final Conversation conversation, final String messageId) {
2024        final var db = this.getReadableDatabase();
2025        final String sql =
2026                "select * from messages LEFT JOIN cheogram.messages USING (uuid) where conversationUuid=? and (uuid=? OR remoteMsgId=?) LIMIT 1";
2027        final String[] args = {conversation.getUuid(), messageId, messageId};
2028        final Cursor cursor = db.rawQuery(sql, args);
2029        if (cursor == null) {
2030            return null;
2031        }
2032        Message message = null;
2033        try {
2034            if (cursor.moveToFirst()) {
2035                message = Message.fromCursor(cursor, conversation);
2036            }
2037        } catch (final IOException e) { }
2038        cursor.close();
2039        return message;
2040    }
2041
2042    public void insertCapsCache(
2043            EntityCapabilities.EntityCapsHash caps,
2044            EntityCapabilities2.EntityCaps2Hash caps2,
2045            InfoQuery infoQuery) {
2046        final var contentValues = new ContentValues();
2047        contentValues.put("caps", caps.encoded());
2048        contentValues.put("caps2", caps2.encoded());
2049        contentValues.put("disco_info", infoQuery.toString());
2050        getWritableDatabase()
2051                .insertWithOnConflict(
2052                        "caps_cache", null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
2053    }
2054
2055    public InfoQuery getInfoQuery(final EntityCapabilities.Hash hash) {
2056        final String selection;
2057        final String[] args;
2058        if (hash instanceof EntityCapabilities.EntityCapsHash) {
2059            selection = "caps=?";
2060            args = new String[] {hash.encoded()};
2061        } else if (hash instanceof EntityCapabilities2.EntityCaps2Hash) {
2062            selection = "caps2=?";
2063            args = new String[] {hash.encoded()};
2064        } else {
2065            return null;
2066        }
2067        try (final Cursor cursor =
2068                getReadableDatabase()
2069                        .query(
2070                                "caps_cache",
2071                                new String[] {"disco_info"},
2072                                selection,
2073                                args,
2074                                null,
2075                                null,
2076                                null)) {
2077            if (cursor.moveToFirst()) {
2078                final var cached = cursor.getString(0);
2079                try {
2080                    final var element =
2081                            XmlElementReader.read(cached.getBytes(StandardCharsets.UTF_8));
2082                    if (element instanceof InfoQuery infoQuery) {
2083                        return infoQuery;
2084                    }
2085                } catch (final IOException e) {
2086                    Log.e(Config.LOGTAG, "could not restore info query from cache", e);
2087                    return null;
2088                }
2089            } else {
2090                return null;
2091            }
2092        }
2093        return null;
2094    }
2095
2096    public static class FilePath {
2097        public final UUID uuid;
2098        public final String path;
2099
2100        private FilePath(String uuid, String path) {
2101            this.uuid = UUID.fromString(uuid);
2102            this.path = path;
2103        }
2104    }
2105
2106    public static class FilePathInfo extends FilePath {
2107        public boolean deleted;
2108
2109        private FilePathInfo(String uuid, String path, boolean deleted) {
2110            super(uuid, path);
2111            this.deleted = deleted;
2112        }
2113
2114        public boolean setDeleted(boolean deleted) {
2115            final boolean changed = deleted != this.deleted;
2116            this.deleted = deleted;
2117            return changed;
2118        }
2119    }
2120
2121    public Conversation findConversation(final String uuid) {
2122        final var db = this.getReadableDatabase();
2123        final String[] selectionArgs = {uuid};
2124        try (final Cursor cursor =
2125                db.query(
2126                        Conversation.TABLENAME,
2127                        null,
2128                        Conversation.UUID + "=?",
2129                        selectionArgs,
2130                        null,
2131                        null,
2132                        null)) {
2133            if (cursor.getCount() == 0) {
2134                return null;
2135            }
2136            cursor.moveToFirst();
2137            final Conversation conversation = Conversation.fromCursor(cursor);
2138            if (conversation.getJid() instanceof Jid.Invalid) {
2139                return null;
2140            }
2141            return conversation;
2142        }
2143    }
2144
2145    public Conversation findConversation(final Account account, final Jid contactJid) {
2146        final SQLiteDatabase db = this.getReadableDatabase();
2147        final String[] selectionArgs = {
2148            account.getUuid(),
2149            contactJid.asBareJid().toString() + "/%",
2150            contactJid.asBareJid().toString()
2151        };
2152        try (final Cursor cursor =
2153                db.query(
2154                        Conversation.TABLENAME,
2155                        null,
2156                        Conversation.ACCOUNT
2157                                + "=? AND ("
2158                                + Conversation.CONTACTJID
2159                                + " like ? OR "
2160                                + Conversation.CONTACTJID
2161                                + "=?)",
2162                        selectionArgs,
2163                        null,
2164                        null,
2165                        null)) {
2166            if (cursor.getCount() == 0) {
2167                return null;
2168            }
2169            cursor.moveToFirst();
2170            final Conversation conversation = Conversation.fromCursor(cursor);
2171            if (conversation.getJid() instanceof Jid.Invalid) {
2172                return null;
2173            }
2174            conversation.setAccount(account);
2175            return conversation;
2176        }
2177    }
2178
2179    public String findConversationUuid(final Jid account, final Jid jid) {
2180        final SQLiteDatabase db = this.getReadableDatabase();
2181        final String[] selectionArgs = {
2182            account.getLocal(),
2183            account.getDomain().toString(),
2184            jid.asBareJid().toString() + "/%",
2185            jid.asBareJid().toString()
2186        };
2187        try (final Cursor cursor =
2188                db.rawQuery(
2189                        "SELECT conversations.uuid FROM conversations JOIN accounts ON"
2190                            + " conversations.accountUuid=accounts.uuid WHERE accounts.username=?"
2191                            + " AND accounts.server=? AND (contactJid=? OR contactJid LIKE ?)",
2192                        selectionArgs)) {
2193            if (cursor.getCount() == 0) {
2194                return null;
2195            }
2196            cursor.moveToFirst();
2197            return cursor.getString(0);
2198        }
2199    }
2200
2201    public void updateConversation(final Conversation conversation) throws SQLiteException {
2202        final SQLiteDatabase db = this.getWritableDatabase();
2203        db.beginTransaction();
2204        try {
2205            final String[] args = {conversation.getUuid()};
2206            db.update(
2207                    Conversation.TABLENAME,
2208                    conversation.getContentValues(),
2209                    Conversation.UUID + "=?",
2210                    args);
2211            for (final var cv : conversation.mucOccupantCacheAsContentValues()) {
2212                db.insertWithOnConflict(
2213                        MucOptions.User.CacheEntry.TABLENAME,
2214                        null,
2215                        cv,
2216                        SQLiteDatabase.CONFLICT_REPLACE);
2217            }
2218            db.setTransactionSuccessful();
2219        } finally {
2220            db.endTransaction();
2221        }
2222    }
2223
2224    public List<Account> getAccounts() {
2225        SQLiteDatabase db = this.getReadableDatabase();
2226        return getAccounts(db);
2227    }
2228
2229    public List<Jid> getAccountJids(final boolean enabledOnly) {
2230        final SQLiteDatabase db = this.getReadableDatabase();
2231        final List<Jid> jids = new ArrayList<>();
2232        final String[] columns = new String[] {Account.USERNAME, Account.SERVER};
2233        final String where = enabledOnly ? "not options & (1 <<1)" : null;
2234        try (final Cursor cursor =
2235                db.query(Account.TABLENAME, columns, where, null, null, null, null)) {
2236            while (cursor != null && cursor.moveToNext()) {
2237                jids.add(Jid.of(cursor.getString(0), cursor.getString(1), null));
2238            }
2239        } catch (final Exception e) {
2240            return jids;
2241        }
2242        return jids;
2243    }
2244
2245    private List<Account> getAccounts(SQLiteDatabase db) {
2246        final List<Account> list = new ArrayList<>();
2247        try (final Cursor cursor =
2248                db.query(Account.TABLENAME, null, null, null, null, null, null)) {
2249            while (cursor != null && cursor.moveToNext()) {
2250                list.add(Account.fromCursor(cursor));
2251            }
2252        }
2253        return list;
2254    }
2255
2256    public boolean updateAccount(Account account) {
2257        final var db = this.getWritableDatabase();
2258        final String[] args = {account.getUuid()};
2259        final int rows =
2260                db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
2261        return rows == 1;
2262    }
2263
2264    public boolean deleteAccount(final Account account) {
2265        final var db = this.getWritableDatabase();
2266        final String[] args = {account.getUuid()};
2267        final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
2268        return rows == 1;
2269    }
2270
2271    public boolean updateMessage(final Message message, final boolean includeBody) {
2272        final var db = this.getWritableDatabase();
2273        final String[] args = {message.getUuid()};
2274        final var contentValues = message.getContentValues();
2275        contentValues.remove(Message.UUID);
2276        if (!includeBody) {
2277            contentValues.remove(Message.BODY);
2278        }
2279        return db.update(Message.TABLENAME, contentValues, Message.UUID + "=?", args) == 1 &&
2280               db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
2281    }
2282
2283    public boolean updateMessage(Message message, String uuid) {
2284        SQLiteDatabase db = this.getWritableDatabase();
2285        String[] args = {uuid};
2286        return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
2287               db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
2288    }
2289
2290
2291    public boolean deleteMessage(String uuid) {
2292        SQLiteDatabase db = this.getWritableDatabase();
2293        String[] args = {uuid};
2294        return db.delete(Message.TABLENAME, Message.UUID + "=?", args) == 1 &&
2295               db.delete("cheogram." + Message.TABLENAME, Message.UUID + "=?", args) == 1;
2296    }
2297
2298    public Map<Jid, Contact> readRoster(final Account account) {
2299        final var builder = new ImmutableMap.Builder<Jid, Contact>();
2300        final SQLiteDatabase db = this.getReadableDatabase();
2301        final String[] args = {account.getUuid()};
2302        try (final Cursor cursor =
2303                db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?", args, null, null, null)) {
2304            while (cursor.moveToNext()) {
2305                final var contact = Contact.fromCursor(cursor);
2306                if (contact != null) {
2307                    contact.setAccount(account);
2308                    builder.put(contact.getJid(), contact);
2309                }
2310            }
2311        }
2312        return builder.buildKeepingLast();
2313    }
2314
2315    public void writeRoster(
2316            final Account account, final String version, final List<Contact> contacts) {
2317        final long start = SystemClock.elapsedRealtime();
2318        final SQLiteDatabase db = this.getWritableDatabase();
2319        db.beginTransaction();
2320        for (final Contact contact : contacts) {
2321            if (contact.getOption(Contact.Options.IN_ROSTER)
2322                    || contact.hasAvatarOrPresenceName()
2323                    || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
2324                db.insert(Contact.TABLENAME, null, contact.getContentValues());
2325            } else {
2326                String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
2327                String[] whereArgs = {account.getUuid(), contact.getJid().toString()};
2328                db.delete(Contact.TABLENAME, where, whereArgs);
2329            }
2330        }
2331        db.setTransactionSuccessful();
2332        db.endTransaction();
2333        account.setRosterVersion(version);
2334        updateAccount(account);
2335        long duration = SystemClock.elapsedRealtime() - start;
2336        Log.d(
2337                Config.LOGTAG,
2338                account.getJid().asBareJid() + ": persisted roster in " + duration + "ms");
2339    }
2340
2341    public void deleteMessagesInConversation(Conversation conversation) {
2342        long start = SystemClock.elapsedRealtime();
2343        final SQLiteDatabase db = this.getWritableDatabase();
2344        db.beginTransaction();
2345        final String[] args = {conversation.getUuid()};
2346        int num = db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
2347        db.delete("cheogram.webxdc_updates", Message.CONVERSATION + "=?", args);
2348        db.setTransactionSuccessful();
2349        db.endTransaction();
2350        Log.d(
2351                Config.LOGTAG,
2352                "deleted "
2353                        + num
2354                        + " messages for "
2355                        + conversation.getJid().asBareJid()
2356                        + " in "
2357                        + (SystemClock.elapsedRealtime() - start)
2358                        + "ms");
2359    }
2360
2361    public void expireOldMessages(long timestamp) {
2362        final String[] args = {String.valueOf(timestamp)};
2363        SQLiteDatabase db = this.getReadableDatabase();
2364        db.beginTransaction();
2365        db.delete(Message.TABLENAME, "timeSent<?", args);
2366        db.delete("cheogram.messages", "timeReceived<?", args);
2367        db.setTransactionSuccessful();
2368        db.endTransaction();
2369    }
2370
2371    public MamReference getLastMessageReceived(Account account) {
2372        Cursor cursor = null;
2373        try {
2374            SQLiteDatabase db = this.getReadableDatabase();
2375            String sql =
2376                    "select messages.timeSent,messages.serverMsgId from accounts join conversations"
2377                        + " on accounts.uuid=conversations.accountUuid join messages on"
2378                        + " conversations.uuid=messages.conversationUuid where accounts.uuid=? and"
2379                        + " (messages.status=0 or messages.carbon=1 or messages.serverMsgId not"
2380                        + " null) and (conversations.mode=0 or (messages.serverMsgId not null and"
2381                        + " messages.type=4)) order by messages.timesent desc limit 1";
2382            String[] args = {account.getUuid()};
2383            cursor = db.rawQuery(sql, args);
2384            if (cursor.getCount() == 0) {
2385                return null;
2386            } else {
2387                cursor.moveToFirst();
2388                return new MamReference(cursor.getLong(0), cursor.getString(1));
2389            }
2390        } catch (Exception e) {
2391            return null;
2392        } finally {
2393            if (cursor != null) {
2394                cursor.close();
2395            }
2396        }
2397    }
2398
2399    public long getLastTimeFingerprintUsed(Account account, String fingerprint) {
2400        String SQL =
2401                "select messages.timeSent from accounts join conversations on"
2402                        + " accounts.uuid=conversations.accountUuid join messages on"
2403                        + " conversations.uuid=messages.conversationUuid where accounts.uuid=? and"
2404                        + " messages.axolotl_fingerprint=? order by messages.timesent desc limit 1";
2405        String[] args = {account.getUuid(), fingerprint};
2406        Cursor cursor = getReadableDatabase().rawQuery(SQL, args);
2407        long time;
2408        if (cursor.moveToFirst()) {
2409            time = cursor.getLong(0);
2410        } else {
2411            time = 0;
2412        }
2413        cursor.close();
2414        return time;
2415    }
2416
2417    public MamReference getLastClearDate(Account account) {
2418        SQLiteDatabase db = this.getReadableDatabase();
2419        String[] columns = {Conversation.ATTRIBUTES};
2420        String selection = Conversation.ACCOUNT + "=?";
2421        String[] args = {account.getUuid()};
2422        Cursor cursor =
2423                db.query(Conversation.TABLENAME, columns, selection, args, null, null, null);
2424        MamReference maxClearDate = new MamReference(0);
2425        while (cursor.moveToNext()) {
2426            try {
2427                final JSONObject o = new JSONObject(cursor.getString(0));
2428                maxClearDate =
2429                        MamReference.max(
2430                                maxClearDate,
2431                                MamReference.fromAttribute(
2432                                        o.getString(Conversation.ATTRIBUTE_LAST_CLEAR_HISTORY)));
2433            } catch (Exception e) {
2434                // ignored
2435            }
2436        }
2437        cursor.close();
2438        return maxClearDate;
2439    }
2440
2441    private Cursor getCursorForSession(Account account, SignalProtocolAddress contact) {
2442        final SQLiteDatabase db = this.getReadableDatabase();
2443        String[] selectionArgs = {
2444            account.getUuid(), contact.getName(), Integer.toString(contact.getDeviceId())
2445        };
2446        return db.query(
2447                SQLiteAxolotlStore.SESSION_TABLENAME,
2448                null,
2449                SQLiteAxolotlStore.ACCOUNT
2450                        + " = ? AND "
2451                        + SQLiteAxolotlStore.NAME
2452                        + " = ? AND "
2453                        + SQLiteAxolotlStore.DEVICE_ID
2454                        + " = ? ",
2455                selectionArgs,
2456                null,
2457                null,
2458                null);
2459    }
2460
2461    public SessionRecord loadSession(Account account, SignalProtocolAddress contact) {
2462        SessionRecord session = null;
2463        Cursor cursor = getCursorForSession(account, contact);
2464        if (cursor.getCount() != 0) {
2465            cursor.moveToFirst();
2466            try {
2467                session =
2468                        new SessionRecord(
2469                                Base64.decode(
2470                                        cursor.getString(
2471                                                cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2472                                        Base64.DEFAULT));
2473            } catch (IOException e) {
2474                cursor.close();
2475                throw new AssertionError(e);
2476            }
2477        }
2478        cursor.close();
2479        return session;
2480    }
2481
2482    public List<Integer> getSubDeviceSessions(Account account, SignalProtocolAddress contact) {
2483        final SQLiteDatabase db = this.getReadableDatabase();
2484        return getSubDeviceSessions(db, account, contact);
2485    }
2486
2487    private List<Integer> getSubDeviceSessions(
2488            SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
2489        List<Integer> devices = new ArrayList<>();
2490        String[] columns = {SQLiteAxolotlStore.DEVICE_ID};
2491        String[] selectionArgs = {account.getUuid(), contact.getName()};
2492        Cursor cursor =
2493                db.query(
2494                        SQLiteAxolotlStore.SESSION_TABLENAME,
2495                        columns,
2496                        SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.NAME + " = ?",
2497                        selectionArgs,
2498                        null,
2499                        null,
2500                        null);
2501
2502        while (cursor.moveToNext()) {
2503            devices.add(cursor.getInt(cursor.getColumnIndex(SQLiteAxolotlStore.DEVICE_ID)));
2504        }
2505
2506        cursor.close();
2507        return devices;
2508    }
2509
2510    public List<String> getKnownSignalAddresses(Account account) {
2511        List<String> addresses = new ArrayList<>();
2512        String[] colums = {"DISTINCT " + SQLiteAxolotlStore.NAME};
2513        String[] selectionArgs = {account.getUuid()};
2514        Cursor cursor =
2515                getReadableDatabase()
2516                        .query(
2517                                SQLiteAxolotlStore.SESSION_TABLENAME,
2518                                colums,
2519                                SQLiteAxolotlStore.ACCOUNT + " = ?",
2520                                selectionArgs,
2521                                null,
2522                                null,
2523                                null);
2524        while (cursor.moveToNext()) {
2525            addresses.add(cursor.getString(0));
2526        }
2527        cursor.close();
2528        return addresses;
2529    }
2530
2531    public boolean containsSession(Account account, SignalProtocolAddress contact) {
2532        Cursor cursor = getCursorForSession(account, contact);
2533        int count = cursor.getCount();
2534        cursor.close();
2535        return count != 0;
2536    }
2537
2538    public void storeSession(
2539            Account account, SignalProtocolAddress contact, SessionRecord session) {
2540        SQLiteDatabase db = this.getWritableDatabase();
2541        ContentValues values = new ContentValues();
2542        values.put(SQLiteAxolotlStore.NAME, contact.getName());
2543        values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
2544        values.put(
2545                SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
2546        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2547        db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
2548    }
2549
2550    public void deleteSession(Account account, SignalProtocolAddress contact) {
2551        SQLiteDatabase db = this.getWritableDatabase();
2552        deleteSession(db, account, contact);
2553    }
2554
2555    private void deleteSession(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
2556        String[] args = {
2557            account.getUuid(), contact.getName(), Integer.toString(contact.getDeviceId())
2558        };
2559        db.delete(
2560                SQLiteAxolotlStore.SESSION_TABLENAME,
2561                SQLiteAxolotlStore.ACCOUNT
2562                        + " = ? AND "
2563                        + SQLiteAxolotlStore.NAME
2564                        + " = ? AND "
2565                        + SQLiteAxolotlStore.DEVICE_ID
2566                        + " = ? ",
2567                args);
2568    }
2569
2570    public void deleteAllSessions(Account account, SignalProtocolAddress contact) {
2571        SQLiteDatabase db = this.getWritableDatabase();
2572        String[] args = {account.getUuid(), contact.getName()};
2573        db.delete(
2574                SQLiteAxolotlStore.SESSION_TABLENAME,
2575                SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.NAME + " = ?",
2576                args);
2577    }
2578
2579    private Cursor getCursorForPreKey(Account account, int preKeyId) {
2580        SQLiteDatabase db = this.getReadableDatabase();
2581        String[] columns = {SQLiteAxolotlStore.KEY};
2582        String[] selectionArgs = {account.getUuid(), Integer.toString(preKeyId)};
2583        Cursor cursor =
2584                db.query(
2585                        SQLiteAxolotlStore.PREKEY_TABLENAME,
2586                        columns,
2587                        SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2588                        selectionArgs,
2589                        null,
2590                        null,
2591                        null);
2592
2593        return cursor;
2594    }
2595
2596    public PreKeyRecord loadPreKey(Account account, int preKeyId) {
2597        PreKeyRecord record = null;
2598        Cursor cursor = getCursorForPreKey(account, preKeyId);
2599        if (cursor.getCount() != 0) {
2600            cursor.moveToFirst();
2601            try {
2602                record =
2603                        new PreKeyRecord(
2604                                Base64.decode(
2605                                        cursor.getString(
2606                                                cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2607                                        Base64.DEFAULT));
2608            } catch (IOException e) {
2609                throw new AssertionError(e);
2610            }
2611        }
2612        cursor.close();
2613        return record;
2614    }
2615
2616    public boolean containsPreKey(Account account, int preKeyId) {
2617        Cursor cursor = getCursorForPreKey(account, preKeyId);
2618        int count = cursor.getCount();
2619        cursor.close();
2620        return count != 0;
2621    }
2622
2623    public void storePreKey(Account account, PreKeyRecord record) {
2624        SQLiteDatabase db = this.getWritableDatabase();
2625        ContentValues values = new ContentValues();
2626        values.put(SQLiteAxolotlStore.ID, record.getId());
2627        values.put(
2628                SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
2629        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2630        db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
2631    }
2632
2633    public int deletePreKey(Account account, int preKeyId) {
2634        SQLiteDatabase db = this.getWritableDatabase();
2635        String[] args = {account.getUuid(), Integer.toString(preKeyId)};
2636        return db.delete(
2637                SQLiteAxolotlStore.PREKEY_TABLENAME,
2638                SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2639                args);
2640    }
2641
2642    private Cursor getCursorForSignedPreKey(Account account, int signedPreKeyId) {
2643        SQLiteDatabase db = this.getReadableDatabase();
2644        String[] columns = {SQLiteAxolotlStore.KEY};
2645        String[] selectionArgs = {account.getUuid(), Integer.toString(signedPreKeyId)};
2646        Cursor cursor =
2647                db.query(
2648                        SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2649                        columns,
2650                        SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2651                        selectionArgs,
2652                        null,
2653                        null,
2654                        null);
2655
2656        return cursor;
2657    }
2658
2659    public SignedPreKeyRecord loadSignedPreKey(Account account, int signedPreKeyId) {
2660        SignedPreKeyRecord record = null;
2661        Cursor cursor = getCursorForSignedPreKey(account, signedPreKeyId);
2662        if (cursor.getCount() != 0) {
2663            cursor.moveToFirst();
2664            try {
2665                record =
2666                        new SignedPreKeyRecord(
2667                                Base64.decode(
2668                                        cursor.getString(
2669                                                cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2670                                        Base64.DEFAULT));
2671            } catch (IOException e) {
2672                throw new AssertionError(e);
2673            }
2674        }
2675        cursor.close();
2676        return record;
2677    }
2678
2679    public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
2680        List<SignedPreKeyRecord> prekeys = new ArrayList<>();
2681        SQLiteDatabase db = this.getReadableDatabase();
2682        String[] columns = {SQLiteAxolotlStore.KEY};
2683        String[] selectionArgs = {account.getUuid()};
2684        Cursor cursor =
2685                db.query(
2686                        SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2687                        columns,
2688                        SQLiteAxolotlStore.ACCOUNT + "=?",
2689                        selectionArgs,
2690                        null,
2691                        null,
2692                        null);
2693
2694        while (cursor.moveToNext()) {
2695            try {
2696                prekeys.add(
2697                        new SignedPreKeyRecord(
2698                                Base64.decode(
2699                                        cursor.getString(
2700                                                cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2701                                        Base64.DEFAULT)));
2702            } catch (IOException ignored) {
2703            }
2704        }
2705        cursor.close();
2706        return prekeys;
2707    }
2708
2709    public int getSignedPreKeysCount(Account account) {
2710        String[] columns = {"count(" + SQLiteAxolotlStore.KEY + ")"};
2711        String[] selectionArgs = {account.getUuid()};
2712        SQLiteDatabase db = this.getReadableDatabase();
2713        Cursor cursor =
2714                db.query(
2715                        SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2716                        columns,
2717                        SQLiteAxolotlStore.ACCOUNT + "=?",
2718                        selectionArgs,
2719                        null,
2720                        null,
2721                        null);
2722        final int count;
2723        if (cursor.moveToFirst()) {
2724            count = cursor.getInt(0);
2725        } else {
2726            count = 0;
2727        }
2728        cursor.close();
2729        return count;
2730    }
2731
2732    public boolean containsSignedPreKey(Account account, int signedPreKeyId) {
2733        Cursor cursor = getCursorForPreKey(account, signedPreKeyId);
2734        int count = cursor.getCount();
2735        cursor.close();
2736        return count != 0;
2737    }
2738
2739    public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
2740        SQLiteDatabase db = this.getWritableDatabase();
2741        ContentValues values = new ContentValues();
2742        values.put(SQLiteAxolotlStore.ID, record.getId());
2743        values.put(
2744                SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
2745        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2746        db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
2747    }
2748
2749    public void deleteSignedPreKey(Account account, int signedPreKeyId) {
2750        SQLiteDatabase db = this.getWritableDatabase();
2751        String[] args = {account.getUuid(), Integer.toString(signedPreKeyId)};
2752        db.delete(
2753                SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2754                SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2755                args);
2756    }
2757
2758    private Cursor getIdentityKeyCursor(Account account, String name, boolean own) {
2759        final SQLiteDatabase db = this.getReadableDatabase();
2760        return getIdentityKeyCursor(db, account, name, own);
2761    }
2762
2763    private Cursor getIdentityKeyCursor(
2764            SQLiteDatabase db, Account account, String name, boolean own) {
2765        return getIdentityKeyCursor(db, account, name, own, null);
2766    }
2767
2768    private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
2769        final SQLiteDatabase db = this.getReadableDatabase();
2770        return getIdentityKeyCursor(db, account, fingerprint);
2771    }
2772
2773    private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String fingerprint) {
2774        return getIdentityKeyCursor(db, account, null, null, fingerprint);
2775    }
2776
2777    private Cursor getIdentityKeyCursor(
2778            SQLiteDatabase db, Account account, String name, Boolean own, String fingerprint) {
2779        String[] columns = {
2780            SQLiteAxolotlStore.TRUST,
2781            SQLiteAxolotlStore.ACTIVE,
2782            SQLiteAxolotlStore.LAST_ACTIVATION,
2783            SQLiteAxolotlStore.KEY
2784        };
2785        ArrayList<String> selectionArgs = new ArrayList<>(4);
2786        selectionArgs.add(account.getUuid());
2787        String selectionString = SQLiteAxolotlStore.ACCOUNT + " = ?";
2788        if (name != null) {
2789            selectionArgs.add(name);
2790            selectionString += " AND " + SQLiteAxolotlStore.NAME + " = ?";
2791        }
2792        if (fingerprint != null) {
2793            selectionArgs.add(fingerprint);
2794            selectionString += " AND " + SQLiteAxolotlStore.FINGERPRINT + " = ?";
2795        }
2796        if (own != null) {
2797            selectionArgs.add(own ? "1" : "0");
2798            selectionString += " AND " + SQLiteAxolotlStore.OWN + " = ?";
2799        }
2800        Cursor cursor =
2801                db.query(
2802                        SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2803                        columns,
2804                        selectionString,
2805                        selectionArgs.toArray(new String[selectionArgs.size()]),
2806                        null,
2807                        null,
2808                        null);
2809
2810        return cursor;
2811    }
2812
2813    public IdentityKeyPair loadOwnIdentityKeyPair(Account account) {
2814        SQLiteDatabase db = getReadableDatabase();
2815        return loadOwnIdentityKeyPair(db, account);
2816    }
2817
2818    private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
2819        String name = account.getJid().asBareJid().toString();
2820        IdentityKeyPair identityKeyPair = null;
2821        Cursor cursor = getIdentityKeyCursor(db, account, name, true);
2822        if (cursor.getCount() != 0) {
2823            cursor.moveToFirst();
2824            try {
2825                identityKeyPair =
2826                        new IdentityKeyPair(
2827                                Base64.decode(
2828                                        cursor.getString(
2829                                                cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2830                                        Base64.DEFAULT));
2831            } catch (InvalidKeyException e) {
2832                Log.d(
2833                        Config.LOGTAG,
2834                        AxolotlService.getLogprefix(account)
2835                                + "Encountered invalid IdentityKey in database for account"
2836                                + account.getJid().asBareJid()
2837                                + ", address: "
2838                                + name);
2839            }
2840        }
2841        cursor.close();
2842
2843        return identityKeyPair;
2844    }
2845
2846    public Set<IdentityKey> loadIdentityKeys(Account account, String name) {
2847        return loadIdentityKeys(account, name, null);
2848    }
2849
2850    public Set<IdentityKey> loadIdentityKeys(
2851            Account account, String name, FingerprintStatus status) {
2852        Set<IdentityKey> identityKeys = new HashSet<>();
2853        Cursor cursor = getIdentityKeyCursor(account, name, false);
2854
2855        while (cursor.moveToNext()) {
2856            if (status != null && !FingerprintStatus.fromCursor(cursor).equals(status)) {
2857                continue;
2858            }
2859            try {
2860                String key = cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY));
2861                if (key != null) {
2862                    identityKeys.add(new IdentityKey(Base64.decode(key, Base64.DEFAULT), 0));
2863                } else {
2864                    Log.d(
2865                            Config.LOGTAG,
2866                            AxolotlService.getLogprefix(account)
2867                                    + "Missing key (possibly preverified) in database for account"
2868                                    + account.getJid().asBareJid()
2869                                    + ", address: "
2870                                    + name);
2871                }
2872            } catch (InvalidKeyException e) {
2873                Log.d(
2874                        Config.LOGTAG,
2875                        AxolotlService.getLogprefix(account)
2876                                + "Encountered invalid IdentityKey in database for account"
2877                                + account.getJid().asBareJid()
2878                                + ", address: "
2879                                + name);
2880            }
2881        }
2882        cursor.close();
2883
2884        return identityKeys;
2885    }
2886
2887    public long numTrustedKeys(Account account, String name) {
2888        SQLiteDatabase db = getReadableDatabase();
2889        String[] args = {
2890            account.getUuid(),
2891            name,
2892            FingerprintStatus.Trust.TRUSTED.toString(),
2893            FingerprintStatus.Trust.VERIFIED.toString(),
2894            FingerprintStatus.Trust.VERIFIED_X509.toString()
2895        };
2896        return DatabaseUtils.queryNumEntries(
2897                db,
2898                SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2899                SQLiteAxolotlStore.ACCOUNT
2900                        + " = ?"
2901                        + " AND "
2902                        + SQLiteAxolotlStore.NAME
2903                        + " = ?"
2904                        + " AND ("
2905                        + SQLiteAxolotlStore.TRUST
2906                        + " = ? OR "
2907                        + SQLiteAxolotlStore.TRUST
2908                        + " = ? OR "
2909                        + SQLiteAxolotlStore.TRUST
2910                        + " = ?)"
2911                        + " AND "
2912                        + SQLiteAxolotlStore.ACTIVE
2913                        + " > 0",
2914                args);
2915    }
2916
2917    private void storeIdentityKey(
2918            Account account,
2919            String name,
2920            boolean own,
2921            String fingerprint,
2922            String base64Serialized,
2923            FingerprintStatus status) {
2924        SQLiteDatabase db = this.getWritableDatabase();
2925        ContentValues values = new ContentValues();
2926        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2927        values.put(SQLiteAxolotlStore.NAME, name);
2928        values.put(SQLiteAxolotlStore.OWN, own ? 1 : 0);
2929        values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
2930        values.put(SQLiteAxolotlStore.KEY, base64Serialized);
2931        values.putAll(status.toContentValues());
2932        String where =
2933                SQLiteAxolotlStore.ACCOUNT
2934                        + "=? AND "
2935                        + SQLiteAxolotlStore.NAME
2936                        + "=? AND "
2937                        + SQLiteAxolotlStore.FINGERPRINT
2938                        + " =?";
2939        String[] whereArgs = {account.getUuid(), name, fingerprint};
2940        int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, where, whereArgs);
2941        if (rows == 0) {
2942            db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
2943        }
2944    }
2945
2946    public void storePreVerification(
2947            Account account, String name, String fingerprint, FingerprintStatus status) {
2948        SQLiteDatabase db = this.getWritableDatabase();
2949        ContentValues values = new ContentValues();
2950        values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2951        values.put(SQLiteAxolotlStore.NAME, name);
2952        values.put(SQLiteAxolotlStore.OWN, 0);
2953        values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
2954        values.putAll(status.toContentValues());
2955        db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
2956    }
2957
2958    public FingerprintStatus getFingerprintStatus(Account account, String fingerprint) {
2959        Cursor cursor = getIdentityKeyCursor(account, fingerprint);
2960        final FingerprintStatus status;
2961        if (cursor.getCount() > 0) {
2962            cursor.moveToFirst();
2963            status = FingerprintStatus.fromCursor(cursor);
2964        } else {
2965            status = null;
2966        }
2967        cursor.close();
2968        return status;
2969    }
2970
2971    public boolean setIdentityKeyTrust(
2972            Account account, String fingerprint, FingerprintStatus fingerprintStatus) {
2973        SQLiteDatabase db = this.getWritableDatabase();
2974        return setIdentityKeyTrust(db, account, fingerprint, fingerprintStatus);
2975    }
2976
2977    private boolean setIdentityKeyTrust(
2978            SQLiteDatabase db, Account account, String fingerprint, FingerprintStatus status) {
2979        String[] selectionArgs = {account.getUuid(), fingerprint};
2980        int rows =
2981                db.update(
2982                        SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2983                        status.toContentValues(),
2984                        SQLiteAxolotlStore.ACCOUNT
2985                                + " = ? AND "
2986                                + SQLiteAxolotlStore.FINGERPRINT
2987                                + " = ? ",
2988                        selectionArgs);
2989        return rows == 1;
2990    }
2991
2992    public boolean setIdentityKeyCertificate(
2993            Account account, String fingerprint, X509Certificate x509Certificate) {
2994        SQLiteDatabase db = this.getWritableDatabase();
2995        String[] selectionArgs = {account.getUuid(), fingerprint};
2996        try {
2997            ContentValues values = new ContentValues();
2998            values.put(SQLiteAxolotlStore.CERTIFICATE, x509Certificate.getEncoded());
2999            return db.update(
3000                            SQLiteAxolotlStore.IDENTITIES_TABLENAME,
3001                            values,
3002                            SQLiteAxolotlStore.ACCOUNT
3003                                    + " = ? AND "
3004                                    + SQLiteAxolotlStore.FINGERPRINT
3005                                    + " = ? ",
3006                            selectionArgs)
3007                    == 1;
3008        } catch (CertificateEncodingException e) {
3009            Log.d(Config.LOGTAG, "could not encode certificate");
3010            return false;
3011        }
3012    }
3013
3014    public X509Certificate getIdentityKeyCertifcate(Account account, String fingerprint) {
3015        SQLiteDatabase db = this.getReadableDatabase();
3016        String[] selectionArgs = {account.getUuid(), fingerprint};
3017        String[] colums = {SQLiteAxolotlStore.CERTIFICATE};
3018        String selection =
3019                SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ";
3020        Cursor cursor =
3021                db.query(
3022                        SQLiteAxolotlStore.IDENTITIES_TABLENAME,
3023                        colums,
3024                        selection,
3025                        selectionArgs,
3026                        null,
3027                        null,
3028                        null);
3029        if (cursor.getCount() < 1) {
3030            return null;
3031        } else {
3032            cursor.moveToFirst();
3033            byte[] certificate =
3034                    cursor.getBlob(cursor.getColumnIndex(SQLiteAxolotlStore.CERTIFICATE));
3035            cursor.close();
3036            if (certificate == null || certificate.length == 0) {
3037                return null;
3038            }
3039            try {
3040                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
3041                return (X509Certificate)
3042                        certificateFactory.generateCertificate(
3043                                new ByteArrayInputStream(certificate));
3044            } catch (CertificateException e) {
3045                Log.d(Config.LOGTAG, "certificate exception " + e.getMessage());
3046                return null;
3047            }
3048        }
3049    }
3050
3051    public void storeIdentityKey(
3052            Account account, String name, IdentityKey identityKey, FingerprintStatus status) {
3053        storeIdentityKey(
3054                account,
3055                name,
3056                false,
3057                CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()),
3058                Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT),
3059                status);
3060    }
3061
3062    public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
3063        storeIdentityKey(
3064                account,
3065                account.getJid().asBareJid().toString(),
3066                true,
3067                CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()),
3068                Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT),
3069                FingerprintStatus.createActiveVerified(false));
3070    }
3071
3072    private void recreateAxolotlDb(SQLiteDatabase db) {
3073        Log.d(
3074                Config.LOGTAG,
3075                AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<");
3076        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME);
3077        db.execSQL(CREATE_SESSIONS_STATEMENT);
3078        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.PREKEY_TABLENAME);
3079        db.execSQL(CREATE_PREKEYS_STATEMENT);
3080        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME);
3081        db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
3082        db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.IDENTITIES_TABLENAME);
3083        db.execSQL(CREATE_IDENTITIES_STATEMENT);
3084    }
3085
3086    public void wipeAxolotlDb(Account account) {
3087        String accountName = account.getUuid();
3088        Log.d(
3089                Config.LOGTAG,
3090                AxolotlService.getLogprefix(account)
3091                        + ">>> WIPING AXOLOTL DATABASE FOR ACCOUNT "
3092                        + accountName
3093                        + " <<<");
3094        SQLiteDatabase db = this.getWritableDatabase();
3095        String[] deleteArgs = {accountName};
3096        db.delete(
3097                SQLiteAxolotlStore.SESSION_TABLENAME,
3098                SQLiteAxolotlStore.ACCOUNT + " = ?",
3099                deleteArgs);
3100        db.delete(
3101                SQLiteAxolotlStore.PREKEY_TABLENAME,
3102                SQLiteAxolotlStore.ACCOUNT + " = ?",
3103                deleteArgs);
3104        db.delete(
3105                SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
3106                SQLiteAxolotlStore.ACCOUNT + " = ?",
3107                deleteArgs);
3108        db.delete(
3109                SQLiteAxolotlStore.IDENTITIES_TABLENAME,
3110                SQLiteAxolotlStore.ACCOUNT + " = ?",
3111                deleteArgs);
3112    }
3113
3114    public List<ShortcutService.FrequentContact> getFrequentContacts(final int days) {
3115        final var db = this.getReadableDatabase();
3116        final String SQL =
3117                "select "
3118                        + Conversation.TABLENAME
3119                        + "."
3120                        + Conversation.UUID
3121                        + ","
3122                        + Conversation.TABLENAME
3123                        + "."
3124                        + Conversation.ACCOUNT
3125                        + ","
3126                        + Conversation.TABLENAME
3127                        + "."
3128                        + Conversation.CONTACTJID
3129                        + " from "
3130                        + Conversation.TABLENAME
3131                        + " join "
3132                        + Message.TABLENAME
3133                        + " on conversations.uuid=messages.conversationUuid where"
3134                        + " messages.status!=0 and carbon==0  and conversations.mode=0 and"
3135                        + " messages.timeSent>=? group by conversations.uuid order by count(body)"
3136                        + " desc limit 4;";
3137        String[] whereArgs =
3138                new String[] {
3139                    String.valueOf(System.currentTimeMillis() - (Config.MILLISECONDS_IN_DAY * days))
3140                };
3141        Cursor cursor = db.rawQuery(SQL, whereArgs);
3142        ArrayList<ShortcutService.FrequentContact> contacts = new ArrayList<>();
3143        while (cursor.moveToNext()) {
3144            try {
3145                contacts.add(
3146                        new ShortcutService.FrequentContact(
3147                                cursor.getString(0),
3148                                cursor.getString(1),
3149                                Jid.of(cursor.getString(2))));
3150            } catch (final Exception e) {
3151                Log.e(Config.LOGTAG, "could not create frequent contact", e);
3152            }
3153        }
3154        cursor.close();
3155        return contacts;
3156    }
3157}