Revision d3ea294b src/com/rackspacecloud/android/ContainerObjectsActivity.java

b/src/com/rackspacecloud/android/ContainerObjectsActivity.java
3 3
import java.io.IOException;
4 4
import java.io.StringReader;
5 5
import java.util.ArrayList;
6
import java.util.Arrays;
6 7

  
7 8
import javax.xml.parsers.FactoryConfigurationError;
8 9
import javax.xml.parsers.ParserConfigurationException;
......
20 21
import android.app.AlertDialog;
21 22
import android.app.Dialog;
22 23
import android.app.ListActivity;
24
import android.app.ProgressDialog;
23 25
import android.content.Context;
24 26
import android.content.DialogInterface;
25 27
import android.content.Intent;
......
33 35
import android.view.View;
34 36
import android.view.ViewGroup;
35 37
import android.widget.ArrayAdapter;
38
import android.widget.EditText;
36 39
import android.widget.ListView;
37 40
import android.widget.TextView;
38 41

  
......
42 45
import com.rackspace.cloud.files.api.client.ContainerObjects;
43 46
import com.rackspace.cloud.servers.api.client.CloudServersException;
44 47
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
48
//import com.rackspacecloud.android.ViewServerActivity.RenameServerTask;
45 49

  
46 50
/**
47 51
 * 
......
63 67
	private Context context;
64 68
	private String currentPath;
65 69
	private ContainerObjects[] curDirFiles;
70
	ProgressDialog dialog;
66 71

  
67 72
	@Override
68 73
	public void onCreate(Bundle savedInstanceState) {
......
83 88
		super.onSaveInstanceState(outState);
84 89
		outState.putSerializable("container", files);
85 90
		outState.putString("path", currentPath);
91
		outState.putSerializable("curFiles", curDirFiles);
86 92
	}
87 93

  
88 94
	private void restoreState(Bundle state) {
89
		getPath();
90
		
91
		if (state != null && state.containsKey("container")) {
92
			files = (ContainerObjects[]) state.getSerializable("container");
93
			if (files.length == 0) {
94
				displayNoServersCell();
95
			} else {
96
				getListView().setDividerHeight(1); // restore divider lines
97
				setListAdapter(new FileAdapter());
95
		if(state != null){
96
			if(state.containsKey("path")){
97
				currentPath = state.getString("path");
98 98
			}
99
		} 
100
		else {
101
			loadFiles();
102
		}
103
	}
104

  
105
	protected void onListItemClick(ListView l, View v, int position, long id) {
106
		if (curDirFiles != null && curDirFiles.length > 0) {
107
			Intent viewIntent;
108
			if(curDirFiles[position].getContentType().equals("application/directory")){
109
				/*
110
				viewIntent = new Intent(this, ContainerObjectsActivity.class);
111
				viewIntent.putExtra("container", container);
112
				viewIntent.putExtra("cdnUrl", container.getCdnUrl());
113
				viewIntent.putExtra("containerNames", container.getName());
114
				viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
115
				viewIntent.putExtra("path", files[position].getCName());
116
				Log.d("the path name is: ","captin " + files[position].getCName());
117
				startActivityForResult(viewIntent, 56);
118
				*/
119
				
120
				currentPath = curDirFiles[position].getCName() + "/";
121
				loadCurrentDirectoryFiles();
122
				displayCurrentFiles();
123
			}
124

  
125 99
			else{
126
				viewIntent = new Intent(this, ContainerObjectDetails.class);
127
				viewIntent.putExtra("container", files[position]);
128
				viewIntent.putExtra("cdnUrl", container.getCdnUrl());
129
				viewIntent.putExtra("containerNames", container.getName());
130
				viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
131
				startActivityForResult(viewIntent, 55); // arbitrary number; never
132
				// used again
100
				currentPath = "";
101
			}
102
			if(state.containsKey("container") && state.containsKey("curFiles")){
103
				files = (ContainerObjects[]) state.getSerializable("container");
104
				curDirFiles = (ContainerObjects[]) state.getSerializable("curFiles");
105
				if(curDirFiles.length == 0){
106
					displayNoServersCell();
107
				} else {
108
					getListView().setDividerHeight(1); //restore divider lines
109
					setListAdapter(new FileAdapter());
110
				}
133 111
			}
