2d4137c0cef131a74b4d53d7c69f39b5ea260a29
[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(((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,
51 ll,
52 R.layout.simple_list_item_2,
53 new String[] {"NAME", "DESC"},
54 new int[] {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()).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() { return mName; }
110 public String getDescription() { return mDescription; }
111 public String getIcon() { return mIcon; }
112 public String getDownload() { return mDownload; }
113 public String getPlayId() { return mPlayId; }
114 public OwnCloudVersion getVersion() { return mVersion; }
115 }
116 }