root / src / com / rackspacecloud / android / ContainerObjectDetails.java @ f58bf9f4
History | View | Annotate | Download (10 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.text.ParseException; |
6 |
import java.text.SimpleDateFormat; |
7 |
import java.util.Date; |
8 |
|
9 |
import javax.xml.parsers.FactoryConfigurationError; |
10 |
import javax.xml.parsers.ParserConfigurationException; |
11 |
import javax.xml.parsers.SAXParser; |
12 |
import javax.xml.parsers.SAXParserFactory; |
13 |
|
14 |
import org.apache.http.HttpResponse; |
15 |
import org.apache.http.client.ClientProtocolException; |
16 |
import org.apache.http.impl.client.BasicResponseHandler; |
17 |
import org.xml.sax.InputSource; |
18 |
import org.xml.sax.SAXException; |
19 |
import org.xml.sax.XMLReader; |
20 |
|
21 |
import android.app.Activity; |
22 |
import android.app.AlertDialog; |
23 |
import android.app.Dialog; |
24 |
import android.content.Context; |
25 |
import android.content.DialogInterface; |
26 |
import android.content.Intent; |
27 |
import android.net.Uri; |
28 |
import android.os.AsyncTask; |
29 |
import android.os.Bundle; |
30 |
import android.util.Log; |
31 |
import android.view.Menu; |
32 |
import android.view.MenuInflater; |
33 |
import android.view.MenuItem; |
34 |
import android.view.View; |
35 |
import android.widget.Button; |
36 |
import android.widget.TextView; |
37 |
|
38 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
39 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
40 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
41 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
42 |
|
43 |
|
44 |
/**
|
45 |
*
|
46 |
* @author Phillip Toohill
|
47 |
*
|
48 |
*/
|
49 |
|
50 |
public class ContainerObjectDetails extends Activity { |
51 |
|
52 |
private static final int deleteObject = 0; |
53 |
private ContainerObjects objects;
|
54 |
private String containerNames; |
55 |
private String cdnURL; |
56 |
private String cdnEnabled; |
57 |
public String LOG = "ViewObject"; |
58 |
private int bConver = 1048576; |
59 |
private int kbConver = 1024; |
60 |
private double megaBytes; |
61 |
private double kiloBytes; |
62 |
public Button previewButton; |
63 |
public Context context; |
64 |
|
65 |
/** Called when the activity is first created. */
|
66 |
@Override
|
67 |
public void onCreate(Bundle savedInstanceState) { |
68 |
super.onCreate(savedInstanceState);
|
69 |
|
70 |
context = getApplicationContext(); |
71 |
|
72 |
objects = (ContainerObjects) this.getIntent().getExtras().get("container"); |
73 |
containerNames = (String) this.getIntent().getExtras().get("containerNames"); |
74 |
cdnURL = (String) this.getIntent().getExtras().get("cdnUrl"); |
75 |
cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled"); |
76 |
|
77 |
|
78 |
setContentView(R.layout.viewobject); |
79 |
restoreState(savedInstanceState); |
80 |
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
protected void onSaveInstanceState(Bundle outState) { |
85 |
super.onSaveInstanceState(outState);
|
86 |
outState.putSerializable("container", objects);
|
87 |
} |
88 |
|
89 |
private void restoreState(Bundle state) { |
90 |
if (state != null && state.containsKey("container")) { |
91 |
objects = (ContainerObjects) state.getSerializable("container");
|
92 |
} |
93 |
loadObjectData(); |
94 |
|
95 |
if ( cdnEnabled.equals("true")) { |
96 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
97 |
previewButton.setOnClickListener(new MyOnClickListener());
|
98 |
} else {
|
99 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
100 |
previewButton.setVisibility(View.GONE);
|
101 |
} |
102 |
|
103 |
} |
104 |
|
105 |
|
106 |
|
107 |
private void loadObjectData() { |
108 |
//Object Name
|
109 |
TextView name = (TextView) findViewById(R.id.view_container_name); |
110 |
name.setText(objects.getCName().toString()); |
111 |
|
112 |
//File size
|
113 |
if (objects.getBytes() >= bConver) {
|
114 |
megaBytes = Math.abs(objects.getBytes()/bConver + 0.2); |
115 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
116 |
sublabel.setText(megaBytes + " MB");
|
117 |
} else if (objects.getBytes() >= kbConver){ |
118 |
kiloBytes = Math.abs(objects.getBytes()/kbConver + 0.2); |
119 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
120 |
sublabel.setText(kiloBytes + " KB");
|
121 |
} else {
|
122 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
123 |
sublabel.setText(objects.getBytes() + " B");
|
124 |
} |
125 |
|
126 |
//Content Type
|
127 |
TextView cType = (TextView) findViewById(R.id.view_content_type); |
128 |
cType.setText(objects.getContentType().toString()); |
129 |
|
130 |
//Last Modification date
|
131 |
String strDate = objects.getLastMod();
|
132 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.ssssss"); |
133 |
Date dateStr = null; |
134 |
try {
|
135 |
dateStr = formatter.parse(strDate); |
136 |
} catch (ParseException e1) { |
137 |
e1.printStackTrace(); |
138 |
} |
139 |
String formattedDate = formatter.format(dateStr);
|
140 |
Date date1 = null; |
141 |
try {
|
142 |
date1 = formatter.parse(formattedDate); |
143 |
} catch (ParseException e) { |
144 |
e.printStackTrace(); |
145 |
} |
146 |
formatter = new SimpleDateFormat("MMM-dd-yyyy"); |
147 |
formattedDate = formatter.format(date1); |
148 |
TextView lastmod = (TextView) findViewById(R.id.view_file_modification); |
149 |
lastmod.setText(formattedDate); |
150 |
|
151 |
} |
152 |
|
153 |
private class MyOnClickListener implements View.OnClickListener { |
154 |
//@Override********************************************************************remove comment
|
155 |
public void onClick(View v) { |
156 |
Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(cdnURL + "/" + objects.getCName())); |
157 |
startActivity(viewIntent); |
158 |
|
159 |
} |
160 |
} |
161 |
|
162 |
private void showAlert(String title, String message) { |
163 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
164 |
alert.setTitle(title); |
165 |
alert.setMessage(message); |
166 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
167 |
public void onClick(DialogInterface dialog, int which) { |
168 |
return;
|
169 |
} }); |
170 |
alert.show(); |
171 |
} |
172 |
//Create the Menu options
|
173 |
@Override
|
174 |
public boolean onCreateOptionsMenu(Menu menu) { |
175 |
super.onCreateOptionsMenu(menu);
|
176 |
MenuInflater inflater = getMenuInflater(); |
177 |
inflater.inflate(R.menu.container_object_list_menu, menu); |
178 |
return true; |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public boolean onOptionsItemSelected(MenuItem item) { |
183 |
switch (item.getItemId()) {
|
184 |
case R.id.delete_object:
|
185 |
showDialog(deleteObject); |
186 |
return true; |
187 |
case R.id.refresh:
|
188 |
loadObjectData(); |
189 |
return true; |
190 |
} |
191 |
return false; |
192 |
} |
193 |
|
194 |
@Override
|
195 |
protected Dialog onCreateDialog(int id ) { |
196 |
switch (id) {
|
197 |
case deleteObject:
|
198 |
return new AlertDialog.Builder(ContainerObjectDetails.this) |
199 |
.setIcon(R.drawable.alert_dialog_icon) |
200 |
.setTitle("Delete File")
|
201 |
.setMessage("Are you sure you want to delete this file?")
|
202 |
.setPositiveButton("Delete File", new DialogInterface.OnClickListener() { |
203 |
public void onClick(DialogInterface dialog, int whichButton) { |
204 |
// User clicked OK so do some stuff
|
205 |
new ContainerObjectDeleteTask().execute((Void[]) null); |
206 |
} |
207 |
}) |
208 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
209 |
public void onClick(DialogInterface dialog, int whichButton) { |
210 |
// User clicked Cancel so do some stuff
|
211 |
} |
212 |
}) |
213 |
.create(); |
214 |
} |
215 |
return null; |
216 |
} |
217 |
/**
|
218 |
* @return the file
|
219 |
*/
|
220 |
public ContainerObjects getViewFile() {
|
221 |
return objects;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* @param File the file to set
|
226 |
*/
|
227 |
public void setViewFile(ContainerObjects object) { |
228 |
this.objects = object;
|
229 |
} |
230 |
|
231 |
//Task's
|
232 |
|
233 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
234 |
CloudServersException cse = new CloudServersException();
|
235 |
try {
|
236 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
237 |
String body = responseHandler.handleResponse(response);
|
238 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
239 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
240 |
XMLReader xmlReader = saxParser.getXMLReader(); |
241 |
xmlReader.setContentHandler(parser); |
242 |
xmlReader.parse(new InputSource(new StringReader(body))); |
243 |
cse = parser.getException(); |
244 |
} catch (ClientProtocolException e) {
|
245 |
cse = new CloudServersException();
|
246 |
cse.setMessage(e.getLocalizedMessage()); |
247 |
} catch (IOException e) { |
248 |
cse = new CloudServersException();
|
249 |
cse.setMessage(e.getLocalizedMessage()); |
250 |
} catch (ParserConfigurationException e) { |
251 |
cse = new CloudServersException();
|
252 |
cse.setMessage(e.getLocalizedMessage()); |
253 |
} catch (SAXException e) {
|
254 |
cse = new CloudServersException();
|
255 |
cse.setMessage(e.getLocalizedMessage()); |
256 |
} catch (FactoryConfigurationError e) { |
257 |
cse = new CloudServersException();
|
258 |
cse.setMessage(e.getLocalizedMessage()); |
259 |
} |
260 |
return cse;
|
261 |
} |
262 |
|
263 |
private class ContainerObjectDeleteTask extends AsyncTask<Void, Void, HttpResponse> { |
264 |
|
265 |
private CloudServersException exception;
|
266 |
|
267 |
@Override
|
268 |
protected HttpResponse doInBackground(Void... arg0) { |
269 |
HttpResponse resp = null;
|
270 |
try {
|
271 |
resp = (new ContainerObjectManager(context)).deleteObject(containerNames, objects.getCName() );
|
272 |
Log.v(LOG, "container name " + objects.getCName() + " " + containerNames); |
273 |
} catch (CloudServersException e) {
|
274 |
exception = e; |
275 |
} |
276 |
return resp;
|
277 |
} |
278 |
|
279 |
@Override
|
280 |
protected void onPostExecute(HttpResponse response) { |
281 |
if (response != null) { |
282 |
int statusCode = response.getStatusLine().getStatusCode();
|
283 |
if (statusCode == 204) { |
284 |
setResult(Activity.RESULT_OK); |
285 |
finish(); |
286 |
|
287 |
} else {
|
288 |
CloudServersException cse = parseCloudServersException(response); |
289 |
if ("".equals(cse.getMessage())) { |
290 |
showAlert("Error", "There was a problem deleting your File."); |
291 |
} else {
|
292 |
showAlert("Error", "There was a problem deleting your file: " + cse.getMessage()); |
293 |
} |
294 |
} |
295 |
} else if (exception != null) { |
296 |
showAlert("Error", "There was a problem deleting your file: " + exception.getMessage()); |
297 |
} |
298 |
} |
299 |
} |
300 |
|
301 |
} |