Revision b722cab3 src/com/rackspacecloud/android/AddNodesActivity.java
b/src/com/rackspacecloud/android/AddNodesActivity.java | ||
---|---|---|
1 | 1 |
package com.rackspacecloud.android; |
2 | 2 |
|
3 | 3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
|
5 | 4 |
|
6 | 5 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
7 |
import com.rackspace.cloud.servers.api.client.Account; |
|
8 | 6 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
9 | 7 |
import com.rackspace.cloud.servers.api.client.Server; |
10 | 8 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
11 | 9 |
|
12 |
import android.app.AlertDialog; |
|
13 |
import android.app.ListActivity; |
|
14 |
import android.app.ProgressDialog; |
|
15 |
import android.content.Context; |
|
16 |
import android.content.DialogInterface; |
|
17 | 10 |
import android.content.Intent; |
18 | 11 |
import android.os.AsyncTask; |
19 | 12 |
import android.os.Bundle; |
... | ... | |
22 | 15 |
import android.view.View; |
23 | 16 |
import android.view.View.OnClickListener; |
24 | 17 |
import android.view.ViewGroup; |
25 |
import android.view.WindowManager; |
|
26 |
import android.view.ViewGroup.LayoutParams; |
|
27 | 18 |
import android.widget.ArrayAdapter; |
28 | 19 |
import android.widget.Button; |
29 | 20 |
import android.widget.CheckBox; |
... | ... | |
32 | 23 |
import android.widget.ListView; |
33 | 24 |
import android.widget.CompoundButton.OnCheckedChangeListener; |
34 | 25 |
import android.widget.ImageView; |
35 |
import android.widget.ProgressBar; |
|
36 | 26 |
import android.widget.TextView; |
37 | 27 |
|
38 |
public class AddNodesActivity extends ListActivity { |
|
28 |
public class AddNodesActivity extends CloudListActivity {
|
|
39 | 29 |
|
40 | 30 |
private static final int ADD_NODE_CODE = 178; |
41 | 31 |
|
42 | 32 |
private Server[] servers; |
43 |
private Context context; |
|
44 | 33 |
private int lastCheckedPos; |
45 | 34 |
private ArrayList<Node> nodes; |
46 |
ProgressDialog pDialog; |
|
47 | 35 |
|
36 |
@SuppressWarnings("unchecked") |
|
48 | 37 |
@Override |
49 | 38 |
public void onCreate(Bundle savedInstanceState) { |
50 | 39 |
super.onCreate(savedInstanceState); |
... | ... | |
57 | 46 |
protected void onSaveInstanceState(Bundle outState) { |
58 | 47 |
super.onSaveInstanceState(outState); |
59 | 48 |
outState.putSerializable("nodes", nodes); |
60 |
} |
|
61 |
|
|
62 |
private void print(ArrayList<Node> nodes){ |
|
63 |
for(Node n : nodes){ |
|
64 |
Log.d("info", "the node is " + n.getName()); |
|
65 |
} |
|
49 |
outState.putSerializable("servers", servers); |
|
66 | 50 |
} |
67 | 51 |
|
68 |
private void restoreState(Bundle state) { |
|
69 |
|
|
70 |
context = getApplicationContext(); |
|
71 |
|
|
72 |
if(nodes != null){ |
|
73 |
print(nodes); |
|
74 |
} |
|
75 |
|
|
52 |
@SuppressWarnings("unchecked") |
|
53 |
protected void restoreState(Bundle state) { |
|
54 |
super.restoreState(state); |
|
55 |
|
|
76 | 56 |
if (state != null && state.containsKey("nodes")){ |
77 | 57 |
nodes = (ArrayList<Node>) state.getSerializable("nodes"); |
78 | 58 |
if(nodes == null){ |
... | ... | |
80 | 60 |
} |
81 | 61 |
} |
82 | 62 |
|
83 |
if (state != null && state.containsKey("server")) { |
|
63 |
if (state != null && state.containsKey("servers")) {
|
|
84 | 64 |
servers = (Server[]) state.getSerializable("servers"); |
85 | 65 |
if (servers.length == 0) { |
86 | 66 |
displayNoServersCell(); |
... | ... | |
113 | 93 |
|
114 | 94 |
private void displayNoServersCell() { |
115 | 95 |
String a[] = new String[1]; |
116 |
a[0] = "No Servers";
|
|
96 |
a[0] = "No Nodes";
|
|
117 | 97 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noserverscell, R.id.no_servers_label, a)); |
118 | 98 |
getListView().setTextFilterEnabled(true); |
119 | 99 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
... | ... | |
147 | 127 |
new LoadServersTask().execute((Void[]) null); |
148 | 128 |
} |
149 | 129 |
|
150 |
protected void showDialog() { |
|
151 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
|
152 |
// // Set blur to background |
|
153 |
WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes(); |
|
154 |
lp.dimAmount = 0.0f; |
|
155 |
pDialog.getWindow().setAttributes(lp); |
|
156 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
|
157 |
pDialog.show(); |
|
158 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
|
159 |
} |
|
160 |
|
|
161 |
private void showAlert(String title, String message) { |
|
162 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
|
163 |
alert.setTitle(title); |
|
164 |
alert.setMessage(message); |
|
165 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
|
166 |
public void onClick(DialogInterface dialog, int which) { |
|
167 |
return; |
|
168 |
} }); |
|
169 |
alert.show(); |
|
170 |
} |
|
171 |
|
|
172 | 130 |
private class LoadServersTask extends AsyncTask<Void, Void, ArrayList<Server>> { |
173 | 131 |
private CloudServersException exception; |
174 | 132 |
|
... | ... | |
181 | 139 |
protected ArrayList<Server> doInBackground(Void... arg0) { |
182 | 140 |
ArrayList<Server> servers = null; |
183 | 141 |
try { |
184 |
servers = (new ServerManager()).createList(true, context);
|
|
142 |
servers = (new ServerManager()).createList(true, getContext());
|
|
185 | 143 |
} catch (CloudServersException e) { |
186 | 144 |
exception = e; |
187 | 145 |
} |
188 |
pDialog.dismiss(); |
|
189 | 146 |
return servers; |
190 | 147 |
} |
191 | 148 |
|
192 | 149 |
@Override |
193 | 150 |
protected void onPostExecute(ArrayList<Server> result) { |
151 |
hideDialog(); |
|
194 | 152 |
if (exception != null) { |
195 | 153 |
showAlert("Error", exception.getMessage()); |
196 | 154 |
} |
... | ... | |
233 | 191 |
|
234 | 192 |
final int pos = position; |
235 | 193 |
CheckBox add = (CheckBox) row.findViewById(R.id.add_node_checkbox); |
236 |
|
|
194 |
|
|
237 | 195 |
if(inNodeList(server)){ |
238 | 196 |
add.setChecked(true); |
239 | 197 |
} |
240 |
|
|
198 |
|
|
241 | 199 |
add.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
242 | 200 |
|
243 | 201 |
@Override |
244 | 202 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
245 | 203 |
if(isChecked){ |
246 | 204 |
lastCheckedPos = pos; |
247 |
Intent viewIntent = new Intent(context, AddNodeActivity.class);
|
|
205 |
Intent viewIntent = new Intent(getContext(), AddNodeActivity.class);
|
|
248 | 206 |
viewIntent.putExtra("ipAddresses", ipAddresses); |
207 |
viewIntent.putExtra("name", server.getName()); |
|
208 |
//weighted is false, because on initial node add |
|
209 |
//weight is not option |
|
210 |
viewIntent.putExtra("weighted", false); |
|
249 | 211 |
startActivityForResult(viewIntent, ADD_NODE_CODE); |
250 | 212 |
} |
251 | 213 |
else{ |
... | ... | |
253 | 215 |
} |
254 | 216 |
} |
255 | 217 |
}); |
256 |
|
|
218 |
|
|
257 | 219 |
return(row); |
258 | 220 |
} |
259 |
} |
|
260 |
|
|
261 |
private boolean inNodeList(Server server){ |
|
262 |
Log.d("info", "the server is " + server.getName()); |
|
263 |
for(Node node : nodes){ |
|
264 |
Log.d("info", "the node is " + node.getName()); |
|
265 |
String nodeIp = node.getAddress(); |
|
266 |
if(serverHasIp(server, nodeIp)){ |
|
267 |
return true; |
|
221 |
|
|
222 |
private boolean inNodeList(Server server){ |
|
223 |
for(Node node : nodes){ |
|
224 |
String nodeIp = node.getAddress(); |
|
225 |
if(serverHasIp(server, nodeIp)){ |
|
226 |
return true; |
|
227 |
} |
|
268 | 228 |
} |
229 |
return false; |
|
269 | 230 |
} |
270 |
return false; |
|
271 |
} |
|
272 | 231 |
|
273 |
/* |
|
274 |
* need to remove by id because that is |
|
275 |
* what is unique |
|
276 |
*/ |
|
277 |
private void removeNodeFromList(Server server){ |
|
278 |
for(int i = 0; i < nodes.size(); i++){ |
|
279 |
Node node = nodes.get(i); |
|
280 |
if(serverHasIp(server, node.getAddress())){ |
|
281 |
nodes.remove(i); |
|
282 |
break; |
|
232 |
/* |
|
233 |
* need to remove by id because that is |
|
234 |
* what is unique |
|
235 |
*/ |
|
236 |
private void removeNodeFromList(Server server){ |
|
237 |
for(int i = 0; i < nodes.size(); i++){ |
|
238 |
Node node = nodes.get(i); |
|
239 |
if(serverHasIp(server, node.getAddress())){ |
|
240 |
nodes.remove(i); |
|
241 |
break; |
|
242 |
} |
|
283 | 243 |
} |
284 | 244 |
} |
285 |
} |
|
286 | 245 |
|
287 |
private boolean serverHasIp(Server server, String address){ |
|
288 |
String[] addresses = server.getPrivateIpAddresses(); |
|
289 |
for(int i = 0; i < addresses.length; i++){ |
|
290 |
if(addresses[i].equals(address)){ |
|
291 |
return true; |
|
246 |
private boolean serverHasIp(Server server, String address){ |
|
247 |
String[] addresses = server.getPrivateIpAddresses(); |
|
248 |
for(int i = 0; i < addresses.length; i++){ |
|
249 |
if(addresses[i].equals(address)){ |
|
250 |
return true; |
|
251 |
} |
|
292 | 252 |
} |
293 |
}
|
|
294 |
addresses = server.getPublicIpAddresses();
|
|
295 |
for(int i = 0; i < addresses.length; i++){
|
|
296 |
if(addresses[i].equals(address)){
|
|
297 |
return true;
|
|
253 |
addresses = server.getPublicIpAddresses();
|
|
254 |
for(int i = 0; i < addresses.length; i++){
|
|
255 |
if(addresses[i].equals(address)){
|
|
256 |
return true;
|
|
257 |
}
|
|
298 | 258 |
} |
259 |
return false; |
|
299 | 260 |
} |
300 |
return false; |
|
301 | 261 |
} |
302 | 262 |
|
263 |
|
|
264 |
|
|
303 | 265 |
protected void onActivityResult(int requestCode, int resultCode, Intent data){ |
304 | 266 |
int pos = lastCheckedPos; |
305 | 267 |
if(requestCode == ADD_NODE_CODE && resultCode == RESULT_OK){ |
... | ... | |
308 | 270 |
node.setCondition(data.getStringExtra("nodeCondition")); |
309 | 271 |
node.setName(servers[pos].getName()); |
310 | 272 |
node.setPort(data.getStringExtra("nodePort")); |
273 |
Log.d("info", "the node weight is " + data.getStringExtra("nodeWeight")); |
|
274 |
node.setWeight(data.getStringExtra("nodeWeight")); |
|
311 | 275 |
nodes.add(node); |
312 | 276 |
} |
313 | 277 |
else if(requestCode == ADD_NODE_CODE && resultCode == RESULT_CANCELED){ |
Also available in: Unified diff