commit b722cab3b8a0178fb131654415445242cf08b5c4
Author: Adam Menz <adam.menz@rackspace.com>
Date:   Thu Jul 21 14:25:16 2011 -0500

    added new load balancing features
    working now is add/remove lb, add/remove nodes
    and edit lb/nodes

diff --git a/src/com/rackspacecloud/android/AddLoadBalancerActivity.java b/src/com/rackspacecloud/android/AddLoadBalancerActivity.java
index 9619826..7c4f2be 100644
--- a/src/com/rackspacecloud/android/AddLoadBalancerActivity.java
+++ b/src/com/rackspacecloud/android/AddLoadBalancerActivity.java
@@ -1,53 +1,31 @@
 package com.rackspacecloud.android;
 
-import java.io.IOException;
-import java.io.StringReader;
 import java.util.ArrayList;
 
-import javax.xml.parsers.FactoryConfigurationError;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParser;
-import javax.xml.parsers.SAXParserFactory;
-
 import org.apache.http.HttpResponse;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.impl.client.BasicResponseHandler;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.XMLReader;
 
 import com.rackspace.cloud.loadbalancer.api.client.Algorithm;
 import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
 import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
 import com.rackspace.cloud.loadbalancer.api.client.Node;
 import com.rackspace.cloud.loadbalancer.api.client.Protocol;
-import com.rackspace.cloud.servers.api.client.Account;
 import com.rackspace.cloud.servers.api.client.CloudServersException;
 import com.rackspace.cloud.servers.api.client.http.HttpBundle;
-import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
 
 import android.app.Activity;
-import android.app.AlertDialog;
-import android.app.ProgressDialog;
-import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.os.Bundle;
-import android.util.Log;
 import android.view.View;
-import android.view.WindowManager;
 import android.view.View.OnClickListener;
-import android.view.ViewGroup.LayoutParams;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.EditText;
-import android.widget.ProgressBar;
 import android.widget.Spinner;
 
-public class AddLoadBalancerActivity extends Activity implements OnItemSelectedListener {
+public class AddLoadBalancerActivity extends CloudActivity implements OnItemSelectedListener {
 
 	//TODO Shared Virtual IP (not in API though?)
 	private static final String[] VIP_TYPES = {"Public", "ServiceNet"};
@@ -69,9 +47,7 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 	private String selectedName;
 	private String selectedPort;
 	private ArrayList<Node> nodes;
-	private Context context;
 	private LoadBalancer loadBalancer;
-	ProgressDialog pDialog;
 
 	@Override
 	public void onCreate(Bundle savedInstanceState) {
@@ -81,9 +57,8 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 	}
 
 	@SuppressWarnings("unchecked")
-	private void restoreState(Bundle state) {
-		
-		context = getApplicationContext();
+	protected void restoreState(Bundle state) {
+		super.restoreState(state);
 		
 		portText = (EditText)findViewById(R.id.edit_port_text);
 
@@ -110,7 +85,7 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
 		if (parent == protocolSpinner) {
 			selectedProtocol = protocols[position];
-			portText.setText(protocols[position].getDefaultPort());
+			//portText.setText(protocols[position].getDefaultPort());
 		} 
 		else if (parent == algorithmSpinner){
 			selectedAlgorithm = algorithms[position];
@@ -133,28 +108,9 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 
 			@Override
 			public void onClick(View v) {
-				/*
-				selectedName = ((EditText)findViewById(R.id.edit_lb_name_text)).getText().toString();
-				selectedPort = ((EditText)findViewById(R.id.edit_port_text)).getText().toString();
-				if(!validName()){
-					showAlert("Error", "Load balancer name cannot be blank.");
-				}
-				else if(!validPort()){
-					showAlert("Error", "Must have a protocol port number that is between 1 and 65535.");
-				}
-				 */
-				//else{
 				Intent viewIntent = new Intent(getApplicationContext(), AddNodesActivity.class);
-				/*viewIntent.putExtra("name", selectedName);
-					viewIntent.putExtra("protocol", selectedProtocol);
-					viewIntent.putExtra("port", selectedPort);
-					viewIntent.putExtra("vip", selectedVipType);
-					viewIntent.putExtra("algorithm", selectedAlgorithm);
-					viewIntent.putExtra("region", selectedRegion);
-				 */
 				viewIntent.putExtra("nodes", nodes);
 				startActivityForResult(viewIntent, ADD_NODES_ACTIVITY_CODE);
-				//}
 			}
 		});
 
@@ -172,16 +128,6 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 					showAlert("Error", "Must have a protocol port number that is between 1 and 65535.");
 				}
 				else{
-					/*
-					Intent viewIntent = new Intent(getApplicationContext(), AddNodesActivity.class);
-					viewIntent.putExtra("name", selectedName);
-					viewIntent.putExtra("protocol", selectedProtocol);
-					viewIntent.putExtra("port", selectedPort);
-					viewIntent.putExtra("vip", selectedVipType);
-					viewIntent.putExtra("algorithm", selectedAlgorithm);
-					viewIntent.putExtra("region", selectedRegion);
-					viewIntent.putExtra("nodes", nodes);
-					 */
 					loadBalancer = new LoadBalancer();
 					loadBalancer.setName(selectedName);
 					loadBalancer.setProtocol(selectedProtocol.getName());
@@ -189,7 +135,6 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 					loadBalancer.setVirtualIpType(selectedVipType);
 					loadBalancer.setAlgorithm(selectedAlgorithm.getName());
 					loadBalancer.setNodes(nodes);
-					
 					new AddLoadBalancerTask().execute();
 				}
 			}
@@ -254,68 +199,16 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 		algorithmSpinner.setAdapter(algorithmAdapter);
 	}
 
