diff --git a/src/com/androidquery/AbstractAQuery.java b/src/com/androidquery/AbstractAQuery.java index 79210148..35a801f0 100644 --- a/src/com/androidquery/AbstractAQuery.java +++ b/src/com/androidquery/AbstractAQuery.java @@ -101,6 +101,33 @@ public abstract class AbstractAQuery> implements Con private Transformer trans; private Integer policy; private HttpHost proxy; + + private LoadListener loadListener; + + public interface LoadListener { + /** + * Use Handler handle view change
+ *

Example:

+ * private Handler handler = new Handler(){
+ * public void handleMessage(Message msg) {
+ * super.handleMessage(msg);
+ * tv.setText("" + msg.obj);
+ * }
+ * };
+ * String imageurl ="http://jpp2.imghb.com/pic/pic/52/60/24/1396053752602447_a602x602.jpg";
+ * aq.id(R.id.imageView1).progress(R.id.progressBar1).load(new LoadListener() {
+ * public void onLoadProgress(int current, float max) {
+ * float present = (float) (max - current)/ max;
+ * Message msg = new Message();
+ * msg.obj = String.format("%.2f%%", (present * 100));
+ * handler.sendMessage(msg);
+ * }
+ * }).image(imageurl, false, false);
+ * @param current + * @param max + */ + public void onLoadProgress(int current, float max); + } protected T create(View view){ @@ -763,7 +790,7 @@ public T image(String url, boolean memCache, boolean fileCache, int targetWidth, protected T image(String url, boolean memCache, boolean fileCache, int targetWidth, int fallbackId, Bitmap preset, int animId, float ratio, int round, String networkUrl){ if(view instanceof ImageView){ - BitmapAjaxCallback.async(act, getContext(), (ImageView) view, url, memCache, fileCache, targetWidth, fallbackId, preset, animId, ratio, AQuery.ANCHOR_DYNAMIC, progress, ah, policy, round, proxy, networkUrl); + BitmapAjaxCallback.async(act, getContext(), (ImageView) view, url, memCache, fileCache, targetWidth, fallbackId, preset, animId, ratio, AQuery.ANCHOR_DYNAMIC, progress, ah, policy, round, proxy, loadListener, networkUrl); reset(); } @@ -1862,6 +1889,10 @@ protected T invoke(AbstractAjaxCallback cb){ cb.proxy(proxy.getHostName(), proxy.getPort()); } + if(loadListener != null){ + cb.load(loadListener); + } + if(act != null){ cb.async(act); }else{ @@ -1880,7 +1911,7 @@ protected void reset(){ trans = null; policy = CACHE_DEFAULT; proxy = null; - + loadListener = null; } @@ -2707,4 +2738,10 @@ public T download(String url, File target, Object handler, String callback){ } + public T load(LoadListener listener) { + this.loadListener = listener; + return self(); + } + + } diff --git a/src/com/androidquery/callback/AbstractAjaxCallback.java b/src/com/androidquery/callback/AbstractAjaxCallback.java index 1fbb68a7..fd755bd3 100644 --- a/src/com/androidquery/callback/AbstractAjaxCallback.java +++ b/src/com/androidquery/callback/AbstractAjaxCallback.java @@ -93,6 +93,7 @@ import android.view.View; import com.androidquery.AQuery; +import com.androidquery.AbstractAQuery.LoadListener; import com.androidquery.auth.AccountHandle; import com.androidquery.auth.GoogleHandle; import com.androidquery.util.AQUtility; @@ -152,6 +153,8 @@ public abstract class AbstractAjaxCallback implements Runnable{ private boolean uiCallback = true; private int retry = 0; + private LoadListener loadListener; + @SuppressWarnings("unchecked") private K self(){ return (K) this; @@ -165,6 +168,7 @@ private void clear(){ transformer = null; ah = null; act = null; + loadListener = null; } /** @@ -459,6 +463,15 @@ public K params(Map params){ return self(); } + /**Set the loadlistener can listen downloading percent + * @param li + * @return + */ + public K load(LoadListener loadlistener){ + loadListener = loadlistener; + return self(); + } + /** * Set the progress view (can be a progress bar or any view) to be shown (VISIBLE) and hide (GONE) depends on progress. * @@ -1709,7 +1722,7 @@ private void copy(InputStream is, OutputStream os, int max) throws IOException{ p = new Progress(o); } - AQUtility.copy(is, os, max, p); + AQUtility.copy(is, os, max, p, loadListener); } diff --git a/src/com/androidquery/callback/BitmapAjaxCallback.java b/src/com/androidquery/callback/BitmapAjaxCallback.java index 27ad08df..a55d405d 100644 --- a/src/com/androidquery/callback/BitmapAjaxCallback.java +++ b/src/com/androidquery/callback/BitmapAjaxCallback.java @@ -54,6 +54,7 @@ import android.widget.ImageView; import com.androidquery.AQuery; +import com.androidquery.AbstractAQuery.LoadListener; import com.androidquery.auth.AccountHandle; import com.androidquery.util.AQUtility; import com.androidquery.util.BitmapCache; @@ -940,7 +941,7 @@ private static boolean fadeIn(int animation, int source){ public static void async(Activity act, Context context, ImageView iv, String url, Object progress, AccountHandle ah, ImageOptions options, HttpHost proxy, String networkUrl){ - async(act, context, iv, url, options.memCache, options.fileCache, options.targetWidth, options.fallback, options.preset, options.animation, options.ratio, options.anchor, progress, ah, options.policy, options.round, proxy, networkUrl); + async(act, context, iv, url, options.memCache, options.fileCache, options.targetWidth, options.fallback, options.preset, options.animation, options.ratio, options.anchor, progress, ah, options.policy, options.round, proxy,null, networkUrl); } @@ -953,7 +954,7 @@ public static void async(Activity act, Context context, ImageView iv, String url * */ - public static void async(Activity act, Context context, ImageView iv, String url, boolean memCache, boolean fileCache, int targetWidth, int fallbackId, Bitmap preset, int animation, float ratio, float anchor, Object progress, AccountHandle ah, int policy, int round, HttpHost proxy, String networkUrl){ + public static void async(Activity act, Context context, ImageView iv, String url, boolean memCache, boolean fileCache, int targetWidth, int fallbackId, Bitmap preset, int animation, float ratio, float anchor, Object progress, AccountHandle ah, int policy, int round, HttpHost proxy, LoadListener loadlistener, String networkUrl){ Bitmap bm = null; @@ -967,7 +968,7 @@ public static void async(Activity act, Context context, ImageView iv, String url setBmAnimate(iv, bm, preset, fallbackId, animation, ratio, anchor, AjaxStatus.MEMORY); }else{ BitmapAjaxCallback cb = new BitmapAjaxCallback(); - cb.url(url).imageView(iv).memCache(memCache).fileCache(fileCache).targetWidth(targetWidth).fallback(fallbackId).preset(preset).animation(animation).ratio(ratio).anchor(anchor).progress(progress).auth(ah).policy(policy).round(round).networkUrl(networkUrl); + cb.url(url).imageView(iv).memCache(memCache).fileCache(fileCache).targetWidth(targetWidth).fallback(fallbackId).preset(preset).animation(animation).ratio(ratio).anchor(anchor).progress(progress).auth(ah).policy(policy).round(round).load(loadlistener).networkUrl(networkUrl); if(proxy != null){ cb.proxy(proxy.getHostName(), proxy.getPort()); } diff --git a/src/com/androidquery/util/AQUtility.java b/src/com/androidquery/util/AQUtility.java index ec6a3e05..e106622f 100644 --- a/src/com/androidquery/util/AQUtility.java +++ b/src/com/androidquery/util/AQUtility.java @@ -50,6 +50,7 @@ import android.view.animation.AlphaAnimation; import com.androidquery.AQuery; +import com.androidquery.AbstractAQuery.LoadListener; /** * Utility methods. Warning: Methods might changed in future versions. @@ -390,6 +391,10 @@ public static void copy(InputStream in, OutputStream out) throws IOException { } public static void copy(InputStream in, OutputStream out, int max, Progress progress) throws IOException { + copy(in, out, max, progress, null); + } + + public static void copy(InputStream in, OutputStream out, int max, Progress progress, LoadListener loadListener) throws IOException { if(progress != null){ progress.reset(); @@ -398,7 +403,12 @@ public static void copy(InputStream in, OutputStream out, int max, Progress prog byte[] b = new byte[IO_BUFFER_SIZE]; int read; + int count = max; while((read = in.read(b)) != -1){ + if(loadListener != null){ + count = count - read; + loadListener.onLoadProgress(count, max); + } out.write(b, 0, read); if(progress != null){ progress.increment(read);