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