Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ContainerObjectDetails.java @ b2436f98

History | View | Annotate | Download (27.6 kB)

1 7dbfc514 koutsoub
package com.rackspace.cloud.android;
2 3d6041e8 Phillip Toohill
3 51938302 Adam Menz
import java.io.BufferedOutputStream;
4 51938302 Adam Menz
import java.io.File;
5 51938302 Adam Menz
import java.io.FileOutputStream;
6 3d6041e8 Phillip Toohill
import java.io.IOException;
7 9c4430bd koutsoub
import java.io.InputStream;
8 9c4430bd koutsoub
import java.io.OutputStream;
9 d37efce5 koutsoub
import java.util.ArrayList;
10 bd384c08 koutsoub
import java.util.HashMap;
11 c99967ba koutsoub
import java.util.Iterator;
12 d37efce5 koutsoub
import java.util.List;
13 bd384c08 koutsoub
import java.util.Map;
14 c99967ba koutsoub
import java.util.Map.Entry;
15 9c4430bd koutsoub
import java.util.StringTokenizer;
16 3d6041e8 Phillip Toohill
17 51938302 Adam Menz
import org.apache.http.HttpEntity;
18 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
19 3d6041e8 Phillip Toohill
20 3d6041e8 Phillip Toohill
import android.app.Activity;
21 3d6041e8 Phillip Toohill
import android.app.AlertDialog;
22 3d6041e8 Phillip Toohill
import android.app.Dialog;
23 3d6041e8 Phillip Toohill
import android.content.DialogInterface;
24 82cd98ea unknown
import android.content.Intent;
25 53026239 koutsoub
import android.graphics.Color;
26 82cd98ea unknown
import android.net.Uri;
27 3d6041e8 Phillip Toohill
import android.os.AsyncTask;
28 3d6041e8 Phillip Toohill
import android.os.Bundle;
29 51938302 Adam Menz
import android.os.Environment;
30 c99967ba koutsoub
import android.util.Log;
31 c99967ba koutsoub
import android.view.LayoutInflater;
32 3d6041e8 Phillip Toohill
import android.view.Menu;
33 3d6041e8 Phillip Toohill
import android.view.MenuInflater;
34 3d6041e8 Phillip Toohill
import android.view.MenuItem;
35 82cd98ea unknown
import android.view.View;
36 732d9c2c koutsoub
import android.view.View.OnClickListener;
37 82cd98ea unknown
import android.widget.Button;
38 732d9c2c koutsoub
import android.widget.CheckBox;
39 732d9c2c koutsoub
import android.widget.CompoundButton;
40 732d9c2c koutsoub
import android.widget.CompoundButton.OnCheckedChangeListener;
41 bd384c08 koutsoub
import android.widget.EditText;
42 b2436f98 koutsoub
import android.widget.ImageButton;
43 c99967ba koutsoub
import android.widget.LinearLayout;
44 732d9c2c koutsoub
import android.widget.TabHost;
45 3d6041e8 Phillip Toohill
import android.widget.TextView;
46 697895bc Adam Menz
import android.widget.Toast;
47 3d6041e8 Phillip Toohill
48 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
49 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjects;
50 53026239 koutsoub
import com.rackspace.cloud.files.api.client.GroupResource;
51 d37efce5 koutsoub
import com.rackspace.cloud.files.api.client.ObjectVersion;
52 732d9c2c koutsoub
import com.rackspace.cloud.files.api.client.Permission;
53 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
54 32731215 Adam Menz
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
55 3d6041e8 Phillip Toohill
56 9c4430bd koutsoub
/**
57 3d6041e8 Phillip Toohill
 * 
58 3d6041e8 Phillip Toohill
 * @author Phillip Toohill
59 9c4430bd koutsoub
 * 
60 3d6041e8 Phillip Toohill
 */
