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