0fe3c6c11f83c178611958a91a579896f59dd5f3
[pub/Android/ownCloud.git] / src / com / owncloud / android / extensions / ExtensionsListActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.extensions;
20
21 import java.util.HashMap;
22 import java.util.LinkedList;
23
24 import org.apache.commons.httpclient.HttpClient;
25 import org.apache.commons.httpclient.methods.GetMethod;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29
30 import com.owncloud.android.utils.OwnCloudVersion;
31
32
33 import android.R;
34 import android.app.ListActivity;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.util.Log;
38 import android.widget.SimpleAdapter;
39
40 public class ExtensionsListActivity extends ListActivity {
41
42 private static final String packages_url = "http://alefzero.eu/a/packages.php";
43
44 private Thread mGetterThread;
45 private final Handler mHandler = new Handler();
46
47 @Override
48 protected void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50 mGetterThread = new Thread(new JsonGetter());
51 mGetterThread.start();
52 }
53
54 public void done(JSONArray a) {
55 LinkedList<HashMap<String, String>> ll = new LinkedList<HashMap<String, String>>();
56 for (int i = 0; i < a.length(); ++i) {
57 try {
58 ExtensionApplicationEntry ela = new ExtensionApplicationEntry(
59 ((JSONObject) a.get(i)));
60 HashMap<String, String> ss = new HashMap<String, String>();
61 ss.put("NAME", ela.getName());
62 ss.put("DESC", ela.getDescription());
63 ll.add(ss);
64 } catch (JSONException e) {
65 e.printStackTrace();
66 }
67 }
68 setListAdapter(new SimpleAdapter(this, ll, R.layout.simple_list_item_2,
69 new String[] { "NAME", "DESC" }, new int[] {
70 android.R.id.text1, android.R.id.text2 }));
71
72 }
73
74 private class JsonGetter implements Runnable {
75
76 @Override
77 public void run() {
78 HttpClient hc = new HttpClient();
79 GetMethod gm = new GetMethod(packages_url);
80 final JSONArray ar;
81 try {
82 hc.executeMethod(gm);
83 Log.e("ASD", gm.getResponseBodyAsString() + "");
84 ar = new JSONObject(gm.getResponseBodyAsString())
85 .getJSONArray("apps");
86 } catch (Exception e) {
87 e.printStackTrace();
88 return;
89 }
90
91 mHandler.post(new Runnable() {
92 @Override
93 public void run() {
94 done(ar);
95 }
96 });
97
98 }
99
100 }
101
102 private class ExtensionApplicationEntry {
103 private static final String APP_NAME = "name";
104 private static final String APP_VERSION = "version";
105 private static final String APP_DESC = "description";
106 private static final String APP_ICON = "icon";
107 private static final String APP_URL = "download";
108 private static final String APP_PLAYID = "play_id";
109
110 private String mName, mDescription, mIcon, mDownload, mPlayId;
111 private OwnCloudVersion mVersion;
112
113 public ExtensionApplicationEntry(JSONObject appentry) {
114 try {
115 mName = appentry.getString(APP_NAME);
116 mDescription = appentry.getString(APP_DESC);
117 mIcon = appentry.getString(APP_ICON);
118 mDownload = appentry.getString(APP_URL);
119 mPlayId = appentry.getString(APP_PLAYID);
120 mVersion = new OwnCloudVersion(appentry.getString(APP_VERSION));
121 } catch (JSONException e) {
122 e.printStackTrace();
123 }
124 }
125
126 public String getName() {
127 return mName;
128 }
129
130 public String getDescription() {
131 return mDescription;
132 }
133
134 @SuppressWarnings("unused")
135 public String getIcon() {
136 return mIcon;
137 }
138
139 @SuppressWarnings("unused")
140 public String getDownload() {
141 return mDownload;
142 }
143
144 @SuppressWarnings("unused")
145 public String getPlayId() {
146 return mPlayId;
147 }
148
149 @SuppressWarnings("unused")
150 public OwnCloudVersion getVersion() {
151 return mVersion;
152 }
153 }
154
155 }