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