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

@ -323,8 +323,8 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
restoreAccountStats(icicle);
}
}
@ -347,22 +347,19 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
outState.putSerializable(ACCOUNT_STATS, accountStats);
}
private StorageManager.StorageListener storageListener = new StorageManager.StorageListener()
{
@Override
public void onUnmount(String providerId)
{
refresh();
}
@Override
public void onMount(String providerId)
{
refresh();
}
};
private StorageManager.StorageListener storageListener = new StorageManager.StorageListener() {
@Override
public void onUnmount(String providerId) {
refresh();
}
@Override
public void onMount(String providerId) {
refresh();
}
};
@Override
public void onResume() {
super.onResume();
@ -377,7 +374,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
super.onPause();
MessagingController.getInstance(getApplication()).removeListener(mListener);
StorageManager.getInstance(getApplication()).removeListener(storageListener);
}
private void refresh() {
@ -481,7 +478,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
String toastText = getString(R.string.account_unavailable, account.getDescription());
Toast toast = Toast.makeText(getApplication(), toastText, Toast.LENGTH_SHORT);
toast.show();
Log.i(K9.LOG_TAG, "refusing to open account that is not available");
return false;
}
@ -856,13 +853,11 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
private void onImport(Uri uri) {
Log.i(K9.LOG_TAG, "onImport importing from URI " + 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
public void success(int numAccounts)
{
public void success(int numAccounts) {
mHandler.progress(false);
String messageText =
numAccounts != 1
@ -871,48 +866,42 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
showDialog(Accounts.this, R.string.settings_import_success_header, messageText);
runOnUiThread(new Runnable() {
@Override
public void run()
{
public void run() {
refresh();
}
});
}
@Override
public void failure(String message, Exception e)
{
public void failure(String message, Exception e) {
mHandler.progress(false);
showDialog(Accounts.this, R.string.settings_import_failed_header, Accounts.this.getString(R.string.settings_import_failure, fileName, e.getLocalizedMessage()));
}
@Override
public void canceled()
{
public void canceled() {
mHandler.progress(false);
}
@Override
public void started()
{
public void started() {
runOnUiThread(new Runnable() {
@Override
public void run()
{
public void run() {
mHandler.progress(true);
String toastText = Accounts.this.getString(R.string.settings_importing);
Toast toast = Toast.makeText(Accounts.this, toastText, Toast.LENGTH_SHORT);
toast.show();
}
});
}
});
}
private void showDialog(final Context context, final int headerRes, final String message) {
this.runOnUiThread(new Runnable() {
@Override
public void run()
{
public void run() {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(headerRes);
builder.setMessage(message);
@ -927,7 +916,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
}
});
}
class AccountsAdapter extends ArrayAdapter<BaseAccount> {
public AccountsAdapter(BaseAccount[] accounts) {
super(Accounts.this, 0, accounts);
@ -989,7 +978,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
holder.flaggedMessageCount.setOnClickListener(new AccountClickListener(account, SearchModifier.FLAGGED));
holder.newMessageCount.setOnClickListener(new AccountClickListener(account, SearchModifier.UNREAD));
view.getBackground().setAlpha(stats.available ? 0 : 127);
holder.activeIcons.setOnClickListener(new OnClickListener() {

View file

@ -63,7 +63,7 @@ public class AsyncUIProcessor {
);
}
public void importSettings(final Activity activity, final Uri uri, final ImportListener listener) {
threadPool.execute(new Runnable() {
@Override
@ -72,12 +72,11 @@ public class AsyncUIProcessor {
try {
ContentResolver resolver = mApplication.getContentResolver();
is = resolver.openInputStream(uri);
}
catch (Exception e) {
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception while resolving Uri to InputStream", e);
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
}
return;
}
final InputStream myIs = is;
@ -107,29 +106,26 @@ public class AsyncUIProcessor {
}
@Override
public void started()
{
public void started() {
if (listener != null) {
listener.started();
}
}
});
});
}
}
);
);
}
private void quietClose(InputStream is)
{
private void quietClose(InputStream is) {
if (is != null) {
try {
is.close();
}
catch (Exception e) {
} catch (Exception 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() {
@Override
public void canceled()
{
public void canceled() {
if (listener != null) {
listener.canceled();
}
}
@Override
public void failure(String message, Exception e)
{
public void failure(String message, Exception e) {
if (listener != null) {
listener.failure(message, e);
}
@ -37,16 +35,14 @@ public class ExportHelper {
}
@Override
public void started()
{
public void started() {
if (listener != null) {
listener.started();
}
activity.runOnUiThread(new Runnable() {
@Override
public void run()
{
public void run() {
String toastText = activity.getString(R.string.settings_exporting);
Toast toast = Toast.makeText(activity, toastText, Toast.LENGTH_SHORT);
toast.show();
@ -55,8 +51,7 @@ public class ExportHelper {
}
@Override
public void success(String fileName)
{
public void success(String fileName) {
if (listener != null) {
listener.success(fileName);
}
@ -64,19 +59,17 @@ public class ExportHelper {
}
@Override
public void success()
{
public void success() {
// This one should never be called here because the AsyncUIProcessor will generate a filename
}
});
}
private static void showDialog(final Activity activity, final int headerRes, final String message) {
activity.runOnUiThread(new Runnable() {
@Override
public void run()
{
public void run() {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(headerRes);
builder.setMessage(message);
@ -91,6 +84,6 @@ public class ExportHelper {
builder.show();
}
});
}
}

View file

@ -5,9 +5,9 @@ public interface ExportListener {
public void success();
public void failure(String message, Exception e);
public void canceled();
public void started();
}

View file

@ -4,9 +4,9 @@ public interface ImportListener {
public void success(int numAccounts);
public void failure(String message, Exception e);
public void canceled();
public void started();
}

View file

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

View file

@ -94,36 +94,30 @@ public class K9ListActivity extends ListActivity {
}
public void onExport(final Account account) {
ExportHelper.exportSettings(this, account, new ExportListener()
{
ExportHelper.exportSettings(this, account, new ExportListener() {
@Override
public void canceled()
{
public void canceled() {
setProgress(false);
}
@Override
public void failure(String message, Exception e)
{
public void failure(String message, Exception e) {
setProgress(false);
}
@Override
public void started()
{
public void started() {
setProgress(true);
}
@Override
public void success(String fileName)
{
public void success(String fileName) {
setProgress(false);
}
@Override
public void success()
{
public void success() {
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;
public interface IStorageExporter
{
public interface IStorageExporter {
public boolean needsKey();
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.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) {
try {
IStorageExporter storageExporter = StorageVersioning.createExporter(version);
@ -23,77 +22,71 @@ public class StorageExporter
}
if (storageExporter.needsKey() && encryptionKey == null) {
gatherPassword(activity, storageExporter, uuid, fileName, os, listener);
}
else
{
} else {
finishExport(activity, storageExporter, uuid, fileName, os, encryptionKey, listener);
}
}
catch (Exception e)
{
catch (Exception e) {
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
}
}
public static void exportPreferences(Activity activity, String version, String uuid, String fileName, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, uuid, fileName, null, encryptionKey, listener);
}
public static void exportPrefererences(Activity activity, String version, String uuid, OutputStream os, String encryptionKey, final ExportListener listener) throws StorageImportExportException {
exportPreferences(activity, version, uuid, null, os, encryptionKey, listener);
}
private static void gatherPassword(final Activity activity, final IStorageExporter storageExporter, final String uuid, final String fileName, final OutputStream os, final ExportListener listener) {
activity.runOnUiThread(new Runnable() {
@Override
public void run()
{
public void run() {
PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_encryption_password_prompt),
new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) {
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) {
@Override
public void run()
{
try {
finishExport(activity, storageExporter, uuid, fileName, os, chosenPassword, listener);
}
catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception while finishing export", e);
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
@Override
public void run() {
try {
finishExport(activity, storageExporter, uuid, fileName, os, chosenPassword, listener);
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception while finishing export", 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();
}
});
}
private static void finishExport(Activity activity, IStorageExporter storageExporter, String uuid, String fileName, OutputStream os, String encryptionKey, ExportListener listener) throws StorageImportExportException {
boolean needToClose = false;
if (listener != null) {
listener.started();
}
try {
// This needs to be after the password prompt. If the user cancels the password, we do not want
// This needs to be after the password prompt. If the user cancels the password, we do not want
// to create the file needlessly
if (os == null && fileName != null) {
needToClose = true;
@ -105,30 +98,25 @@ public class StorageExporter
if (listener != null) {
if (fileName != null) {
listener.success(fileName);
}
else {
} else {
listener.success();
}
}
}
else {
} else {
throw new StorageImportExportException("Internal error; no fileName or OutputStream", null);
}
}
catch (Exception e) {
} catch (Exception e) {
throw new StorageImportExportException(e.getLocalizedMessage(), e);
}
finally {
} finally {
if (needToClose && os != null) {
try {
os.close();
}
catch (Exception e) {
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Unable to close OutputStream", e);
}
}
}
}
}

View file

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

View file

@ -43,25 +43,21 @@ public class StorageImporter {
Log.i(K9.LOG_TAG, "Got settings file version " + version);
IStorageImporter storageImporter = StorageVersioning.createImporter(version);
if (storageImporter == null)
{
if (storageImporter == null) {
throw new StorageImportExportException(activity.getString(R.string.settings_unknown_version, version));
}
if (storageImporter.needsKey() && providedEncryptionKey == null) {
gatherPassword(activity, storageImporter, dataset, listener);
}
else {
} else {
finishImport(activity, storageImporter, dataset, providedEncryptionKey, listener);
}
}
catch (Exception e)
{
} catch (Exception e) {
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
}
}
private static void finishImport(Activity context, IStorageImporter storageImporter, ImportElement dataset, String encryptionKey, ImportListener listener) throws StorageImportExportException {
if (listener != null) {
listener.started();
@ -82,46 +78,42 @@ public class StorageImporter {
listener.success(numAccounts);
}
}
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
public void run()
{
public void run() {
PasswordEntryDialog dialog = new PasswordEntryDialog(activity, activity.getString(R.string.settings_encryption_password_prompt),
new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) {
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
@Override
public void run()
{
try {
finishImport(activity, storageImporter, dataset, chosenPassword, listener);
}
catch (Exception e) {
Log.w(K9.LOG_TAG, "Failure during import", e);
if (listener != null) {
listener.failure(e.getLocalizedMessage(), e);
}
}
new PasswordEntryDialog.PasswordEntryListener() {
public void passwordChosen(final String chosenPassword) {
AsyncUIProcessor.getInstance(activity.getApplication()).execute(new Runnable() {
@Override
public void run() {
try {
finishImport(activity, storageImporter, dataset, chosenPassword, listener);
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Failure during import", 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();
}
});
};
public static class ImportElement {
String name;

View file

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

View file

@ -4,32 +4,31 @@ import java.util.HashMap;
import java.util.Map;
public class StorageVersioning
{
public class StorageVersioning {
public enum STORAGE_VERSION {
VERSION1(StorageImporterVersion1.class, StorageExporterVersion1.class, true, STORAGE_VERSION_1);
private Class<? extends IStorageImporter> importerClass;
private Class<? extends IStorageExporter> exporterClass;
private Class <? extends IStorageImporter > importerClass;
private Class <? extends IStorageExporter > exporterClass;
private boolean needsKey;
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;
exporterClass = exclass;
needsKey = nk;
versionString = vs;
}
public Class<? extends IStorageImporter> getImporterClass() {
public Class <? extends IStorageImporter > getImporterClass() {
return importerClass;
}
public IStorageImporter createImporter() throws InstantiationException, IllegalAccessException {
IStorageImporter storageImporter = importerClass.newInstance();
return storageImporter;
}
public Class<? extends IStorageExporter> getExporterClass() {
public Class <? extends IStorageExporter > getExporterClass() {
return exporterClass;
}
public IStorageExporter createExporter() throws InstantiationException, IllegalAccessException {
@ -43,43 +42,37 @@ public class StorageVersioning
return versionString;
}
}
// Never, ever re-use these numbers!
private static final String STORAGE_VERSION_1 = "1";
public static Map<String, STORAGE_VERSION> versionMap = new HashMap<String, STORAGE_VERSION>();
static {
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);
if (storageVersion == null)
{
if (storageVersion == null) {
return null;
}
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);
if (storageVersion == null)
{
if (storageVersion == null) {
return null;
}
return storageVersion.createExporter();
}
public Boolean needsKey(String version)
{
public Boolean needsKey(String version) {
STORAGE_VERSION storageVersion = versionMap.get(version);
if (storageVersion == null)
{
if (storageVersion == null) {
return null;
}
return storageVersion.needsKey();
}
}