d2887a04411e67cee8c3748000eb390b06b62c8d
[pub/Android/ownCloud.git] / src / com / owncloud / android / extensions / ExtensionsListActivity.java
1 package com.owncloud.android.extensions;
2
3 import java.util.HashMap;
4 import java.util.LinkedList;
5
6 import org.apache.commons.httpclient.HttpClient;
7 import org.apache.commons.httpclient.methods.GetMethod;
8 import org.json.JSONArray;
9 import org.json.JSONException;
10 import org.json.JSONObject;
11
12 import com.owncloud.android.utils.OwnCloudVersion;
13
14
15 import android.R;
16 import android.app.ListActivity;
17 import android.os.Bundle;
18 import android.os.Handler;
19 import android.util.Log;
20 import android.widget.SimpleAdapter;
21
22 public class ExtensionsListActivity extends ListActivity {
23
24 private static final String packages_url = "http://alefzero.eu/a/packages.php";
25
26 private Thread mGetterThread;
27 private final Handler mHandler = new Handler();
28
29 @Override
30 protected void onCreate(Bundle savedInstanceState) {
31 super.onCreate(savedInstanceState);
32 mGetterThread = new Thread(new JsonGetter());
33 mGetterThread.start();
34 }
35
36 public void done(JSONArray a) {
37 LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
38 for (int i = 0; i < a.length(); ++i) {
39 try {
40 ExtensionApplicationEntry ela = new ExtensionApplicationEntry(
41 ((JSONObject) a.get(i)));
42 HashMap<String, String> ss = new HashMap<String, String>();
43 ss.put("NAME", ela.getName());
44 ss.put("DESC", ela.getDescription());
45 ll.add(ss);
46 } catch (JSONException e) {
47 e.printStackTrace();
48 }
49 }
50 setListAdapter(new SimpleAdapter(this, ll, R.layout.simple_list_item_2,
51 new String[] { "NAME", "DESC" }, new int[] {
52 android.R.id.text1, android.R.id.text2 }));
53
54 }
55
56 private class JsonGetter implements Runnable {
57
58 @Override
59 public void run() {
60 HttpClient hc = new HttpClient();
61 GetMethod gm = new GetMethod(packages_url);
62 final JSONArray ar;
63 try {
64 hc.executeMethod(gm);
65 Log.e("ASD", gm.getResponseBodyAsString() + "");
66 ar = new JSONObject(gm.getResponseBodyAsString())
67 .getJSONArray("apps");
68 } catch (Exception e) {
69 e.printStackTrace();
70 return;
71 }
72
73 mHandler.post(new Runnable() {
74 @Override
75 public void run() {
76 done(ar);
77 }
78 });
79
80 }
81
82 }
83
84 private class ExtensionApplicationEntry {
85 private static final String APP_NAME = "name";
86 private static final String APP_VERSION = "version";
87 private static final String APP_DESC = "description";
88 private static final String APP_ICON = "icon";
89 private static final String APP_URL = "download";
90 private static final String APP_PLAYID = "play_id";
91
92 private String mName, mDescription, mIcon, mDownload, mPlayId;
93 private OwnCloudVersion mVersion;
94
95 public ExtensionApplicationEntry(JSONObject appentry) {
96 try {
97 mName = appentry.getString(APP_NAME);
98 mDescription = appentry.getString(APP_DESC);
99 mIcon = appentry.getString(APP_ICON);
100 mDownload = appentry.getString(APP_URL);
101 mPlayId = appentry.getString(APP_PLAYID);
102 mVersion = new OwnCloudVersion(appentry.getString(APP_VERSION));
103 } catch (JSONException e) {
104 e.printStackTrace();
105 }
106 }
107
108 public String getName() {
109 return mName;
110 }
111
112 public String getDescription() {
113 return mDescription;
114 }
115
116 @SuppressWarnings("unused")
117 public String getIcon() {
118 return mIcon;
119 }
120
121 @SuppressWarnings("unused")
122 public String getDownload() {
123 return mDownload;
124 }
125
126 @SuppressWarnings("unused")
127 public String getPlayId() {
128 return mPlayId;
129 }
130
131 @SuppressWarnings("unused")
132 public OwnCloudVersion getVersion() {
133 return mVersion;
134 }
135 }
136
137 }