aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2011-10-25 01:39:52 -0500
committerAndy Doan <andy.doan@linaro.org>2011-10-25 01:39:52 -0500
commitcddfd0e405e9ee5f460f900e4712827ffb633a7e (patch)
tree7afca94407576563bc6b7532d2ac40f7e7fad4f6
parent6343397e877b7639e31b6160ea651255f371c666 (diff)
downloadLinaroConnect-cddfd0e405e9ee5f460f900e4712827ffb633a7e.tar.gz
convert CachedJSONLayoutAdapter to CachedLayoutAdapter
-rw-r--r--src/org/linaro/connect/CachedJSONLayoutAdapter.java147
-rw-r--r--src/org/linaro/connect/LinaroConnectActivity.java2
-rw-r--r--src/org/linaro/connect/PostingsActivity.java2
3 files changed, 6 insertions, 145 deletions
diff --git a/src/org/linaro/connect/CachedJSONLayoutAdapter.java b/src/org/linaro/connect/CachedJSONLayoutAdapter.java
index da845b9..c08fd5d 100644
--- a/src/org/linaro/connect/CachedJSONLayoutAdapter.java
+++ b/src/org/linaro/connect/CachedJSONLayoutAdapter.java
@@ -1,161 +1,22 @@
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 abstract class CachedJSONLayoutAdapter extends CachedLayoutAdapter<JSONItem, JSONObject> {
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;
- }
-
- /**
- * 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 setJSONUrl(String url) {
- mJSONUrl = url;
- if( mCacheFile == null ) {
- String parts[] = url.split("/");
- mCacheFile = parts[parts.length-1];
- }
- }
-
- /**
- * Called with true when a backround action is occurring.
- */
- protected void onBusy(boolean busy) {
- }
-
- void refresh(boolean force) {
- onBusy(true);
- JSONObject jso = getJSONObject();
- updateLayout(jso);
-
- long now = System.currentTimeMillis();
- if( force || now > mLastCheck + mInterval) {
- new DownloadJSONTask().execute(mJSONUrl);
- mLastCheck = now;
- } else {
- onBusy(false);
- }
- }
-
- //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();
+ @Override
+ protected JSONObject getItems() {
+ InputStream is = getInputStream();
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);
- }
- onBusy(false);
- }
- }
}
diff --git a/src/org/linaro/connect/LinaroConnectActivity.java b/src/org/linaro/connect/LinaroConnectActivity.java
index f1dbe2b..9144e6e 100644
--- a/src/org/linaro/connect/LinaroConnectActivity.java
+++ b/src/org/linaro/connect/LinaroConnectActivity.java
@@ -62,7 +62,7 @@ public class LinaroConnectActivity extends Activity {
super(ctx, R.layout.connect_item, R.id.connect_item_label);
setRefreshInterval(INTERVAL);
- setJSONUrl(url);
+ setUrl(url);
setCacheResourceId(resid);
}
diff --git a/src/org/linaro/connect/PostingsActivity.java b/src/org/linaro/connect/PostingsActivity.java
index ff8452d..63d941a 100644
--- a/src/org/linaro/connect/PostingsActivity.java
+++ b/src/org/linaro/connect/PostingsActivity.java
@@ -75,7 +75,7 @@ public class PostingsActivity extends Activity {
super(getApplicationContext(), R.layout.posting_item, R.id.posting_item_title);
setRefreshInterval(INTERVAL);
setCacheFileName("postings.json." + label);
- setJSONUrl(getIntent().getDataString());
+ setUrl(getIntent().getDataString());
}
@Override