DatabaseBackend.java

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