Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ListContainerActivity.java @ 038ac9a4

History | View | Annotate | Download (8.7 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.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.files.api.client.Container;
23
import com.rackspace.cloud.files.api.client.ContainerManager;
24
import com.rackspace.cloud.servers.api.client.CloudServersException;
25

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

    
33
        protected static final int DELETE_ID = 0;
34

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

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

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

    
61
        private void restoreState(Bundle state) {
62
                if (state != null && state.containsKey("loading")
63
                                && state.getBoolean("loading")) {
64
                        loadContainers();
65
                        registerForContextMenu(getListView());
66
                } else if (state != null && state.containsKey("container")
67
                                && state.getSerializable("container") != null) {
68
                        containers = (Container[]) state.getSerializable("container");
69
                        if (containers.length == 0) {
70
                                displayNoServersCell();
71
                        } else {
72
                                getListView().setDividerHeight(1); // restore divider lines
73
                                setListAdapter(new FileAdapter());
74
                        }
75
                } else {
76
                        loadContainers();
77
                        registerForContextMenu(getListView());
78
                }
79
        }
80

    
81
        protected void onListItemClick(ListView l, View v, int position, long id) {
82
                if (containers != null && containers.length > 0) {
83
                        Intent viewIntent = new Intent(this, ContainerObjectsActivity.class);
84
                        viewIntent.putExtra("container", containers[position]);
85
                        startActivityForResult(viewIntent, 55);
86
                }
87
        }
88

    
89
        private void loadContainers() {
90
                displayLoadingCell();
91
                new LoadContainersTask().execute((Void[]) null);
92
        }
93

    
94
        private void setContainerList() {
95
                if (containerNames.length == 0) {
96
                        displayNoServersCell();
97
                } else {
98
                        getListView().setDividerHeight(1); // restore divider lines
99
                        setListAdapter(new FileAdapter());
100
                }
101
        }
102

    
103
        private void displayLoadingCell() {
104
                String a[] = new String[1];
105
                a[0] = "Loading...";
106
                setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell,
107
                                R.id.loading_label, a));
108
                getListView().setTextFilterEnabled(true);
109
                getListView().setDividerHeight(0); // hide the dividers so it won't look
110
                                                                                        // like a list row
111
                getListView().setItemsCanFocus(false);
112
        }
113

    
114
        private void displayNoServersCell() {
115
                String a[] = new String[1];
116
                a[0] = "No Files";
117
                setListAdapter(new ArrayAdapter<String>(this,
118
                                R.layout.nocontainerscell, R.id.no_containers_label, a));
119
                getListView().setTextFilterEnabled(true);
120
                getListView().setDividerHeight(0); // hide the dividers so it won't look
121
                                                                                        // like a list row
122
                getListView().setItemsCanFocus(false);
123
        }
124

    
125
        private void showAlert(String title, String message) {
126
                // Can't create handler inside thread that has not called
127
                // Looper.prepare()
128
                // Looper.prepare();
129
                try {
130
                        AlertDialog alert = new AlertDialog.Builder(this).create();
131
                        alert.setTitle(title);
132
                        alert.setMessage(message);
133
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
134
                                public void onClick(DialogInterface dialog, int which) {
135
                                        return;
136
                                }
137
                        });
138
                        alert.show();
139
                } catch (Exception e) {
140
                        e.printStackTrace();
141
                }
142
        }
143

    
144
        private class LoadContainersTask extends
145
                        AsyncTask<Void, Void, ArrayList<Container>> {
146

    
147
                private CloudServersException exception;
148

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

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

    
158
                        try {
159
                                containers = (new ContainerManager(context)).createList(true);
160
                        } catch (CloudServersException e) {
161
                                exception = e;
162
                        }
163
                        return containers;
164
                }
165

    
166
                @Override
167
                protected void onPostExecute(ArrayList<Container> result) {
168
                        if (exception != null) {
169
                                Log.e("ListContainerActivity", "", exception);
170
                                showAlert("Error In " + ListContainerActivity.class.getName(),
171
                                                exception.getMessage());
172
                        }
173
                        ArrayList<Container> containerList = result;
174

    
175
                        if (containerList != null) {
176
                                containerNames = new String[containerList.size()];
177
                                containers = new Container[containerList.size()];
178
                                for (int i = 0; i < containerList.size(); i++) {
179
                                        Container container = containerList.get(i);
180
                                        containers[i] = container;
181
                                        containerNames[i] = container.getName();
182
                                }
183
                        } else {
184
                                containerNames = new String[0];
185
                                containers = new Container[0];
186
                        }
187
                        loading = false;
188
                        new LoadCDNContainersTask().execute((Void[]) null);
189
                }
