root / src / com / rackspace / cloud / android / ContainerObjectsActivity.java @ d2e9a932
History | View | Annotate | Download (32.8 kB)
1 |
package com.rackspace.cloud.android; |
---|---|
2 |
|
3 |
|
4 |
|
5 |
import java.io.File; |
6 |
import java.net.URLEncoder; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Arrays; |
9 |
import java.util.List; |
10 |
|
11 |
import org.apache.http.HttpResponse; |
12 |
|
13 |
import android.app.Activity; |
14 |
import android.app.AlertDialog; |
15 |
import android.app.Dialog; |
16 |
import android.content.ActivityNotFoundException; |
17 |
import android.content.DialogInterface; |
18 |
import android.content.Intent; |
19 |
import android.database.Cursor; |
20 |
import android.net.Uri; |
21 |
import android.os.AsyncTask; |
22 |
import android.os.Bundle; |
23 |
import android.util.Log; |
24 |
import android.view.ContextMenu; |
25 |
import android.view.ContextMenu.ContextMenuInfo; |
26 |
import android.view.LayoutInflater; |
27 |
import android.view.Menu; |
28 |
import android.view.MenuInflater; |
29 |
import android.view.MenuItem; |
30 |
import android.view.View; |
31 |
import android.view.ViewGroup; |
32 |
import android.webkit.MimeTypeMap; |
33 |
import android.widget.AdapterView; |
34 |
import android.widget.ArrayAdapter; |
35 |
import android.widget.EditText; |
36 |
import android.widget.ImageView; |
37 |
import android.widget.ListView; |
38 |
import android.widget.TextView; |
39 |
import android.widget.Toast; |
40 |
|
41 |
import com.rackspace.cloud.files.api.client.Container; |
42 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
43 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
44 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
45 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
46 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
47 |
|
48 |
/**
|
49 |
*
|
50 |
* @author Phillip Toohill
|
51 |
*
|
52 |
*/
|
53 |
|
54 |
public class ContainerObjectsActivity extends CloudListActivity { |
55 |
|
56 |
private static final int deleteContainer = 0; |
57 |
private static final int deleteFolder = 1; |
58 |
|
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 boolean loadingFiles; |
68 |
private String currentPath; |
69 |
private AndroidCloudApplication app;
|
70 |
private AddObjectListenerTask task;
|
71 |
private DeleteObjectListenerTask deleteObjTask;
|
72 |
private DeleteContainerListenerTask deleteContainerTask;
|
73 |
|
74 |
@Override
|
75 |
public void onCreate(Bundle savedInstanceState) { |
76 |
super.onCreate(savedInstanceState);
|
77 |
trackPageView(GoogleAnalytics.PAGE_FOLDER); |
78 |
container = getContainer(); |
79 |
if (container.isCdnEnabled() == true) { |
80 |
cdnEnabledIs = "true";
|
81 |
} else {
|
82 |
cdnEnabledIs = "false";
|
83 |
} |
84 |
|
85 |
restoreState(savedInstanceState); |
86 |
} |
87 |
|
88 |
protected Container getContainer() { |
89 |
return (Container) this.getIntent().getExtras().get("container"); |
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 |
outState.putBoolean("loadingFiles", loadingFiles);
|
103 |
} |
104 |
|
105 |
protected void restoreState(Bundle state) { |
106 |
super.restoreState(state);
|
107 |
|
108 |
/*
|
109 |
* need reference to the app so you can access curDirFiles as well as
|
110 |
* processing status
|
111 |
*/
|
112 |
app = (AndroidCloudApplication) this.getApplication();
|
113 |
|
114 |
if (state != null) { |
115 |
if (state.containsKey("path")) { |
116 |
currentPath = state.getString("path");
|
117 |
} else {
|
118 |
currentPath = "";
|
119 |
} |
120 |
|
121 |
if (state.containsKey("loadingFiles") |
122 |
&& state.getBoolean("loadingFiles")) {
|
123 |
loadFiles(); |
124 |
} else if (state.containsKey("container")) { |
125 |
files = (ContainerObjects[]) state.getSerializable("container"); |
126 |
if (app.getCurFiles() == null || app.getCurFiles().size() == 0) { |
127 |
displayNoFilesCell(); |
128 |
} else {
|
129 |
getListView().setDividerHeight(1); // restore divider lines |
130 |
setListAdapter(new FileAdapter());
|
131 |
|
132 |
} |
133 |
|
134 |
} |
135 |
} else {
|
136 |
currentPath = "";
|
137 |
loadFiles(); |
138 |
} |
139 |
|
140 |
/*
|
141 |
* if the app is process when we enter the activity we must listen for
|
142 |
* the new curDirFiles list
|
143 |
*/
|
144 |
if (app.isAddingObject()) {
|
145 |
task = new AddObjectListenerTask();
|
146 |
task.execute(); |
147 |
} |
148 |
|
149 |
if (app.isDeletingObject()) {
|
150 |
displayNoFilesCell(); |
151 |
deleteObjTask = new DeleteObjectListenerTask();
|
152 |
deleteObjTask.execute(); |
153 |
} |
154 |
|
155 |
if (app.isDeletingContainer()) {
|
156 |
displayNoFilesCell(); |
157 |
deleteContainerTask = new DeleteContainerListenerTask();
|
158 |
deleteContainerTask.execute(); |
159 |
} |
160 |
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
protected void onStop() { |
165 |
super.onStop();
|
166 |
|
167 |
/*
|
168 |
* Need to stop running listener task if we exit
|
169 |
*/
|
170 |
if (task != null) { |
171 |
task.cancel(true);
|
172 |
} |
173 |
|
174 |
if (deleteObjTask != null) { |
175 |
deleteObjTask.cancel(true);
|
176 |
} |
177 |
|
178 |
if (deleteContainerTask != null) { |
179 |
deleteContainerTask.cancel(true);
|
180 |
} |
181 |
|
182 |
} |
183 |
|
184 |
/*
|
185 |
* overriding back button press, because we are not actually changing
|
186 |
* activities when we navigate the file structure
|
187 |
*/
|
188 |
public void onBackPressed() { |
189 |
|
190 |
if (currentPath.equals("")) { |
191 |
if(container.getName().equals(Container.MYSHARED)){} |
192 |
else
|
193 |
finish(); |
194 |
} else {
|
195 |
goUpDirectory(); |
196 |
} |
197 |
} |
198 |
List<ContainerObjects> previousFiles = new ArrayList<ContainerObjects>(); |
199 |
/*
|
200 |
* go to the current directory's parent and display that data
|
201 |
*/
|
202 |
private void goUpDirectory() { |
203 |
currentPath = currentPath.substring( |
204 |
0,
|
205 |
currentPath.substring(0, currentPath.length() - 2).lastIndexOf( |
206 |
"/") + 1); |
207 |
if(container.getName().equalsIgnoreCase(Container.MYSHARED)){ |
208 |
loadCurrentDirectoryFiles(); |
209 |
displayCurrentFiles(); |
210 |
} |
211 |
else if(previousFiles!=null&&previousFiles.size()>0){ |
212 |
files = previousFiles.toArray(new ContainerObjects[]{}); |
213 |
previousFiles=null;
|
214 |
loadCurrentDirectoryFiles(); |
215 |
displayCurrentFiles(); |
216 |
} |
217 |
else
|
218 |
loadFiles(); |
219 |
//now executing new call
|
220 |
|
221 |
} |
222 |
|
223 |
/*
|
224 |
* load all file that are in the container
|
225 |
*/
|
226 |
private void loadFiles() { |
227 |
// displayLoadingCell();
|
228 |
new LoadFilesTask().execute(currentPath);
|
229 |
} |
230 |
|
231 |
/*
|
232 |
* load only the files that should display for the current directory in the
|
233 |
* curDirFiles[]
|
234 |
*/
|
235 |
protected void loadCurrentDirectoryFiles() { |
236 |
ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>(); |
237 |
Log.i(LOG, "loading files");
|
238 |
if (files != null) { |
239 |
for (int i = 0; i < files.length; i++) { |
240 |
Log.i(LOG, "loading files" + i);
|
241 |
if (fileBelongsInDir(files[i])) {
|
242 |
curFiles.add(files[i]); |
243 |
} |
244 |
} |
245 |
app.setCurFiles(curFiles); |
246 |
} |
247 |
} |
248 |
|
249 |
/*
|
250 |
* determines if a file should be displayed in current directory
|
251 |
*/
|
252 |
protected Boolean fileBelongsInDir(ContainerObjects obj) { |
253 |
String objPath = obj.getCName();
|
254 |
|
255 |
if (!objPath.startsWith(currentPath)) {
|
256 |
Log.i(LOG, "Path is:" + currentPath + " " + objPath + " " + false); |
257 |
return false; |
258 |
} else {
|
259 |
objPath = objPath.substring(currentPath.length()); |
260 |
Log.i(LOG, "Path is:" + currentPath + " " + objPath + " " |
261 |
+ !objPath.contains("/"));
|
262 |
return !objPath.contains("/"); |
263 |
} |
264 |
} |
265 |
|
266 |
/*
|
267 |
* loads all the files that are in the container into one array
|
268 |
*/
|
269 |
private void setFileList(ArrayList<ContainerObjects> files) { |
270 |
if (files == null) { |
271 |
files = new ArrayList<ContainerObjects>(); |
272 |
} |
273 |
String[] fileNames = new String[files.size()]; |
274 |
this.files = new ContainerObjects[files.size()]; |
275 |
|
276 |
if (files != null) { |
277 |
for (int i = 0; i < files.size(); i++) { |
278 |
ContainerObjects file = files.get(i); |
279 |
// Log.i(file.getCName(),file.getCName());
|
280 |
this.files[i] = file;
|
281 |
fileNames[i] = file.getName(); |
282 |
} |
283 |
} |
284 |
} |
285 |
|
286 |
protected void displayCurrentFiles() { |
287 |
if (app.getCurFiles().size() == 0) { |
288 |
displayNoFilesCell(); |
289 |
} else {
|
290 |
ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>(); |
291 |
for (int i = 0; i < app.getCurFiles().size(); i++) { |
292 |
tempList.add(app.getCurFiles().get(i)); |
293 |
} |
294 |
getListView().setDividerHeight(1); // restore divider lines |
295 |
setListAdapter(new FileAdapter());
|
296 |
} |
297 |
} |
298 |
|
299 |
/*
|
300 |
* display a different empty page depending of if you are at top of
|
301 |
* container or in a folder
|
302 |
*/
|
303 |
protected void displayNoFilesCell() { |
304 |
String a[] = new String[1]; |
305 |
if (currentPath.equals("")) { |
306 |
a[0] = "Empty Container"; |
307 |
setListAdapter(new ArrayAdapter<String>(this, |
308 |
R.layout.noobjectscell, R.id.no_files_label, a)); |
309 |
} else {
|
310 |
a[0] = "No Files"; |
311 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell, |
312 |
R.id.no_files_label, a)); |
313 |
} |
314 |
getListView().setTextFilterEnabled(true);
|
315 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
316 |
// like a list row
|
317 |
getListView().setItemsCanFocus(false);
|
318 |
} |
319 |
|
320 |
/*
|
321 |
* just get the last part of the filename so the entire file path does not
|
322 |
* show
|
323 |
*/
|
324 |
private String getShortName(String longName) { |
325 |
String s = longName;
|
326 |
if (!s.contains("/")) { |
327 |
return s;
|
328 |
} else {
|
329 |
String tmp = s.substring(s.lastIndexOf('/') + 1); |
330 |
if (tmp.equals("")) { |
331 |
return s;
|
332 |
} |
333 |
return tmp;
|
334 |
} |
335 |
} |
336 |
|
337 |
/*
|
338 |
* removed a specified object from the array of all the files in the
|
339 |
* container
|
340 |
*/
|
341 |
private void removeFromList(String path) { |
342 |
ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>( |
343 |
Arrays.asList(files));
|
344 |
for (int i = 0; i < files.length; i++) { |
345 |
if (files[i].getCName()
|
346 |
.equals(path.substring(0, path.length() - 1))) { |
347 |
temp.remove(i); |
348 |
} |
349 |
} |
350 |
files = new ContainerObjects[temp.size()];
|
351 |
for (int i = 0; i < temp.size(); i++) { |
352 |
files[i] = temp.get(i); |
353 |
} |
354 |
} |
355 |
|
356 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
357 |
if (app.getCurFiles() != null && app.getCurFiles().size() > 0) { |
358 |
Intent viewIntent; |
359 |
if (app.getCurFiles().get(position).getContentType()
|
360 |
.startsWith("application/directory")) {
|
361 |
currentPath = app.getCurFiles().get(position).getCName() + "/";
|
362 |
if(container.getName().equalsIgnoreCase(Container.MYSHARED)){ |
363 |
loadCurrentDirectoryFiles(); |
364 |
displayCurrentFiles(); |
365 |
} |
366 |
else{
|
367 |
previousFiles = Arrays.asList(files);
|
368 |
loadFiles(); |
369 |
} |
370 |
//loadCurrentDirectoryFiles();
|
371 |
//displayCurrentFiles();
|
372 |
} |
373 |
|
374 |
else {
|
375 |
viewIntent = new Intent(this, ContainerObjectDetails.class); |
376 |
viewIntent.putExtra("container", app.getCurFiles()
|
377 |
.get(position)); |
378 |
viewIntent.putExtra("cdnUrl", container.getCdnUrl());
|
379 |
viewIntent.putExtra("containerNames", container.getName());
|
380 |
viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
|
381 |
startActivityForResult(viewIntent, 55); // arbitrary number; |
382 |
// never
|
383 |
// used again
|
384 |
} |
385 |
} |
386 |
} |
387 |
|
388 |
@Override
|
389 |
public boolean onCreateOptionsMenu(Menu menu) { |
390 |
super.onCreateOptionsMenu(menu);
|
391 |
MenuInflater inflater = getMenuInflater(); |
392 |
inflater.inflate(R.menu.view_container_object_list_menu, menu); |
393 |
menu.findItem(R.id.enable_cdn).setVisible(false);
|
394 |
if(Container.TRASH.equalsIgnoreCase(container.getName())){ |
395 |
menu.findItem(R.id.delete_container).setVisible(false);
|
396 |
menu.findItem(R.id.add_folder).setVisible(false);
|
397 |
menu.findItem(R.id.add_file).setVisible(false);
|
398 |
} |
399 |
else if(Container.MYSHARED.equalsIgnoreCase(container.getName())){ |
400 |
menu.findItem(R.id.delete_container).setVisible(false);
|
401 |
menu.findItem(R.id.add_folder).setVisible(false);
|
402 |
menu.findItem(R.id.add_file).setVisible(false);
|
403 |
} |
404 |
return true; |
405 |
} |
406 |
@Override
|
407 |
public void onCreateContextMenu(ContextMenu menu, View v, |
408 |
ContextMenuInfo menuInfo) { |
409 |
super.onCreateContextMenu(menu, v, menuInfo);
|
410 |
MenuInflater inflater = getMenuInflater(); |
411 |
inflater.inflate(R.menu.view_container_object_list_context, menu); |
412 |
menu.findItem(R.id.fromtrash_contextmenu).setVisible(false);
|
413 |
if(Container.TRASH.equalsIgnoreCase(container.getName())){ |
414 |
menu.findItem(R.id.delete_contextmenu).setVisible(false);
|
415 |
menu.findItem(R.id.totrash_contextmenu).setVisible(false);
|
416 |
menu.findItem(R.id.fromtrash_contextmenu).setVisible(true);
|
417 |
} |
418 |
else if(Container.MYSHARED.equalsIgnoreCase(container.getName())){ |
419 |
menu.findItem(R.id.delete_contextmenu).setVisible(false);
|
420 |
menu.findItem(R.id.totrash_contextmenu).setVisible(false);
|
421 |
} |
422 |
} |
423 |
|
424 |
@Override
|
425 |
public boolean onContextItemSelected(final MenuItem item) { |
426 |
final AdapterView.AdapterContextMenuInfo info;
|
427 |
try {
|
428 |
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
429 |
} catch (ClassCastException e) { |
430 |
Log.e(LOG, "bad menuInfo", e);
|
431 |
return false; |
432 |
} |
433 |
long id = getListAdapter().getItemId(info.position);
|
434 |
final ContainerObjects obj = (ContainerObjects) getListAdapter().getItem(info.position);
|
435 |
|
436 |
switch (item.getItemId()) {
|
437 |
case R.id.delete_contextmenu:
|
438 |
/*if (currentPath.equals("")) {
|
439 |
showDialog(deleteContainer);
|
440 |
} else {
|
441 |
showDialog(deleteFolder);
|
442 |
}*///TODO |
443 |
return true; |
444 |
case R.id.totrash_contextmenu:
|
445 |
/*if (currentPath.equals("")) {
|
446 |
showDialog(deleteContainer);
|
447 |
} else {
|
448 |
showDialog(deleteFolder);
|
449 |
}*///TODO |
450 |
return true; |
451 |
case R.id.fromtrash_contextmenu:
|
452 |
/*if (currentPath.equals("")) {
|
453 |
showDialog(deleteContainer);
|
454 |
} else {
|
455 |
showDialog(deleteFolder);
|
456 |
}*///TODO |
457 |
return true; |
458 |
case R.id.properties_contextmenu:
|
459 |
Intent viewIntent; |
460 |
viewIntent = new Intent(this, ContainerObjectDetails.class); |
461 |
viewIntent.putExtra("container", obj);
|
462 |
viewIntent.putExtra("cdnUrl", container.getCdnUrl());
|
463 |
viewIntent.putExtra("containerNames", container.getName());
|
464 |
viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
|
465 |
startActivityForResult(viewIntent, 55);
|
466 |
return true; |
467 |
|
468 |
} |
469 |
return false; |
470 |
|
471 |
} |
472 |
@Override
|
473 |
/*
|
474 |
* option performed for delete depends on if you are at the top of a
|
475 |
* container or in a folder
|
476 |
*/
|
477 |
public boolean onOptionsItemSelected(MenuItem item) { |
478 |
switch (item.getItemId()) {
|
479 |
case R.id.delete_container:
|
480 |
if (currentPath.equals("")) { |
481 |
showDialog(deleteContainer); |
482 |
} else {
|
483 |
showDialog(deleteFolder); |
484 |
} |
485 |
return true; |
486 |
case R.id.enable_cdn:
|
487 |
Intent viewIntent1 = new Intent(this, EnableCDNActivity.class); |
488 |
viewIntent1.putExtra("Cname", container.getName());
|
489 |
startActivityForResult(viewIntent1, 56);
|
490 |
return true; |
491 |
case R.id.refresh:
|
492 |
loadFiles(); |
493 |
return true; |
494 |
case R.id.add_folder:
|
495 |
showDialog(R.id.add_folder); |
496 |
return true; |
497 |
case R.id.add_file:
|
498 |
/*
|
499 |
* Intent viewIntent2 = new Intent(this, AddFileActivity.class);
|
500 |
* viewIntent2.putExtra("Cname", container.getName());
|
501 |
* viewIntent2.putExtra("curPath", currentPath);
|
502 |
* startActivityForResult(viewIntent2, 56);
|
503 |
*/
|
504 |
|
505 |
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
506 |
intent.addCategory(Intent.CATEGORY_OPENABLE); |
507 |
intent.setData(Uri.parse("file://"));
|
508 |
intent.setType("*/*");
|
509 |
try {
|
510 |
int i = 1; |
511 |
startActivityForResult(intent, i); |
512 |
|
513 |
} catch (ActivityNotFoundException e) {
|
514 |
Toast.makeText(this, "No File Manager", Toast.LENGTH_SHORT) |
515 |
.show(); |
516 |
} |
517 |
|
518 |
return true; |
519 |
} |
520 |
return false; |
521 |
} |
522 |
|
523 |
@Override
|
524 |
protected Dialog onCreateDialog(int id) { |
525 |
switch (id) {
|
526 |
case deleteContainer:
|
527 |
if (app.getCurFiles().size() == 0) { |
528 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
529 |
.setIcon(R.drawable.alert_dialog_icon) |
530 |
.setTitle("Delete Container")
|
531 |
.setMessage( |
532 |
"Are you sure you want to delete this Container?")
|
533 |
.setPositiveButton("Delete Container",
|
534 |
new DialogInterface.OnClickListener() {
|
535 |
public void onClick(DialogInterface dialog, |
536 |
int whichButton) {
|
537 |
// User clicked OK so do some stuff
|
538 |
trackEvent( |
539 |
GoogleAnalytics.CATEGORY_CONTAINER, |
540 |
GoogleAnalytics.EVENT_DELETE, |
541 |
"", -1); |
542 |
new DeleteContainerTask()
|
543 |
.execute(currentPath); |
544 |
} |
545 |
}) |
546 |
.setNegativeButton("Cancel",
|
547 |
new DialogInterface.OnClickListener() {
|
548 |
public void onClick(DialogInterface dialog, |
549 |
int whichButton) {
|
550 |
// User clicked Cancel so do some stuff
|
551 |
} |
552 |
}).create(); |
553 |
} else {
|
554 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
555 |
.setIcon(R.drawable.alert_dialog_icon) |
556 |
.setTitle("Delete Container")
|
557 |
.setMessage("Container must be empty to delete")
|
558 |
.setNegativeButton("OK",
|
559 |
new DialogInterface.OnClickListener() {
|
560 |
public void onClick(DialogInterface dialog, |
561 |
int whichButton) {
|
562 |
// User clicked Cancel so do some stuff
|
563 |
} |
564 |
}).create(); |
565 |
} |
566 |
case deleteFolder:
|
567 |
if (app.getCurFiles().size() == 0) { |
568 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
569 |
.setIcon(R.drawable.alert_dialog_icon) |
570 |
.setTitle("Delete Folder")
|
571 |
.setMessage( |
572 |
"Are you sure you want to delete this Folder?")
|
573 |
.setPositiveButton("Delete Folder",
|
574 |
new DialogInterface.OnClickListener() {
|
575 |
public void onClick(DialogInterface dialog, |
576 |
int whichButton) {
|
577 |
// User clicked OK so do some stuff
|
578 |
new DeleteObjectTask().execute(container.getName(), currentPath);
|
579 |
} |
580 |
}) |
581 |
.setNegativeButton("Cancel",
|
582 |
new DialogInterface.OnClickListener() {
|
583 |
public void onClick(DialogInterface dialog, |
584 |
int whichButton) {
|
585 |
// User clicked Cancel so do some stuff
|
586 |
} |
587 |
}).create(); |
588 |
} else {
|
589 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
590 |
.setIcon(R.drawable.alert_dialog_icon) |
591 |
.setTitle("Delete Folder")
|
592 |
.setMessage("Folder must be empty to delete")
|
593 |
.setNegativeButton("OK",
|
594 |
new DialogInterface.OnClickListener() {
|
595 |
public void onClick(DialogInterface dialog, |
596 |
int whichButton) {
|
597 |
// User clicked Cancel so do some stuff
|
598 |
} |
599 |
}).create(); |
600 |
} |
601 |
case R.id.add_folder:
|
602 |
final EditText input = new EditText(this); |
603 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
604 |
.setIcon(R.drawable.alert_dialog_icon) |
605 |
.setView(input) |
606 |
.setTitle("Add Folder")
|
607 |
.setMessage("Enter new name for folder: ")
|
608 |
.setPositiveButton("Add",
|
609 |
new DialogInterface.OnClickListener() {
|
610 |
public void onClick(DialogInterface dialog, |
611 |
int whichButton) {
|
612 |
// User clicked OK so do some stuff
|
613 |
String[] info = { |
614 |
input.getText().toString(), |
615 |
"application/directory" };
|
616 |
new AddFolderTask().execute(info);
|
617 |
} |
618 |
}) |
619 |
.setNegativeButton("Cancel",
|
620 |
new DialogInterface.OnClickListener() {
|
621 |
public void onClick(DialogInterface dialog, |
622 |
int whichButton) {
|
623 |
// User clicked Cancel so do some stuff
|
624 |
} |
625 |
}).create(); |
626 |
} |
627 |
return null; |
628 |
} |
629 |
|
630 |
@Override
|
631 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
632 |
super.onActivityResult(requestCode, resultCode, data);
|
633 |
|
634 |
if (resultCode == RESULT_OK && requestCode == 56) { |
635 |
// a sub-activity kicked back, so we want to refresh the server list
|
636 |
previousFiles = null;
|
637 |
loadFiles(); |
638 |
} |
639 |
|
640 |
if (resultCode == RESULT_OK && requestCode == 1) { |
641 |
String filename = data.getDataString();
|
642 |
uploadFile(filename, data); |
643 |
|
644 |
} |
645 |
|
646 |
// deleted file so need to update the list
|
647 |
if (requestCode == 55 && resultCode == 99) { |
648 |
Log.d(LOG,"LOADING FROM DDELETE");
|
649 |
previousFiles = null;
|
650 |
loadFiles(); |
651 |
} |
652 |
|
653 |
} |
654 |
|
655 |
class FileAdapter extends ArrayAdapter<ContainerObjects> { |
656 |
FileAdapter() { |
657 |
super(ContainerObjectsActivity.this,
|
658 |
R.layout.listcontainerobjectcell, app.getCurFiles()); |
659 |
} |
660 |
|
661 |
public View getView(int position, View convertView, ViewGroup parent) { |
662 |
ContainerObjects file = app.getCurFiles().get(position); |
663 |
LayoutInflater inflater = getLayoutInflater(); |
664 |
View row = inflater.inflate(R.layout.listcontainerobjectcell,
|
665 |
parent, false);
|
666 |
|
667 |
TextView label = (TextView) row.findViewById(R.id.label); |
668 |
label.setText(getShortName(file.getCName())); |
669 |
|
670 |
ImageView objectImage = (ImageView) row |
671 |
.findViewById(R.id.file_type_image); |
672 |
if (file.getContentType().startsWith("application/directory")||file.getContentType().startsWith("application/folder")) { |
673 |
objectImage.setImageResource(R.drawable.folder); |
674 |
} else {
|
675 |
objectImage.setImageResource(R.drawable.file); |
676 |
} |
677 |
|
678 |
if (file.getBytes() >= bConver) {
|
679 |
megaBytes = Math.abs(file.getBytes() / bConver + 0.2); |
680 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
681 |
sublabel.setText(megaBytes + " MB");
|
682 |
} else if (file.getBytes() >= kbConver) { |
683 |
kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2); |
684 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
685 |
sublabel.setText(kiloBytes + " KB");
|
686 |
} else {
|
687 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
688 |
sublabel.setText(file.getBytes() + " B");
|
689 |
} |
690 |
|
691 |
return (row);
|
692 |
} |
693 |
} |
694 |
|
695 |
private class LoadFilesTask extends |
696 |
AsyncTask<String, Void, ArrayList<ContainerObjects>> { |
697 |
|
698 |
private CloudServersException exception;
|
699 |
|
700 |
protected void onPreExecute() { |
701 |
showDialog(); |
702 |
loadingFiles = true;
|
703 |
} |
704 |
|
705 |
@Override
|
706 |
protected ArrayList<ContainerObjects> doInBackground(String... path) { |
707 |
ArrayList<ContainerObjects> files = null; |
708 |
try {
|
709 |
if (container.getName().equals(Container.MYSHARED)) { |
710 |
files = (new ContainerObjectManager(getContext()))
|
711 |
.createListMyShared(true, container.getName(),
|
712 |
new ContainerManager(getContext())
|
713 |
.createList(true));
|
714 |
} else if (container.getName().equals(Container.OTHERS)) { |
715 |
|
716 |
} else {
|
717 |
if (container.getOtherUser() == null) |
718 |
files = (new ContainerObjectManager(getContext()))
|
719 |
.createList(true, container.getName(),path[0]); |
720 |
else
|
721 |
files = (new ContainerObjectManager(getContext()))
|
722 |
.createOtherList(true, container.getName(),
|
723 |
container.getOtherUser()); |
724 |
} |
725 |
} catch (CloudServersException e) {
|
726 |
exception = e; |
727 |
e.printStackTrace(); |
728 |
} |
729 |
return files;
|
730 |
} |
731 |
|
732 |
@Override
|
733 |
protected void onPostExecute(ArrayList<ContainerObjects> result) { |
734 |
loadingFiles = false;
|
735 |
hideDialog(); |
736 |
if (exception != null) { |
737 |
showAlert("Error", exception.getMessage());
|
738 |
} |
739 |
setFileList(result); |
740 |
loadCurrentDirectoryFiles(); |
741 |
displayCurrentFiles(); |
742 |
} |
743 |
|
744 |
} |
745 |
|
746 |
private class AddFolderTask extends AsyncTask<String, Void, HttpBundle> { |
747 |
|
748 |
private CloudServersException exception;
|
749 |
|
750 |
@Override
|
751 |
protected void onPreExecute() { |
752 |
showDialog(); |
753 |
app.setAddingObject(true);
|
754 |
task = new AddObjectListenerTask();
|
755 |
task.execute(); |
756 |
} |
757 |
|
758 |
@Override
|
759 |
protected HttpBundle doInBackground(String... data) { |
760 |
HttpBundle bundle = null;
|
761 |
try {
|
762 |
|
763 |
bundle = (new ContainerObjectManager(getContext())).addObject(
|
764 |
container.getName(), currentPath, data[0], data[1]); |
765 |
} catch (CloudServersException e) {
|
766 |
exception = e; |
767 |
} |
768 |
return bundle;
|
769 |
} |
770 |
|
771 |
@Override
|
772 |
protected void onPostExecute(HttpBundle bundle) { |
773 |
app.setAddingObject(false);
|
774 |
hideDialog(); |
775 |
HttpResponse response = bundle.getResponse(); |
776 |
if (response != null) { |
777 |
int statusCode = response.getStatusLine().getStatusCode();
|
778 |
if (statusCode == 201) { |
779 |
setResult(Activity.RESULT_OK); |
780 |
// loading the new files is done by ListenerTask
|
781 |
} else {
|
782 |
CloudServersException cse = parseCloudServersException(response); |
783 |
if ("".equals(cse.getMessage())) { |
784 |
showError("There was a problem deleting your folder.",
|
785 |
bundle); |
786 |
} else {
|
787 |
showError("There was a problem deleting your folder: "
|
788 |
+ cse.getMessage(), bundle); |
789 |
} |
790 |
} |
791 |
} else if (exception != null) { |
792 |
showError("There was a problem deleting your folder: "
|
793 |
+ exception.getMessage(), bundle); |
794 |
} |
795 |
} |
796 |
} |
797 |
|
798 |
private class AddFileTask extends AsyncTask<String, Void, HttpBundle> { |
799 |
|
800 |
private CloudServersException exception;
|
801 |
|
802 |
@Override
|
803 |
protected void onPreExecute() { |
804 |
showDialog(); |
805 |
app.setAddingObject(true);
|
806 |
task = new AddObjectListenerTask();
|
807 |
task.execute(); |
808 |
} |
809 |
|
810 |
@Override
|
811 |
protected HttpBundle doInBackground(String... data) { |
812 |
HttpBundle bundle = null;
|
813 |
try {
|
814 |
|
815 |
bundle = (new ContainerObjectManager(getContext())).addFileObject(
|
816 |
container.getName(), currentPath, data[0], data[1], data[2]); |
817 |
} catch (CloudServersException e) {
|
818 |
exception = e; |
819 |
} |
820 |
return bundle;
|
821 |
} |
822 |
|
823 |
@Override
|
824 |
protected void onPostExecute(HttpBundle bundle) { |
825 |
app.setAddingObject(false);
|
826 |
hideDialog(); |
827 |
HttpResponse response = bundle.getResponse(); |
828 |
if (response != null) { |
829 |
int statusCode = response.getStatusLine().getStatusCode();
|
830 |
if (statusCode == 201) { |
831 |
setResult(Activity.RESULT_OK); |
832 |
// loading the new files is done by ListenerTask
|
833 |
} else {
|
834 |
CloudServersException cse = parseCloudServersException(response); |
835 |
if ("".equals(cse.getMessage())) { |
836 |
showError("There was a problem deleting your folder.",
|
837 |
bundle); |
838 |
} else {
|
839 |
showError("There was a problem deleting your folder: "
|
840 |
+ cse.getMessage(), bundle); |
841 |
} |
842 |
} |
843 |
} else if (exception != null) { |
844 |
showError("There was a problem deleting your folder: "
|
845 |
+ exception.getMessage(), bundle); |
846 |
} |
847 |
} |
848 |
} |
849 |
|
850 |
private class DeleteObjectTask extends AsyncTask<String, Void, HttpBundle> { |
851 |
|
852 |
private CloudServersException exception;
|
853 |
|
854 |
@Override
|
855 |
protected void onPreExecute() { |
856 |
showDialog(); |
857 |
app.setDeleteingObject(true);
|
858 |
deleteObjTask = new DeleteObjectListenerTask();
|
859 |
deleteObjTask.execute(); |
860 |
} |
861 |
|
862 |
@Override
|
863 |
protected HttpBundle doInBackground(String... args) { |
864 |
HttpBundle bundle = null;
|
865 |
try {
|
866 |
// subtring because the current directory contains a "/" at the
|
867 |
// end of the string
|
868 |
String cname = args[0]; |
869 |
String cpath = args[1]; |
870 |
bundle = (new ContainerObjectManager(getContext()))
|
871 |
.deleteObject(cname, cpath |
872 |
.substring(0, cpath.length() - 1)); |
873 |
} catch (CloudServersException e) {
|
874 |
exception = e; |
875 |
} |
876 |
return bundle;
|
877 |
} |
878 |
|
879 |
@Override
|
880 |
protected void onPostExecute(HttpBundle bundle) { |
881 |
app.setDeleteingObject(false);
|
882 |
hideDialog(); |
883 |
HttpResponse response = bundle.getResponse(); |
884 |
if (response != null) { |
885 |
int statusCode = response.getStatusLine().getStatusCode();
|
886 |
if (statusCode == 409) { |
887 |
showAlert("Error",
|
888 |
"Folder must be empty in order to delete");
|
889 |
} |
890 |
if (statusCode == 204) { |
891 |
setResult(Activity.RESULT_OK); |
892 |
} else {
|
893 |
CloudServersException cse = parseCloudServersException(response); |
894 |
if ("".equals(cse.getMessage())) { |
895 |
showError("There was a problem deleting your folder.",
|
896 |
bundle); |
897 |
} else {
|
898 |
showError("There was a problem deleting your folder: "
|
899 |
+ cse.getMessage(), bundle); |
900 |
} |
901 |
} |
902 |
} else if (exception != null) { |
903 |
showError("There was a problem deleting your folder: "
|
904 |
+ exception.getMessage(), bundle); |
905 |
} |
906 |
} |
907 |
} |
908 |
|
909 |
private class DeleteContainerTask extends |
910 |
AsyncTask<String, Void, HttpBundle> { |
911 |
|
912 |
private CloudServersException exception;
|
913 |
|
914 |
@Override
|
915 |
protected void onPreExecute() { |
916 |
showDialog(); |
917 |
app.setDeletingContainer(true);
|
918 |
deleteContainerTask = new DeleteContainerListenerTask();
|
919 |
deleteContainerTask.execute(); |
920 |
} |
921 |
|
922 |
@Override
|
923 |
protected HttpBundle doInBackground(String... object) { |
924 |
HttpBundle bundle = null;
|
925 |
try {
|
926 |
bundle = (new ContainerManager(getContext())).delete(container
|
927 |
.getName()); |
928 |
} catch (CloudServersException e) {
|
929 |
exception = e; |
930 |
} |
931 |
return bundle;
|
932 |
} |
933 |
|
934 |
@Override
|
935 |
protected void onPostExecute(HttpBundle bundle) { |
936 |
hideDialog(); |
937 |
app.setDeletingContainer(false);
|
938 |
HttpResponse response = bundle.getResponse(); |
939 |
if (response != null) { |
940 |
int statusCode = response.getStatusLine().getStatusCode();
|
941 |
if (statusCode == 409) { |
942 |
showError("Container must be empty in order to delete",
|
943 |
bundle); |
944 |
} |
945 |
if (statusCode == 204) { |
946 |
setResult(Activity.RESULT_OK); |
947 |
|
948 |
} else {
|
949 |
CloudServersException cse = parseCloudServersException(response); |
950 |
if ("".equals(cse.getMessage())) { |
951 |
showError( |
952 |
"There was a problem deleting your container.",
|
953 |
bundle); |
954 |
} else {
|
955 |
showError( |
956 |
"There was a problem deleting your container: "
|
957 |
+ cse.getMessage(), bundle); |
958 |
} |
959 |
} |
960 |
} else if (exception != null) { |
961 |
showError("There was a problem deleting your server: "
|
962 |
+ exception.getMessage(), bundle); |
963 |
} |
964 |
} |
965 |
} |
966 |
|
967 |
/*
|
968 |
* listens for the application to change isProcessing listens so activity
|
969 |
* knows when it should display the new curDirFiles
|
970 |
*/
|
971 |
private class AddObjectListenerTask extends AsyncTask<Void, Void, Void> { |
972 |
|
973 |
@Override
|
974 |
protected Void doInBackground(Void... arg1) { |
975 |
|
976 |
while (app.isAddingObject()) {
|
977 |
// wait for process to finish
|
978 |
// or have it be canceled
|
979 |
if (task.isCancelled()) {
|
980 |
return null; |
981 |
} |
982 |
} |
983 |
return null; |
984 |
} |
985 |
|
986 |
/*
|
987 |
* when no longer processing, time to load the new files
|
988 |
*/
|
989 |
@Override
|
990 |
protected void onPostExecute(Void arg1) { |
991 |
hideDialog(); |
992 |
loadFiles(); |
993 |
} |
994 |
} |
995 |
|
996 |
private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> { |
997 |
|
998 |
@Override
|
999 |
protected Void doInBackground(Void... arg1) { |
1000 |
|
1001 |
while (app.isDeletingObject()) {
|
1002 |
// wait for process to finish
|
1003 |
// or have it be canceled
|
1004 |
if (deleteObjTask.isCancelled()) {
|
1005 |
return null; |
1006 |
} |
1007 |
} |
1008 |
return null; |
1009 |
} |
1010 |
|
1011 |
/*
|
1012 |
* when no longer processing, time to load the new files
|
1013 |
*/
|
1014 |
@Override
|
1015 |
protected void onPostExecute(Void arg1) { |
1016 |
hideDialog(); |
1017 |
removeFromList(currentPath); |
1018 |
previousFiles=null;
|
1019 |
goUpDirectory(); |
1020 |
} |
1021 |
} |
1022 |
|
1023 |
private class DeleteContainerListenerTask extends |
1024 |
AsyncTask<Void, Void, Void> { |
1025 |
|
1026 |
@Override
|
1027 |
protected Void doInBackground(Void... arg1) { |
1028 |
|
1029 |
while (app.isDeletingContainer()) {
|
1030 |
// wait for process to finish
|
1031 |
// or have it be canceled
|
1032 |
if (deleteContainerTask.isCancelled()) {
|
1033 |
return null; |
1034 |
} |
1035 |
} |
1036 |
return null; |
1037 |
} |
1038 |
|
1039 |
/*
|
1040 |
* when no longer processing, time to load the new files
|
1041 |
*/
|
1042 |
@Override
|
1043 |
protected void onPostExecute(Void arg1) { |
1044 |
|
1045 |
hideDialog(); |
1046 |
setResult(RESULT_OK); |
1047 |
finish(); |
1048 |
} |
1049 |
} |
1050 |
|
1051 |
public String getCurrentPath() { |
1052 |
return currentPath;
|
1053 |
} |
1054 |
|
1055 |
public ContainerObjects[] getFiles() { |
1056 |
return files;
|
1057 |
} |
1058 |
|
1059 |
/***** UPLOAD FILE ****/
|
1060 |
protected void uploadFile(String filename, Intent data){ |
1061 |
if (filename.startsWith("file://")) { |
1062 |
filename = filename.substring(7);
|
1063 |
} |
1064 |
// replace %20 and so on
|
1065 |
filename = Uri.decode(filename); |
1066 |
|
1067 |
int indx = filename.lastIndexOf("//"); |
1068 |
File file = new File(data.getData().getPath()); |
1069 |
String filenameNew = null; |
1070 |
Long fileSize = null; |
1071 |
if (!file.exists()) {
|
1072 |
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null); |
1073 |
if (cursor != null) { |
1074 |
while (cursor.moveToNext()) {
|
1075 |
for (int i = 0; i < cursor.getColumnCount(); i++) { |
1076 |
if (cursor.getColumnName(i).equals("_data")) |
1077 |
filenameNew = cursor.getString(i); |
1078 |
if (cursor.getColumnName(i).equals("_size")) |
1079 |
fileSize = cursor.getLong(i); |
1080 |
} |
1081 |
} |
1082 |
} |
1083 |
} else {
|
1084 |
filenameNew = data.getData().getPath(); |
1085 |
} |
1086 |
if (filenameNew != null) { |
1087 |
file = new File(filenameNew); |
1088 |
String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(file.getPath());
|
1089 |
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
|
1090 |
String[] info = { |
1091 |
file.getName(), |
1092 |
mime, filenameNew}; |
1093 |
new AddFileTask().execute(info);
|
1094 |
//getTasks().getUploadTask(GssMyFolders.this).execute(getCurrentResource().getUri() + URLEncoder.encode(file.getName()), filenameNew);
|
1095 |
} |
1096 |
} |
1097 |
|
1098 |
protected void uploadFile(List<Uri> uris){ |
1099 |
List<String> strings = new ArrayList<String>(); |
1100 |
String path = "";//getCurrentResource().getUri(); |
1101 |
if(!path.endsWith("/")) |
1102 |
path = path+"/";
|
1103 |
|
1104 |
for(Uri data : uris){
|
1105 |
String filename = data.getPath();
|
1106 |
if (filename.startsWith("file://")) { |
1107 |
filename = filename.substring(7);
|
1108 |
} |
1109 |
// replace %20 and so on
|
1110 |
filename = Uri.decode(filename); |
1111 |
|
1112 |
int indx = filename.lastIndexOf("//"); |
1113 |
File file = new File(filename); |
1114 |
String filenameNew = null; |
1115 |
Long fileSize = null; |
1116 |
if (!file.exists()) {
|
1117 |
Cursor cursor = getContentResolver().query(data, null, null, null, null); |
1118 |
if (cursor != null) { |
1119 |
while (cursor.moveToNext()) {
|
1120 |
for (int i = 0; i < cursor.getColumnCount(); i++) { |
1121 |
if (cursor.getColumnName(i).equals("_data")) |
1122 |
filenameNew = cursor.getString(i); |
1123 |
if (cursor.getColumnName(i).equals("_size")) |
1124 |
fileSize = cursor.getLong(i); |
1125 |
} |
1126 |
} |
1127 |
} |
1128 |
} else {
|
1129 |
filenameNew = data.getPath(); |
1130 |
} |
1131 |
if (filenameNew != null) { |
1132 |
file = new File(filenameNew); |
1133 |
strings.add(path+URLEncoder.encode(file.getName()));
|
1134 |
strings.add(filenameNew); |
1135 |
} |
1136 |
} |
1137 |
Log.i("*******",""+strings.size()); |
1138 |
//getTasks().getMultiUploadTask(this).execute(strings.toArray(new String[strings.size()]));
|
1139 |
|
1140 |
|
1141 |
} |
1142 |
protected void uploadFile(Uri data){ |
1143 |
String filename = data.getPath();
|
1144 |
if (filename.startsWith("file://")) { |
1145 |
filename = filename.substring(7);
|
1146 |
} |
1147 |
// replace %20 and so on
|
1148 |
filename = Uri.decode(filename); |
1149 |
|
1150 |
int indx = filename.lastIndexOf("//"); |
1151 |
File file = new File(filename); |
1152 |
String filenameNew = null; |
1153 |
Long fileSize = null; |
1154 |
if (!file.exists()) {
|
1155 |
Cursor cursor = getContentResolver().query(data, null, null, null, null); |
1156 |
if (cursor != null) { |
1157 |
while (cursor.moveToNext()) {
|
1158 |
for (int i = 0; i < cursor.getColumnCount(); i++) { |
1159 |
if (cursor.getColumnName(i).equals("_data")) |
1160 |
filenameNew = cursor.getString(i); |
1161 |
if (cursor.getColumnName(i).equals("_size")) |
1162 |
fileSize = cursor.getLong(i); |
1163 |
} |
1164 |
} |
1165 |
} |
1166 |
} else {
|
1167 |
filenameNew = data.getPath(); |
1168 |
} |
1169 |
if (filenameNew != null) { |
1170 |
file = new File(filenameNew); |
1171 |
//String path = getCurrentResource().getUri();
|
1172 |
//if(!path.endsWith("/"))
|
1173 |
//path = path+"/";
|
1174 |
//getTasks().getUploadTask(GssMyFolders.this).execute(path + URLEncoder.encode(file.getName()), filenameNew);
|
1175 |
} |
1176 |
|
1177 |
} |
1178 |
} |