Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ListLoadBalancersActivity.java @ 71e52c4a

History | View | Annotate | Download (6.8 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.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.ImageView;
20
import android.widget.ListView;
21
import android.widget.TextView;
22

    
23
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
24
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
25
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException;
26

    
27
public class ListLoadBalancersActivity extends ListActivity {
28

    
29
        private final int ADD_LOAD_BALANCER_CODE = 22;
30
        private LoadBalancer[] loadBalancers;
31
        private Context context;
32

    
33
        @Override
34
        public void onCreate(Bundle savedInstanceState) {
35
                super.onCreate(savedInstanceState);
36
                context = getApplicationContext();
37
                restoreState(savedInstanceState);
38
        }
39

    
40
        @Override
41
        protected void onSaveInstanceState(Bundle outState) {
42
                super.onSaveInstanceState(outState);
43
                outState.putSerializable("loadBalancers", loadBalancers);
44
        }
45

    
46
        private void restoreState(Bundle state) {
47
                if (state != null && state.containsKey("loadBalancers") && state.getSerializable("loadBalancers") != null) {
48
                        loadBalancers = (LoadBalancer[]) state.getSerializable("loadBalancers");
49
                        if (loadBalancers.length == 0) {
50
                                displayNoLoadBalancerCell();
51
                        } else {
52
                                getListView().setDividerHeight(1); // restore divider lines
53
                                setListAdapter(new LoadBalancerAdapter());
54
                        }
55
                } else {
56
                        loadLoadBalancers();
57
                }
58
        }
59
        
60
          private void displayNoLoadBalancerCell() {
61
                    String a[] = new String[1];
62
                    a[0] = "No Load Balancers";
63
                setListAdapter(new ArrayAdapter<String>(this, R.layout.noloadbalancerscell, R.id.no_loadbalancers_label, a));
64
                getListView().setTextFilterEnabled(true);
65
                getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
66
                getListView().setItemsCanFocus(false);
67
            }
68
            
69

    
70
        protected void onListItemClick(ListView l, View v, int position, long id) {
71
                if (loadBalancers != null && loadBalancers.length > 0) {
72
                        Intent viewIntent = new Intent(this, ViewLoadBalancerActivity.class);
73
                        viewIntent.putExtra("loadBalancer", loadBalancers[position]);
74
                        startActivityForResult(viewIntent, 55); // arbitrary number; never
75
                        // used again
76
                }
77
        }
78

    
79
        private void loadLoadBalancers() {
80
                displayLoadingCell();
81
                new LoadLoadBalancersTask().execute((Void[]) null);
82
        }
83

    
84
        private void setLoadBalancersList(ArrayList<LoadBalancer> loadBalancers) {
85
                if (loadBalancers == null) {
86
                        loadBalancers = new ArrayList<LoadBalancer>();
87
                }
88
                String[] loadBalancerNames = new String[loadBalancers.size()];
89
                this.loadBalancers = new LoadBalancer[loadBalancers.size()];
90

    
91
                if (loadBalancers != null) {
92
                        for (int i = 0; i < loadBalancers.size(); i++) {
93
                                LoadBalancer loadBalancer = loadBalancers.get(i);
94
                                this.loadBalancers[i] = loadBalancer;
95
                                loadBalancerNames[i] = loadBalancer.getName();
96
                        }
97
                }
98

    
99
                if (loadBalancerNames.length == 0) {
100
                        displayNoLoadBalancerCell();
101
                } else {
102
                        getListView().setDividerHeight(1); // restore divider lines
103
                        setListAdapter(new LoadBalancerAdapter());
104
                }
105
        }
106

    
107
        @Override
108
        public boolean onCreateOptionsMenu(Menu menu) {
109
                super.onCreateOptionsMenu(menu);
110
                MenuInflater inflater = getMenuInflater();
111
                inflater.inflate(R.menu.loadbalancers_list_menu, menu);
112
                return true;
113
        }
114

    
115
        @Override
116
        public boolean onOptionsItemSelected(MenuItem item) {
117
                switch (item.getItemId()) {
118
                case R.id.add_loadbalancer:
119
                        startActivityForResult(new Intent(this, AddLoadBalancerActivity.class), ADD_LOAD_BALANCER_CODE); 
120
                        return true;
121
                case R.id.refresh:
122
                        loadBalancers = null;
123
                        loadLoadBalancers();
124
                        return true;
125
                }
126
                return false;
127
        }
128

    
129
         // * Adapter/
130
        class LoadBalancerAdapter extends ArrayAdapter<LoadBalancer> {
131
        
132
                LoadBalancerAdapter() {
133
                        super(ListLoadBalancersActivity.this,
134
                                        R.layout.list_loadbalancer_item, loadBalancers);
135
                }
136
        
137
                public View getView(int position, View convertView, ViewGroup parent) {
138
                        LoadBalancer loadBalancer = loadBalancers[position];
139
                        LayoutInflater inflater = getLayoutInflater();
140
                        View row = inflater.inflate(R.layout.list_loadbalancer_item,
141
                                        parent, false);
142
                        
143
                        ImageView status = (ImageView) row.findViewById(R.id.load_balancer_status);
144
                        if(loadBalancer.getStatus().equals("DELETED") || loadBalancer.getStatus().equals("PENDING_DELETE")){
145
                                status.setImageResource(R.drawable.deny_rule);
146
                        } else {
147
                                status.setImageResource(R.drawable.allow_rule);
148
                        }
149
        
150
                        TextView label = (TextView) row.findViewById(R.id.label);
151
                        label.setText(loadBalancer.getName());
152
                        
153
                        TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
154
                        sublabel.setText("ID: " + loadBalancer.getId());
155
        
156
                        return (row);
157
                }
158
        }
159

    
160
        private void displayLoadingCell() {
161
                    String a[] = new String[1];
162
                    a[0] = "Loading...";
163
                setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell, R.id.loading_label, a));
164
                getListView().setTextFilterEnabled(true);
165
                getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
166
                getListView().setItemsCanFocus(false);
167
            }
168

    
169
        private void showAlert(String title, String message) {
170
                // Can't create handler inside thread that has not called
171
                // Looper.prepare()
172
                // Looper.prepare();
173
                try {
174
                        AlertDialog alert = new AlertDialog.Builder(this).create();
175
                        alert.setTitle(title);
176
                        alert.setMessage(message);
177
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
178
                                public void onClick(DialogInterface dialog, int which) {
179
                                        return;
180
                                }
181
                        });
182
                        alert.show();
183
                } catch (Exception e) {
184
                        e.printStackTrace();
185
                }
186
        }
187

    
188
        private class LoadLoadBalancersTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> {
189
                private LoadBalancersException exception;
190
        
191
                @Override
192
                protected ArrayList<LoadBalancer> doInBackground(Void... arg0) {
193
                        ArrayList<LoadBalancer> loadBalancers = null;
194
                        try {
195
                                loadBalancers = (new LoadBalancerManager(context)).createList();
196
                        } catch (LoadBalancersException e) {
197
                                exception = e;
198
                        }
199
                        return loadBalancers;
200
                }
201
        
202
                @Override
203
                protected void onPostExecute(ArrayList<LoadBalancer> result) {
204
                        if (exception != null) {
205
                                showAlert("Error", exception.getMessage());
206
                        }
207
                        setLoadBalancersList(result);
208
                }
209
        }
210

    
211
        @Override
212
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
213
                super.onActivityResult(requestCode, resultCode, data);
214

    
215
                if (resultCode == RESULT_OK) {
216
                        // a sub-activity kicked back, so we want to refresh the server list
217
                        loadLoadBalancers();
218
                }
219
        }
220
}