Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.2 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.util.ArrayList;
4

    
5
import android.app.AlertDialog;
6
import android.app.ListActivity;
7
import android.content.Context;
8
import android.content.DialogInterface;
9
import android.content.Intent;
10
import android.os.AsyncTask;
11
import android.os.Bundle;
12
import android.util.Log;
13
import android.view.LayoutInflater;
14
import android.view.Menu;
15
import android.view.MenuInflater;
16
import android.view.MenuItem;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.widget.ArrayAdapter;
20
import android.widget.ListView;
21
import android.widget.TextView;
22

    
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 ListActivity {
33

    
34
        private Container[] containers;
35
        public Container container;
36
        public Container cdnContainer;
37
        public String[] containerNames;
38
        public Object megaBytes;
39
        public Object kiloBytes;
40
        public int bConver = 1048576;
41
        public int kbConver = 1024;
42
        protected static final int DELETE_ID = 0;
43
        private Context context;
44
        
45
        @Override
46
        public void onCreate(Bundle savedInstanceState) {
47
                super.onCreate(savedInstanceState);
48
                context = getApplicationContext();
49
                restoreState(savedInstanceState);
50
        }
51

    
52
        @Override
53
        protected void onSaveInstanceState(Bundle outState) {
54
                super.onSaveInstanceState(outState);
55
                outState.putSerializable("container", containers);
56
        }
57

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

    
73
        protected void onListItemClick(ListView l, View v, int position, long id) {
74
                if (containers != null && containers.length > 0) {
75
                        Intent viewIntent = new Intent(this, ContainerObjectsActivity.class);
76
                        viewIntent.putExtra("container", containers[position]);
77
                        startActivityForResult(viewIntent, 55);
78

    
79
                        // startActivityForResult(viewIntent, 55); // arbitrary number;
80
                        // never used again
81
                }
82
        }
83

    
84
        private void loadContainers() {
85
                displayLoadingCell();
86
                new LoadContainersTask().execute((Void[]) null);
87
        }
88

    
89
        private void setContainerList() {
90
                if (containerNames.length == 0) {
91
                        displayNoServersCell();
92
                } else {
93
                        getListView().setDividerHeight(1); // restore divider lines
94
                        setListAdapter(new FileAdapter());
95
                }
96
        }
97

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

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

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

    
139
        private class LoadContainersTask extends
140
                        AsyncTask<Void, Void, ArrayList<Container>> {
141

    
142
                private CloudServersException exception;
143

    
144
                @Override
145
                protected ArrayList<Container> doInBackground(Void... arg0) {
146
                        ArrayList<Container> containers = null;
147

    
148
                        try {
149
                                containers = (new ContainerManager(context)).createList(true);
150
                        } catch (CloudServersException e) {
151
                                exception = e;
152
                        }
153
                        return containers;
154
                }
155

    
156
                @Override
157
                protected void onPostExecute(ArrayList<Container> result) {
158
                        if (exception != null) {
159
                                showAlert("Error", exception.getMessage());
160
                        }
161
                        ArrayList<Container> containerList = result;
162
                        containerNames = new String[containerList.size()];
163
                        containers = new Container[containerList.size()];
164
                        if (containerList != null) {
165
                                for (int i = 0; i < containerList.size(); i++) {
166
                                        Container container = containerList.get(i);
167
                                        containers[i] = container;
168
                                        containerNames[i] = container.getName();
169
                                }
170
                        }
171

    
172
                        new LoadCDNContainersTask().execute((Void[]) null);
173
                }
174
        }
175

    
176
        private class LoadCDNContainersTask extends
177
                        AsyncTask<Void, Void, ArrayList<Container>> {
178

    
179
                private CloudServersException exception;
180

    
181
                @Override
182
                protected ArrayList<Container> doInBackground(Void... arg0) {
183
                        ArrayList<Container> cdnContainers = null;
184

    
185
                        try {
186
                                cdnContainers = (new ContainerManager(context)).createCDNList(true);
187
                        } catch (CloudServersException e) {
188
                                exception = e;
189
                        }
190
                        return cdnContainers;
191
                }
192

    
193
                @Override
194
                protected void onPostExecute(ArrayList<Container> result) {
195
                        Log.v("listcontainerActivity", "onPostExecute loadCDNcontainerTask");
196
                        if (exception != null) {
197
                                showAlert("Error", exception.getMessage());
198
                        }
199

    
200
                        ArrayList<Container> cdnContainers = result;
201

    
202
                        for (int i = 0; i < containers.length; i++) {
203
                                Container container = containers[i];
204
                                for (int t = 0; t < cdnContainers.size(); t++) {
205
                                        Container cdnContainer = cdnContainers.get(t);
206
                                        if (container.getName().equals(cdnContainer.getName())) {
207
                                                container.setCdnEnabled(cdnContainer.isCdnEnabled());
208
                                                container.setCdnUrl(cdnContainer.getCdnUrl());
209
                                                container.setTtl(cdnContainer.getTtl());
210
                                        }
211
                                }
212
                        }
213
                        setContainerList();
214
                }
215
        }
216

    
217
        @Override
218
        public boolean onCreateOptionsMenu(Menu menu) {
219
                super.onCreateOptionsMenu(menu);
220
                MenuInflater inflater = getMenuInflater();
221
                inflater.inflate(R.menu.container_list_menu, menu);
222
                return true;
223
        }
224

    
225
        @Override
226
        public boolean onOptionsItemSelected(MenuItem item) {
227
                switch (item.getItemId()) {
228
                case R.id.add_container:
229
                        startActivityForResult(
230
                                        new Intent(this, AddContainerActivity.class), 56); // arbitrary
231
                                                                                                                                                // number;
232
                                                                                                                                                // never
233
                                                                                                                                                // used
234
                                                                                                                                                // again
235
                        return true;
236
                case R.id.refresh:
237
                        containers = null;
238
                        loadContainers();
239
                        return true;
240
                }
241
                return false;
242
        }
243

    
244
        class FileAdapter extends ArrayAdapter<Container> {
245
                FileAdapter() {
246
                        super(ListContainerActivity.this, R.layout.listcontainerscell,
247
                                        containers);
248
                }
249

    
250
                public View getView(int position, View convertView, ViewGroup parent) {
251

    
252
                        Container container = containers[position];
253

    
254
                        LayoutInflater inflater = getLayoutInflater();
255
                        View row = inflater.inflate(R.layout.listcontainerscell, parent,
256
                                        false);
257

    
258
                        TextView label = (TextView) row.findViewById(R.id.label);
259
                        label.setText(container.getName());
260

    
261
                        if (container.getBytes() >= bConver) {
262
                                megaBytes = Math.abs(container.getBytes() / bConver + 0.2);
263
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
264
                                sublabel.setText(container.getCount() + " Objects " + megaBytes
265
                                                + " MB");
266
                        } else if (container.getBytes() >= kbConver) {
267
                                kiloBytes = Math.abs(container.getBytes() / kbConver + 0.2);
268
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
269
                                sublabel.setText(container.getCount() + " Objects " + kiloBytes
270
                                                + " KB");
271
                        } else {
272
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
273
                                sublabel.setText(container.getCount() + " Objects "
274
                                                + container.getBytes() + " B");
275
                        }
276

    
277
                        return (row);
278
                }
279
        }
280

    
281
        @Override
282
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
283
                super.onActivityResult(requestCode, resultCode, data);
284

    
285
                if (resultCode == RESULT_OK) {
286
                        // a sub-activity kicked back, so we want to refresh the server list
287
                        loadContainers();
288
                }
289
        }
290

    
291
}