Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / EnableCDNActivity.java @ c476482b

History | View | Annotate | Download (10.9 kB)

1 3d6041e8 Phillip Toohill
package com.rackspacecloud.android;
2 3d6041e8 Phillip Toohill
3 3d6041e8 Phillip Toohill
import java.io.IOException;
4 3d6041e8 Phillip Toohill
import java.io.StringReader;
5 3d6041e8 Phillip Toohill
6 3d6041e8 Phillip Toohill
import javax.xml.parsers.FactoryConfigurationError;
7 3d6041e8 Phillip Toohill
import javax.xml.parsers.ParserConfigurationException;
8 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParser;
9 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParserFactory;
10 3d6041e8 Phillip Toohill
11 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
12 3d6041e8 Phillip Toohill
import org.apache.http.client.ClientProtocolException;
13 3d6041e8 Phillip Toohill
import org.apache.http.impl.client.BasicResponseHandler;
14 3d6041e8 Phillip Toohill
import org.xml.sax.InputSource;
15 3d6041e8 Phillip Toohill
import org.xml.sax.SAXException;
16 3d6041e8 Phillip Toohill
import org.xml.sax.XMLReader;
17 3d6041e8 Phillip Toohill
18 3d6041e8 Phillip Toohill
import android.app.Activity;
19 3d6041e8 Phillip Toohill
import android.app.AlertDialog;
20 3d6041e8 Phillip Toohill
import android.app.Dialog;
21 b347d5e3 Chmouel Boudjnah
import android.content.Context;
22 3d6041e8 Phillip Toohill
import android.content.DialogInterface;
23 32731215 Adam Menz
import android.content.Intent;
24 3d6041e8 Phillip Toohill
import android.os.AsyncTask;
25 3d6041e8 Phillip Toohill
import android.os.Bundle;
26 3d6041e8 Phillip Toohill
import android.view.View;
27 3d6041e8 Phillip Toohill
import android.view.View.OnClickListener;
28 3d6041e8 Phillip Toohill
import android.widget.AdapterView;
29 3d6041e8 Phillip Toohill
import android.widget.ArrayAdapter;
30 3d6041e8 Phillip Toohill
import android.widget.Button;
31 3d6041e8 Phillip Toohill
import android.widget.Spinner;
32 32731215 Adam Menz
import android.widget.Toast;
33 3d6041e8 Phillip Toohill
import android.widget.AdapterView.OnItemSelectedListener;
34 3d6041e8 Phillip Toohill
35 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerManager;
36 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
37 32731215 Adam Menz
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
38 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
39 3d6041e8 Phillip Toohill
40 4f5e4ef0 Adam Menz
public class EnableCDNActivity extends GaActivity implements OnClickListener,
41 240418be Phillip Toohill
                OnItemSelectedListener {
42 3d6041e8 Phillip Toohill
43 3d6041e8 Phillip Toohill
        public static String containerName = null;
44 3d6041e8 Phillip Toohill
        private String selectedTtlId;
45 3d6041e8 Phillip Toohill
        private String selectedLogRetId;
46 3d6041e8 Phillip Toohill
        private String selectedCdnId;
47 3d6041e8 Phillip Toohill
        private Spinner ttlSpinner;
48 3d6041e8 Phillip Toohill
        private Spinner logRetSpinner;
49 3d6041e8 Phillip Toohill
        private Spinner cdnSpinner;
50 b347d5e3 Chmouel Boudjnah
        private Context context;
51 b347d5e3 Chmouel Boudjnah
        
52 240418be Phillip Toohill
        /** Called when the activity is first created. */
53 240418be Phillip Toohill
        @Override
54 240418be Phillip Toohill
        public void onCreate(Bundle savedInstanceState) {
55 240418be Phillip Toohill
                super.onCreate(savedInstanceState);
56 4f5e4ef0 Adam Menz
                trackPageView(PAGE_CONTAINER_DETAILS);
57 240418be Phillip Toohill
                setContentView(R.layout.enable_cdn_container);
58 b347d5e3 Chmouel Boudjnah
                context = getApplicationContext();
59 240418be Phillip Toohill
                containerName = (String) this.getIntent().getExtras().get("Cname");
60 240418be Phillip Toohill
                setupButtons();
61 240418be Phillip Toohill
                loadTtlSpinner();
62 3d6041e8 Phillip Toohill
                loadLogRetSpinner();
63 3d6041e8 Phillip Toohill
                loadCdnSpinner();
64 240418be Phillip Toohill
65 240418be Phillip Toohill
        }
66 240418be Phillip Toohill
67 240418be Phillip Toohill
        private void setupButton(int resourceId, OnClickListener onClickListener) {
68 3d6041e8 Phillip Toohill
                Button button = (Button) findViewById(resourceId);
69 3d6041e8 Phillip Toohill
                button.setOnClickListener(onClickListener);
70 240418be Phillip Toohill
        }
71 240418be Phillip Toohill
72 240418be Phillip Toohill
        private void setupButtons() {
73 240418be Phillip Toohill
                setupButton(R.id.enable_button, new OnClickListener() {
74 240418be Phillip Toohill
                        public void onClick(View v) {
75 240418be Phillip Toohill
                                showDialog(R.id.enable_button);
76 240418be Phillip Toohill
                        }
77 240418be Phillip Toohill
                });
78 240418be Phillip Toohill
79 240418be Phillip Toohill
                setupButton(R.id.disable_button, new OnClickListener() {
80 240418be Phillip Toohill
                        public void onClick(View v) {
81 240418be Phillip Toohill
                                showDialog(R.id.disable_button);
82 240418be Phillip Toohill
                        }
83 240418be Phillip Toohill
                });
84 240418be Phillip Toohill
        }
85 240418be Phillip Toohill
86 240418be Phillip Toohill
        private void loadCdnSpinner() {
87 240418be Phillip Toohill
                cdnSpinner = (Spinner) findViewById(R.id.cdn_spinner);
88 240418be Phillip Toohill
                cdnSpinner.setOnItemSelectedListener(this);
89 240418be Phillip Toohill
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
90 240418be Phillip Toohill
                                this, R.array.cdn, android.R.layout.simple_spinner_item);
91 240418be Phillip Toohill
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
92 240418be Phillip Toohill
                cdnSpinner.setAdapter(adapter);
93 240418be Phillip Toohill
94 240418be Phillip Toohill
        }
