This commit is contained in:
danapple 2011-03-20 15:21:24 -05:00
parent 2e7a785209
commit 8e5c50a8ef
14 changed files with 1010 additions and 1074 deletions

View file

@ -347,21 +347,18 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
outState.putSerializable(ACCOUNT_STATS, accountStats); outState.putSerializable(ACCOUNT_STATS, accountStats);
} }
private StorageManager.StorageListener storageListener = new StorageManager.StorageListener() private StorageManager.StorageListener storageListener = new StorageManager.StorageListener() {
{
@Override @Override
public void onUnmount(String providerId) public void onUnmount(String providerId) {
{ refresh();
refresh(); }
}
@Override @Override
public void onMount(String providerId) public void onMount(String providerId) {
{ refresh();
refresh(); }
} };
};
@Override @Override
public void onResume() { public void onResume() {
@ -858,11 +855,9 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
Log.i(K9.LOG_TAG, "onImport importing from URI " + uri.getPath()); Log.i(K9.LOG_TAG, "onImport importing from URI " + uri.getPath());
final String fileName = uri.getPath(); final String fileName = uri.getPath();
AsyncUIProcessor.getInstance(Accounts.this.getApplication()).importSettings(this, uri, new ImportListener() AsyncUIProcessor.getInstance(Accounts.this.getApplication()).importSettings(this, uri, new ImportListener() {
{
@Override @Override
public void success(int numAccounts) public void success(int numAccounts) {
{
mHandler.progress(false); mHandler.progress(false);
String messageText = String messageText =
numAccounts != 1 numAccounts != 1
@ -871,33 +866,28 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
showDialog(Accounts.this, R.string.settings_import_success_header, messageText); showDialog(Accounts.this, R.string.settings_import_success_header, messageText);
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() public void run() {
{
refresh(); refresh();
} }
}); });
} }
@Override @Override
public void failure(String message, Exception e) public void failure(String message, Exception e) {
{
mHandler.progress(false); mHandler.progress(false);
showDialog(Accounts.this, R.string.settings_import_failed_header, Accounts.this.getString(R.string.settings_import_failure, fileName, e.getLocalizedMessage())); showDialog(Accounts.this, R.string.settings_import_failed_header, Accounts.this.getString(R.string.settings_import_failure, fileName, e.getLocalizedMessage()));
} }
@Override @Override
public void canceled() public void canceled() {
{
mHandler.progress(false); mHandler.progress(false);
} }
@Override @Override
public void started() public void started() {
{
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() public void run() {
{
mHandler.progress(true); mHandler.progress(true);
String toastText = Accounts.this.getString(R.string.settings_importing); String toastText = Accounts.this.getString(R.string.settings_importing);
Toast toast = Toast.makeText(Accounts.this, toastText, Toast.LENGTH_SHORT); Toast toast = Toast.makeText(Accounts.this, toastText, Toast.LENGTH_SHORT);
@ -911,8 +901,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
private void showDialog(final Context context, final int headerRes, final String message) { private void showDialog(final Context context, final int headerRes, final String message) {
this.runOnUiThread(new Runnable() { this.runOnUiThread(new Runnable() {
@Override @Override
public void run() public void run() {
{
final AlertDialog.Builder builder = new AlertDialog.Builder(context); final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(headerRes); builder.setTitle(headerRes);
builder.setMessage(message); builder.setMessage(message);

View file

@ -72,8 +72,7 @@ public class AsyncUIProcessor {
try { try {
ContentResolver resolver = mApplication.getContentResolver(); ContentResolver resolver = mApplication.getContentResolver();
is = resolver.openInputStream(uri); is = resolver.openInputStream(uri);
} } catch (Exception e) {
catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception while resolving Uri to InputStream", e); Log.w(K9.LOG_TAG, "Exception while resolving Uri to InputStream", e);
if (listener != null) { if (listener != null) {
listener.failure(e.getLocalizedMessage(), e); listener.failure(e.getLocalizedMessage(), e);
@ -107,8 +106,7 @@ public class AsyncUIProcessor {
} }
@Override @Override
public void started() public void started() {
{
if (listener != null) { if (listener != null) {
listener.started(); listener.started();
} }
@ -116,16 +114,14 @@ public class AsyncUIProcessor {
}); });
} }
} }
); );
} }
private void quietClose(InputStream is) private void quietClose(InputStream is) {
{
if (is != null) { if (is != null) {
try { try {
is.close(); is.close();
} } catch (Exception e) {
catch (Exception e) {
Log.w(K9.LOG_TAG, "Unable to close inputStream", e); Log.w(K9.LOG_TAG, "Unable to close inputStream", e);
} }
} }

View file

@ -20,16 +20,14 @@ public class ExportHelper {
AsyncUIProcessor.getInstance(activity.getApplication()).exportSettings(activity, version, uuid, new ExportListener() { AsyncUIProcessor.getInstance(activity.getApplication()).exportSettings(activity, version, uuid, new ExportListener() {
@Override @Override
public void canceled() public void canceled() {
{
if (listener != null) { if (listener != null) {
listener.canceled(); listener.canceled();
} }
} }
@Override @Override
public void failure(String message, Exception e) public void failure(String message, Exception e) {
{
if (listener != null) { if (listener != null) {
listener.failure(message, e); listener.failure(message, e);
} }
@ -37,16 +35,14 @@ public class ExportHelper {
} }
@Override @Override
public void started() public void started() {
{
if (listener != null) { if (listener != null) {
listener.started(); listener.started();
} }
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() public void run() {
{
String toastText = activity.getString(R.string.settings_exporting); String toastText = activity.getString(R.string.settings_exporting);
Toast toast = Toast.makeText(activity, toastText, Toast.LENGTH_SHORT); Toast toast = Toast.makeText(activity, toastText, Toast.LENGTH_SHORT);
toast.show(); toast.show();
@ -55,8 +51,7 @@ public class ExportHelper {
} }
@Override @Override
public void success(String fileName) public void success(String fileName) {
{
if (listener != null) { if (listener != null) {
listener.success(fileName); listener.success(fileName);
} }
@ -64,8 +59,7 @@ public class ExportHelper {
} }
@Override @Override
public void success() public void success() {
{
// This one should never be called here because the AsyncUIProcessor will generate a filename // This one should never be called here because the AsyncUIProcessor will generate a filename
} }
}); });
@ -75,8 +69,7 @@ public class ExportHelper {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() public void run() {
{
final AlertDialog.Builder builder = new AlertDialog.Builder(activity); final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(headerRes); builder.setTitle(headerRes);
builder.setMessage(message); builder.setMessage(message);

View file

@ -167,36 +167,30 @@ public class K9Activity extends Activity {
} }
public void onExport(final Account account) { public void onExport(final Account account) {
ExportHelper.exportSettings(this, account, new ExportListener() ExportHelper.exportSettings(this, account, new ExportListener() {
{
@Override @Override
public void canceled() public void canceled() {
{
setProgress(false); setProgress(false);
} }
@Override @Override
public void failure(String message, Exception e) public void failure(String message, Exception e) {
{
setProgress(false); setProgress(false);
} }
@Override @Override
public void started() public void started() {
{
setProgress(true); setProgress(true);
} }
@Override @Override
public void success(String fileName) public void success(String fileName) {
{
setProgress(false); setProgress(false);
} }
@Override @Override
public void success() public void success() {
{
setProgress(false); setProgress(false);
} }
}); });

View file

@ -94,36 +94,30 @@ public class K9ListActivity extends ListActivity {
} }
public void onExport(final Account account) { public void onExport(final Account account) {
ExportHelper.exportSettings(this, account, new ExportListener() ExportHelper.exportSettings(this, account, new ExportListener() {
{
@Override @Override
public void canceled() public void canceled() {
{
setProgress(false); setProgress(false);
} }
@Override @Override
public void failure(String message, Exception e) public void failure(String message, Exception e) {
{
setProgress(false); setProgress(false);
} }
@Override @Override
public void started() public void started() {
{
setProgress(true); setProgress(true);
} }
@Override @Override
public void success(String fileName) public void success(String fileName) {
{
setProgress(false); setProgress(false);
} }
@Override @Override
public void success() public void success() {
{
setProgress(false); setProgress(false);
} }
}); });

File diff suppressed because it is too large Load diff

View file

@ -4,8 +4,7 @@ import java.io.OutputStream;
import android.content.Context; import android.content.Context;
public interface IStorageExporter public interface IStorageExporter {
{
public boolean needsKey(); public boolean needsKey();
public void exportPreferences(Context context, String uuid, OutputStream os, String encryptionKey) throws StorageImportExportException; public void exportPreferences(Context context, String uuid, OutputStream os, String encryptionKey) throws StorageImportExportException;
} }

View file

@ -13,8 +13,7 @@ import com.fsck.k9.activity.AsyncUIProcessor;
import com.fsck.k9.activity.ExportListener; import com.fsck.k9.activity.ExportListener;
import com.fsck.k9.activity.PasswordEntryDialog; import com.fsck.k9.activity.PasswordEntryDialog;
public class StorageExporter public class StorageExporter {
{
private static void exportPreferences(Activity activity, String version, String uuid, String fileName, OutputStream os, String encryptionKey, final ExportListener listener) { private static void exportPreferences(Activity activity, String version, String uuid, String fileName, OutputStream os, String encryptionKey, final ExportListener listener) {
try { try {
IStorageExporter storageExporter = StorageVersioning.createExporter(version); IStorageExporter storageExporter = StorageVersioning.createExporter(version);
@ -23,15 +22,12 @@ public class StorageExporter
} }
if (storageExporter.needsKey() && encryptionKey == null) { if (storageExporter.needsKey() && encryptionKey == null) {
gatherPassword(activity, storageExporter, uuid, fileName, os, listener); gatherPassword(activity, storageExporter, uuid, fileName, os, listener);
} } else {
else
{
finishExport(activity, storageExporter, uuid, fileName, os, encryptionKey, listener); finishExport(activity, storageExporter, uuid, fileName, os, encryptionKey, listener);
} }
} }
catch (Exception e) catch (Exception e) {
{
if (listener != null) { if (listener != null) {
listener.failure(e.getLocalizedMessage(), e); listener.failure(e.getLocalizedMessage(), e);
} }
@ -50,38 +46,35 @@ public class StorageExporter
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() public void run() {
{
PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_encryption_password_prompt), PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_encryption_password_prompt),
new PasswordEntryDialog.PasswordEntryListener() { new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) { public void passwordChosen(final String chosenPassword) {
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() { AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
@Override @Override
public void run() public void run() {
{ try {
try { finishExport(activity, storageExporter, uuid, fileName, os, chosenPassword, listener);
finishExport(activity, storageExporter, uuid, fileName, os, chosenPassword, listener); } catch (Exception e) {
} Log.w(K9.LOG_TAG, "Exception while finishing export", e);
catch (Exception e) { if (listener != null) {
Log.w(K9.LOG_TAG, "Exception while finishing export", e); listener.failure(e.getLocalizedMessage(), e);
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
} }
} }
});
}
public void cancel() {
if (listener != null) {
listener.canceled();
} }
});
}
public void cancel() {
if (listener != null) {
listener.canceled();
} }
}); }
dialog.show(); });
dialog.show();
} }
}); });
} }
@ -105,25 +98,20 @@ public class StorageExporter
if (listener != null) { if (listener != null) {
if (fileName != null) { if (fileName != null) {
listener.success(fileName); listener.success(fileName);
} } else {
else {
listener.success(); listener.success();
} }
} }
} } else {
else {
throw new StorageImportExportException("Internal error; no fileName or OutputStream", null); throw new StorageImportExportException("Internal error; no fileName or OutputStream", null);
} }
} } catch (Exception e) {
catch (Exception e) {
throw new StorageImportExportException(e.getLocalizedMessage(), e); throw new StorageImportExportException(e.getLocalizedMessage(), e);
} } finally {
finally {
if (needToClose && os != null) { if (needToClose && os != null) {
try { try {
os.close(); os.close();
} } catch (Exception e) {
catch (Exception e) {
Log.w(K9.LOG_TAG, "Unable to close OutputStream", e); Log.w(K9.LOG_TAG, "Unable to close OutputStream", e);
} }
} }

View file

@ -85,8 +85,7 @@ public class StorageExporterVersion1 implements IStorageExporter {
} }
@Override @Override
public boolean needsKey() public boolean needsKey() {
{
return true; return true;
} }
} }

View file

@ -43,19 +43,15 @@ public class StorageImporter {
Log.i(K9.LOG_TAG, "Got settings file version " + version); Log.i(K9.LOG_TAG, "Got settings file version " + version);
IStorageImporter storageImporter = StorageVersioning.createImporter(version); IStorageImporter storageImporter = StorageVersioning.createImporter(version);
if (storageImporter == null) if (storageImporter == null) {
{
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, version)); throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, version));
} }
if (storageImporter.needsKey() && providedEncryptionKey == null) { if (storageImporter.needsKey() && providedEncryptionKey == null) {
gatherPassword(activity, storageImporter, dataset, listener); gatherPassword(activity, storageImporter, dataset, listener);
} } else {
else {
finishImport(activity, storageImporter, dataset, providedEncryptionKey, listener); finishImport(activity, storageImporter, dataset, providedEncryptionKey, listener);
} }
} } catch (Exception e) {
catch (Exception e)
{
if (listener != null) { if (listener != null) {
listener.failure(e.getLocalizedMessage(), e); listener.failure(e.getLocalizedMessage(), e);
} }
@ -84,37 +80,33 @@ public class StorageImporter {
} }
private static void gatherPassword(final Activity activity, final IStorageImporter storageImporter, final ImportElement dataset, final ImportListener listener) { private static void gatherPassword(final Activity activity, final IStorageImporter storageImporter, final ImportElement dataset, final ImportListener listener) {
activity.runOnUiThread(new Runnable() activity.runOnUiThread(new Runnable() {
{
@Override @Override
public void run() public void run() {
{
PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_encryption_password_prompt), PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_encryption_password_prompt),
new PasswordEntryDialog.PasswordEntryListener() { new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) { public void passwordChosen(final String chosenPassword) {
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() { AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
@Override @Override
public void run() public void run() {
{ try {
try { finishImport(activity, storageImporter, dataset, chosenPassword, listener);
finishImport(activity, storageImporter, dataset, chosenPassword, listener); } catch (Exception e) {
} Log.w(K9.LOG_TAG, "Failure during import", e);
catch (Exception e) { if (listener != null) {
Log.w(K9.LOG_TAG, "Failure during import", e); listener.failure(e.getLocalizedMessage(), e);
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
}
} }
});
}
public void cancel() {
if (listener != null) {
listener.canceled();
} }
} }
}); });
}
public void cancel() {
if (listener != null) {
listener.canceled();
}
}
});
dialog.show(); dialog.show();
} }

