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