Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / AddContainerActivity.java @ 48601850

History | View | Annotate | Download (2.8 kB)

1
package com.rackspacecloud.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.ContainerManager;
15
import com.rackspace.cloud.servers.api.client.CloudServersException;
16
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
17
/** 
18
 * 
19
 * @author Phillip Toohill
20
 *
21
 */
22
public class AddContainerActivity extends CloudActivity implements OnClickListener {
23

    
24
        private EditText containerName;        
25
        
26
        /** Called when the activity is first created. */
27
        @Override
28
        public void onCreate(Bundle savedInstanceState) {
29
                super.onCreate(savedInstanceState);
30
                trackPageView(GoogleAnalytics.PAGE_ADD_CONTAINER);
31
                setContentView(R.layout.createcontainer);
32
                restoreState(savedInstanceState);
33
        }
34
        
35
        protected void restoreState(Bundle state){
36
                super.restoreState(state);
37
                containerName = (EditText) findViewById(R.id.container_name);
38
                ((Button) findViewById(R.id.save_button)).setOnClickListener(this);
39
        }
40
        
41
        public void onClick(View arg0) {
42
                if ("".equals(containerName.getText().toString())) {
43
                        showAlert("Required Fields Missing", " Container name is required.");
44
                } else {
45
                        trackEvent(GoogleAnalytics.CATEGORY_CONTAINER, GoogleAnalytics.EVENT_CREATE, "", -1);
46
                        new CreateContainerTask().execute((Void[]) null);
47
                }
48
        }
49
        
50
        private class CreateContainerTask extends AsyncTask<Void, Void, HttpBundle> {
51
                private CloudServersException exception;
52

    
53
                protected void onPreExecute(){
54
                        showDialog();
55
                }
56
                
57
                @Override
58
                protected HttpBundle doInBackground(Void... arg0) {
59
                        HttpBundle bundle = null;
60
                        try {
61
                                bundle = (new ContainerManager(getContext())).create(containerName.getText());
62
                        } catch (CloudServersException e) {
63
                                exception = e;
64
                        }
65
                        return bundle;
66
                }
67

    
68
                @Override
69
                protected void onPostExecute(HttpBundle bundle) {
70
                        hideDialog();
71
                        HttpResponse response = bundle.getResponse();
72
                        if (response != null) {
73
                                int statusCode = response.getStatusLine().getStatusCode();
74
                                if (statusCode == 201) {
75
                                        setResult(Activity.RESULT_OK);
76
                                        finish();
77
                                } else {
78
                                        CloudServersException cse = parseCloudServersException(response);
79
                                        if ("".equals(cse.getMessage())) {
80
                                                showError("There was a problem creating your container.", bundle);
81
                                        } else {
82
                                                showError("There was a problem creating your container: " + cse.getMessage() + " See details for more information.", bundle);
83
                                        }
84
                                }
85
                        } else if (exception != null) {
86
                                showError("There was a problem creating your container: " + exception.getMessage()+" See details for more information.", bundle);                                
87
                        }
88
                        finish();
89
                }
90
        }
91

    
92

    
93

    
94
}