2012-07-18 22:35:13 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-18 22:35:13 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-18 22:35:13 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.channels.FileChannel;
|
|
|
|
|
2013-06-25 04:02:30 +00:00
|
|
|
public class EncryptedBackupExporter {
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-07-18 22:35:13 +00:00
|
|
|
public static void exportToSd(Context context) throws NoExternalStorageException, IOException {
|
|
|
|
verifyExternalStorageForExport();
|
|
|
|
exportDirectory(context, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void importFromSd(Context context) throws NoExternalStorageException, IOException {
|
|
|
|
verifyExternalStorageForImport();
|
|
|
|
importDirectory(context, "");
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static String getExportDirectoryPath() {
|
|
|
|
File sdDirectory = Environment.getExternalStorageDirectory();
|
2012-07-18 22:35:13 +00:00
|
|
|
return sdDirectory.getAbsolutePath() + File.separator + "TextSecureExport";
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static void verifyExternalStorageForExport() throws NoExternalStorageException {
|
|
|
|
if (!Environment.getExternalStorageDirectory().canWrite())
|
|
|
|
throw new NoExternalStorageException();
|
|
|
|
|
|
|
|
String exportDirectoryPath = getExportDirectoryPath();
|
|
|
|
File exportDirectory = new File(exportDirectoryPath);
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (!exportDirectory.exists())
|
|
|
|
exportDirectory.mkdir();
|
|
|
|
}
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static void verifyExternalStorageForImport() throws NoExternalStorageException {
|
|
|
|
if (!Environment.getExternalStorageDirectory().canRead() ||
|
2012-07-18 22:35:13 +00:00
|
|
|
!(new File(getExportDirectoryPath()).exists()))
|
|
|
|
throw new NoExternalStorageException();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2012-08-05 20:12:51 +00:00
|
|
|
private static void migrateFile(File from, File to) {
|
|
|
|
try {
|
|
|
|
if (from.exists()) {
|
|
|
|
FileChannel source = new FileInputStream(from).getChannel();
|
|
|
|
FileChannel destination = new FileOutputStream(to).getChannel();
|
|
|
|
|
|
|
|
destination.transferFrom(source, 0, source.size());
|
|
|
|
source.close();
|
|
|
|
destination.close();
|
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
2013-06-25 04:02:30 +00:00
|
|
|
Log.w("EncryptedBackupExporter", ioe);
|
2012-07-18 22:35:13 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static void exportDirectory(Context context, String directoryName) throws IOException {
|
|
|
|
File directory = new File(context.getFilesDir().getParent() + File.separatorChar + directoryName);
|
|
|
|
File exportDirectory = new File(getExportDirectoryPath() + File.separatorChar + directoryName);
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (directory.exists()) {
|
|
|
|
exportDirectory.mkdirs();
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
File[] contents = directory.listFiles();
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (int i=0;i<contents.length;i++) {
|
2012-07-18 22:35:13 +00:00
|
|
|
File localFile = contents[i];
|
|
|
|
|
|
|
|
if (localFile.isFile()) {
|
|
|
|
File exportedFile = new File(exportDirectory.getAbsolutePath() + File.separator + localFile.getName());
|
|
|
|
migrateFile(localFile, exportedFile);
|
|
|
|
} else {
|
|
|
|
exportDirectory(context, directoryName + File.separator + localFile.getName());
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-25 04:02:30 +00:00
|
|
|
Log.w("EncryptedBackupExporter", "Could not find directory: " + directory.getAbsolutePath());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void importDirectory(Context context, String directoryName) throws IOException {
|
|
|
|
File directory = new File(getExportDirectoryPath() + File.separator + directoryName);
|
|
|
|
File importDirectory = new File(context.getFilesDir().getParent() + File.separator + directoryName);
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2013-05-21 20:25:44 +00:00
|
|
|
if (directory.exists() && directory.isDirectory()) {
|
2011-12-20 18:20:44 +00:00
|
|
|
importDirectory.mkdirs();
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
File[] contents = directory.listFiles();
|
2012-07-18 22:35:13 +00:00
|
|
|
|
2012-08-05 20:12:51 +00:00
|
|
|
for (File exportedFile : contents) {
|
2012-07-18 22:35:13 +00:00
|
|
|
if (exportedFile.isFile()) {
|
|
|
|
File localFile = new File(importDirectory.getAbsolutePath() + File.separator + exportedFile.getName());
|
|
|
|
migrateFile(exportedFile, localFile);
|
2013-05-21 20:25:44 +00:00
|
|
|
} else if (exportedFile.isDirectory()) {
|
2012-07-18 22:35:13 +00:00
|
|
|
importDirectory(context, directoryName + File.separator + exportedFile.getName());
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|