Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ListContainerActivity.java @ 856ad13d

History | View | Annotate | Download (9.7 kB)

1
package com.rackspace.cloud.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.util.Log;
12
import android.view.LayoutInflater;
13
import android.view.Menu;
14
import android.view.MenuInflater;
15
import android.view.MenuItem;
16
import android.view.View;
17
import android.view.ViewGroup;
18
import android.widget.ArrayAdapter;
19
import android.widget.ListView;
20
import android.widget.TextView;
21

    
22
import com.rackspace.cloud.android.R;
23
import com.rackspace.cloud.files.api.client.Container;
24
import com.rackspace.cloud.files.api.client.ContainerManager;
25
import com.rackspace.cloud.servers.api.client.CloudServersException;
26

    
27
/**
28
 * 
29
 * @author Phillip Toohill
30
 * 
31
 */
32
public class ListContainerActivity extends GaListActivity {
33

    
34
        protected static final int DELETE_ID = 0;
35

    
36
        protected Container[] containers;
37
        public Container container;
38
        public Container cdnContainer;
39
        public String[] containerNames;
40
        public Object megaBytes;
41
        public Object kiloBytes;
42
        public int bConver = 1048576;
43
        public int kbConver = 1024;
44
        protected Context context;
45
        protected boolean loading;
46

    
47
        @Override
48
        public void onCreate(Bundle savedInstanceState) {
49
                super.onCreate(savedInstanceState);
50
                trackPageView(GoogleAnalytics.PAGE_CONTAINERS);
51
                context = getApplicationContext();
52
                restoreState(savedInstanceState);
53
        }
54

    
55
        @Override
56
        protected void onSaveInstanceState(Bundle outState) {
57
                super.onSaveInstanceState(outState);
58
                outState.putSerializable("container", containers);
59
                outState.putBoolean("loading", loading);
60
        }
61

    
62
        private void restoreState(Bundle state) {
63
                if (state != null && state.containsKey("loading")
64
                                && state.getBoolean("loading")) {
65
                        loadContainers();
66
                        registerForContextMenu(getListView());
67
                } else if (state != null && state.containsKey("container")
68
                                && state.getSerializable("container") != null) {
69
                        containers = (Container[]) state.getSerializable("container");
70
                        if (containers.length == 0) {
71
                                displayNoServersCell();
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
        protected void loadContainers() {
91
                displayLoadingCell();
92
                new LoadContainersTask().execute((Void[]) null);
93
        }
94

    
95
        private void setContainerList() {
96
                if (containerNames.length == 0) {
97
                        displayNoServersCell();
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
        protected void displayNoServersCell() {
116
                String a[] = new String[1];
117
                a[0] = "No Containers";
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
        private void showAlert(String title, String message) {
127
                // Can't create handler inside thread that has not called
128
                // Looper.prepare()
129
                // Looper.prepare();
130
                try {
131
                        AlertDialog alert = new AlertDialog.Builder(this).create();
132
                        alert.setTitle(title);
133
                        alert.setMessage(message);
134
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
135
                                public void onClick(DialogInterface dialog, int which) {
136
                                        return;
137
                                }
138
                        });
139
                        alert.show();
140
                } catch (Exception e) {
141
                        e.printStackTrace();
142
                }
143
        }
144

    
145
        protected class LoadContainersTask extends
146
                        AsyncTask<Void, Void, ArrayList<Container>> {
147

    
148
                private CloudServersException exception;
149

    
150
                @Override
151
                protected void onPreExecute() {
152
                        loading = true;
153
                }
154

    
155
                @Override
156
                protected ArrayList<Container> doInBackground(Void... arg0) {
157
                        ArrayList<Container> containers = null;
158

    
159
                        try {
160
                                containers = loadContainersInner();
161
                        } catch (CloudServersException e) {
162
                                exception = e;
163
                        }
164
                        return containers;
165
                }
166

    
167
                @Override
168
                protected void onPostExecute(ArrayList<Container> result) {
169
                        if (exception != null) {
170
                                Log.e("ListContainerActivity", "", exception);
171
                                showAlert("Error In " + ListContainerActivity.class.getName(),
172
                                                exception.getMessage());
173
                        }
174
                        ArrayList<Container> containerList = result;
175
                        if(result==null)
176
                                result = new ArrayList<Container>();
177
                        /*Container containerToAdd = new Container();
178
                        containerToAdd.setCount(0);
179
                        containerToAdd.setBytes(0);
180
                        containerToAdd.setName(Container.MYSHARED);
181
                        containerToAdd.setLogRetention(false);
182
                        containerToAdd.setCdnEnabled(false);
183
                        containerToAdd.setTtl(0);
184
                        containerList.add(containerToAdd);
185
                        containerToAdd = new Container();
186
                        containerToAdd.setCount(0);
187
                        containerToAdd.setBytes(0);
188
                        containerToAdd.setName(Container.OTHERS);
189
                        containerToAdd.setLogRetention(false);
190
                        containerToAdd.setCdnEnabled(false);
191
                        containerToAdd.setTtl(0);
192
                        containerList.add(containerToAdd);*/
193
                        
194
                        if (containerList != null) {
195
                                containerNames = new String[containerList.size()];
196
                                containers = new Container[containerList.size()];
197
                                for (int i = 0; i < containerList.size(); i++) {
198
                                        Container container = containerList.get(i);
199
                                        containers[i] = container;
200
                                        containerNames[i] = container.getName();
201
                                }
202
                        } else {
203
                                containerNames = new String[0];
204
                                containers = new Container[0];
205
                        }
206
                        loading = false;
207
                        new LoadCDNContainersTask().execute((Void[]) null);
208
                }
209
        }
210
        
211
        protected ArrayList<Container>  loadContainersInner() throws CloudServersException{
212
                return (new ContainerManager(context)).createList(true);
213
        }
214
        public Context getContext() {
215
                return context;
216
        }
217
        private class LoadCDNContainersTask extends
218
                        AsyncTask<Void, Void, ArrayList<Container>> {
219

    
220
                private CloudServersException exception;
221

    
222
                @Override
223
                protected void onPreExecute() {
224
                        loading = true;
225
                }
226

    
227
                @Override
228
                protected ArrayList<Container> doInBackground(Void... arg0) {
229
                        ArrayList<Container> cdnContainers = null;
230

    
231
                        try {
232
                                cdnContainers = (new ContainerManager(context))
233
                                                .createCDNList(true);
234
                        } catch (CloudServersException e) {
235
                                exception = e;
236
                        }
237
                        return cdnContainers;
238
                }
239

    
240
                @Override
241
                protected void onPostExecute(ArrayList<Container> result) {
242
                        if (exception != null) {
243
                                showAlert("Error", exception.getMessage());
244
                        }
245

    
246
                        ArrayList<Container> cdnContainers = result;
247
                        Log.i("papala", cdnContainers + " ");
248
                        for (int i = 0; i < containers.length; i++) {
249
                                Container container = containers[i];
250
                                for (int t = 0; t < cdnContainers.size(); t++) {
251
                                        Container cdnContainer = cdnContainers.get(t);
252
                                        if (container.getName().equals(cdnContainer.getName())) {
253
                                                container.setCdnEnabled(cdnContainer.isCdnEnabled());
254
                                                container.setCdnUrl(cdnContainer.getCdnUrl());
255
                                                container.setTtl(cdnContainer.getTtl());
256
                                        }
257
                                }
258
                        }
259
                        setContainerList();
260
                        loading = false;
261
                }
262
        }
263

    
264
        @Override
265
        public boolean onCreateOptionsMenu(Menu menu) {
266
                super.onCreateOptionsMenu(menu);
267
                MenuInflater inflater = getMenuInflater();
268
                inflater.inflate(R.menu.container_list_menu, menu);
269
                return true;
270
        }
271

    
272
        @Override
273
        public boolean onOptionsItemSelected(MenuItem item) {
274
                switch (item.getItemId()) {
275
                case R.id.add_container:
276
                        startActivityForResult(
277
                                        new Intent(this, AddContainerActivity.class), 56); // arbitrary
278
                                                                                                                                                // number
279
                                                                                                                                                // never
280
                                                                                                                                                // used
281
                                                                                                                                                // again
282
                        return true;
283
                case R.id.refresh:
284
                        containers = null;
285
                        loadContainers();
286
                        return true;
287
                }
288
                return false;
289
        }
290

    
291
        class FileAdapter extends ArrayAdapter<Container> {
292
                FileAdapter() {
293
                        super(ListContainerActivity.this, R.layout.listcontainerscell,
294
                                        containers);
295
                }
296

    
297
                public View getView(int position, View convertView, ViewGroup parent) {
298

    
299
                        Container container = containers[position];
300

    
301
                        LayoutInflater inflater = getLayoutInflater();
302
                        View row = inflater.inflate(R.layout.listcontainerscell, parent,
303
                                        false);
304

    
305
                        TextView label = (TextView) row.findViewById(R.id.label);
306
                        if(container.getOtherUser()!=null)
307
                                label.setText(container.getOtherUser()+"/"+container.getName());
308
                        else
309
                                label.setText(container.getName());
310

    
311
                        if (container.getBytes() >= bConver) {
312
                                megaBytes = Math.abs(container.getBytes() / bConver + 0.2);
313
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
314
                                sublabel.setText(container.getCount() + " Objects " + megaBytes
315
                                                + " MB");
316
                        } else if (container.getBytes() >= kbConver) {
317
                                kiloBytes = Math.abs(container.getBytes() / kbConver + 0.2);
318
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
319
                                sublabel.setText(container.getCount() + " Objects " + kiloBytes
320
                                                + " KB");
321
                        } else {
322
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
323
                                sublabel.setText(container.getCount() + " Objects "
324
                                                + container.getBytes() + " B");
325
                        }
326

    
327
                        return (row);
328
                }
329
        }
330

    
331
        @Override
332
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
333
                super.onActivityResult(requestCode, resultCode, data);
334

    
335
                if (resultCode == RESULT_OK) {
336
                        // a sub-activity kicked back, so we want to refresh the server list
337
                        loadContainers();
338
                }
339
        }
340

    
341
}