/** * Copyright (C) 2011 Whisper Systems * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package org.thoughtcrime.securesms.database; import android.content.Context; import android.os.Environment; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class ApplicationExporter { public static void exportToSd(Context context) throws NoExternalStorageException, IOException { verifyExternalStorageForExport(); exportDirectory(context, ""); } public static void importFromSd(Context context) throws NoExternalStorageException, IOException { verifyExternalStorageForImport(); importDirectory(context, ""); } private static String getExportDirectoryPath() { File sdDirectory = Environment.getExternalStorageDirectory(); return sdDirectory.getAbsolutePath() + File.separator + "TextSecureExport"; } private static void verifyExternalStorageForExport() throws NoExternalStorageException { if (!Environment.getExternalStorageDirectory().canWrite()) throw new NoExternalStorageException(); String exportDirectoryPath = getExportDirectoryPath(); File exportDirectory = new File(exportDirectoryPath); if (!exportDirectory.exists()) exportDirectory.mkdir(); } private static void verifyExternalStorageForImport() throws NoExternalStorageException { if (!Environment.getExternalStorageDirectory().canRead() || !(new File(getExportDirectoryPath()).exists())) throw new NoExternalStorageException(); } 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) { Log.w("ApplicationExporter", ioe); } } 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); if (directory.exists()) { exportDirectory.mkdirs(); File[] contents = directory.listFiles(); for (int i=0;i