Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / AddFileActivity.java @ 7dbfc514

History | View | Annotate | Download (3.2 kB)

1
package com.rackspace.cloud.android;
2

    
3
import org.apache.http.HttpResponse;
4

    
5
import android.app.Activity;
6
import android.os.AsyncTask;
7
import android.os.Bundle;
8
import android.view.View;
9
import android.view.View.OnClickListener;
10
import android.widget.Button;
11
import android.widget.EditText;
12

    
13
import com.rackspace.cloud.android.R;
14
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
15
import com.rackspace.cloud.servers.api.client.CloudServersException;
16
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
17

    
18
public class AddFileActivity extends CloudActivity implements OnClickListener{
19

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

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

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

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

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

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

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

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

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

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