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