-	private void showAlert(String title, String message) {
-		AlertDialog alert = new AlertDialog.Builder(this).create();
-		alert.setTitle(title);
-		alert.setMessage(message);
-		alert.setButton("OK", new DialogInterface.OnClickListener() {
-			public void onClick(DialogInterface dialog, int which) {
-				return;
-			} }); 
-		alert.show();
-	}
-	
-	private void startLoadBalancerError(String message, HttpBundle bundle){
-		Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
-		viewIntent.putExtra("errorMessage", message);
-		viewIntent.putExtra("response", bundle.getResponseText());
-		viewIntent.putExtra("request", bundle.getCurlRequest());
-		startActivity(viewIntent);
-	}
-	
-	//using cloudServersException, it works for us too
-	private CloudServersException parseCloudServersException(HttpResponse response) {
-		CloudServersException cse = new CloudServersException();
-		try {
-		    BasicResponseHandler responseHandler = new BasicResponseHandler();
-		    String body = responseHandler.handleResponse(response);
-	    	CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
-	    	SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
-	    	XMLReader xmlReader = saxParser.getXMLReader();
-	    	xmlReader.setContentHandler(parser);
-	    	xmlReader.parse(new InputSource(new StringReader(body)));		    	
-	    	cse = parser.getException();		    	
-		} catch (ClientProtocolException e) {
-			cse = new CloudServersException();
-			cse.setMessage(e.getLocalizedMessage());
-		} catch (IOException e) {
-			cse = new CloudServersException();
-			cse.setMessage(e.getLocalizedMessage());
-		} catch (ParserConfigurationException e) {
-			cse = new CloudServersException();
-			cse.setMessage(e.getLocalizedMessage());
-		} catch (SAXException e) {
-			cse = new CloudServersException();
-			cse.setMessage(e.getLocalizedMessage());
-		} catch (FactoryConfigurationError e) {
-			cse = new CloudServersException();
-			cse.setMessage(e.getLocalizedMessage());
-		}
-		return cse;
-	}
-
 	@SuppressWarnings("unchecked")
 	@Override
 	protected void onActivityResult (int requestCode, int resultCode, Intent data){
 		if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_OK){
 			//set node list
-			Log.d("info", "Result ok");
 			nodes = ((ArrayList<Node>)data.getSerializableExtra("nodes"));
-			Log.d("info", "the length is " + nodes.size());
 		}
 		else if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_CANCELED){
 			//don't change list
-			Log.d("info", "Result cancel");
+			
 		}
 	}
 
@@ -330,7 +223,7 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 		protected HttpBundle doInBackground(Void... arg0) {
 			HttpBundle bundle = null;
 			try {
-				bundle = (new LoadBalancerManager(context)).create(loadBalancer, LoadBalancer.getRegionUrl(selectedRegion));
+				bundle = (new LoadBalancerManager(getContext())).create(loadBalancer, LoadBalancer.getRegionUrl(selectedRegion));
 			} catch (CloudServersException e) {
 				exception = e;
 			}
@@ -339,7 +232,7 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 
 		@Override
 		protected void onPostExecute(HttpBundle bundle) {
-			pDialog.dismiss();
+			hideDialog();
 			HttpResponse response = bundle.getResponse();
 			if (response != null) {
 				int statusCode = response.getStatusLine().getStatusCode();
@@ -349,28 +242,16 @@ public class AddLoadBalancerActivity extends Activity implements OnItemSelectedL
 				} else {
 					CloudServersException cse = parseCloudServersException(response);
 					if ("".equals(cse.getMessage())) {
-						startLoadBalancerError("There was a problem creating your load balancer.", bundle);
+						showError("There was a problem creating your load balancer.", bundle);
 					} else {
 						//if container with same name already exists
-						startLoadBalancerError("There was a problem creating your load balancer: " + cse.getMessage() + "\n See details for more information", bundle);
+						showError("There was a problem creating your load balancer: " + cse.getMessage() + "\n See details for more information", bundle);
 					}
 				}
 			} else if (exception != null) {
-				startLoadBalancerError("There was a problem creating your container: " + exception.getMessage()+"\n See details for more information.", bundle);				
+				showError("There was a problem creating your container: " + exception.getMessage()+"\n See details for more information.", bundle);				
 			}
 			finish();
 		}
 	}
-	
-	protected void showDialog() {
-		pDialog = new ProgressDialog(this, R.style.NewDialog);
-		// // Set blur to background
-		WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes();
-		lp.dimAmount = 0.0f;
-		pDialog.getWindow().setAttributes(lp);
-		pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
-		pDialog.show();
-		pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
-	}
-
 }
