Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ContainerObjectDetails.java @ 6864568a

History | View | Annotate | Download (8.5 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5

    
6
import javax.xml.parsers.FactoryConfigurationError;
7
import javax.xml.parsers.ParserConfigurationException;
8
import javax.xml.parsers.SAXParser;
9
import javax.xml.parsers.SAXParserFactory;
10

    
11
import org.apache.http.HttpResponse;
12
import org.apache.http.client.ClientProtocolException;
13
import org.apache.http.impl.client.BasicResponseHandler;
14
import org.xml.sax.InputSource;
15
import org.xml.sax.SAXException;
16
import org.xml.sax.XMLReader;
17

    
18
import android.app.Activity;
19
import android.app.AlertDialog;
20
import android.app.Dialog;
21
import android.content.DialogInterface;
22
import android.os.AsyncTask;
23
import android.os.Bundle;
24
import android.util.Log;
25
import android.view.Menu;
26
import android.view.MenuInflater;
27
import android.view.MenuItem;
28
import android.widget.TextView;
29

    
30
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
31
import com.rackspace.cloud.files.api.client.ContainerObjects;
32
import com.rackspace.cloud.servers.api.client.CloudServersException;
33
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
34

    
35

    
36
/** 
37
 * 
38
 * @author Phillip Toohill
39
 *
40
 */
41

    
42
public class ContainerObjectDetails extends Activity {
43
        
44
        private static final int deleteObject = 0;
45
        private ContainerObjects objects;
46
        private String containerNames;
47
        private String cdnURL;
48
        private String cdnEnabled;
49
        public String LOG = "ViewObject";
50
        private int bConver = 1048576;
51
        private int kbConver = 1024;
52
        private double megaBytes;
53
        private double kiloBytes;
54
        
55
    /** Called when the activity is first created. */
56
    @Override
57
    public void onCreate(Bundle savedInstanceState) {
58
        super.onCreate(savedInstanceState);
59
        
60
        objects = (ContainerObjects) this.getIntent().getExtras().get("container");
61
        containerNames =  (String) this.getIntent().getExtras().get("containerNames");
62
        cdnURL = (String) this.getIntent().getExtras().get("cdnUrl");
63
        cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled");
64
        
65
        setContentView(R.layout.viewobject);
66
        restoreState(savedInstanceState);
67
        
68
    }
69
    
70
        @Override
71
        protected void onSaveInstanceState(Bundle outState) {
72
                super.onSaveInstanceState(outState);
73
                outState.putSerializable("container", objects);
74
        }
75

    
76
    private void restoreState(Bundle state) {
77
            if (state != null && state.containsKey("container")) {
78
                    objects = (ContainerObjects) state.getSerializable("container");
79
            }
80
        loadObjectData();       
81
    }
82
   
83
     
84
    private void loadObjectData() {
85
            TextView name = (TextView) findViewById(R.id.view_container_name);
86
            name.setText(objects.getCName().toString());
87
                        
88
            if (objects.getBytes() >= bConver) {
89
                        megaBytes = Math.abs(objects.getBytes()/bConver + 0.2);
90
                                TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
91
                                sublabel.setText(megaBytes + " MB");
92
                } else if (objects.getBytes() >= kbConver){
93
                        kiloBytes = Math.abs(objects.getBytes()/kbConver + 0.2);
94
                                TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
95
                                sublabel.setText(kiloBytes + " KB");
96
                } else {
97
                                TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
98
                                sublabel.setText(objects.getBytes() + " B");
99
                }                
100
            
101
            TextView cType = (TextView) findViewById(R.id.view_content_type);
102
            cType.setText(objects.getContentType().toString());
103
                        
104
            TextView lastmod = (TextView) findViewById(R.id.view_file_modification);
105
            lastmod.setText(objects.getLastMod().toString());
106
            
107
            if ( cdnEnabled.equals("true"))  {
108
            TextView cdnUrl = (TextView) findViewById(R.id.view_file_cdnurl);
109
            cdnUrl.setText( cdnURL + "/" + objects.getCName());
110
            }
111
            
112
    }
113
    private void showAlert(String title, String message) {
114
                AlertDialog alert = new AlertDialog.Builder(this).create();
115
                alert.setTitle(title);
116
                alert.setMessage(message);
117
                alert.setButton("OK", new DialogInterface.OnClickListener() {
118
              public void onClick(DialogInterface dialog, int which) {
119
                return;
120
            } }); 
121
                alert.show();
122
    }
123
  //Create the Menu options
124
    @Override 
125
    public boolean onCreateOptionsMenu(Menu menu) {
126
                super.onCreateOptionsMenu(menu);
127
                MenuInflater inflater = getMenuInflater();
128
                inflater.inflate(R.menu.container_object_list_menu, menu);
129
                return true;
130
        } 
131
    
132
    @Override 
133
    public boolean onOptionsItemSelected(MenuItem item) {
134
                switch (item.getItemId()) {
135
                case R.id.delete_object:
136
                        showDialog(deleteObject); 
137
                        return true;
138
                case R.id.refresh:
139
                        loadObjectData();
140
                return true;
141
                }
142
                return false;
143
        } 
144
    
145
    @Override
146
    protected Dialog onCreateDialog(int id ) {
147
            switch (id) {
148
                        case deleteObject:
149
            return new AlertDialog.Builder(ContainerObjectDetails.this)
150
                .setIcon(R.drawable.alert_dialog_icon)
151
                .setTitle("Delete File")
152
                .setMessage("Are you sure you want to delete this file?")
153
                .setPositiveButton("Delete File", new DialogInterface.OnClickListener() {
154
                        public void onClick(DialogInterface dialog, int whichButton) {
155
                                // User clicked OK so do some stuff
156
                                new ContainerObjectDeleteTask().execute((Void[]) null);
157
                        }
158
                })
159
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
160
                        public void onClick(DialogInterface dialog, int whichButton) {
161
                                // User clicked Cancel so do some stuff
162
                        }
163
                })
164
                .create();
165
        }
166
        return null;
167
    }
