Search in sources :

Example 36 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Signal-Android by signalapp.

the class SignedPreKeyDatabase method getAllSignedPreKeys.

@NonNull
public List<SignedPreKeyRecord> getAllSignedPreKeys() {
    SQLiteDatabase database = databaseHelper.getReadableDatabase();
    List<SignedPreKeyRecord> results = new LinkedList<>();
    try (Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null)) {
        while (cursor != null && cursor.moveToNext()) {
            try {
                int keyId = cursor.getInt(cursor.getColumnIndexOrThrow(KEY_ID));
                ECPublicKey publicKey = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
                ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
                byte[] signature = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(SIGNATURE)));
                long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));
                results.add(new SignedPreKeyRecord(keyId, timestamp, new ECKeyPair(publicKey, privateKey), signature));
            } catch (InvalidKeyException | IOException e) {
                Log.w(TAG, e);
            }
        }
    }
    return results;
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) IOException(java.io.IOException) Cursor(android.database.Cursor) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) LinkedList(java.util.LinkedList) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord) NonNull(android.support.annotation.NonNull)

Example 37 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Signal-Android by signalapp.

the class SignedPreKeyDatabase method insertSignedPreKey.

public void insertSignedPreKey(int keyId, SignedPreKeyRecord record) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_ID, keyId);
    contentValues.put(PUBLIC_KEY, Base64.encodeBytes(record.getKeyPair().getPublicKey().serialize()));
    contentValues.put(PRIVATE_KEY, Base64.encodeBytes(record.getKeyPair().getPrivateKey().serialize()));
    contentValues.put(SIGNATURE, Base64.encodeBytes(record.getSignature()));
    contentValues.put(TIMESTAMP, record.getTimestamp());
    database.replace(TABLE_NAME, null, contentValues);
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase)

Example 38 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Signal-Android by signalapp.

the class SmsDatabase method setTimestampRead.

public List<Pair<Long, Long>> setTimestampRead(SyncMessageId messageId, long expireStarted) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    List<Pair<Long, Long>> expiring = new LinkedList<>();
    Cursor cursor = null;
    try {
        cursor = database.query(TABLE_NAME, new String[] { ID, THREAD_ID, ADDRESS, TYPE, EXPIRES_IN }, DATE_SENT + " = ?", new String[] { String.valueOf(messageId.getTimetamp()) }, null, null, null, null);
        while (cursor.moveToNext()) {
            Address theirAddress = messageId.getAddress();
            Address ourAddress = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
            if (ourAddress.equals(theirAddress)) {
                long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
                long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
                long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(EXPIRES_IN));
                ContentValues contentValues = new ContentValues();
                contentValues.put(READ, 1);
                if (expiresIn > 0) {
                    contentValues.put(EXPIRE_STARTED, expireStarted);
                    expiring.add(new Pair<>(id, expiresIn));
                }
                database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] { cursor.getLong(cursor.getColumnIndexOrThrow(ID)) + "" });
                DatabaseFactory.getThreadDatabase(context).updateReadState(threadId);
                DatabaseFactory.getThreadDatabase(context).setLastSeen(threadId);
                notifyConversationListeners(threadId);
            }
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return expiring;
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) Pair(android.util.Pair)

Example 39 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Signal-Android by signalapp.

the class SmsDatabase method getThreadIdForMessage.

public long getThreadIdForMessage(long id) {
    String sql = "SELECT " + THREAD_ID + " FROM " + TABLE_NAME + " WHERE " + ID + " = ?";
    String[] sqlArgs = new String[] { id + "" };
    SQLiteDatabase db = databaseHelper.getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(sql, sqlArgs);
        if (cursor != null && cursor.moveToFirst())
            return cursor.getLong(0);
        else
            return -1;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor)

Example 40 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project Signal-Android by signalapp.

the class SmsDatabase method getMessageCount.

public int getMessageCount() {
    SQLiteDatabase db = databaseHelper.getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.query(TABLE_NAME, new String[] { "COUNT(*)" }, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst())
            return cursor.getInt(0);
        else
            return 0;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor)

Aggregations

SQLiteDatabase (net.sqlcipher.database.SQLiteDatabase)124 Cursor (android.database.Cursor)37 ContentValues (android.content.ContentValues)32 IOException (java.io.IOException)15 LinkedList (java.util.LinkedList)12 NonNull (android.support.annotation.NonNull)7 MergeCursor (android.database.MergeCursor)6 Nullable (android.support.annotation.Nullable)6 Pair (android.util.Pair)6 File (java.io.File)5 SQLiteConstraintException (net.sqlcipher.database.SQLiteConstraintException)5 FileNotFoundException (java.io.FileNotFoundException)4 StreamCorruptedException (java.io.StreamCorruptedException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 AudioClipTable (org.storymaker.app.model.AudioClipTable)4 AuthTable (org.storymaker.app.model.AuthTable)4 JobTable (org.storymaker.app.model.JobTable)4 LessonTable (org.storymaker.app.model.LessonTable)4 MediaTable (org.storymaker.app.model.MediaTable)4 ProjectTable (org.storymaker.app.model.ProjectTable)4