Revision b722cab3 src/com/rackspacecloud/android/ViewLoadBalancerActivity.java

b/src/com/rackspacecloud/android/ViewLoadBalancerActivity.java
3 3
 */
4 4
package com.rackspacecloud.android;
5 5

  
6
import java.io.IOException;
7
import java.io.StringReader;
8

  
9
import javax.xml.parsers.FactoryConfigurationError;
10
import javax.xml.parsers.ParserConfigurationException;
11
import javax.xml.parsers.SAXParser;
12
import javax.xml.parsers.SAXParserFactory;
6
import java.util.ArrayList;
7
import java.util.Collections;
13 8

  
14 9
import org.apache.http.HttpResponse;
15
import org.apache.http.client.ClientProtocolException;
16
import org.apache.http.impl.client.BasicResponseHandler;
17
import org.xml.sax.InputSource;
18
import org.xml.sax.SAXException;
19
import org.xml.sax.XMLReader;
20 10

  
21 11
import android.app.Activity;
22 12
import android.app.AlertDialog;
23 13
import android.app.Dialog;
24
import android.app.ProgressDialog;
25
import android.content.Context;
26 14
import android.content.DialogInterface;
27 15
import android.content.Intent;
16
import android.graphics.Color;
28 17
import android.os.AsyncTask;
29 18
import android.os.Bundle;
30 19
import android.util.Log;
20
import android.util.TypedValue;
31 21
import android.view.Menu;
32 22
import android.view.MenuInflater;
33 23
import android.view.MenuItem;
34 24
import android.view.View;
35 25
import android.view.View.OnClickListener;
36
import android.view.WindowManager;
37
import android.view.ViewGroup.LayoutParams;
38 26
import android.widget.Button;
39
import android.widget.ProgressBar;
27
import android.widget.LinearLayout;
40 28
import android.widget.TextView;
41 29

  
42 30
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
43 31
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
32
import com.rackspace.cloud.loadbalancer.api.client.Node;
33
import com.rackspace.cloud.loadbalancer.api.client.VirtualIp;
44 34
import com.rackspace.cloud.loadbalancers.api.client.http.LoadBalancersException;
45 35
import com.rackspace.cloud.servers.api.client.CloudServersException;
46 36
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
47
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
48 37

  
49
public class ViewLoadBalancerActivity extends Activity {
38
public class ViewLoadBalancerActivity extends CloudActivity {
50 39

  
51 40
	private static final int EDIT_LOAD_BALANCER_CODE = 184;
52 41
	private static final int EDIT_NODES_CODE = 185;
53
	
42

  
54 43
	private LoadBalancer loadBalancer;
55
	private LoadBalancer returnLoadBalancer;
56
	private Context context;
57
	ProgressDialog pDialog;
44
	private PollLoadBalancerTask pollLoadBalancerTask;
58 45

  
59 46
	@Override
60 47
	public void onCreate(Bundle savedInstanceState) {
61 48
		super.onCreate(savedInstanceState);
62 49
		loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
63
		Log.i("VIEWLOADBALANCERS: ", loadBalancer.getAlgorithm() +","+loadBalancer.getProtocol()+","+loadBalancer.getStatus());
64

  
50
		Log.d("info", "the status is " + loadBalancer.getStatus());
65 51
		setContentView(R.layout.view_loadbalancer);
66 52
		restoreState(savedInstanceState);
67 53
	}
......
72 58
		outState.putSerializable("loadBalancer", loadBalancer);
73 59
	}
74 60

  
75
	private void restoreState(Bundle state) {
76
		context = getApplicationContext();
77
		if (state != null && state.containsKey("loadBalancer")) {
61
	protected void restoreState(Bundle state) {
62
		super.restoreState(state);
63

  
64
		if (state != null && state.containsKey("loadBalancer") && state.getSerializable("loadBalancer") != null) {
78 65
			loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
66
			loadLoadBalancerData();
67
			if(!loadBalancer.getStatus().contains("DELETE")){
68
				setUpButtons();
69
			} else {
70
				setUpBadButtons();
71
			}
72
		}
73
		else{
74
			new LoadLoadBalancerTask().execute((Void[]) null);
79 75
		}
80
		new LoadLoadBalancerTask().execute((Void[]) null);   
81
		setUpButtons();
82 76
	}
83 77

  
84 78
	private void setUpButtons(){
......
87 81

  
88 82
			@Override
89 83
			public void onClick(View v) {
90
				Intent editLoadBalancerIntent = new Intent(context, EditLoadBalancerActivity.class);
84
				Intent editLoadBalancerIntent = new Intent(getContext(), EditLoadBalancerActivity.class);
91 85
				editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
92 86
				startActivityForResult(editLoadBalancerIntent, EDIT_LOAD_BALANCER_CODE);
93 87
			}
......
103 97
			}
104 98

  
105 99
		});
106
		
100

  
107 101
		Button editNodes = (Button)findViewById(R.id.edit_nodes_button);
108 102
		editNodes.setOnClickListener(new OnClickListener() {
109 103

  
110 104
			@Override
111 105
			public void onClick(View v) {
112
				Intent editLoadBalancerIntent = new Intent(context, EditNodesActivity.class);
113
				Log.d("info", "the nodes are null before?" + Boolean.toString(returnLoadBalancer.getNodes() == null));
114
				editLoadBalancerIntent.putExtra("nodes", returnLoadBalancer.getNodes());
115
				editLoadBalancerIntent.putExtra("loadBalancer", returnLoadBalancer);
106
				Intent editLoadBalancerIntent = new Intent(getContext(), EditNodesActivity.class);
107
				editLoadBalancerIntent.putExtra("nodes", loadBalancer.getNodes());
108
				editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
116 109
				startActivityForResult(editLoadBalancerIntent, EDIT_NODES_CODE);
117 110
			}
118 111
		});
112
	}
