Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ContainerObjectDetails.java @ b6fa6007

History | View | Annotate | Download (9.1 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.content.Intent;
23
import android.net.Uri;
24
import android.os.AsyncTask;
25
import android.os.Bundle;
26
import android.util.Log;
27
import android.view.Menu;
28
import android.view.MenuInflater;
29
import android.view.MenuItem;
30
import android.view.View;
31
import android.widget.Button;
32
import android.widget.TextView;
33

    
34
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
35
import com.rackspace.cloud.files.api.client.ContainerObjects;
36
import com.rackspace.cloud.servers.api.client.CloudServersException;
37
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
38

    
39

    
40
/** 
41
 * 
42
 * @author Phillip Toohill
43
 *
44
 */
45

    
46
public class ContainerObjectDetails extends Activity {
47
        
48
        private static final int deleteObject = 0;
49
        private ContainerObjects objects;
50
        private String containerNames;
51
        private String cdnURL;
52
        private String cdnEnabled;
53
        public String LOG = "ViewObject";
54
        private int bConver = 1048576;
55
        private int kbConver = 1024;
56
        private double megaBytes;
57
        private double kiloBytes;
58
        public Button previewButton;
59
        
60
    /** Called when the activity is first created. */
61
    @Override
62
    public void onCreate(Bundle savedInstanceState) {
63
        super.onCreate(savedInstanceState);
64
        
65
        objects = (ContainerObjects) this.getIntent().getExtras().get("container");
66
        containerNames =  (String) this.getIntent().getExtras().get("containerNames");
67
        cdnURL = (String) this.getIntent().getExtras().get("cdnUrl");
68
        cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled");
69
        
70

    
71
        setContentView(R.layout.viewobject);       
72
        restoreState(savedInstanceState);
73
        
74
    }
75
    
76
        @Override
77
        protected void onSaveInstanceState(Bundle outState) {
78
                super.onSaveInstanceState(outState);
79
                outState.putSerializable("container", objects);
80
        }
81

    
82
    private void restoreState(Bundle state) {
83
            if (state != null && state.containsKey("container")) {
84
                    objects = (ContainerObjects) state.getSerializable("container");
85
            }
86
        loadObjectData();
87
        
88
        if ( cdnEnabled.equals("true"))  {
89
                this.previewButton = (Button) findViewById(R.id.preview_button);
90
                previewButton.setOnClickListener(new MyOnClickListener());
91
        } else {
92
                this.previewButton = (Button) findViewById(R.id.preview_button);
93
                previewButton.setVisibility(View.GONE);
94
      }
95
        
96
    }
97
   
98
    
99

    
100
    private void loadObjectData() {
101
            TextView name = (TextView) findViewById(R.id.view_container_name);
102
            name.setText(objects.getCName().toString());
103
                        
104
            if (objects.getBytes() >= bConver) {
105
                        megaBytes = Math.abs(objects.getBytes()/bConver + 0.2);
106
                                TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
107
                                sublabel.setText(megaBytes + " MB");
108
                } else if (objects.getBytes() >= kbConver){
109
                        kiloBytes = Math.abs(objects.getBytes()/kbConver + 0.2);
110
                                TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
111
                                sublabel.setText(kiloBytes + " KB");
112
                } else {
113
                                TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
114
                                sublabel.setText(objects.getBytes() + " B");
115
                }                
116
            
117
            TextView cType = (TextView) findViewById(R.id.view_content_type);
118
            cType.setText(objects.getContentType().toString());
119
                        
120
            TextView lastmod = (TextView) findViewById(R.id.view_file_modification);
121
            lastmod.setText(objects.getLastMod().toString());
122
              
123
                        
124
    }
125
    
126
    private class MyOnClickListener implements View.OnClickListener {
127
        @Override
128
        public void onClick(View v) {
129
                Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(cdnURL + "/" + objects.getCName()));
130
                startActivity(viewIntent);  
131

    
132
        }
133
    }
134
    
135
    private void showAlert(String title, String message) {
136
                AlertDialog alert = new AlertDialog.Builder(this).create();
137
                alert.setTitle(title);
138
                alert.setMessage(message);
139
                alert.setButton("OK", new DialogInterface.OnClickListener() {
140
              public void onClick(DialogInterface dialog, int which) {
141
                return;
142
            } }); 
143
                alert.show();
144
    }
145
  //Create the Menu options
146
    @Override 
147
    public boolean onCreateOptionsMenu(Menu menu) {
148
                super.onCreateOptionsMenu(menu);
149
                MenuInflater inflater = getMenuInflater();
150
                inflater.inflate(R.menu.container_object_list_menu, menu);
151
                return true;
152
        } 
153
    
154
    @Override 
155
    public boolean onOptionsItemSelected(MenuItem item) {
156
                switch (item.getItemId()) {
157
                case R.id.delete_object:
158
                        showDialog(deleteObject); 
159
                        return true;
160
                case R.id.refresh:
161
                        loadObjectData();
162
                return true;
163
                }
164
                return false;
165
        } 
166
    
167
    @Override
168
    protected Dialog onCreateDialog(int id ) {
169
            switch (id) {
170
                        case deleteObject:
171
            return new AlertDialog.Builder(ContainerObjectDetails.this)
172
                .setIcon(R.drawable.alert_dialog_icon)
173
                .setTitle("Delete File")
174
                .setMessage("Are you sure you want to delete this file?")
175
                .setPositiveButton("Delete File", new DialogInterface.OnClickListener() {
176
                        public void onClick(DialogInterface dialog, int whichButton) {
177
                                // User clicked OK so do some stuff
178
                                new ContainerObjectDeleteTask().execute((Void[]) null);
179
                        }
180
                })
181
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
182
                        public void onClick(DialogInterface dialog, int whichButton) {
183
                                // User clicked Cancel so do some stuff
184
                        }
185
                })
186
                .create();
187
        }
