Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / EnableCDNActivity.java @ f65ce369

History | View | Annotate | Download (8.2 kB)

1 7dbfc514 koutsoub
package com.rackspace.cloud.android;
2 3d6041e8 Phillip Toohill
3 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
4 3d6041e8 Phillip Toohill
5 3d6041e8 Phillip Toohill
import android.app.Activity;
6 3d6041e8 Phillip Toohill
import android.app.AlertDialog;
7 3d6041e8 Phillip Toohill
import android.app.Dialog;
8 3d6041e8 Phillip Toohill
import android.content.DialogInterface;
9 3d6041e8 Phillip Toohill
import android.os.AsyncTask;
10 3d6041e8 Phillip Toohill
import android.os.Bundle;
11 3d6041e8 Phillip Toohill
import android.view.View;
12 3d6041e8 Phillip Toohill
import android.view.View.OnClickListener;
13 3d6041e8 Phillip Toohill
import android.widget.AdapterView;
14 48601850 Mike Mayo
import android.widget.AdapterView.OnItemSelectedListener;
15 3d6041e8 Phillip Toohill
import android.widget.ArrayAdapter;
16 3d6041e8 Phillip Toohill
import android.widget.Button;
17 3d6041e8 Phillip Toohill
import android.widget.Spinner;
18 3d6041e8 Phillip Toohill
19 48601850 Mike Mayo
import com.rackspace.cloud.android.R;
20 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerManager;
21 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
22 32731215 Adam Menz
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
23 3d6041e8 Phillip Toohill
24 6b8dad86 Adam Menz
public class EnableCDNActivity extends CloudActivity implements OnClickListener,
25 240418be Phillip Toohill
                OnItemSelectedListener {
26 3d6041e8 Phillip Toohill
27 3d6041e8 Phillip Toohill
        public static String containerName = null;
28 3d6041e8 Phillip Toohill
        private String selectedTtlId;
29 3d6041e8 Phillip Toohill
        private String selectedLogRetId;
30 3d6041e8 Phillip Toohill
        private String selectedCdnId;
31 3d6041e8 Phillip Toohill
        private Spinner ttlSpinner;
32 3d6041e8 Phillip Toohill
        private Spinner logRetSpinner;
33 3d6041e8 Phillip Toohill
        private Spinner cdnSpinner;
34 b347d5e3 Chmouel Boudjnah
        
35 240418be Phillip Toohill
        /** Called when the activity is first created. */
36 240418be Phillip Toohill
        @Override
37 240418be Phillip Toohill
        public void onCreate(Bundle savedInstanceState) {
38 240418be Phillip Toohill
                super.onCreate(savedInstanceState);
39 5018a7f8 Adam Menz
                trackPageView(GoogleAnalytics.PAGE_CONTAINER_DETAILS);
40 240418be Phillip Toohill
                setContentView(R.layout.enable_cdn_container);
41 240418be Phillip Toohill
                containerName = (String) this.getIntent().getExtras().get("Cname");
42 6b8dad86 Adam Menz
                restoreState(savedInstanceState);
43 6b8dad86 Adam Menz
        }
44 6b8dad86 Adam Menz
        
45 6b8dad86 Adam Menz
        protected void restoreState(Bundle state){
46 6b8dad86 Adam Menz
                super.restoreState(state);
47 240418be Phillip Toohill
                setupButtons();
48 240418be Phillip Toohill
                loadTtlSpinner();
49 3d6041e8 Phillip Toohill
                loadLogRetSpinner();
50 3d6041e8 Phillip Toohill
                loadCdnSpinner();
51 240418be Phillip Toohill
        }
52 240418be Phillip Toohill
53 240418be Phillip Toohill
        private void setupButton(int resourceId, OnClickListener onClickListener) {
54 3d6041e8 Phillip Toohill
                Button button = (Button) findViewById(resourceId);
55 3d6041e8 Phillip Toohill
                button.setOnClickListener(onClickListener);
56 240418be Phillip Toohill
        }
57 240418be Phillip Toohill
58 240418be Phillip Toohill
        private void setupButtons() {
59 240418be Phillip Toohill
                setupButton(R.id.enable_button, new OnClickListener() {
60 240418be Phillip Toohill
                        public void onClick(View v) {
61 240418be Phillip Toohill
                                showDialog(R.id.enable_button);
62 240418be Phillip Toohill
                        }
63 240418be Phillip Toohill
                });
64 240418be Phillip Toohill
65 240418be Phillip Toohill
                setupButton(R.id.disable_button, new OnClickListener() {
66 240418be Phillip Toohill
                        public void onClick(View v) {
67 240418be Phillip Toohill
                                showDialog(R.id.disable_button);
68 240418be Phillip Toohill
                        }
69 240418be Phillip Toohill
                });
70 240418be Phillip Toohill
        }
71 240418be Phillip Toohill
72 240418be Phillip Toohill
        private void loadCdnSpinner() {
73 240418be Phillip Toohill
                cdnSpinner = (Spinner) findViewById(R.id.cdn_spinner);
74 240418be Phillip Toohill
                cdnSpinner.setOnItemSelectedListener(this);
75 240418be Phillip Toohill
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
76 240418be Phillip Toohill
                                this, R.array.cdn, android.R.layout.simple_spinner_item);
77 240418be Phillip Toohill
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
78 240418be Phillip Toohill
                cdnSpinner.setAdapter(adapter);
79 240418be Phillip Toohill
80 240418be Phillip Toohill
        }