95 240418be Phillip Toohill
96 240418be Phillip Toohill
        private void loadTtlSpinner() {
97 240418be Phillip Toohill
                ttlSpinner = (Spinner) findViewById(R.id.ttl_spinner);
98 240418be Phillip Toohill
                ttlSpinner.setOnItemSelectedListener(this);
99 240418be Phillip Toohill
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
100 240418be Phillip Toohill
                                this, R.array.ttl, android.R.layout.simple_spinner_item);
101 240418be Phillip Toohill
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
102 240418be Phillip Toohill
                ttlSpinner.setAdapter(adapter);
103 240418be Phillip Toohill
104 240418be Phillip Toohill
        }
105 240418be Phillip Toohill
106 240418be Phillip Toohill
        private void loadLogRetSpinner() {
107 240418be Phillip Toohill
                logRetSpinner = (Spinner) findViewById(R.id.log_retention_spinner);
108 240418be Phillip Toohill
                logRetSpinner.setOnItemSelectedListener(this);
109 240418be Phillip Toohill
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
110 240418be Phillip Toohill
                                this, R.array.logRet, android.R.layout.simple_spinner_item);
111 240418be Phillip Toohill
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
112 240418be Phillip Toohill
                logRetSpinner.setAdapter(adapter);
113 240418be Phillip Toohill
114 240418be Phillip Toohill
        }
115 240418be Phillip Toohill
116 240418be Phillip Toohill
        public void onItemSelected(AdapterView<?> parent, View view, int id,
117 240418be Phillip Toohill
                        long arg3) {
118 240418be Phillip Toohill
                if (parent == cdnSpinner) {
119 240418be Phillip Toohill
                        selectedCdnId = cdnSpinner.getSelectedItem().toString();
120 240418be Phillip Toohill
                }
121 3d6041e8 Phillip Toohill
                if (parent == ttlSpinner) {
122 3d6041e8 Phillip Toohill
                        selectedTtlId = ttlSpinner.getSelectedItem().toString();
123 3d6041e8 Phillip Toohill
                } else if (parent == logRetSpinner) {
124 3d6041e8 Phillip Toohill
                        selectedLogRetId = logRetSpinner.getSelectedItem().toString();
125 3d6041e8 Phillip Toohill
                }
126 240418be Phillip Toohill
        }
127 240418be Phillip Toohill
128 240418be Phillip Toohill
        public void onNothingSelected(AdapterView<?> parent) {
129 240418be Phillip Toohill
130 240418be Phillip Toohill
        }