134 112
		}
113
		else {
114
			currentPath = "";
115
			loadFiles();
116
		}	
135 117
	}
136
	
118

  
137 119
	/*
138 120
	 * overriding back button press, because we are not actually changing
139
	 * activites when we navigate the file structure
121
	 * activities when we navigate the file structure
140 122
	 */
141 123
	public void onBackPressed() {
142 124
		if(currentPath.equals("")){
......
156 138
		displayCurrentFiles();
157 139
	}
158 140

  
159
	/*
160
	 * get the path that was stored in the intent
161
	 */
162
	private void getPath(){
163
		if(this.getIntent().getExtras().containsKey("path")){
164
			currentPath = (String)this.getIntent().getExtras().get("path");
165
		}
166
		else{
167
			currentPath = "";
168
		}
169
		
170
	}
171

  
172 141
	private void loadFiles() {
173
		displayLoadingCell();
142
		//displayLoadingCell();
174 143
		new LoadFilesTask().execute();
175 144
	}
145

  
176 146
	
177 147
	/* load only the files that should display for the 
178 148
	 * current directory in the curDirFiles[]
......
242 212
		}
243 213
	}
244 214

  
245
	private void displayLoadingCell() {
246
		String a[] = new String[1];
247
		a[0] = "Loading...";
248
		setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell,
249
				R.id.loading_label, a));
250
		getListView().setTextFilterEnabled(true);
251
		getListView().setDividerHeight(0); // hide the dividers so it won't look
252
											// like a list row
253
		getListView().setItemsCanFocus(false);
254
	}
255

  
256 215
	/*
257 216
	 * display a different empty page depending
258 217
	 * of if you are at top of container or
......
295 254
		}
296 255
	}
297 256

  
298
	private class LoadFilesTask extends
299
			AsyncTask<String, Void, ArrayList<ContainerObjects>> {
300

  
301
		private CloudServersException exception;
302

  
303
		@Override
304
		protected ArrayList<ContainerObjects> doInBackground(String... path) {
305
			ArrayList<ContainerObjects> files = null;
306
			try {
307
				files = (new ContainerObjectManager(context)).createList(true,
308
						container.getName());
309
			} catch (CloudServersException e) {
310
				exception = e;
311
				e.printStackTrace();
312
			}
313
			return files;
314
		}
315

  
316
		@Override
317
		protected void onPostExecute(ArrayList<ContainerObjects> result) {
318
			if (exception != null) {
319
				showAlert("Error", exception.getMessage());
320
			}
321
			setFileList(result);
322
		}
323
	}
324

  
325
	class FileAdapter extends ArrayAdapter<ContainerObjects> {
326
		FileAdapter() {
327
			super(ContainerObjectsActivity.this,
328
					R.layout.listcontainerobjectcell, curDirFiles);
329
		}
330

  
331
		public View getView(int position, View convertView, ViewGroup parent) {
332

  
333
			ContainerObjects file = curDirFiles[position];
334
			LayoutInflater inflater = getLayoutInflater();
335
			View row = inflater.inflate(R.layout.listcontainerobjectcell,
336
					parent, false);
337

  
338
			TextView label = (TextView) row.findViewById(R.id.label);
339
			//label.setText(file.getCName());
340
			label.setText(file.getCName());
341

  
342
			if (file.getBytes() >= bConver) {
343
				megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
344
				TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
345
				sublabel.setText(megaBytes + " MB");
346
			} else if (file.getBytes() >= kbConver) {
347
				kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
348
				TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
349
				sublabel.setText(kiloBytes + " KB");
350
			} else {
351
				TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
352
				sublabel.setText(file.getBytes() + " B");
353
			}
354

  
355
			return (row);
356
		}
357
	}
358
	
359 257
	/* just get the last part of the filename
360 258
	 * so the entire file path does not show
361 259
	*/
......
365 263
			return s;
366 264
		}
367 265
		else {
368
			return s.substring(s.lastIndexOf('/'));
266
			return s.substring(s.lastIndexOf('/')+1);
267
		}
