Revision f506786e src/com/rackspacecloud/android/SharedVipActivity.java
b/src/com/rackspacecloud/android/SharedVipActivity.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 android.content.Intent; |
7 |
import android.graphics.Color; |
|
8 | 6 |
import android.os.AsyncTask; |
9 | 7 |
import android.os.Bundle; |
10 |
import android.view.LayoutInflater; |
|
8 |
import android.util.Log; |
|
9 |
import android.util.TypedValue; |
|
11 | 10 |
import android.view.View; |
12 |
import android.view.ViewGroup; |
|
13 |
import android.widget.ArrayAdapter; |
|
14 |
import android.widget.ListView; |
|
15 |
import android.widget.TextView; |
|
11 |
import android.view.View.OnClickListener; |
|
12 |
import android.widget.Button; |
|
13 |
import android.widget.RadioButton; |
|
14 |
import android.widget.RadioGroup; |
|
15 |
import android.widget.RadioGroup.OnCheckedChangeListener; |
|
16 | 16 |
|
17 | 17 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
18 | 18 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
19 | 19 |
import com.rackspace.cloud.loadbalancer.api.client.VirtualIp; |
20 | 20 |
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException; |
21 | 21 |
|
22 |
public class SharedVipActivity extends CloudListActivity {
|
|
22 |
public class SharedVipActivity extends CloudActivity { |
|
23 | 23 |
|
24 |
private LoadBalancer[] loadBalancers; |
|
25 | 24 |
private VirtualIp[] vips; |
26 | 25 |
private String loadBalancerPort; |
27 | 26 |
private String loadBalancerRegion; |
28 | 27 |
private VirtualIp selectedVip; |
29 |
|
|
28 |
private RadioGroup vipGroup; |
|
29 |
|
|
30 | 30 |
@Override |
31 | 31 |
public void onCreate(Bundle savedInstanceState) { |
32 | 32 |
super.onCreate(savedInstanceState); |
... | ... | |
36 | 36 |
selectedVip = (VirtualIp) this.getIntent().getExtras().get("selectedVip"); |
37 | 37 |
restoreState(savedInstanceState); |
38 | 38 |
} |
39 |
|
|
39 |
|
|
40 | 40 |
@Override |
41 | 41 |
protected void onSaveInstanceState(Bundle outState) { |
42 | 42 |
super.onSaveInstanceState(outState); |
43 |
outState.putSerializable("loadBalancers", loadBalancers); |
|
43 |
outState.putSerializable("vips", vips); |
|
44 |
outState.putSerializable("selectedVip", selectedVip); |
|
44 | 45 |
} |
45 | 46 |
|
46 | 47 |
protected void restoreState(Bundle state) { |
47 | 48 |
super.restoreState(state); |
49 |
|
|
50 |
setupButton(); |
|
51 |
|
|
52 |
vipGroup = (RadioGroup) findViewById(R.id.vip_group); |
|
53 |
|
|
54 |
if(state != null && state.containsKey("selectedVip")){ |
|
55 |
selectedVip = (VirtualIp) state.getSerializable("selectedVip"); |
|
56 |
} |
|
48 | 57 |
|
49 |
if (state != null && state.containsKey("loadBalancers") && state.getSerializable("loadBalancers") != null) {
|
|
50 |
loadBalancers = (LoadBalancer[]) state.getSerializable("loadBalancers");
|
|
51 |
if (loadBalancers.length == 0) {
|
|
52 |
displayNoLoadBalancerCell();
|
|
58 |
if (state != null && state.containsKey("vips") && state.getSerializable("vips") != null) {
|
|
59 |
vips = (VirtualIp[]) state.getSerializable("vips");
|
|
60 |
if (vips.length == 0) {
|
|
61 |
displayNoVipsCell();
|
|
53 | 62 |
} else { |
54 |
getListView().setDividerHeight(1); // restore divider lines |
|
55 |
setListAdapter(new VirtualIpAdapter()); |
|
63 |
displayRadioButtons(); |
|
56 | 64 |
} |
57 | 65 |
} else { |
58 |
loadLoadBalancers();
|
|
66 |
loadVirtualIps();
|
|
59 | 67 |
} |
60 | 68 |
} |
69 |
|
|
70 |
private void setupButton(){ |
|
71 |
Button submit = (Button) findViewById(R.id.select_vip_button); |
|
72 |
submit.setOnClickListener(new OnClickListener() { |
|
73 |
|
|
74 |
@Override |
|
75 |
public void onClick(View v) { |
|
76 |
Intent viewIntent = new Intent(); |
|
77 |
viewIntent.putExtra("selectedVip", selectedVip); |
|
78 |
setResult(RESULT_OK, viewIntent); |
|
79 |
finish(); |
|
80 |
} |
|
81 |
}); |
|
82 |
} |
|
61 | 83 |
|
62 |
private void displayNoLoadBalancerCell() {
|
|
63 |
String a[] = new String[1]; |
|
84 |
private void displayNoVipsCell() {
|
|
85 |
/* String a[] = new String[1];
|
|
64 | 86 |
a[0] = "No Load Balancers"; |
65 | 87 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noloadbalancerscell, R.id.no_loadbalancers_label, a)); |
66 | 88 |
getListView().setTextFilterEnabled(true); |
67 | 89 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
68 | 90 |
getListView().setItemsCanFocus(false); |
91 |
*/ |
|
69 | 92 |
} |
70 |
|
|
93 |
|
|
94 |
private void displayRadioButtons(){ |
|
95 |
for(VirtualIp vip : vips){ |
|
96 |
RadioButton button = new RadioButton(getContext()); |
|
97 |
//Display the load balancer info next to the radio |
|
98 |
//buttons |
|
99 |
button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14); |
|
100 |
button.setText(vip.getAddress() + "\n" + |
|
101 |
"Type: " + vip.getType() + "\n" + |
|
102 |
"Load Balancer: " + vip.getLoadBalancer().getName() + "\n"); |
|
103 |
|
|
104 |
//if can't add vip make it unselectable |
|
105 |
if((vip.getLoadBalancer().getPort().equals(loadBalancerPort) |
|
106 |
|| !vip.getLoadBalancer().getRegion().equals(loadBalancerRegion))){ |
|
107 |
button.setEnabled(false); |
|
108 |
} |
|
109 |
vipGroup.addView(button); |
|
110 |
if(selectedVip != null && selectedVip.getId().equals(vip.getId())){ |
|
111 |
((RadioButton)vipGroup.getChildAt(vipGroup.getChildCount() - 1)).toggle(); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
vipGroup.setOnCheckedChangeListener (new OnCheckedChangeListener() { |
|
116 |
|
|
117 |
@Override |
|
118 |
public void onCheckedChanged(RadioGroup group, int checkedId) { |
|
119 |
|
|
120 |
View radioButton = group.findViewById(checkedId); |
|
121 |
int index = group.indexOfChild(radioButton); |
|
122 |
|
|
123 |
if(vips[index].getLoadBalancer().getPort().equals(loadBalancerPort)){ |
|
124 |
showToast("Cannot use this Virtual IP. The same port cannot be used on multiple load balancers for a Shared Virtual IP."); |
|
125 |
} else if(!vips[index].getLoadBalancer().getRegion().equals(loadBalancerRegion)){ |
|
126 |
showToast("Cannot use this Virtual IP. The Shared Virtual IP must come the same region as the new load balancer."); |
|
127 |
} else { |
|
128 |
Log.d("info", "the selected vip is " + vips[index].getAddress()); |
|
129 |
selectedVip = vips[index]; |
|
130 |
} |
|
131 |
} |
|
132 |
}); |
|
133 |
|
|
134 |
} |
|
135 |
|
|
71 | 136 |
private void setLoadBalancersList(ArrayList<VirtualIp> vips) { |
72 | 137 |
if (vips == null) { |
73 | 138 |
vips = new ArrayList<VirtualIp>(); |
74 | 139 |
} |
75 |
|
|
140 |
|
|
76 | 141 |
this.vips = new VirtualIp[vips.size()]; |
77 | 142 |
|
78 | 143 |
if (vips != null) { |
... | ... | |
83 | 148 |
} |
84 | 149 |
|
85 | 150 |
if (this.vips.length == 0) { |
86 |
displayNoLoadBalancerCell();
|
|
151 |
displayNoVipsCell();
|
|
87 | 152 |
} else { |
88 |
getListView().setDividerHeight(1); // restore divider lines |
|
89 |
setListAdapter(new VirtualIpAdapter()); |
|
153 |
displayRadioButtons(); |
|
90 | 154 |
} |
91 | 155 |
} |
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 | 156 |
|
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); |
|
157 |
private void loadVirtualIps() { |
|
158 |
new LoadVirtualIpsTask().execute((Void[]) null); |
|
158 | 159 |
} |
159 |
|
|
160 |
private class LoadLoadBalancersTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> {
|
|
160 |
|
|
161 |
private class LoadVirtualIpsTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> {
|
|
161 | 162 |
private LoadBalancersException exception; |
162 |
|
|
163 |
|
|
163 | 164 |
@Override |
164 | 165 |
protected void onPreExecute(){ |
165 | 166 |
showDialog(); |
166 | 167 |
} |
167 |
|
|
168 |
|
|
168 | 169 |
@Override |
169 | 170 |
protected ArrayList<LoadBalancer> doInBackground(Void... arg0) { |
170 | 171 |
ArrayList<LoadBalancer> loadBalancers = null; |
... | ... | |
175 | 176 |
} |
176 | 177 |
return loadBalancers; |
177 | 178 |
} |
178 |
|
|
179 |
|
|
179 | 180 |
@Override |
180 | 181 |
protected void onPostExecute(ArrayList<LoadBalancer> result) { |
181 | 182 |
hideDialog(); |
... | ... | |
186 | 187 |
setLoadBalancersList(vipList); |
187 | 188 |
} |
188 | 189 |
} |
189 |
|
|
190 |
|
|
190 | 191 |
private ArrayList<VirtualIp> getVipList(ArrayList<LoadBalancer> result){ |
191 | 192 |
ArrayList<VirtualIp> vips = new ArrayList<VirtualIp>(); |
192 | 193 |
for(LoadBalancer lb : result){ |
... | ... | |
197 | 198 |
} |
198 | 199 |
return vips; |
199 | 200 |
} |
200 |
|
|
201 |
|
|
201 | 202 |
} |
Also available in: Unified diff