61 3d6041e8 Phillip Toohill
62 b2a2d2f1 Adam Menz
public class ContainerObjectDetails extends CloudActivity {
63 9c4430bd koutsoub
        private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
64 3d6041e8 Phillip Toohill
        private static final int deleteObject = 0;
65 9c4430bd koutsoub
        private final String DOWNLOAD_DIRECTORY = "PithosPlus";
66 9c4430bd koutsoub
67 3d6041e8 Phillip Toohill
        private ContainerObjects objects;
68 3d6041e8 Phillip Toohill
        private String containerNames;
69 3d6041e8 Phillip Toohill
        private String cdnURL;
70 8b432514 Phillip Toohill
        private String cdnEnabled;
71 3d6041e8 Phillip Toohill
        public String LOG = "ViewObject";
72 6864568a Phillip Toohill
        private int bConver = 1048576;
73 6864568a Phillip Toohill
        private int kbConver = 1024;
74 6864568a Phillip Toohill
        private double megaBytes;
75 6864568a Phillip Toohill
        private double kiloBytes;
76 82cd98ea unknown
        public Button previewButton;
77 51938302 Adam Menz
        public Button downloadButton;
78 51938302 Adam Menz
        private Boolean isDownloaded;
79 3e180b04 Adam Menz
        private AndroidCloudApplication app;
80 3e180b04 Adam Menz
        private DeleteObjectListenerTask deleteObjTask;
81 3e180b04 Adam Menz
        private DownloadObjectListenerTask downloadObjTask;
82 9c4430bd koutsoub
        private List<ObjectVersion> versions = new ArrayList<ObjectVersion>();
83 9c4430bd koutsoub
84 3e180b04 Adam Menz
        /** Called when the activity is first created. */
85 3e180b04 Adam Menz
        @Override
86 3e180b04 Adam Menz
        public void onCreate(Bundle savedInstanceState) {
87 3e180b04 Adam Menz
                super.onCreate(savedInstanceState);
88 5018a7f8 Adam Menz
                trackPageView(GoogleAnalytics.PAGE_STORAGE_OBJECT);
89 3e180b04 Adam Menz
90 9c4430bd koutsoub
                objects = (ContainerObjects) this.getIntent().getExtras()
91 9c4430bd koutsoub
                                .get("container");
92 9c4430bd koutsoub
                containerNames = (String) this.getIntent().getExtras()
93 9c4430bd koutsoub
                                .get("containerNames");
94 3e180b04 Adam Menz
                cdnURL = (String) this.getIntent().getExtras().get("cdnUrl");
95 3e180b04 Adam Menz
                cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled");
96 3e180b04 Adam Menz
97 9c4430bd koutsoub
                setContentView(R.layout.viewobject);
98 9c4430bd koutsoub
                TabHost tabs = (TabHost) findViewById(R.id.tabhost2);
99 9c4430bd koutsoub
100 9c4430bd koutsoub
                tabs.setup();
101 9c4430bd koutsoub
102 9c4430bd koutsoub
                TabHost.TabSpec spec = tabs.newTabSpec("tag1");
103 9c4430bd koutsoub
104 9c4430bd koutsoub
                spec.setContent(R.id.details);
105 9c4430bd koutsoub
                spec.setIndicator("Details");
106 9c4430bd koutsoub
                tabs.addTab(spec);
107 9c4430bd koutsoub
108 9c4430bd koutsoub
                spec = tabs.newTabSpec("tag2");
109 9c4430bd koutsoub
                spec.setContent(R.id.metadata);
110 9c4430bd koutsoub
                spec.setIndicator("Metadata");
111 9c4430bd koutsoub
                tabs.addTab(spec);
112 9c4430bd koutsoub
113 9c4430bd koutsoub
                spec = tabs.newTabSpec("tag3");
114 9c4430bd koutsoub
                spec.setContent(R.id.sharing);
115 9c4430bd koutsoub
                spec.setIndicator("Sharing");
116 9c4430bd koutsoub
                tabs.addTab(spec);
117 9c4430bd koutsoub
118 9c4430bd koutsoub
                spec = tabs.newTabSpec("tag4");
119 9c4430bd koutsoub
                spec.setContent(R.id.versions);
120 9c4430bd koutsoub
                spec.setIndicator("Versions");
121 9c4430bd koutsoub
                tabs.addTab(spec);
122 9c4430bd koutsoub
123 3e180b04 Adam Menz
                restoreState(savedInstanceState);
124 3e180b04 Adam Menz
        }
125 3e180b04 Adam Menz
126 da02192c koutsoub
        public boolean isFolder(){
127 da02192c koutsoub
                return objects.getContentType().startsWith("application/folder");
128 da02192c koutsoub
        }
129 3d6041e8 Phillip Toohill
        @Override
130 3d6041e8 Phillip Toohill
        protected void onSaveInstanceState(Bundle outState) {
131 3d6041e8 Phillip Toohill
                super.onSaveInstanceState(outState);
132 3d6041e8 Phillip Toohill
                outState.putSerializable("container", objects);
133 51938302 Adam Menz
                outState.putBoolean("isDownloaded", isDownloaded);
134 3d6041e8 Phillip Toohill
        }
135 3d6041e8 Phillip Toohill
136 b2a2d2f1 Adam Menz
        protected void restoreState(Bundle state) {
137 b2a2d2f1 Adam Menz
                super.restoreState(state);
138 3e180b04 Adam Menz
                /*
139 9c4430bd koutsoub
                 * need reference to the app so you can access curDirFiles as well as
140 9c4430bd koutsoub
                 * processing status
141 3e180b04 Adam Menz
                 */
142 9c4430bd koutsoub
                app = (AndroidCloudApplication) this.getApplication();
143 51938302 Adam Menz
144 3e180b04 Adam Menz
                if (state != null && state.containsKey("container")) {
145 3e180b04 Adam Menz
                        objects = (ContainerObjects) state.getSerializable("container");
146 3e180b04 Adam Menz
                }
147 3e180b04 Adam Menz
                loadObjectData();
148 51938302 Adam Menz
149 9c4430bd koutsoub
                if (cdnEnabled.equals("true")) {
150 3e180b04 Adam Menz
                        this.previewButton = (Button) findViewById(R.id.preview_button);
151 3e180b04 Adam Menz
                        previewButton.setOnClickListener(new MyOnClickListener());
152 3e180b04 Adam Menz
                } else {
153 3e180b04 Adam Menz
                        this.previewButton = (Button) findViewById(R.id.preview_button);
154 3e180b04 Adam Menz
                        previewButton.setVisibility(View.GONE);
155 3e180b04 Adam Menz
                }
156 3e180b04 Adam Menz
157 9c4430bd koutsoub
                if (state != null && state.containsKey("isDownloaded")) {
158 3e180b04 Adam Menz
                        isDownloaded = state.getBoolean("isDownloaded");
159 9c4430bd koutsoub
                } else {
160 3e180b04 Adam Menz
                        isDownloaded = fileIsDownloaded();
161 3e180b04 Adam Menz
                }
162 3e180b04 Adam Menz
                this.downloadButton = (Button) findViewById(R.id.download_button);
163 9c4430bd koutsoub
                if (isDownloaded) {
164 3e180b04 Adam Menz
                        downloadButton.setText("Open File");
165 3e180b04 Adam Menz
                } else {
166 3e180b04 Adam Menz
                        downloadButton.setText("Download File");
167 9c4430bd koutsoub
                }
168 3e180b04 Adam Menz
                downloadButton.setOnClickListener(new MyOnClickListener());
169 9c4430bd koutsoub
170 9c4430bd koutsoub
                if (app.isDeletingObject()) {
171 3e180b04 Adam Menz
                        deleteObjTask = new DeleteObjectListenerTask();
172 3e180b04 Adam Menz
                        deleteObjTask.execute();
173 3e180b04 Adam Menz
                }
174 9c4430bd koutsoub
175 9c4430bd koutsoub
                if (app.isDownloadingObject()) {
176 3e180b04 Adam Menz
                        downloadObjTask = new DownloadObjectListenerTask();
177 3e180b04 Adam Menz
                        downloadObjTask.execute();
178 3e180b04 Adam Menz
                }
179 9c4430bd koutsoub
180 3e180b04 Adam Menz
        }
181 9c4430bd koutsoub
182 3e180b04 Adam Menz
        @Override
183 9c4430bd koutsoub
        protected void onStop() {
184 3e180b04 Adam Menz
                super.onStop();
185 51938302 Adam Menz
186 3e180b04 Adam Menz
                /*
187 9c4430bd koutsoub
                 * Need to stop running listener task if we exit
188 3e180b04 Adam Menz
                 */
189 9c4430bd koutsoub
                if (deleteObjTask != null) {
190 3e180b04 Adam Menz
                        deleteObjTask.cancel(true);
191 3e180b04 Adam Menz
                }
192 9c4430bd koutsoub
193 9c4430bd koutsoub
                if (downloadObjTask != null) {
194 3e180b04 Adam Menz
                        downloadObjTask.cancel(true);
195 3e180b04 Adam Menz
                }
196 3e180b04 Adam Menz
        }
197 3e180b04 Adam Menz
198 3e180b04 Adam Menz
        private void loadObjectData() {
199 9c4430bd koutsoub
                // Object Name
200 3e180b04 Adam Menz
                TextView name = (TextView) findViewById(R.id.view_container_name);
201 3e180b04 Adam Menz
                name.setText(objects.getCName().toString());
202 3e180b04 Adam Menz
203 9c4430bd koutsoub
                // File size
204 3e180b04 Adam Menz
                if (objects.getBytes() >= bConver) {
205 9c4430bd koutsoub
                        megaBytes = Math.abs(objects.getBytes() / bConver + 0.2);
206 3e180b04 Adam Menz
                        TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
207 3e180b04 Adam Menz
                        sublabel.setText(megaBytes + " MB");
208 9c4430bd koutsoub
                } else if (objects.getBytes() >= kbConver) {
209 9c4430bd koutsoub
                        kiloBytes = Math.abs(objects.getBytes() / kbConver + 0.2);
210 3e180b04 Adam Menz
                        TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
211 3e180b04 Adam Menz
                        sublabel.setText(kiloBytes + " KB");
212 6864568a Phillip Toohill
                } else {
213 3e180b04 Adam Menz
                        TextView sublabel = (TextView) findViewById(R.id.view_file_bytes);
214 3e180b04 Adam Menz
                        sublabel.setText(objects.getBytes() + " B");
215 9c4430bd koutsoub
                }
216 3e180b04 Adam Menz
217 9c4430bd koutsoub
                // Content Type
218 3e180b04 Adam Menz
                TextView cType = (TextView) findViewById(R.id.view_content_type);
219 3e180b04 Adam Menz
                cType.setText(objects.getContentType().toString());
220 3e180b04 Adam Menz
221 9c4430bd koutsoub
                // Last Modification date
222 3e180b04 Adam Menz
                String strDate = objects.getLastMod();
223 877f6f58 Adam Menz
                strDate = strDate.substring(0, strDate.indexOf('T'));
224 877f6f58 Adam Menz
225 3e180b04 Adam Menz
                TextView lastmod = (TextView) findViewById(R.id.view_file_modification);
226 9c4430bd koutsoub
                lastmod.setText(strDate);
227 c99967ba koutsoub
                rebuildMetadataList();
228 da02192c koutsoub
                if(isFolder()){
229 da02192c koutsoub
                        findViewById(R.id.download_text).setVisibility(View.GONE);
230 da02192c koutsoub
                        findViewById(R.id.linearLayout1).setVisibility(View.GONE);
231 da02192c koutsoub
                }
232 732d9c2c koutsoub
                rebuildPermissionList();
233 d37efce5 koutsoub
                try {
234 9c4430bd koutsoub
                        versions = new ContainerObjectManager(getApplicationContext())
235 9c4430bd koutsoub
                                        .getObjectVersions(objects.getContainerName(),
236 9c4430bd koutsoub
                                                        objects.getCName());
237 d37efce5 koutsoub
                        rebuildVersionList();
238 d37efce5 koutsoub
                } catch (CloudServersException e) {
239 d37efce5 koutsoub
                        // TODO Auto-generated catch block
240 d37efce5 koutsoub
                        e.printStackTrace();
241 d37efce5 koutsoub
                }
242 c99967ba koutsoub
        }
243 bd384c08 koutsoub
        
244 9c4430bd koutsoub
        private void rebuildMetadataList() {
245 bd384c08 koutsoub
                Button newmetadata = (Button)findViewById(R.id.newMetadata);
246 bd384c08 koutsoub
                newmetadata.setOnClickListener(new OnClickListener() {
247 bd384c08 koutsoub
                        
248 bd384c08 koutsoub
                        @Override
249 bd384c08 koutsoub
                        public void onClick(View arg0) {
250 bd384c08 koutsoub
                                final AlertDialog.Builder alert = new AlertDialog.Builder(ContainerObjectDetails.this);
251 bd384c08 koutsoub
                                alert.setTitle("Add Metadata");
252 bd384c08 koutsoub
                                LinearLayout ll = new LinearLayout(ContainerObjectDetails.this);
253 bd384c08 koutsoub
                                ll.setOrientation(LinearLayout.VERTICAL);
254 bd384c08 koutsoub
255 bd384c08 koutsoub
                                final EditText input = new EditText(ContainerObjectDetails.this);
256 bd384c08 koutsoub
                                final EditText input2 = new EditText(ContainerObjectDetails.this);
257 bd384c08 koutsoub
                                ll.addView(input);
258 bd384c08 koutsoub
                                ll.addView(input2);
259 1634500c koutsoub
                                alert.setView(ll);
260 bd384c08 koutsoub
                                alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {
261 bd384c08 koutsoub
                                        public void onClick(DialogInterface dialog, int whichButton) {
262 bd384c08 koutsoub
                                                String key = input.getText().toString().trim();
263 bd384c08 koutsoub
                                                String value = input2.getText().toString().trim();
264 bd384c08 koutsoub
                                                addMetatadata(key, value);
265 bd384c08 koutsoub
                                        }
266 bd384c08 koutsoub
                                });
267 bd384c08 koutsoub
                                
268 bd384c08 koutsoub
                                alert.setNegativeButton("Cancel",
269 bd384c08 koutsoub
                                                new DialogInterface.OnClickListener() {
270 bd384c08 koutsoub
                                        public void onClick(DialogInterface dialog, int whichButton) {
271 bd384c08 koutsoub
                                                dialog.cancel();
272 bd384c08 koutsoub
                                        }
273 bd384c08 koutsoub
                                });
274 bd384c08 koutsoub
                                alert.show();
275 bd384c08 koutsoub
                        }
276 bd384c08 koutsoub
                });
277 9c4430bd koutsoub
                LayoutInflater layoutInflater = LayoutInflater
278 9c4430bd koutsoub
                                .from(ContainerObjectDetails.this);
279 c99967ba koutsoub
                final LinearLayout metadata = (LinearLayout) findViewById(R.id.metadataList);
280 9c4430bd koutsoub
                if (metadata.getChildCount() > 0)
281 9c4430bd koutsoub
                        metadata.removeViews(0, metadata.getChildCount());
282 c99967ba koutsoub
                metadata.removeAllViews();
283 9c4430bd koutsoub
284 9c4430bd koutsoub
                if (objects.getMetadata() != null) {
285 9c4430bd koutsoub
                        int i = 0;
286 9c4430bd koutsoub
                        Iterator<Entry<String, String>> it = objects.getMetadata()
287 9c4430bd koutsoub
                                        .entrySet().iterator();
288 c99967ba koutsoub
                        while (it.hasNext()) {
289 9c4430bd koutsoub
                                final Entry<String, String> perm = it.next();
290 9c4430bd koutsoub
                                final View v = layoutInflater.inflate(R.layout.metadatarow,
291 9c4430bd koutsoub
                                                null);
292 c99967ba koutsoub
                                populateMetadataList(perm, v, metadata, i);
293 c99967ba koutsoub
                                i++;
294 c99967ba koutsoub
                        }
295 c99967ba koutsoub
                }
296 c99967ba koutsoub
        }
