root / src / com / rackspacecloud / android / SharedVipActivity.java @ 01340459
History | View | Annotate | Download (6.3 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
|
6 |
import android.content.Intent; |
7 |
import android.graphics.Color; |
8 |
import android.os.AsyncTask; |
9 |
import android.os.Bundle; |
10 |
import android.view.LayoutInflater; |
11 |
import android.view.View; |
12 |
import android.view.ViewGroup; |
13 |
import android.widget.ArrayAdapter; |
14 |
import android.widget.ListView; |
15 |
import android.widget.TextView; |
16 |
|
17 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
18 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
19 |
import com.rackspace.cloud.loadbalancer.api.client.VirtualIp; |
20 |
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException; |
21 |
|
22 |
public class SharedVipActivity extends CloudListActivity { |
23 |
|
24 |
private LoadBalancer[] loadBalancers; |
25 |
private VirtualIp[] vips; |
26 |
private String loadBalancerPort; |
27 |
private String loadBalancerRegion; |
28 |
private VirtualIp selectedVip;
|
29 |
|
30 |
@Override
|
31 |
public void onCreate(Bundle savedInstanceState) { |
32 |
super.onCreate(savedInstanceState);
|
33 |
setContentView(R.layout.list_vips); |
34 |
loadBalancerPort = (String) this.getIntent().getExtras().get("loadBalancerPort"); |
35 |
loadBalancerRegion = (String) this.getIntent().getExtras().get("loadBalancerRegion"); |
36 |
selectedVip = (VirtualIp) this.getIntent().getExtras().get("selectedVip"); |
37 |
restoreState(savedInstanceState); |
38 |
} |
39 |
|
40 |
@Override
|
41 |
protected void onSaveInstanceState(Bundle outState) { |
42 |
super.onSaveInstanceState(outState);
|
43 |
outState.putSerializable("loadBalancers", loadBalancers);
|
44 |
} |
45 |
|
46 |
protected void restoreState(Bundle state) { |
47 |
super.restoreState(state);
|
48 |
|
49 |
if (state != null && state.containsKey("loadBalancers") && state.getSerializable("loadBalancers") != null) { |
50 |
loadBalancers = (LoadBalancer[]) state.getSerializable("loadBalancers"); |
51 |
if (loadBalancers.length == 0) { |
52 |
displayNoLoadBalancerCell(); |
53 |
} else {
|
54 |
getListView().setDividerHeight(1); // restore divider lines |
55 |
setListAdapter(new VirtualIpAdapter());
|
56 |
} |
57 |
} else {
|
58 |
loadLoadBalancers(); |
59 |
} |
60 |
} |
61 |
|
62 |
private void displayNoLoadBalancerCell() { |
63 |
String a[] = new String[1]; |
64 |
a[0] = "No Load Balancers"; |
65 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noloadbalancerscell, R.id.no_loadbalancers_label, a)); |
66 |
getListView().setTextFilterEnabled(true);
|
67 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
68 |
getListView().setItemsCanFocus(false);
|
69 |
} |
70 |
|
71 |
private void setLoadBalancersList(ArrayList<VirtualIp> vips) { |
72 |
if (vips == null) { |
73 |
vips = new ArrayList<VirtualIp>(); |
74 |
} |
75 |
|
76 |
this.vips = new VirtualIp[vips.size()]; |
77 |
|
78 |
if (vips != null) { |
79 |
for (int i = 0; i < vips.size(); i++) { |
80 |
VirtualIp virtualIp = vips.get(i); |
81 |
this.vips[i] = virtualIp;
|
82 |
} |
83 |
} |
84 |
|
85 |
if (this.vips.length == 0) { |
86 |
displayNoLoadBalancerCell(); |
87 |
} else {
|
88 |
getListView().setDividerHeight(1); // restore divider lines |
89 |
setListAdapter(new VirtualIpAdapter());
|
90 |
} |
91 |
} |
92 |
|
93 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
94 |
/*
|
95 |
* only allow clicks on vips that do not have the same port
|
96 |
* as the lb and are in same region
|
97 |
*/
|
98 |
if (vips != null && vips.length > 0) { |
99 |
if(vips[position].getLoadBalancer().getPort().equals(loadBalancerPort)){
|
100 |
showToast("Cannot use this Virtual IP. The same port cannot be used on multiple load balancers for a Shared Virtual IP.");
|
101 |
} else if(!vips[position].getLoadBalancer().getRegion().equals(loadBalancerRegion)){ |
102 |
showToast("Cannot use this Virtual IP. The Shared Virtual IP must come the same region as the new load balancer.");
|
103 |
} else {
|
104 |
Intent viewIntent = new Intent();
|
105 |
selectedVip = vips[position]; |
106 |
viewIntent.putExtra("selectedVip", vips[position]);
|
107 |
setResult(RESULT_OK, viewIntent); |
108 |
//the redisplay will color the users selection white
|
109 |
setLoadBalancersList(new ArrayList<VirtualIp>(Arrays.asList(vips))); |
110 |
} |
111 |
} |
112 |
} |
113 |
|
114 |
// * Adapter/
|
115 |
class VirtualIpAdapter extends ArrayAdapter<VirtualIp> { |
116 |
|
117 |
VirtualIpAdapter() { |
118 |
super(SharedVipActivity.this,
|
119 |
R.layout.sharedvipcell, vips); |
120 |
} |
121 |
|
122 |
public View getView(int position, View convertView, ViewGroup parent) { |
123 |
|
124 |
VirtualIp virtualIp = vips[position]; |
125 |
LayoutInflater inflater = getLayoutInflater(); |
126 |
View row = inflater.inflate(R.layout.sharedvipcell,
|
127 |
parent, false);
|
128 |
|
129 |
TextView vipAddress = (TextView) row.findViewById(R.id.vip_address); |
130 |
vipAddress.setText(virtualIp.getAddress()); |
131 |
|
132 |
TextView type = (TextView) row.findViewById(R.id.vip_type); |
133 |
type.setText(virtualIp.getType()); |
134 |
|
135 |
TextView name = (TextView) row.findViewById(R.id.load_balancer_name); |
136 |
name.setText(virtualIp.getLoadBalancer().getName()); |
137 |
|
138 |
TextView protocol = (TextView) row.findViewById(R.id.vip_protocol); |
139 |
protocol.setText(virtualIp.getLoadBalancer().getProtocol() |
140 |
+ "(" + virtualIp.getLoadBalancer().getPort() + ")"); |
141 |
|
142 |
//Set the text of the selected vip (if there is one)
|
143 |
//to white so the user knows what they picked
|
144 |
boolean isSelected = selectedVip != null && selectedVip.getAddress().equals(vips[position].getAddress()); |
145 |
if(isSelected){
|
146 |
vipAddress.setTextColor(Color.WHITE);
|
147 |
type.setTextColor(Color.WHITE);
|
148 |
name.setTextColor(Color.WHITE);
|
149 |
protocol.setTextColor(Color.WHITE);
|
150 |
protocol.setTextColor(Color.WHITE);
|
151 |
} |
152 |
return (row);
|
153 |
} |
154 |
} |
155 |
|
156 |
private void loadLoadBalancers() { |
157 |
new LoadLoadBalancersTask().execute((Void[]) null); |
158 |
} |
159 |
|
160 |
private class LoadLoadBalancersTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> { |
161 |
private LoadBalancersException exception;
|
162 |
|
163 |
@Override
|
164 |
protected void onPreExecute(){ |
165 |
showDialog(); |
166 |
} |
167 |
|
168 |
@Override
|
169 |
protected ArrayList<LoadBalancer> doInBackground(Void... arg0) { |
170 |
ArrayList<LoadBalancer> loadBalancers = null; |
171 |
try {
|
172 |
loadBalancers = (new LoadBalancerManager(context)).createList();
|
173 |
} catch (LoadBalancersException e) {
|
174 |
exception = e; |
175 |
} |
176 |
return loadBalancers;
|
177 |
} |
178 |
|
179 |
@Override
|
180 |
protected void onPostExecute(ArrayList<LoadBalancer> result) { |
181 |
hideDialog(); |
182 |
if (exception != null) { |
183 |
showAlert("Error", exception.getMessage());
|
184 |
} |
185 |
ArrayList<VirtualIp> vipList = getVipList(result);
|
186 |
setLoadBalancersList(vipList); |
187 |
} |
188 |
} |
189 |
|
190 |
private ArrayList<VirtualIp> getVipList(ArrayList<LoadBalancer> result){ |
191 |
ArrayList<VirtualIp> vips = new ArrayList<VirtualIp>(); |
192 |
for(LoadBalancer lb : result){
|
193 |
for(VirtualIp ip : lb.getVirtualIps()){
|
194 |
ip.setLoadBalancer(lb); |
195 |
vips.add(ip); |
196 |
} |
197 |
} |
198 |
return vips;
|
199 |
} |
200 |
|
201 |
} |