81 240418be Phillip Toohill
82 240418be Phillip Toohill
        private void loadTtlSpinner() {
83 240418be Phillip Toohill
                ttlSpinner = (Spinner) findViewById(R.id.ttl_spinner);
84 240418be Phillip Toohill
                ttlSpinner.setOnItemSelectedListener(this);
85 240418be Phillip Toohill
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
86 240418be Phillip Toohill
                                this, R.array.ttl, android.R.layout.simple_spinner_item);
87 240418be Phillip Toohill
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
88 240418be Phillip Toohill
                ttlSpinner.setAdapter(adapter);
89 240418be Phillip Toohill
90 240418be Phillip Toohill
        }
91 240418be Phillip Toohill
92 240418be Phillip Toohill
        private void loadLogRetSpinner() {
93 240418be Phillip Toohill
                logRetSpinner = (Spinner) findViewById(R.id.log_retention_spinner);
94 240418be Phillip Toohill
                logRetSpinner.setOnItemSelectedListener(this);
95 240418be Phillip Toohill
                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
96 240418be Phillip Toohill
                                this, R.array.logRet, android.R.layout.simple_spinner_item);
97 240418be Phillip Toohill
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
98 240418be Phillip Toohill
                logRetSpinner.setAdapter(adapter);
99 240418be Phillip Toohill
100 240418be Phillip Toohill
        }
101 240418be Phillip Toohill
102 240418be Phillip Toohill
        public void onItemSelected(AdapterView<?> parent, View view, int id,
103 240418be Phillip Toohill
                        long arg3) {
104 240418be Phillip Toohill
                if (parent == cdnSpinner) {
105 240418be Phillip Toohill
                        selectedCdnId = cdnSpinner.getSelectedItem().toString();
106 240418be Phillip Toohill
                }
107 3d6041e8 Phillip Toohill
                if (parent == ttlSpinner) {
108 3d6041e8 Phillip Toohill
                        selectedTtlId = ttlSpinner.getSelectedItem().toString();
109 3d6041e8 Phillip Toohill
                } else if (parent == logRetSpinner) {
110 3d6041e8 Phillip Toohill
                        selectedLogRetId = logRetSpinner.getSelectedItem().toString();
111 3d6041e8 Phillip Toohill
                }
112 240418be Phillip Toohill
        }
113 240418be Phillip Toohill
114 240418be Phillip Toohill
        public void onNothingSelected(AdapterView<?> parent) {
115 240418be Phillip Toohill
116 240418be Phillip Toohill
        }
117 32731215 Adam Menz
        
118 240418be Phillip Toohill
        @Override
