Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.4 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
        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
                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("container") && state.getSerializable("container") != null) {
63
                        containers = (Container[]) state.getSerializable("container");
64
                        if (containers.length == 0) {
65
                                displayNoServersCell();
66
                        } else {
67
                                getListView().setDividerHeight(1); // restore divider lines
68
                                setListAdapter(new FileAdapter());
69
                        }
70
                } else {
71
                        loadContainers();
72
                        registerForContextMenu(getListView());
73
                }
74
        }
75

    
76
        protected void onListItemClick(ListView l, View v, int position, long id) {
77
                if (containers != null && containers.length > 0) {
78
                        Intent viewIntent = new Intent(this, ContainerObjectsActivity.class);
79
                        viewIntent.putExtra("container", containers[position]);
80
                        startActivityForResult(viewIntent, 55);
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 void onPreExecute(){
146
                        loading = true;
147
                }
148
                        
149
                @Override
150
                protected ArrayList<Container> doInBackground(Void... arg0) {
151
                        ArrayList<Container> containers = null;
152

    
153
                        try {
154
                                containers = (new ContainerManager(context)).createList(true);
155
                        } catch (CloudServersException e) {
156
                                exception = e;
157
                        }
158
                        return containers;
159
                }
160

    
161
                @Override
162
                protected void onPostExecute(ArrayList<Container> result) {
163
                        if (exception != null) {
164
                                showAlert("Error", exception.getMessage());
165
                        }
166
                        ArrayList<Container> containerList = result;
167
                        containerNames = new String[containerList.size()];
168
                        containers = new Container[containerList.size()];
169
                        if (containerList != null) {
170
                                for (int i = 0; i < containerList.size(); i++) {
171
                                        Container container = containerList.get(i);
172
                                        containers[i] = container;
173
                                        containerNames[i] = container.getName();
174
                                }
175
                        }
176
                        loading = false;
177
                        new LoadCDNContainersTask().execute((Void[]) null);
178
                }
179
        }
180

    
181
        private class LoadCDNContainersTask extends
182
                        AsyncTask<Void, Void, ArrayList<Container>> {
183

    
184
                private CloudServersException exception;
185

    
186
                @Override
187
                protected void onPreExecute(){
188
                        loading = true;
189
                }
190
                
191
                @Override
192
                protected ArrayList<Container> doInBackground(Void... arg0) {
193
                        ArrayList<Container> cdnContainers = null;
194

    
195
                        try {
196
                                cdnContainers = (new ContainerManager(context)).createCDNList(true);
197
                        } catch (CloudServersException e) {
198
                                exception = e;
199
                        }
200
                        return cdnContainers;
201
                }
202

    
203
                @Override
204
                protected void onPostExecute(ArrayList<Container> result) {
205
                        Log.v("listcontainerActivity", "onPostExecute loadCDNcontainerTask");
206
                        if (exception != null) {
207
                                showAlert("Error", exception.getMessage());
208
                        }
209

    
210
                        ArrayList<Container> cdnContainers = result;
211

    
212
                        for (int i = 0; i < containers.length; i++) {
213
                                Container container = containers[i];
214
                                for (int t = 0; t < cdnContainers.size(); t++) {
215
                                        Container cdnContainer = cdnContainers.get(t);
216
                                        if (container.getName().equals(cdnContainer.getName())) {
217
                                                container.setCdnEnabled(cdnContainer.isCdnEnabled());
218
                                                container.setCdnUrl(cdnContainer.getCdnUrl());
219
                                                container.setTtl(cdnContainer.getTtl());
220
                                        }
221
                                }
222
                        }
223
                        setContainerList();
224
                        loading = false;
225
                }
226
        }
227

    
228
        @Override
229
        public boolean onCreateOptionsMenu(Menu menu) {
230
                super.onCreateOptionsMenu(menu);
231
                MenuInflater inflater = getMenuInflater();
232
                inflater.inflate(R.menu.container_list_menu, menu);
233
                return true;
234
        }
235

    
236
        @Override
237
        public boolean onOptionsItemSelected(MenuItem item) {
238
                switch (item.getItemId()) {
239
                case R.id.add_container:
240
                        startActivityForResult(
241
                                        new Intent(this, AddContainerActivity.class), 56); // arbitrary number never used again
242
                        return true;
243
                case R.id.refresh:
244
                        containers = null;
245
                        loadContainers();
246
                        return true;
247
                }
248
                return false;
249
        }
250

    
251
        class FileAdapter extends ArrayAdapter<Container> {
252
                FileAdapter() {
253
                        super(ListContainerActivity.this, R.layout.listcontainerscell,
254
                                        containers);
255
                }
256

    
257
                public View getView(int position, View convertView, ViewGroup parent) {
258

    
259
                        Container container = containers[position];
260

    
261
                        LayoutInflater inflater = getLayoutInflater();
262
                        View row = inflater.inflate(R.layout.listcontainerscell, parent,
263
                                        false);
264

    
265
                        TextView label = (TextView) row.findViewById(R.id.label);
266
                        label.setText(container.getName());
267

    
268
                        if (container.getBytes() >= bConver) {
269
                                megaBytes = Math.abs(container.getBytes() / bConver + 0.2);
270
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
271
                                sublabel.setText(container.getCount() + " Objects " + megaBytes
272
                                                + " MB");
273
                        } else if (container.getBytes() >= kbConver) {
274
                                kiloBytes = Math.abs(container.getBytes() / kbConver + 0.2);
275
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
276
                                sublabel.setText(container.getCount() + " Objects " + kiloBytes
277
                                                + " KB");
278
                        } else {
279
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
280
                                sublabel.setText(container.getCount() + " Objects "
281
                                                + container.getBytes() + " B");
282
                        }
283

    
284
                        return (row);
285
                }
286
        }
287
        
288
        @Override
289
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
290
                super.onActivityResult(requestCode, resultCode, data);
291

    
292
                Log.d("info", "captin in the list container activity result with code " + requestCode + " " + resultCode);
293
                
294
                if (resultCode == RESULT_OK) {
295
                        // a sub-activity kicked back, so we want to refresh the server list
296
                        loadContainers();
297
                }
298
        }
299

    
300
}