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