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