297 bd384c08 koutsoub
        
298 bd384c08 koutsoub
        private void addMetatadata(String key, String value){
299 bd384c08 koutsoub
                if(objects.getMetadata()==null)
300 bd384c08 koutsoub
                        objects.setMetadata(new HashMap<String, String>());
301 bd384c08 koutsoub
                objects.getMetadata().put(key, value);
302 bd384c08 koutsoub
                rebuildMetadataList();
303 bd384c08 koutsoub
        }
304 9c4430bd koutsoub
305 9c4430bd koutsoub
        private void populateMetadataList(final Entry<String, String> metadata,
306 9c4430bd koutsoub
                        final View v, final LinearLayout properties, int i) {
307 c99967ba koutsoub
                properties.addView(v, i);
308 9c4430bd koutsoub
                Log.d(LOG, i + " " + metadata.getKey() + " " + metadata.getValue());
309 c99967ba koutsoub
                ((TextView) v.findViewById(R.id.mkey)).setText(metadata.getKey());
310 c99967ba koutsoub
                ((TextView) v.findViewById(R.id.mvalue)).setText(metadata.getValue());
311 b2436f98 koutsoub
                ((ImageButton) v.findViewById(R.id.remove))
312 53026239 koutsoub
                .setOnClickListener(new OnClickListener() {
313 53026239 koutsoub
314 53026239 koutsoub
                        @Override
315 53026239 koutsoub
                        public void onClick(View v1) {
316 53026239 koutsoub
                                properties.removeView(v);
317 53026239 koutsoub
                                objects.getMetadata().remove(metadata.getKey());
318 53026239 koutsoub
319 53026239 koutsoub
                        }
320 53026239 koutsoub
                });
321 3e180b04 Adam Menz
        }
322 3e180b04 Adam Menz
323 9c4430bd koutsoub
        private void rebuildPermissionList() {
324 53026239 koutsoub
                Button newmetadata = (Button)findViewById(R.id.newPermission);
325 53026239 koutsoub
                newmetadata.setOnClickListener(new OnClickListener() {
326 53026239 koutsoub
327 53026239 koutsoub
                        @Override
328 53026239 koutsoub
                        public void onClick(View v) {
329 53026239 koutsoub
                                showAddDialog();                                
330 53026239 koutsoub
                        }
331 53026239 koutsoub
                        
332 53026239 koutsoub
                });
333 9c4430bd koutsoub
                LayoutInflater layoutInflater = LayoutInflater
334 9c4430bd koutsoub
                                .from(ContainerObjectDetails.this);
335 732d9c2c koutsoub
                final LinearLayout properties = (LinearLayout) findViewById(R.id.permissionsList);
336 9c4430bd koutsoub
                if (properties.getChildCount() > 0)
337 9c4430bd koutsoub
                        properties.removeViews(0, properties.getChildCount());
338 732d9c2c koutsoub
                properties.removeAllViews();
339 9c4430bd koutsoub
                Iterator<Permission> it = null;
340 9c4430bd koutsoub
                if (objects.getPermissions() != null) {
341 9c4430bd koutsoub
                        it = objects.getPermissions().iterator();
342 732d9c2c koutsoub
                        int i = 0;
343 732d9c2c koutsoub
                        while (it.hasNext()) {
344 732d9c2c koutsoub
                                final Permission perm = it.next();
345 9c4430bd koutsoub
                                final View v = layoutInflater.inflate(R.layout.propertiesrow,
346 9c4430bd koutsoub
                                                null);
347 732d9c2c koutsoub
                                populatePermissionList(perm, v, properties, i);
348 732d9c2c koutsoub
                                i++;
349 732d9c2c koutsoub
                        }
350 732d9c2c koutsoub
                }
351 732d9c2c koutsoub
        }
