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
71 changes: 65 additions & 6 deletions src/com/androidquery/AbstractAQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -1987,8 +1987,30 @@ public <K> T ajax(String url, Class<K> type, AjaxCallback<K> callback){
*/

public <K> T ajax(String url, Class<K> type, long expire, AjaxCallback<K> callback){
return ajax(url, type, expire, expire, callback);
}

/**
* Ajax call with various callback data types with file caching.
*
* The expire param is the duration to consider cached data expired (if hit).
* For example, expire = 15 * 60 * 1000 means if the cache data is within 15 minutes old,
* return cached data immediately, otherwise go fetch the source again.
*
*
* @param url url
* @param type data type
* @param expire duration in millseconds, 0 = always use cache
* @param fresh duration in millseconds, 0 = always use cache
* @param callback callback handler
* @return self
*
*
*/

public <K> T ajax(String url, Class<K> type, long expire, long fresh, AjaxCallback<K> callback){

callback.type(type).url(url).fileCache(true).expire(expire);
callback.type(type).url(url).fileCache(true).expire(expire, fresh);

return ajax(callback);
}
Expand Down Expand Up @@ -2020,7 +2042,6 @@ public <K> T ajax(String url, Class<K> type, Object handler, String callback){

}


/**
* Ajax call with various callback data types with file caching.
*
Expand All @@ -2040,9 +2061,33 @@ public <K> T ajax(String url, Class<K> type, Object handler, String callback){
*/

public <K> T ajax(String url, Class<K> type, long expire, Object handler, String callback){
return ajax(url, type, expire, expire, handler, callback);
}


/**
* Ajax call with various callback data types with file caching.
*
* The expire param is the duration to consider cache data expired (if hit).
* For example, expire = 15 * 60 * 1000 means if the cache data is within 15 minutes old,
* return cached data immediately, otherwise go fetch the source again.
*
*
* @param url url
* @param type data type
* @param expire duration in millseconds, 0 = always use cache
* @param fresh duration in millseconds, 0 = always use cache
* @param handler the handler object with the callback method to be called
* @param callback callback method name
* @return self
*
*
*/

public <K> T ajax(String url, Class<K> type, long expire, long fresh, Object handler, String callback){

AjaxCallback<K> cb = new AjaxCallback<K>();
cb.type(type).weakHandler(handler, callback).fileCache(true).expire(expire);
cb.type(type).weakHandler(handler, callback).fileCache(true).expire(expire, fresh);

return ajax(url, type, cb);
}
Expand Down Expand Up @@ -2200,8 +2245,6 @@ public <K> T sync(AjaxCallback<K> callback){
return self();
}



/**
* Cache the url to file cache without any callback.
*
Expand All @@ -2214,7 +2257,23 @@ public <K> T sync(AjaxCallback<K> callback){
* @see testCache
*/
public T cache(String url, long expire){
return ajax(url, byte[].class, expire, null, null);
return cache(url, expire, expire);
}

/**
* Cache the url to file cache without any callback.
*
*
* @param url url to cache
* @param expire duration in millseconds, 0 = never consider cached data as expired
* @param fresh duration in millseconds, 0 = never consider cached data as expired
*
* @return self
*
* @see testCache
*/
public T cache(String url, long expire, long fresh){
return ajax(url, byte[].class, expire, fresh, null, null);
}


Expand Down
42 changes: 34 additions & 8 deletions src/com/androidquery/callback/AbstractAjaxCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public abstract class AbstractAjaxCallback<T, K> implements Runnable{
private boolean redirect = true;

private long expire;
private long fresh;
private String encoding = "UTF-8";
private WeakReference<Activity> act;

Expand Down Expand Up @@ -395,6 +396,22 @@ public K uiCallback(boolean uiCallback){
*/
public K expire(long expire){
this.expire = expire;
this.fresh = expire;
return self();
}

/**
* The expire and fresh duation for filecache.
* If a cached copy will be served if a cached file exists within current time minus fresh duration.
* But, with network error case and if a cached copy will be served if a cached file exists within current time minus expire duration
*
* @param expire the expire
* @param fresh the fresh
* @return self
*/
public K expire(long expire, long fresh){
this.expire = expire;
this.fresh = fresh;
return self();
}

Expand Down Expand Up @@ -911,15 +928,17 @@ protected void filePut(String url, T object, File file, byte[] data){

}

protected File accessFile(File cacheDir, String url){
protected File accessFile(File cacheDir, String url, boolean onlyFresh){

if(expire < 0) return null;

long limit = onlyFresh ? fresh : expire;

File file = AQUtility.getExistedCacheByUrl(cacheDir, url);

if(file != null && expire != 0){
long diff = System.currentTimeMillis() - file.lastModified();
if(diff > expire){
if(diff > limit){
return null;
}
}
Expand Down Expand Up @@ -1072,11 +1091,9 @@ public void run() {
}

private void backgroundWork(){

if(!refresh){

if(fileCache){
fileWork();
fileWork(true);
}
}

Expand All @@ -1088,7 +1105,16 @@ private void backgroundWork(){
networkWork();
}


if(result == null && !refresh && fileCache){
AjaxStatus prevStatus = status;
status = new AjaxStatus();

fileWork(false);

if (result == null){
status = prevStatus;
}
}
}

private String getCacheUrl(){
Expand All @@ -1113,9 +1139,9 @@ private String getNetworkUrl(String url){
return result;
}

private void fileWork(){
private void fileWork(boolean freshOnly){

File file = accessFile(cacheDir, getCacheUrl());
File file = accessFile(cacheDir, getCacheUrl(), freshOnly);

//if file exist
if(file != null){
Expand Down
4 changes: 2 additions & 2 deletions src/com/androidquery/callback/BitmapAjaxCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,13 @@ private Bitmap bmGet(String path, byte[] data){
}

@Override
protected File accessFile(File cacheDir, String url){
protected File accessFile(File cacheDir, String url, boolean onlyFresh){

if(imageFile != null && imageFile.exists()){
return imageFile;
}

return super.accessFile(cacheDir, url);
return super.accessFile(cacheDir, url, onlyFresh);
}


Expand Down