Revision 51938302 src/com/rackspacecloud/android/ContainerObjectDetails.java

b/src/com/rackspacecloud/android/ContainerObjectDetails.java
1 1
package com.rackspacecloud.android;
2 2

  
3
import java.io.BufferedOutputStream;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileOutputStream;
3 7
import java.io.IOException;
4 8
import java.io.StringReader;
5 9
import java.text.ParseException;
......
11 15
import javax.xml.parsers.SAXParser;
12 16
import javax.xml.parsers.SAXParserFactory;
13 17

  
18
import org.apache.http.HttpEntity;
14 19
import org.apache.http.HttpResponse;
15 20
import org.apache.http.client.ClientProtocolException;
16 21
import org.apache.http.impl.client.BasicResponseHandler;
22
import org.apache.http.util.EntityUtils;
17 23
import org.xml.sax.InputSource;
18 24
import org.xml.sax.SAXException;
19 25
import org.xml.sax.XMLReader;
......
28 34
import android.net.Uri;
29 35
import android.os.AsyncTask;
30 36
import android.os.Bundle;
37
import android.os.Environment;
31 38
import android.util.Log;
32 39
import android.view.Menu;
33 40
import android.view.MenuInflater;
......
51 58
public class ContainerObjectDetails extends Activity {
52 59
	
53 60
	private static final int deleteObject = 0;
61
	private final String DOWNLOAD_DIRECTORY = "/RackspaceCloud";
54 62
	private ContainerObjects objects;
55 63
	private String containerNames;
56 64
	private String cdnURL;
......
61 69
	private double megaBytes;
62 70
	private double kiloBytes;
63 71
	public Button previewButton;
72
	public Button downloadButton;
64 73
	public Context context;
65
	ProgressDialog dialog;
74
	private ProgressDialog dialog;
75
	private Boolean isDownloaded;
66 76
	
67 77
    /** Called when the activity is first created. */
68 78
    @Override
......
86 96
	protected void onSaveInstanceState(Bundle outState) {
87 97
		super.onSaveInstanceState(outState);
88 98
		outState.putSerializable("container", objects);
99
		outState.putBoolean("isDownloaded", isDownloaded);
89 100
	}
90 101

  
91 102
    private void restoreState(Bundle state) {
92 103
    	if (state != null && state.containsKey("container")) {
93 104
    		objects = (ContainerObjects) state.getSerializable("container");
94 105
    	}
95
        loadObjectData();
96
        
97
        if ( cdnEnabled.equals("true"))  {
98
        	this.previewButton = (Button) findViewById(R.id.preview_button);
99
        	previewButton.setOnClickListener(new MyOnClickListener());
100
        } else {
101
        	this.previewButton = (Button) findViewById(R.id.preview_button);
102
        	previewButton.setVisibility(View.GONE);
103
      }
104
        
106
    	loadObjectData();
107

  
108
    	if ( cdnEnabled.equals("true"))  {
109
    		this.previewButton = (Button) findViewById(R.id.preview_button);
110
    		previewButton.setOnClickListener(new MyOnClickListener());
111
    	} else {
112
    		this.previewButton = (Button) findViewById(R.id.preview_button);
113
    		previewButton.setVisibility(View.GONE);
114
    	}
115

  
116
    	if (state != null && state.containsKey("isDownloaded")){
117
    		isDownloaded = state.getBoolean("isDownloaded");
118
    	}
119
    	else{
120
    		isDownloaded = fileIsDownloaded();
121
    	}
122
    	this.downloadButton = (Button) findViewById(R.id.download_button);
123
    	if ( isDownloaded )  {
124
    		downloadButton.setText("Open File");
125
    	} else {
126
    		downloadButton.setText("Download File");
127
    	}   	
128
    	downloadButton.setOnClickListener(new MyOnClickListener());
105 129
    }
106
   
107
    
130

  
108 131

  
109 132
    private void loadObjectData() {
110 133
    	//Object Name
......
155 178
    private class MyOnClickListener implements View.OnClickListener {
156 179
        //@Override********************************************************************remove comment
157 180
        public void onClick(View v) {
158
        	Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(cdnURL + "/" + objects.getCName()));
159
        	startActivity(viewIntent);  
160

  
181
        	if(v.equals(findViewById(R.id.preview_button))){
182
        		Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(cdnURL + "/" + objects.getCName()));
183
        		startActivity(viewIntent);  
184
        	}
185
        	/*
186
        	 * need to perform different functions based on if
187
        	 * the file has in the devices filesystem
188
        	 */
189
        	if(v.equals(findViewById(R.id.download_button))){
190
        		if(!isDownloaded){
191
        			if(storageIsReady()){
192
        				new ContainerObjectDownloadTask().execute();
193
        			}
194
        		}
195
        		else{
196
        			openFile();
197
        		}
198
        	}
161 199
        }
162 200
    }
163 201
    
......
230 268
		this.objects = object;
231 269
	}
232 270
	
271
	/*
272
	 * returns false if external storage is not avaliable
273
	 * (if its mounted, missing, read-only, etc)
274
	 * from: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
275
	 */