352 9c4430bd koutsoub
353 9c4430bd koutsoub
        private void populatePermissionList(final Permission perm, final View v,
354 9c4430bd koutsoub
                        final LinearLayout properties, int i) {
355 9c4430bd koutsoub
356 732d9c2c koutsoub
                properties.addView(v, i);
357 9c4430bd koutsoub
358 732d9c2c koutsoub
                ((TextView) v.findViewById(R.id.ownerName)).setText(perm.getUser());
359 732d9c2c koutsoub
                ((CheckBox) v.findViewById(R.id.read)).setChecked(perm.isRead());
360 732d9c2c koutsoub
                ((CheckBox) v.findViewById(R.id.write)).setChecked(perm.isWrite());
361 732d9c2c koutsoub
362 9c4430bd koutsoub
                ((CheckBox) v.findViewById(R.id.read))
363 9c4430bd koutsoub
                                .setOnCheckedChangeListener(new OnCheckedChangeListener() {
364 732d9c2c koutsoub
365 9c4430bd koutsoub
                                        @Override
366 9c4430bd koutsoub
                                        public void onCheckedChanged(CompoundButton buttonView,
367 9c4430bd koutsoub
                                                        boolean isChecked) {
368 9c4430bd koutsoub
                                                perm.setRead(isChecked);
369 732d9c2c koutsoub
370 9c4430bd koutsoub
                                        }
371 9c4430bd koutsoub
                                });
372 9c4430bd koutsoub
                ((CheckBox) v.findViewById(R.id.write))
373 9c4430bd koutsoub
                                .setOnCheckedChangeListener(new OnCheckedChangeListener() {
374 732d9c2c koutsoub
375 9c4430bd koutsoub
                                        @Override
376 9c4430bd koutsoub
                                        public void onCheckedChanged(CompoundButton buttonView,
377 9c4430bd koutsoub
                                                        boolean isChecked) {
378 9c4430bd koutsoub
                                                perm.setWrite(isChecked);
379 732d9c2c koutsoub
380 9c4430bd koutsoub
                                        }
381 9c4430bd koutsoub
                                });
382 9c4430bd koutsoub
                ((Button) v.findViewById(R.id.remove))
383 9c4430bd koutsoub
                                .setOnClickListener(new OnClickListener() {
384 732d9c2c koutsoub
385 9c4430bd koutsoub
                                        @Override
386 9c4430bd koutsoub
                                        public void onClick(View v1) {
387 9c4430bd koutsoub
                                                properties.removeView(v);
388 9c4430bd koutsoub
                                                objects.getPermissions().remove(perm);
389 9c4430bd koutsoub
390 9c4430bd koutsoub
                                        }
391 9c4430bd koutsoub
                                });
392 732d9c2c koutsoub
        }
393 53026239 koutsoub
        
394 53026239 koutsoub
        private void addPermission(boolean group, String userOrGroup){
395 53026239 koutsoub
                if(objects.getPermissions()==null)
396 53026239 koutsoub
                        objects.setPermissions(new ArrayList<Permission>());
397 53026239 koutsoub
                Permission np = new Permission();
398 53026239 koutsoub
                if(group)
399 53026239 koutsoub
                        np.setGroup(userOrGroup);
400 53026239 koutsoub
                else
401 53026239 koutsoub
                        np.setUser(userOrGroup);
402 53026239 koutsoub
                objects.getPermissions().add(np);
403 53026239 koutsoub
                rebuildPermissionList();
404 53026239 koutsoub
        }
405 53026239 koutsoub
        private void showAddDialog(){
406 53026239 koutsoub
                final CharSequence[] items = {"Add User", "Add Group"};
407 53026239 koutsoub
408 53026239 koutsoub
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
409 53026239 koutsoub
                builder.setTitle("User Or Group");
410 53026239 koutsoub
                builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
411 53026239 koutsoub
                    public void onClick(DialogInterface dialog, int item) {
412 53026239 koutsoub
                            boolean user;
413 53026239 koutsoub
                       if(item ==0){
414 53026239 koutsoub
                               user=true;
415 53026239 koutsoub
                       }
416 53026239 koutsoub
                       else
417 53026239 koutsoub
                               user=false;
418 53026239 koutsoub
                       populateAddDialog(user);
419 53026239 koutsoub
                       dialog.dismiss();
420 53026239 koutsoub
                       
421 53026239 koutsoub
                    }
422 53026239 koutsoub
                });
423 53026239 koutsoub
                AlertDialog alert2 = builder.create();
424 53026239 koutsoub
                alert2.show();
425 53026239 koutsoub
                
426 53026239 koutsoub
        }
427 53026239 koutsoub
        
428 53026239 koutsoub
        private void populateAddDialog(final boolean user){
429 53026239 koutsoub
                final AlertDialog.Builder alert = new AlertDialog.Builder(ContainerObjectDetails.this);
430 53026239 koutsoub
               if(user){
431 53026239 koutsoub
                       alert.setTitle("Add User");
432 53026239 koutsoub
                       final EditText input = new EditText(ContainerObjectDetails.this);
433 53026239 koutsoub
                input.setTextColor(Color.BLACK);
434 53026239 koutsoub
                    
435 53026239 koutsoub
                    alert.setView(input);
436 53026239 koutsoub
                    
437 53026239 koutsoub
                    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
438 53026239 koutsoub
                            public void onClick(DialogInterface dialog, int whichButton) {
439 53026239 koutsoub
                                    String value = input.getText().toString().trim();
440 53026239 koutsoub
                                    addPermission(false, value);
441 53026239 koutsoub
                                    
442 53026239 koutsoub
                            }
443 53026239 koutsoub
                    });
444 53026239 koutsoub
445 53026239 koutsoub
                    alert.setNegativeButton("Cancel",
446 53026239 koutsoub
                                    new DialogInterface.OnClickListener() {
447 53026239 koutsoub
                                            public void onClick(DialogInterface dialog, int whichButton) {
448 53026239 koutsoub
                                                    dialog.cancel();
449 53026239 koutsoub
                                            
450 53026239 koutsoub
                                            }
451 53026239 koutsoub
                                    });
452 53026239 koutsoub
                    alert.show();
453 53026239 koutsoub
               }
454 53026239 koutsoub
               else{
455 53026239 koutsoub
                       AlertDialog.Builder builder = new AlertDialog.Builder(this);
456 53026239 koutsoub
                    builder.setTitle("Add Group");
457 53026239 koutsoub
                    builder.setSingleChoiceItems(getGroupNames().toArray(new String[0]), -1, new DialogInterface.OnClickListener() {
458 53026239 koutsoub
                        public void onClick(DialogInterface dialog, int item) {
459 53026239 koutsoub
                                addPermission(true, getGroupNames().get(item));
460 53026239 koutsoub
                           dialog.dismiss();
461 53026239 koutsoub
                           
462 53026239 koutsoub
                        }
463 53026239 koutsoub
                    });
464 53026239 koutsoub
                    AlertDialog alert2 = builder.create();
465 53026239 koutsoub
                    alert2.show();
466 53026239 koutsoub
               }
467 53026239 koutsoub
            
468 53026239 koutsoub
        }
469 53026239 koutsoub
        
470 53026239 koutsoub
        public List<String> getGroupNames(){
471 53026239 koutsoub
                List<String> result = new ArrayList<String>();
472 53026239 koutsoub
                List<GroupResource> groups = ((AndroidCloudApplication)getApplication()).getGroups();
473 53026239 koutsoub
                for(GroupResource g : groups){
474 53026239 koutsoub
                        result.add(g.getName());
475 53026239 koutsoub
                }
476 53026239 koutsoub
                return result;
477 53026239 koutsoub
        }
478 53026239 koutsoub
        
