root / src / com / rackspacecloud / android / ListContainerActivity.java @ ea001570
History | View | Annotate | Download (9.1 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.files.api.client.Container; |
28 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
29 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
30 |
|
31 |
/**
|
32 |
*
|
33 |
* @author Phillip Toohill
|
34 |
*
|
35 |
*/
|
36 |
public class ListContainerActivity extends ListActivity { |
37 |
|
38 |
protected static final int DELETE_ID = 0; |
39 |
|
40 |
private Container[] containers; |
41 |
public Container container; |
42 |
public Container cdnContainer; |
43 |
public String[] containerNames; |
44 |
public Object megaBytes; |
45 |
public Object kiloBytes; |
46 |
public int bConver = 1048576; |
47 |
public int kbConver = 1024; |
48 |
private Context context; |
49 |
private boolean loading; |
50 |
ProgressDialog pDialog; |
51 |
|
52 |
@Override
|
53 |
public void onCreate(Bundle savedInstanceState) { |
54 |
super.onCreate(savedInstanceState);
|
55 |
context = getApplicationContext(); |
56 |
setContentView(R.layout.list_containers); |
57 |
restoreState(savedInstanceState); |
58 |
} |
59 |
|
60 |
@Override
|
61 |
protected void onSaveInstanceState(Bundle outState) { |
62 |
super.onSaveInstanceState(outState);
|
63 |
outState.putSerializable("container", containers);
|
64 |
outState.putBoolean("loading", loading);
|
65 |
} |
66 |
|
67 |
private void restoreState(Bundle state) { |
68 |
if (state != null && state.containsKey("container") && state.getSerializable("container") != null) { |
69 |
containers = (Container[]) state.getSerializable("container"); |
70 |
if (containers.length == 0) { |
71 |
displayNoContainersCell(); |
72 |
} else {
|
73 |
getListView().setDividerHeight(1); // restore divider lines |
74 |
setListAdapter(new FileAdapter());
|
75 |
} |
76 |
} else {
|
77 |
loadContainers(); |
78 |
registerForContextMenu(getListView()); |
79 |
} |
80 |
} |
81 |
|
82 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
83 |
if (containers != null && containers.length > 0) { |
84 |
Intent viewIntent = new Intent(this, ContainerObjectsActivity.class); |
85 |
viewIntent.putExtra("container", containers[position]);
|
86 |
startActivityForResult(viewIntent, 55);
|
87 |
} |
88 |
} |
89 |
|
90 |
private void loadContainers() { |
91 |
showProgressDialog(); |
92 |
new LoadContainersTask().execute((Void[]) null); |
93 |
} |
94 |
|
95 |
private void setContainerList() { |
96 |
if (containerNames.length == 0) { |
97 |
displayNoContainersCell(); |
98 |
} else {
|
99 |
getListView().setDividerHeight(1); // restore divider lines |
100 |
setListAdapter(new FileAdapter());
|
101 |
} |
102 |
} |
103 |
|
104 |
// private void displayLoadingCell() {
|
105 |
// String a[] = new String[1];
|
106 |
// a[0] = "Loading...";
|
107 |
// setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell,
|
108 |
// R.id.loading_label, a));
|
109 |
// getListView().setTextFilterEnabled(true);
|
110 |
// getListView().setDividerHeight(0); // hide the dividers so it won't look
|
111 |
// // like a list row
|
112 |
// getListView().setItemsCanFocus(false);
|
113 |
// }
|
114 |
|
115 |
private void displayNoContainersCell() { |
116 |
String a[] = new String[1]; |
117 |
a[0] = "No Files"; |
118 |
setListAdapter(new ArrayAdapter<String>(this, |
119 |
R.layout.nocontainerscell, R.id.no_containers_label, a)); |
120 |
getListView().setTextFilterEnabled(true);
|
121 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
122 |
// like a list row
|
123 |
getListView().setItemsCanFocus(false);
|
124 |
} |
125 |
|
126 |
protected void showProgressDialog() { |
127 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
128 |
// // Set blur to background
|
129 |
WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes(); |
130 |
lp.dimAmount = 0.0f;
|
131 |
pDialog.getWindow().setAttributes(lp); |
132 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
133 |
pDialog.show(); |
134 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
135 |
} |
136 |
|
137 |
private void showAlert(String title, String message) { |
138 |
// Can't create handler inside thread that has not called
|
139 |
// Looper.prepare()
|
140 |
// Looper.prepare();
|
141 |
try {
|
142 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
143 |
alert.setTitle(title); |
144 |
alert.setMessage(message); |
145 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
146 |
public void onClick(DialogInterface dialog, int which) { |
147 |
return;
|
148 |
} |
149 |
}); |
150 |
alert.show(); |
151 |
} catch (Exception e) { |
152 |
e.printStackTrace(); |
153 |
} |
154 |
} |
155 |
|
156 |
private class LoadContainersTask extends |
157 |
AsyncTask<Void, Void, ArrayList<Container>> { |
158 |
|
159 |
private CloudServersException exception;
|
160 |
|
161 |
@Override
|
162 |
protected void onPreExecute(){ |
163 |
loading = true;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
protected ArrayList<Container> doInBackground(Void... arg0) { |
168 |
ArrayList<Container> containers = null; |
169 |
|
170 |
try {
|
171 |
containers = (new ContainerManager(context)).createList(true); |
172 |
} catch (CloudServersException e) {
|
173 |
exception = e; |
174 |
} |
175 |
return containers;
|
176 |
} |
177 |
|
178 |
@Override
|
179 |
protected void onPostExecute(ArrayList<Container> result) { |
180 |
if (exception != null) { |
181 |
showAlert("Error", exception.getMessage());
|
182 |
} |
183 |
ArrayList<Container> containerList = result; |
184 |
containerNames = new String[containerList.size()]; |
185 |
containers = new Container[containerList.size()]; |
186 |
if (containerList != null) { |
187 |
for (int i = 0; i < containerList.size(); i++) { |
188 |
Container container = containerList.get(i);
|
189 |
containers[i] = container; |
190 |
containerNames[i] = container.getName(); |
191 |
} |
192 |
} |
193 |
loading = false;
|
194 |
new LoadCDNContainersTask().execute((Void[]) null); |
195 |
} |
196 |
} |
197 |
|
198 |
private class LoadCDNContainersTask extends |
199 |
AsyncTask<Void, Void, ArrayList<Container>> { |
200 |
|
201 |
private CloudServersException exception;
|
202 |
|
203 |
@Override
|
204 |
protected void onPreExecute(){ |
205 |
showProgressDialog(); |
206 |
loading = true;
|
207 |
} |
208 |
|
209 |
@Override
|
210 |
protected ArrayList<Container> doInBackground(Void... arg0) { |
211 |
ArrayList<Container> cdnContainers = null; |
212 |
|
213 |
try {
|
214 |
cdnContainers = (new ContainerManager(context)).createCDNList(true); |
215 |
} catch (CloudServersException e) {
|
216 |
exception = e; |
217 |
} |
218 |
return cdnContainers;
|
219 |
} |
220 |
|
221 |
@Override
|
222 |
protected void onPostExecute(ArrayList<Container> result) { |
223 |
Log.v("listcontainerActivity", "onPostExecute loadCDNcontainerTask"); |
224 |
pDialog.dismiss(); |
225 |
if (exception != null) { |
226 |
showAlert("Error", exception.getMessage());
|
227 |
} |
228 |
|
229 |
ArrayList<Container> cdnContainers = result; |
230 |
|
231 |
for (int i = 0; i < containers.length; i++) { |
232 |
Container container = containers[i];
|
233 |
for (int t = 0; t < cdnContainers.size(); t++) { |
234 |
Container cdnContainer = cdnContainers.get(t);
|
235 |
if (container.getName().equals(cdnContainer.getName())) {
|
236 |
container.setCdnEnabled(cdnContainer.isCdnEnabled()); |
237 |
container.setCdnUrl(cdnContainer.getCdnUrl()); |
238 |
container.setTtl(cdnContainer.getTtl()); |
239 |
} |
240 |
} |
241 |
} |
242 |
setContainerList(); |
243 |
loading = false;
|
244 |
} |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public boolean onCreateOptionsMenu(Menu menu) { |
249 |
super.onCreateOptionsMenu(menu);
|
250 |
MenuInflater inflater = getMenuInflater(); |
251 |
inflater.inflate(R.menu.container_list_menu, menu); |
252 |
return true; |
253 |
} |
254 |
|
255 |
@Override
|
256 |
public boolean onOptionsItemSelected(MenuItem item) { |
257 |
switch (item.getItemId()) {
|
258 |
case R.id.add_container:
|
259 |
startActivityForResult( |
260 |
new Intent(this, AddContainerActivity.class), 56); // arbitrary number never used again |
261 |
return true; |
262 |
case R.id.refresh:
|
263 |
containers = null;
|
264 |
loadContainers(); |
265 |
return true; |
266 |
} |
267 |
return false; |
268 |
} |
269 |
|
270 |
class FileAdapter extends ArrayAdapter<Container> { |
271 |
FileAdapter() { |
272 |
super(ListContainerActivity.this, R.layout.listcontainerscell,
|
273 |
containers); |
274 |
} |
275 |
|
276 |
public View getView(int position, View convertView, ViewGroup parent) { |
277 |
|
278 |
Container container = containers[position];
|
279 |
|
280 |
LayoutInflater inflater = getLayoutInflater(); |
281 |
View row = inflater.inflate(R.layout.listcontainerscell, parent,
|
282 |
false);
|
283 |
|
284 |
TextView label = (TextView) row.findViewById(R.id.label); |
285 |
label.setText(container.getName()); |
286 |
|
287 |
if (container.getBytes() >= bConver) {
|
288 |
megaBytes = Math.abs(container.getBytes() / bConver + 0.2); |
289 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
290 |
sublabel.setText(container.getCount() + " Objects " + megaBytes
|
291 |
+ " MB");
|
292 |
} else if (container.getBytes() >= kbConver) { |
293 |
kiloBytes = Math.abs(container.getBytes() / kbConver + 0.2); |
294 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
295 |
sublabel.setText(container.getCount() + " Objects " + kiloBytes
|
296 |
+ " KB");
|
297 |
} else {
|
298 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
299 |
sublabel.setText(container.getCount() + " Objects "
|
300 |
+ container.getBytes() + " B");
|
301 |
} |
302 |
|
303 |
return (row);
|
304 |
} |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
309 |
super.onActivityResult(requestCode, resultCode, data);
|
310 |
|
311 |
if (resultCode == RESULT_OK) {
|
312 |
// a sub-activity kicked back, so we want to refresh the server list
|
313 |
loadContainers(); |
314 |
} |
315 |
} |
316 |
|
317 |
} |