113
	
114
	private void setUpBadButtons(){
115
		Button editLoadBalancer = (Button)findViewById(R.id.edit_loadbalancer_button);
116
		editLoadBalancer.setOnClickListener(new OnClickListener() {
117

  
118
			@Override
119
			public void onClick(View v) {
120
				showAlert(loadBalancer.getStatus(), "The Load Balancer cannot currently be updated");
121
			}
122

  
123
		});
124

  
125
		Button deleteLoadBalancer = (Button)findViewById(R.id.delete_loadbalancer_button);
126
		deleteLoadBalancer.setOnClickListener(new OnClickListener() {
127

  
128
			@Override
129
			public void onClick(View v) {
130
				showAlert(loadBalancer.getStatus(), "The Load Balancer cannot currently be deleted");
131
			}
132

  
133
		});
134

  
135
		Button editNodes = (Button)findViewById(R.id.edit_nodes_button);
136
		editNodes.setOnClickListener(new OnClickListener() {
137

  
138
			@Override
139
			public void onClick(View v) {
140
				showAlert(loadBalancer.getStatus(), "The nodes cannot currently be edited");
141
			}
142
		});
119 143

  
120 144

  
121 145
	}
......
145 169
	}
146 170

  
147 171
	private void loadLoadBalancerData() {
148
		TextView name = (TextView) findViewById(R.id.view_name);
149
		name.setText(returnLoadBalancer.getName());
172
		if(loadBalancer != null){
173
			TextView name = (TextView) findViewById(R.id.view_name);
174
			name.setText(loadBalancer.getName());
175

  
176
			TextView id = (TextView) findViewById(R.id.view_lb_id);
177
			id.setText(loadBalancer.getId());
150 178

  
151
		TextView id = (TextView) findViewById(R.id.view_lb_id);
152
		id.setText(returnLoadBalancer.getId());
179
			TextView protocol = (TextView) findViewById(R.id.view_protocol);
180
			protocol.setText(loadBalancer.getProtocol());
153 181

  
154
		TextView protocol = (TextView) findViewById(R.id.view_protocol);
155
		protocol.setText(returnLoadBalancer.getProtocol());
182
			TextView port = (TextView) findViewById(R.id.view_port);
183
			port.setText(loadBalancer.getPort());
156 184

  
157
		TextView port = (TextView) findViewById(R.id.view_port);
158
		port.setText(returnLoadBalancer.getPort());
185
			TextView algorithm = (TextView) findViewById(R.id.view_algorithm);
186
			algorithm.setText(loadBalancer.getAlgorithm());
187

  
188
			TextView status = (TextView) findViewById(R.id.view_status);
189
			if (!"ACTIVE".equals(loadBalancer.getStatus())) {
190
				status.setText(loadBalancer.getStatus());
191
				pollLoadBalancerTask = new PollLoadBalancerTask();
192
				pollLoadBalancerTask.execute((Void[]) null);
193
			} else {
194
				status.setText(loadBalancer.getStatus());
195
			}
159 196

  
160
		TextView algorithm = (TextView) findViewById(R.id.view_algorithm);
161
		algorithm.setText(returnLoadBalancer.getAlgorithm());
162 197

  
163
		TextView status = (TextView) findViewById(R.id.view_status);
164
		status.setText(returnLoadBalancer.getStatus());
198
			status.setText(loadBalancer.getStatus());
165 199

  
166
		TextView connectionLogging = (TextView) findViewById(R.id.view_islogging);
167
		connectionLogging.setText(returnLoadBalancer.getIsConnectionLoggingEnabled());
200
			TextView connectionLogging = (TextView) findViewById(R.id.view_islogging);
201
			connectionLogging.setText(loadBalancer.getIsConnectionLoggingEnabled());
168 202

  
169
		loadVirutalIpData();
203
			loadVirutalIpData();
204
		}
170 205
	}
