Back to project page iPhoroidUI.
The source code is released under:
Apache License
If you think the Android project iPhoroidUI listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (c) 2011 by KLab Inc., All rights reserved. *//from w w w. j a va 2s . c o m * Programmed by iphoroid team */ package org.klab.iphoroid.widget.support; import java.io.IOException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.util.Log; import android.widget.ImageView; /** * ImageDownloadTask. * * @author <a href="mailto:kodama-t@klab.jp">Takuya KODAMA</a> (kodamta-t) * @author <a href="mailto:sano-n@klab.jp">Naohide Sano</a> (sano-n) */ public final class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> { /** */ public static interface ImageDownloadHelper<T> { /** * ???????????????? imageView ????????????????????????????????? * @param imageView */ void onPreDownload(ImageView imageView); /** * ????? Bitmap ?????????????????????????????????? * @param param */ Bitmap doDownload(T param) throws IOException; /** * ????????????????????????????? imageView ??? Bitmap ??????????????????????????????? * @param imageView */ void onDownloadSuccess(ImageView imageView); /** * ?????????????????????????? imageView ??? no image ????????????????????????????????? * @param imageView */ void onDownloadFailure(ImageView imageView); } /** */ public static class DefaultImageDownloadHelper implements ImageDownloadHelper<String> { /** ??????????????????????? */ private Drawable loadingDrawable; /** ?????????????????????????????????????????? */ private Drawable noImageDrawable; /** * @param loadingDrawable ??????????????????????? * @param noImageDrawable ?????????????????????????????????????????? */ public DefaultImageDownloadHelper(Drawable loadingDrawable, Drawable noImageDrawable) { this.loadingDrawable = loadingDrawable; this.noImageDrawable = noImageDrawable; } /* ???????????? imageView ??????????????? */ public void onPreDownload(ImageView imageView) { imageView.setImageDrawable(loadingDrawable); } /* ????????????? */ public void onDownloadSuccess(ImageView imageView) { } public void onDownloadFailure(ImageView imageView) { imageView.setImageDrawable(noImageDrawable); } /* ?????????? Bitmap ???????????????? */ public Bitmap doDownload(String param) throws IOException { return BitmapFactory.decodeFile(param); } } private ImageView imageView; /** ????????????? (kodama-t ????????????????????) */ private Integer tag; static int queueInCount; static int executeCount; static int cancelCount; static int doneCount; static int errorCount; static int runningCount; /** */ private ImageDownloadHelper<String> imageDownloadHelper; /** * @param imageView to be set after download, should be set position to tag */ public ImageDownloadTask(ImageView imageView, ImageDownloadHelper<String> imageDownloadHelper) { this.imageView = imageView; this.tag = (Integer) (imageView.getTag()); // ??????? tag ??????????????????????????????? this.imageDownloadHelper = imageDownloadHelper; ++queueInCount; ++runningCount; } @Override protected void onPreExecute() { imageDownloadHelper.onPreDownload(imageView); super.onPreExecute(); } /** * ????????????????????????????????????? * * @param urls [0] image URL * @return null when download failed or canceled * @see DetailImageDownloadTask */ @Override protected Bitmap doInBackground(String... urls) { try { if (tag != null && tag.equals(imageView.getTag())) { // HasImage ?????? Cache ????????????????? // ????????????????????????????????????????????? Bitmap image = ImageCache.getImage(urls[0]); if (image == null) { Log.i("ImageDownloadTask", "loading: " + tag + ", " + urls[0]); image = imageDownloadHelper.doDownload(urls[0]); ++executeCount; if (image != null) { ImageCache.setImage(urls[0], image); } else { Log.w("ImageDownloadTask", "canceled : " + urls[0]); } } return image; } else { Log.w("ImageDownloadTask", "tag not match 1: " + tag); return null; } } catch (java.io.FileNotFoundException e) { ++errorCount; Log.e("ImageDownloadTask", urls[0] + ": " + e.toString()); return null; } catch (NullPointerException e) { // for at org.klab.iphoroid.net.HTTPClient.getByteArrayFromURL(HTTPClient.java:62) Log.w("ImageDownloadTask", "may be canceled?: " + urls[0]); return null; } catch (Exception e) { ++errorCount; Log.e("ImageDownloadTask", urls[0] + ": " + e.getMessage(), e); return null; } } @Override protected void onCancelled() { super.onCancelled(); imageDownloadHelper.onDownloadSuccess(imageView); Log.w("ImageDownloadTask", "canceled: " + tag); ++cancelCount; --runningCount; } @Override protected void onPostExecute(Bitmap result) { if (result != null) { if (tag != null && tag.equals(imageView.getTag())) { // view ??????????????? imageView.setImageBitmap(result); Log.i("ImageDownloadTask", "done: " + tag); ++doneCount; } else { Log.w("ImageDownloadTask", "tag not match 2: " + tag); } } else { if (tag != null && tag.equals(imageView.getTag())) { imageDownloadHelper.onDownloadFailure(imageView); } } imageDownloadHelper.onDownloadSuccess(imageView); --runningCount; Log.w("ImageDownloadTask", "in: " + queueInCount + ", exec: " + executeCount + ", error: " + errorCount + ", cancel: " + cancelCount + ", done: " + doneCount + ", run: " + runningCount); } }