168
    /**
169
         * @return the file
170
         */
171
        public ContainerObjects getViewFile() {
172
                return objects;
173
        }
174

    
175
        /**
176
         * @param File the file to set
177
         */
178
        public void setViewFile(ContainerObjects object) {
179
                this.objects = object;
180
        }
181
        
182
        //Task's
183
                    
184
                     private CloudServersException parseCloudServersException(HttpResponse response) {
185
                                    CloudServersException cse = new CloudServersException();
186
                                    try {
187
                                        BasicResponseHandler responseHandler = new BasicResponseHandler();
188
                                        String body = responseHandler.handleResponse(response);
189
                                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
190
                                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
191
                                        XMLReader xmlReader = saxParser.getXMLReader();
192
                                        xmlReader.setContentHandler(parser);
193
                                        xmlReader.parse(new InputSource(new StringReader(body)));                            
194
                                        cse = parser.getException();                            
195
                                    } catch (ClientProtocolException e) {
196
                                            cse = new CloudServersException();
197
                                            cse.setMessage(e.getLocalizedMessage());
198
                                    } catch (IOException e) {
199
                                            cse = new CloudServersException();
200
                                            cse.setMessage(e.getLocalizedMessage());
201
                                    } catch (ParserConfigurationException e) {
202
                                            cse = new CloudServersException();
203
                                            cse.setMessage(e.getLocalizedMessage());
204
                                    } catch (SAXException e) {
205
                                            cse = new CloudServersException();
206
                                            cse.setMessage(e.getLocalizedMessage());
207
                                    } catch (FactoryConfigurationError e) {
208
                                            cse = new CloudServersException();
209
                                            cse.setMessage(e.getLocalizedMessage());
210
                                    }
211
                                    return cse;
212
                        }
213
                     
214
                     private class ContainerObjectDeleteTask extends AsyncTask<Void, Void, HttpResponse> {
215
                                
216
                                    private CloudServersException exception;
217

    
218
                                    @Override
219
                                    protected HttpResponse doInBackground(Void... arg0) {
220
                                            HttpResponse resp = null;
221
                                            try {
222
                                                    resp = (new ContainerObjectManager()).deleteObject(containerNames, objects.getCName() );
223
                                                    Log.v(LOG, "container name " + objects.getCName() + " " + containerNames);
224
                                            } catch (CloudServersException e) {
225
                                                    exception = e;
226
                                            }
227
                                            return resp;
228
                                    }
229
                                
230
                                    @Override
231
                                    protected void onPostExecute(HttpResponse response) {
232
                                            if (response != null) {
233
                                                    int statusCode = response.getStatusLine().getStatusCode();
234
                                                    if (statusCode == 204) {
235
                                                            setResult(Activity.RESULT_OK);
236
                                                            finish();
237
                                                            
238
                                                    } else {
239
                                                            CloudServersException cse = parseCloudServersException(response);
240
                                                            if ("".equals(cse.getMessage())) {
241
                                                                    showAlert("Error", "There was a problem deleting your File.");
242
                                                            } else {
243
                                                                    showAlert("Error", "There was a problem deleting your file: " + cse.getMessage());
244
                                                            }
245
                                                    }
246
                                            } else if (exception != null) {
247
                                                    showAlert("Error", "There was a problem deleting your file: " + exception.getMessage());                                
248
                                            }                        
249
                                    }
250
                        }
251
        
252
  }