Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (6 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.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.View;
15
import android.view.ViewGroup;
16
import android.view.WindowManager;
17
import android.view.ViewGroup.LayoutParams;
18
import android.widget.ArrayAdapter;
19
import android.widget.ListView;
20
import android.widget.ProgressBar;
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.loadbalancers.api.client.http.LoadBalancersException;
26

    
27
public class ListLoadBalancersActivity extends ListActivity {
28
        private LoadBalancer[] loadBalancers;
29
        ProgressDialog pDialog;
30
        
31
        @Override
32
        public void onCreate(Bundle savedInstanceState) {
33
                super.onCreate(savedInstanceState);
34
                setContentView(R.layout.list_loadbalancers);
35
                restoreState(savedInstanceState);
36
        }
37

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

    
44
        private void restoreState(Bundle state) {
45
                if (state != null && state.containsKey("loadBalancers")) {
46
                        loadBalancers = (LoadBalancer[]) state
47
                                        .getSerializable("loadBalancers");
48
                        if (loadBalancers.length == 0) {
49
                                // displayNoServersCell();
50
                        } else {
51
                                getListView().setDividerHeight(1); // restore divider lines
52
                                setListAdapter(new LoadBalancerAdapter());
53
                        }
54
                } else {
55
                        loadLoadBalancers();
56
                }
57
        }
58

    
59
        protected void onListItemClick(ListView l, View v, int position, long id) {
60
                if (loadBalancers != null && loadBalancers.length > 0) {
61
                        Intent viewIntent = new Intent(this, ViewLoadBalancerActivity.class);
62
                        viewIntent.putExtra("loadBalancer", loadBalancers[position]);
63
                        Log.i("VIEWLOADBALANCERS: ", loadBalancers[position].getAlgorithm()
64
                                        + "," + loadBalancers[position].getProtocol() + ","
65
                                        + loadBalancers[position].getStatus());
66
                        startActivityForResult(viewIntent, 55); // arbitrary number; never
67
                                                                                                        // used again
68
                }
69
        }
70

    
71
        private void loadLoadBalancers() {
72
                new LoadLoadBalancersTask().execute((Void[]) null);
73
        }
74

    
75
        private void setLoadBalancersList(ArrayList<LoadBalancer> loadBalancers) {
76
                if (loadBalancers == null) {
77
                        loadBalancers = new ArrayList<LoadBalancer>();
78
                }
79
                String[] loadBalancerNames = new String[loadBalancers.size()];
80
                this.loadBalancers = new LoadBalancer[loadBalancers.size()];
81

    
82
                if (loadBalancers != null) {
83
                        for (int i = 0; i < loadBalancers.size(); i++) {
84
                                LoadBalancer loadBalancer = loadBalancers.get(i);
85
                                this.loadBalancers[i] = loadBalancer;
86
                                loadBalancerNames[i] = loadBalancer.getName();
87
                        }
88
                }
89

    
90
                if (loadBalancerNames.length == 0) {
91
                        // displayNoServersCell();
92
                } else {
93
                        getListView().setDividerHeight(1); // restore divider lines
94
                        setListAdapter(new LoadBalancerAdapter());
95
                }
96
        }
97

    
98
        protected void showDialog() {
99
                pDialog = new ProgressDialog(this, R.style.NewDialog);
100
                // // Set blur to background
101
                WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes();
102
                lp.dimAmount = 0.0f;
103
                pDialog.getWindow().setAttributes(lp);
104
                pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
105
                pDialog.show();
106
                pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
107
        }
108

    
109
        private void showAlert(String title, String message) {
110
                // Can't create handler inside thread that has not called
111
                // Looper.prepare()
112
                // Looper.prepare();
113
                try {
114
                        AlertDialog alert = new AlertDialog.Builder(this).create();
115
                        alert.setTitle(title);
116
                        alert.setMessage(message);
117
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
118
                                public void onClick(DialogInterface dialog, int which) {
119
                                        return;
120
                                }
121
                        });
122
                        alert.show();
123
                } catch (Exception e) {
124
                        e.printStackTrace();
125
                }
126
        }
127

    
128
        private class LoadLoadBalancersTask extends AsyncTask<Void, Void, ArrayList<LoadBalancer>> {
129
                private LoadBalancersException exception;
130

    
131
                protected void onPreExecute() {
132
                Log.d("rscloudactivity", " pre execute async");
133
                showDialog();
134
            }
135
                
136
                @Override
137
                protected ArrayList<LoadBalancer> doInBackground(Void... arg0) {
138
                        ArrayList<LoadBalancer> loadBalancers = null;
139
                        try {
140
                                loadBalancers = (new LoadBalancerManager()).createList();
141
                        } catch (LoadBalancersException e) {
142
                                exception = e;
143
                        }
144
                        pDialog.dismiss();
145
                        return loadBalancers;
146
                }
147

    
148
                @Override
149
                protected void onPostExecute(ArrayList<LoadBalancer> result) {
150
                        if (exception != null) {
151
                                pDialog.dismiss();
152
                                showAlert("Error", exception.getMessage());
153
                        }
154
                        pDialog.dismiss();
155
                        setLoadBalancersList(result);
156
                }
157
        }
158
        
159
        @Override
160
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
161
                super.onActivityResult(requestCode, resultCode, data);
162

    
163
                if (resultCode == RESULT_OK) {
164
                        // a sub-activity kicked back, so we want to refresh the server list
165
                        loadLoadBalancers();
166
                }
167
        }
168

    
169
        // * Adapter/
170
        class LoadBalancerAdapter extends ArrayAdapter<LoadBalancer> {
171
                private static final int RESULT_OK = 200;
172

    
173
                LoadBalancerAdapter() {
174
                        super(ListLoadBalancersActivity.this,
175
                                        R.layout.list_loadbalancer_item, loadBalancers);
176
                }
177

    
178
                public View getView(int position, View convertView, ViewGroup parent) {
179
                        LoadBalancer loadBalancer = loadBalancers[position];
180
                        LayoutInflater inflater = getLayoutInflater();
181
                        View row = inflater.inflate(R.layout.list_loadbalancer_item,
182
                                        parent, false);
183

    
184
                        TextView label = (TextView) row.findViewById(R.id.label);
185
                        label.setText(loadBalancer.getName());
186
                        //
187
                        TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
188
                        sublabel.setText("ID: " + loadBalancer.getId());
189
                        //
190
                        // ImageView icon = (ImageView) row.findViewById(R.id.icon);
191
                        // icon.setImageResource(server.getImage().iconResourceId());
192

    
193
                        return (row);
194
                }
195
        }
196
}