1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-24 02:12:15 -05:00

Use AsyncTask instead of AsyncUIProcessor for export

This commit is contained in:
cketti 2011-03-28 01:18:36 +02:00
parent ad4c7f7071
commit a559a35249
2 changed files with 57 additions and 67 deletions

View File

@ -1,6 +1,7 @@
package com.fsck.k9.activity; package com.fsck.k9.activity;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@ -18,7 +19,9 @@ import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
@ -57,10 +60,12 @@ import com.fsck.k9.activity.setup.Prefs;
import com.fsck.k9.controller.MessagingController; import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener; import com.fsck.k9.controller.MessagingListener;
import com.fsck.k9.helper.SizeFormatter; import com.fsck.k9.helper.SizeFormatter;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mail.internet.MimeUtility;
import com.fsck.k9.mail.store.StorageManager; import com.fsck.k9.mail.store.StorageManager;
import com.fsck.k9.view.ColorChip; import com.fsck.k9.view.ColorChip;
import com.fsck.k9.preferences.StorageExporter;
import com.fsck.k9.preferences.StorageFormat; import com.fsck.k9.preferences.StorageFormat;
@ -1123,48 +1128,62 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
// Once there are more file formats, build a UI to select which one to use. For now, use the encrypted/encoded format: // Once there are more file formats, build a UI to select which one to use. For now, use the encrypted/encoded format:
String storageFormat = StorageFormat.ENCRYPTED_XML_FILE; String storageFormat = StorageFormat.ENCRYPTED_XML_FILE;
AsyncUIProcessor.getInstance(this.getApplication()).exportSettings(this, storageFormat, includeGlobals, accountUuids, new ExportListener() { new ExportAsyncTask(storageFormat, includeGlobals, accountUuids).execute();
}
@Override private class ExportAsyncTask extends AsyncTask<Void, Void, Boolean> {
public void canceled() { private boolean mIncludeGlobals;
setProgress(false); private Set<String> mAccountUuids;
private String mStorageFormat;
private String mFileName;
private ExportAsyncTask(String storageFormat, boolean includeGlobals,
Set<String> accountUuids) {
mStorageFormat = storageFormat;
mIncludeGlobals = includeGlobals;
mAccountUuids = accountUuids;
} }
@Override @Override
public void failure(String message, Exception e) { protected void onPreExecute() {
setProgress(false); //TODO: show progress bar instead of displaying toast
showDialog(Accounts.this, R.string.settings_export_failed_header, Accounts.this.getString(R.string.settings_export_failure, message));
}
@Override
public void started() {
setProgress(true);
runOnUiThread(new Runnable() {
@Override
public void run() {
String toastText = Accounts.this.getString(R.string.settings_exporting); String toastText = Accounts.this.getString(R.string.settings_exporting);
Toast toast = Toast.makeText(Accounts.this, toastText, Toast.LENGTH_SHORT); Toast toast = Toast.makeText(Accounts.this, toastText, Toast.LENGTH_SHORT);
toast.show(); toast.show();
} }
});
@Override
protected Boolean doInBackground(Void... params) {
try {
//FIXME: this code doesn't belong here!
// Do not store with application files. Settings exports should *not* be
// deleted when the application is uninstalled.
File dir = new File(Environment.getExternalStorageDirectory() + File.separator
+ Accounts.this.getApplication().getPackageName());
dir.mkdirs();
File file = Utility.createUniqueFile(dir, "settings.k9s");
mFileName = file.getAbsolutePath();
StorageExporter.exportPreferences(Accounts.this, mStorageFormat, mIncludeGlobals,
mAccountUuids, mFileName, null, null);
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception during export", e);
return false;
}
return true;
} }
@Override @Override
public void success(String fileName) { protected void onPostExecute(Boolean success) {
setProgress(false); setProgress(false);
showDialog(Accounts.this, R.string.settings_export_success_header, Accounts.this.getString(R.string.settings_export_success, fileName)); if (success) {
showDialog(Accounts.this, R.string.settings_export_success_header,
Accounts.this.getString(R.string.settings_export_success, mFileName));
} else {
//TODO: make the exporter return an error code; translate that error code to a localized string here
showDialog(Accounts.this, R.string.settings_export_failed_header,
Accounts.this.getString(R.string.settings_export_failure, "Something went wrong"));
} }
@Override
public void success() {
// This one should never be called here because the AsyncUIProcessor will generate a filename
setProgress(false);
} }
});
} }
} }

View File

@ -1,22 +1,16 @@
package com.fsck.k9.activity; package com.fsck.k9.activity;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.util.Set;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.HashSet;
import android.app.Activity; import android.app.Activity;
import android.app.Application; import android.app.Application;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.net.Uri; import android.net.Uri;
import android.os.Environment;
import android.util.Log; import android.util.Log;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.helper.Utility;
import com.fsck.k9.preferences.StorageExporter;
import com.fsck.k9.preferences.StorageImporter; import com.fsck.k9.preferences.StorageImporter;
/** /**
@ -42,29 +36,6 @@ public class AsyncUIProcessor {
public void execute(Runnable runnable) { public void execute(Runnable runnable) {
threadPool.execute(runnable); threadPool.execute(runnable);
} }
public void exportSettings(final Activity activity, final String storageFormat, final boolean includeGlobals, final Set<String> accountUuids, final ExportListener listener) {
threadPool.execute(new Runnable() {
@Override
public void run() {
try {
// Do not store with application files. Settings exports should *not* be
// deleted when the application is uninstalled
File dir = new File(Environment.getExternalStorageDirectory() + File.separator
+ mApplication.getPackageName());
dir.mkdirs();
File file = Utility.createUniqueFile(dir, "settings.k9s");
String fileName = file.getAbsolutePath();
StorageExporter.exportPreferences(activity, storageFormat, includeGlobals, accountUuids, fileName, null, listener);
} catch (Exception e) {
Log.w(K9.LOG_TAG, "Exception during export", e);
listener.failure(e.getLocalizedMessage(), e);
}
}
}
);
}
public void importSettings(final Activity activity, final Uri uri, final ImportListener listener) { public void importSettings(final Activity activity, final Uri uri, final ImportListener listener) {
threadPool.execute(new Runnable() { threadPool.execute(new Runnable() {