root / src / com / rackspacecloud / android / EditLoadBalancerActivity.java @ 5018a7f8
History | View | Annotate | Download (8.4 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 |
trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_UPDATED, "", -1); |
110 |
new UpdateLoadBalancerTask().execute();
|
111 |
} else {
|
112 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
113 |
} |
114 |
} |
115 |
}); |
116 |
|
117 |
} |
118 |
|
119 |
private boolean validPort(){ |
120 |
Log.d("info", "selectedPort is " + selectedPort); |
121 |
try{
|
122 |
return (!selectedPort.equals("")) && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536; |
123 |
} catch (NumberFormatException nfe){ |
124 |
return false; |
125 |
} |
126 |
} |
127 |
|
128 |
private void loadProtocolSpinner() { |
129 |
protocolSpinner = (Spinner) findViewById(R.id.edit_protocol_spinner); |
130 |
protocolSpinner.setOnItemSelectedListener(this);
|
131 |
String protocolNames[] = new String[Protocol.getProtocols().size()]; |
132 |
protocols = new Protocol[Protocol.getProtocols().size()];
|
133 |
|
134 |
/*
|
135 |
* set the spinner to the current value
|
136 |
* so user doesnt have to remember
|
137 |
*/
|
138 |
int defaultPosition = 0; |
139 |
|
140 |
for(int i = 0; i < Protocol.getProtocols().size(); i++){ |
141 |
protocols[i] = Protocol.getProtocols().get(i); |
142 |
protocolNames[i] = Protocol.getProtocols().get(i).getName(); |
143 |
if(protocolNames[i].equals(selectedProtocol)){
|
144 |
defaultPosition = i; |
145 |
} |
146 |
} |
147 |
|
148 |
ArrayAdapter<String> protocolAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, protocolNames); |
149 |
protocolAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
150 |
protocolSpinner.setAdapter(protocolAdapter); |
151 |
protocolSpinner.setSelection(defaultPosition); |
152 |
} |
153 |
|
154 |
private void loadAlgorithmSpinner() { |
155 |
algorithmSpinner = (Spinner) findViewById(R.id.edit_algorithm_spinner); |
156 |
algorithmSpinner.setOnItemSelectedListener(this);
|
157 |
String algorithmNames[] = new String[Algorithm.getAlgorithms().size()]; |
158 |
algorithms = new Algorithm[Algorithm.getAlgorithms().size()];
|
159 |
|
160 |
/*
|
161 |
* set the spinner to the current value
|
162 |
* so user doesnt have to remember
|
163 |
*/
|
164 |
int defaultPosition = 0; |
165 |
|
166 |
for(int i = 0; i < Algorithm.getAlgorithms().size(); i++){ |
167 |
algorithms[i] = Algorithm.getAlgorithms().get(i); |
168 |
algorithmNames[i] = getPrettyAlgoName(Algorithm.getAlgorithms().get(i).getName()); |
169 |
if(algorithmNames[i].equals(getPrettyAlgoName(selectedAlgorithm))){
|
170 |
defaultPosition = i; |
171 |
} |
172 |
} |
173 |
|
174 |
ArrayAdapter<String> algorithmAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, algorithmNames); |
175 |
algorithmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
176 |
algorithmSpinner.setAdapter(algorithmAdapter); |
177 |
algorithmSpinner.setSelection(defaultPosition); |
178 |
} |
179 |
|
180 |
private String getPrettyAlgoName(String name){ |
181 |
if(name == null || name.length() == 0){ |
182 |
return ""; |
183 |
} else {
|
184 |
String result = name.charAt(0) + ""; |
185 |
boolean previousWasSpace = false;; |
186 |
for(int i = 1; i < name.length(); i++){ |
187 |
char curLetter = name.charAt(i);
|
188 |
if(curLetter == '_'){ |
189 |
result += " ";
|
190 |
previousWasSpace = true;
|
191 |
} else {
|
192 |
if(previousWasSpace){
|
193 |
result += Character.toUpperCase(curLetter);
|
194 |
} else {
|
195 |
result += Character.toLowerCase(curLetter);
|
196 |
} |
197 |
previousWasSpace = false;
|
198 |
} |
199 |
} |
200 |
return result;
|
201 |
} |
202 |
} |
203 |
|
204 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
205 |
if (parent == protocolSpinner) {
|
206 |
selectedProtocol = Protocol.getProtocols().get(position).getName(); |
207 |
} |
208 |
|
209 |
else if (parent == algorithmSpinner){ |
210 |
selectedAlgorithm = algorithms[position].getName(); |
211 |
} |
212 |
} |
213 |
|
214 |
public void onNothingSelected(AdapterView<?> parent) { |
215 |
|
216 |
} |
217 |
|
218 |
private class UpdateLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> { |
219 |
|
220 |
private CloudServersException exception;
|
221 |
|
222 |
@Override
|
223 |
protected void onPreExecute(){ |
224 |
showDialog(); |
225 |
} |
226 |
|
227 |
@Override
|
228 |
protected HttpBundle doInBackground(Void... arg0) { |
229 |
HttpBundle bundle = null;
|
230 |
try {
|
231 |
bundle = (new LoadBalancerManager(getContext())).update(loadBalancer, name.getText().toString(), selectedAlgorithm,
|
232 |
selectedProtocol, portText.getText().toString()); |
233 |
} catch (CloudServersException e) {
|
234 |
exception = e; |
235 |
} |
236 |
return bundle;
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
protected void onPostExecute(HttpBundle bundle) { |
241 |
hideDialog(); |
242 |
HttpResponse response = bundle.getResponse(); |
243 |
if (response != null) { |
244 |
int statusCode = response.getStatusLine().getStatusCode();
|
245 |
if(statusCode == 202){ |
246 |
setResult(RESULT_OK); |
247 |
finish(); |
248 |
} |
249 |
else{
|
250 |
CloudServersException cse = parseCloudServersException(response); |
251 |
if ("".equals(cse.getMessage())) { |
252 |
showError("There was a problem updating your load balancer.", bundle);
|
253 |
} else {
|
254 |
showError("There was a problem updating your load balancer: " + cse.getMessage(), bundle);
|
255 |
} |
256 |
} |
257 |
} else if (exception != null) { |
258 |
showError("There was a problem updating your load balancer: " + exception.getMessage(), bundle);
|
259 |
|
260 |
} |
261 |
|
262 |
} |
263 |
} |
264 |
|
265 |
|
266 |
/*
|
267 |
* for testing purposes
|
268 |
*/
|
269 |
@SuppressWarnings("unused") |
270 |
private void setSelectedPort(String s){ |
271 |
selectedPort = s; |
272 |
} |
273 |
|
274 |
@SuppressWarnings("unused") |
275 |
private String getSelectedProtocol(){ |
276 |
return selectedProtocol;
|
277 |
} |
278 |
|
279 |
@SuppressWarnings("unused") |
280 |
private String getSelectedAlgorithm(){ |
281 |
return selectedAlgorithm;
|
282 |
} |
283 |
|
284 |
} |