root / src / com / rackspace / cloud / android / ContainerObjectsActivity.java @ ecbad159
History | View | Annotate | Download (32.6 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();
|
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 |
loadFiles(); |
637 |
} |
638 |
|
639 |
if (resultCode == RESULT_OK && requestCode == 1) { |
640 |
String filename = data.getDataString();
|
641 |
uploadFile(filename, data); |
642 |
|
643 |
} |
644 |
|
645 |
// deleted file so need to update the list
|
646 |
if (requestCode == 55 && resultCode == 99) { |
647 |
loadFiles(); |
648 |
} |
649 |
|
650 |
} |
651 |
|
652 |
class FileAdapter extends ArrayAdapter<ContainerObjects> { |
653 |
FileAdapter() { |
654 |
super(ContainerObjectsActivity.this,
|
655 |
R.layout.listcontainerobjectcell, app.getCurFiles()); |
656 |
} |
657 |
|
658 |
public View getView(int position, View convertView, ViewGroup parent) { |
659 |
ContainerObjects file = app.getCurFiles().get(position); |
660 |
LayoutInflater inflater = getLayoutInflater(); |
661 |
View row = inflater.inflate(R.layout.listcontainerobjectcell,
|
662 |
parent, false);
|
663 |
|
664 |
TextView label = (TextView) row.findViewById(R.id.label); |
665 |
label.setText(getShortName(file.getCName())); |
666 |
|
667 |
ImageView objectImage = (ImageView) row |
668 |
.findViewById(R.id.file_type_image); |
669 |
if (file.getContentType().startsWith("application/directory")||file.getContentType().startsWith("application/folder")) { |
670 |
objectImage.setImageResource(R.drawable.folder); |
671 |
} else {
|
672 |
objectImage.setImageResource(R.drawable.file); |
673 |
} |
674 |
|
675 |
if (file.getBytes() >= bConver) {
|
676 |
megaBytes = Math.abs(file.getBytes() / bConver + 0.2); |
677 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
678 |
sublabel.setText(megaBytes + " MB");
|
679 |
} else if (file.getBytes() >= kbConver) { |
680 |
kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2); |
681 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
682 |
sublabel.setText(kiloBytes + " KB");
|
683 |
} else {
|
684 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
685 |
sublabel.setText(file.getBytes() + " B");
|
686 |
} |
687 |
|
688 |
return (row);
|
689 |
} |
690 |
} |
691 |
|
692 |
private class LoadFilesTask extends |
693 |
AsyncTask<String, Void, ArrayList<ContainerObjects>> { |
694 |
|
695 |
private CloudServersException exception;
|
696 |
|
697 |
protected void onPreExecute() { |
698 |
showDialog(); |
699 |
loadingFiles = true;
|
700 |
} |
701 |
|
702 |
@Override
|
703 |
protected ArrayList<ContainerObjects> doInBackground(String... path) { |
704 |
ArrayList<ContainerObjects> files = null; |
705 |
try {
|
706 |
if (container.getName().equals(Container.MYSHARED)) { |
707 |
files = (new ContainerObjectManager(getContext()))
|
708 |
.createListMyShared(true, container.getName(),
|
709 |
new ContainerManager(getContext())
|
710 |
.createList(true));
|
711 |
} else if (container.getName().equals(Container.OTHERS)) { |
712 |
|
713 |
} else {
|
714 |
if (container.getOtherUser() == null) |
715 |
files = (new ContainerObjectManager(getContext()))
|
716 |
.createList(true, container.getName(),path[0]); |
717 |
else
|
718 |
files = (new ContainerObjectManager(getContext()))
|
719 |
.createOtherList(true, container.getName(),
|
720 |
container.getOtherUser()); |
721 |
} |
722 |
} catch (CloudServersException e) {
|
723 |
exception = e; |
724 |
e.printStackTrace(); |
725 |
} |
726 |
return files;
|
727 |
} |
728 |
|
729 |
@Override
|
730 |
protected void onPostExecute(ArrayList<ContainerObjects> result) { |
731 |
loadingFiles = false;
|
732 |
hideDialog(); |
733 |
if (exception != null) { |
734 |
showAlert("Error", exception.getMessage());
|
735 |
} |
736 |
setFileList(result); |
737 |
loadCurrentDirectoryFiles(); |
738 |
displayCurrentFiles(); |
739 |
} |
740 |
|
741 |
} |
742 |
|
743 |
private class AddFolderTask extends AsyncTask<String, Void, HttpBundle> { |
744 |
|
745 |
private CloudServersException exception;
|
746 |
|
747 |
@Override
|
748 |
protected void onPreExecute() { |
749 |
showDialog(); |
750 |
app.setAddingObject(true);
|
751 |
task = new AddObjectListenerTask();
|
752 |
task.execute(); |
753 |
} |
754 |
|
755 |
@Override
|
756 |
protected HttpBundle doInBackground(String... data) { |
757 |
HttpBundle bundle = null;
|
758 |
try {
|
759 |
|
760 |
bundle = (new ContainerObjectManager(getContext())).addObject(
|
761 |
container.getName(), currentPath, data[0], data[1]); |
762 |
} catch (CloudServersException e) {
|
763 |
exception = e; |
764 |
} |
765 |
return bundle;
|
766 |
} |
767 |
|
768 |
@Override
|
769 |
protected void onPostExecute(HttpBundle bundle) { |
770 |
app.setAddingObject(false);
|
771 |
hideDialog(); |
772 |
HttpResponse response = bundle.getResponse(); |
773 |
if (response != null) { |
774 |
int statusCode = response.getStatusLine().getStatusCode();
|
775 |
if (statusCode == 201) { |
776 |
setResult(Activity.RESULT_OK); |
777 |
// loading the new files is done by ListenerTask
|
778 |
} else {
|
779 |
CloudServersException cse = parseCloudServersException(response); |
780 |
if ("".equals(cse.getMessage())) { |
781 |
showError("There was a problem deleting your folder.",
|
782 |
bundle); |
783 |
} else {
|
784 |
showError("There was a problem deleting your folder: "
|
785 |
+ cse.getMessage(), bundle); |
786 |
} |
787 |
} |
788 |
} else if (exception != null) { |
789 |
showError("There was a problem deleting your folder: "
|
790 |
+ exception.getMessage(), bundle); |
791 |
} |
792 |
} |
793 |
} |
794 |
|
795 |
private class AddFileTask extends AsyncTask<String, Void, HttpBundle> { |
796 |
|
797 |
private CloudServersException exception;
|
798 |
|
799 |
@Override
|
800 |
protected void onPreExecute() { |
801 |
showDialog(); |
802 |
app.setAddingObject(true);
|
803 |
task = new AddObjectListenerTask();
|
804 |
task.execute(); |
805 |
} |
806 |
|
807 |
@Override
|
808 |
protected HttpBundle doInBackground(String... data) { |
809 |
HttpBundle bundle = null;
|
810 |
try {
|
811 |
|
812 |
bundle = (new ContainerObjectManager(getContext())).addFileObject(
|
813 |
container.getName(), currentPath, data[0], data[1], data[2]); |
814 |
} catch (CloudServersException e) {
|
815 |
exception = e; |
816 |
} |
817 |
return bundle;
|
818 |
} |
819 |
|
820 |
@Override
|
821 |
protected void onPostExecute(HttpBundle bundle) { |
822 |
app.setAddingObject(false);
|
823 |
hideDialog(); |
824 |
HttpResponse response = bundle.getResponse(); |
825 |
if (response != null) { |
826 |
int statusCode = response.getStatusLine().getStatusCode();
|
827 |
if (statusCode == 201) { |
828 |
setResult(Activity.RESULT_OK); |
829 |
// loading the new files is done by ListenerTask
|
830 |
} else {
|
831 |
CloudServersException cse = parseCloudServersException(response); |
832 |
if ("".equals(cse.getMessage())) { |
833 |
showError("There was a problem deleting your folder.",
|
834 |
bundle); |
835 |
} else {
|
836 |
showError("There was a problem deleting your folder: "
|
837 |
+ cse.getMessage(), bundle); |
838 |
} |
839 |
} |
840 |
} else if (exception != null) { |
841 |
showError("There was a problem deleting your folder: "
|
842 |
+ exception.getMessage(), bundle); |
843 |
} |
844 |
} |
845 |
} |
846 |
|
847 |
private class DeleteObjectTask extends AsyncTask<Void, Void, HttpBundle> { |
848 |
|
849 |
private CloudServersException exception;
|
850 |
|
851 |
@Override
|
852 |
protected void onPreExecute() { |
853 |
showDialog(); |
854 |
app.setDeleteingObject(true);
|
855 |
deleteObjTask = new DeleteObjectListenerTask();
|
856 |
deleteObjTask.execute(); |
857 |
} |
858 |
|
859 |
@Override
|
860 |
protected HttpBundle doInBackground(Void... arg0) { |
861 |
HttpBundle bundle = null;
|
862 |
try {
|
863 |
// subtring because the current directory contains a "/" at the
|
864 |
// end of the string
|
865 |
bundle = (new ContainerObjectManager(getContext()))
|
866 |
.deleteObject(container.getName(), currentPath |
867 |
.substring(0, currentPath.length() - 1)); |
868 |
} catch (CloudServersException e) {
|
869 |
exception = e; |
870 |
} |
871 |
return bundle;
|
872 |
} |
873 |
|
874 |
@Override
|
875 |
protected void onPostExecute(HttpBundle bundle) { |
876 |
app.setDeleteingObject(false);
|
877 |
hideDialog(); |
878 |
HttpResponse response = bundle.getResponse(); |
879 |
if (response != null) { |
880 |
int statusCode = response.getStatusLine().getStatusCode();
|
881 |
if (statusCode == 409) { |
882 |
showAlert("Error",
|
883 |
"Folder must be empty in order to delete");
|
884 |
} |
885 |
if (statusCode == 204) { |
886 |
setResult(Activity.RESULT_OK); |
887 |
} else {
|
888 |
CloudServersException cse = parseCloudServersException(response); |
889 |
if ("".equals(cse.getMessage())) { |
890 |
showError("There was a problem deleting your folder.",
|
891 |
bundle); |
892 |
} else {
|
893 |
showError("There was a problem deleting your folder: "
|
894 |
+ cse.getMessage(), bundle); |
895 |
} |
896 |
} |
897 |
} else if (exception != null) { |
898 |
showError("There was a problem deleting your folder: "
|
899 |
+ exception.getMessage(), bundle); |
900 |
} |
901 |
} |
902 |
} |
903 |
|
904 |
private class DeleteContainerTask extends |
905 |
AsyncTask<String, Void, HttpBundle> { |
906 |
|
907 |
private CloudServersException exception;
|
908 |
|
909 |
@Override
|
910 |
protected void onPreExecute() { |
911 |
showDialog(); |
912 |
app.setDeletingContainer(true);
|
913 |
deleteContainerTask = new DeleteContainerListenerTask();
|
914 |
deleteContainerTask.execute(); |
915 |
} |
916 |
|
917 |
@Override
|
918 |
protected HttpBundle doInBackground(String... object) { |
919 |
HttpBundle bundle = null;
|
920 |
try {
|
921 |
bundle = (new ContainerManager(getContext())).delete(container
|
922 |
.getName()); |
923 |
} catch (CloudServersException e) {
|
924 |
exception = e; |
925 |
} |
926 |
return bundle;
|
927 |
} |
928 |
|
929 |
@Override
|
930 |
protected void onPostExecute(HttpBundle bundle) { |
931 |
hideDialog(); |
932 |
app.setDeletingContainer(false);
|
933 |
HttpResponse response = bundle.getResponse(); |
934 |
if (response != null) { |
935 |
int statusCode = response.getStatusLine().getStatusCode();
|
936 |
if (statusCode == 409) { |
937 |
showError("Container must be empty in order to delete",
|
938 |
bundle); |
939 |
} |
940 |
if (statusCode == 204) { |
941 |
setResult(Activity.RESULT_OK); |
942 |
|
943 |
} else {
|
944 |
CloudServersException cse = parseCloudServersException(response); |
945 |
if ("".equals(cse.getMessage())) { |
946 |
showError( |
947 |
"There was a problem deleting your container.",
|
948 |
bundle); |
949 |
} else {
|
950 |
showError( |
951 |
"There was a problem deleting your container: "
|
952 |
+ cse.getMessage(), bundle); |
953 |
} |
954 |
} |
955 |
} else if (exception != null) { |
956 |
showError("There was a problem deleting your server: "
|
957 |
+ exception.getMessage(), bundle); |
958 |
} |
959 |
} |
960 |
} |
961 |
|
962 |
/*
|
963 |
* listens for the application to change isProcessing listens so activity
|
964 |
* knows when it should display the new curDirFiles
|
965 |
*/
|
966 |
private class AddObjectListenerTask extends AsyncTask<Void, Void, Void> { |
967 |
|
968 |
@Override
|
969 |
protected Void doInBackground(Void... arg1) { |
970 |
|
971 |
while (app.isAddingObject()) {
|
972 |
// wait for process to finish
|
973 |
// or have it be canceled
|
974 |
if (task.isCancelled()) {
|
975 |
return null; |
976 |
} |
977 |
} |
978 |
return null; |
979 |
} |
980 |
|
981 |
/*
|
982 |
* when no longer processing, time to load the new files
|
983 |
*/
|
984 |
@Override
|
985 |
protected void onPostExecute(Void arg1) { |
986 |
hideDialog(); |
987 |
loadFiles(); |
988 |
} |
989 |
} |
990 |
|
991 |
private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> { |
992 |
|
993 |
@Override
|
994 |
protected Void doInBackground(Void... arg1) { |
995 |
|
996 |
while (app.isDeletingObject()) {
|
997 |
// wait for process to finish
|
998 |
// or have it be canceled
|
999 |
if (deleteObjTask.isCancelled()) {
|
1000 |
return null; |
1001 |
} |
1002 |
} |
1003 |
return null; |
1004 |
} |
1005 |
|
1006 |
/*
|
1007 |
* when no longer processing, time to load the new files
|
1008 |
*/
|
1009 |
@Override
|
1010 |
protected void onPostExecute(Void arg1) { |
1011 |
hideDialog(); |
1012 |
removeFromList(currentPath); |
1013 |
goUpDirectory(); |
1014 |
} |
1015 |
} |
1016 |
|
1017 |
private class DeleteContainerListenerTask extends |
1018 |
AsyncTask<Void, Void, Void> { |
1019 |
|
1020 |
@Override
|
1021 |
protected Void doInBackground(Void... arg1) { |
1022 |
|
1023 |
while (app.isDeletingContainer()) {
|
1024 |
// wait for process to finish
|
1025 |
// or have it be canceled
|
1026 |
if (deleteContainerTask.isCancelled()) {
|
1027 |
return null; |
1028 |
} |
1029 |
} |
1030 |
return null; |
1031 |
} |
1032 |
|
1033 |
/*
|
1034 |
* when no longer processing, time to load the new files
|
1035 |
*/
|
1036 |
@Override
|
1037 |
protected void onPostExecute(Void arg1) { |
1038 |
|
1039 |
hideDialog(); |
1040 |
setResult(RESULT_OK); |
1041 |
finish(); |
1042 |
} |
1043 |
} |
1044 |
|
1045 |
public String getCurrentPath() { |
1046 |
return currentPath;
|
1047 |
} |
1048 |
|
1049 |
public ContainerObjects[] getFiles() { |
1050 |
return files;
|
1051 |
} |
1052 |
|
1053 |
/***** UPLOAD FILE ****/
|
1054 |
protected void uploadFile(String filename, Intent data){ |
1055 |
if (filename.startsWith("file://")) { |
1056 |
filename = filename.substring(7);
|
1057 |
} |
1058 |
// replace %20 and so on
|
1059 |
filename = Uri.decode(filename); |
1060 |
|
1061 |
int indx = filename.lastIndexOf("//"); |
1062 |
File file = new File(data.getData().getPath()); |
1063 |
String filenameNew = null; |
1064 |
Long fileSize = null; |
1065 |
if (!file.exists()) {
|
1066 |
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null); |
1067 |
if (cursor != null) { |
1068 |
while (cursor.moveToNext()) {
|
1069 |
for (int i = 0; i < cursor.getColumnCount(); i++) { |
1070 |
if (cursor.getColumnName(i).equals("_data")) |
1071 |
filenameNew = cursor.getString(i); |
1072 |
if (cursor.getColumnName(i).equals("_size")) |
1073 |
fileSize = cursor.getLong(i); |
1074 |
} |
1075 |
} |
1076 |
} |
1077 |
} else {
|
1078 |
filenameNew = data.getData().getPath(); |
1079 |
} |
1080 |
if (filenameNew != null) { |
1081 |
file = new File(filenameNew); |
1082 |
String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(file.getPath());
|
1083 |
String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
|
1084 |
String[] info = { |
1085 |
file.getName(), |
1086 |
mime, filenameNew}; |
1087 |
new AddFileTask().execute(info);
|
1088 |
//getTasks().getUploadTask(GssMyFolders.this).execute(getCurrentResource().getUri() + URLEncoder.encode(file.getName()), filenameNew);
|
1089 |
} |
1090 |
} |
1091 |
|
1092 |
protected void uploadFile(List<Uri> uris){ |
1093 |
List<String> strings = new ArrayList<String>(); |
1094 |
String path = "";//getCurrentResource().getUri(); |
1095 |
if(!path.endsWith("/")) |
1096 |
path = path+"/";
|
1097 |
|
1098 |
for(Uri data : uris){
|
1099 |
String filename = data.getPath();
|
1100 |
if (filename.startsWith("file://")) { |
1101 |
filename = filename.substring(7);
|
1102 |
} |
1103 |
// replace %20 and so on
|
1104 |
filename = Uri.decode(filename); |
1105 |
|
1106 |
int indx = filename.lastIndexOf("//"); |
1107 |
File file = new File(filename); |
1108 |
String filenameNew = null; |
1109 |
Long fileSize = null; |
1110 |
if (!file.exists()) {
|
1111 |
Cursor cursor = getContentResolver().query(data, null, null, null, null); |
1112 |
if (cursor != null) { |
1113 |
while (cursor.moveToNext()) {
|
1114 |
for (int i = 0; i < cursor.getColumnCount(); i++) { |
1115 |
if (cursor.getColumnName(i).equals("_data")) |
1116 |
filenameNew = cursor.getString(i); |
1117 |
if (cursor.getColumnName(i).equals("_size")) |
1118 |
fileSize = cursor.getLong(i); |
1119 |
} |
1120 |
} |
1121 |
} |
1122 |
} else {
|
1123 |
filenameNew = data.getPath(); |
1124 |
} |
1125 |
if (filenameNew != null) { |
1126 |
file = new File(filenameNew); |
1127 |
strings.add(path+URLEncoder.encode(file.getName()));
|
1128 |
strings.add(filenameNew); |
1129 |
} |
1130 |
} |
1131 |
Log.i("*******",""+strings.size()); |
1132 |
//getTasks().getMultiUploadTask(this).execute(strings.toArray(new String[strings.size()]));
|
1133 |
|
1134 |
|
1135 |
} |
1136 |
protected void uploadFile(Uri data){ |
1137 |
String filename = data.getPath();
|
1138 |
if (filename.startsWith("file://")) { |
1139 |
filename = filename.substring(7);
|
1140 |
} |
1141 |
// replace %20 and so on
|
1142 |
filename = Uri.decode(filename); |
1143 |
|
1144 |
int indx = filename.lastIndexOf("//"); |
1145 |
File file = new File(filename); |
1146 |
String filenameNew = null; |
1147 |
Long fileSize = null; |
1148 |
if (!file.exists()) {
|
1149 |
Cursor cursor = getContentResolver().query(data, null, null, null, null); |
1150 |
if (cursor != null) { |
1151 |
while (cursor.moveToNext()) {
|
1152 |
for (int i = 0; i < cursor.getColumnCount(); i++) { |
1153 |
if (cursor.getColumnName(i).equals("_data")) |
1154 |
filenameNew = cursor.getString(i); |
1155 |
if (cursor.getColumnName(i).equals("_size")) |
1156 |
fileSize = cursor.getLong(i); |
1157 |
} |
1158 |
} |
1159 |
} |
1160 |
} else {
|
1161 |
filenameNew = data.getPath(); |
1162 |
} |
1163 |
if (filenameNew != null) { |
1164 |
file = new File(filenameNew); |
1165 |
//String path = getCurrentResource().getUri();
|
1166 |
//if(!path.endsWith("/"))
|
1167 |
//path = path+"/";
|
1168 |
//getTasks().getUploadTask(GssMyFolders.this).execute(path + URLEncoder.encode(file.getName()), filenameNew);
|
1169 |
} |
1170 |
|
1171 |
} |
1172 |
} |