268
	}
269
	
270
	/*
271
	 * removed a specified object from the array of 
272
	 * all the files in the container
273
	 */
274
	private void removeFromList(String path){
275
		ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(Arrays.asList(files));
276
		for(int i = 0; i < files.length; i++){
277
			if(files[i].getCName().equals(path.substring(0, path.length()-1))){
278
				temp.remove(i);
279
			}
280
		}
281
		files = new ContainerObjects[temp.size()];
282
		for(int i = 0; i < temp.size(); i++){
283
			files[i] = temp.get(i);
284
		}
285
	}
286

  
287
	protected void onListItemClick(ListView l, View v, int position, long id) {
288
		if (curDirFiles != null && curDirFiles.length > 0) {
289
			Intent viewIntent;
290
			if(curDirFiles[position].getContentType().equals("application/directory")){			
291
				currentPath = curDirFiles[position].getCName() + "/";
292
				loadCurrentDirectoryFiles();
293
				displayCurrentFiles();
294
			}
295
	
296
			else{
297
				viewIntent = new Intent(this, ContainerObjectDetails.class);
298
				viewIntent.putExtra("container", curDirFiles[position]);
299
				viewIntent.putExtra("cdnUrl", container.getCdnUrl());
300
				viewIntent.putExtra("containerNames", container.getName());
301
				viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
302
				startActivityForResult(viewIntent, 55); // arbitrary number; never
303
				// used again
304
			}
369 305
		}
370 306
	}
371 307

  
......
389 325
		switch (item.getItemId()) {
390 326
		case R.id.delete_container:
391 327
			if(currentPath.equals("")){
392
				Log.d("deleting", "captin deleting a container");
393 328
				showDialog(deleteContainer);
394 329
			}
395 330
			else{
396
				Log.d("deleting", "captin deleting a folder");
397 331
				showDialog(deleteFolder);
398 332
			}
399 333
			return true;
......
405 339
		case R.id.refresh:
406 340
			loadFiles();
407 341
			return true;
342
		case R.id.add_folder:
343
			showDialog(R.id.add_folder);
344
		case R.id.add_file:
345
			Intent viewIntent2 = new Intent(this, AddFileActivity.class);
346
			viewIntent2.putExtra("Cname", container.getName());
347
			viewIntent2.putExtra("curPath", currentPath);
348
			startActivityForResult(viewIntent2, 56);
349
			return true;	
408 350
		}
409 351
		return false;
410 352
	}
......
487 429
					}
488 430
				}).create();
489 431
			}
490

  
432
		case R.id.add_folder:
433
			final EditText input = new EditText(this);
434
            return new AlertDialog.Builder(ContainerObjectsActivity.this)
435
        	.setIcon(R.drawable.alert_dialog_icon)
436
            .setView(input)
437
        	.setTitle("Add Folder")
438
        	.setMessage("Enter new name for folder: ")        	         
439
        	.setPositiveButton("Add", new DialogInterface.OnClickListener() {
440
        		public void onClick(DialogInterface dialog, int whichButton) {
441
        			// User clicked OK so do some stuff
442
        			String[] info = {input.getText().toString(), "application/directory"};
443
        			new AddFolderTask().execute(info);
444
        		}
445
        	})
446
        	.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
447
        		public void onClick(DialogInterface dialog, int whichButton) {
448
        			// User clicked Cancel so do some stuff
449
        		}
450
        	})
451
        	.create();     
491 452
		}
492 453
		return null;
493 454
	}
......
539 500
		}
540 501
		return cse;
541 502
	}
503
	
