Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ListContainerActivity.java @ 6c0d81f1

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

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

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

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

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

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

    
124
        private void showAlert(String title, String message) {
125
                // Can't create handler inside thread that has not called
126
                // Looper.prepare()
127
                // Looper.prepare();
128
                try {
129
                        AlertDialog alert = new AlertDialog.Builder(this).create();
130
                        alert.setTitle(title);
131
                        alert.setMessage(message);
132
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
133
                                public void onClick(DialogInterface dialog, int which) {
134
                                        return;
135
                                }
136
                        });
137
                        alert.show();
138
                } catch (Exception e) {
139
                        e.printStackTrace();
140
                }
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(), exception.getMessage());
171
                        }
172
                        ArrayList<Container> containerList = result;
173
                        
174
                        if (containerList != null) {
175
                                containerNames = new String[containerList.size()];
176
                                containers = new Container[containerList.size()];
177
                                for (int i = 0; i < containerList.size(); i++) {
178
                                        Container container = containerList.get(i);
179
                                        containers[i] = container;
180
                                        containerNames[i] = container.getName();
181
                                }
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)).createCDNList(true);
208
                        } catch (CloudServersException e) {
209
                                exception = e;
210
                        }
211
                        return cdnContainers;
212
                }
213

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

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

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

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

    
261
        class FileAdapter extends ArrayAdapter<Container> {
262
                FileAdapter() {
263
                        super(ListContainerActivity.this, R.layout.listcontainerscell,
264
                                        containers);
265
                }
266

    
267
                public View getView(int position, View convertView, ViewGroup parent) {
268

    
269
                        Container container = containers[position];
270

    
271
                        LayoutInflater inflater = getLayoutInflater();
272
                        View row = inflater.inflate(R.layout.listcontainerscell, parent,
273
                                        false);
274

    
275
                        TextView label = (TextView) row.findViewById(R.id.label);
276
                        label.setText(container.getName());
277

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

    
294
                        return (row);
295
                }
296
        }
297
        
298
        @Override
299
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
300
                super.onActivityResult(requestCode, resultCode, data);
301
                
302
                if (resultCode == RESULT_OK) {
303
                        // a sub-activity kicked back, so we want to refresh the server list
304
                        loadContainers();
305
                }
306
        }
307

    
308
}