Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ListContainerActivity.java @ e6c34a0f

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