504
	class FileAdapter extends ArrayAdapter<ContainerObjects> {
505
		FileAdapter() {
506
			super(ContainerObjectsActivity.this,
507
					R.layout.listcontainerobjectcell, curDirFiles);
508
		}
509
	
510
		public View getView(int position, View convertView, ViewGroup parent) {
511
	
512
			ContainerObjects file = curDirFiles[position];
513
			LayoutInflater inflater = getLayoutInflater();
514
			View row = inflater.inflate(R.layout.listcontainerobjectcell,
515
					parent, false);
516
	
517
			TextView label = (TextView) row.findViewById(R.id.label);
518
			//label.setText(file.getCName());
519
			label.setText(getShortName(file.getCName()));
520
	
521
			if (file.getBytes() >= bConver) {
522
				megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
523
				TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
524
				sublabel.setText(megaBytes + " MB");
525
			} else if (file.getBytes() >= kbConver) {
526
				kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
527
				TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
528
				sublabel.setText(kiloBytes + " KB");
529
			} else {
530
				TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
531
				sublabel.setText(file.getBytes() + " B");
532
			}
533
	
534
			return (row);
535
		}
536
	}
542 537

  
538
	private class LoadFilesTask extends
539
			AsyncTask<String, Void, ArrayList<ContainerObjects>> {
540
	
541
		private CloudServersException exception;
542
		
543
		protected void onPreExecute(){
544
			dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Loading Files...", true);
545
		}
543 546
	
547
		@Override
548
		protected ArrayList<ContainerObjects> doInBackground(String... path) {
549
			ArrayList<ContainerObjects> files = null;
550
			try {
551
				files = (new ContainerObjectManager(context)).createList(true,
552
						container.getName());
553
			} catch (CloudServersException e) {
554
				exception = e;
555
				e.printStackTrace();
556
			}
557
			return files;
558
		}
559
	
560
		@Override
561
		protected void onPostExecute(ArrayList<ContainerObjects> result) {
562
			dialog.dismiss();
563
			if (exception != null) {
564
				showAlert("Error", exception.getMessage());
565
			}
566
			setFileList(result);
567
		}
568
	}
569

  
544 570
	private class DeleteObjectTask extends
545 571
	AsyncTask<Void, Void, HttpResponse> {
546 572

  
547 573
		private CloudServersException exception;
548

  
574
		
575
		protected void onPreExecute(){
576
			dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Deleting...", true);
577
		}
578
		
549 579
		@Override
550 580
		protected HttpResponse doInBackground(Void... arg0) {
551 581
			HttpResponse resp = null;
......
568 598
				}
569 599
				if (statusCode == 204) {
570 600
					setResult(Activity.RESULT_OK);
601
					removeFromList(currentPath);
571 602
					goUpDirectory();
572
					loadFiles();
573

  
603
					dialog.dismiss();
574 604
				} else {
575 605
					CloudServersException cse = parseCloudServersException(response);
576 606
					if ("".equals(cse.getMessage())) {
......
589 619
		}
590 620
	}
591 621
	
622
	private class AddFolderTask extends
623
	AsyncTask<String, Void, HttpResponse> {
624
	
625
		private CloudServersException exception;
626
		
627
		protected void onPreExecute(){
628
			dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Adding Folder...", true);
629
		}
630
	
631
		@Override
632
		protected HttpResponse doInBackground(String... data) {
633
			HttpResponse resp = null;
634
			try {
635
				resp = (new ContainerObjectManager(context)).addObject(container.getName(), currentPath, data[0], data[1]);
636
			} catch (CloudServersException e) {
637
				exception = e;
638
			}
639
			return resp;
640
		}
641
	
642
		@Override
643
		protected void onPostExecute(HttpResponse response) {
644
			dialog.dismiss();
645
			if (response != null) {
646
				int statusCode = response.getStatusLine().getStatusCode();
647
				if (statusCode == 201) {
648
					setResult(Activity.RESULT_OK);
649
					loadFiles();
650
				} else {
651
					CloudServersException cse = parseCloudServersException(response);
652
					if ("".equals(cse.getMessage())) {
653
						showAlert("Error",
654
							"There was a problem deleting your folder.");
655
					} else {
656
						showAlert("Error",
657
								"There was a problem deleting your folder: "
658
									+ cse.getMessage());
659
					}
660
				}
661
			} else if (exception != null) {
662
				showAlert("Error", "There was a problem deleting your folder: "
663
						+ exception.getMessage());
664
			}
665
		}
666
	}
667

  
592 668
	private class DeleteContainerTask extends
593 669
	AsyncTask<String, Void, HttpResponse> {
594 670

  

Also available in: Unified diff