Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / AddContainerActivity.java @ 0edf6b39

History | View | Annotate | Download (6.4 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5

    
6
import javax.xml.parsers.FactoryConfigurationError;
7
import javax.xml.parsers.ParserConfigurationException;
8
import javax.xml.parsers.SAXParser;
9
import javax.xml.parsers.SAXParserFactory;
10

    
11
import org.apache.http.HttpResponse;
12
import org.apache.http.client.ClientProtocolException;
13
import org.apache.http.impl.client.BasicResponseHandler;
14
import org.xml.sax.InputSource;
15
import org.xml.sax.SAXException;
16
import org.xml.sax.XMLReader;
17

    
18
import android.app.Activity;
19
import android.app.AlertDialog;
20
import android.content.Context;
21
import android.content.DialogInterface;
22
import android.content.Intent;
23
import android.os.AsyncTask;
24
import android.os.Bundle;
25
import android.view.View;
26
import android.view.View.OnClickListener;
27
import android.widget.Button;
28
import android.widget.EditText;
29
import android.widget.ProgressBar;
30
import android.widget.TextView;
31
import android.widget.Toast;
32

    
33
import com.rackspace.cloud.files.api.client.ContainerManager;
34
import com.rackspace.cloud.servers.api.client.CloudServersException;
35
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
36
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
37
/** 
38
 * 
39
 * @author Phillip Toohill
40
 *
41
 */
42
public class AddContainerActivity extends GaActivity implements  OnClickListener {
43

    
44
        private EditText containerName;
45
        private Context context;        
46
        private boolean isSaving;
47
        
48
        /** Called when the activity is first created. */
49
        @Override
50
        public void onCreate(Bundle savedInstanceState) {
51
                super.onCreate(savedInstanceState);
52
                trackPageView(PAGE_ADD_CONTAINER);
53
                context = getApplicationContext();
54
                setContentView(R.layout.createcontainer);
55
                containerName = (EditText) findViewById(R.id.container_name);
56
                ((Button) findViewById(R.id.save_button)).setOnClickListener(this);
57
                isSaving = savedInstanceState != null && savedInstanceState.containsKey("isSaving")
58
                        && savedInstanceState.getBoolean("isSaving");
59
                if(isSaving){
60
                        showActivityIndicators();
61
                }
62
        }
63
        
64
        @Override
65
        protected void onSaveInstanceState(Bundle outState){
66
                super.onSaveInstanceState(outState);
67
                outState.putBoolean("isSaving", isSaving);
68
                
69
        }
70
        
71
        public void onClick(View arg0) {
72
                if ("".equals(containerName.getText().toString())) {
73
                        showAlert("Required Fields Missing", " Container name is required.");
74
                } else {
75
                        showActivityIndicators();
76
                        trackEvent(CATEGORY_CONTAINER, EVENT_CREATE, "", -1);
77
                        new CreateContainerTask().execute((Void[]) null);
78
                }
79
        }
80

    
81

    
82
        private void showAlert(String title, String message) {
83
                AlertDialog alert = new AlertDialog.Builder(this).create();
84
                alert.setTitle(title);
85
                alert.setMessage(message);
86
                alert.setButton("OK", new DialogInterface.OnClickListener() {
87
                        public void onClick(DialogInterface dialog, int which) {
88
                                return;
89
                        } }); 
90
                alert.show();
91
                hideActivityIndicators();
92
        }
93

    
94
        private void showToast(String message) {
95
                Context context = getApplicationContext();
96
                int duration = Toast.LENGTH_SHORT;
97
                Toast toast = Toast.makeText(context, message, duration);
98
                toast.show();
99
        }
100

    
101
        private void setActivityIndicatorsVisibility(int visibility) {
102
                ProgressBar pb = (ProgressBar) findViewById(R.id.save_container_progress_bar);
103
                TextView tv = (TextView) findViewById(R.id.saving_container_label);
104
                pb.setVisibility(visibility);
105
                tv.setVisibility(visibility);
106
        }
107

    
108
        private void showActivityIndicators() {
109
                isSaving = true;
110
                setActivityIndicatorsVisibility(View.VISIBLE);
111
        }
112

    
113
        private void hideActivityIndicators() {
114
                isSaving = false;
115
                setActivityIndicatorsVisibility(View.INVISIBLE);
116
        }
117
        //using cloudServersException, it works for us too
118
        private CloudServersException parseCloudServersException(HttpResponse response) {
119
                CloudServersException cse = new CloudServersException();
120
                try {
121
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
122
                        String body = responseHandler.handleResponse(response);
123
                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
124
                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
125
                        XMLReader xmlReader = saxParser.getXMLReader();
126
                        xmlReader.setContentHandler(parser);
127
                        xmlReader.parse(new InputSource(new StringReader(body)));                            
128
                        cse = parser.getException();                            
129
                } catch (ClientProtocolException e) {
130
                        cse = new CloudServersException();
131
                        cse.setMessage(e.getLocalizedMessage());
132
                } catch (IOException e) {
133
                        cse = new CloudServersException();
134
                        cse.setMessage(e.getLocalizedMessage());
135
                } catch (ParserConfigurationException e) {
136
                        cse = new CloudServersException();
137
                        cse.setMessage(e.getLocalizedMessage());
138
                } catch (SAXException e) {
139
                        cse = new CloudServersException();
140
                        cse.setMessage(e.getLocalizedMessage());
141
                } catch (FactoryConfigurationError e) {
142
                        cse = new CloudServersException();
143
                        cse.setMessage(e.getLocalizedMessage());
144
                }
145
                return cse;
146
        }
147

    
148
        private void startFileError(String message, HttpBundle bundle){
149
                Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
150
                viewIntent.putExtra("errorMessage", message);
151
                viewIntent.putExtra("response", bundle.getResponseText());
152
                viewIntent.putExtra("request", bundle.getCurlRequest());
153
                startActivity(viewIntent);
154
        }
155

    
156
        private class CreateContainerTask extends AsyncTask<Void, Void, HttpBundle> {
157
                private CloudServersException exception;
158

    
159
                @Override
160
                protected HttpBundle doInBackground(Void... arg0) {
161
                        HttpBundle bundle = null;
162
                        try {
163
                                bundle = (new ContainerManager(context)).create(containerName.getText());
164
                        } catch (CloudServersException e) {
165
                                exception = e;
166
                        }
167
                        return bundle;
168
                }
169

    
170
                @Override
171
                protected void onPostExecute(HttpBundle bundle) {
172
                        HttpResponse response = bundle.getResponse();
173
                        if (response != null) {
174
                                int statusCode = response.getStatusLine().getStatusCode();
175
                                if (statusCode == 201) {
176
                                        setResult(Activity.RESULT_OK);
177
                                        finish();
178
                                } else {
179
                                        CloudServersException cse = parseCloudServersException(response);
180
                                        if ("".equals(cse.getMessage())) {
181
                                                startFileError("There was a problem creating your container.", bundle);
182
                                        } else {
183
                                                //if container with same name already exists
184
                                                showToast("There was a problem creating your container: " + cse.getMessage() + " Check container name and try again");
185
                                        }
186
                                }
187
                        } else if (exception != null) {
188
                                startFileError("There was a problem creating your container: " + exception.getMessage()+" Check container name and try again", bundle);                                
189
                        }
190
                        finish();
191
                }
192
        }
193

    
194

    
195

    
196
}