2013-06-24 21:02:30 -07:00
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2017-03-29 00:20:35 +02:00
|
|
|
import org.thoughtcrime.securesms.util.StorageUtil;
|
2013-06-24 21:02:30 -07:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class PlaintextBackupExporter {
|
|
|
|
|
2016-02-19 12:14:17 +01:00
|
|
|
private static final String FILENAME = "SignalPlaintextBackup.xml";
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
public static void exportPlaintextToSd(Context context)
|
2013-06-24 21:02:30 -07:00
|
|
|
throws NoExternalStorageException, IOException
|
|
|
|
{
|
2018-01-24 19:17:44 -08:00
|
|
|
exportPlaintext(context);
|
2013-06-24 21:02:30 -07:00
|
|
|
}
|
|
|
|
|
2017-03-29 00:20:35 +02:00
|
|
|
public static File getPlaintextExportFile() throws NoExternalStorageException {
|
|
|
|
return new File(StorageUtil.getBackupDir(), FILENAME);
|
2013-06-24 21:02:30 -07:00
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
private static void exportPlaintext(Context context)
|
2017-03-29 00:20:35 +02:00
|
|
|
throws NoExternalStorageException, IOException
|
2013-06-24 21:02:30 -07:00
|
|
|
{
|
2018-01-24 19:17:44 -08:00
|
|
|
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
int count = database.getMessageCount();
|
|
|
|
XmlBackup.Writer writer = new XmlBackup.Writer(getPlaintextExportFile().getAbsolutePath(), count);
|
2013-06-24 21:02:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
SmsMessageRecord record;
|
2018-01-24 19:17:44 -08:00
|
|
|
|
|
|
|
SmsDatabase.Reader reader = null;
|
|
|
|
int skip = 0;
|
|
|
|
int ROW_LIMIT = 500;
|
2013-06-24 21:02:30 -07:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (reader != null)
|
|
|
|
reader.close();
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
reader = database.readerFor(database.getMessages(skip, ROW_LIMIT));
|
2013-06-24 21:02:30 -07:00
|
|
|
|
|
|
|
while ((record = reader.getNext()) != null) {
|
|
|
|
XmlBackup.XmlBackupItem item =
|
2017-07-26 09:59:15 -07:00
|
|
|
new XmlBackup.XmlBackupItem(0, record.getIndividualRecipient().getAddress().serialize(),
|
2017-03-27 16:41:16 -06:00
|
|
|
record.getIndividualRecipient().getName(),
|
2013-06-24 21:02:30 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|