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