479 d37efce5 koutsoub
        private void rebuildVersionList() {
480 9c4430bd koutsoub
                LayoutInflater layoutInflater = LayoutInflater
481 9c4430bd koutsoub
                                .from(ContainerObjectDetails.this);
482 d37efce5 koutsoub
                final LinearLayout properties = (LinearLayout) findViewById(R.id.versionsList);
483 d37efce5 koutsoub
                if (properties.getChildCount() > 0)
484 d37efce5 koutsoub
                        properties.removeViews(0, properties.getChildCount());
485 d37efce5 koutsoub
                properties.removeAllViews();
486 d37efce5 koutsoub
                Iterator<ObjectVersion> it;
487 9c4430bd koutsoub
                // Collections.reverse(versions);
488 d37efce5 koutsoub
                it = versions.iterator();
489 d37efce5 koutsoub
                int i = 0;
490 d37efce5 koutsoub
                while (it.hasNext()) {
491 d37efce5 koutsoub
                        final ObjectVersion perm = it.next();
492 d37efce5 koutsoub
                        final View v = layoutInflater.inflate(R.layout.versionsrow, null);
493 d37efce5 koutsoub
                        populateVersionList(perm, v, properties, i);
494 d37efce5 koutsoub
                        i++;
495 d37efce5 koutsoub
                }
496 d37efce5 koutsoub
        }
497 d37efce5 koutsoub
498 d37efce5 koutsoub
        private void populateVersionList(final ObjectVersion perm, final View v,
499 d37efce5 koutsoub
                        final LinearLayout properties, int i) {
500 d37efce5 koutsoub
501 d37efce5 koutsoub
                properties.addView(v, i);
502 d37efce5 koutsoub
503 d37efce5 koutsoub
                ((TextView) v.findViewById(R.id.versionName)).setText("Version: "
504 d37efce5 koutsoub
                                + perm.getVersion());
505 9c4430bd koutsoub
506 d37efce5 koutsoub
                ((TextView) v.findViewById(R.id.versionModified)).setText("Modified: "
507 d37efce5 koutsoub
                                + perm.getDateString());
508 9c4430bd koutsoub
                if (versions.size() == 1) {
509 9c4430bd koutsoub
                        // ((Button)
510 9c4430bd koutsoub
                        // v.findViewById(R.id.vremove)).setVisibility(View.INVISIBLE);
511 9c4430bd koutsoub
                        ((Button) v.findViewById(R.id.vrestore))
512 9c4430bd koutsoub
                                        .setVisibility(View.INVISIBLE);
513 d37efce5 koutsoub
                }
514 d37efce5 koutsoub
                /*
515 9c4430bd koutsoub
                 * ((Button) v.findViewById(R.id.vremove)) .setOnClickListener(new
516 9c4430bd koutsoub
                 * OnClickListener() {
517 9c4430bd koutsoub
                 * 
518 9c4430bd koutsoub
                 * @Override public void onClick(View v1) { Log.d("PERMS",
519 9c4430bd koutsoub
                 * perm.getUri()); try { new GssHttpCommands(getDroidApplication()
520 9c4430bd koutsoub
                 * .getUserDetails
521 9c4430bd koutsoub
                 * ()).deleteFolder(perm.getUri()+"?version="+perm.getVersion()); }
522 9c4430bd koutsoub
                 * catch (SystemErrorException e) { // TODO Auto-generated catch block
523 9c4430bd koutsoub
                 * e.printStackTrace(); } catch (GssHttpException e) { // TODO
524 9c4430bd koutsoub
                 * Auto-generated catch block e.printStackTrace(); }
525 9c4430bd koutsoub
                 * getFileTask().execute(res.getUri()); //properties.removeView(v);
526 9c4430bd koutsoub
                 * 
527 9c4430bd koutsoub
                 * } });
528 9c4430bd koutsoub
                 */
529 9c4430bd koutsoub
                ((Button) v.findViewById(R.id.vrestore))
530 d37efce5 koutsoub
                                .setOnClickListener(new OnClickListener() {
531 d37efce5 koutsoub
532 d37efce5 koutsoub
                                        @Override
533 d37efce5 koutsoub
                                        public void onClick(View v1) {
534 9c4430bd koutsoub
                                                // Log.d("PERMS", perm.getUri());
535 9c4430bd koutsoub
                                                // geRestoreVersionTask().execute(perm.getUri()+"?restoreVersion="+perm.getVersion());
536 d37efce5 koutsoub
537 d37efce5 koutsoub
                                        }
538 9c4430bd koutsoub
                                });
539 d37efce5 koutsoub
                ((Button) v.findViewById(R.id.vdownload))
540 9c4430bd koutsoub
                                .setOnClickListener(new OnClickListener() {
541 d37efce5 koutsoub
542 9c4430bd koutsoub
                                        @Override
543 9c4430bd koutsoub
                                        public void onClick(View v1) {
544 d37efce5 koutsoub
545 9c4430bd koutsoub
                                                // getDownloadTask().execute(perm.getUri()+"?version="+perm.getVersion());
546 9c4430bd koutsoub
                                                // properties.removeView(v);
547 9c4430bd koutsoub
548 9c4430bd koutsoub
                                        }
549 9c4430bd koutsoub
                                });
550 d37efce5 koutsoub
        }
551 9c4430bd koutsoub
552 3e180b04 Adam Menz
        private class MyOnClickListener implements View.OnClickListener {
553 3e180b04 Adam Menz
                @Override
554 3e180b04 Adam Menz
                public void onClick(View v) {
555 9c4430bd koutsoub
                        if (v.equals(findViewById(R.id.preview_button))) {
556 9c4430bd koutsoub
                                Intent viewIntent = new Intent("android.intent.action.VIEW",
557 9c4430bd koutsoub
                                                Uri.parse(cdnURL + "/" + objects.getCName()));
558 9c4430bd koutsoub
                                startActivity(viewIntent);
559 78af992b unknown
                        }
560 3e180b04 Adam Menz
                        /*
561 9c4430bd koutsoub
                         * need to perform different functions based on if the file is in
562 9c4430bd koutsoub
                         * the devices filesystem
563 3e180b04 Adam Menz
                         */
564 9c4430bd koutsoub
                        if (v.equals(findViewById(R.id.download_button))) {
565 9c4430bd koutsoub
                                if (!isDownloaded) {
566 9c4430bd koutsoub
                                        if (storageIsReady()) {
567 3e180b04 Adam Menz
                                                new ContainerObjectDownloadTask().execute();
568 9c4430bd koutsoub
                                        } else {
569 3e180b04 Adam Menz
                                                showAlert("Error", "Storage not found.");
570 3e180b04 Adam Menz
                                        }
571 9c4430bd koutsoub
                                } else {
572 3e180b04 Adam Menz
                                        openFile();
573 3e180b04 Adam Menz
                                }
574 3e180b04 Adam Menz
                        }
575 3e180b04 Adam Menz
                }
576 3e180b04 Adam Menz
        }
577 3e180b04 Adam Menz
578 9c4430bd koutsoub
        // Create the Menu options
579 9c4430bd koutsoub
        @Override
580 3e180b04 Adam Menz
        public boolean onCreateOptionsMenu(Menu menu) {
581 3d6041e8 Phillip Toohill
                super.onCreateOptionsMenu(menu);
582 3d6041e8 Phillip Toohill
                MenuInflater inflater = getMenuInflater();
583 3d6041e8 Phillip Toohill
                inflater.inflate(R.menu.container_object_list_menu, menu);
584 3d6041e8 Phillip Toohill
                return true;
585 9c4430bd koutsoub
        }
586 3e180b04 Adam Menz
587 9c4430bd koutsoub
        @Override
588 3e180b04 Adam Menz
        public boolean onOptionsItemSelected(MenuItem item) {
589 3d6041e8 Phillip Toohill
                switch (item.getItemId()) {
590 3d6041e8 Phillip Toohill
                case R.id.delete_object:
591 9c4430bd koutsoub
                        showDialog(deleteObject);
592 3d6041e8 Phillip Toohill
                        return true;
593 3d6041e8 Phillip Toohill
                case R.id.refresh:
594 3d6041e8 Phillip Toohill
                        loadObjectData();
595 3e180b04 Adam Menz
                        return true;
596 05a718db koutsoub
                case R.id.save:
597 05a718db koutsoub
                        saveObject();
598 05a718db koutsoub
                        return true;
599 3d6041e8 Phillip Toohill
                }
600 3d6041e8 Phillip Toohill
                return false;
601 9c4430bd koutsoub
        }
