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
40 changes: 39 additions & 1 deletion src/com/androidquery/AbstractAQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -2707,5 +2707,43 @@ public T download(String url, File target, Object handler, String callback){
return download(url, target, cb);

}



/**
* Download a file asynchronously.
*
* @param url url
* @param target the file to be saved
* @param resume resume download flag
* @param cb callback to be called when done
* @return self
*
*/

public T download(String url, File target, boolean resume, AjaxCallback<File> cb){

cb.url(url).type(File.class).targetFile(target).resume(resume);
return ajax(cb);

}

/**
* Download a file asynchronously.
*
* @param url url
* @param target the file to be saved
* @param resume resume download flag
* @param handler the callback handler
* @param callback the callback method
* @return self
*
*/

public T download(String url, File target, boolean resume, Object handler, String callback){

AjaxCallback<File> cb = new AjaxCallback<File>();
cb.weakHandler(handler, callback);
return download(url, target, resume, cb);

}
}
56 changes: 53 additions & 3 deletions src/com/androidquery/callback/AbstractAjaxCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,31 @@ private void clear(){
transformer = null;
ah = null;
act = null;

callback = null;
targetFile = null;
cacheDir = null;
cookies = null;
headers = null;
params = null;
proxy = null;
networkUrl = null;

abort = false;
blocked = false;
completed = false;
fileCache = false;
memCache = false;
reauth = false;
refresh = false;

method = Constants.METHOD_DETECT;
policy = Constants.CACHE_DEFAULT;
lastStatus = 200;
expire = 0;
retry = 0;
timeout = 0;
uiCallback = true;
}

/**
Expand Down Expand Up @@ -1183,6 +1208,11 @@ private void filePut(){
}
}

}else if(status.getFile() != null && status.getSource() == AjaxStatus.NETWORK && status.getInvalid()){
File file = status.getFile();
if (file.exists()) {
file.delete();
}
}
}catch(Exception e){
AQUtility.debug(e);
Expand Down Expand Up @@ -1523,6 +1553,11 @@ private HttpResponse execute(HttpUriRequest hr, DefaultHttpClient client, HttpCo
return response;
}

private boolean resume;
public K resume(boolean resume){
this.resume = resume;
return self();
}

private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers, AjaxStatus status) throws ClientProtocolException, IOException{

Expand All @@ -1546,6 +1581,13 @@ private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers,
hr.addHeader("Cookie", cookie);
}

if(resume && targetFile != null){
long len = targetFile.length();
if (len > 0){
hr.addHeader("Range", "bytes=" + len + "-");
}
}

if(ah != null){
ah.applyToken(this, hr);
}
Expand Down Expand Up @@ -1642,8 +1684,8 @@ private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers,
if(file == null){
os = new PredefinedBAOS(size);
}else{
file.createNewFile();
os = new BufferedOutputStream(new FileOutputStream(file));
if(!resume || !file.exists()) file.createNewFile();
os = new BufferedOutputStream(new FileOutputStream(file, true));
}

is = entity.getContent();
Expand All @@ -1663,7 +1705,15 @@ private void httpDo(HttpUriRequest hr, String url, Map<String, String> headers,
file = null;
}
}


}catch(IOException e){
if (file != null && !resume) {
AQUtility.close(os);
os = null;
file.delete();
}
throw e;

}finally{
AQUtility.close(is);
AQUtility.close(os);
Expand Down