131 32731215 Adam Menz
        
132 32731215 Adam Menz
        private void showToast(String message) {
133 32731215 Adam Menz
                Context context = getApplicationContext();
134 32731215 Adam Menz
                int duration = Toast.LENGTH_SHORT;
135 32731215 Adam Menz
                Toast toast = Toast.makeText(context, message, duration);
136 32731215 Adam Menz
                toast.show();
137 32731215 Adam Menz
    }
138 32731215 Adam Menz
        
139 240418be Phillip Toohill
        @Override
140 240418be Phillip Toohill
        protected Dialog onCreateDialog(int id) {
141 240418be Phillip Toohill
                switch (id) {
142 240418be Phillip Toohill
                case R.id.enable_button:
143 240418be Phillip Toohill
                        return new AlertDialog.Builder(EnableCDNActivity.this)
144 240418be Phillip Toohill
                                        .setIcon(R.drawable.alert_dialog_icon)
145 240418be Phillip Toohill
                                        .setTitle("Enable CDN")
146 240418be Phillip Toohill
                                        .setMessage(
147 240418be Phillip Toohill
                                                        "Are you sure you want to enable CDN on this container?")
148 240418be Phillip Toohill
                                        .setPositiveButton("Enable CDN",
149 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
150 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
151 240418be Phillip Toohill
                                                                                int whichButton) {
152 240418be Phillip Toohill
                                                                        // User clicked OK so do some stuff
153 4f5e4ef0 Adam Menz
                                                                        trackEvent(CATEGORY_CONTAINER, EVENT_UPDATED, "", -1);
154 240418be Phillip Toohill
                                                                        new EnableCDNTask().execute((Void[]) null);
155 240418be Phillip Toohill
                                                                }
156 240418be Phillip Toohill
                                                        })
157 240418be Phillip Toohill
                                        .setNegativeButton("Cancel",
158 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
159 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
160 240418be Phillip Toohill
                                                                                int whichButton) {
161 240418be Phillip Toohill
                                                                        // User clicked Cancel so do some stuff
162 240418be Phillip Toohill
                                                                }
163 240418be Phillip Toohill
                                                        }).create();
164 240418be Phillip Toohill
                case R.id.disable_button:
165 240418be Phillip Toohill
                        return new AlertDialog.Builder(EnableCDNActivity.this)
166 240418be Phillip Toohill
                                        .setIcon(R.drawable.alert_dialog_icon)
167 240418be Phillip Toohill
                                        .setTitle("Change Attributes")
168 240418be Phillip Toohill
                                        .setMessage(
169 240418be Phillip Toohill
                                                        "Are you sure you want to disable CDN, and/or change attributes?")
170 240418be Phillip Toohill
                                        .setPositiveButton("Change Attributes",
171 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
172 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
173 240418be Phillip Toohill
                                                                                int whichButton) {
174 240418be Phillip Toohill
                                                                        // User clicked OK so do some stuff
175 4f5e4ef0 Adam Menz
                                                                        trackEvent(CATEGORY_CONTAINER, EVENT_UPDATED, "", -1);
176 240418be Phillip Toohill
                                                                        new ChangeAttributesCDNTask().execute((Void[]) null);
177 240418be Phillip Toohill
                                                                }
178 240418be Phillip Toohill
                                                        })
179 240418be Phillip Toohill
                                        .setNegativeButton("Cancel",
180 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
181 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
182 240418be Phillip Toohill
                                                                                int whichButton) {
183 240418be Phillip Toohill
                                                                        // User clicked Cancel so do some stuff
184 240418be Phillip Toohill
                                                                }
185 240418be Phillip Toohill
                                                        }).create();
186 240418be Phillip Toohill
                }
187 240418be Phillip Toohill
                return null;
188 240418be Phillip Toohill
        }
189 a7d1fdc2 Adam Menz
        /*
190 240418be Phillip Toohill
        private void setActivityIndicatorsVisibility(int visibility) {
191 240418be Phillip Toohill
                ProgressBar pb = (ProgressBar) findViewById(R.id.save_container_progress_bar);
192 240418be Phillip Toohill
                TextView tv = (TextView) findViewById(R.id.saving_container_label);
193 240418be Phillip Toohill
                pb.setVisibility(visibility);
194 240418be Phillip Toohill
                tv.setVisibility(visibility);
195 240418be Phillip Toohill
        }
196 240418be Phillip Toohill

197 28dc0ca1 Phillip Toohill
        private void showActivityIndicators() {
198 240418be Phillip Toohill
                setActivityIndicatorsVisibility(View.VISIBLE);
199 240418be Phillip Toohill
        }
200 240418be Phillip Toohill

201 240418be Phillip Toohill
        private void hideActivityIndicators() {
202 240418be Phillip Toohill
                setActivityIndicatorsVisibility(View.INVISIBLE);
203 240418be Phillip Toohill
        }
204 a7d1fdc2 Adam Menz
        */
