Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ListContainerActivity.java @ 006434d8

History | View | Annotate | Download (8.3 kB)

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