root / src / com / rackspacecloud / android / EditLoadBalancerActivity.java @ 48601850
History | View | Annotate | Download (8.4 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import org.apache.http.HttpResponse; |
4 |
|
5 |
import android.os.AsyncTask; |
6 |
import android.os.Bundle; |
7 |
import android.util.Log; |
8 |
import android.view.View; |
9 |
import android.view.View.OnClickListener; |
10 |
import android.widget.AdapterView; |
11 |
import android.widget.AdapterView.OnItemSelectedListener; |
12 |
import android.widget.ArrayAdapter; |
13 |
import android.widget.Button; |
14 |
import android.widget.EditText; |
15 |
import android.widget.Spinner; |
16 |
|
17 |
import com.rackspace.cloud.android.R; |
18 |
import com.rackspace.cloud.loadbalancer.api.client.Algorithm; |
19 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
20 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
21 |
import com.rackspace.cloud.loadbalancer.api.client.Protocol; |
22 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
23 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
24 |
|
25 |
public class EditLoadBalancerActivity extends CloudActivity implements OnItemSelectedListener { |
26 |
|
27 |
private LoadBalancer loadBalancer;
|
28 |
private Protocol[] protocols; |
29 |
private Algorithm[] algorithms; |
30 |
private Spinner protocolSpinner;
|
31 |
private Spinner algorithmSpinner;
|
32 |
private String selectedProtocol; |
33 |
private String selectedAlgorithm; |
34 |
private String selectedPort; |
35 |
private String selectedName; |
36 |
private EditText name;
|
37 |
private EditText portText;
|
38 |
|
39 |
@Override
|
40 |
public void onCreate(Bundle savedInstanceState) { |
41 |
super.onCreate(savedInstanceState);
|
42 |
loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer"); |
43 |
setContentView(R.layout.updateloadbalancer); |
44 |
restoreState(savedInstanceState); |
45 |
} |
46 |
|
47 |
@Override
|
48 |
protected void onSaveInstanceState(Bundle outState) { |
49 |
super.onSaveInstanceState(outState);
|
50 |
outState.putSerializable("loadBalancer", loadBalancer);
|
51 |
outState.putString("selectedProtocol", selectedProtocol);
|
52 |
outState.putString("selectedAlgorithm", selectedAlgorithm);
|
53 |
outState.putString("selectedPort", selectedPort);
|
54 |
outState.putString("selectedName", selectedName);
|
55 |
} |
56 |
|
57 |
protected void restoreState(Bundle state) { |
58 |
super.restoreState(state);
|
59 |
|
60 |
if (state != null && state.containsKey("loadBalancer")) { |
61 |
loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
|
62 |
} |
63 |
|
64 |
if (state != null && state.containsKey("selectedProtocol")) { |
65 |
selectedProtocol = state.getString("selectedProtocol");
|
66 |
} else {
|
67 |
selectedProtocol = loadBalancer.getProtocol(); |
68 |
} |
69 |
|
70 |
if (state != null && state.containsKey("selectedAlgorithm")) { |
71 |
selectedAlgorithm = state.getString("selectedAlgorithm");
|
72 |
} else {
|
73 |
selectedAlgorithm = loadBalancer.getAlgorithm(); |
74 |
} |
75 |
|
76 |
if (state != null && state.containsKey("selectedPort")) { |
77 |
selectedPort = state.getString("selectedPort");
|
78 |
} else {
|
79 |
selectedPort = loadBalancer.getPort(); |
80 |
} |
81 |
|
82 |
if (state != null && state.containsKey("selectedName")) { |
83 |
selectedName = state.getString("selectedName");
|
84 |
} else {
|
85 |
selectedName = loadBalancer.getName(); |
86 |
} |
87 |
|
88 |
setupInputs(); |
89 |
} |
90 |
|
91 |
private void setupInputs(){ |
92 |
loadProtocolSpinner(); |
93 |
loadAlgorithmSpinner(); |
94 |
|
95 |
name = (EditText) findViewById(R.id.edit_lb_name_text); |
96 |
name.setText(selectedName); |
97 |
|
98 |
portText = (EditText) findViewById(R.id.edit_port_text); |
99 |
portText.setText(selectedPort); |
100 |
|
101 |
Button submit = (Button)findViewById(R.id.update_lb_button); |
102 |
submit.setOnClickListener(new OnClickListener() {
|
103 |
|
104 |
@Override
|
105 |
public void onClick(View v) { |
106 |
|
107 |
selectedPort = portText.getText().toString(); |
108 |
|
109 |
if(validPort()) {
|
110 |
trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_UPDATED, "", -1); |
111 |
new UpdateLoadBalancerTask().execute();
|
112 |
} else {
|
113 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
114 |
} |
115 |
} |
116 |
}); |
117 |
|
118 |
} |
119 |
|
120 |
private boolean validPort(){ |
121 |
Log.d("info", "selectedPort is " + selectedPort); |
122 |
try{
|
123 |
return (!selectedPort.equals("")) && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536; |
124 |
} catch (NumberFormatException nfe){ |
125 |
return false; |
126 |
} |
127 |
} |
128 |
|
129 |
private void loadProtocolSpinner() { |
130 |
protocolSpinner = (Spinner) findViewById(R.id.edit_protocol_spinner); |
131 |
protocolSpinner.setOnItemSelectedListener(this);
|
132 |
String protocolNames[] = new String[Protocol.getProtocols().size()]; |
133 |
protocols = new Protocol[Protocol.getProtocols().size()];
|
134 |
|
135 |
/*
|
136 |
* set the spinner to the current value
|
137 |
* so user doesnt have to remember
|
138 |
*/
|
139 |
int defaultPosition = 0; |
140 |
|
141 |
for(int i = 0; i < Protocol.getProtocols().size(); i++){ |
142 |
protocols[i] = Protocol.getProtocols().get(i); |
143 |
protocolNames[i] = Protocol.getProtocols().get(i).getName(); |
144 |
if(protocolNames[i].equals(selectedProtocol)){
|
145 |
defaultPosition = i; |
146 |
} |
147 |
} |
148 |
|
149 |
ArrayAdapter<String> protocolAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, protocolNames); |
150 |
protocolAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
151 |
protocolSpinner.setAdapter(protocolAdapter); |
152 |
protocolSpinner.setSelection(defaultPosition); |
153 |
} |
154 |
|
155 |
private void loadAlgorithmSpinner() { |
156 |
algorithmSpinner = (Spinner) findViewById(R.id.edit_algorithm_spinner); |
157 |
algorithmSpinner.setOnItemSelectedListener(this);
|
158 |
String algorithmNames[] = new String[Algorithm.getAlgorithms().size()]; |
159 |
algorithms = new Algorithm[Algorithm.getAlgorithms().size()];
|
160 |
|
161 |
/*
|
162 |
* set the spinner to the current value
|
163 |
* so user doesnt have to remember
|
164 |
*/
|
165 |
int defaultPosition = 0; |
166 |
|
167 |
for(int i = 0; i < Algorithm.getAlgorithms().size(); i++){ |
168 |
algorithms[i] = Algorithm.getAlgorithms().get(i); |
169 |
algorithmNames[i] = getPrettyAlgoName(Algorithm.getAlgorithms().get(i).getName()); |
170 |
if(algorithmNames[i].equals(getPrettyAlgoName(selectedAlgorithm))){
|
171 |
defaultPosition = i; |
172 |
} |
173 |
} |
174 |
|
175 |
ArrayAdapter<String> algorithmAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, algorithmNames); |
176 |
algorithmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
177 |
algorithmSpinner.setAdapter(algorithmAdapter); |
178 |
algorithmSpinner.setSelection(defaultPosition); |
179 |
} |
180 |
|
181 |
private String getPrettyAlgoName(String name){ |
182 |
if(name == null || name.length() == 0){ |
183 |
return ""; |
184 |
} else {
|
185 |
String result = name.charAt(0) + ""; |
186 |
boolean previousWasSpace = false;; |
187 |
for(int i = 1; i < name.length(); i++){ |
188 |
char curLetter = name.charAt(i);
|
189 |
if(curLetter == '_'){ |
190 |
result += " ";
|
191 |
previousWasSpace = true;
|
192 |
} else {
|
193 |
if(previousWasSpace){
|
194 |
result += Character.toUpperCase(curLetter);
|
195 |
} else {
|
196 |
result += Character.toLowerCase(curLetter);
|
197 |
} |
198 |
previousWasSpace = false;
|
199 |
} |
200 |
} |
201 |
return result;
|
202 |
} |
203 |
} |
204 |
|
205 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
206 |
if (parent == protocolSpinner) {
|
207 |
selectedProtocol = Protocol.getProtocols().get(position).getName(); |
208 |
} |
209 |
|
210 |
else if (parent == algorithmSpinner){ |
211 |
selectedAlgorithm = algorithms[position].getName(); |
212 |
} |
213 |
} |
214 |
|
215 |
public void onNothingSelected(AdapterView<?> parent) { |
216 |
|
217 |
} |
218 |
|
219 |
private class UpdateLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> { |
220 |
|
221 |
private CloudServersException exception;
|
222 |
|
223 |
@Override
|
224 |
protected void onPreExecute(){ |
225 |
showDialog(); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
protected HttpBundle doInBackground(Void... arg0) { |
230 |
HttpBundle bundle = null;
|
231 |
try {
|
232 |
bundle = (new LoadBalancerManager(getContext())).update(loadBalancer, name.getText().toString(), selectedAlgorithm,
|
233 |
selectedProtocol, portText.getText().toString()); |
234 |
} catch (CloudServersException e) {
|
235 |
exception = e; |
236 |
} |
237 |
return bundle;
|
238 |
} |
239 |
|
240 |
@Override
|
241 |
protected void onPostExecute(HttpBundle bundle) { |
242 |
hideDialog(); |
243 |
HttpResponse response = bundle.getResponse(); |
244 |
if (response != null) { |
245 |
int statusCode = response.getStatusLine().getStatusCode();
|
246 |
if(statusCode == 202){ |
247 |
setResult(RESULT_OK); |
248 |
finish(); |
249 |
} |
250 |
else{
|
251 |
CloudServersException cse = parseCloudServersException(response); |
252 |
if ("".equals(cse.getMessage())) { |
253 |
showError("There was a problem updating your load balancer.", bundle);
|
254 |
} else {
|
255 |
showError("There was a problem updating your load balancer: " + cse.getMessage(), bundle);
|
256 |
} |
257 |
} |
258 |
} else if (exception != null) { |
259 |
showError("There was a problem updating your load balancer: " + exception.getMessage(), bundle);
|
260 |
|
261 |
} |
262 |
|
263 |
} |
264 |
} |
265 |
|
266 |
|
267 |
/*
|
268 |
* for testing purposes
|
269 |
*/
|
270 |
@SuppressWarnings("unused") |
271 |
private void setSelectedPort(String s){ |
272 |
selectedPort = s; |
273 |
} |
274 |
|
275 |
@SuppressWarnings("unused") |
276 |
private String getSelectedProtocol(){ |
277 |
return selectedProtocol;
|
278 |
} |
279 |
|
280 |
@SuppressWarnings("unused") |
281 |
private String getSelectedAlgorithm(){ |
282 |
return selectedAlgorithm;
|
283 |
} |
284 |
|
285 |
} |