602 3e180b04 Adam Menz
603 3e180b04 Adam Menz
        @Override
604 9c4430bd koutsoub
        protected Dialog onCreateDialog(int id) {
605 3e180b04 Adam Menz
                switch (id) {
606 3e180b04 Adam Menz
                case deleteObject:
607 3e180b04 Adam Menz
                        return new AlertDialog.Builder(ContainerObjectDetails.this)
608 9c4430bd koutsoub
                                        .setIcon(R.drawable.alert_dialog_icon)
609 9c4430bd koutsoub
                                        .setTitle("Delete File")
610 9c4430bd koutsoub
                                        .setMessage("Are you sure you want to delete this file?")
611 9c4430bd koutsoub
                                        .setPositiveButton("Delete File",
612 9c4430bd koutsoub
                                                        new DialogInterface.OnClickListener() {
613 9c4430bd koutsoub
                                                                public void onClick(DialogInterface dialog,
614 9c4430bd koutsoub
                                                                                int whichButton) {
615 9c4430bd koutsoub
                                                                        // User clicked OK so do some stuff
616 9c4430bd koutsoub
                                                                        trackEvent(GoogleAnalytics.CATEGORY_FILE,
617 9c4430bd koutsoub
                                                                                        GoogleAnalytics.EVENT_DELETE, "",
618 9c4430bd koutsoub
                                                                                        -1);
619 9c4430bd koutsoub
                                                                        new ContainerObjectDeleteTask()
620 9c4430bd koutsoub
                                                                                        .execute((Void[]) null);
621 9c4430bd koutsoub
                                                                }
622 9c4430bd koutsoub
                                                        })
623 9c4430bd koutsoub
                                        .setNegativeButton("Cancel",
624 9c4430bd koutsoub
                                                        new DialogInterface.OnClickListener() {
625 9c4430bd koutsoub
                                                                public void onClick(DialogInterface dialog,
626 9c4430bd koutsoub
                                                                                int whichButton) {
627 9c4430bd koutsoub
                                                                        // User clicked Cancel so do some stuff
628 9c4430bd koutsoub
                                                                }
629 9c4430bd koutsoub
                                                        }).create();
630 3e180b04 Adam Menz
                }
631 3e180b04 Adam Menz
                return null;
632 3e180b04 Adam Menz
        }
633 9c4430bd koutsoub
634 3e180b04 Adam Menz
        /**
635 3d6041e8 Phillip Toohill
         * @return the file
636 3d6041e8 Phillip Toohill
         */
637 3d6041e8 Phillip Toohill
        public ContainerObjects getViewFile() {
638 3d6041e8 Phillip Toohill
                return objects;
639 3d6041e8 Phillip Toohill
        }
640 3d6041e8 Phillip Toohill
641 3d6041e8 Phillip Toohill
        /**
642 9c4430bd koutsoub
         * @param File
643 9c4430bd koutsoub
         *            the file to set
644 3d6041e8 Phillip Toohill
         */
645 3d6041e8 Phillip Toohill
        public void setViewFile(ContainerObjects object) {
646 3d6041e8 Phillip Toohill
                this.objects = object;
647 3d6041e8 Phillip Toohill
        }
648 3e180b04 Adam Menz
649 51938302 Adam Menz
        /*
650 9c4430bd koutsoub
         * returns false if external storage is not avaliable (if its mounted,
651 9c4430bd koutsoub
         * missing, read-only, etc) from:
652 9c4430bd koutsoub
         * http://developer.android.com/guide/topics/data
653 9c4430bd koutsoub
         * /data-storage.html#filesExternal
654 51938302 Adam Menz
         */
655 9c4430bd koutsoub
        private boolean storageIsReady() {
656 51938302 Adam Menz
                boolean mExternalStorageAvailable = false;
657 51938302 Adam Menz
                boolean mExternalStorageWriteable = false;
658 51938302 Adam Menz
                String state = Environment.getExternalStorageState();
659 51938302 Adam Menz
660 51938302 Adam Menz
                if (Environment.MEDIA_MOUNTED.equals(state)) {
661 3e180b04 Adam Menz
                        // We can read and write the media
662 3e180b04 Adam Menz
                        mExternalStorageAvailable = mExternalStorageWriteable = true;
663 51938302 Adam Menz
                } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
664 3e180b04 Adam Menz
                        // We can only read the media
665 3e180b04 Adam Menz
                        mExternalStorageAvailable = true;
666 3e180b04 Adam Menz
                        mExternalStorageWriteable = false;
667 51938302 Adam Menz
                } else {
668 9c4430bd koutsoub
                        // Something else is wrong. It may be one of many other states, but
669 9c4430bd koutsoub
                        // all we need
670 9c4430bd koutsoub
                        // to know is we can neither read nor write
671 3e180b04 Adam Menz
                        mExternalStorageAvailable = mExternalStorageWriteable = false;
672 51938302 Adam Menz
                }
673 51938302 Adam Menz
                return mExternalStorageAvailable && mExternalStorageWriteable;
674 51938302 Adam Menz
        }
675 3e180b04 Adam Menz
676 9c4430bd koutsoub
        private boolean fileIsDownloaded() {
677 9c4430bd koutsoub
                if (storageIsReady()) {
678 9c4430bd koutsoub
                        String fileName = Environment.getExternalStorageDirectory()
679 9c4430bd koutsoub
                                        .getPath() + "/PithosPlus/" + objects.getCName();
680 51938302 Adam Menz
                        File f = new File(fileName);
681 51938302 Adam Menz
                        return f.isFile();
682 51938302 Adam Menz
                }
683 51938302 Adam Menz
                return false;
684 51938302 Adam Menz
        }
685 3e180b04 Adam Menz
686 9c4430bd koutsoub
        private void openFile() {
687 9c4430bd koutsoub
                File object = new File(Environment.getExternalStorageDirectory()
688 9c4430bd koutsoub
                                .getPath() + "/PithosPlus/" + objects.getCName());
689 51938302 Adam Menz
                Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
690 9c4430bd koutsoub
                File file = new File(object.getAbsolutePath());
691 9c4430bd koutsoub
                String extension = android.webkit.MimeTypeMap
692 9c4430bd koutsoub
                                .getFileExtensionFromUrl(Uri.fromFile(file).toString());
693 9c4430bd koutsoub
                String mimetype = android.webkit.MimeTypeMap.getSingleton()
694 9c4430bd koutsoub
                                .getMimeTypeFromExtension(extension);
695 9c4430bd koutsoub
                myIntent.setDataAndType(Uri.fromFile(file), mimetype);
696 9c4430bd koutsoub
                // myIntent.setData(Uri.fromFile(file));
697 9c4430bd koutsoub
                try {
698 3e180b04 Adam Menz
                        startActivity(myIntent);
699 9c4430bd koutsoub
                } catch (Exception e) {
700 9c4430bd koutsoub
                        Toast.makeText(this, "Could not open file.", Toast.LENGTH_SHORT)
701 9c4430bd koutsoub
                                        .show();
702 3e180b04 Adam Menz
                }
703 51938302 Adam Menz
        }
704 3e180b04 Adam Menz
705 9c4430bd koutsoub
        // Task's
706 3e180b04 Adam Menz
707 9c4430bd koutsoub
        private class ContainerObjectDeleteTask extends
