Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / SharedVipActivity.java @ 48601850

History | View | Annotate | Download (6 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.util.ArrayList;
4

    
5
import android.content.Intent;
6
import android.os.AsyncTask;
7
import android.os.Bundle;
8
import android.util.Log;
9
import android.util.TypedValue;
10
import android.view.View;
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

    
17
import com.rackspace.cloud.android.R;
18
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
19
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
20
import com.rackspace.cloud.loadbalancer.api.client.VirtualIp;
21
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException;
22

    
23
public class SharedVipActivity extends CloudActivity {
24

    
25
        private VirtualIp[] vips;
26
        private String loadBalancerPort;
27
        private String loadBalancerRegion;
28
        private VirtualIp selectedVip;
29
        private RadioGroup vipGroup;
30

    
31
        @Override
32
        public void onCreate(Bundle savedInstanceState) {
33
                super.onCreate(savedInstanceState);
34
                setContentView(R.layout.list_vips);
35
                loadBalancerPort = (String) this.getIntent().getExtras().get("loadBalancerPort");
36
                loadBalancerRegion = (String) this.getIntent().getExtras().get("loadBalancerRegion");
37
                selectedVip = (VirtualIp) this.getIntent().getExtras().get("selectedVip");
38
                restoreState(savedInstanceState);
39
        }
40

    
41
        @Override
42
        protected void onSaveInstanceState(Bundle outState) {
43
                super.onSaveInstanceState(outState);
44
                outState.putSerializable("vips", vips);
45
                outState.putSerializable("selectedVip", selectedVip);
46
        }
47

    
48
        protected void restoreState(Bundle state) {
49
                super.restoreState(state);
50

    
51
                setupButton();
52
                
53
                vipGroup = (RadioGroup) findViewById(R.id.vip_group);
54

    
55
                if(state != null && state.containsKey("selectedVip")){
56
                        selectedVip = (VirtualIp) state.getSerializable("selectedVip");
57
                } 
58
                
59
                if (state != null && state.containsKey("vips") && state.getSerializable("vips") != null) {
60
                        vips = (VirtualIp[]) state.getSerializable("vips");
61
                        if (vips.length == 0) {
62
                                displayNoVipsCell();
63
                        } else {
64
                                displayRadioButtons();
65
                        }
66
                } else {
67
                        loadVirtualIps();
68
                }
69
        }
70
        
71
        private void setupButton(){
72
                Button submit = (Button) findViewById(R.id.select_vip_button);
73
                submit.setOnClickListener(new OnClickListener() {
74
                        
75
                        @Override
76
                        public void onClick(View v) {
77
                                Intent viewIntent = new Intent();
78
                                viewIntent.putExtra("selectedVip", selectedVip);
79
                                setResult(RESULT_OK, viewIntent);
80
                                finish();
81
                        }
82
                });
83
        }
84

    
85
        private void displayNoVipsCell() {
86
                /*        String a[] = new String[1];
87
                a[0] = "No Load Balancers";
88
                setListAdapter(new ArrayAdapter<String>(this, R.layout.noloadbalancerscell, R.id.no_loadbalancers_label, a));
89
                getListView().setTextFilterEnabled(true);
90
                getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
91
                getListView().setItemsCanFocus(false);
92
                 */
93
        }
94

    
95
        private void displayRadioButtons(){
96
                for(VirtualIp vip : vips){
97
                        RadioButton button = new RadioButton(getContext());
98
                        //Display the load balancer info next to the radio
99
                        //buttons
100
                        button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
101
                        button.setText(vip.getAddress() + "\n" +
102
                                        "Type: " + vip.getType() + "\n" +
103
                                        "Load Balancer: " + vip.getLoadBalancer().getName() + "\n");
104

    
105
                        //if can't add vip make it unselectable
106
                        if((vip.getLoadBalancer().getPort().equals(loadBalancerPort) 
107
                                        || !vip.getLoadBalancer().getRegion().equals(loadBalancerRegion))){
108
                                button.setEnabled(false);
109
                        }
110
                        vipGroup.addView(button);
111
                        if(selectedVip != null && selectedVip.getId().equals(vip.getId())){
112
                                ((RadioButton)vipGroup.getChildAt(vipGroup.getChildCount() - 1)).toggle();
113
                        }
114
                }
115
                
116
                vipGroup.setOnCheckedChangeListener (new OnCheckedChangeListener() {
117

    
118
                        @Override
119
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
120

    
121
                                View radioButton = group.findViewById(checkedId);
122
                                int index = group.indexOfChild(radioButton);
123

    
124
                                if(vips[index].getLoadBalancer().getPort().equals(loadBalancerPort)){
125
                                        showToast("Cannot use this Virtual IP. The same port cannot be used on multiple load balancers for a Shared Virtual IP.");
126
                                } else if(!vips[index].getLoadBalancer().getRegion().equals(loadBalancerRegion)){
127
                                        showToast("Cannot use this Virtual IP. The Shared Virtual IP must come the same region as the new load balancer.");
128
                                } else {
129
                                        Log.d("info", "the selected vip is " + vips[index].getAddress());
130
                                        selectedVip = vips[index];
131
                                }
132
                        }
133
                });
134

    
135
        }
136

    
137
        private void setLoadBalancersList(ArrayList<VirtualIp> vips) {
138
                if (vips == null) {
139
                        vips = new ArrayList<VirtualIp>();
140
                }
141

    
142
                this.vips = new VirtualIp[vips.size()];
143

    
144
                if (vips != null) {
145
                        for (int i = 0; i < vips.size(); i++) {
146
                                VirtualIp virtualIp = vips.get(i);
147
                                this.vips[i] = virtualIp;
148
                        }
149
                }
150

    
151
                if (this.vips.length == 0) {
152
                        displayNoVipsCell();
153
                } else {
154
                        displayRadioButtons();
155
                }
156
        }
157

    
158
        private void loadVirtualIps() {
159
                new LoadVirtualIpsTask().execute((Void[]) null);
160
        }
161

    
162
        private class LoadVirtualIpsTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> {
163
                private LoadBalancersException exception;
164

    
165
                @Override
166
                protected void onPreExecute(){
167
                        showDialog();
168
                }
169

    
170
                @Override
171
                protected ArrayList<LoadBalancer> doInBackground(Void... arg0) {
172
                        ArrayList<LoadBalancer> loadBalancers = null;
173
                        try {
174
                                loadBalancers = (new LoadBalancerManager(getContext())).createList();
175
                        } catch (LoadBalancersException e) {
176
                                exception = e;
177
                        }
178
                        return loadBalancers;
179
                }
180

    
181
                @Override
182
                protected void onPostExecute(ArrayList<LoadBalancer> result) {
183
                        hideDialog();
184
                        if (exception != null) {
185
                                showAlert("Error", exception.getMessage());
186
                        }
187
                        ArrayList<VirtualIp> vipList = getVipList(result);
188
                        setLoadBalancersList(vipList);
189
                }
190
        }
191

    
192
        private ArrayList<VirtualIp> getVipList(ArrayList<LoadBalancer> result){
193
                ArrayList<VirtualIp> vips = new ArrayList<VirtualIp>();
194
                for(LoadBalancer lb : result){
195
                        for(VirtualIp ip : lb.getVirtualIps()){
196
                                ip.setLoadBalancer(lb);
197
                                vips.add(ip);
198
                        }
199
                }
200
                return vips;
201
        }
202

    
203
}