190
        }
191

    
192
        private class LoadCDNContainersTask extends
193
                        AsyncTask<Void, Void, ArrayList<Container>> {
194

    
195
                private CloudServersException exception;
196

    
197
                @Override
198
                protected void onPreExecute() {
199
                        loading = true;
200
                }
201

    
202
                @Override
203
                protected ArrayList<Container> doInBackground(Void... arg0) {
204
                        ArrayList<Container> cdnContainers = null;
205

    
206
                        try {
207
                                cdnContainers = (new ContainerManager(context))
208
                                                .createCDNList(true);
209
                        } catch (CloudServersException e) {
210
                                exception = e;
211
                        }
212
                        return cdnContainers;
213
                }
214

    
215
                @Override
216
                protected void onPostExecute(ArrayList<Container> result) {
217
                        if (exception != null) {
218
                                showAlert("Error", exception.getMessage());
219
                        }
220

    
221
                        ArrayList<Container> cdnContainers = result;
222
                        Log.i("papala", cdnContainers + " ");
223
                        for (int i = 0; i < containers.length; i++) {
224
                                Container container = containers[i];
225
                                for (int t = 0; t < cdnContainers.size(); t++) {
226
                                        Container cdnContainer = cdnContainers.get(t);
227
                                        if (container.getName().equals(cdnContainer.getName())) {
228
                                                container.setCdnEnabled(cdnContainer.isCdnEnabled());
229
                                                container.setCdnUrl(cdnContainer.getCdnUrl());
230
                                                container.setTtl(cdnContainer.getTtl());
231
                                        }
232
                                }
233
                        }
234
                        setContainerList();
235
                        loading = false;
236
                }
237
        }
238

    
239
        @Override
240
        public boolean onCreateOptionsMenu(Menu menu) {
241
                super.onCreateOptionsMenu(menu);
242
                MenuInflater inflater = getMenuInflater();
243
                inflater.inflate(R.menu.container_list_menu, menu);
244
                return true;
245
        }
246

    
247
        @Override
248
        public boolean onOptionsItemSelected(MenuItem item) {
249
                switch (item.getItemId()) {
250
                case R.id.add_container:
251
                        startActivityForResult(
252
                                        new Intent(this, AddContainerActivity.class), 56); // arbitrary
253
                                                                                                                                                // number
254
                                                                                                                                                // never
255
                                                                                                                                                // used
256
                                                                                                                                                // again
257
                        return true;
258
                case R.id.refresh:
259
                        containers = null;
260
                        loadContainers();
261
                        return true;
262
                }
263
                return false;
264
        }
265

    
266
        class FileAdapter extends ArrayAdapter<Container> {
267
                FileAdapter() {
268
                        super(ListContainerActivity.this, R.layout.listcontainerscell,
269
                                        containers);
270
                }
271

    
272
                public View getView(int position, View convertView, ViewGroup parent) {
273

    
274
                        Container container = containers[position];
275

    
276
                        LayoutInflater inflater = getLayoutInflater();
277
                        View row = inflater.inflate(R.layout.listcontainerscell, parent,
278
                                        false);
279

    
280
                        TextView label = (TextView) row.findViewById(R.id.label);
281
                        label.setText(container.getName());
282

    
283
                        if (container.getBytes() >= bConver) {
284
                                megaBytes = Math.abs(container.getBytes() / bConver + 0.2);
285
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
286
                                sublabel.setText(container.getCount() + " Objects " + megaBytes
287
                                                + " MB");
288
                        } else if (container.getBytes() >= kbConver) {
289
                                kiloBytes = Math.abs(container.getBytes() / kbConver + 0.2);
290
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
291
                                sublabel.setText(container.getCount() + " Objects " + kiloBytes
292
                                                + " KB");
293
                        } else {
294
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
295
                                sublabel.setText(container.getCount() + " Objects "
296
                                                + container.getBytes() + " B");
297
                        }
298

    
299
                        return (row);
300
                }
301
        }
302

    
303
        @Override
304
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
305
                super.onActivityResult(requestCode, resultCode, data);
306

    
307
                if (resultCode == RESULT_OK) {
308
                        // a sub-activity kicked back, so we want to refresh the server list
309
                        loadContainers();
310
                }
311
        }
312

    
313
}