Search in sources :

Example 1 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project cw-omnibus by commonsguy.

the class DatabaseHelper method encrypt.

static void encrypt(Context ctxt) {
    SQLiteDatabase.loadLibs(ctxt);
    File dbFile = ctxt.getDatabasePath(DATABASE_NAME);
    File legacyFile = ctxt.getDatabasePath(LEGACY_DATABASE_NAME);
    if (!dbFile.exists() && legacyFile.exists()) {
        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(legacyFile, "", null);
        db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';", dbFile.getAbsolutePath(), PASSPHRASE));
        db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
        db.rawExecSQL("DETACH DATABASE encrypted;");
        int version = db.getVersion();
        db.close();
        db = SQLiteDatabase.openOrCreateDatabase(dbFile, PASSPHRASE, null);
        db.setVersion(version);
        db.close();
        legacyFile.delete();
    }
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) File(java.io.File)

Example 2 with SQLiteDatabase

use of net.sqlcipher.database.SQLiteDatabase in project requery by requery.

the class SqlCipherMetaData method queryMemory.

@Override
protected <R> R queryMemory(Function<Cursor, R> function, String query) throws SQLException {
    try {
        final SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(":memory:", "", null);
        Cursor cursor = database.rawQuery(query, null);
        return function.apply(closeWithCursor(new Closeable() {

            @Override
            public void close() throws IOException {
                database.close();
            }
        }, cursor));
    } catch (SQLiteException e) {
        throw new SQLException(e);
    }
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) SQLException(java.sql.SQLException) Closeable(java.io.Closeable) Cursor(android.database.Cursor) SQLiteException(net.sqlcipher.database.SQLiteException)

Example 3 with SQLiteDatabase

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

the class AttachmentDatabase method getPendingAttachments.

@NonNull
public List<DatabaseAttachment> getPendingAttachments() {
    final SQLiteDatabase database = databaseHelper.getReadableDatabase();
    final List<DatabaseAttachment> attachments = new LinkedList<>();
    Cursor cursor = null;
    try {
        cursor = database.query(TABLE_NAME, PROJECTION, TRANSFER_STATE + " = ?", new String[] { String.valueOf(TRANSFER_PROGRESS_STARTED) }, null, null, null);
        while (cursor != null && cursor.moveToNext()) {
            attachments.add(getAttachment(cursor));
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return attachments;
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) NonNull(android.support.annotation.NonNull)

Example 4 with SQLiteDatabase

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

the class AttachmentDatabase method getAttachment.

@Nullable
public DatabaseAttachment getAttachment(@NonNull AttachmentId attachmentId) {
    SQLiteDatabase database = databaseHelper.getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = database.query(TABLE_NAME, PROJECTION, PART_ID_WHERE, attachmentId.toStrings(), null, null, null);
        if (cursor != null && cursor.moveToFirst())
            return getAttachment(cursor);
        else
            return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor) Nullable(android.support.annotation.Nullable)

Example 5 with SQLiteDatabase

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

the class AttachmentDatabase method insertAttachment.

private AttachmentId insertAttachment(long mmsId, Attachment attachment) throws MmsException {
    Log.w(TAG, "Inserting attachment for mms id: " + mmsId);
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    DataInfo dataInfo = null;
    long uniqueId = System.currentTimeMillis();
    if (attachment.getDataUri() != null) {
        dataInfo = setAttachmentData(attachment.getDataUri());
        Log.w(TAG, "Wrote part to file: " + dataInfo.file.getAbsolutePath());
    }
    ContentValues contentValues = new ContentValues();
    contentValues.put(MMS_ID, mmsId);
    contentValues.put(CONTENT_TYPE, attachment.getContentType());
    contentValues.put(TRANSFER_STATE, attachment.getTransferState());
    contentValues.put(UNIQUE_ID, uniqueId);
    contentValues.put(CONTENT_LOCATION, attachment.getLocation());
    contentValues.put(DIGEST, attachment.getDigest());
    contentValues.put(CONTENT_DISPOSITION, attachment.getKey());
    contentValues.put(NAME, attachment.getRelay());
    contentValues.put(FILE_NAME, StorageUtil.getCleanFileName(attachment.getFileName()));
    contentValues.put(SIZE, attachment.getSize());
    contentValues.put(FAST_PREFLIGHT_ID, attachment.getFastPreflightId());
    contentValues.put(VOICE_NOTE, attachment.isVoiceNote() ? 1 : 0);
    if (dataInfo != null) {
        contentValues.put(DATA, dataInfo.file.getAbsolutePath());
        contentValues.put(SIZE, dataInfo.length);
        contentValues.put(DATA_RANDOM, dataInfo.random);
    }
    long rowId = database.insert(TABLE_NAME, null, contentValues);
    AttachmentId attachmentId = new AttachmentId(rowId, uniqueId);
    if (dataInfo != null) {
        if (MediaUtil.hasVideoThumbnail(attachment.getDataUri())) {
            Bitmap bitmap = MediaUtil.getVideoThumbnail(context, attachment.getDataUri());
            if (bitmap != null) {
                ThumbnailData thumbnailData = new ThumbnailData(bitmap);
                updateAttachmentThumbnail(attachmentId, thumbnailData.toDataStream(), thumbnailData.getAspectRatio());
            } else {
                Log.w(TAG, "Retrieving video thumbnail failed, submitting thumbnail generation job...");
                thumbnailExecutor.submit(new ThumbnailFetchCallable(attachmentId));
            }
        } else {
            Log.w(TAG, "Submitting thumbnail generation job...");
            thumbnailExecutor.submit(new ThumbnailFetchCallable(attachmentId));
        }
    }
    return attachmentId;
}
Also used : ContentValues(android.content.ContentValues) Bitmap(android.graphics.Bitmap) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) ThumbnailData(org.thoughtcrime.securesms.util.MediaUtil.ThumbnailData) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId)

Aggregations

SQLiteDatabase (net.sqlcipher.database.SQLiteDatabase)158 Cursor (android.database.Cursor)56 ContentValues (android.content.ContentValues)48 LinkedList (java.util.LinkedList)19 IOException (java.io.IOException)15 NonNull (android.support.annotation.NonNull)10 MergeCursor (android.database.MergeCursor)8 Nullable (android.support.annotation.Nullable)8 Pair (android.util.Pair)7 File (java.io.File)7 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