aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2011-09-19 14:43:49 -0500
committerAndy Doan <doanac@gmail.com>2011-09-19 14:43:49 -0500
commit1800bf18743a5566f515608f6f7693beb89918f2 (patch)
tree0cdb1d2a5995bb6027f2139fdfc2c106cbad456d /src
downloadLinaroConnect-1800bf18743a5566f515608f6f7693beb89918f2.tar.gz
initial commit
this includes a very simple way to display items that have URL's associated with them
Diffstat (limited to 'src')
-rw-r--r--src/org/linaro/connect/JSONLayoutAdapter.java51
-rw-r--r--src/org/linaro/connect/JSONLayoutItem.java64
-rw-r--r--src/org/linaro/connect/JSONUtils.java77
-rw-r--r--src/org/linaro/connect/LinaroConnectActivity.java42
-rw-r--r--src/org/linaro/connect/handler/IJSONHandler.java8
-rw-r--r--src/org/linaro/connect/handler/JSONUrlHandler.java16
6 files changed, 258 insertions, 0 deletions
diff --git a/src/org/linaro/connect/JSONLayoutAdapter.java b/src/org/linaro/connect/JSONLayoutAdapter.java
new file mode 100644
index 0000000..cf8e38e
--- /dev/null
+++ b/src/org/linaro/connect/JSONLayoutAdapter.java
@@ -0,0 +1,51 @@
+package org.linaro.connect;
+
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.content.Context;
+import android.util.Log;
+import android.widget.ArrayAdapter;
+
+public class JSONLayoutAdapter extends ArrayAdapter<JSONLayoutItem> {
+
+ private final static String TAG = LinaroConnectActivity.TAG;
+ private final static String LAYOUT_FILE = "connect-layout.json";
+
+ public JSONLayoutAdapter(Context ctx, int resId, int textViewResId) {
+ super(ctx, resId, textViewResId);
+
+ JSONObject jso = getJSONLayout(ctx);
+ try {
+ JSONArray links = jso.getJSONArray("links");
+ for(int i = 0; i < links.length() && !links.isNull(i); i++)
+ add(new JSONLayoutItem(links.getJSONObject(i)));
+ }
+ catch(JSONException e) {
+ Log.e(TAG, "Error accessing JSON layout", e);
+ }
+ }
+
+ private static InputStream getDefaultFile(Context ctx) {
+ return ctx.getResources().openRawResource(R.raw.connect_layout);
+ }
+
+ private static InputStream getJSONInputStream(Context ctx) {
+ InputStream is;
+ try {
+ is = ctx.openFileInput(LAYOUT_FILE);
+ } catch (FileNotFoundException e) {
+ is = getDefaultFile(ctx);
+ }
+ return is;
+ }
+
+ private static JSONObject getJSONLayout(Context ctx) {
+ InputStream is = getJSONInputStream(ctx);
+ return JSONUtils.getObject(is);
+ }
+}
diff --git a/src/org/linaro/connect/JSONLayoutItem.java b/src/org/linaro/connect/JSONLayoutItem.java
new file mode 100644
index 0000000..8f0703d
--- /dev/null
+++ b/src/org/linaro/connect/JSONLayoutItem.java
@@ -0,0 +1,64 @@
+package org.linaro.connect;
+
+import java.util.HashMap;
+
+import org.json.JSONObject;
+import org.linaro.connect.handler.IJSONHandler;
+import org.linaro.connect.handler.JSONUrlHandler;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+class JSONLayoutItem {
+
+ private final JSONObject mJSO;
+
+ private final static HashMap<String, IJSONHandler> HANDLERS;
+
+ static {
+ HANDLERS = new HashMap<String, IJSONHandler>();
+ HANDLERS.put("URL", new JSONUrlHandler());
+ }
+
+ public JSONLayoutItem(JSONObject jso) {
+ mJSO = jso;
+ }
+
+ private static String getField(JSONObject jso, String name) {
+ try {
+ return jso.getString(name);
+ }
+ catch(Throwable t) {
+ Log.e(LinaroConnectActivity.TAG, "error getting '"+name+"'", t);
+ }
+ return null;
+ }
+
+ public String getLabel() {
+ return getField(mJSO, "label");
+ }
+
+ public String getHandler() {
+ return getField(mJSO, "handler");
+ }
+
+ public String getData() {
+ return getField(mJSO, "data");
+ }
+
+ public Intent getIntent(Context ctx) {
+ String key = getHandler();
+ String data = getData();
+ IJSONHandler handler = HANDLERS.get(key);
+ return handler.getIntent(ctx, data);
+ }
+
+ @Override
+ public String toString() {
+ String lbl = getLabel();
+ if (lbl == null)
+ lbl = mJSO.toString();
+ return lbl;
+ }
+}
diff --git a/src/org/linaro/connect/JSONUtils.java b/src/org/linaro/connect/JSONUtils.java
new file mode 100644
index 0000000..47e25ea
--- /dev/null
+++ b/src/org/linaro/connect/JSONUtils.java
@@ -0,0 +1,77 @@
+package org.linaro.connect;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+
+import android.util.Log;
+
+public class JSONUtils {
+
+ private final static String TAG = LinaroConnectActivity.TAG;
+
+ /**
+ * Parses the given input stream into a JSON object. This function
+ * also takes care of closing the input stream
+ */
+ public static JSONObject getObject(InputStream is) {
+ StringBuffer sb = new StringBuffer();
+ BufferedReader br = new BufferedReader(new InputStreamReader(is), 1024);
+
+ try {
+ char buff[] = new char[1024];
+ int len;
+ while ( (len=br.read(buff)) > 0 )
+ sb.append(buff, 0, len);
+ }
+ catch(IOException e) {
+ Log.e(TAG, "Unable to read JSON layout", e);
+ }
+
+ try {
+ is.close();
+ } catch (IOException e) {}
+
+ return toJSON(sb.toString());
+ }
+
+ public static JSONObject getObjectFromUrl(String url) {
+ HttpClient client = new DefaultHttpClient();
+ InputStream is = null;
+ try {
+ HttpResponse resp = client.execute(new HttpGet(url));
+ if (resp.getStatusLine().getStatusCode() == 200) {
+ is = resp.getEntity().getContent();
+ } else {
+ Log.e(TAG, "Error fetching URL: " + url);
+ }
+ }
+ catch(Exception e) {
+ Log.e(TAG, "Exception while fetching URL: " + url, e);
+ }
+
+ if (is == null) //just give an empty object
+ return toJSON("{}");
+
+ return getObject(is);
+ }
+
+ public static JSONObject toJSON(String json) {
+ try {
+ return (JSONObject) new JSONTokener(json).nextValue();
+ } catch(JSONException e) {
+ Log.e(TAG, "Unable to convert string to JSON", e);
+ }
+
+ return null;
+ }
+}
diff --git a/src/org/linaro/connect/LinaroConnectActivity.java b/src/org/linaro/connect/LinaroConnectActivity.java
new file mode 100644
index 0000000..bd4bfc1
--- /dev/null
+++ b/src/org/linaro/connect/LinaroConnectActivity.java
@@ -0,0 +1,42 @@
+package org.linaro.connect;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.ListView;
+
+public class LinaroConnectActivity extends Activity {
+
+ public final static String TAG = "LinaroConnect";
+
+ private JSONLayoutAdapter mAdapter;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ Context ctx = getApplicationContext();
+ setContentView(R.layout.main);
+
+ ListView lv = (ListView) findViewById(R.id.connect_items);
+ mAdapter = new JSONLayoutAdapter(ctx, R.layout.connect_item, R.id.connect_item_label);
+ lv.setAdapter(mAdapter);
+ lv.setOnItemClickListener(mClickListener);
+ }
+
+ private final OnItemClickListener mClickListener = new OnItemClickListener() {
+ @Override
+ public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
+ {
+ JSONLayoutItem jsi = mAdapter.getItem(pos);
+ if( jsi != null ) {
+ Intent i = jsi.getIntent(getApplicationContext());
+ startActivityForResult(i, 0);
+ }
+ }
+ };
+} \ No newline at end of file
diff --git a/src/org/linaro/connect/handler/IJSONHandler.java b/src/org/linaro/connect/handler/IJSONHandler.java
new file mode 100644
index 0000000..0a5372b
--- /dev/null
+++ b/src/org/linaro/connect/handler/IJSONHandler.java
@@ -0,0 +1,8 @@
+package org.linaro.connect.handler;
+
+import android.content.Context;
+import android.content.Intent;
+
+public interface IJSONHandler {
+ Intent getIntent(Context ctx, String data);
+}
diff --git a/src/org/linaro/connect/handler/JSONUrlHandler.java b/src/org/linaro/connect/handler/JSONUrlHandler.java
new file mode 100644
index 0000000..2ec3ff1
--- /dev/null
+++ b/src/org/linaro/connect/handler/JSONUrlHandler.java
@@ -0,0 +1,16 @@
+package org.linaro.connect.handler;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+
+public class JSONUrlHandler implements IJSONHandler {
+
+ @Override
+ public Intent getIntent(Context ctx, String data) {
+ Intent i = new Intent(Intent.ACTION_VIEW);
+ i.setData(Uri.parse(data));
+ return i;
+ }
+
+}