119 240418be Phillip Toohill
        protected Dialog onCreateDialog(int id) {
120 240418be Phillip Toohill
                switch (id) {
121 240418be Phillip Toohill
                case R.id.enable_button:
122 240418be Phillip Toohill
                        return new AlertDialog.Builder(EnableCDNActivity.this)
123 240418be Phillip Toohill
                                        .setIcon(R.drawable.alert_dialog_icon)
124 240418be Phillip Toohill
                                        .setTitle("Enable CDN")
125 240418be Phillip Toohill
                                        .setMessage(
126 240418be Phillip Toohill
                                                        "Are you sure you want to enable CDN on this container?")
127 240418be Phillip Toohill
                                        .setPositiveButton("Enable CDN",
128 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
129 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
130 240418be Phillip Toohill
                                                                                int whichButton) {
131 240418be Phillip Toohill
                                                                        // User clicked OK so do some stuff
132 5018a7f8 Adam Menz
                                                                        trackEvent(GoogleAnalytics.CATEGORY_CONTAINER, GoogleAnalytics.EVENT_UPDATED, "", -1);
133 240418be Phillip Toohill
                                                                        new EnableCDNTask().execute((Void[]) null);
134 240418be Phillip Toohill
                                                                }
135 240418be Phillip Toohill
                                                        })
136 240418be Phillip Toohill
                                        .setNegativeButton("Cancel",
137 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
138 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
139 240418be Phillip Toohill
                                                                                int whichButton) {
140 240418be Phillip Toohill
                                                                        // User clicked Cancel so do some stuff
141 240418be Phillip Toohill
                                                                }
142 240418be Phillip Toohill
                                                        }).create();
143 240418be Phillip Toohill
                case R.id.disable_button:
144 240418be Phillip Toohill
                        return new AlertDialog.Builder(EnableCDNActivity.this)
145 240418be Phillip Toohill
                                        .setIcon(R.drawable.alert_dialog_icon)
146 240418be Phillip Toohill
                                        .setTitle("Change Attributes")
147 240418be Phillip Toohill
                                        .setMessage(
148 240418be Phillip Toohill
                                                        "Are you sure you want to disable CDN, and/or change attributes?")
149 240418be Phillip Toohill
                                        .setPositiveButton("Change Attributes",
150 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
151 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
152 240418be Phillip Toohill
                                                                                int whichButton) {
153 240418be Phillip Toohill
                                                                        // User clicked OK so do some stuff
154 5018a7f8 Adam Menz
                                                                        trackEvent(GoogleAnalytics.CATEGORY_CONTAINER, GoogleAnalytics.EVENT_UPDATED, "", -1);
155 240418be Phillip Toohill
                                                                        new ChangeAttributesCDNTask().execute((Void[]) null);
156 240418be Phillip Toohill
                                                                }
157 240418be Phillip Toohill
                                                        })
158 240418be Phillip Toohill
                                        .setNegativeButton("Cancel",
159 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
160 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
161 240418be Phillip Toohill
                                                                                int whichButton) {
162 240418be Phillip Toohill
                                                                        // User clicked Cancel so do some stuff
163 240418be Phillip Toohill
                                                                }
164 240418be Phillip Toohill
                                                        }).create();
165 240418be Phillip Toohill
                }
166 240418be Phillip Toohill
                return null;
167 240418be Phillip Toohill
        }
168 32731215 Adam Menz
        
169 32731215 Adam Menz
        public class EnableCDNTask extends AsyncTask<Void, Void, HttpBundle> {
170 240418be Phillip Toohill
                private CloudServersException exception;
171 240418be Phillip Toohill
172 240418be Phillip Toohill
                @Override
173 32731215 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
174 32731215 Adam Menz
                        HttpBundle bundle = null;
175 3d6041e8 Phillip Toohill
                        try {
176 6b8dad86 Adam Menz
                                bundle = (new ContainerManager(getContext())).enable(containerName,
177 240418be Phillip Toohill
                                                selectedTtlId, selectedLogRetId);
178 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
179 3d6041e8 Phillip Toohill
                                exception = e;
180 3d6041e8 Phillip Toohill
                        }
181 32731215 Adam Menz
                        return bundle;
182 3d6041e8 Phillip Toohill
                }
183 240418be Phillip Toohill
184 3d6041e8 Phillip Toohill
                @Override
185 32731215 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
186 32731215 Adam Menz
                        HttpResponse response = bundle.getResponse();
