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