Search in sources :

Example 81 with SQLiteDatabase

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

the class PlaintextBackupImporter method importPlaintextFromSd.

public static void importPlaintextFromSd(Context context) throws NoExternalStorageException, IOException {
    Log.w("PlaintextBackupImporter", "importPlaintext()");
    SmsDatabase db = DatabaseFactory.getSmsDatabase(context);
    SQLiteDatabase transaction = db.beginTransaction();
    try {
        ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
        XmlBackup backup = new XmlBackup(getPlaintextExportFile().getAbsolutePath());
        Set<Long> modifiedThreads = new HashSet<>();
        XmlBackup.XmlBackupItem item;
        while ((item = backup.getNext()) != null) {
            Recipient recipient = Recipient.from(context, Address.fromExternal(context, item.getAddress()), false);
            long threadId = threads.getThreadIdFor(recipient);
            SQLiteStatement statement = db.createInsertStatement(transaction);
            if (item.getAddress() == null || item.getAddress().equals("null"))
                continue;
            if (!isAppropriateTypeForImport(item.getType()))
                continue;
            addStringToStatement(statement, 1, item.getAddress());
            addNullToStatement(statement, 2);
            addLongToStatement(statement, 3, item.getDate());
            addLongToStatement(statement, 4, item.getDate());
            addLongToStatement(statement, 5, item.getProtocol());
            addLongToStatement(statement, 6, item.getRead());
            addLongToStatement(statement, 7, item.getStatus());
            addTranslatedTypeToStatement(statement, 8, item.getType());
            addNullToStatement(statement, 9);
            addStringToStatement(statement, 10, item.getSubject());
            addStringToStatement(statement, 11, item.getBody());
            addStringToStatement(statement, 12, item.getServiceCenter());
            addLongToStatement(statement, 13, threadId);
            modifiedThreads.add(threadId);
            statement.execute();
        }
        for (long threadId : modifiedThreads) {
            threads.update(threadId, true);
        }
        Log.w("PlaintextBackupImporter", "Exited loop");
    } catch (XmlPullParserException e) {
        Log.w("PlaintextBackupImporter", e);
        throw new IOException("XML Parsing error!");
    } finally {
        db.endTransaction(transaction);
    }
}
Also used : Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) SQLiteStatement(net.sqlcipher.database.SQLiteStatement) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) HashSet(java.util.HashSet)

Example 82 with SQLiteDatabase

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

the class RecipientDatabase method updateOrInsert.

private void updateOrInsert(Address address, ContentValues contentValues) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    database.beginTransaction();
    int updated = database.update(TABLE_NAME, contentValues, ADDRESS + " = ?", new String[] { address.serialize() });
    if (updated < 1) {
        contentValues.put(ADDRESS, address.serialize());
        database.insert(TABLE_NAME, null, contentValues);
    }
    database.setTransactionSuccessful();
    database.endTransaction();
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase)

Example 83 with SQLiteDatabase

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

the class RecipientDatabase method getSystemContacts.

public List<Address> getSystemContacts() {
    SQLiteDatabase db = databaseHelper.getReadableDatabase();
    List<Address> results = new LinkedList<>();
    try (Cursor cursor = db.query(TABLE_NAME, new String[] { ADDRESS }, SYSTEM_DISPLAY_NAME + " IS NOT NULL AND " + SYSTEM_DISPLAY_NAME + " != \"\"", null, null, null, null)) {
        while (cursor != null && cursor.moveToNext()) {
            results.add(Address.fromSerialized(cursor.getString(0)));
        }
    }
    return results;
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList)

Example 84 with SQLiteDatabase

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

the class RecipientDatabase method getAllAddresses.

public Set<Address> getAllAddresses() {
    SQLiteDatabase db = databaseHelper.getReadableDatabase();
    Set<Address> results = new HashSet<>();
    try (Cursor cursor = db.query(TABLE_NAME, new String[] { ADDRESS }, null, null, null, null, null)) {
        while (cursor != null && cursor.moveToNext()) {
            results.add(Address.fromExternal(context, cursor.getString(0)));
        }
    }
    return results;
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor) HashSet(java.util.HashSet)

Example 85 with SQLiteDatabase

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

the class RecipientDatabase method getRegistered.

public List<Address> getRegistered() {
    SQLiteDatabase db = databaseHelper.getReadableDatabase();
    List<Address> results = new LinkedList<>();
    try (Cursor cursor = db.query(TABLE_NAME, new String[] { ADDRESS }, REGISTERED + " = ?", new String[] { "1" }, null, null, null)) {
        while (cursor != null && cursor.moveToNext()) {
            results.add(Address.fromSerialized(cursor.getString(0)));
        }
    }
    return results;
}
Also used : SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList)

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