205 240418be Phillip Toohill
206 240418be Phillip Toohill
        // using CloudServersException, it works for us too
207 240418be Phillip Toohill
        private CloudServersException parseCloudServersException(
208 240418be Phillip Toohill
                        HttpResponse response) {
209 3d6041e8 Phillip Toohill
                CloudServersException cse = new CloudServersException();
210 3d6041e8 Phillip Toohill
                try {
211 240418be Phillip Toohill
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
212 240418be Phillip Toohill
                        String body = responseHandler.handleResponse(response);
213 240418be Phillip Toohill
                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
214 240418be Phillip Toohill
                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
215 240418be Phillip Toohill
                        XMLReader xmlReader = saxParser.getXMLReader();
216 240418be Phillip Toohill
                        xmlReader.setContentHandler(parser);
217 240418be Phillip Toohill
                        xmlReader.parse(new InputSource(new StringReader(body)));
218 240418be Phillip Toohill
                        cse = parser.getException();
219 3d6041e8 Phillip Toohill
                } catch (ClientProtocolException e) {
220 3d6041e8 Phillip Toohill
                        cse = new CloudServersException();
221 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
222 3d6041e8 Phillip Toohill
                } catch (IOException e) {
223 3d6041e8 Phillip Toohill
                        cse = new CloudServersException();
224 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
225 3d6041e8 Phillip Toohill
                } catch (ParserConfigurationException e) {
226 3d6041e8 Phillip Toohill
                        cse = new CloudServersException();
227 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
228 3d6041e8 Phillip Toohill
                } catch (SAXException e) {
229 3d6041e8 Phillip Toohill
                        cse = new CloudServersException();
230 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
231 3d6041e8 Phillip Toohill
                } catch (FactoryConfigurationError e) {
232 3d6041e8 Phillip Toohill
                        cse = new CloudServersException();
233 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
234 3d6041e8 Phillip Toohill
                }
235 3d6041e8 Phillip Toohill
                return cse;
236 240418be Phillip Toohill
        }
237 32731215 Adam Menz
        
238 32731215 Adam Menz
        private void startFileError(String message, HttpBundle bundle){
239 32731215 Adam Menz
                Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
240 32731215 Adam Menz
                viewIntent.putExtra("errorMessage", message);
241 32731215 Adam Menz
                viewIntent.putExtra("response", bundle.getResponseText());
242 32731215 Adam Menz
                viewIntent.putExtra("request", bundle.getCurlRequest());
243 32731215 Adam Menz
                startActivity(viewIntent);
244 32731215 Adam Menz
        }
245 240418be Phillip Toohill
246 32731215 Adam Menz
        public class EnableCDNTask extends AsyncTask<Void, Void, HttpBundle> {
247 240418be Phillip Toohill
                private CloudServersException exception;
248 240418be Phillip Toohill
249 240418be Phillip Toohill
                @Override
250 32731215 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
251 32731215 Adam Menz
                        HttpBundle bundle = null;
252 3d6041e8 Phillip Toohill
                        try {
253 32731215 Adam Menz
                                bundle = (new ContainerManager(context)).enable(containerName,
254 240418be Phillip Toohill
                                                selectedTtlId, selectedLogRetId);
255 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
256 3d6041e8 Phillip Toohill
                                exception = e;
257 3d6041e8 Phillip Toohill
                        }
258 32731215 Adam Menz
                        return bundle;
259 3d6041e8 Phillip Toohill
                }
260 240418be Phillip Toohill
261 3d6041e8 Phillip Toohill
                @Override
262 32731215 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
263 32731215 Adam Menz
                        HttpResponse response = bundle.getResponse();
