Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ListLoadBalancersActivity.java @ 143cdf16

History | View | Annotate | Download (7 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.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 GaListActivity {
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
                trackPageView(GoogleAnalytics.PAGE_LOADBALANCERS);
37
                context = getApplicationContext();
38
                restoreState(savedInstanceState);
39
        }
40

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

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

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

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

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

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

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

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

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

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

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

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

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

    
216
        @Override
217
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
218
                super.onActivityResult(requestCode, resultCode, data);
219

    
220
                if (resultCode == RESULT_OK) {
221
                        // a sub-activity kicked back, so we want to refresh the server list
222
                        loadLoadBalancers();
223
                }
224
        }
225
}