View file

@ -88,8 +88,7 @@ public class StorageImporterVersion1 implements IStorageImporter {
} }
@Override @Override
public boolean needsKey() public boolean needsKey() {
{
return true; return true;
} }
} }

View file

@ -4,32 +4,31 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class StorageVersioning public class StorageVersioning {
{
public enum STORAGE_VERSION { public enum STORAGE_VERSION {
VERSION1(StorageImporterVersion1.class, StorageExporterVersion1.class, true, STORAGE_VERSION_1); VERSION1(StorageImporterVersion1.class, StorageExporterVersion1.class, true, STORAGE_VERSION_1);
private Class<? extends IStorageImporter> importerClass; private Class <? extends IStorageImporter > importerClass;
private Class<? extends IStorageExporter> exporterClass; private Class <? extends IStorageExporter > exporterClass;
private boolean needsKey; private boolean needsKey;
private String versionString; private String versionString;
private STORAGE_VERSION(Class<? extends IStorageImporter> imclass, Class<? extends IStorageExporter> exclass, boolean nk, String vs) { private STORAGE_VERSION(Class <? extends IStorageImporter > imclass, Class <? extends IStorageExporter > exclass, boolean nk, String vs) {
importerClass = imclass; importerClass = imclass;
exporterClass = exclass; exporterClass = exclass;
needsKey = nk; needsKey = nk;
versionString = vs; versionString = vs;
} }
public Class<? extends IStorageImporter> getImporterClass() { public Class <? extends IStorageImporter > getImporterClass() {
return importerClass; return importerClass;
} }
public IStorageImporter createImporter() throws InstantiationException, IllegalAccessException { public IStorageImporter createImporter() throws InstantiationException, IllegalAccessException {
IStorageImporter storageImporter = importerClass.newInstance(); IStorageImporter storageImporter = importerClass.newInstance();
return storageImporter; return storageImporter;
} }
public Class<? extends IStorageExporter> getExporterClass() { public Class <? extends IStorageExporter > getExporterClass() {
return exporterClass; return exporterClass;
} }
public IStorageExporter createExporter() throws InstantiationException, IllegalAccessException { public IStorageExporter createExporter() throws InstantiationException, IllegalAccessException {
@ -52,31 +51,25 @@ public class StorageVersioning
versionMap.put(STORAGE_VERSION.VERSION1.getVersionString(), STORAGE_VERSION.VERSION1); versionMap.put(STORAGE_VERSION.VERSION1.getVersionString(), STORAGE_VERSION.VERSION1);
} }
public static IStorageImporter createImporter(String version) throws InstantiationException, IllegalAccessException public static IStorageImporter createImporter(String version) throws InstantiationException, IllegalAccessException {
{
STORAGE_VERSION storageVersion = versionMap.get(version); STORAGE_VERSION storageVersion = versionMap.get(version);
if (storageVersion == null) if (storageVersion == null) {
{
return null; return null;
} }
return storageVersion.createImporter(); return storageVersion.createImporter();
} }
public static IStorageExporter createExporter(String version) throws InstantiationException, IllegalAccessException public static IStorageExporter createExporter(String version) throws InstantiationException, IllegalAccessException {
{
STORAGE_VERSION storageVersion = versionMap.get(version); STORAGE_VERSION storageVersion = versionMap.get(version);
if (storageVersion == null) if (storageVersion == null) {
{
return null; return null;
} }
return storageVersion.createExporter(); return storageVersion.createExporter();
} }
public Boolean needsKey(String version) public Boolean needsKey(String version) {
{
STORAGE_VERSION storageVersion = versionMap.get(version); STORAGE_VERSION storageVersion = versionMap.get(version);
if (storageVersion == null) if (storageVersion == null) {
{
return null; return null;
} }
return storageVersion.needsKey(); return storageVersion.needsKey();