Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.8 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.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
        private 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
        private Context context;
45
        private 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
        private 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
        private 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
        private 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 = (new ContainerManager(context)).createList(true);
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

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

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

    
196
                private CloudServersException exception;
197

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

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

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

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

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

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

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

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

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

    
275
                        Container container = containers[position];
276

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

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

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

    
300
                        return (row);
301
                }
302
        }
303

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

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

    
314
}