Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/com/androidquery/AbstractAQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ public abstract class AbstractAQuery<T extends AbstractAQuery<T>> implements Con
private Transformer trans;
private Integer policy;
private HttpHost proxy;

private LoadListener loadListener;

public interface LoadListener {
/**
* Use Handler handle view change </br>
* <h1>Example: </h1>
* private Handler handler = new Handler(){</br>
* public void handleMessage(Message msg) { </br>
* super.handleMessage(msg);</br>
* tv.setText("" + msg.obj); </br>
* }</br>
* };</br>
* String imageurl ="http://jpp2.imghb.com/pic/pic/52/60/24/1396053752602447_a602x602.jpg";</br>
* aq.id(R.id.imageView1).progress(R.id.progressBar1).load(new LoadListener() {</br>
* public void onLoadProgress(int current, float max) { </br>
* float present = (float) (max - current)/ max; </br>
* Message msg = new Message();</br>
* msg.obj = String.format("%.2f%%", (present * 100)); </br>
* handler.sendMessage(msg); </br>
* } </br>
* }).image(imageurl, false, false);</br>
* @param current
* @param max
*/
public void onLoadProgress(int current, float max);
}

protected T create(View view){

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -1862,6 +1889,10 @@ protected <K> T invoke(AbstractAjaxCallback<?, K> cb){
cb.proxy(proxy.getHostName(), proxy.getPort());
}

if(loadListener != null){
cb.load(loadListener);
}

if(act != null){
cb.async(act);
}else{
Expand All @@ -1880,7 +1911,7 @@ protected void reset(){
trans = null;
policy = CACHE_DEFAULT;
proxy = null;

loadListener = null;

}

Expand Down Expand Up @@ -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();
}


}
15 changes: 14 additions & 1 deletion src/com/androidquery/callback/AbstractAjaxCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -152,6 +153,8 @@ public abstract class AbstractAjaxCallback<T, K> implements Runnable{
private boolean uiCallback = true;
private int retry = 0;

private LoadListener loadListener;

@SuppressWarnings("unchecked")
private K self(){
return (K) this;
Expand All @@ -165,6 +168,7 @@ private void clear(){
transformer = null;
ah = null;
act = null;
loadListener = null;
}

/**
Expand Down Expand Up @@ -459,6 +463,15 @@ public K params(Map<String, ?> 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.
*
Expand Down Expand Up @@ -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);


}
Expand Down
7 changes: 4 additions & 3 deletions src/com/androidquery/callback/BitmapAjaxCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

}

Expand All @@ -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;

Expand All @@ -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());
}
Expand Down
10 changes: 10 additions & 0 deletions src/com/androidquery/util/AQUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down