From a559a3524951ad1fa2207d296d40fb2f9c982c25 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 28 Mar 2011 01:18:36 +0200 Subject: [PATCH] Use AsyncTask instead of AsyncUIProcessor for export --- src/com/fsck/k9/activity/Accounts.java | 95 +++++++++++-------- .../fsck/k9/activity/AsyncUIProcessor.java | 29 ------ 2 files changed, 57 insertions(+), 67 deletions(-) diff --git a/src/com/fsck/k9/activity/Accounts.java b/src/com/fsck/k9/activity/Accounts.java index 714afbd26..8737c6c3f 100644 --- a/src/com/fsck/k9/activity/Accounts.java +++ b/src/com/fsck/k9/activity/Accounts.java @@ -1,6 +1,7 @@ package com.fsck.k9.activity; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -18,7 +19,9 @@ import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; +import android.os.Environment; import android.os.Handler; import android.util.Log; 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.MessagingListener; import com.fsck.k9.helper.SizeFormatter; +import com.fsck.k9.helper.Utility; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mail.store.StorageManager; import com.fsck.k9.view.ColorChip; +import com.fsck.k9.preferences.StorageExporter; 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: String storageFormat = StorageFormat.ENCRYPTED_XML_FILE; - AsyncUIProcessor.getInstance(this.getApplication()).exportSettings(this, storageFormat, includeGlobals, accountUuids, new ExportListener() { + new ExportAsyncTask(storageFormat, includeGlobals, accountUuids).execute(); + } - @Override - public void canceled() { - setProgress(false); + private class ExportAsyncTask extends AsyncTask { + private boolean mIncludeGlobals; + private Set mAccountUuids; + private String mStorageFormat; + private String mFileName; + + private ExportAsyncTask(String storageFormat, boolean includeGlobals, + Set accountUuids) { + mStorageFormat = storageFormat; + mIncludeGlobals = includeGlobals; + mAccountUuids = accountUuids; + } + + @Override + protected void onPreExecute() { + //TODO: show progress bar instead of displaying toast + String toastText = Accounts.this.getString(R.string.settings_exporting); + Toast toast = Toast.makeText(Accounts.this, toastText, Toast.LENGTH_SHORT); + 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 - public void failure(String message, Exception e) { - setProgress(false); - showDialog(Accounts.this, R.string.settings_export_failed_header, Accounts.this.getString(R.string.settings_export_failure, message)); + @Override + protected void onPostExecute(Boolean success) { + setProgress(false); + 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 started() { - setProgress(true); - runOnUiThread(new Runnable() { - - @Override - public void run() { - String toastText = Accounts.this.getString(R.string.settings_exporting); - Toast toast = Toast.makeText(Accounts.this, toastText, Toast.LENGTH_SHORT); - toast.show(); - } - }); - } - - @Override - public void success(String fileName) { - setProgress(false); - showDialog(Accounts.this, R.string.settings_export_success_header, Accounts.this.getString(R.string.settings_export_success, fileName)); - } - - @Override - public void success() { - // This one should never be called here because the AsyncUIProcessor will generate a filename - setProgress(false); - } - }); - - - + } } } diff --git a/src/com/fsck/k9/activity/AsyncUIProcessor.java b/src/com/fsck/k9/activity/AsyncUIProcessor.java index 0ab2971e7..a9108dcb5 100644 --- a/src/com/fsck/k9/activity/AsyncUIProcessor.java +++ b/src/com/fsck/k9/activity/AsyncUIProcessor.java @@ -1,22 +1,16 @@ package com.fsck.k9.activity; -import java.io.File; import java.io.InputStream; -import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.HashSet; import android.app.Activity; import android.app.Application; import android.content.ContentResolver; import android.net.Uri; -import android.os.Environment; import android.util.Log; import com.fsck.k9.K9; -import com.fsck.k9.helper.Utility; -import com.fsck.k9.preferences.StorageExporter; import com.fsck.k9.preferences.StorageImporter; /** @@ -42,29 +36,6 @@ public class AsyncUIProcessor { public void execute(Runnable runnable) { threadPool.execute(runnable); } - public void exportSettings(final Activity activity, final String storageFormat, final boolean includeGlobals, final Set 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) { threadPool.execute(new Runnable() {