1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-04 16:45:09 -05:00

Save attachments in background thread

This commit is contained in:
cketti 2015-01-19 23:35:14 +01:00
parent a7b16c1210
commit da51bdf1b3

View File

@ -138,19 +138,7 @@ public class AttachmentController {
}
private void saveLocalAttachmentTo(File directory) {
//FIXME: write file in background thread
try {
File file = saveAttachmentWithUniqueFileName(directory);
displayAttachmentSavedMessage(file.toString());
MediaScannerNotifier.notify(context, file);
} catch (IOException ioe) {
if (K9.DEBUG) {
Log.e(K9.LOG_TAG, "Error saving attachment", ioe);
}
displayAttachmentNotSavedMessage();
}
new SaveAttachmentAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, directory);
}
private File saveAttachmentWithUniqueFileName(File directory) throws IOException {
@ -346,4 +334,36 @@ public class AttachmentController {
}
}
}
private class SaveAttachmentAsyncTask extends AsyncTask<File, Void, File> {
@Override
protected void onPreExecute() {
messageViewFragment.disableAttachmentButtons(attachment);
}
@Override
protected File doInBackground(File... params) {
try {
File directory = params[0];
return saveAttachmentWithUniqueFileName(directory);
} catch (IOException e) {
if (K9.DEBUG) {
Log.e(K9.LOG_TAG, "Error saving attachment", e);
}
return null;
}
}
@Override
protected void onPostExecute(File file) {
messageViewFragment.enableAttachmentButtons(attachment);
if (file != null) {
displayAttachmentSavedMessage(file.toString());
MediaScannerNotifier.notify(context, file);
} else {
displayAttachmentNotSavedMessage();
}
}
}
}