Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / AddFileActivity.java @ fee0d4aa

History | View | Annotate | Download (3.1 kB)

1
package com.rackspacecloud.android;
2

    
3
import org.apache.http.HttpResponse;
4

    
5
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
6
import com.rackspace.cloud.servers.api.client.CloudServersException;
7
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
8

    
9
import android.app.Activity;
10
import android.os.AsyncTask;
11
import android.os.Bundle;
12
import android.view.View;
13
import android.view.View.OnClickListener;
14
import android.widget.Button;
15
import android.widget.EditText;
16

    
17
public class AddFileActivity extends CloudActivity implements OnClickListener{
18

    
19
        private EditText fileName;
20
        private EditText contents;
21
        private String containerName;
22
        private String path;
23

    
24
        /** Called when the activity is first created. */
25
        @Override
26
        public void onCreate(Bundle savedInstanceState) {
27
                super.onCreate(savedInstanceState);
28
                trackPageView(PAGE_ADD_OBJECT);
29
                setContentView(R.layout.addtextfile);
30
                restoreState(savedInstanceState);
31
        }
32

    
33
        protected void restoreState(Bundle state){
34
                super.restoreState(state);
35
                containerName = (String) this.getIntent().getExtras().get("Cname");
36
                path = (String) this.getIntent().getExtras().get("curPath");
37
                setUpInputs();
38
        }
39

    
40
        @Override
41
        protected void onSaveInstanceState(Bundle outState) {
42
                super.onSaveInstanceState(outState);
43
        }
44

    
45
        private void setUpInputs(){
46
                ((Button) findViewById(R.id.new_file_button)).setOnClickListener(this);
47
                fileName = ((EditText)findViewById(R.id.file_name_text));
48
                fileName.append(".txt");
49
                contents = ((EditText)findViewById(R.id.new_file_text));
50
        }
51

    
52
        public void onClick(View arg0) {
53
                if ("".equals(fileName.getText().toString())) {
54
                        showAlert("Required Fields Missing", " File name is required.");
55
                } else {
56
                        trackEvent(CATEGORY_FILE, EVENT_CREATE, "", -1);
57
                        new SaveFileTask().execute((Void[]) null);
58
                }
59
        }
60

    
61
        private class SaveFileTask extends AsyncTask<Void, Void, HttpBundle> {
62
                private CloudServersException exception;
63

    
64
                protected void onPreExecute(){
65
                        showDialog();
66
                }
67

    
68
                @Override
69
                protected HttpBundle doInBackground(Void... arg0) {
70
                        HttpBundle bundle = null;
71
                        try {
72
                                bundle = (new ContainerObjectManager(getContext())).addObject(containerName, path, fileName.getText().toString(), "text/plain", contents.getText().toString());
73
                        } catch (CloudServersException e) {
74
                                exception = e;
75
                        }
76
                        return bundle;
77
                }
78

    
79
                @Override
80
                protected void onPostExecute(HttpBundle bundle) {
81
                        hideDialog();
82
                        HttpResponse response = bundle.getResponse();
83
                        if (response != null) {
84
                                int statusCode = response.getStatusLine().getStatusCode();
85
                                if (statusCode == 201) {
86
                                        setResult(Activity.RESULT_OK);
87
                                        finish();
88
                                } else {
89
                                        CloudServersException cse = parseCloudServersException(response);
90
                                        if ("".equals(cse.getMessage())) {
91
                                                showError("There was a problem creating your file.", bundle);
92
                                        } else {
93
                                                showError("There was a problem creating your file: " + cse.getMessage() +  " See details for more information", bundle);
94
                                        }
95
                                }
96
                        } else if (exception != null) {
97
                                showError("There was a problem creating your file: " + exception.getMessage()+ " See details for more information", bundle);                                
98
                        }                        
99
                }
100
        }
101
}