708 9c4430bd koutsoub
                        AsyncTask<Void, Void, HttpBundle> {
709 3e180b04 Adam Menz
710 3e180b04 Adam Menz
                private CloudServersException exception;
711 3e180b04 Adam Menz
712 9c4430bd koutsoub
                protected void onPreExecute() {
713 3e180b04 Adam Menz
                        showDialog();
714 3e180b04 Adam Menz
                        app.setDeleteingObject(true);
715 3e180b04 Adam Menz
                        deleteObjTask = new DeleteObjectListenerTask();
716 3e180b04 Adam Menz
                        deleteObjTask.execute();
717 3e180b04 Adam Menz
                }
718 3e180b04 Adam Menz
719 3e180b04 Adam Menz
                @Override
720 3e180b04 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
721 9c4430bd koutsoub
                        HttpBundle bundle = null;
722 3e180b04 Adam Menz
                        try {
723 9c4430bd koutsoub
                                bundle = (new ContainerObjectManager(getContext()))
724 9c4430bd koutsoub
                                                .deleteObject(containerNames, objects.getCName());
725 3e180b04 Adam Menz
                        } catch (CloudServersException e) {
726 3e180b04 Adam Menz
                                exception = e;
727 3e180b04 Adam Menz
                        }
728 9c4430bd koutsoub
729 3e180b04 Adam Menz
                        return bundle;
730 3e180b04 Adam Menz
                }
731 3e180b04 Adam Menz
732 3e180b04 Adam Menz
                @Override
733 3e180b04 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
734 3e180b04 Adam Menz
                        app.setDeleteingObject(false);
735 3e180b04 Adam Menz
                        hideDialog();
736 3e180b04 Adam Menz
                        HttpResponse response = bundle.getResponse();
737 3e180b04 Adam Menz
                        if (response != null) {
738 3e180b04 Adam Menz
                                int statusCode = response.getStatusLine().getStatusCode();
739 3e180b04 Adam Menz
                                if (statusCode == 204) {
740 9c4430bd koutsoub
                                        // handled by listener
741 3e180b04 Adam Menz
                                } else {
742 3e180b04 Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
743 3e180b04 Adam Menz
                                        if ("".equals(cse.getMessage())) {
744 9c4430bd koutsoub
                                                showError("There was a problem deleting your File.",
745 9c4430bd koutsoub
                                                                bundle);
746 3e180b04 Adam Menz
                                        } else {
747 9c4430bd koutsoub
                                                showError("There was a problem deleting your file: "
748 9c4430bd koutsoub
                                                                + cse.getMessage(), bundle);
749 3e180b04 Adam Menz
                                        }
750 3e180b04 Adam Menz
                                }
751 3e180b04 Adam Menz
                        } else if (exception != null) {
752 9c4430bd koutsoub
                                showError("There was a problem deleting your file: "
753 9c4430bd koutsoub
                                                + exception.getMessage(), bundle);
754 9c4430bd koutsoub
                        }
755 3e180b04 Adam Menz
                }
756 3e180b04 Adam Menz
        }
757 3e180b04 Adam Menz
758 9c4430bd koutsoub
        private class ContainerObjectDownloadTask extends
759 9c4430bd koutsoub
                        AsyncTask<Void, Void, HttpBundle> {
760 3e180b04 Adam Menz
761 3e180b04 Adam Menz
                private CloudServersException exception;
762 3e180b04 Adam Menz
763 3e180b04 Adam Menz
                @Override
764 9c4430bd koutsoub
                protected void onPreExecute() {
765 3e180b04 Adam Menz
                        showDialog();
766 3e180b04 Adam Menz
                        app.setDownloadingObject(true);
767 3e180b04 Adam Menz
                        downloadObjTask = new DownloadObjectListenerTask();
768 3e180b04 Adam Menz
                        downloadObjTask.execute();
769 3e180b04 Adam Menz
                }
770 3e180b04 Adam Menz
771 3e180b04 Adam Menz
                @Override
772 3e180b04 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
773 9c4430bd koutsoub
                        HttpBundle bundle = null;
774 3e180b04 Adam Menz
                        try {
775 9c4430bd koutsoub
                                bundle = (new ContainerObjectManager(getContext())).getObject(
776 9c4430bd koutsoub
                                                containerNames, objects.getCName());
777 3e180b04 Adam Menz
                        } catch (CloudServersException e) {
778 3e180b04 Adam Menz
                                exception = e;
779 3e180b04 Adam Menz
                        }
780 3e180b04 Adam Menz
                        return bundle;
781 3e180b04 Adam Menz
                }
782 3e180b04 Adam Menz
783 3e180b04 Adam Menz
                @Override
784 3e180b04 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
785 9c4430bd koutsoub
                        
786 9c4430bd koutsoub
                        
787 3e180b04 Adam Menz
                        HttpResponse response = bundle.getResponse();
788 3e180b04 Adam Menz
                        if (response != null) {
789 3e180b04 Adam Menz
                                int statusCode = response.getStatusLine().getStatusCode();
790 3e180b04 Adam Menz
                                if (statusCode == 200) {
791 3e180b04 Adam Menz
                                        setResult(Activity.RESULT_OK);
792 3e180b04 Adam Menz
                                        HttpEntity entity = response.getEntity();
793 3e180b04 Adam Menz
                                        app.setDownloadedEntity(entity);
794 3e180b04 Adam Menz
                                } else {
795 3e180b04 Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
796 3e180b04 Adam Menz
                                        if ("".equals(cse.getMessage())) {
797 9c4430bd koutsoub
                                                showError("There was a problem downloading your File.",
798 9c4430bd koutsoub
                                                                bundle);
799 3e180b04 Adam Menz
                                        } else {
800 9c4430bd koutsoub
                                                showError("There was a problem downloading your file: "
801 9c4430bd koutsoub
                                                                + cse.getMessage(), bundle);
802 3e180b04 Adam Menz
                                        }
803 3e180b04 Adam Menz
                                }
804 3e180b04 Adam Menz
                        } else if (exception != null) {
805 9c4430bd koutsoub
                                showError("There was a problem downloading your file: "
806 9c4430bd koutsoub
                                                + exception.getMessage(), bundle);
807 9c4430bd koutsoub
                        }
808 9c4430bd koutsoub
                        app.setDownloadingObject(false);
809 3e180b04 Adam Menz
                }
810 3e180b04 Adam Menz
        }
811 9c4430bd koutsoub
812 9c4430bd koutsoub
        private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> {
813 9c4430bd koutsoub
814 3e180b04 Adam Menz
                @Override
815 3e180b04 Adam Menz
                protected Void doInBackground(Void... arg1) {
816 3e180b04 Adam Menz
817 9c4430bd koutsoub
                        while (app.isDeletingObject()) {
818 3e180b04 Adam Menz
                                // wait for process to finish
819 3e180b04 Adam Menz
                                // or have it be canceled
820 9c4430bd koutsoub
                                if (deleteObjTask.isCancelled()) {
821 3e180b04 Adam Menz
                                        return null;
822 3e180b04 Adam Menz
                                }
823 3e180b04 Adam Menz
                        }
824 3e180b04 Adam Menz
                        return null;
825 3e180b04 Adam Menz
                }
826 3e180b04 Adam Menz
827 3e180b04 Adam Menz
                /*
828 9c4430bd koutsoub
                 * when no longer processing, time to load the new files
829 3e180b04 Adam Menz
                 */
830 3e180b04 Adam Menz
                @Override
831 3e180b04 Adam Menz
                protected void onPostExecute(Void arg1) {
832 3e180b04 Adam Menz
                        hideDialog();
833 3e180b04 Adam Menz
                        setResult(99);
834 3e180b04 Adam Menz
                        finish();
835 3e180b04 Adam Menz
                }
836 3e180b04 Adam Menz
        }
837 9c4430bd koutsoub
838 3e180b04 Adam Menz
        private class DownloadObjectListenerTask extends