171 206

  
172 207
	private void loadVirutalIpData() {
173
		TextView vipId = (TextView) findViewById(R.id.view_vip_id);
174
		vipId.setText(returnLoadBalancer.getVirtualIps().get(0).getId());
208
		int layoutIndex = 0;
209
		LinearLayout layout = (LinearLayout) this.findViewById(R.id.vip_addresses);    
210
		layout.removeAllViews();
211
		ArrayList<VirtualIp> virtualIps = loadBalancer.getVirtualIps();
212
		//maybe null if the lb has been deleted
213
		if(virtualIps != null){
214
			for (int i = 0; i < virtualIps.size(); i++) {
215
				TextView tv = new TextView(this.getBaseContext());
216
				tv.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
217
				tv.setTypeface(tv.getTypeface(), 1); // 1 == bold
218
				tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
219
				tv.setTextColor(Color.WHITE);
220
				tv.setText(virtualIps.get(i).getAddress());
221
				layout.addView(tv, layoutIndex++);
222
			}
223
		}
224

  
225
		//TextView vipId = (TextView) findViewById(R.id.view_vip_id);
226
		//vipId.setText(loadBalancer.getVirtualIps().get(0).getId());
175 227

  
176
		TextView address = (TextView) findViewById(R.id.view_vip_address);
177
		address.setText(returnLoadBalancer.getVirtualIps().get(0).getAddress());
228
		//TextView address = (TextView) findViewById(R.id.view_vip_address);
178 229

  
179
		TextView ipVersion = (TextView) findViewById(R.id.view_ipversion);
180
		ipVersion.setText(returnLoadBalancer.getVirtualIps().get(0).getIpVersion());
230
		//TextView ipVersion = (TextView) findViewById(R.id.view_ipversion);
231
		//ipVersion.setText(loadBalancer.getVirtualIps().get(0).getIpVersion());
181 232

  
182
		TextView type = (TextView) findViewById(R.id.view_vip_type);
183
		type.setText(returnLoadBalancer.getVirtualIps().get(0).getType());
233
		//TextView type = (TextView) findViewById(R.id.view_vip_type);
234
		//type.setText(loadBalancer.getVirtualIps().get(0).getType());
184 235

  
185 236
		loadNodeData();
186 237
	}
