Revision 37a14877 src/com/rackspacecloud/android/ContainerObjectsActivity.java
b/src/com/rackspacecloud/android/ContainerObjectsActivity.java | ||
---|---|---|
51 | 51 |
public class ContainerObjectsActivity extends ListActivity { |
52 | 52 |
|
53 | 53 |
private static final int deleteContainer = 0; |
54 |
private static final int deleteFolder = 1; |
|
54 | 55 |
private ContainerObjects[] files; |
55 | 56 |
private static Container container; |
56 | 57 |
public String LOG = "viewFilesActivity"; |
... | ... | |
61 | 62 |
public int kbConver = 1024; |
62 | 63 |
private Context context; |
63 | 64 |
private String currentPath; |
65 |
private ContainerObjects[] curDirFiles; |
|
64 | 66 |
|
65 | 67 |
@Override |
66 | 68 |
public void onCreate(Bundle savedInstanceState) { |
... | ... | |
73 | 75 |
} else { |
74 | 76 |
cdnEnabledIs = "false"; |
75 | 77 |
} |
76 |
getPath(); |
|
77 | 78 |
restoreState(savedInstanceState); |
78 | 79 |
} |
79 | 80 |
|
... | ... | |
85 | 86 |
} |
86 | 87 |
|
87 | 88 |
private void restoreState(Bundle state) { |
89 |
getPath(); |
|
90 |
|
|
88 | 91 |
if (state != null && state.containsKey("container")) { |
89 | 92 |
files = (ContainerObjects[]) state.getSerializable("container"); |
90 | 93 |
if (files.length == 0) { |
... | ... | |
100 | 103 |
} |
101 | 104 |
|
102 | 105 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
103 |
if (files != null && files.length > 0) { |
|
104 |
|
|
106 |
if (curDirFiles != null && curDirFiles.length > 0) { |
|
105 | 107 |
Intent viewIntent; |
106 |
|
|
107 |
if(files[position].getContentType().equals("application/directory")){
|
|
108 |
if(curDirFiles[position].getContentType().equals("application/directory")){ |
|
109 |
/*
|
|
108 | 110 |
viewIntent = new Intent(this, ContainerObjectsActivity.class); |
109 | 111 |
viewIntent.putExtra("container", container); |
110 | 112 |
viewIntent.putExtra("cdnUrl", container.getCdnUrl()); |
... | ... | |
112 | 114 |
viewIntent.putExtra("isCdnEnabled", cdnEnabledIs); |
113 | 115 |
viewIntent.putExtra("path", files[position].getCName()); |
114 | 116 |
Log.d("the path name is: ","captin " + files[position].getCName()); |
115 |
startActivity(viewIntent); |
|
117 |
startActivityForResult(viewIntent, 56); |
|
118 |
*/ |
|
119 |
|
|
120 |
currentPath = curDirFiles[position].getCName() + "/"; |
|
121 |
loadCurrentDirectoryFiles(); |
|
122 |
displayCurrentFiles(); |
|
116 | 123 |
} |
117 | 124 |
|
118 |
|
|
119 | 125 |
else{ |
120 | 126 |
viewIntent = new Intent(this, ContainerObjectDetails.class); |
121 | 127 |
viewIntent.putExtra("container", files[position]); |
... | ... | |
127 | 133 |
} |
128 | 134 |
} |
129 | 135 |
} |
136 |
|
|
137 |
/* |
|
138 |
* overriding back button press, because we are not actually changing |
|
139 |
* activites when we navigate the file structure |
|
140 |
*/ |
|
141 |
public void onBackPressed() { |
|
142 |
if(currentPath.equals("")){ |
|
143 |
finish(); |
|
144 |
} |
|
145 |
else{ |
|
146 |
goUpDirectory(); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
/* |
|
151 |
* go to the current directory's parent and display that data |
|
152 |
*/ |
|
153 |
private void goUpDirectory(){ |
|
154 |
currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1); |
|
155 |
loadCurrentDirectoryFiles(); |
|
156 |
displayCurrentFiles(); |
|
157 |
} |
|
130 | 158 |
|
131 |
|
|
159 |
/* |
|
160 |
* get the path that was stored in the intent |
|
161 |
*/ |
|
132 | 162 |
private void getPath(){ |
133 | 163 |
if(this.getIntent().getExtras().containsKey("path")){ |
134 |
Log.d("contained path", "captin: " + (String)this.getIntent().getExtras().get("path")); |
|
135 | 164 |
currentPath = (String)this.getIntent().getExtras().get("path"); |
136 | 165 |
} |
137 | 166 |
else{ |
138 |
Log.d("contained path", "captin no path"); |
|
139 | 167 |
currentPath = ""; |
140 | 168 |
} |
141 | 169 |
|
... | ... | |
143 | 171 |
|
144 | 172 |
private void loadFiles() { |
145 | 173 |
displayLoadingCell(); |
146 |
new LoadFilesTask().execute(currentPath); |
|
174 |
new LoadFilesTask().execute(); |
|
175 |
} |
|
176 |
|
|
177 |
/* load only the files that should display for the |
|
178 |
* current directory in the curDirFiles[] |
|
179 |
*/ |
|
180 |
private void loadCurrentDirectoryFiles(){ |
|
181 |
ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>(); |
|
182 |
|
|
183 |
if(files != null){ |
|
184 |
for(int i = 0 ; i < files.length; i ++){ |
|
185 |
if(fileBelongsInDir(files[i])){ |
|
186 |
curFiles.add(files[i]); |
|
187 |
} |
|
188 |
} |
|
147 | 189 |
|
190 |
curDirFiles = new ContainerObjects[curFiles.size()]; |
|
191 |
for(int i = 0; i < curFiles.size(); i++){ |
|
192 |
curDirFiles[i] = curFiles.get(i); |
|
193 |
} |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
/* |
|
198 |
* determines if a file should be displayed in current |
|
199 |
* directory |
|
200 |
*/ |
|
201 |
private Boolean fileBelongsInDir(ContainerObjects obj){ |
|
202 |
String objPath = obj.getCName(); |
|
203 |
if(!objPath.startsWith(currentPath)){ |
|
204 |
return false; |
|
205 |
} |
|
206 |
else{ |
|
207 |
objPath = objPath.substring(currentPath.length()); |
|
208 |
return !objPath.contains("/"); |
|
209 |
} |
|
148 | 210 |
} |
149 | 211 |
|
212 |
|
|
213 |
/* |
|
214 |
* loads all the files that are in the container |
|
215 |
* into one array |
|
216 |
*/ |
|
150 | 217 |
private void setFileList(ArrayList<ContainerObjects> files) { |
151 | 218 |
if (files == null) { |
152 | 219 |
files = new ArrayList<ContainerObjects>(); |
... | ... | |
162 | 229 |
} |
163 | 230 |
} |
164 | 231 |
|
165 |
if (fileNames.length == 0) { |
|
232 |
displayCurrentFiles(); |
|
233 |
} |
|
234 |
|
|
235 |
private void displayCurrentFiles(){ |
|
236 |
loadCurrentDirectoryFiles(); |
|
237 |
if (curDirFiles.length == 0) { |
|
166 | 238 |
displayNoServersCell(); |
167 | 239 |
} else { |
168 | 240 |
getListView().setDividerHeight(1); // restore divider lines |
... | ... | |
181 | 253 |
getListView().setItemsCanFocus(false); |
182 | 254 |
} |
183 | 255 |
|
256 |
/* |
|
257 |
* display a different empty page depending |
|
258 |
* of if you are at top of container or |
|
259 |
* in a folder |
|
260 |
*/ |
|
184 | 261 |
private void displayNoServersCell() { |
185 | 262 |
String a[] = new String[1]; |
186 |
a[0] = "No Files"; |
|
187 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell, |
|
263 |
if(currentPath.equals("")){ |
|
264 |
a[0] = "Empty Container"; |
|
265 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell, |
|
188 | 266 |
R.id.no_files_label, a)); |
267 |
} |
|
268 |
else{ |
|
269 |
a[0] = "No Files"; |
|
270 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell, |
|
271 |
R.id.no_files_label, a)); |
|
272 |
} |
|
189 | 273 |
getListView().setTextFilterEnabled(true); |
190 | 274 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
191 | 275 |
// like a list row |
... | ... | |
221 | 305 |
ArrayList<ContainerObjects> files = null; |
222 | 306 |
try { |
223 | 307 |
files = (new ContainerObjectManager(context)).createList(true, |
224 |
container.getName(), path[0]);
|
|
308 |
container.getName()); |
|
225 | 309 |
} catch (CloudServersException e) { |
226 | 310 |
exception = e; |
227 | 311 |
e.printStackTrace(); |
... | ... | |
241 | 325 |
class FileAdapter extends ArrayAdapter<ContainerObjects> { |
242 | 326 |
FileAdapter() { |
243 | 327 |
super(ContainerObjectsActivity.this, |
244 |
R.layout.listcontainerobjectcell, files);
|
|
328 |
R.layout.listcontainerobjectcell, curDirFiles);
|
|
245 | 329 |
} |
246 | 330 |
|
247 | 331 |
public View getView(int position, View convertView, ViewGroup parent) { |
248 | 332 |
|
249 |
ContainerObjects file = files[position];
|
|
333 |
ContainerObjects file = curDirFiles[position];
|
|
250 | 334 |
LayoutInflater inflater = getLayoutInflater(); |
251 | 335 |
View row = inflater.inflate(R.layout.listcontainerobjectcell, |
252 | 336 |
parent, false); |
253 | 337 |
|
254 | 338 |
TextView label = (TextView) row.findViewById(R.id.label); |
255 | 339 |
//label.setText(file.getCName()); |
256 |
label.setText(getShortName(file.getCName()));
|
|
340 |
label.setText(file.getCName());
|
|
257 | 341 |
|
258 | 342 |
if (file.getBytes() >= bConver) { |
259 | 343 |
megaBytes = Math.abs(file.getBytes() / bConver + 0.2); |
... | ... | |
281 | 365 |
return s; |
282 | 366 |
} |
283 | 367 |
else { |
284 |
return s.substring(s.lastIndexOf('/')+1);
|
|
368 |
return s.substring(s.lastIndexOf('/')); |
|
285 | 369 |
} |
286 | 370 |
} |
287 | 371 |
|
288 |
// Create the Menu options |
|
372 |
/* |
|
373 |
* Create the Menu options |
|
374 |
*/ |
|
289 | 375 |
@Override |
290 | 376 |
public boolean onCreateOptionsMenu(Menu menu) { |
291 | 377 |
super.onCreateOptionsMenu(menu); |
... | ... | |
295 | 381 |
} |
296 | 382 |
|
297 | 383 |
@Override |
384 |
/* |
|
385 |
* option performed for delete depends on if you |
|
386 |
* are at the top of a container or in a folder |
|
387 |
*/ |
|
298 | 388 |
public boolean onOptionsItemSelected(MenuItem item) { |
299 | 389 |
switch (item.getItemId()) { |
300 | 390 |
case R.id.delete_container: |
301 |
showDialog(deleteContainer); |
|
391 |
if(currentPath.equals("")){ |
|
392 |
Log.d("deleting", "captin deleting a container"); |
|
393 |
showDialog(deleteContainer); |
|
394 |
} |
|
395 |
else{ |
|
396 |
Log.d("deleting", "captin deleting a folder"); |
|
397 |
showDialog(deleteFolder); |
|
398 |
} |
|
302 | 399 |
return true; |
303 | 400 |
case R.id.enable_cdn: |
304 | 401 |
Intent viewIntent1 = new Intent(this, EnableCDNActivity.class); |
... | ... | |
316 | 413 |
protected Dialog onCreateDialog(int id) { |
317 | 414 |
switch (id) { |
318 | 415 |
case deleteContainer: |
319 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
|
320 |
.setIcon(R.drawable.alert_dialog_icon) |
|
321 |
.setTitle("Delete Container") |
|
322 |
.setMessage( |
|
323 |
"Are you sure you want to delete this Container?") |
|
324 |
.setPositiveButton("Delete Container", |
|
325 |
new DialogInterface.OnClickListener() { |
|
326 |
public void onClick(DialogInterface dialog, |
|
327 |
int whichButton) { |
|
328 |
// User clicked OK so do some stuff |
|
329 |
new DeleteContainerTask() |
|
330 |
.execute((Void[]) null); |
|
331 |
} |
|
332 |
}) |
|
333 |
.setNegativeButton("Cancel", |
|
334 |
new DialogInterface.OnClickListener() { |
|
335 |
public void onClick(DialogInterface dialog, |
|
336 |
int whichButton) { |
|
337 |
// User clicked Cancel so do some stuff |
|
338 |
} |
|
339 |
}).create(); |
|
416 |
if(curDirFiles.length == 0){ |
|
417 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
|
418 |
.setIcon(R.drawable.alert_dialog_icon) |
|
419 |
.setTitle("Delete Container") |
|
420 |
.setMessage( |
|
421 |
"Are you sure you want to delete this Container?") |
|
422 |
.setPositiveButton("Delete Container", |
|
423 |
new DialogInterface.OnClickListener() { |
|
424 |
public void onClick(DialogInterface dialog, |
|
425 |
int whichButton) { |
|
426 |
// User clicked OK so do some stuff |
|
427 |
new DeleteContainerTask() |
|
428 |
.execute(currentPath); |
|
429 |
} |
|
430 |
}) |
|
431 |
.setNegativeButton("Cancel", |
|
432 |
new DialogInterface.OnClickListener() { |
|
433 |
public void onClick(DialogInterface dialog, |
|
434 |
int whichButton) { |
|
435 |
// User clicked Cancel so do some stuff |
|
436 |
} |
|
437 |
}).create(); |
|
438 |
} |
|
439 |
else{ |
|
440 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
|
441 |
.setIcon(R.drawable.alert_dialog_icon) |
|
442 |
.setTitle("Delete Container") |
|
443 |
.setMessage("Container must be empty to delete") |
|
444 |
.setNegativeButton("OK", |
|
445 |
new DialogInterface.OnClickListener() { |
|
446 |
public void onClick(DialogInterface dialog, |
|
447 |
int whichButton) { |
|
448 |
// User clicked Cancel so do some stuff |
|
449 |
} |
|
450 |
}).create(); |
|
451 |
} |
|
452 |
case deleteFolder: |
|
453 |
if(curDirFiles.length == 0){ |
|
454 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
|
455 |
.setIcon(R.drawable.alert_dialog_icon) |
|
456 |
.setTitle("Delete Folder") |
|
457 |
.setMessage( |
|
458 |
"Are you sure you want to delete this Folder?") |
|
459 |
.setPositiveButton("Delete Folder", |
|
460 |
new DialogInterface.OnClickListener() { |
|
461 |
public void onClick(DialogInterface dialog, |
|
462 |
int whichButton) { |
|
463 |
// User clicked OK so do some stuff |
|
464 |
new DeleteObjectTask() |
|
465 |
.execute(); |
|
466 |
} |
|
467 |
}) |
|
468 |
.setNegativeButton("Cancel", |
|
469 |
new DialogInterface.OnClickListener() { |
|
470 |
public void onClick(DialogInterface dialog, |
|
471 |
int whichButton) { |
|
472 |
// User clicked Cancel so do some stuff |
|
473 |
} |
|
474 |
}).create(); |
|
475 |
} |
|
476 |
else{ |
|
477 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
|
478 |
.setIcon(R.drawable.alert_dialog_icon) |
|
479 |
.setTitle("Delete Folder") |
|
480 |
.setMessage( |
|
481 |
"Folder must be empty to delete") |
|
482 |
.setNegativeButton("OK", |
|
483 |
new DialogInterface.OnClickListener() { |
|
484 |
public void onClick(DialogInterface dialog, |
|
485 |
int whichButton) { |
|
486 |
// User clicked Cancel so do some stuff |
|
487 |
} |
|
488 |
}).create(); |
|
489 |
} |
|
340 | 490 |
|
341 | 491 |
} |
342 | 492 |
return null; |
... | ... | |
390 | 540 |
return cse; |
391 | 541 |
} |
392 | 542 |
|
393 |
private class DeleteContainerTask extends |
|
394 |
AsyncTask<Void, Void, HttpResponse> { |
|
543 |
|
|
544 |
private class DeleteObjectTask extends |
|
545 |
AsyncTask<Void, Void, HttpResponse> { |
|
395 | 546 |
|
396 | 547 |
private CloudServersException exception; |
397 | 548 |
|
... | ... | |
399 | 550 |
protected HttpResponse doInBackground(Void... arg0) { |
400 | 551 |
HttpResponse resp = null; |
401 | 552 |
try { |
553 |
//subtring because the current directory contains a "/" at the end of the string |
|
554 |
resp = (new ContainerObjectManager(context)).deleteObject(container.getName(), currentPath.substring(0, currentPath.length()-1)); |
|
555 |
} catch (CloudServersException e) { |
|
556 |
exception = e; |
|
557 |
} |
|
558 |
return resp; |
|
559 |
} |
|
560 |
|
|
561 |
@Override |
|
562 |
protected void onPostExecute(HttpResponse response) { |
|
563 |
if (response != null) { |
|
564 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
565 |
if (statusCode == 409) { |
|
566 |
showAlert("Error", |
|
567 |
"Folder must be empty in order to delete"); |
|
568 |
} |
|
569 |
if (statusCode == 204) { |
|
570 |
setResult(Activity.RESULT_OK); |
|
571 |
goUpDirectory(); |
|
572 |
loadFiles(); |
|
573 |
|
|
574 |
} else { |
|
575 |
CloudServersException cse = parseCloudServersException(response); |
|
576 |
if ("".equals(cse.getMessage())) { |
|
577 |
showAlert("Error", |
|
578 |
"There was a problem deleting your folder."); |
|
579 |
} else { |
|
580 |
showAlert("Error", |
|
581 |
"There was a problem deleting your folder: " |
|
582 |
+ cse.getMessage()); |
|
583 |
} |
|
584 |
} |
|
585 |
} else if (exception != null) { |
|
586 |
showAlert("Error", "There was a problem deleting your folder: " |
|
587 |
+ exception.getMessage()); |
|
588 |
} |
|
589 |
} |
|
590 |
} |
|
591 |
|
|
592 |
private class DeleteContainerTask extends |
|
593 |
AsyncTask<String, Void, HttpResponse> { |
|
594 |
|
|
595 |
private CloudServersException exception; |
|
596 |
|
|
597 |
@Override |
|
598 |
protected HttpResponse doInBackground(String... object) { |
|
599 |
HttpResponse resp = null; |
|
600 |
try { |
|
402 | 601 |
resp = (new ContainerManager(context)).delete(container.getName()); |
403 | 602 |
Log.v(LOG, "container's name " + container.getName()); |
404 | 603 |
} catch (CloudServersException e) { |
... | ... | |
413 | 612 |
int statusCode = response.getStatusLine().getStatusCode(); |
414 | 613 |
if (statusCode == 409) { |
415 | 614 |
showAlert("Error", |
416 |
"Container must be empty in order to delete");
|
|
615 |
"Container must be empty in order to delete"); |
|
417 | 616 |
} |
418 | 617 |
if (statusCode == 204) { |
419 | 618 |
setResult(Activity.RESULT_OK); |
619 |
loadFiles(); |
|
420 | 620 |
finish(); |
421 | 621 |
|
422 | 622 |
} else { |
423 | 623 |
CloudServersException cse = parseCloudServersException(response); |
424 | 624 |
if ("".equals(cse.getMessage())) { |
425 | 625 |
showAlert("Error", |
426 |
"There was a problem deleting your container.");
|
|
626 |
"There was a problem deleting your container."); |
|
427 | 627 |
} else { |
428 | 628 |
showAlert("Error", |
429 | 629 |
"There was a problem deleting your container: " |
430 |
+ cse.getMessage());
|
|
630 |
+ cse.getMessage()); |
|
431 | 631 |
} |
432 | 632 |
} |
433 | 633 |
} else if (exception != null) { |
Also available in: Unified diff