188
        return null;
189
    }
190
    /**
191
         * @return the file
192
         */
193
        public ContainerObjects getViewFile() {
194
                return objects;
195
        }
196

    
197
        /**
198
         * @param File the file to set
199
         */
200
        public void setViewFile(ContainerObjects object) {
201
                this.objects = object;
202
        }
203
        
204
        //Task's
205
                    
206
                     private CloudServersException parseCloudServersException(HttpResponse response) {
207
                                    CloudServersException cse = new CloudServersException();
208
                                    try {
209
                                        BasicResponseHandler responseHandler = new BasicResponseHandler();
210
                                        String body = responseHandler.handleResponse(response);
211
                                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
212
                                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
213
                                        XMLReader xmlReader = saxParser.getXMLReader();
214
                                        xmlReader.setContentHandler(parser);
215
                                        xmlReader.parse(new InputSource(new StringReader(body)));                            
216
                                        cse = parser.getException();                            
217
                                    } catch (ClientProtocolException e) {
218
                                            cse = new CloudServersException();
219
                                            cse.setMessage(e.getLocalizedMessage());
220
                                    } catch (IOException e) {
221
                                            cse = new CloudServersException();
222
                                            cse.setMessage(e.getLocalizedMessage());
223
                                    } catch (ParserConfigurationException e) {
224
                                            cse = new CloudServersException();
225
                                            cse.setMessage(e.getLocalizedMessage());
226
                                    } catch (SAXException e) {
227
                                            cse = new CloudServersException();
228
                                            cse.setMessage(e.getLocalizedMessage());
229
                                    } catch (FactoryConfigurationError e) {
230
                                            cse = new CloudServersException();
231
                                            cse.setMessage(e.getLocalizedMessage());
232
                                    }
233
                                    return cse;
234
                        }
235
                     
236
                     private class ContainerObjectDeleteTask extends AsyncTask<Void, Void, HttpResponse> {
237
                                
238
                                    private CloudServersException exception;
239

    
240
                                    @Override
241
                                    protected HttpResponse doInBackground(Void... arg0) {
242
                                            HttpResponse resp = null;
243
                                            try {
244
                                                    resp = (new ContainerObjectManager()).deleteObject(containerNames, objects.getCName() );
245
                                                    Log.v(LOG, "container name " + objects.getCName() + " " + containerNames);
246
                                            } catch (CloudServersException e) {
247
                                                    exception = e;
248
                                            }
249
                                            return resp;
250
                                    }
251
                                
252
                                    @Override
253
                                    protected void onPostExecute(HttpResponse response) {
254
                                            if (response != null) {
255
                                                    int statusCode = response.getStatusLine().getStatusCode();
256
                                                    if (statusCode == 204) {
257
                                                            setResult(Activity.RESULT_OK);
258
                                                            finish();
259
                                                            
260
                                                    } else {
261
                                                            CloudServersException cse = parseCloudServersException(response);
262
                                                            if ("".equals(cse.getMessage())) {
263
                                                                    showAlert("Error", "There was a problem deleting your File.");
264
                                                            } else {
265
                                                                    showAlert("Error", "There was a problem deleting your file: " + cse.getMessage());
266
                                                            }
267
                                                    }
268
                                            } else if (exception != null) {
269
                                                    showAlert("Error", "There was a problem deleting your file: " + exception.getMessage());                                
270
                                            }                        
271
                                    }
272
                        }
273
        
274
  }