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