Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9.1 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.app.ProgressDialog;
8
import android.content.Context;
9
import android.content.DialogInterface;
10
import android.content.Intent;
11
import android.os.AsyncTask;
12
import android.os.Bundle;
13
import android.util.Log;
14
import android.view.LayoutInflater;
15
import android.view.Menu;
16
import android.view.MenuInflater;
17
import android.view.MenuItem;
18
import android.view.View;
19
import android.view.ViewGroup;
20
import android.view.WindowManager;
21
import android.view.ViewGroup.LayoutParams;
22
import android.widget.ArrayAdapter;
23
import android.widget.ListView;
24
import android.widget.ProgressBar;
25
import android.widget.TextView;
26

    
27
import com.rackspace.cloud.files.api.client.Container;
28
import com.rackspace.cloud.files.api.client.ContainerManager;
29
import com.rackspace.cloud.servers.api.client.CloudServersException;
30

    
31
public class ListContainerActivity extends ListActivity {
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
        ProgressDialog pDialog;
46
        
47
        @Override
48
        public void onCreate(Bundle savedInstanceState) {
49
                super.onCreate(savedInstanceState);
50
                context = getApplicationContext();
51
                setContentView(R.layout.list_containers);
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("container") && state.getSerializable("container") != null) {
64
                        containers = (Container[]) state.getSerializable("container");
65
                        if (containers.length == 0) {
66
                                displayNoContainersCell();
67
                        } else {
68
                                getListView().setDividerHeight(1); // restore divider lines
69
                                setListAdapter(new FileAdapter());
70
                        }
71
                } else {
72
                        loadContainers();
73
                        registerForContextMenu(getListView());
74
                }
75
        }
76

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

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

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

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

    
110
        private void displayNoContainersCell() {
111
                String a[] = new String[1];
112
                a[0] = "No Files";
113
                setListAdapter(new ArrayAdapter<String>(this,
114
                                R.layout.nocontainerscell, R.id.no_containers_label, a));
115
                getListView().setTextFilterEnabled(true);
116
                getListView().setDividerHeight(0); // hide the dividers so it won't look
117
                                                                                        // like a list row
118
                getListView().setItemsCanFocus(false);
119
        }
120
        
121
        protected void showProgressDialog() {
122
                pDialog = new ProgressDialog(this, R.style.NewDialog);
123
                // // Set blur to background
124
                WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes();
125
                lp.dimAmount = 0.0f;
126
                pDialog.getWindow().setAttributes(lp);
127
                pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
128
                pDialog.show();
129
                pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
130
        }
131

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

    
151
        private class LoadContainersTask extends
152
                        AsyncTask<Void, Void, ArrayList<Container>> {
153

    
154
                private CloudServersException exception;
155

    
156
                @Override
157
                protected void onPreExecute(){
158
                        showProgressDialog();
159
                        loading = true;
160
                }
161
                        
162
                @Override
163
                protected ArrayList<Container> doInBackground(Void... arg0) {
164
                        ArrayList<Container> containers = null;
165
                        try {
166
                                containers = (new ContainerManager(context)).createList(true);
167
                        } catch (CloudServersException e) {
168
                                exception = e;
169
                        }
170
                        pDialog.dismiss();   
171
                        return containers;
172
                }
173

    
174
                @Override
175
                protected void onPostExecute(ArrayList<Container> result) {
176
                        if (exception != null) {
177
                                showAlert("Error", exception.getMessage());
178
                        }
179
                        ArrayList<Container> containerList = result;
180
                        containerNames = new String[containerList.size()];
181
                        containers = new Container[containerList.size()];
182
                        if (containerList != null) {
183
                                for (int i = 0; i < containerList.size(); i++) {
184
                                        Container container = containerList.get(i);
185
                                        containers[i] = container;
186
                                        containerNames[i] = container.getName();
187
                                }
188
                        }
189
                        pDialog.dismiss();
190
                        loading = false;
191
                        new LoadCDNContainersTask().execute((Void[]) null);
192
                }
193
        }
194

    
195
        private class LoadCDNContainersTask extends
196
                        AsyncTask<Void, Void, ArrayList<Container>> {
197

    
198
                private CloudServersException exception;
199

    
200
                @Override
201
                protected void onPreExecute(){
202
                        showProgressDialog();
203
                        loading = true;
204
                }
205
                
206
                @Override
207
                protected ArrayList<Container> doInBackground(Void... arg0) {
208
                        ArrayList<Container> cdnContainers = null;
209
                        try {
210
                                cdnContainers = (new ContainerManager(context)).createCDNList(true);
211
                        } catch (CloudServersException e) {
212
                                exception = e;
213
                        }
214
                        pDialog.dismiss();
215
                        return cdnContainers;
216
                }
217

    
218
                @Override
219
                protected void onPostExecute(ArrayList<Container> result) {
220
                        Log.v("listcontainerActivity", "onPostExecute loadCDNcontainerTask");
221
                        if (exception != null) {
222
                                showAlert("Error", exception.getMessage());
223
                        }
224

    
225
                        ArrayList<Container> cdnContainers = result;
226

    
227
                        for (int i = 0; i < containers.length; i++) {
228
                                Container container = containers[i];
229
                                for (int t = 0; t < cdnContainers.size(); t++) {
230
                                        Container cdnContainer = cdnContainers.get(t);
231
                                        if (container.getName().equals(cdnContainer.getName())) {
232
                                                container.setCdnEnabled(cdnContainer.isCdnEnabled());
233
                                                container.setCdnUrl(cdnContainer.getCdnUrl());
234
                                                container.setTtl(cdnContainer.getTtl());
235
                                        }
236
                                }
237
                        }
238
                        pDialog.dismiss();
239
                        setContainerList();
240
                        loading = false;
241
                }
242
        }
243

    
244
        @Override
245
        public boolean onCreateOptionsMenu(Menu menu) {
246
                super.onCreateOptionsMenu(menu);
247
                MenuInflater inflater = getMenuInflater();
248
                inflater.inflate(R.menu.container_list_menu, menu);
249
                return true;
250
        }
251

    
252
        @Override
253
        public boolean onOptionsItemSelected(MenuItem item) {
254
                switch (item.getItemId()) {
255
                case R.id.add_container:
256
                        startActivityForResult(
257
                                        new Intent(this, AddContainerActivity.class), 56); // arbitrary number never used 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
}