187 3d6041e8 Phillip Toohill
                        if (response != null) {
188 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
189 3d6041e8 Phillip Toohill
                                if (statusCode == 201) {
190 3d6041e8 Phillip Toohill
                                        setResult(Activity.RESULT_OK);
191 3d6041e8 Phillip Toohill
                                        finish();
192 3d6041e8 Phillip Toohill
                                } else if (statusCode == 202) {
193 32731215 Adam Menz
                                        showToast("Accepted, container is already cdn enabled");
194 240418be Phillip Toohill
                                        finish();
195 240418be Phillip Toohill
                                } else {
196 3d6041e8 Phillip Toohill
                                        CloudServersException cse = parseCloudServersException(response);
197 3d6041e8 Phillip Toohill
                                        if ("".equals(cse.getMessage())) {
198 6b8dad86 Adam Menz
                                                showError("There was a problem creating your container.", bundle);
199 3d6041e8 Phillip Toohill
                                        } else {
200 6b8dad86 Adam Menz
                                                showError("There was a problem creating your container: "
201 240418be Phillip Toohill
                                                                                + cse.getMessage()
202 32731215 Adam Menz
                                                                                + " Check container name and try again", bundle);
203 3d6041e8 Phillip Toohill
                                        }
204 3d6041e8 Phillip Toohill
                                }
205 3d6041e8 Phillip Toohill
                        } else if (exception != null) {
206 6b8dad86 Adam Menz
                                showError("There was a problem creating your container: "
207 240418be Phillip Toohill
                                                                + exception.getMessage()
208 32731215 Adam Menz
                                                                + " Check container name and try again", bundle);
209 240418be Phillip Toohill
                        }
210 3d6041e8 Phillip Toohill
                }
211 240418be Phillip Toohill
        }
212 240418be Phillip Toohill
213 32731215 Adam Menz
        public class ChangeAttributesCDNTask extends AsyncTask<Void, Void, HttpBundle> {
214 240418be Phillip Toohill
                private CloudServersException exception;
215 240418be Phillip Toohill
216 240418be Phillip Toohill
                @Override
217 32731215 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
218 32731215 Adam Menz
                        HttpBundle bundle = null;
219 3d6041e8 Phillip Toohill
                        try {
220 6b8dad86 Adam Menz
                                bundle = (new ContainerManager(getContext())).disable(containerName,
221 240418be Phillip Toohill
                                                selectedCdnId, selectedTtlId, selectedLogRetId);
222 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
223 3d6041e8 Phillip Toohill
                                exception = e;
224 3d6041e8 Phillip Toohill
                        }
225 32731215 Adam Menz
                        return bundle;
226 3d6041e8 Phillip Toohill
                }
227 240418be Phillip Toohill
228 3d6041e8 Phillip Toohill
                @Override
229 32731215 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
230 32731215 Adam Menz
                        HttpResponse response = bundle.getResponse();
231 3d6041e8 Phillip Toohill
                        if (response != null) {
232 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
233 3d6041e8 Phillip Toohill
                                if (statusCode == 202) {
234 3d6041e8 Phillip Toohill
                                        setResult(Activity.RESULT_OK);
235 3d6041e8 Phillip Toohill
                                        finish();
236 3d6041e8 Phillip Toohill
                                } else {
237 3d6041e8 Phillip Toohill
                                        CloudServersException cse = parseCloudServersException(response);
238 3d6041e8 Phillip Toohill
                                        if ("".equals(cse.getMessage())) {
239 6b8dad86 Adam Menz
                                                showError("There was a problem creating your container.", bundle);
240 3d6041e8 Phillip Toohill
                                        } else {
241 6b8dad86 Adam Menz
                                                showError("There was a problem creating your container: "
242 240418be Phillip Toohill
                                                                                + cse.getMessage()
243 32731215 Adam Menz
                                                                                + " Check container name and try again", bundle);
244 3d6041e8 Phillip Toohill
                                        }
245 3d6041e8 Phillip Toohill
                                }
246 3d6041e8 Phillip Toohill
                        } else if (exception != null) {
247 6b8dad86 Adam Menz
                                showError("There was a problem creating your container: "
248 240418be Phillip Toohill
                                                                + exception.getMessage()
249 32731215 Adam Menz
                                                                + " Check container name and try again", bundle);
250 240418be Phillip Toohill
                        }
251 3d6041e8 Phillip Toohill
                }
252 240418be Phillip Toohill
        }
253 240418be Phillip Toohill
254 3d6041e8 Phillip Toohill
        public void onClick(View v) {
255 3d6041e8 Phillip Toohill
                // TODO Auto-generated method stub
256 3d6041e8 Phillip Toohill
257 240418be Phillip Toohill
        }
258 3d6041e8 Phillip Toohill
259 240418be Phillip Toohill
}