264 3d6041e8 Phillip Toohill
                        if (response != null) {
265 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
266 3d6041e8 Phillip Toohill
                                if (statusCode == 201) {
267 3d6041e8 Phillip Toohill
                                        setResult(Activity.RESULT_OK);
268 3d6041e8 Phillip Toohill
                                        finish();
269 3d6041e8 Phillip Toohill
                                } else if (statusCode == 202) {
270 32731215 Adam Menz
                                        showToast("Accepted, container is already cdn enabled");
271 240418be Phillip Toohill
                                        finish();
272 240418be Phillip Toohill
                                } else {
273 3d6041e8 Phillip Toohill
                                        CloudServersException cse = parseCloudServersException(response);
274 3d6041e8 Phillip Toohill
                                        if ("".equals(cse.getMessage())) {
275 32731215 Adam Menz
                                                startFileError("There was a problem creating your container.", bundle);
276 3d6041e8 Phillip Toohill
                                        } else {
277 32731215 Adam Menz
                                                startFileError("There was a problem creating your container: "
278 240418be Phillip Toohill
                                                                                + cse.getMessage()
279 32731215 Adam Menz
                                                                                + " Check container name and try again", bundle);
280 3d6041e8 Phillip Toohill
                                        }
281 3d6041e8 Phillip Toohill
                                }
282 3d6041e8 Phillip Toohill
                        } else if (exception != null) {
283 32731215 Adam Menz
                                startFileError("There was a problem creating your container: "
284 240418be Phillip Toohill
                                                                + exception.getMessage()
285 32731215 Adam Menz
                                                                + " Check container name and try again", bundle);
286 240418be Phillip Toohill
                        }
287 3d6041e8 Phillip Toohill
                }
288 240418be Phillip Toohill
        }
289 240418be Phillip Toohill
290 32731215 Adam Menz
        public class ChangeAttributesCDNTask extends AsyncTask<Void, Void, HttpBundle> {
291 240418be Phillip Toohill
                private CloudServersException exception;
292 240418be Phillip Toohill
293 240418be Phillip Toohill
                @Override
294 32731215 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
295 32731215 Adam Menz
                        HttpBundle bundle = null;
296 3d6041e8 Phillip Toohill
                        try {
297 32731215 Adam Menz
                                bundle = (new ContainerManager(context)).disable(containerName,
298 240418be Phillip Toohill
                                                selectedCdnId, selectedTtlId, selectedLogRetId);
299 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
300 3d6041e8 Phillip Toohill
                                exception = e;
301 3d6041e8 Phillip Toohill
                        }
302 32731215 Adam Menz
                        return bundle;
303 3d6041e8 Phillip Toohill
                }
304 240418be Phillip Toohill
305 3d6041e8 Phillip Toohill
                @Override
306 32731215 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
307 32731215 Adam Menz
                        HttpResponse response = bundle.getResponse();
308 3d6041e8 Phillip Toohill
                        if (response != null) {
309 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
310 3d6041e8 Phillip Toohill
                                if (statusCode == 202) {
311 3d6041e8 Phillip Toohill
                                        setResult(Activity.RESULT_OK);
312 3d6041e8 Phillip Toohill
                                        finish();
313 3d6041e8 Phillip Toohill
                                } else {
314 3d6041e8 Phillip Toohill
                                        CloudServersException cse = parseCloudServersException(response);
315 3d6041e8 Phillip Toohill
                                        if ("".equals(cse.getMessage())) {
316 32731215 Adam Menz
                                                startFileError("There was a problem creating your container.", bundle);
317 3d6041e8 Phillip Toohill
                                        } else {
318 32731215 Adam Menz
                                                startFileError("There was a problem creating your container: "
319 240418be Phillip Toohill
                                                                                + cse.getMessage()
320 32731215 Adam Menz
                                                                                + " Check container name and try again", bundle);
321 3d6041e8 Phillip Toohill
                                        }
322 3d6041e8 Phillip Toohill
                                }
323 3d6041e8 Phillip Toohill
                        } else if (exception != null) {
324 32731215 Adam Menz
                                startFileError("There was a problem creating your container: "
325 240418be Phillip Toohill
                                                                + exception.getMessage()
326 32731215 Adam Menz
                                                                + " Check container name and try again", bundle);
327 240418be Phillip Toohill
                        }
328 3d6041e8 Phillip Toohill
                }
329 240418be Phillip Toohill
        }
330 240418be Phillip Toohill
331 3d6041e8 Phillip Toohill
        public void onClick(View v) {
332 3d6041e8 Phillip Toohill
                // TODO Auto-generated method stub
333 3d6041e8 Phillip Toohill
334 240418be Phillip Toohill
        }
335 3d6041e8 Phillip Toohill
336 240418be Phillip Toohill
}