root / src / com / rackspacecloud / android / ListLoadBalancersActivity.java @ 4f9d1a69
History | View | Annotate | Download (6.7 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.app.ProgressDialog; |
8 |
import android.content.Context; |
9 |
import android.content.DialogInterface; |
10 |
import android.content.Intent; |
11 |
import android.os.AsyncTask; |
12 |
import android.os.Bundle; |
13 |
import android.util.Log; |
14 |
import android.view.LayoutInflater; |
15 |
import android.view.Menu; |
16 |
import android.view.MenuInflater; |
17 |
import android.view.MenuItem; |
18 |
import android.view.View; |
19 |
import android.view.ViewGroup; |
20 |
import android.view.WindowManager; |
21 |
import android.view.ViewGroup.LayoutParams; |
22 |
import android.widget.ArrayAdapter; |
23 |
import android.widget.ListView; |
24 |
import android.widget.ProgressBar; |
25 |
import android.widget.TextView; |
26 |
|
27 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
28 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
29 |
import com.rackspace.cloud.loadbalancers.api.client.http.LoadBalancersException; |
30 |
|
31 |
public class ListLoadBalancersActivity extends ListActivity { |
32 |
|
33 |
private final int ADD_LOAD_BALANCER_CODE = 22; |
34 |
private LoadBalancer[] loadBalancers; |
35 |
private Context context; |
36 |
ProgressDialog pDialog; |
37 |
|
38 |
@Override
|
39 |
public void onCreate(Bundle savedInstanceState) { |
40 |
super.onCreate(savedInstanceState);
|
41 |
setContentView(R.layout.list_loadbalancers); |
42 |
restoreState(savedInstanceState); |
43 |
} |
44 |
|
45 |
@Override
|
46 |
protected void onSaveInstanceState(Bundle outState) { |
47 |
super.onSaveInstanceState(outState);
|
48 |
outState.putSerializable("loadBalancers", loadBalancers);
|
49 |
} |
50 |
|
51 |
private void restoreState(Bundle state) { |
52 |
context = getApplicationContext(); |
53 |
if (state != null && state.containsKey("loadBalancers")) { |
54 |
loadBalancers = (LoadBalancer[]) state
|
55 |
.getSerializable("loadBalancers");
|
56 |
if (loadBalancers.length == 0) { |
57 |
// displayNoServersCell();
|
58 |
} else {
|
59 |
getListView().setDividerHeight(1); // restore divider lines |
60 |
setListAdapter(new LoadBalancerAdapter());
|
61 |
} |
62 |
} else {
|
63 |
loadLoadBalancers(); |
64 |
} |
65 |
} |
66 |
|
67 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
68 |
if (loadBalancers != null && loadBalancers.length > 0) { |
69 |
Intent viewIntent = new Intent(this, ViewLoadBalancerActivity.class); |
70 |
viewIntent.putExtra("loadBalancer", loadBalancers[position]);
|
71 |
Log.i("VIEWLOADBALANCERS: ", loadBalancers[position].getAlgorithm()
|
72 |
+ "," + loadBalancers[position].getProtocol() + "," |
73 |
+ loadBalancers[position].getStatus()); |
74 |
startActivityForResult(viewIntent, 55); // arbitrary number; never |
75 |
// used again
|
76 |
} |
77 |
} |
78 |
|
79 |
private void loadLoadBalancers() { |
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 |
// displayNoServersCell();
|
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( |
119 |
new Intent(this, AddLoadBalancerActivity.class), ADD_LOAD_BALANCER_CODE); // arbitrary number never used again |
120 |
return true; |
121 |
case R.id.refresh:
|
122 |
loadBalancers = null;
|
123 |
loadLoadBalancers(); |
124 |
return true; |
125 |
} |
126 |
return false; |
127 |
} |
128 |
|
129 |
protected void showDialog() { |
130 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
131 |
// // Set blur to background
|
132 |
WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes(); |
133 |
lp.dimAmount = 0.0f;
|
134 |
pDialog.getWindow().setAttributes(lp); |
135 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
136 |
pDialog.show(); |
137 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
138 |
} |
139 |
|
140 |
private void showAlert(String title, String message) { |
141 |
// Can't create handler inside thread that has not called
|
142 |
// Looper.prepare()
|
143 |
// Looper.prepare();
|
144 |
try {
|
145 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
146 |
alert.setTitle(title); |
147 |
alert.setMessage(message); |
148 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
149 |
public void onClick(DialogInterface dialog, int which) { |
150 |
return;
|
151 |
} |
152 |
}); |
153 |
alert.show(); |
154 |
} catch (Exception e) { |
155 |
e.printStackTrace(); |
156 |
} |
157 |
} |
158 |
|
159 |
private class LoadLoadBalancersTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> { |
160 |
private LoadBalancersException exception;
|
161 |
|
162 |
protected void onPreExecute() { |
163 |
showDialog(); |
164 |
} |
165 |
|
166 |
@Override
|
167 |
protected ArrayList<LoadBalancer> doInBackground(Void... arg0) { |
168 |
ArrayList<LoadBalancer> loadBalancers = null; |
169 |
try {
|
170 |
loadBalancers = (new LoadBalancerManager(context)).createList();
|
171 |
} catch (LoadBalancersException e) {
|
172 |
exception = e; |
173 |
} |
174 |
pDialog.dismiss(); |
175 |
return loadBalancers;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
protected void onPostExecute(ArrayList<LoadBalancer> result) { |
180 |
if (exception != null) { |
181 |
pDialog.dismiss(); |
182 |
showAlert("Error", exception.getMessage());
|
183 |
} |
184 |
pDialog.dismiss(); |
185 |
setLoadBalancersList(result); |
186 |
} |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
191 |
super.onActivityResult(requestCode, resultCode, data);
|
192 |
|
193 |
if (resultCode == RESULT_OK) {
|
194 |
// a sub-activity kicked back, so we want to refresh the server list
|
195 |
loadLoadBalancers(); |
196 |
} |
197 |
} |
198 |
|
199 |
// * Adapter/
|
200 |
class LoadBalancerAdapter extends ArrayAdapter<LoadBalancer> { |
201 |
|
202 |
LoadBalancerAdapter() { |
203 |
super(ListLoadBalancersActivity.this,
|
204 |
R.layout.list_loadbalancer_item, loadBalancers); |
205 |
} |
206 |
|
207 |
public View getView(int position, View convertView, ViewGroup parent) { |
208 |
LoadBalancer loadBalancer = loadBalancers[position]; |
209 |
LayoutInflater inflater = getLayoutInflater(); |
210 |
View row = inflater.inflate(R.layout.list_loadbalancer_item,
|
211 |
parent, false);
|
212 |
|
213 |
TextView label = (TextView) row.findViewById(R.id.label); |
214 |
label.setText(loadBalancer.getName()); |
215 |
//
|
216 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
217 |
sublabel.setText("ID: " + loadBalancer.getId());
|
218 |
//
|
219 |
// ImageView icon = (ImageView) row.findViewById(R.id.icon);
|
220 |
// icon.setImageResource(server.getImage().iconResourceId());
|
221 |
|
222 |
return (row);
|
223 |
} |
224 |
} |
225 |
} |