276
	private boolean storageIsReady(){
277
		boolean mExternalStorageAvailable = false;
278
		boolean mExternalStorageWriteable = false;
279
		String state = Environment.getExternalStorageState();
280

  
281
		if (Environment.MEDIA_MOUNTED.equals(state)) {
282
		    // We can read and write the media
283
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
284
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
285
		    // We can only read the media
286
		    mExternalStorageAvailable = true;
287
		    mExternalStorageWriteable = false;
288
		} else {
289
		    // Something else is wrong. It may be one of many other states, but all we need
290
		    //  to know is we can neither read nor write
291
		    mExternalStorageAvailable = mExternalStorageWriteable = false;
292
		}
293
		return mExternalStorageAvailable && mExternalStorageWriteable;
294
	}
295
	
296
	private boolean fileIsDownloaded(){
297
		if(storageIsReady()){
298
			String fileName = Environment.getExternalStorageDirectory().getPath() + "/RackspaceCloud/" + objects.getCName();
299
			File f = new File(fileName);
300
			return f.isFile();
301
		}
302
		return false;
303
	}
304
	
305
	private void openFile(){
306
		File object = new File(Environment.getExternalStorageDirectory().getPath() + "/RackspaceCloud/" + objects.getCName());
307
		Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
308
	    File file = new File(object.getAbsolutePath()); 
309
	    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
310
	    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
311
	    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
312
	    startActivity(myIntent);
313
	}
314
	
315
	private boolean writeFile(byte[] data){
316
		String directoryName = Environment.getExternalStorageDirectory().getPath() + DOWNLOAD_DIRECTORY;
317
		File f = new File(directoryName);
318
		
319
		if(!f.isDirectory()){
320
			if(!f.mkdir()){
321
				return false;
322
			}
323
		}
324
		
325
		String filename = directoryName + "/" + objects.getCName();
326
		File object = new File(filename);
327
		BufferedOutputStream bos = null;
328
		
329
		try{
330
			FileOutputStream fos = new FileOutputStream(object);
331
			bos = new BufferedOutputStream(fos);
332
			bos.write(data);
333
		}
334
		catch(FileNotFoundException e){
335
			e.printStackTrace();
336
		}
337
		catch(IOException e){
338
			e.printStackTrace();
339
		}
340
		finally{
341
			if(bos != null){
342
				try {
343
					bos.flush();
344
					bos.close();
345
				} catch (IOException e) {
346
					// TODO Auto-generated catch block
347
					e.printStackTrace();
348
				}
349
			}
350
		}
351
		return true;
352
	}
353
	
233 354
	//Task's
234 355
	    	
235 356
	    	 private CloudServersException parseCloudServersException(HttpResponse response) {
......
304 425
	    				}			
305 426
	    			}
306 427
	    	    }
428
	    	 
429
	    	 private class ContainerObjectDownloadTask extends AsyncTask<Void, Void, HttpResponse> {
430
	    	    	
431
	    			private CloudServersException exception;
432
	    			
433
	    			protected void onPreExecute(){
434
	    				dialog = ProgressDialog.show(ContainerObjectDetails.this, "", "Downloading...", true);
435
	    			}
436

  
437
	    			@Override
438
	    			protected HttpResponse doInBackground(Void... arg0) {
439
	    				HttpResponse resp = null;	
440
	    				try {
441
	    					resp = (new ContainerObjectManager(context)).getObject(containerNames, objects.getCName());
442
	    				} catch (CloudServersException e) {
443
	    					exception = e;
444
	    				}
445
	    				return resp;
446
	    			}
447
	    	    	
448
	    			@Override
449
	    			protected void onPostExecute(HttpResponse response) {
450
	    				dialog.dismiss();
451
	    				if (response != null) {
452
	    					int statusCode = response.getStatusLine().getStatusCode();
453
	    					if (statusCode == 200) {
454
	    						setResult(Activity.RESULT_OK);
455
	    				    	HttpEntity entity = response.getEntity();
456
								
457
								try {
458
									if(writeFile(EntityUtils.toByteArray(entity))){
459
										downloadButton.setText("Open File");
460
										isDownloaded = true;
461
									}
462
									else{
463
										showAlert("Error", "There was a problem downloading your file.");
464
									}
465
									
466
								} catch (IOException e) {
467
									// TODO Auto-generated catch block
468
									e.printStackTrace();
469
								}
470
								
471
	    					} else {
472
	    						CloudServersException cse = parseCloudServersException(response);
473
	    						if ("".equals(cse.getMessage())) {
474
	    							showAlert("Error", "There was a problem downloading your File.");
475
	    						} else {
476
	    							showAlert("Error", "There was a problem downloading your file: " + cse.getMessage());
477
	    						}
478
	    					}
479
	    				} else if (exception != null) {
480
	    					showAlert("Error", "There was a problem downloading your file: " + exception.getMessage());				
481
	    				}			
482
	    			}
483
	    	    }
307 484
	
308 485
  }

Also available in: Unified diff