root / src / com / rackspace / cloud / android / ListLoadBalancersActivity.java @ 7dbfc514
History | View | Annotate | Download (7.1 kB)
1 |
package com.rackspace.cloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import android.app.AlertDialog; |
6 |
import android.content.Context; |
7 |
import android.content.DialogInterface; |
8 |
import android.content.Intent; |
9 |
import android.os.AsyncTask; |
10 |
import android.os.Bundle; |
11 |
import android.util.Log; |
12 |
import android.view.LayoutInflater; |
13 |
import android.view.Menu; |
14 |
import android.view.MenuInflater; |
15 |
import android.view.MenuItem; |
16 |
import android.view.View; |
17 |
import android.view.ViewGroup; |
18 |
import android.widget.ArrayAdapter; |
19 |
import android.widget.ImageView; |
20 |
import android.widget.ListView; |
21 |
import android.widget.TextView; |
22 |
|
23 |
import com.rackspace.cloud.android.R; |
24 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
25 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
26 |
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException; |
27 |
|
28 |
public class ListLoadBalancersActivity extends GaListActivity { |
29 |
|
30 |
private final int ADD_LOAD_BALANCER_CODE = 22; |
31 |
private LoadBalancer[] loadBalancers; |
32 |
private Context context; |
33 |
|
34 |
@Override
|
35 |
public void onCreate(Bundle savedInstanceState) { |
36 |
super.onCreate(savedInstanceState);
|
37 |
trackPageView(GoogleAnalytics.PAGE_LOADBALANCERS); |
38 |
context = getApplicationContext(); |
39 |
restoreState(savedInstanceState); |
40 |
} |
41 |
|
42 |
@Override
|
43 |
protected void onSaveInstanceState(Bundle outState) { |
44 |
super.onSaveInstanceState(outState);
|
45 |
outState.putSerializable("loadBalancers", loadBalancers);
|
46 |
} |
47 |
|
48 |
private void restoreState(Bundle state) { |
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 LoadBalancerAdapter());
|
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 |
|
72 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
73 |
if (loadBalancers != null && loadBalancers.length > 0) { |
74 |
Intent viewIntent = new Intent(this, ViewLoadBalancerActivity.class); |
75 |
viewIntent.putExtra("loadBalancer", loadBalancers[position]);
|
76 |
startActivityForResult(viewIntent, 55); // arbitrary number; never |
77 |
// used again
|
78 |
} |
79 |
} |
80 |
|
81 |
private void loadLoadBalancers() { |
82 |
displayLoadingCell(); |
83 |
new LoadLoadBalancersTask().execute((Void[]) null); |
84 |
} |
85 |
|
86 |
private void setLoadBalancersList(ArrayList<LoadBalancer> loadBalancers) { |
87 |
if (loadBalancers == null) { |
88 |
loadBalancers = new ArrayList<LoadBalancer>(); |
89 |
} |
90 |
String[] loadBalancerNames = new String[loadBalancers.size()]; |
91 |
this.loadBalancers = new LoadBalancer[loadBalancers.size()]; |
92 |
|
93 |
if (loadBalancers != null) { |
94 |
for (int i = 0; i < loadBalancers.size(); i++) { |
95 |
LoadBalancer loadBalancer = loadBalancers.get(i); |
96 |
this.loadBalancers[i] = loadBalancer;
|
97 |
loadBalancerNames[i] = loadBalancer.getName(); |
98 |
} |
99 |
} |
100 |
|
101 |
if (loadBalancerNames.length == 0) { |
102 |
displayNoLoadBalancerCell(); |
103 |
} else {
|
104 |
getListView().setDividerHeight(1); // restore divider lines |
105 |
setListAdapter(new LoadBalancerAdapter());
|
106 |
} |
107 |
} |
108 |
|
109 |
@Override
|
110 |
public boolean onCreateOptionsMenu(Menu menu) { |
111 |
super.onCreateOptionsMenu(menu);
|
112 |
MenuInflater inflater = getMenuInflater(); |
113 |
inflater.inflate(R.menu.loadbalancers_list_menu, menu); |
114 |
return true; |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public boolean onOptionsItemSelected(MenuItem item) { |
119 |
switch (item.getItemId()) {
|
120 |
case R.id.add_loadbalancer:
|
121 |
startActivityForResult(new Intent(this, AddLoadBalancerActivity.class), ADD_LOAD_BALANCER_CODE); |
122 |
return true; |
123 |
case R.id.refresh:
|
124 |
loadBalancers = null;
|
125 |
loadLoadBalancers(); |
126 |
return true; |
127 |
} |
128 |
return false; |
129 |
} |
130 |
|
131 |
// * Adapter/
|
132 |
class LoadBalancerAdapter extends ArrayAdapter<LoadBalancer> { |
133 |
|
134 |
LoadBalancerAdapter() { |
135 |
super(ListLoadBalancersActivity.this,
|
136 |
R.layout.list_loadbalancer_item, loadBalancers); |
137 |
} |
138 |
|
139 |
public View getView(int position, View convertView, ViewGroup parent) { |
140 |
LoadBalancer loadBalancer = loadBalancers[position]; |
141 |
LayoutInflater inflater = getLayoutInflater(); |
142 |
View row = inflater.inflate(R.layout.list_loadbalancer_item,
|
143 |
parent, false);
|
144 |
|
145 |
Log.d("info", "name: " + loadBalancer.getName() + " status: " + loadBalancer.getStatus()); |
146 |
|
147 |
ImageView status = (ImageView) row.findViewById(R.id.load_balancer_status); |
148 |
if(loadBalancer.getStatus().equals("DELETED") || loadBalancer.getStatus().equals("PENDING_DELETE")){ |
149 |
status.setImageResource(R.drawable.deny_rule); |
150 |
} else if(loadBalancer.getStatus().equals("ERROR")){ |
151 |
status.setImageResource(R.drawable.error_icon); |
152 |
} else {
|
153 |
status.setImageResource(R.drawable.allow_rule); |
154 |
} |
155 |
|
156 |
TextView label = (TextView) row.findViewById(R.id.label); |
157 |
label.setText(loadBalancer.getName()); |
158 |
|
159 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
160 |
sublabel.setText("ID: " + loadBalancer.getId());
|
161 |
|
162 |
return (row);
|
163 |
} |
164 |
} |
165 |
|
166 |
private void displayLoadingCell() { |
167 |
String a[] = new String[1]; |
168 |
a[0] = "Loading..."; |
169 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell, R.id.loading_label, a)); |
170 |
getListView().setTextFilterEnabled(true);
|
171 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
172 |
getListView().setItemsCanFocus(false);
|
173 |
} |
174 |
|
175 |
private void showAlert(String title, String message) { |
176 |
// Can't create handler inside thread that has not called
|
177 |
// Looper.prepare()
|
178 |
// Looper.prepare();
|
179 |
try {
|
180 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
181 |
alert.setTitle(title); |
182 |
alert.setMessage(message); |
183 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
184 |
public void onClick(DialogInterface dialog, int which) { |
185 |
return;
|
186 |
} |
187 |
}); |
188 |
alert.show(); |
189 |
} catch (Exception e) { |
190 |
e.printStackTrace(); |
191 |
} |
192 |
} |
193 |
|
194 |
private class LoadLoadBalancersTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> { |
195 |
private LoadBalancersException exception;
|
196 |
|
197 |
@Override
|
198 |
protected ArrayList<LoadBalancer> doInBackground(Void... arg0) { |
199 |
ArrayList<LoadBalancer> loadBalancers = null; |
200 |
try {
|
201 |
loadBalancers = (new LoadBalancerManager(context)).createList();
|
202 |
} catch (LoadBalancersException e) {
|
203 |
exception = e; |
204 |
} |
205 |
return loadBalancers;
|
206 |
} |
207 |
|
208 |
@Override
|
209 |
protected void onPostExecute(ArrayList<LoadBalancer> result) { |
210 |
if (exception != null) { |
211 |
showAlert("Error", exception.getMessage());
|
212 |
} |
213 |
setLoadBalancersList(result); |
214 |
} |
215 |
} |
216 |
|
217 |
@Override
|
218 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
219 |
super.onActivityResult(requestCode, resultCode, data);
|
220 |
|
221 |
if (resultCode == RESULT_OK) {
|
222 |
// a sub-activity kicked back, so we want to refresh the server list
|
223 |
loadLoadBalancers(); |
224 |
} |
225 |
} |
226 |
} |