use of org.thoughtcrime.securesms.keyvalue.KeyValueDataSet in project Signal-Android by WhisperSystems.
the class FullBackupExporter method exportKeyValues.
private static int exportKeyValues(@NonNull BackupFrameOutputStream outputStream, @NonNull List<String> keysToIncludeInBackup, int count, long estimatedCount, BackupCancellationSignal cancellationSignal) throws IOException {
KeyValueDataSet dataSet = KeyValueDatabase.getInstance(ApplicationDependencies.getApplication()).getDataSet();
for (String key : keysToIncludeInBackup) {
throwIfCanceled(cancellationSignal);
if (!dataSet.containsKey(key)) {
continue;
}
BackupProtos.KeyValue.Builder builder = BackupProtos.KeyValue.newBuilder().setKey(key);
Class<?> type = dataSet.getType(key);
if (type == byte[].class) {
byte[] data = dataSet.getBlob(key, null);
if (data != null) {
builder.setBlobValue(ByteString.copyFrom(dataSet.getBlob(key, null)));
} else {
Log.w(TAG, "Skipping storing null blob for key: " + key);
}
} else if (type == Boolean.class) {
builder.setBooleanValue(dataSet.getBoolean(key, false));
} else if (type == Float.class) {
builder.setFloatValue(dataSet.getFloat(key, 0));
} else if (type == Integer.class) {
builder.setIntegerValue(dataSet.getInteger(key, 0));
} else if (type == Long.class) {
builder.setLongValue(dataSet.getLong(key, 0));
} else if (type == String.class) {
String data = dataSet.getString(key, null);
if (data != null) {
builder.setStringValue(dataSet.getString(key, null));
} else {
Log.w(TAG, "Skipping storing null string for key: " + key);
}
} else {
throw new AssertionError("Unknown type: " + type);
}
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.PROGRESS, ++count, estimatedCount));
outputStream.write(builder.build());
}
return count;
}
use of org.thoughtcrime.securesms.keyvalue.KeyValueDataSet in project Signal-Android by signalapp.
the class KeyValueDatabase method getDataSet.
@Override
@NonNull
public KeyValueDataSet getDataSet() {
KeyValueDataSet dataSet = new KeyValueDataSet();
try (Cursor cursor = getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null)) {
while (cursor != null && cursor.moveToNext()) {
Type type = Type.fromId(cursor.getInt(cursor.getColumnIndexOrThrow(TYPE)));
String key = cursor.getString(cursor.getColumnIndexOrThrow(KEY));
switch(type) {
case BLOB:
dataSet.putBlob(key, cursor.getBlob(cursor.getColumnIndexOrThrow(VALUE)));
break;
case BOOLEAN:
dataSet.putBoolean(key, cursor.getInt(cursor.getColumnIndexOrThrow(VALUE)) == 1);
break;
case FLOAT:
dataSet.putFloat(key, cursor.getFloat(cursor.getColumnIndexOrThrow(VALUE)));
break;
case INTEGER:
dataSet.putInteger(key, cursor.getInt(cursor.getColumnIndexOrThrow(VALUE)));
break;
case LONG:
dataSet.putLong(key, cursor.getLong(cursor.getColumnIndexOrThrow(VALUE)));
break;
case STRING:
dataSet.putString(key, cursor.getString(cursor.getColumnIndexOrThrow(VALUE)));
break;
}
}
}
return dataSet;
}
use of org.thoughtcrime.securesms.keyvalue.KeyValueDataSet in project Signal-Android by signalapp.
the class FullBackupImporter method processKeyValue.
private static void processKeyValue(BackupProtos.KeyValue keyValue) {
KeyValueDataSet dataSet = new KeyValueDataSet();
if (keyValue.hasBlobValue()) {
dataSet.putBlob(keyValue.getKey(), keyValue.getBlobValue().toByteArray());
} else if (keyValue.hasBooleanValue()) {
dataSet.putBoolean(keyValue.getKey(), keyValue.getBooleanValue());
} else if (keyValue.hasFloatValue()) {
dataSet.putFloat(keyValue.getKey(), keyValue.getFloatValue());
} else if (keyValue.hasIntegerValue()) {
dataSet.putInteger(keyValue.getKey(), keyValue.getIntegerValue());
} else if (keyValue.hasLongValue()) {
dataSet.putLong(keyValue.getKey(), keyValue.getLongValue());
} else if (keyValue.hasStringValue()) {
dataSet.putString(keyValue.getKey(), keyValue.getStringValue());
} else {
Log.i(TAG, "Unknown KeyValue backup value, skipping");
return;
}
KeyValueDatabase.getInstance(ApplicationDependencies.getApplication()).writeDataSet(dataSet, Collections.emptyList());
}
Aggregations