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