Put long-running operation into AsyncTask

This commit is contained in:
yulin2 2014-02-16 17:10:44 -06:00 committed by cketti
parent abc765e893
commit fa7118dab3
1 changed files with 16 additions and 7 deletions

View File

@ -14,6 +14,7 @@ import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment; import android.os.Environment;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
@ -149,7 +150,7 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
contentType = MimeUtility.getMimeTypeForViewing(part.getMimeType(), name); contentType = MimeUtility.getMimeTypeForViewing(part.getMimeType(), name);
TextView attachmentName = (TextView) findViewById(R.id.attachment_name); TextView attachmentName = (TextView) findViewById(R.id.attachment_name);
TextView attachmentInfo = (TextView) findViewById(R.id.attachment_info); TextView attachmentInfo = (TextView) findViewById(R.id.attachment_info);
ImageView attachmentIcon = (ImageView) findViewById(R.id.attachment_icon); final ImageView attachmentIcon = (ImageView) findViewById(R.id.attachment_icon);
viewButton = (Button) findViewById(R.id.view); viewButton = (Button) findViewById(R.id.view);
downloadButton = (Button) findViewById(R.id.download); downloadButton = (Button) findViewById(R.id.download);
if ((!MimeUtility.mimeTypeMatches(contentType, K9.ACCEPTABLE_ATTACHMENT_VIEW_TYPES)) if ((!MimeUtility.mimeTypeMatches(contentType, K9.ACCEPTABLE_ATTACHMENT_VIEW_TYPES))
@ -171,12 +172,20 @@ public class AttachmentView extends FrameLayout implements OnClickListener, OnLo
attachmentName.setText(name); attachmentName.setText(name);
attachmentInfo.setText(SizeFormatter.formatSize(mContext, size)); attachmentInfo.setText(SizeFormatter.formatSize(mContext, size));
Bitmap previewIcon = getPreviewIcon(); new AsyncTask<Void, Void, Bitmap>() {
if (previewIcon != null) { protected Bitmap doInBackground(Void... asyncTaskArgs) {
attachmentIcon.setImageBitmap(previewIcon); Bitmap previewIcon = getPreviewIcon();
} else { return previewIcon;
attachmentIcon.setImageResource(R.drawable.attached_image_placeholder); }
}
protected void onPostExecute(Bitmap previewIcon) {
if (previewIcon != null) {
attachmentIcon.setImageBitmap(previewIcon);
} else {
attachmentIcon.setImageResource(R.drawable.attached_image_placeholder);
}
}
}.execute();
return firstClassAttachment; return firstClassAttachment;
} }