From 55d61d8cdf011752c5075468d3ce7f437df4ffd8 Mon Sep 17 00:00:00 2001 From: youxiachai Date: Sat, 11 May 2013 20:23:34 +0800 Subject: [PATCH 1/6] Update AbstractAQuery.java support acquire downloading progress count --- src/com/androidquery/AbstractAQuery.java | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/com/androidquery/AbstractAQuery.java b/src/com/androidquery/AbstractAQuery.java index 79210148..119a1c6a 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 listener; + + 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){ @@ -2707,4 +2734,10 @@ public T download(String url, File target, Object handler, String callback){ } + public T load(LoadListener listener) { + this.listener = listener; + return self(); + } + + } From eeb0b5f3dbdce7fd74268bea49ff0ad87b97f1ed Mon Sep 17 00:00:00 2001 From: youxiachai Date: Sat, 11 May 2013 20:26:04 +0800 Subject: [PATCH 2/6] Update AQUtility.java handle loadlistener --- src/com/androidquery/util/AQUtility.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/com/androidquery/util/AQUtility.java b/src/com/androidquery/util/AQUtility.java index ec6a3e05..d1ef6568 100644 --- a/src/com/androidquery/util/AQUtility.java +++ b/src/com/androidquery/util/AQUtility.java @@ -390,6 +390,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 +402,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); From 6bef756ef70312588691e3e251e4c30bedce8be2 Mon Sep 17 00:00:00 2001 From: youxiachai Date: Sat, 11 May 2013 20:31:21 +0800 Subject: [PATCH 3/6] Update AbstractAjaxCallback.java set the downloadlistener in ajaxCallback --- .../callback/AbstractAjaxCallback.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/com/androidquery/callback/AbstractAjaxCallback.java b/src/com/androidquery/callback/AbstractAjaxCallback.java index 1fbb68a7..f3ac1530 100644 --- a/src/com/androidquery/callback/AbstractAjaxCallback.java +++ b/src/com/androidquery/callback/AbstractAjaxCallback.java @@ -152,6 +152,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 +167,7 @@ private void clear(){ transformer = null; ah = null; act = null; + loadListener = null; } /** @@ -459,6 +462,15 @@ public K params(Map params){ return self(); } + /**Set the loadlistener can listen downloading percent + * @param li + * @return + */ + public K load(LoadListener li){ + loadListener = li; + 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 +1721,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); } From 22af3e7dfa1c66022dcec8ce698ca0225aded397 Mon Sep 17 00:00:00 2001 From: youxiachai Date: Sat, 11 May 2013 20:37:39 +0800 Subject: [PATCH 4/6] Update BitmapAjaxCallback.java set bitmapAjaxCallback listen downloading percent --- .../callback/BitmapAjaxCallback.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/com/androidquery/callback/BitmapAjaxCallback.java b/src/com/androidquery/callback/BitmapAjaxCallback.java index 27ad08df..fcf165ec 100644 --- a/src/com/androidquery/callback/BitmapAjaxCallback.java +++ b/src/com/androidquery/callback/BitmapAjaxCallback.java @@ -944,6 +944,37 @@ public static void async(Activity act, Context context, ImageView iv, String url } + public static void async(Activity act, Context context, ImageView iv, String url, Object progress, AccountHandle ah, ImageOptions options, HttpHost proxy, String networkUrl,LoadListener loadListener){ + + Bitmap bm = null; + + if (memCache) { + bm = memGet(url, targetWidth, round); + } + + if (bm != null) { + iv.setTag(AQuery.TAG_URL, url); + Common.showProgress(progress, url, false); + 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).load(loadListener).networkUrl(networkUrl); + if (proxy != null) { + cb.proxy(proxy.getHostName(), proxy.getPort()); + } + if (act != null) { + cb.async(act); + } else { + cb.async(context); + } + } + + } /** * AQuery internal use only. Please uses AQuery image() methods instead. From 0d097cd4a68ceec24c50b969f3ee477194325d22 Mon Sep 17 00:00:00 2001 From: youxiachai Date: Sat, 11 May 2013 20:43:13 +0800 Subject: [PATCH 5/6] Update AbstractAQuery.java update bitmapAjaxCallback set the downloading listener --- src/com/androidquery/AbstractAQuery.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/androidquery/AbstractAQuery.java b/src/com/androidquery/AbstractAQuery.java index 119a1c6a..14d5f91b 100644 --- a/src/com/androidquery/AbstractAQuery.java +++ b/src/com/androidquery/AbstractAQuery.java @@ -102,7 +102,7 @@ public abstract class AbstractAQuery> implements Con private Integer policy; private HttpHost proxy; - private LoadListener listener; + private LoadListener loadListener; public interface LoadListener { /** @@ -790,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(); } @@ -1907,7 +1907,7 @@ protected void reset(){ trans = null; policy = CACHE_DEFAULT; proxy = null; - + loadListener = null; } @@ -2735,7 +2735,7 @@ public T download(String url, File target, Object handler, String callback){ } public T load(LoadListener listener) { - this.listener = listener; + this.loadListener = listener; return self(); } From 9fc42372e0203eeb44372a721909e3b2440b968c Mon Sep 17 00:00:00 2001 From: youxiachai Date: Sat, 11 May 2013 21:09:44 +0800 Subject: [PATCH 6/6] support file image acquire downloading count --- src/com/androidquery/AbstractAQuery.java | 4 ++ .../callback/AbstractAjaxCallback.java | 5 ++- .../callback/BitmapAjaxCallback.java | 38 ++----------------- src/com/androidquery/util/AQUtility.java | 1 + 4 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/com/androidquery/AbstractAQuery.java b/src/com/androidquery/AbstractAQuery.java index 14d5f91b..35a801f0 100644 --- a/src/com/androidquery/AbstractAQuery.java +++ b/src/com/androidquery/AbstractAQuery.java @@ -1889,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{ diff --git a/src/com/androidquery/callback/AbstractAjaxCallback.java b/src/com/androidquery/callback/AbstractAjaxCallback.java index f3ac1530..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; @@ -466,8 +467,8 @@ public K params(Map params){ * @param li * @return */ - public K load(LoadListener li){ - loadListener = li; + public K load(LoadListener loadlistener){ + loadListener = loadlistener; return self(); } diff --git a/src/com/androidquery/callback/BitmapAjaxCallback.java b/src/com/androidquery/callback/BitmapAjaxCallback.java index fcf165ec..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,41 +941,10 @@ 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); } - public static void async(Activity act, Context context, ImageView iv, String url, Object progress, AccountHandle ah, ImageOptions options, HttpHost proxy, String networkUrl,LoadListener loadListener){ - - Bitmap bm = null; - - if (memCache) { - bm = memGet(url, targetWidth, round); - } - - if (bm != null) { - iv.setTag(AQuery.TAG_URL, url); - Common.showProgress(progress, url, false); - 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).load(loadListener).networkUrl(networkUrl); - if (proxy != null) { - cb.proxy(proxy.getHostName(), proxy.getPort()); - } - if (act != null) { - cb.async(act); - } else { - cb.async(context); - } - } - - } /** * AQuery internal use only. Please uses AQuery image() methods instead. @@ -984,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; @@ -998,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 d1ef6568..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.