root / src / com / rackspacecloud / android / ContainerObjectsActivity.java @ 25881fb3
History | View | Annotate | Download (25.3 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Arrays; |
7 |
|
8 |
import javax.xml.parsers.FactoryConfigurationError; |
9 |
import javax.xml.parsers.ParserConfigurationException; |
10 |
import javax.xml.parsers.SAXParser; |
11 |
import javax.xml.parsers.SAXParserFactory; |
12 |
|
13 |
import org.apache.http.HttpResponse; |
14 |
import org.apache.http.client.ClientProtocolException; |
15 |
import org.apache.http.impl.client.BasicResponseHandler; |
16 |
import org.xml.sax.InputSource; |
17 |
import org.xml.sax.SAXException; |
18 |
import org.xml.sax.XMLReader; |
19 |
|
20 |
import android.app.Activity; |
21 |
import android.app.AlertDialog; |
22 |
import android.app.Dialog; |
23 |
import android.app.ListActivity; |
24 |
import android.app.ProgressDialog; |
25 |
import android.content.Context; |
26 |
import android.content.DialogInterface; |
27 |
import android.content.Intent; |
28 |
import android.os.AsyncTask; |
29 |
import android.os.Bundle; |
30 |
import android.util.Log; |
31 |
import android.view.LayoutInflater; |
32 |
import android.view.Menu; |
33 |
import android.view.MenuInflater; |
34 |
import android.view.MenuItem; |
35 |
import android.view.View; |
36 |
import android.view.ViewGroup; |
37 |
import android.widget.ArrayAdapter; |
38 |
import android.widget.EditText; |
39 |
import android.widget.ListView; |
40 |
import android.widget.TextView; |
41 |
|
42 |
import com.rackspace.cloud.files.api.client.Container; |
43 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
44 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
45 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
46 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
47 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
48 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
49 |
|
50 |
/**
|
51 |
*
|
52 |
* @author Phillip Toohill
|
53 |
*
|
54 |
*/
|
55 |
public class ContainerObjectsActivity extends ListActivity { |
56 |
|
57 |
private static final int deleteContainer = 0; |
58 |
private static final int deleteFolder = 1; |
59 |
private ContainerObjects[] files; |
60 |
private static Container container; |
61 |
public String LOG = "viewFilesActivity"; |
62 |
private String cdnEnabledIs; |
63 |
public Object megaBytes; |
64 |
public Object kiloBytes; |
65 |
public int bConver = 1048576; |
66 |
public int kbConver = 1024; |
67 |
private Context context; |
68 |
private String currentPath; |
69 |
private boolean loadingFiles; |
70 |
private boolean displayDialog; |
71 |
private ProgressDialog dialog;
|
72 |
private AndroidCloudApplication app;
|
73 |
private AddObjectListenerTask task;
|
74 |
private DeleteObjectListenerTask deleteObjTask;
|
75 |
private DeleteContainerListenerTask deleteContainerTask;
|
76 |
|
77 |
|
78 |
@Override
|
79 |
public void onCreate(Bundle savedInstanceState) { |
80 |
super.onCreate(savedInstanceState);
|
81 |
container = (Container) this.getIntent().getExtras().get("container"); |
82 |
Log.v(LOG, "CDNEnabled:" + container.isCdnEnabled());
|
83 |
context = getApplicationContext(); |
84 |
if (container.isCdnEnabled() == true) { |
85 |
cdnEnabledIs = "true";
|
86 |
} else {
|
87 |
cdnEnabledIs = "false";
|
88 |
} |
89 |
restoreState(savedInstanceState); |
90 |
} |
91 |
|
92 |
@Override
|
93 |
protected void onSaveInstanceState(Bundle outState) { |
94 |
super.onSaveInstanceState(outState);
|
95 |
|
96 |
//files stores all the files in the container
|
97 |
outState.putSerializable("container", files);
|
98 |
|
99 |
//current path represents where you have "navigated" to
|
100 |
outState.putString("path", currentPath);
|
101 |
|
102 |
//stores state if you are loading or not
|
103 |
outState.putBoolean("loadingFiles", loadingFiles);
|
104 |
|
105 |
//stores whether dialog is showing or not
|
106 |
outState.putBoolean("displayDialog", displayDialog);
|
107 |
|
108 |
//need to set authenticating back to true because it is set to false
|
109 |
//in hideDialog()
|
110 |
if(displayDialog){
|
111 |
hideDialog(); |
112 |
displayDialog = true;
|
113 |
} |
114 |
} |
115 |
|
116 |
|
117 |
|
118 |
private void restoreState(Bundle state) { |
119 |
|
120 |
|
121 |
/*
|
122 |
* need reference to the app so you can access curDirFiles
|
123 |
* as well as processing status
|
124 |
*/
|
125 |
app = (AndroidCloudApplication)this.getApplication();
|
126 |
|
127 |
if (state != null && state.containsKey("displayDialog") && state.getBoolean("displayDialog")) { |
128 |
showDialog(); |
129 |
} else {
|
130 |
hideDialog(); |
131 |
} |
132 |
|
133 |
if(state != null){ |
134 |
if(state.containsKey("path")){ |
135 |
currentPath = state.getString("path");
|
136 |
} |
137 |
else{
|
138 |
currentPath = "";
|
139 |
} |
140 |
|
141 |
if(state.containsKey("loadingFiles") && state.getBoolean("loadingFiles")){ |
142 |
loadFiles(); |
143 |
} |
144 |
else{
|
145 |
if(state.containsKey("container")){ |
146 |
files = (ContainerObjects[]) state.getSerializable("container"); |
147 |
if (app.getCurFiles() == null) { |
148 |
displayNoFilesCell(); |
149 |
} else {
|
150 |
getListView().setDividerHeight(1); // restore divider lines |
151 |
setListAdapter(new FileAdapter());
|
152 |
|
153 |
} |
154 |
} |
155 |
} |
156 |
} |
157 |
else {
|
158 |
currentPath = "";
|
159 |
loadFiles(); |
160 |
} |
161 |
|
162 |
/*
|
163 |
* if the app is process when we enter the activity
|
164 |
* we must listen for the new curDirFiles list
|
165 |
*/
|
166 |
if(app.isAddingObject()){
|
167 |
task = new AddObjectListenerTask();
|
168 |
task.execute(); |
169 |
} |
170 |
|
171 |
if(app.isDeletingObject()){
|
172 |
displayNoFilesCell(); |
173 |
deleteObjTask = new DeleteObjectListenerTask();
|
174 |
deleteObjTask.execute(); |
175 |
} |
176 |
|
177 |
if(app.isDeletingContainer()){
|
178 |
displayNoFilesCell(); |
179 |
deleteContainerTask = new DeleteContainerListenerTask();
|
180 |
deleteContainerTask.execute(); |
181 |
} |
182 |
|
183 |
|
184 |
} |
185 |
|
186 |
@Override
|
187 |
protected void onStart(){ |
188 |
super.onStart();
|
189 |
if(displayDialog){
|
190 |
showDialog(); |
191 |
} |
192 |
} |
193 |
|
194 |
|
195 |
@Override
|
196 |
protected void onStop(){ |
197 |
super.onStop();
|
198 |
|
199 |
if(displayDialog){
|
200 |
hideDialog(); |
201 |
displayDialog = true;
|
202 |
} |
203 |
|
204 |
/*
|
205 |
* Need to stop running listener task
|
206 |
* if we exit
|
207 |
*/
|
208 |
if(task != null){ |
209 |
task.cancel(true);
|
210 |
} |
211 |
|
212 |
if(deleteObjTask != null){ |
213 |
deleteObjTask.cancel(true);
|
214 |
} |
215 |
|
216 |
if(deleteContainerTask != null){ |
217 |
deleteContainerTask.cancel(true);
|
218 |
} |
219 |
|
220 |
} |
221 |
|
222 |
/*
|
223 |
* overriding back button press, because we are not actually changing
|
224 |
* activities when we navigate the file structure
|
225 |
*/
|
226 |
public void onBackPressed() { |
227 |
if(currentPath.equals("")){ |
228 |
finish(); |
229 |
} |
230 |
else{
|
231 |
goUpDirectory(); |
232 |
} |
233 |
} |
234 |
|
235 |
/*
|
236 |
* go to the current directory's parent and display that data
|
237 |
*/
|
238 |
private void goUpDirectory(){ |
239 |
currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1); |
240 |
loadCurrentDirectoryFiles(); |
241 |
displayCurrentFiles(); |
242 |
} |
243 |
|
244 |
/*
|
245 |
* load all file that are in the container
|
246 |
*/
|
247 |
private void loadFiles() { |
248 |
//displayLoadingCell();
|
249 |
new LoadFilesTask().execute();
|
250 |
} |
251 |
|
252 |
|
253 |
/* load only the files that should display for the
|
254 |
* current directory in the curDirFiles[]
|
255 |
*/
|
256 |
private void loadCurrentDirectoryFiles(){ |
257 |
ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>(); |
258 |
|
259 |
if(files != null){ |
260 |
for(int i = 0 ; i < files.length; i ++){ |
261 |
if(fileBelongsInDir(files[i])){
|
262 |
curFiles.add(files[i]); |
263 |
} |
264 |
} |
265 |
app.setCurFiles(curFiles); |
266 |
} |
267 |
} |
268 |
|
269 |
/*
|
270 |
* determines if a file should be displayed in current
|
271 |
* directory
|
272 |
*/
|
273 |
private Boolean fileBelongsInDir(ContainerObjects obj){ |
274 |
String objPath = obj.getCName();
|
275 |
if(!objPath.startsWith(currentPath)){
|
276 |
return false; |
277 |
} |
278 |
else{
|
279 |
objPath = objPath.substring(currentPath.length()); |
280 |
return !objPath.contains("/"); |
281 |
} |
282 |
} |
283 |
|
284 |
|
285 |
/*
|
286 |
* loads all the files that are in the container
|
287 |
* into one array
|
288 |
*/
|
289 |
private void setFileList(ArrayList<ContainerObjects> files) { |
290 |
if (files == null) { |
291 |
files = new ArrayList<ContainerObjects>(); |
292 |
} |
293 |
String[] fileNames = new String[files.size()]; |
294 |
this.files = new ContainerObjects[files.size()]; |
295 |
|
296 |
|
297 |
|
298 |
if (files != null) { |
299 |
for (int i = 0; i < files.size(); i++) { |
300 |
ContainerObjects file = files.get(i); |
301 |
this.files[i] = file;
|
302 |
fileNames[i] = file.getName(); |
303 |
} |
304 |
} |
305 |
} |
306 |
|
307 |
private void displayCurrentFiles(){ |
308 |
if (app.getCurFiles().size() == 0) { |
309 |
displayNoFilesCell(); |
310 |
} else {
|
311 |
ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>(); |
312 |
for(int i = 0; i < app.getCurFiles().size(); i++){ |
313 |
tempList.add(app.getCurFiles().get(i)); |
314 |
} |
315 |
getListView().setDividerHeight(1); // restore divider lines |
316 |
setListAdapter(new FileAdapter());
|
317 |
} |
318 |
} |
319 |
|
320 |
/*
|
321 |
* display a different empty page depending
|
322 |
* of if you are at top of container or
|
323 |
* in a folder
|
324 |
*/
|
325 |
private void displayNoFilesCell() { |
326 |
String a[] = new String[1]; |
327 |
if(currentPath.equals("")){ |
328 |
a[0] = "Empty Container"; |
329 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell, |
330 |
R.id.no_files_label, a)); |
331 |
} |
332 |
else{
|
333 |
a[0] = "No Files"; |
334 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell, |
335 |
R.id.no_files_label, a)); |
336 |
} |
337 |
getListView().setTextFilterEnabled(true);
|
338 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
339 |
// like a list row
|
340 |
getListView().setItemsCanFocus(false);
|
341 |
} |
342 |
|
343 |
|
344 |
private void showAlert(String title, String message) { |
345 |
// Can't create handler inside thread that has not called
|
346 |
// Looper.prepare()
|
347 |
// Looper.prepare();
|
348 |
try {
|
349 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
350 |
alert.setTitle(title); |
351 |
alert.setMessage(message); |
352 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
353 |
public void onClick(DialogInterface dialog, int which) { |
354 |
return;
|
355 |
} |
356 |
}); |
357 |
alert.show(); |
358 |
} catch (Exception e) { |
359 |
e.printStackTrace(); |
360 |
} |
361 |
} |
362 |
|
363 |
/* just get the last part of the filename
|
364 |
* so the entire file path does not show
|
365 |
*/
|
366 |
private String getShortName(String longName){ |
367 |
String s = longName;
|
368 |
if(!s.contains("/")){ |
369 |
return s;
|
370 |
} |
371 |
else {
|
372 |
return s.substring(s.lastIndexOf('/')+1); |
373 |
} |
374 |
} |
375 |
|
376 |
/*
|
377 |
* removed a specified object from the array of
|
378 |
* all the files in the container
|
379 |
*/
|
380 |
private void removeFromList(String path){ |
381 |
ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(Arrays.asList(files)); |
382 |
for(int i = 0; i < files.length; i++){ |
383 |
if(files[i].getCName().equals(path.substring(0, path.length()-1))){ |
384 |
temp.remove(i); |
385 |
} |
386 |
} |
387 |
files = new ContainerObjects[temp.size()];
|
388 |
for(int i = 0; i < temp.size(); i++){ |
389 |
files[i] = temp.get(i); |
390 |
} |
391 |
} |
392 |
|
393 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
394 |
if (app.getCurFiles() != null && app.getCurFiles().size() > 0) { |
395 |
Intent viewIntent; |
396 |
if(app.getCurFiles().get(position).getContentType().equals("application/directory")){ |
397 |
currentPath = app.getCurFiles().get(position).getCName() + "/";
|
398 |
loadCurrentDirectoryFiles(); |
399 |
displayCurrentFiles(); |
400 |
} |
401 |
|
402 |
else{
|
403 |
viewIntent = new Intent(this, ContainerObjectDetails.class); |
404 |
viewIntent.putExtra("container", app.getCurFiles().get(position));
|
405 |
viewIntent.putExtra("cdnUrl", container.getCdnUrl());
|
406 |
viewIntent.putExtra("containerNames", container.getName());
|
407 |
viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
|
408 |
startActivityForResult(viewIntent, 55); // arbitrary number; never |
409 |
// used again
|
410 |
} |
411 |
} |
412 |
} |
413 |
|
414 |
/*
|
415 |
* Create the Menu options
|
416 |
*/
|
417 |
@Override
|
418 |
public boolean onCreateOptionsMenu(Menu menu) { |
419 |
super.onCreateOptionsMenu(menu);
|
420 |
MenuInflater inflater = getMenuInflater(); |
421 |
inflater.inflate(R.menu.view_container_object_list_menu, menu); |
422 |
return true; |
423 |
} |
424 |
|
425 |
@Override
|
426 |
/*
|
427 |
* option performed for delete depends on if you
|
428 |
* are at the top of a container or in a folder
|
429 |
*/
|
430 |
public boolean onOptionsItemSelected(MenuItem item) { |
431 |
switch (item.getItemId()) {
|
432 |
case R.id.delete_container:
|
433 |
if(currentPath.equals("")){ |
434 |
showDialog(deleteContainer); |
435 |
} |
436 |
else{
|
437 |
showDialog(deleteFolder); |
438 |
} |
439 |
return true; |
440 |
case R.id.enable_cdn:
|
441 |
Intent viewIntent1 = new Intent(this, EnableCDNActivity.class); |
442 |
viewIntent1.putExtra("Cname", container.getName());
|
443 |
startActivityForResult(viewIntent1, 56);
|
444 |
return true; |
445 |
case R.id.refresh:
|
446 |
loadFiles(); |
447 |
return true; |
448 |
case R.id.add_folder:
|
449 |
showDialog(R.id.add_folder); |
450 |
return true; |
451 |
case R.id.add_file:
|
452 |
Intent viewIntent2 = new Intent(this, AddFileActivity.class); |
453 |
viewIntent2.putExtra("Cname", container.getName());
|
454 |
viewIntent2.putExtra("curPath", currentPath);
|
455 |
startActivityForResult(viewIntent2, 56);
|
456 |
return true; |
457 |
} |
458 |
return false; |
459 |
} |
460 |
|
461 |
@Override
|
462 |
protected Dialog onCreateDialog(int id) { |
463 |
switch (id) {
|
464 |
case deleteContainer:
|
465 |
if(app.getCurFiles().size() == 0){ |
466 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
467 |
.setIcon(R.drawable.alert_dialog_icon) |
468 |
.setTitle("Delete Container")
|
469 |
.setMessage( |
470 |
"Are you sure you want to delete this Container?")
|
471 |
.setPositiveButton("Delete Container",
|
472 |
new DialogInterface.OnClickListener() {
|
473 |
public void onClick(DialogInterface dialog, |
474 |
int whichButton) {
|
475 |
// User clicked OK so do some stuff
|
476 |
new DeleteContainerTask()
|
477 |
.execute(currentPath); |
478 |
} |
479 |
}) |
480 |
.setNegativeButton("Cancel",
|
481 |
new DialogInterface.OnClickListener() {
|
482 |
public void onClick(DialogInterface dialog, |
483 |
int whichButton) {
|
484 |
// User clicked Cancel so do some stuff
|
485 |
} |
486 |
}).create(); |
487 |
} |
488 |
else{
|
489 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
490 |
.setIcon(R.drawable.alert_dialog_icon) |
491 |
.setTitle("Delete Container")
|
492 |
.setMessage("Container must be empty to delete")
|
493 |
.setNegativeButton("OK",
|
494 |
new DialogInterface.OnClickListener() {
|
495 |
public void onClick(DialogInterface dialog, |
496 |
int whichButton) {
|
497 |
// User clicked Cancel so do some stuff
|
498 |
} |
499 |
}).create(); |
500 |
} |
501 |
case deleteFolder:
|
502 |
if(app.getCurFiles().size() == 0){ |
503 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
504 |
.setIcon(R.drawable.alert_dialog_icon) |
505 |
.setTitle("Delete Folder")
|
506 |
.setMessage( |
507 |
"Are you sure you want to delete this Folder?")
|
508 |
.setPositiveButton("Delete Folder",
|
509 |
new DialogInterface.OnClickListener() {
|
510 |
public void onClick(DialogInterface dialog, |
511 |
int whichButton) {
|
512 |
// User clicked OK so do some stuff
|
513 |
new DeleteObjectTask().execute();
|
514 |
} |
515 |
}) |
516 |
.setNegativeButton("Cancel",
|
517 |
new DialogInterface.OnClickListener() {
|
518 |
public void onClick(DialogInterface dialog, |
519 |
int whichButton) {
|
520 |
// User clicked Cancel so do some stuff
|
521 |
} |
522 |
}).create(); |
523 |
} |
524 |
else{
|
525 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
526 |
.setIcon(R.drawable.alert_dialog_icon) |
527 |
.setTitle("Delete Folder")
|
528 |
.setMessage( |
529 |
"Folder must be empty to delete")
|
530 |
.setNegativeButton("OK",
|
531 |
new DialogInterface.OnClickListener() {
|
532 |
public void onClick(DialogInterface dialog, |
533 |
int whichButton) {
|
534 |
// User clicked Cancel so do some stuff
|
535 |
} |
536 |
}).create(); |
537 |
} |
538 |
case R.id.add_folder:
|
539 |
final EditText input = new EditText(this); |
540 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
541 |
.setIcon(R.drawable.alert_dialog_icon) |
542 |
.setView(input) |
543 |
.setTitle("Add Folder")
|
544 |
.setMessage("Enter new name for folder: ")
|
545 |
.setPositiveButton("Add", new DialogInterface.OnClickListener() { |
546 |
public void onClick(DialogInterface dialog, int whichButton) { |
547 |
//User clicked OK so do some stuff
|
548 |
String[] info = {input.getText().toString(), "application/directory"}; |
549 |
new AddFolderTask().execute(info);
|
550 |
} |
551 |
}) |
552 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
553 |
public void onClick(DialogInterface dialog, int whichButton) { |
554 |
// User clicked Cancel so do some stuff
|
555 |
} |
556 |
}) |
557 |
.create(); |
558 |
} |
559 |
return null; |
560 |
} |
561 |
|
562 |
@Override
|
563 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
564 |
super.onActivityResult(requestCode, resultCode, data);
|
565 |
|
566 |
if (resultCode == RESULT_OK && requestCode == 56) { |
567 |
// a sub-activity kicked back, so we want to refresh the server list
|
568 |
loadFiles(); |
569 |
} |
570 |
|
571 |
// deleted file so need to update the list
|
572 |
if (requestCode == 55 && resultCode == 99) { |
573 |
loadFiles(); |
574 |
} |
575 |
|
576 |
} |
577 |
|
578 |
private CloudServersException parseCloudServersException(
|
579 |
HttpResponse response) { |
580 |
CloudServersException cse = new CloudServersException();
|
581 |
try {
|
582 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
583 |
String body = responseHandler.handleResponse(response);
|
584 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
585 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
586 |
XMLReader xmlReader = saxParser.getXMLReader(); |
587 |
xmlReader.setContentHandler(parser); |
588 |
xmlReader.parse(new InputSource(new StringReader(body))); |
589 |
cse = parser.getException(); |
590 |
} catch (ClientProtocolException e) {
|
591 |
cse = new CloudServersException();
|
592 |
cse.setMessage(e.getLocalizedMessage()); |
593 |
} catch (IOException e) { |
594 |
cse = new CloudServersException();
|
595 |
cse.setMessage(e.getLocalizedMessage()); |
596 |
} catch (ParserConfigurationException e) { |
597 |
cse = new CloudServersException();
|
598 |
cse.setMessage(e.getLocalizedMessage()); |
599 |
} catch (SAXException e) {
|
600 |
cse = new CloudServersException();
|
601 |
cse.setMessage(e.getLocalizedMessage()); |
602 |
} catch (FactoryConfigurationError e) { |
603 |
cse = new CloudServersException();
|
604 |
cse.setMessage(e.getLocalizedMessage()); |
605 |
} |
606 |
return cse;
|
607 |
} |
608 |
|
609 |
private void startFileError(String message, HttpBundle bundle){ |
610 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
611 |
viewIntent.putExtra("errorMessage", message);
|
612 |
viewIntent.putExtra("response", bundle.getResponseText());
|
613 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
614 |
startActivity(viewIntent); |
615 |
} |
616 |
|
617 |
private void showDialog() { |
618 |
if(dialog == null || !dialog.isShowing()){ |
619 |
displayDialog = true;
|
620 |
dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Loading...", true); |
621 |
} |
622 |
} |
623 |
|
624 |
private void hideDialog() { |
625 |
if(dialog != null){ |
626 |
dialog.dismiss(); |
627 |
} |
628 |
displayDialog = false;
|
629 |
} |
630 |
|
631 |
class FileAdapter extends ArrayAdapter<ContainerObjects> { |
632 |
FileAdapter() { |
633 |
super(ContainerObjectsActivity.this,
|
634 |
R.layout.listcontainerobjectcell, app.getCurFiles()); |
635 |
} |
636 |
|
637 |
public View getView(int position, View convertView, ViewGroup parent) { |
638 |
ContainerObjects file = app.getCurFiles().get(position); |
639 |
LayoutInflater inflater = getLayoutInflater(); |
640 |
View row = inflater.inflate(R.layout.listcontainerobjectcell,
|
641 |
parent, false);
|
642 |
|
643 |
TextView label = (TextView) row.findViewById(R.id.label); |
644 |
//label.setText(file.getCName());
|
645 |
label.setText(getShortName(file.getCName())); |
646 |
|
647 |
if (file.getBytes() >= bConver) {
|
648 |
megaBytes = Math.abs(file.getBytes() / bConver + 0.2); |
649 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
650 |
sublabel.setText(megaBytes + " MB");
|
651 |
} else if (file.getBytes() >= kbConver) { |
652 |
kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2); |
653 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
654 |
sublabel.setText(kiloBytes + " KB");
|
655 |
} else {
|
656 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
657 |
sublabel.setText(file.getBytes() + " B");
|
658 |
} |
659 |
|
660 |
return (row);
|
661 |
} |
662 |
} |
663 |
|
664 |
private class LoadFilesTask extends |
665 |
AsyncTask<String, Void, ArrayList<ContainerObjects>> { |
666 |
|
667 |
private CloudServersException exception;
|
668 |
protected void onPreExecute(){ |
669 |
showDialog(); |
670 |
loadingFiles = true;
|
671 |
} |
672 |
|
673 |
@Override
|
674 |
protected ArrayList<ContainerObjects> doInBackground(String... path) { |
675 |
ArrayList<ContainerObjects> files = null; |
676 |
try {
|
677 |
files = (new ContainerObjectManager(context)).createList(true, |
678 |
container.getName()); |
679 |
} catch (CloudServersException e) {
|
680 |
exception = e; |
681 |
e.printStackTrace(); |
682 |
} |
683 |
return files;
|
684 |
} |
685 |
|
686 |
@Override
|
687 |
protected void onPostExecute(ArrayList<ContainerObjects> result) { |
688 |
hideDialog(); |
689 |
if (exception != null) { |
690 |
showAlert("Error", exception.getMessage());
|
691 |
} |
692 |
|
693 |
setFileList(result); |
694 |
loadCurrentDirectoryFiles(); |
695 |
displayCurrentFiles(); |
696 |
loadingFiles = false;
|
697 |
} |
698 |
|
699 |
} |
700 |
|
701 |
private class AddFolderTask extends |
702 |
AsyncTask<String, Void, HttpBundle> { |
703 |
|
704 |
private CloudServersException exception;
|
705 |
|
706 |
@Override
|
707 |
protected void onPreExecute(){ |
708 |
showDialog(); |
709 |
app.setAddingObject(true);
|
710 |
task = new AddObjectListenerTask();
|
711 |
task.execute(); |
712 |
} |
713 |
|
714 |
@Override
|
715 |
protected HttpBundle doInBackground(String... data) { |
716 |
HttpBundle bundle = null;
|
717 |
try {
|
718 |
|
719 |
bundle = (new ContainerObjectManager(context)).addObject(container.getName(), currentPath, data[0], data[1]); |
720 |
} catch (CloudServersException e) {
|
721 |
exception = e; |
722 |
} |
723 |
return bundle;
|
724 |
} |
725 |
|
726 |
@Override
|
727 |
protected void onPostExecute(HttpBundle bundle) { |
728 |
app.setAddingObject(false);
|
729 |
HttpResponse response = bundle.getResponse(); |
730 |
if (response != null) { |
731 |
int statusCode = response.getStatusLine().getStatusCode();
|
732 |
if (statusCode == 201) { |
733 |
setResult(Activity.RESULT_OK); |
734 |
//loading the new files is done by ListenerTask
|
735 |
} else {
|
736 |
hideDialog(); |
737 |
CloudServersException cse = parseCloudServersException(response); |
738 |
if ("".equals(cse.getMessage())) { |
739 |
startFileError("There was a problem deleting your folder.", bundle);
|
740 |
} else {
|
741 |
startFileError("There was a problem deleting your folder: "
|
742 |
+ cse.getMessage(), bundle); |
743 |
} |
744 |
} |
745 |
} else if (exception != null) { |
746 |
hideDialog(); |
747 |
startFileError("There was a problem deleting your folder: "
|
748 |
+ exception.getMessage(), bundle); |
749 |
} |
750 |
} |
751 |
} |
752 |
|
753 |
private class DeleteObjectTask extends |
754 |
AsyncTask<Void, Void, HttpBundle> { |
755 |
|
756 |
private CloudServersException exception;
|
757 |
|
758 |
@Override
|
759 |
protected void onPreExecute(){ |
760 |
showDialog(); |
761 |
app.setDeleteingObject(true);
|
762 |
deleteObjTask = new DeleteObjectListenerTask();
|
763 |
deleteObjTask.execute(); |
764 |
} |
765 |
|
766 |
@Override
|
767 |
protected HttpBundle doInBackground(Void... arg0) { |
768 |
HttpBundle bundle = null;
|
769 |
try {
|
770 |
//subtring because the current directory contains a "/" at the end of the string
|
771 |
bundle = (new ContainerObjectManager(context)).deleteObject(container.getName(), currentPath.substring(0, currentPath.length()-1)); |
772 |
} catch (CloudServersException e) {
|
773 |
exception = e; |
774 |
} |
775 |
return bundle;
|
776 |
} |
777 |
|
778 |
@Override
|
779 |
protected void onPostExecute(HttpBundle bundle) { |
780 |
app.setDeleteingObject(false);
|
781 |
HttpResponse response = bundle.getResponse(); |
782 |
if (response != null) { |
783 |
int statusCode = response.getStatusLine().getStatusCode();
|
784 |
if (statusCode == 409) { |
785 |
hideDialog(); |
786 |
showAlert("Error",
|
787 |
"Folder must be empty in order to delete");
|
788 |
} |
789 |
if (statusCode == 204) { |
790 |
setResult(Activity.RESULT_OK); |
791 |
} else {
|
792 |
hideDialog(); |
793 |
CloudServersException cse = parseCloudServersException(response); |
794 |
if ("".equals(cse.getMessage())) { |
795 |
startFileError("There was a problem deleting your folder.", bundle);
|
796 |
} else {
|
797 |
startFileError("There was a problem deleting your folder: "
|
798 |
+ cse.getMessage(), bundle); |
799 |
} |
800 |
} |
801 |
} else if (exception != null) { |
802 |
hideDialog(); |
803 |
startFileError("There was a problem deleting your folder: "
|
804 |
+ exception.getMessage(), bundle); |
805 |
} |
806 |
} |
807 |
} |
808 |
|
809 |
private class DeleteContainerTask extends |
810 |
AsyncTask<String, Void, HttpBundle> { |
811 |
|
812 |
private CloudServersException exception;
|
813 |
|
814 |
@Override
|
815 |
protected void onPreExecute(){ |
816 |
showDialog(); |
817 |
app.setDeletingContainer(true);
|
818 |
deleteContainerTask = new DeleteContainerListenerTask();
|
819 |
deleteContainerTask.execute(); |
820 |
} |
821 |
|
822 |
@Override
|
823 |
protected HttpBundle doInBackground(String... object) { |
824 |
HttpBundle bundle = null;
|
825 |
try {
|
826 |
|
827 |
bundle = (new ContainerManager(context)).delete(container.getName());
|
828 |
} catch (CloudServersException e) {
|
829 |
exception = e; |
830 |
} |
831 |
return bundle;
|
832 |
} |
833 |
|
834 |
@Override
|
835 |
protected void onPostExecute(HttpBundle bundle) { |
836 |
hideDialog(); |
837 |
app.setDeletingContainer(false);
|
838 |
HttpResponse response = bundle.getResponse(); |
839 |
if (response != null) { |
840 |
int statusCode = response.getStatusLine().getStatusCode();
|
841 |
if (statusCode == 409) { |
842 |
startFileError("Container must be empty in order to delete", bundle);
|
843 |
} |
844 |
if (statusCode == 204) { |
845 |
setResult(Activity.RESULT_OK); |
846 |
|
847 |
} else {
|
848 |
CloudServersException cse = parseCloudServersException(response); |
849 |
if ("".equals(cse.getMessage())) { |
850 |
startFileError("There was a problem deleting your container.", bundle);
|
851 |
} else {
|
852 |
startFileError("There was a problem deleting your container: "
|
853 |
+ cse.getMessage(), bundle); |
854 |
} |
855 |
} |
856 |
} else if (exception != null) { |
857 |
startFileError("There was a problem deleting your server: "
|
858 |
+ exception.getMessage(), bundle); |
859 |
} |
860 |
} |
861 |
} |
862 |
|
863 |
/*
|
864 |
* listens for the application to change isProcessing
|
865 |
* listens so activity knows when it should display
|
866 |
* the new curDirFiles
|
867 |
*/
|
868 |
private class AddObjectListenerTask extends |
869 |
AsyncTask<Void, Void, Void> { |
870 |
|
871 |
@Override
|
872 |
protected Void doInBackground(Void... arg1) { |
873 |
|
874 |
while(app.isAddingObject()){
|
875 |
// wait for process to finish
|
876 |
// or have it be canceled
|
877 |
if(task.isCancelled()){
|
878 |
return null; |
879 |
} |
880 |
} |
881 |
return null; |
882 |
} |
883 |
|
884 |
/*
|
885 |
* when no longer processing, time to load
|
886 |
* the new files
|
887 |
*/
|
888 |
@Override
|
889 |
protected void onPostExecute(Void arg1) { |
890 |
loadFiles(); |
891 |
} |
892 |
} |
893 |
|
894 |
|
895 |
private class DeleteObjectListenerTask extends |
896 |
AsyncTask<Void, Void, Void> { |
897 |
|
898 |
@Override
|
899 |
protected Void doInBackground(Void... arg1) { |
900 |
|
901 |
while(app.isDeletingObject()){
|
902 |
// wait for process to finish
|
903 |
// or have it be canceled
|
904 |
if(deleteObjTask.isCancelled()){
|
905 |
return null; |
906 |
} |
907 |
} |
908 |
return null; |
909 |
} |
910 |
|
911 |
/*
|
912 |
* when no longer processing, time to load
|
913 |
* the new files
|
914 |
*/
|
915 |
@Override
|
916 |
protected void onPostExecute(Void arg1) { |
917 |
hideDialog(); |
918 |
removeFromList(currentPath); |
919 |
goUpDirectory(); |
920 |
} |
921 |
} |
922 |
|
923 |
private class DeleteContainerListenerTask extends |
924 |
AsyncTask<Void, Void, Void> { |
925 |
|
926 |
@Override
|
927 |
protected Void doInBackground(Void... arg1) { |
928 |
|
929 |
while(app.isDeletingContainer()){
|
930 |
// wait for process to finish
|
931 |
// or have it be canceled
|
932 |
if(deleteContainerTask.isCancelled()){
|
933 |
return null; |
934 |
} |
935 |
} |
936 |
return null; |
937 |
} |
938 |
|
939 |
/*
|
940 |
* when no longer processing, time to load
|
941 |
* the new files
|
942 |
*/
|
943 |
@Override
|
944 |
protected void onPostExecute(Void arg1) { |
945 |
|
946 |
hideDialog(); |
947 |
setResult(RESULT_OK); |
948 |
finish(); |
949 |
} |
950 |
} |
951 |
} |