root / src / com / rackspacecloud / android / AddNodesActivity.java @ 4f9d1a69
History | View | Annotate | Download (9 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
|
6 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
7 |
import com.rackspace.cloud.servers.api.client.Account; |
8 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
9 |
import com.rackspace.cloud.servers.api.client.Server; |
10 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
11 |
|
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 |
import android.content.Intent; |
18 |
import android.os.AsyncTask; |
19 |
import android.os.Bundle; |
20 |
import android.util.Log; |
21 |
import android.view.LayoutInflater; |
22 |
import android.view.View; |
23 |
import android.view.View.OnClickListener; |
24 |
import android.view.ViewGroup; |
25 |
import android.view.WindowManager; |
26 |
import android.view.ViewGroup.LayoutParams; |
27 |
import android.widget.ArrayAdapter; |
28 |
import android.widget.Button; |
29 |
import android.widget.CheckBox; |
30 |
import android.widget.CompoundButton; |
31 |
import android.widget.LinearLayout; |
32 |
import android.widget.ListView; |
33 |
import android.widget.CompoundButton.OnCheckedChangeListener; |
34 |
import android.widget.ImageView; |
35 |
import android.widget.ProgressBar; |
36 |
import android.widget.TextView; |
37 |
|
38 |
public class AddNodesActivity extends ListActivity { |
39 |
|
40 |
private static final int ADD_NODE_CODE = 178; |
41 |
|
42 |
private Server[] servers; |
43 |
private Context context; |
44 |
private int lastCheckedPos; |
45 |
private ArrayList<Node> nodes; |
46 |
ProgressDialog pDialog; |
47 |
|
48 |
@Override
|
49 |
public void onCreate(Bundle savedInstanceState) { |
50 |
super.onCreate(savedInstanceState);
|
51 |
nodes = (ArrayList<Node>) this.getIntent().getExtras().get("nodes"); |
52 |
setContentView(R.layout.addnodes); |
53 |
restoreState(savedInstanceState); |
54 |
} |
55 |
|
56 |
@Override
|
57 |
protected void onSaveInstanceState(Bundle outState) { |
58 |
super.onSaveInstanceState(outState);
|
59 |
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 |
} |
66 |
} |
67 |
|
68 |
private void restoreState(Bundle state) { |
69 |
|
70 |
context = getApplicationContext(); |
71 |
|
72 |
if(nodes != null){ |
73 |
print(nodes); |
74 |
} |
75 |
|
76 |
if (state != null && state.containsKey("nodes")){ |
77 |
nodes = (ArrayList<Node>) state.getSerializable("nodes"); |
78 |
if(nodes == null){ |
79 |
nodes = new ArrayList<Node>(); |
80 |
} |
81 |
} |
82 |
|
83 |
if (state != null && state.containsKey("server")) { |
84 |
servers = (Server[]) state.getSerializable("servers"); |
85 |
if (servers.length == 0) { |
86 |
displayNoServersCell(); |
87 |
} else {
|
88 |
getListView().setDividerHeight(1); // restore divider lines |
89 |
setListAdapter(new ServerAdapter());
|
90 |
} |
91 |
} else {
|
92 |
loadServers(); |
93 |
} |
94 |
|
95 |
Button submitNodes = (Button) findViewById(R.id.submit_nodes_button); |
96 |
submitNodes.setOnClickListener(new OnClickListener() {
|
97 |
|
98 |
@Override
|
99 |
public void onClick(View v) { |
100 |
Intent viewIntent = new Intent();
|
101 |
viewIntent.putExtra("nodes", nodes);
|
102 |
setResult(RESULT_OK, viewIntent); |
103 |
finish(); |
104 |
} |
105 |
}); |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void onBackPressed(){ |
110 |
setResult(RESULT_CANCELED); |
111 |
finish(); |
112 |
} |
113 |
|
114 |
private void displayNoServersCell() { |
115 |
String a[] = new String[1]; |
116 |
a[0] = "No Servers"; |
117 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noserverscell, R.id.no_servers_label, a)); |
118 |
getListView().setTextFilterEnabled(true);
|
119 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
120 |
getListView().setItemsCanFocus(false);
|
121 |
} |
122 |
|
123 |
private void setServerList(ArrayList<Server> servers) { |
124 |
if (servers == null) { |
125 |
servers = new ArrayList<Server>(); |
126 |
} |
127 |
String[] serverNames = new String[servers.size()]; |
128 |
this.servers = new Server[servers.size()]; |
129 |
|
130 |
if (servers != null) { |
131 |
for (int i = 0; i < servers.size(); i++) { |
132 |
Server server = servers.get(i); |
133 |
this.servers[i] = server;
|
134 |
serverNames[i] = server.getName(); |
135 |
} |
136 |
} |
137 |
|
138 |
if (serverNames.length == 0) { |
139 |
displayNoServersCell(); |
140 |
} else {
|
141 |
getListView().setDividerHeight(1); // restore divider lines |
142 |
setListAdapter(new ServerAdapter());
|
143 |
} |
144 |
} |
145 |
|
146 |
private void loadServers() { |
147 |
new LoadServersTask().execute((Void[]) null); |
148 |
} |
149 |
|
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 |
private class LoadServersTask extends AsyncTask<Void, Void, ArrayList<Server>> { |
173 |
private CloudServersException exception;
|
174 |
|
175 |
@Override
|
176 |
protected void onPreExecute(){ |
177 |
showDialog(); |
178 |
} |
179 |
|
180 |
@Override
|
181 |
protected ArrayList<Server> doInBackground(Void... arg0) { |
182 |
ArrayList<Server> servers = null; |
183 |
try {
|
184 |
servers = (new ServerManager()).createList(true, context); |
185 |
} catch (CloudServersException e) {
|
186 |
exception = e; |
187 |
} |
188 |
pDialog.dismiss(); |
189 |
return servers;
|
190 |
} |
191 |
|
192 |
@Override
|
193 |
protected void onPostExecute(ArrayList<Server> result) { |
194 |
if (exception != null) { |
195 |
showAlert("Error", exception.getMessage());
|
196 |
} |
197 |
setServerList(result); |
198 |
} |
199 |
} |
200 |
|
201 |
// * Adapter/
|
202 |
class ServerAdapter extends ArrayAdapter<Server> { |
203 |
ServerAdapter() { |
204 |
super(AddNodesActivity.this, R.layout.listservernodecell, servers);
|
205 |
} |
206 |
|
207 |
public View getView(int position, View convertView, ViewGroup parent) { |
208 |
|
209 |
final Server server = servers[position];
|
210 |
LayoutInflater inflater = getLayoutInflater(); |
211 |
View row = inflater.inflate(R.layout.listservernodecell, parent, false); |
212 |
|
213 |
TextView label = (TextView) row.findViewById(R.id.label); |
214 |
label.setText(server.getName()); |
215 |
|
216 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
217 |
sublabel.setText(server.getFlavor().getName() + " - " + server.getImage().getName());
|
218 |
|
219 |
ImageView icon = (ImageView) row.findViewById(R.id.icon); |
220 |
icon.setImageResource(server.getImage().iconResourceId()); |
221 |
|
222 |
String[] publicIp = server.getPublicIpAddresses(); |
223 |
String[] privateIp = server.getPrivateIpAddresses(); |
224 |
|
225 |
|
226 |
final String[] ipAddresses = new String[privateIp.length + publicIp.length]; |
227 |
for(int i = 0; i < privateIp.length; i++){ |
228 |
ipAddresses[i] = privateIp[i]; |
229 |
} |
230 |
for(int i = 0; i < publicIp.length; i++){ |
231 |
ipAddresses[i+privateIp.length] = publicIp[i]; |
232 |
} |
233 |
|
234 |
final int pos = position; |
235 |
CheckBox add = (CheckBox) row.findViewById(R.id.add_node_checkbox); |
236 |
|
237 |
if(inNodeList(server)){
|
238 |
add.setChecked(true);
|
239 |
} |
240 |
|
241 |
add.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
242 |
|
243 |
@Override
|
244 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
245 |
if(isChecked){
|
246 |
lastCheckedPos = pos; |
247 |
Intent viewIntent = new Intent(context, AddNodeActivity.class);
|
248 |
viewIntent.putExtra("ipAddresses", ipAddresses);
|
249 |
startActivityForResult(viewIntent, ADD_NODE_CODE); |
250 |
} |
251 |
else{
|
252 |
removeNodeFromList(server); |
253 |
} |
254 |
} |
255 |
}); |
256 |
|
257 |
return(row);
|
258 |
} |
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; |
268 |
} |
269 |
} |
270 |
return false; |
271 |
} |
272 |
|
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;
|
283 |
} |
284 |
} |
285 |
} |
286 |
|
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; |
292 |
} |
293 |
} |
294 |
addresses = server.getPublicIpAddresses(); |
295 |
for(int i = 0; i < addresses.length; i++){ |
296 |
if(addresses[i].equals(address)){
|
297 |
return true; |
298 |
} |
299 |
} |
300 |
return false; |
301 |
} |
302 |
|
303 |
protected void onActivityResult(int requestCode, int resultCode, Intent data){ |
304 |
int pos = lastCheckedPos;
|
305 |
if(requestCode == ADD_NODE_CODE && resultCode == RESULT_OK){
|
306 |
Node node = new Node();
|
307 |
node.setAddress(data.getStringExtra("nodeIp"));
|
308 |
node.setCondition(data.getStringExtra("nodeCondition"));
|
309 |
node.setName(servers[pos].getName()); |
310 |
node.setPort(data.getStringExtra("nodePort"));
|
311 |
nodes.add(node); |
312 |
} |
313 |
else if(requestCode == ADD_NODE_CODE && resultCode == RESULT_CANCELED){ |
314 |
//uncheck the node at lastCheckedPos
|
315 |
LinearLayout linear = (LinearLayout) findViewById(R.id.nodes_linear_layout); |
316 |
ListView serversList = (ListView) linear.findViewById(android.R.id.list); |
317 |
View row = serversList.getChildAt(pos);
|
318 |
CheckBox checkBox = (CheckBox)row.findViewById(R.id.add_node_checkbox); |
319 |
checkBox.setChecked(false);
|
320 |
} |
321 |
} |
322 |
|
323 |
|
324 |
} |