DatabaseBackend.java

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