mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 09:22:23 +00:00
Support for full backup/restore to sdcard
This commit is contained in:
@@ -65,27 +65,29 @@ public class AttachmentDatabase extends Database {
|
||||
|
||||
private static final String TAG = AttachmentDatabase.class.getSimpleName();
|
||||
|
||||
static final String TABLE_NAME = "part";
|
||||
static final String ROW_ID = "_id";
|
||||
public static final String TABLE_NAME = "part";
|
||||
public static final String ROW_ID = "_id";
|
||||
public static final String ATTACHMENT_ID_ALIAS = "attachment_id";
|
||||
static final String MMS_ID = "mid";
|
||||
static final String CONTENT_TYPE = "ct";
|
||||
static final String NAME = "name";
|
||||
static final String CONTENT_DISPOSITION = "cd";
|
||||
static final String CONTENT_LOCATION = "cl";
|
||||
static final String DATA = "_data";
|
||||
public static final String DATA = "_data";
|
||||
static final String TRANSFER_STATE = "pending_push";
|
||||
static final String SIZE = "data_size";
|
||||
public static final String SIZE = "data_size";
|
||||
static final String FILE_NAME = "file_name";
|
||||
static final String THUMBNAIL = "thumbnail";
|
||||
public static final String THUMBNAIL = "thumbnail";
|
||||
static final String THUMBNAIL_ASPECT_RATIO = "aspect_ratio";
|
||||
public static final String UNIQUE_ID = "unique_id";
|
||||
static final String DIGEST = "digest";
|
||||
static final String VOICE_NOTE = "voice_note";
|
||||
public static final String FAST_PREFLIGHT_ID = "fast_preflight_id";
|
||||
private static final String DATA_RANDOM = "data_random";
|
||||
public static final String DATA_RANDOM = "data_random";
|
||||
private static final String THUMBNAIL_RANDOM = "thumbnail_random";
|
||||
|
||||
public static final String DIRECTORY = "parts";
|
||||
|
||||
public static final int TRANSFER_PROGRESS_DONE = 0;
|
||||
public static final int TRANSFER_PROGRESS_STARTED = 1;
|
||||
public static final int TRANSFER_PROGRESS_PENDING = 2;
|
||||
@@ -256,7 +258,7 @@ public class AttachmentDatabase extends Database {
|
||||
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
||||
database.delete(TABLE_NAME, null, null);
|
||||
|
||||
File attachmentsDirectory = context.getDir("parts", Context.MODE_PRIVATE);
|
||||
File attachmentsDirectory = context.getDir(DIRECTORY, Context.MODE_PRIVATE);
|
||||
File[] attachments = attachmentsDirectory.listFiles();
|
||||
|
||||
for (File attachment : attachments) {
|
||||
@@ -459,7 +461,7 @@ public class AttachmentDatabase extends Database {
|
||||
throws MmsException
|
||||
{
|
||||
try {
|
||||
File partsDirectory = context.getDir("parts", Context.MODE_PRIVATE);
|
||||
File partsDirectory = context.getDir(DIRECTORY, Context.MODE_PRIVATE);
|
||||
File dataFile = File.createTempFile("part", ".mms", partsDirectory);
|
||||
return setAttachmentData(dataFile, in);
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -130,6 +130,14 @@ public class DatabaseFactory {
|
||||
return getInstance(context).sessionDatabase;
|
||||
}
|
||||
|
||||
public static SQLiteDatabase getBackupDatabase(Context context) {
|
||||
return getInstance(context).databaseHelper.getReadableDatabase();
|
||||
}
|
||||
|
||||
public static void upgradeRestored(Context context, SQLiteDatabase database){
|
||||
getInstance(context).databaseHelper.onUpgrade(database, database.getVersion(), -1);
|
||||
}
|
||||
|
||||
private DatabaseFactory(@NonNull Context context) {
|
||||
SQLiteDatabase.loadLibs(context);
|
||||
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
||||
import org.thoughtcrime.securesms.util.StorageUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PlaintextBackupExporter {
|
||||
|
||||
private static final String FILENAME = "SignalPlaintextBackup.xml";
|
||||
|
||||
public static void exportPlaintextToSd(Context context)
|
||||
throws NoExternalStorageException, IOException
|
||||
{
|
||||
exportPlaintext(context);
|
||||
}
|
||||
|
||||
public static File getPlaintextExportFile() throws NoExternalStorageException {
|
||||
return new File(StorageUtil.getBackupDir(), FILENAME);
|
||||
}
|
||||
|
||||
private static void exportPlaintext(Context context)
|
||||
throws NoExternalStorageException, IOException
|
||||
{
|
||||
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
||||
int count = database.getMessageCount();
|
||||
XmlBackup.Writer writer = new XmlBackup.Writer(getPlaintextExportFile().getAbsolutePath(), count);
|
||||
|
||||
|
||||
SmsMessageRecord record;
|
||||
|
||||
SmsDatabase.Reader reader = null;
|
||||
int skip = 0;
|
||||
int ROW_LIMIT = 500;
|
||||
|
||||
do {
|
||||
if (reader != null)
|
||||
reader.close();
|
||||
|
||||
reader = database.readerFor(database.getMessages(skip, ROW_LIMIT));
|
||||
|
||||
while ((record = reader.getNext()) != null) {
|
||||
XmlBackup.XmlBackupItem item =
|
||||
new XmlBackup.XmlBackupItem(0, record.getIndividualRecipient().getAddress().serialize(),
|
||||
record.getIndividualRecipient().getName(),
|
||||
record.getDateReceived(),
|
||||
MmsSmsColumns.Types.translateToSystemBaseType(record.getType()),
|
||||
null, record.getDisplayBody().toString(), null,
|
||||
1, record.getDeliveryStatus());
|
||||
|
||||
writer.writeItem(item);
|
||||
}
|
||||
|
||||
skip += ROW_LIMIT;
|
||||
} while (reader.getCount() > 0);
|
||||
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import net.sqlcipher.database.SQLiteDatabase;
|
||||
import net.sqlcipher.database.SQLiteStatement;
|
||||
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.StorageUtil;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.File;
|
||||
@@ -72,7 +73,7 @@ public class PlaintextBackupImporter {
|
||||
}
|
||||
|
||||
private static File getPlaintextExportFile() throws NoExternalStorageException {
|
||||
File backup = PlaintextBackupExporter.getPlaintextExportFile();
|
||||
File backup = new File(StorageUtil.getLegacyBackupDirectory(), "SignalPlaintextBackup.xml");
|
||||
File oldBackup = new File(Environment.getExternalStorageDirectory(), "TextSecurePlaintextBackup.xml");
|
||||
|
||||
return !backup.exists() && oldBackup.exists() ? oldBackup : backup;
|
||||
|
||||
Reference in New Issue
Block a user