Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ContainerObjectDetails.java @ 05a718db

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