root / src / com / rackspacecloud / android / ListContainerActivity.java @ 25881fb3
History | View | Annotate | Download (8.4 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import android.app.AlertDialog; |
6 |
import android.app.ListActivity; |
7 |
import android.content.Context; |
8 |
import android.content.DialogInterface; |
9 |
import android.content.Intent; |
10 |
import android.os.AsyncTask; |
11 |
import android.os.Bundle; |
12 |
import android.util.Log; |
13 |
import android.view.LayoutInflater; |
14 |
import android.view.Menu; |
15 |
import android.view.MenuInflater; |
16 |
import android.view.MenuItem; |
17 |
import android.view.View; |
18 |
import android.view.ViewGroup; |
19 |
import android.widget.ArrayAdapter; |
20 |
import android.widget.ListView; |
21 |
import android.widget.TextView; |
22 |
|
23 |
import com.rackspace.cloud.files.api.client.Container; |
24 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
25 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author Phillip Toohill
|
30 |
*
|
31 |
*/
|
32 |
public class ListContainerActivity extends ListActivity { |
33 |
|
34 |
protected static final int DELETE_ID = 0; |
35 |
|
36 |
private Container[] containers; |
37 |
public Container container; |
38 |
public Container cdnContainer; |
39 |
public String[] containerNames; |
40 |
public Object megaBytes; |
41 |
public Object kiloBytes; |
42 |
public int bConver = 1048576; |
43 |
public int kbConver = 1024; |
44 |
private Context context; |
45 |
private boolean loading; |
46 |
|
47 |
@Override
|
48 |
public void onCreate(Bundle savedInstanceState) { |
49 |
super.onCreate(savedInstanceState);
|
50 |
context = getApplicationContext(); |
51 |
restoreState(savedInstanceState); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
protected void onSaveInstanceState(Bundle outState) { |
56 |
super.onSaveInstanceState(outState);
|
57 |
outState.putSerializable("container", containers);
|
58 |
outState.putBoolean("loading", loading);
|
59 |
} |
60 |
|
61 |
private void restoreState(Bundle state) { |
62 |
if(state != null && state.containsKey("loading") && state.getBoolean("loading")){ |
63 |
loadContainers(); |
64 |
registerForContextMenu(getListView()); |
65 |
} |
66 |
else if (state != null && state.containsKey("container") && state.getSerializable("container") != null) { |
67 |
containers = (Container[]) state.getSerializable("container"); |
68 |
if (containers.length == 0) { |
69 |
displayNoServersCell(); |
70 |
} else {
|
71 |
getListView().setDividerHeight(1); // restore divider lines |
72 |
setListAdapter(new FileAdapter());
|
73 |
} |
74 |
} else {
|
75 |
loadContainers(); |
76 |
registerForContextMenu(getListView()); |
77 |
} |
78 |
} |
79 |
|
80 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
81 |
if (containers != null && containers.length > 0) { |
82 |
Intent viewIntent = new Intent(this, ContainerObjectsActivity.class); |
83 |
viewIntent.putExtra("container", containers[position]);
|
84 |
startActivityForResult(viewIntent, 55);
|
85 |
} |
86 |
} |
87 |
|
88 |
private void loadContainers() { |
89 |
displayLoadingCell(); |
90 |
new LoadContainersTask().execute((Void[]) null); |
91 |
} |
92 |
|
93 |
private void setContainerList() { |
94 |
if (containerNames.length == 0) { |
95 |
displayNoServersCell(); |
96 |
} else {
|
97 |
getListView().setDividerHeight(1); // restore divider lines |
98 |
setListAdapter(new FileAdapter());
|
99 |
} |
100 |
} |
101 |
|
102 |
private void displayLoadingCell() { |
103 |
String a[] = new String[1]; |
104 |
a[0] = "Loading..."; |
105 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell, |
106 |
R.id.loading_label, a)); |
107 |
getListView().setTextFilterEnabled(true);
|
108 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
109 |
// like a list row
|
110 |
getListView().setItemsCanFocus(false);
|
111 |
} |
112 |
|
113 |
private void displayNoServersCell() { |
114 |
String a[] = new String[1]; |
115 |
a[0] = "No Files"; |
116 |
setListAdapter(new ArrayAdapter<String>(this, |
117 |
R.layout.nocontainerscell, R.id.no_containers_label, a)); |
118 |
getListView().setTextFilterEnabled(true);
|
119 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
120 |
// like a list row
|
121 |
getListView().setItemsCanFocus(false);
|
122 |
} |
123 |
|
124 |
private void showAlert(String title, String message) { |
125 |
// Can't create handler inside thread that has not called
|
126 |
// Looper.prepare()
|
127 |
// Looper.prepare();
|
128 |
try {
|
129 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
130 |
alert.setTitle(title); |
131 |
alert.setMessage(message); |
132 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
133 |
public void onClick(DialogInterface dialog, int which) { |
134 |
return;
|
135 |
} |
136 |
}); |
137 |
alert.show(); |
138 |
} catch (Exception e) { |
139 |
e.printStackTrace(); |
140 |
} |
141 |
} |
142 |
|
143 |
|
144 |
private class LoadContainersTask extends |
145 |
AsyncTask<Void, Void, ArrayList<Container>> { |
146 |
|
147 |
private CloudServersException exception;
|
148 |
|
149 |
@Override
|
150 |
protected void onPreExecute(){ |
151 |
loading = true;
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
protected ArrayList<Container> doInBackground(Void... arg0) { |
156 |
ArrayList<Container> containers = null; |
157 |
|
158 |
try {
|
159 |
containers = (new ContainerManager(context)).createList(true); |
160 |
} catch (CloudServersException e) {
|
161 |
exception = e; |
162 |
} |
163 |
return containers;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
protected void onPostExecute(ArrayList<Container> result) { |
168 |
if (exception != null) { |
169 |
showAlert("Error", exception.getMessage());
|
170 |
} |
171 |
ArrayList<Container> containerList = result; |
172 |
containerNames = new String[containerList.size()]; |
173 |
containers = new Container[containerList.size()]; |
174 |
if (containerList != null) { |
175 |
for (int i = 0; i < containerList.size(); i++) { |
176 |
Container container = containerList.get(i);
|
177 |
containers[i] = container; |
178 |
containerNames[i] = container.getName(); |
179 |
} |
180 |
} |
181 |
loading = false;
|
182 |
new LoadCDNContainersTask().execute((Void[]) null); |
183 |
} |
184 |
} |
185 |
|
186 |
private class LoadCDNContainersTask extends |
187 |
AsyncTask<Void, Void, ArrayList<Container>> { |
188 |
|
189 |
private CloudServersException exception;
|
190 |
|
191 |
@Override
|
192 |
protected void onPreExecute(){ |
193 |
loading = true;
|
194 |
} |
195 |
|
196 |
@Override
|
197 |
protected ArrayList<Container> doInBackground(Void... arg0) { |
198 |
ArrayList<Container> cdnContainers = null; |
199 |
|
200 |
try {
|
201 |
cdnContainers = (new ContainerManager(context)).createCDNList(true); |
202 |
} catch (CloudServersException e) {
|
203 |
exception = e; |
204 |
} |
205 |
return cdnContainers;
|
206 |
} |
207 |
|
208 |
@Override
|
209 |
protected void onPostExecute(ArrayList<Container> result) { |
210 |
if (exception != null) { |
211 |
showAlert("Error", exception.getMessage());
|
212 |
} |
213 |
|
214 |
ArrayList<Container> cdnContainers = result; |
215 |
|
216 |
for (int i = 0; i < containers.length; i++) { |
217 |
Container container = containers[i];
|
218 |
for (int t = 0; t < cdnContainers.size(); t++) { |
219 |
Container cdnContainer = cdnContainers.get(t);
|
220 |
if (container.getName().equals(cdnContainer.getName())) {
|
221 |
container.setCdnEnabled(cdnContainer.isCdnEnabled()); |
222 |
container.setCdnUrl(cdnContainer.getCdnUrl()); |
223 |
container.setTtl(cdnContainer.getTtl()); |
224 |
} |
225 |
} |
226 |
} |
227 |
setContainerList(); |
228 |
loading = false;
|
229 |
} |
230 |
} |
231 |
|
232 |
@Override
|
233 |
public boolean onCreateOptionsMenu(Menu menu) { |
234 |
super.onCreateOptionsMenu(menu);
|
235 |
MenuInflater inflater = getMenuInflater(); |
236 |
inflater.inflate(R.menu.container_list_menu, menu); |
237 |
return true; |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public boolean onOptionsItemSelected(MenuItem item) { |
242 |
switch (item.getItemId()) {
|
243 |
case R.id.add_container:
|
244 |
startActivityForResult( |
245 |
new Intent(this, AddContainerActivity.class), 56); // arbitrary number never used again |
246 |
return true; |
247 |
case R.id.refresh:
|
248 |
containers = null;
|
249 |
loadContainers(); |
250 |
return true; |
251 |
} |
252 |
return false; |
253 |
} |
254 |
|
255 |
class FileAdapter extends ArrayAdapter<Container> { |
256 |
FileAdapter() { |
257 |
super(ListContainerActivity.this, R.layout.listcontainerscell,
|
258 |
containers); |
259 |
} |
260 |
|
261 |
public View getView(int position, View convertView, ViewGroup parent) { |
262 |
|
263 |
Container container = containers[position];
|
264 |
|
265 |
LayoutInflater inflater = getLayoutInflater(); |
266 |
View row = inflater.inflate(R.layout.listcontainerscell, parent,
|
267 |
false);
|
268 |
|
269 |
TextView label = (TextView) row.findViewById(R.id.label); |
270 |
label.setText(container.getName()); |
271 |
|
272 |
if (container.getBytes() >= bConver) {
|
273 |
megaBytes = Math.abs(container.getBytes() / bConver + 0.2); |
274 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
275 |
sublabel.setText(container.getCount() + " Objects " + megaBytes
|
276 |
+ " MB");
|
277 |
} else if (container.getBytes() >= kbConver) { |
278 |
kiloBytes = Math.abs(container.getBytes() / kbConver + 0.2); |
279 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
280 |
sublabel.setText(container.getCount() + " Objects " + kiloBytes
|
281 |
+ " KB");
|
282 |
} else {
|
283 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
284 |
sublabel.setText(container.getCount() + " Objects "
|
285 |
+ container.getBytes() + " B");
|
286 |
} |
287 |
|
288 |
return (row);
|
289 |
} |
290 |
} |
291 |
|
292 |
@Override
|
293 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
294 |
super.onActivityResult(requestCode, resultCode, data);
|
295 |
|
296 |
if (resultCode == RESULT_OK) {
|
297 |
// a sub-activity kicked back, so we want to refresh the server list
|
298 |
loadContainers(); |
299 |
} |
300 |
} |
301 |
|
302 |
} |