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