187 238

  
188 239
	private void loadNodeData() {
240
		int layoutIndex = 0; // public IPs start here
241
		LinearLayout layout = (LinearLayout) this.findViewById(R.id.node_addresses);   
242
		layout.removeAllViews();
243
		ArrayList<Node> nodeIps = loadBalancer.getNodes();
244
		
245
		/*
246
		 * need to sort the addresses because during polling
247
		 * their order can change and the display becomes
248
		 * jumpy
249
		 */
250
		ArrayList<String> addresses = new ArrayList<String>();
251
		for(Node n : nodeIps){
252
			addresses.add(n.getAddress());
253
		}
254
		
255
		Collections.sort(addresses);
256
				
257
		//may be null if lb has been deleted
258
		if(nodeIps != null){
259
			for (int i = 0; i < nodeIps.size(); i++) {
260
				TextView tv = new TextView(this.getBaseContext());
261
				tv.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
262
				tv.setTypeface(tv.getTypeface(), 1); // 1 == bold
263
				tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
264
				tv.setTextColor(Color.WHITE);
265
				tv.setText(addresses.get(i));
266
				layout.addView(tv, layoutIndex++);
267
			}
268
		}
269

  
270
		/*
189 271
		TextView nodeID = (TextView) findViewById(R.id.view_node_id);
190
		nodeID.setText(returnLoadBalancer.getNodes().get(0).getId());
272
		nodeID.setText(loadBalancer.getNodes().get(0).getId());
191 273

  
192 274
		TextView address = (TextView) findViewById(R.id.view_node_address);
193
		address.setText(returnLoadBalancer.getNodes().get(0).getAddress());
275
		address.setText(loadBalancer.getNodes().get(0).getAddress());
194 276

  
195 277
		TextView nodePort = (TextView) findViewById(R.id.view_node_port);
196
		nodePort.setText(returnLoadBalancer.getNodes().get(0).getPort());
278
		nodePort.setText(loadBalancer.getNodes().get(0).getPort());
197 279

  
198 280
		TextView condition = (TextView) findViewById(R.id.view_node_condition);
199
		condition.setText(returnLoadBalancer.getNodes().get(0).getCondition());
281
		condition.setText(loadBalancer.getNodes().get(0).getCondition());
200 282

  
201 283
		TextView nodeStatus = (TextView) findViewById(R.id.view_node_status);
202
		nodeStatus.setText(returnLoadBalancer.getNodes().get(0).getStatus());
203

  
284
		nodeStatus.setText(loadBalancer.getNodes().get(0).getStatus());
285
		 */
204 286
	}
205
	
287

  
206 288
	//setup menu for when menu button is pressed
207 289
	public boolean onCreateOptionsMenu(Menu menu) {
208 290
		super.onCreateOptionsMenu(menu);
......
210 292
		inflater.inflate(R.menu.view_loadbalancer_menu, menu);
211 293
		return true;
212 294
	} 
213
    
214
    @Override 
215
    public boolean onOptionsItemSelected(MenuItem item) {
216
    	switch (item.getItemId()) {
217
    	case R.id.refresh_loadbalancer:
218
    		new LoadLoadBalancerTask().execute((Void[]) null);   
219
    		return true;
220
    	}	
221
    	return false;
222
    } 
223
    
224
    @Override
295

  
296
	@Override 
297
	public boolean onOptionsItemSelected(MenuItem item) {
298
		switch (item.getItemId()) {
299
		case R.id.refresh_loadbalancer:
300
			new LoadLoadBalancerTask().execute((Void[]) null);   
301
			return true;
302
		}	
303
		return false;
304
	} 
