diff options
author | Andy Doan <andy.doan@linaro.org> | 2011-10-25 01:39:02 -0500 |
---|---|---|
committer | Andy Doan <andy.doan@linaro.org> | 2011-10-25 01:39:02 -0500 |
commit | 6343397e877b7639e31b6160ea651255f371c666 (patch) | |
tree | 3ff1a04096c1986233b9ad63318797e8b13d7781 | |
parent | 2d56eba50455cbfc86904bd006329cac78ca49b3 (diff) | |
download | LinaroConnect-6343397e877b7639e31b6160ea651255f371c666.tar.gz |
create a more generic cached layout adapter
this is based on CacheJSONLayoutAdapter, but more generic
so that non JSON streams can be used
-rw-r--r-- | src/org/linaro/connect/CachedLayoutAdapter.java | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/org/linaro/connect/CachedLayoutAdapter.java b/src/org/linaro/connect/CachedLayoutAdapter.java new file mode 100644 index 0000000..aa25667 --- /dev/null +++ b/src/org/linaro/connect/CachedLayoutAdapter.java @@ -0,0 +1,151 @@ +package org.linaro.connect; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; + +import android.content.Context; +import android.os.AsyncTask; +import android.util.Log; +import android.widget.ArrayAdapter; + +public abstract class CachedLayoutAdapter<T,K> extends ArrayAdapter<T> { + + private long mLastCheck = 0; + private long mInterval = 0; + private int mCacheResId = 0; + private String mCacheFile; + private String mUrl; + + public CachedLayoutAdapter(Context ctx, int resId, int textViewResId) { + super(ctx, resId, textViewResId); + } + + protected abstract void updateLayout(K items); + protected abstract K getItems(); + + protected void setRefreshInterval(long interval) { + mInterval = interval; + } + + /** + * This can be used to tell the adapter we have a default version of the + * file that shipped with the app. + */ + protected void setCacheResourceId(int id) { + mCacheResId = id; + } + + /** + * Sets the filename that is used to read/write the cache to + */ + protected void setCacheFileName(String file) { + mCacheFile = file; + } + + /** + * Sets the url to grab the data from. If no cache file name is set, then + * it will use the basename of the url + */ + protected void setUrl(String url) { + mUrl = url; + if( mCacheFile == null ) { + String parts[] = url.split("/"); + mCacheFile = parts[parts.length-1]; + } + } + + /** + * Called with true when a background action is occurring. + */ + protected void onBusy(boolean busy) { + } + + public void refresh(boolean force) { + onBusy(true); + K items = getItems(); + updateLayout(items); + + long now = System.currentTimeMillis(); + if( force || now > mLastCheck + mInterval) { + new DownloadContentTask().execute(mUrl); + mLastCheck = now; + } else { + onBusy(false); + } + } + + //this shouldn't run when writeCacheFile is run + protected synchronized InputStream getInputStream() { + InputStream is = null; + try { + is = getContext().openFileInput(mCacheFile); + Log.v(LinaroConnect.TAG, "cache file found: " + mCacheFile); + } catch (FileNotFoundException e) { + if (mCacheResId > 0 ) + is = getContext().getResources().openRawResource(mCacheResId); + } + return is; + } + + //this shouldn't run when getJSONInputStream is run + private synchronized void writeCacheFile(Context ctx, String content) { + FileOutputStream os = null; + try { + os = ctx.openFileOutput(mCacheFile, 0); + } + catch(Exception e) { + Log.e(LinaroConnect.TAG, "Error opening cache for writing", e); + return; + } + + try { + os.write(content.getBytes()); + } + catch(Exception e) { + Log.e(LinaroConnect.TAG, "Error writing cache", e); + return; + } + + try { + os.close(); + } + catch(Exception e) {} + } + + /** + * checks to see if there's a new JSON file on the server + */ + private class DownloadContentTask extends AsyncTask<String, Void, Boolean> { + @Override + protected Boolean doInBackground(String... urls) { + String contentNew = null; + boolean layoutChanged = false; + try { + InputStream is = LinaroConnect.getUrlInputStream(urls[0], false); + contentNew = JSONUtils.toString(is); + + } catch (Exception e) { + Log.e(LinaroConnect.TAG, "Error dowloading for cache: " + urls[0], e); + } + + InputStream is = getInputStream(); + String orig = JSONUtils.toString(is); + + layoutChanged = ( contentNew != null && !contentNew.equals(orig) ); + if( layoutChanged) { + Log.i(LinaroConnect.TAG, "cache update found"); + writeCacheFile(getContext(), contentNew); + } + + return new Boolean(layoutChanged); + } + + @Override + protected void onPostExecute(Boolean result) { + if( result.booleanValue() ) + updateLayout(getItems()); + onBusy(false); + } + } +} |