Move to a Fragment world.

1) Start breaking the UI out into Fragments.
2) Switch to Cursor loaders from managed cursors.
3) Switch to inflated menu resources.
4) Break out some basic functionality into helper classes.
This commit is contained in:
Moxie Marlinspike
2012-07-18 15:35:13 -07:00
parent b93bb69c28
commit 3d9475676f
20 changed files with 1709 additions and 1173 deletions

View File

@@ -1,6 +1,6 @@
/**
/**
* 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
@@ -10,75 +10,85 @@
* 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 <http://www.gnu.org/licenses/>.
*/
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;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
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";
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();
!(new File(getExportDirectoryPath()).exists()))
throw new NoExternalStorageException();
}
private static void migrateFile(File from, File to) throws IOException {
if (from.exists()) {
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();
}
}
}
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<contents.length;i++) {
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());
}
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());
}
}
} else {
Log.w("ApplicationExporter", "Could not find directory: " + directory.getAbsolutePath());
@@ -88,40 +98,22 @@ public class ApplicationExporter {
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);
if (directory.exists()) {
importDirectory.mkdirs();
File[] contents = directory.listFiles();
for (int i=0;i<contents.length;i++) {
File exportedFile = contents[i];
if (exportedFile.isFile()) {
File localFile = new File(importDirectory.getAbsolutePath() + File.separator + exportedFile.getName());
migrateFile(exportedFile, localFile);
} else {
importDirectory(context, directoryName + File.separator + exportedFile.getName());
}
File exportedFile = contents[i];
if (exportedFile.isFile()) {
File localFile = new File(importDirectory.getAbsolutePath() + File.separator + exportedFile.getName());
migrateFile(exportedFile, localFile);
} else {
importDirectory(context, directoryName + File.separator + exportedFile.getName());
}
}
}
}
public static void exportToSd(Context context) throws NoExternalStorageException, IOException {
verifyExternalStorageForExport();
exportDirectory(context, "");
// exportDirectory(context, "databases");
// exportDirectory(context, "sessions");
// exportDirectory(context, "shared_prefs");
}
public static void importFromSd(Context context) throws NoExternalStorageException, IOException {
verifyExternalStorageForImport();
importDirectory(context, "");
// importDirectory(context, "databases");
// importDirectory(context, "sessions");
// importDirectory(context, "shared_prefs");
}
}

View File

@@ -0,0 +1,25 @@
package org.thoughtcrime.securesms.database.loaders;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.CursorLoader;
import org.thoughtcrime.securesms.database.DatabaseFactory;
public class ConversationListLoader extends CursorLoader {
private final String filter;
private final Context context;
public ConversationListLoader(Context context, String filter) {
super(context);
this.filter = filter;
this.context = context.getApplicationContext();
}
@Override
public Cursor loadInBackground() {
return DatabaseFactory.getThreadDatabase(context).getConversationList();
}
}