305

  
306
	@Override
225 307
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
226 308
		super.onActivityResult(requestCode, resultCode, data);
227 309

  
228 310
		if (resultCode == RESULT_OK) {
229
    		new LoadLoadBalancerTask().execute((Void[]) null);   
230
		}
231
	}
232
	
233
	private void startLoadBalancerError(String message, HttpBundle bundle){
234
		Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
235
		viewIntent.putExtra("errorMessage", message);
236
		viewIntent.putExtra("response", bundle.getResponseText());
237
		viewIntent.putExtra("request", bundle.getCurlRequest());
238
		startActivity(viewIntent);
239
	}
240
	
241
	//using cloudServersException, it works for us too
242
	private CloudServersException parseCloudServersException(HttpResponse response) {
243
		CloudServersException cse = new CloudServersException();
244
		try {
245
		    BasicResponseHandler responseHandler = new BasicResponseHandler();
246
		    String body = responseHandler.handleResponse(response);
247
	    	CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
248
	    	SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
249
	    	XMLReader xmlReader = saxParser.getXMLReader();
250
	    	xmlReader.setContentHandler(parser);
251
	    	xmlReader.parse(new InputSource(new StringReader(body)));		    	
252
	    	cse = parser.getException();		    	
253
		} catch (ClientProtocolException e) {
254
			cse = new CloudServersException();
255
			cse.setMessage(e.getLocalizedMessage());
256
		} catch (IOException e) {
257
			cse = new CloudServersException();
258
			cse.setMessage(e.getLocalizedMessage());
259
		} catch (ParserConfigurationException e) {
260
			cse = new CloudServersException();
261
			cse.setMessage(e.getLocalizedMessage());
262
		} catch (SAXException e) {
263
			cse = new CloudServersException();
264
			cse.setMessage(e.getLocalizedMessage());
265
		} catch (FactoryConfigurationError e) {
266
			cse = new CloudServersException();
267
			cse.setMessage(e.getLocalizedMessage());
311
			new LoadLoadBalancerTask().execute((Void[]) null);   
268 312
		}
269
		return cse;
270 313
	}
271 314

  
272 315
	// HTTP request tasks