839 9c4430bd koutsoub
                        AsyncTask<Void, Void, Void> {
840 9c4430bd koutsoub
841 3e180b04 Adam Menz
                @Override
842 3e180b04 Adam Menz
                protected Void doInBackground(Void... arg1) {
843 3e180b04 Adam Menz
844 9c4430bd koutsoub
                        while (app.isDownloadingObject()) {
845 3e180b04 Adam Menz
                                // wait for process to finish
846 3e180b04 Adam Menz
                                // or have it be canceled
847 9c4430bd koutsoub
                                if (downloadObjTask.isCancelled()) {
848 3e180b04 Adam Menz
                                        return null;
849 3e180b04 Adam Menz
                                }
850 3e180b04 Adam Menz
                        }
851 3e180b04 Adam Menz
                        return null;
852 3e180b04 Adam Menz
                }
853 3e180b04 Adam Menz
854 3e180b04 Adam Menz
                /*
855 9c4430bd koutsoub
                 * when no longer processing, time to load the new files
856 3e180b04 Adam Menz
                 */
857 3e180b04 Adam Menz
                @Override
858 3e180b04 Adam Menz
                protected void onPostExecute(Void arg1) {
859 9c4430bd koutsoub
                        
860 3e180b04 Adam Menz
                        try {
861 9c4430bd koutsoub
                                //TODO: run in background
862 9c4430bd koutsoub
                                if (writeFile(app.getDownloadedEntity().getContent())) {
863 3e180b04 Adam Menz
                                        downloadButton.setText("Open File");
864 3e180b04 Adam Menz
                                        isDownloaded = true;
865 9c4430bd koutsoub
                                } else {
866 9c4430bd koutsoub
                                        showAlert("Error",
867 9c4430bd koutsoub
                                                        "There was a problem downloading your file.");
868 3e180b04 Adam Menz
                                }
869 3e180b04 Adam Menz
870 3e180b04 Adam Menz
                        } catch (IOException e) {
871 3e180b04 Adam Menz
                                showAlert("Error", "There was a problem downloading your file.");
872 3e180b04 Adam Menz
                                e.printStackTrace();
873 969195cf Adam Menz
                        } catch (Exception e) {
874 969195cf Adam Menz
                                showAlert("Error", "There was a problem downloading your file.");
875 969195cf Adam Menz
                                e.printStackTrace();
876 3e180b04 Adam Menz
                        }
877 9c4430bd koutsoub
                        hideDialog();
878 3e180b04 Adam Menz
                }
879 3e180b04 Adam Menz
        }
880 3e180b04 Adam Menz
881 9c4430bd koutsoub
        private boolean writeFile(InputStream in) {
882 9c4430bd koutsoub
                String directoryName = Environment.getExternalStorageDirectory()
883 9c4430bd koutsoub
                                .getPath();
884 9c4430bd koutsoub
                File f = new File(directoryName, DOWNLOAD_DIRECTORY);
885 9c4430bd koutsoub
                Log.i(LOG,directoryName);
886 9c4430bd koutsoub
                if (!f.isDirectory()) {
887 9c4430bd koutsoub
                        if (!f.mkdir()) {
888 9c4430bd koutsoub
                                return false;
889 9c4430bd koutsoub
                        }
890 9c4430bd koutsoub
                }
891 9c4430bd koutsoub
                Log.i(LOG,objects.toString());
892 9c4430bd koutsoub
                //String filename = directoryName + "/" + objects.getName();
893 9c4430bd koutsoub
                StringTokenizer str = new StringTokenizer(objects.getCName(),"/");
894 9c4430bd koutsoub
                String path="";
895 9c4430bd koutsoub
                String fname="";
896 9c4430bd koutsoub
                int count = str.countTokens();
897 9c4430bd koutsoub
                Log.i(LOG,"object is: "+objects.getCName()+" "+count);
898 9c4430bd koutsoub
                for(int i=0;i<count;i++){
899 9c4430bd koutsoub
                        if(i<(count-1)){
900 9c4430bd koutsoub
                                path = path+str.nextToken()+"/";
901 9c4430bd koutsoub
                        }
902 9c4430bd koutsoub
                        else
903 9c4430bd koutsoub
                                fname=str.nextToken();
904 9c4430bd koutsoub
                }
905 9c4430bd koutsoub
                Log.i(LOG,"Path is:"+path);
906 9c4430bd koutsoub
                Log.i(LOG,"Fname is:"+fname);
907 9c4430bd koutsoub
                File object;
908 9c4430bd koutsoub
                if("".equals(path)){
909 9c4430bd koutsoub
                        object = new File(f,fname);
910 9c4430bd koutsoub
                }
911 9c4430bd koutsoub
                else{
912 9c4430bd koutsoub
                        File t = new File(f,path);
913 9c4430bd koutsoub
                        t.mkdirs();
914 9c4430bd koutsoub
                        object = new File(t,fname);
915 9c4430bd koutsoub
                }
916 9c4430bd koutsoub
                
917 9c4430bd koutsoub
                
918 9c4430bd koutsoub
                BufferedOutputStream bos = null;
919 9c4430bd koutsoub
920 9c4430bd koutsoub
                try {
921 9c4430bd koutsoub
                        FileOutputStream fos = new FileOutputStream(object);
922 9c4430bd koutsoub
                        copy(in, fos);
923 9c4430bd koutsoub
                } catch (IOException e) {
924 9c4430bd koutsoub
                        e.printStackTrace();
925 9c4430bd koutsoub
                } finally {
926 9c4430bd koutsoub
                        if (bos != null) {
927 9c4430bd koutsoub
                                try {
928 9c4430bd koutsoub
                                        bos.flush();
929 9c4430bd koutsoub
                                        bos.close();
930 9c4430bd koutsoub
                                } catch (IOException e) {
931 9c4430bd koutsoub
                                        // TODO Auto-generated catch block
932 9c4430bd koutsoub
                                        e.printStackTrace();
933 9c4430bd koutsoub
                                }
934 9c4430bd koutsoub
                        }
935 9c4430bd koutsoub
                }
936 9c4430bd koutsoub
                return true;
937 9c4430bd koutsoub
        }
938 9c4430bd koutsoub
939 9c4430bd koutsoub
        public static long copy(InputStream input, OutputStream output) throws IOException {
940 9c4430bd koutsoub
                
941 9c4430bd koutsoub
                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
942 9c4430bd koutsoub
                long count = 0;
943 9c4430bd koutsoub
                int n = 0;
944 9c4430bd koutsoub
                try{
945 9c4430bd koutsoub
                        while (-1 != (n = input.read(buffer))) {
946 9c4430bd koutsoub
                                output.write(buffer, 0, n);
947 9c4430bd koutsoub
                                count += n;
948 9c4430bd koutsoub
                                //monitor.setCurrent(count);
949 9c4430bd koutsoub
                        }
950 9c4430bd koutsoub
                }
951 9c4430bd koutsoub
                finally{
952 9c4430bd koutsoub
                        input.close();
953 9c4430bd koutsoub
                        output.close();
954 9c4430bd koutsoub
                }
955 9c4430bd koutsoub
                return count;
956 9c4430bd koutsoub
        }
957 53026239 koutsoub
        
958 53026239 koutsoub
        @Override
959 53026239 koutsoub
        public void onBackPressed() {
960 05a718db koutsoub
                
961 53026239 koutsoub
                //TODO: perform update
962 53026239 koutsoub
                super.onBackPressed();
963 53026239 koutsoub
        }
964 9c4430bd koutsoub
        
965 05a718db koutsoub
        public void saveObject(){
966 05a718db koutsoub
                Map<String,String> headers = new HashMap<String,String>();
967 05a718db koutsoub
                for(Entry<String,String> entry : objects.getMetadata().entrySet()){
968 05a718db koutsoub
                        headers.put("X-Object-Meta-"+entry.getKey(), entry.getValue());
969 05a718db koutsoub
                }
970 05a718db koutsoub
                try {
971 05a718db koutsoub
                        HttpBundle b = new ContainerObjectManager(getApplicationContext()).updateObject(objects.getContainerName(), objects.getCName(), "", null, headers);
972 05a718db koutsoub
                        Log.i(LOG,"response:"+b.getResponse().getStatusLine().getStatusCode());
973 05a718db koutsoub
                } catch (CloudServersException e) {
974 05a718db koutsoub
                        // TODO Auto-generated catch block
975 05a718db koutsoub
                        e.printStackTrace();
976 05a718db koutsoub
                }
977 05a718db koutsoub
        }
978 05a718db koutsoub
        
979 9c4430bd koutsoub
980 3e180b04 Adam Menz
}