Revision c4a37180 src/com/rackspacecloud/android/AddMoreNodesActivity.java
b/src/com/rackspacecloud/android/AddMoreNodesActivity.java | ||
---|---|---|
34 | 34 |
|
35 | 35 |
private static final int ADD_NODE_CODE = 178; |
36 | 36 |
private static final int ADD_EXTERNAL_NODE_CODE = 188; |
37 |
private ArrayList<Server> servers; |
|
37 |
private ArrayList<Server> possibleNodes; |
|
38 |
//the last position in the listview that was clicked |
|
38 | 39 |
private int lastCheckedPos; |
40 |
//the location in nodes of the last node that was clicked |
|
41 |
private int positionOfNode; |
|
39 | 42 |
private ArrayList<Node> nodes; |
40 | 43 |
private ArrayList<Node> nodesToAdd; |
41 | 44 |
private LoadBalancer loadBalancer; |
... | ... | |
57 | 60 |
outState.putSerializable("nodes", nodes); |
58 | 61 |
outState.putSerializable("loadBalancer", loadBalancer); |
59 | 62 |
outState.putSerializable("nodesToAdd", nodesToAdd); |
60 |
outState.putSerializable("servers", servers); |
|
63 |
outState.putSerializable("possibleNodes", possibleNodes); |
|
64 |
outState.putInt("lastCheckedPos", lastCheckedPos); |
|
65 |
outState.putInt("positionOfNode", positionOfNode); |
|
61 | 66 |
} |
62 | 67 |
|
63 | 68 |
@SuppressWarnings("unchecked") |
... | ... | |
78 | 83 |
} |
79 | 84 |
} |
80 | 85 |
|
86 |
if (state != null && state.containsKey("lastCheckedPos")){ |
|
87 |
lastCheckedPos = (Integer) state.getSerializable("lastCheckedPos"); |
|
88 |
} |
|
89 |
|
|
90 |
if (state != null && state.containsKey("positionOfNode")){ |
|
91 |
positionOfNode = (Integer) state.getSerializable("positionOfNode"); |
|
92 |
} |
|
93 |
|
|
81 | 94 |
if (state != null && state.containsKey("nodesToAdd")){ |
82 | 95 |
nodesToAdd = (ArrayList<Node>) state.getSerializable("nodesToAdd"); |
83 | 96 |
} |
... | ... | |
85 | 98 |
nodesToAdd = new ArrayList<Node>(); |
86 | 99 |
} |
87 | 100 |
|
88 |
if (state != null && state.containsKey("servers") && state.getSerializable("servers") != null) {
|
|
89 |
servers = (ArrayList<Server>) state.getSerializable("servers");
|
|
90 |
if (servers.size() == 0) {
|
|
101 |
if (state != null && state.containsKey("possibleNodes") && state.getSerializable("possibleNodes") != null) {
|
|
102 |
possibleNodes = (ArrayList<Server>) state.getSerializable("servers");
|
|
103 |
if (possibleNodes.size() == 0) {
|
|
91 | 104 |
displayNoServersCell(); |
92 | 105 |
} else { |
93 | 106 |
getListView().setDividerHeight(1); // restore divider lines |
... | ... | |
115 | 128 |
|
116 | 129 |
@Override |
117 | 130 |
public void onClick(View v) { |
131 |
positionOfNode = -1; |
|
118 | 132 |
Intent viewIntent = new Intent(getContext(), AddExternalNodeActivity.class); |
119 |
viewIntent.putExtra("weighted", false);
|
|
133 |
viewIntent.putExtra("weighted", loadBalancer.getAlgorithm().toLowerCase().contains("weighted"));
|
|
120 | 134 |
startActivityForResult(viewIntent, ADD_EXTERNAL_NODE_CODE); |
121 | 135 |
} |
122 | 136 |
}); |
... | ... | |
131 | 145 |
//When a list item is click just change the checkbox state |
132 | 146 |
//and then the checkbox's onClick will be performed |
133 | 147 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
134 |
if (servers != null && servers.size() > 0) { |
|
135 |
LinearLayout linear = (LinearLayout) findViewById(R.id.nodes_linear_layout); |
|
136 |
ListView serversList = (ListView) linear.findViewById(android.R.id.list); |
|
137 |
View row = serversList.getChildAt(position); |
|
138 |
CheckBox checkBox = (CheckBox)row.findViewById(R.id.add_node_checkbox); |
|
139 |
if(!checkBox.isChecked()){ |
|
140 |
//if the checkbox was not previously checked, treat the listItemClick |
|
141 |
//the same as checking the checkbox |
|
142 |
checkBox.setChecked(!checkBox.isChecked()); |
|
143 |
} else { |
|
144 |
//if the checkbox was already checked when the listItemClick occurred, |
|
145 |
//then treat it like an edit |
|
148 |
if (possibleNodes != null && possibleNodes.size() > 0) { |
|
149 |
if(!possibleNodes.get(position).getName().equals("External Node")){ |
|
150 |
LinearLayout linear = (LinearLayout) findViewById(R.id.nodes_linear_layout); |
|
151 |
ListView serversList = (ListView) linear.findViewById(android.R.id.list); |
|
152 |
View row = serversList.getChildAt(position); |
|
153 |
CheckBox checkBox = (CheckBox)row.findViewById(R.id.add_node_checkbox); |
|
154 |
if(!checkBox.isChecked()){ |
|
155 |
//if the checkbox was not previously checked, treat the listItemClick |
|
156 |
//the same as checking the checkbox |
|
157 |
checkBox.setChecked(!checkBox.isChecked()); |
|
158 |
} else { |
|
159 |
//if the checkbox was already checked when the listItemClick occurred, |
|
160 |
//then treat it like an edit |
|
146 | 161 |
|
147 |
Server server = servers.get(position);
|
|
162 |
Server server = possibleNodes.get(position);
|
|
148 | 163 |
|
149 |
//Need to put all the ip's of the server into one |
|
150 |
//list so they can all be displayed in one spinner |
|
151 |
String[] ipAddresses = getAllIpsOfServer(server); |
|
164 |
//Need to put all the ip's of the server into one
|
|
165 |
//list so they can all be displayed in one spinner
|
|
166 |
String[] ipAddresses = getAllIpsOfServer(server);
|
|
152 | 167 |
|
153 |
Node node = getNodeFromServer(server); |
|
168 |
Node node = getNodeFromServer(server);
|
|
154 | 169 |
|
170 |
positionOfNode = findNodePosition(node); |
|
171 |
lastCheckedPos = position; |
|
172 |
|
|
173 |
Intent viewIntent = new Intent(getContext(), AddNodeActivity.class); |
|
174 |
viewIntent.putExtra("ipAddresses", ipAddresses); |
|
175 |
viewIntent.putExtra("name", server.getName()); |
|
176 |
if(node != null){ |
|
177 |
viewIntent.putExtra("node", node); |
|
178 |
} |
|
179 |
viewIntent.putExtra("weighted", loadBalancer.getAlgorithm().toLowerCase().contains("weighted")); |
|
180 |
startActivityForResult(viewIntent, ADD_NODE_CODE); |
|
181 |
} |
|
182 |
} else { |
|
183 |
//When clicked on an external node |
|
184 |
Server server = possibleNodes.get(position); |
|
185 |
Node node = getNodeFromServer(server); |
|
186 |
positionOfNode = findNodePosition(node); |
|
155 | 187 |
lastCheckedPos = position; |
156 |
Intent viewIntent = new Intent(getContext(), AddNodeActivity.class); |
|
157 |
viewIntent.putExtra("ipAddresses", ipAddresses); |
|
158 |
viewIntent.putExtra("name", server.getName()); |
|
188 |
Intent viewIntent = new Intent(getContext(), AddExternalNodeActivity.class); |
|
159 | 189 |
if(node != null){ |
160 | 190 |
viewIntent.putExtra("node", node); |
161 | 191 |
} |
162 | 192 |
//weighted is false, because on initial node add |
163 | 193 |
//weight is not option |
164 | 194 |
viewIntent.putExtra("weighted", false); |
165 |
startActivityForResult(viewIntent, ADD_NODE_CODE); |
|
195 |
startActivityForResult(viewIntent, ADD_EXTERNAL_NODE_CODE);
|
|
166 | 196 |
} |
197 |
} |
|
198 |
} |
|
167 | 199 |
|
200 |
//return the location of node in nodes |
|
201 |
//if it is no in there then -1 |
|
202 |
private int findNodePosition(Node node){ |
|
203 |
for(int i = 0; i < nodesToAdd.size(); i++){ |
|
204 |
String address = node.getAddress(); |
|
205 |
if(address.equals(nodesToAdd.get(i).getAddress())){ |
|
206 |
return i; |
|
207 |
} |
|
168 | 208 |
} |
209 |
return -1; |
|
169 | 210 |
} |
170 | 211 |
|
171 | 212 |
private void displayNoServersCell() { |
... | ... | |
194 | 235 |
} |
195 | 236 |
|
196 | 237 |
String[] serverNames = new String[servers.size()]; |
197 |
this.servers = new ArrayList<Server>();
|
|
238 |
this.possibleNodes = new ArrayList<Server>();
|
|
198 | 239 |
|
199 | 240 |
for(int i = 0; i < servers.size(); i++){ |
200 | 241 |
serverNames[i] = servers.get(i).getName(); |
201 |
this.servers.add(i, servers.get(i));
|
|
242 |
this.possibleNodes.add(i, servers.get(i));
|
|
202 | 243 |
} |
203 | 244 |
|
204 | 245 |
if (serverNames.length == 0) { |
... | ... | |
247 | 288 |
// * Adapter/ |
248 | 289 |
class ServerAdapter extends ArrayAdapter<Server> { |
249 | 290 |
ServerAdapter() { |
250 |
super(AddMoreNodesActivity.this, R.layout.listservernodecell, servers);
|
|
291 |
super(AddMoreNodesActivity.this, R.layout.listservernodecell, possibleNodes);
|
|
251 | 292 |
} |
252 | 293 |
|
253 | 294 |
public View getView(int position, View convertView, ViewGroup parent) { |
254 | 295 |
|
255 |
final Server server = servers.get(position);
|
|
296 |
final Server server = possibleNodes.get(position);
|
|
256 | 297 |
LayoutInflater inflater = getLayoutInflater(); |
257 | 298 |
View row = inflater.inflate(R.layout.listservernodecell, parent, false); |
258 | 299 |
|
... | ... | |
302 | 343 |
@Override |
303 | 344 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
304 | 345 |
if(isChecked){ |
346 |
//if node is being check it won't be in nodes positionOfNode is -1 |
|
347 |
positionOfNode = -1; |
|
305 | 348 |
lastCheckedPos = pos; |
306 | 349 |
Intent viewIntent = new Intent(getContext(), AddNodeActivity.class); |
307 | 350 |
viewIntent.putExtra("ipAddresses", ipAddresses); |
... | ... | |
312 | 355 |
else{ |
313 | 356 |
removeNodeFromList(server); |
314 | 357 |
if(server.getName().equals("External Node")){ |
315 |
servers.remove(server);
|
|
316 |
setServerList(servers);
|
|
358 |
possibleNodes.remove(server);
|
|
359 |
setServerList(possibleNodes);
|
|
317 | 360 |
} |
318 | 361 |
} |
319 | 362 |
} |
... | ... | |
338 | 381 |
} |
339 | 382 |
private boolean serverHasIp(Server server, String address){ |
340 | 383 |
String[] addresses = server.getPrivateIpAddresses(); |
341 |
for(int i = 0; i < addresses.length; i++){ |
|
342 |
Log.d("info", "server address: " + addresses[i] + " node address: " + address); |
|
343 |
if(addresses[i].equals(address)){ |
|
344 |
return true; |
|
384 |
if(addresses != null){ |
|
385 |
for(int i = 0; i < addresses.length; i++){ |
|
386 |
if(addresses[i].equals(address)){ |
|
387 |
return true; |
|
388 |
} |
|
345 | 389 |
} |
346 | 390 |
} |
347 | 391 |
addresses = server.getPublicIpAddresses(); |
348 |
for(int i = 0; i < addresses.length; i++){ |
|
349 |
if(addresses[i].equals(address)){ |
|
350 |
return true; |
|
392 |
if(addresses != null){ |
|
393 |
for(int i = 0; i < addresses.length; i++){ |
|
394 |
if(addresses[i].equals(address)){ |
|
395 |
return true; |
|
396 |
} |
|
351 | 397 |
} |
352 | 398 |
} |
353 | 399 |
return false; |
... | ... | |
436 | 482 |
} |
437 | 483 |
|
438 | 484 |
private String getNameFromIp(String address){ |
439 |
for(Server s: servers){
|
|
485 |
for(Server s: possibleNodes){
|
|
440 | 486 |
if(serverHasIp(s, address)){ |
441 | 487 |
return s.getName(); |
442 | 488 |
} |
... | ... | |
445 | 491 |
} |
446 | 492 |
|
447 | 493 |
private boolean isCloudServerIp(String address){ |
448 |
for(Server s : servers){
|
|
494 |
for(Server s : possibleNodes){
|
|
449 | 495 |
if(serverHasIp(s, address)){ |
450 | 496 |
return true; |
451 | 497 |
} |
... | ... | |
487 | 533 |
protected void onActivityResult(int requestCode, int resultCode, Intent data){ |
488 | 534 |
int pos = lastCheckedPos; |
489 | 535 |
if(requestCode == ADD_NODE_CODE && resultCode == RESULT_OK){ |
490 |
//data will be null is user back out on edit
|
|
536 |
//data will be null if user backed out on edit
|
|
491 | 537 |
//we dont need to do anything then |
492 | 538 |
//if new node added data won't be null |
493 | 539 |
//so create the new node and add it to the list |
494 | 540 |
if(data != null){ |
541 |
//will remove the node if it's already in the list |
|
542 |
//so we can update it |
|
543 |
if(positionOfNode >= 0){ |
|
544 |
nodesToAdd.remove(positionOfNode); |
|
545 |
} |
|
546 |
|
|
495 | 547 |
Node node = new Node(); |
496 | 548 |
node.setAddress(data.getStringExtra("nodeIp")); |
497 | 549 |
node.setCondition(data.getStringExtra("nodeCondition")); |
498 |
node.setName(servers.get(pos).getName());
|
|
550 |
node.setName(possibleNodes.get(pos).getName());
|
|
499 | 551 |
node.setPort(data.getStringExtra("nodePort")); |
500 | 552 |
node.setWeight(data.getStringExtra("nodeWeight")); |
501 | 553 |
nodesToAdd.add(node); |
... | ... | |
522 | 574 |
* so they can select it from there |
523 | 575 |
*/ |
524 | 576 |
if(!isCloudServerIp(node.getAddress())){ |
577 |
|
|
578 |
if(positionOfNode >= 0){ |
|
579 |
nodesToAdd.remove(positionOfNode); |
|
580 |
} |
|
581 |
|
|
525 | 582 |
nodesToAdd.add(node); |
526 | 583 |
//Add it to server list so it display in the listview |
527 | 584 |
Server server = new Server(); |
528 | 585 |
server.setName("External Node"); |
529 | 586 |
String[] ip = {data.getStringExtra("nodeIp")}; |
530 | 587 |
server.setPrivateIpAddresses(ip); |
531 |
servers.add(server);
|
|
532 |
setServerList(servers);
|
|
588 |
possibleNodes.add(server);
|
|
589 |
setServerList(possibleNodes);
|
|
533 | 590 |
} else { |
534 | 591 |
showAlert("Error", "This IP belongs to a cloud server: \"" + getNameFromIp(node.getAddress()) |
535 | 592 |
+ "\", please select it from the list."); |
536 | 593 |
} |
537 | 594 |
} |
595 |
printTheNodes(); |
|
596 |
} |
|
597 |
|
|
598 |
private void printTheNodes(){ |
|
599 |
for(Node n: nodesToAdd){ |
|
600 |
Log.d("info", "address: " + n.getAddress() + " Port: " + n.getPort() + " Cond: " + n.getCondition()); |
|
601 |
} |
|
602 |
Log.d("info", " SPACE "); |
|
538 | 603 |
} |
539 | 604 |
} |
Also available in: Unified diff