273
	private class PollServerTask extends AsyncTask<Void, Void, LoadBalancer> {
316
	private class PollLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> {
274 317

  
275 318
		@Override
276 319
		protected LoadBalancer doInBackground(Void... arg0) {
277 320
			try {
278
				returnLoadBalancer = (new LoadBalancerManager(context)).getLoadBalancerById(Integer.parseInt(loadBalancer.getId()));
321
				loadBalancer = (new LoadBalancerManager(getContext())).getLoadBalancerById(Integer.parseInt(loadBalancer.getId()));
279 322
			} catch (NumberFormatException e) {
280 323
				// we're polling, so need to show exceptions
281 324
			} catch (LoadBalancersException e) {
282 325
				// we're polling, so need to show exceptions
283 326
			}
284
			return returnLoadBalancer;
327
			return loadBalancer;
285 328
		}
286 329

  
287 330
		@Override
288 331
		protected void onPostExecute(LoadBalancer result) {
289
			returnLoadBalancer = result;
332
			loadBalancer = result;
290 333
			loadLoadBalancerData();
291 334
		}
292 335
	}
293 336

  
294 337
	private class LoadLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> {
338

  
295 339
		private LoadBalancersException exception;
340
		private String loadBalancerId;
296 341

  
297 342
		protected void onPreExecute() {
343
			loadBalancerId = loadBalancer.getId();
344
			/*
345
			 * set to null, so if config change occurs
346
			 * it will be reloaded in onCreate()
347
			 */
348
			loadBalancer = null;
298 349
			showDialog();
299 350
		}
300 351

  
......
302 353
		protected LoadBalancer doInBackground(Void... arg0) {
303 354
			LoadBalancer result = null;
304 355
			try {
305
				result = (new LoadBalancerManager(context)).getLoadBalancerById(Integer.parseInt(loadBalancer.getId()));
356
				result = (new LoadBalancerManager(getContext())).getLoadBalancerById(Integer.parseInt(loadBalancerId));
306 357
			} catch (LoadBalancersException e) {
307 358
				exception = e;
308 359
			}
309
			pDialog.dismiss();
310 360
			return result;
311 361
		}
312 362

  
313 363
		@Override
314 364
		protected void onPostExecute(LoadBalancer result) {
365
			hideDialog();
315 366
			if (exception != null) {
316
				pDialog.dismiss();
317 367
				showAlert("Error", exception.getMessage());
318 368
			}
319
			pDialog.dismiss();
320
			returnLoadBalancer = result;
369
			loadBalancer = result;
370

  
371
			if(!loadBalancer.getStatus().contains("DELETE")){
372
				setUpButtons();
373
			} else {
374
				setUpBadButtons();
375
			}
321 376
			loadLoadBalancerData();
322 377
		}
323 378
	} 
......
330 385
		protected void onPreExecute(){
331 386
			showDialog();
332 387
		}
333
		
388

  
334 389
		@Override
335 390
		protected HttpBundle doInBackground(Void... arg0) {
336 391
			HttpBundle bundle = null;
337 392
			try {
338
				bundle = (new LoadBalancerManager(context)).delete(loadBalancer);
393
				bundle = (new LoadBalancerManager(getContext())).delete(loadBalancer);
339 394
			} catch (CloudServersException e) {
340 395
				exception = e;
341 396
			}
......
344 399

  
345 400
		@Override
346 401
		protected void onPostExecute(HttpBundle bundle) {
347
			pDialog.dismiss();
402
			hideDialog();
348 403
			HttpResponse response = bundle.getResponse();
349 404
			if (response != null) {
350 405
				int statusCode = response.getStatusLine().getStatusCode();
......
355 410
				} else {
356 411
					CloudServersException cse = parseCloudServersException(response);
357 412
					if ("".equals(cse.getMessage())) {
358
						startLoadBalancerError("There was a problem deleting your load balancer.", bundle);
413
						showError("There was a problem deleting your load balancer.", bundle);
359 414
					} else {
360
						startLoadBalancerError("There was a problem deleting your load balancer: " + cse.getMessage(), bundle);
415
						showError("There was a problem deleting your load balancer: " + cse.getMessage(), bundle);
361 416
					}
362 417
				}
363 418
			} else if (exception != null) {
364
				startLoadBalancerError("There was a problem deleting your load balancer: " + exception.getMessage(), bundle);				
419
				showError("There was a problem deleting your load balancer: " + exception.getMessage(), bundle);				
365 420
			}			
366 421
		}
367 422
	}
368 423

  
369
	private void showAlert(String title, String message) {
370
		// Can't create handler inside thread that has not called
371
		// Looper.prepare()
372
		// Looper.prepare();
373
		try {
374
			AlertDialog alert = new AlertDialog.Builder(this).create();
375
			alert.setTitle(title);
376
			alert.setMessage(message);
377
			alert.setButton("OK", new DialogInterface.OnClickListener() {
378
				public void onClick(DialogInterface dialog, int which) {
379
					return;
380
				}
381
			});
382
			alert.show();
383
		} catch (Exception e) {
384
			e.printStackTrace();
385
		}
386
	}
387 424

  
388
	protected void showDialog() {
389
		pDialog = new ProgressDialog(this, R.style.NewDialog);
390
		// // Set blur to background
391
		WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes();
392
		lp.dimAmount = 0.0f;
393
		pDialog.getWindow().setAttributes(lp);
394
		pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
395
		pDialog.show();
396
		pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
397
	}
398 425
}

Also available in: Unified diff