aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2011-10-16 14:24:54 -0500
committerAndy Doan <doanac@gmail.com>2011-10-16 14:24:54 -0500
commite0faf57f78e7f3f9f1c07d5489b7e427afbd9077 (patch)
tree1301f36657106e2268af9107b8183764bea20b6a
parent64ec8fe4adcd1887582d6f1afdf6faf492f3b272 (diff)
downloadLinaroConnect-e0faf57f78e7f3f9f1c07d5489b7e427afbd9077.tar.gz
create a cache file adapter
This makes the main parts of the JSONLayoutAdapter generic so that other adapters who would like to use a cache copy of some remote url may do so
-rw-r--r--src/org/linaro/connect/CachedJSONLayoutAdapter.java143
1 files changed, 143 insertions, 0 deletions
diff --git a/src/org/linaro/connect/CachedJSONLayoutAdapter.java b/src/org/linaro/connect/CachedJSONLayoutAdapter.java
new file mode 100644
index 0000000..4c40291
--- /dev/null
+++ b/src/org/linaro/connect/CachedJSONLayoutAdapter.java
@@ -0,0 +1,143 @@
+package org.linaro.connect;
+
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
+import org.json.JSONObject;
+
+import android.content.Context;
+import android.os.AsyncTask;
+import android.util.Log;
+import android.widget.ArrayAdapter;
+
+public abstract class CachedJSONLayoutAdapter extends ArrayAdapter<JSONItem> {
+
+ private long mLastCheck = 0;
+ private long mInterval = 0;
+ private int mCacheResId = 0;
+ private String mCacheFile;
+ private String mJSONUrl;
+
+ public CachedJSONLayoutAdapter(Context ctx, int resId, int textViewResId) {
+ super(ctx, resId, textViewResId);
+ }
+
+ protected abstract void updateLayout(JSONObject jso);
+
+ 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;
+ }
+
+ protected void setJSONUrl(String url) {
+ mJSONUrl = url;
+ }
+
+ void refresh() {
+ JSONObject jso = getJSONObject();
+ updateLayout(jso);
+
+ long now = System.currentTimeMillis();
+ if( now > mLastCheck + mInterval) {
+ new DownloadJSONTask().execute(mJSONUrl);
+ mLastCheck = now;
+ }
+ }
+
+ //this shouldn't run when writeCacheFile is run
+ private synchronized InputStream getJSONInputStream() {
+ 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;
+ }
+
+ private JSONObject getJSONObject() {
+ InputStream is = getJSONInputStream();
+ if (is == null)
+ return null;
+ return JSONUtils.getObject(is);
+ }
+
+ //this shouldn't run when getJSONInputStream is run
+ private synchronized void writeCacheFile(Context ctx, String json) {
+ 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(json.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 DownloadJSONTask extends AsyncTask<String, Void, Boolean> {
+ @Override
+ protected Boolean doInBackground(String... urls) {
+ String jsonNew = null;
+ boolean layoutChanged = false;
+ try {
+ InputStream is = LinaroConnect.getUrlInputStream(urls[0], false);
+ jsonNew = JSONUtils.toString(is);
+
+ } catch (Exception e) {
+ Log.e(LinaroConnect.TAG, "Error dowloading for cache: " + urls[0], e);
+ }
+
+ InputStream is = getJSONInputStream();
+ String jsonOrig = JSONUtils.toString(is);
+
+ layoutChanged = ( jsonNew != null && !jsonNew.equals(jsonOrig) );
+ if( layoutChanged) {
+ Log.i(LinaroConnect.TAG, "cache update found");
+ writeCacheFile(getContext(), jsonNew);
+ }
+
+ return new Boolean(layoutChanged);
+ }
+
+ @Override
+ protected void onPostExecute(Boolean result) {
+ if( result.booleanValue() ) {
+ JSONObject jso = getJSONObject();
+ updateLayout(jso);
+ }
+ }
+ }
+}