root / src / com / rackspacecloud / android / EditNodeActivity.java @ 5018a7f8
History | View | Annotate | Download (8.1 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import org.apache.http.HttpResponse; |
4 |
|
5 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
6 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
7 |
import com.rackspace.cloud.loadbalancer.api.client.NodeManager; |
8 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
9 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
10 |
import android.app.Activity; |
11 |
import android.app.AlertDialog; |
12 |
import android.app.Dialog; |
13 |
import android.content.DialogInterface; |
14 |
import android.content.Intent; |
15 |
import android.os.AsyncTask; |
16 |
import android.os.Bundle; |
17 |
import android.view.View; |
18 |
import android.view.View.OnClickListener; |
19 |
import android.view.Window; |
20 |
import android.widget.AdapterView; |
21 |
import android.widget.AdapterView.OnItemSelectedListener; |
22 |
import android.widget.ArrayAdapter; |
23 |
import android.widget.Button; |
24 |
import android.widget.EditText; |
25 |
import android.widget.Spinner; |
26 |
import android.widget.TextView; |
27 |
|
28 |
public class EditNodeActivity extends CloudActivity{ |
29 |
|
30 |
private final String[] CONDITIONS = {"Enabled", "Disabled", "Draining"}; |
31 |
private final int NODE_DELETED_CODE = 389; |
32 |
|
33 |
private Spinner conditionSpinner;
|
34 |
private EditText weightText;
|
35 |
private String selectedCondition; |
36 |
private String selectedWeight; |
37 |
private LoadBalancer loadBalancer;
|
38 |
private Node node;
|
39 |
|
40 |
public void onCreate(Bundle savedInstanceState) { |
41 |
super.onCreate(savedInstanceState);
|
42 |
trackPageView(GoogleAnalytics.PAGE_LB_NODE); |
43 |
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
44 |
setContentView(R.layout.editnode); |
45 |
node = (Node) this.getIntent().getExtras().get("node"); |
46 |
loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer"); |
47 |
restoreState(savedInstanceState); |
48 |
} |
49 |
|
50 |
protected void restoreState(Bundle state){ |
51 |
super.restoreState(state);
|
52 |
loadData(); |
53 |
setUpButtons(); |
54 |
} |
55 |
|
56 |
private void setUpButtons(){ |
57 |
Button submit = (Button) findViewById(R.id.edit_node_button); |
58 |
submit.setOnClickListener(new OnClickListener() {
|
59 |
|
60 |
@Override
|
61 |
public void onClick(View v) { |
62 |
selectedWeight = weightText.getText().toString(); |
63 |
if(weightText.getVisibility() == View.GONE || (weightText.getVisibility() != View.GONE && validWeight(selectedWeight))){ |
64 |
trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_UPDATED_NODE, "", -1); |
65 |
new ModifyNodeTask().execute();
|
66 |
} |
67 |
else{
|
68 |
showAlert("Error", "Weight must be between 1 and 100."); |
69 |
} |
70 |
} |
71 |
}); |
72 |
|
73 |
Button delete = (Button) findViewById(R.id.delete_node_button); |
74 |
delete.setOnClickListener(new OnClickListener() {
|
75 |
|
76 |
@Override
|
77 |
public void onClick(View v) { |
78 |
showDialog(R.id.delete_node_button); |
79 |
} |
80 |
}); |
81 |
} |
82 |
|
83 |
@Override
|
84 |
protected Dialog onCreateDialog(int id) { |
85 |
switch (id) {
|
86 |
case R.id.delete_node_button:
|
87 |
return new AlertDialog.Builder(EditNodeActivity.this) |
88 |
.setIcon(R.drawable.alert_dialog_icon) |
89 |
.setTitle("Remove Node")
|
90 |
.setMessage("Are you sure you want to remove this node?")
|
91 |
.setPositiveButton("Remove", new DialogInterface.OnClickListener() { |
92 |
public void onClick(DialogInterface dialog, int whichButton) { |
93 |
// User clicked OK so do some stuff
|
94 |
trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_DELETE_NODE, "", -1); |
95 |
new DeleteNodeTask().execute((Void[]) null); |
96 |
} |
97 |
}) |
98 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
99 |
public void onClick(DialogInterface dialog, int whichButton) { |
100 |
// User clicked Cancel so do some stuff
|
101 |
} |
102 |
}) |
103 |
.create(); |
104 |
} |
105 |
return null; |
106 |
} |
107 |
|
108 |
public void onBackPressed(){ |
109 |
setResult(RESULT_CANCELED); |
110 |
finish(); |
111 |
} |
112 |
|
113 |
private void loadData(){ |
114 |
loadConditionSpinner(); |
115 |
|
116 |
TextView ipText = (TextView) findViewById(R.id.node_ip_text); |
117 |
ipText.setText(node.getAddress()); |
118 |
|
119 |
TextView portText = (TextView) findViewById(R.id.node_port_text); |
120 |
portText.setText(node.getPort()); |
121 |
|
122 |
weightText = (EditText) findViewById(R.id.node_weight_text); |
123 |
//if algorithm is not weighted then then node's weight will be null
|
124 |
if(node.getWeight() == null){ |
125 |
TextView weightLabel = (TextView) findViewById(R.id.node_weight_label); |
126 |
weightLabel.setVisibility(View.GONE);
|
127 |
weightText.setVisibility(View.GONE);
|
128 |
} |
129 |
else{
|
130 |
weightText.setText(node.getWeight()); |
131 |
} |
132 |
} |
133 |
|
134 |
|
135 |
private Boolean validWeight(String weight){ |
136 |
if(weight.equals("")){ |
137 |
return false; |
138 |
} |
139 |
else{
|
140 |
try{
|
141 |
int w = Integer.valueOf(weight); |
142 |
return w >= 1 && w <= 100 ; |
143 |
} catch (NumberFormatException nfe){ |
144 |
return false; |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
private void loadConditionSpinner(){ |
150 |
conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner); |
151 |
|
152 |
ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS); |
153 |
conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
154 |
conditionSpinner.setAdapter(conditionAdapter); |
155 |
|
156 |
conditionSpinner.setSelection(getSpinnerLocation(node.getCondition())); |
157 |
|
158 |
conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
|
159 |
|
160 |
@Override
|
161 |
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { |
162 |
selectedCondition = CONDITIONS[pos]; |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public void onNothingSelected(AdapterView<?> arg0) { |
167 |
|
168 |
} |
169 |
|
170 |
}); |
171 |
} |
172 |
|
173 |
private int getSpinnerLocation(String condition){ |
174 |
for(int i = 0; i < CONDITIONS.length; i++){ |
175 |
if(CONDITIONS[i].equalsIgnoreCase(condition)){
|
176 |
return i;
|
177 |
} |
178 |
} |
179 |
return 0; |
180 |
} |
181 |
|
182 |
public class ModifyNodeTask extends AsyncTask<Void, Void, HttpBundle> { |
183 |
|
184 |
private CloudServersException exception;
|
185 |
|
186 |
@Override
|
187 |
protected void onPreExecute(){ |
188 |
showDialog(); |
189 |
} |
190 |
|
191 |
@Override
|
192 |
protected HttpBundle doInBackground(Void... arg0) { |
193 |
HttpBundle bundle = null;
|
194 |
try {
|
195 |
bundle = (new NodeManager(getContext())).update(loadBalancer, node, selectedCondition, selectedWeight);
|
196 |
} catch (CloudServersException e) {
|
197 |
exception = e; |
198 |
} |
199 |
return bundle;
|
200 |
} |
201 |
|
202 |
@Override
|
203 |
protected void onPostExecute(HttpBundle bundle) { |
204 |
hideDialog(); |
205 |
HttpResponse response = bundle.getResponse(); |
206 |
if (response != null) { |
207 |
int statusCode = response.getStatusLine().getStatusCode();
|
208 |
if (statusCode == 202 || statusCode == 200) { |
209 |
setResult(Activity.RESULT_OK); |
210 |
finish(); |
211 |
} else {
|
212 |
CloudServersException cse = parseCloudServersException(response); |
213 |
if ("".equals(cse.getMessage())) { |
214 |
showError("There was a problem modifying your load balancer.", bundle);
|
215 |
} else {
|
216 |
showError("There was a problem modifying your load balancer: " + cse.getMessage(), bundle);
|
217 |
} |
218 |
} |
219 |
} else if (exception != null) { |
220 |
showError("There was a problem modifying your load balancer: " + exception.getMessage(), bundle);
|
221 |
} |
222 |
} |
223 |
} |
224 |
|
225 |
public class DeleteNodeTask extends AsyncTask<Void, Void, HttpBundle> { |
226 |
|
227 |
private CloudServersException exception;
|
228 |
|
229 |
@Override
|
230 |
protected void onPreExecute(){ |
231 |
showDialog(); |
232 |
} |
233 |
|
234 |
@Override
|
235 |
protected HttpBundle doInBackground(Void... arg0) { |
236 |
HttpBundle bundle = null;
|
237 |
try {
|
238 |
bundle = (new NodeManager(getContext())).remove(loadBalancer, node);
|
239 |
} catch (CloudServersException e) {
|
240 |
exception = e; |
241 |
} |
242 |
return bundle;
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
protected void onPostExecute(HttpBundle bundle) { |
247 |
hideDialog(); |
248 |
HttpResponse response = bundle.getResponse(); |
249 |
if (response != null) { |
250 |
int statusCode = response.getStatusLine().getStatusCode();
|
251 |
if (statusCode == 202 || statusCode == 200) { |
252 |
Intent viewIntent = new Intent();
|
253 |
viewIntent.putExtra("deletedNode", node);
|
254 |
setResult(NODE_DELETED_CODE, viewIntent); |
255 |
finish(); |
256 |
} else {
|
257 |
CloudServersException cse = parseCloudServersException(response); |
258 |
if ("".equals(cse.getMessage())) { |
259 |
showError("There was a problem modifying your load balancer.", bundle);
|
260 |
} else {
|
261 |
showError("There was a problem modifying your load balancer: " + cse.getMessage(), bundle);
|
262 |
} |
263 |
} |
264 |
} else if (exception != null) { |
265 |
showError("There was a problem modifying your load balancer: " + exception.getMessage(), bundle);
|
266 |
} |
267 |
} |
268 |
} |
269 |
|
270 |
/*
|
271 |
* For testing
|
272 |
*/
|
273 |
@SuppressWarnings("unused") |
274 |
private void setSelectedWeight(String s){ |
275 |
selectedWeight = s; |
276 |
} |
277 |
} |