mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-28 12:35:17 +00:00
aa25f94291
1) Allow imports from the stock SMS database at any time. 2) Provide plaintext export support, in a format compatible with the "SMS Backup And Restore" app. 3) Fix the DB weirdness on encrypted restore that previously required killing the app.
67 lines
2.2 KiB
Java
67 lines
2.2 KiB
Java
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
import android.content.Context;
|
|
import android.os.Environment;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
|
|
public class PlaintextBackupExporter {
|
|
|
|
public static void exportPlaintextToSd(Context context, MasterSecret masterSecret)
|
|
throws NoExternalStorageException, IOException
|
|
{
|
|
verifyExternalStorageForPlaintextExport();
|
|
exportPlaintext(context, masterSecret);
|
|
}
|
|
|
|
private static void verifyExternalStorageForPlaintextExport() throws NoExternalStorageException {
|
|
if (!Environment.getExternalStorageDirectory().canWrite())
|
|
throw new NoExternalStorageException();
|
|
}
|
|
|
|
private static String getPlaintextExportDirectoryPath() {
|
|
File sdDirectory = Environment.getExternalStorageDirectory();
|
|
return sdDirectory.getAbsolutePath() + File.separator + "TextSecurePlaintextBackup.xml";
|
|
}
|
|
|
|
private static void exportPlaintext(Context context, MasterSecret masterSecret)
|
|
throws IOException
|
|
{
|
|
int count = DatabaseFactory.getSmsDatabase(context).getMessageCount();
|
|
XmlBackup.Writer writer = new XmlBackup.Writer(getPlaintextExportDirectoryPath(), count);
|
|
|
|
|
|
SmsMessageRecord record;
|
|
EncryptingSmsDatabase.Reader reader = null;
|
|
int skip = 0;
|
|
int ROW_LIMIT = 500;
|
|
|
|
do {
|
|
if (reader != null)
|
|
reader.close();
|
|
|
|
reader = DatabaseFactory.getEncryptingSmsDatabase(context).getMessages(masterSecret, skip, ROW_LIMIT);
|
|
|
|
while ((record = reader.getNext()) != null) {
|
|
XmlBackup.XmlBackupItem item =
|
|
new XmlBackup.XmlBackupItem(0, record.getIndividualRecipient().getNumber(),
|
|
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();
|
|
}
|
|
}
|