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