root / src / com / rackspace / cloud / android / ListServersActivity.java @ 7dbfc514
History | View | Annotate | Download (6.2 kB)
1 | 35e7942d | Michael Mayo | /**
|
---|---|---|---|
2 | 35e7942d | Michael Mayo | *
|
3 | 35e7942d | Michael Mayo | */
|
4 | 7dbfc514 | koutsoub | package com.rackspace.cloud.android; |
5 | 35e7942d | Michael Mayo | |
6 | 35e7942d | Michael Mayo | import java.util.ArrayList; |
7 | 35e7942d | Michael Mayo | |
8 | 3e6fd0e9 | Michael Mayo | import android.app.AlertDialog; |
9 | a597c658 | Adam Menz | import android.content.Context; |
10 | 3e6fd0e9 | Michael Mayo | import android.content.DialogInterface; |
11 | 344675da | Michael Mayo | import android.content.Intent; |
12 | 35e7942d | Michael Mayo | import android.os.AsyncTask; |
13 | 35e7942d | Michael Mayo | import android.os.Bundle; |
14 | 35e7942d | Michael Mayo | import android.view.LayoutInflater; |
15 | d8f3bb33 | Michael Mayo | import android.view.Menu; |
16 | d8f3bb33 | Michael Mayo | import android.view.MenuInflater; |
17 | d8f3bb33 | Michael Mayo | import android.view.MenuItem; |
18 | 35e7942d | Michael Mayo | import android.view.View; |
19 | 35e7942d | Michael Mayo | import android.view.ViewGroup; |
20 | 35e7942d | Michael Mayo | import android.widget.ArrayAdapter; |
21 | 35e7942d | Michael Mayo | import android.widget.ImageView; |
22 | 35e7942d | Michael Mayo | import android.widget.ListView; |
23 | 35e7942d | Michael Mayo | import android.widget.TextView; |
24 | 35e7942d | Michael Mayo | |
25 | 48601850 | Mike Mayo | import com.rackspace.cloud.android.R; |
26 | 3e6fd0e9 | Michael Mayo | import com.rackspace.cloud.servers.api.client.CloudServersException; |
27 | 3e6fd0e9 | Michael Mayo | import com.rackspace.cloud.servers.api.client.Server; |
28 | 3e6fd0e9 | Michael Mayo | import com.rackspace.cloud.servers.api.client.ServerManager; |
29 | 3e6fd0e9 | Michael Mayo | |
30 | 35e7942d | Michael Mayo | /**
|
31 | e7274b25 | Michael Mayo | * @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
32 | 038ac9a4 | koutsoub | *
|
33 | 35e7942d | Michael Mayo | */
|
34 | 0edf6b39 | Adam Menz | public class ListServersActivity extends GaListActivity { |
35 | 038ac9a4 | koutsoub | |
36 | 35e7942d | Michael Mayo | private Server[] servers; |
37 | e7534f91 | Adam Menz | private Context context; |
38 | 038ac9a4 | koutsoub | |
39 | 35e7942d | Michael Mayo | @Override
|
40 | 35e7942d | Michael Mayo | public void onCreate(Bundle savedInstanceState) { |
41 | 35e7942d | Michael Mayo | super.onCreate(savedInstanceState);
|
42 | 5018a7f8 | Adam Menz | trackPageView(GoogleAnalytics.PAGE_SERVERS); |
43 | a597c658 | Adam Menz | context = getApplicationContext(); |
44 | 2d3dd41d | Michael Mayo | restoreState(savedInstanceState); |
45 | 35e7942d | Michael Mayo | } |
46 | 35e7942d | Michael Mayo | |
47 | 2d3dd41d | Michael Mayo | @Override
|
48 | 2d3dd41d | Michael Mayo | protected void onSaveInstanceState(Bundle outState) { |
49 | 2d3dd41d | Michael Mayo | super.onSaveInstanceState(outState);
|
50 | 2d3dd41d | Michael Mayo | outState.putSerializable("servers", servers);
|
51 | 2d3dd41d | Michael Mayo | } |
52 | 2d3dd41d | Michael Mayo | |
53 | 038ac9a4 | koutsoub | private void restoreState(Bundle state) { |
54 | e7534f91 | Adam Menz | if (state != null && state.containsKey("servers") && state.getSerializable("servers") != null) { |
55 | 038ac9a4 | koutsoub | servers = (Server[]) state.getSerializable("servers"); |
56 | 038ac9a4 | koutsoub | if (servers.length == 0) { |
57 | 038ac9a4 | koutsoub | displayNoServersCell(); |
58 | 038ac9a4 | koutsoub | } else {
|
59 | 038ac9a4 | koutsoub | getListView().setDividerHeight(1); // restore divider lines |
60 | 038ac9a4 | koutsoub | setListAdapter(new ServerAdapter());
|
61 | 038ac9a4 | koutsoub | } |
62 | 038ac9a4 | koutsoub | } else {
|
63 | 038ac9a4 | koutsoub | loadServers(); |
64 | 038ac9a4 | koutsoub | } |
65 | 038ac9a4 | koutsoub | } |
66 | 038ac9a4 | koutsoub | |
67 | 038ac9a4 | koutsoub | protected void onListItemClick(ListView l, View v, int position, long id) { |
68 | 038ac9a4 | koutsoub | if (servers != null && servers.length > 0) { |
69 | 038ac9a4 | koutsoub | Intent viewIntent = new Intent(this, ViewServerActivity.class); |
70 | 038ac9a4 | koutsoub | viewIntent.putExtra("server", servers[position]);
|
71 | 038ac9a4 | koutsoub | startActivityForResult(viewIntent, 55); // arbitrary number; never |
72 | 038ac9a4 | koutsoub | // used again
|
73 | 038ac9a4 | koutsoub | } |
74 | 038ac9a4 | koutsoub | } |
75 | 038ac9a4 | koutsoub | |
76 | 038ac9a4 | koutsoub | private void loadServers() { |
77 | 038ac9a4 | koutsoub | displayLoadingCell(); |
78 | 038ac9a4 | koutsoub | new LoadServersTask().execute((Void[]) null); |
79 | 038ac9a4 | koutsoub | } |
80 | 038ac9a4 | koutsoub | |
81 | 038ac9a4 | koutsoub | private void setServerList(ArrayList<Server> servers) { |
82 | 038ac9a4 | koutsoub | if (servers == null) { |
83 | 038ac9a4 | koutsoub | servers = new ArrayList<Server>(); |
84 | 038ac9a4 | koutsoub | } |
85 | 038ac9a4 | koutsoub | String[] serverNames = new String[servers.size()]; |
86 | 038ac9a4 | koutsoub | this.servers = new Server[servers.size()]; |
87 | 038ac9a4 | koutsoub | |
88 | 35e7942d | Michael Mayo | if (servers != null) { |
89 | 35e7942d | Michael Mayo | for (int i = 0; i < servers.size(); i++) { |
90 | 35e7942d | Michael Mayo | Server server = servers.get(i); |
91 | 35e7942d | Michael Mayo | this.servers[i] = server;
|
92 | 35e7942d | Michael Mayo | serverNames[i] = server.getName(); |
93 | 35e7942d | Michael Mayo | } |
94 | 35e7942d | Michael Mayo | } |
95 | 038ac9a4 | koutsoub | |
96 | 35e7942d | Michael Mayo | if (serverNames.length == 0) { |
97 | 35e7942d | Michael Mayo | displayNoServersCell(); |
98 | 35e7942d | Michael Mayo | } else {
|
99 | 038ac9a4 | koutsoub | getListView().setDividerHeight(1); // restore divider lines |
100 | 35e7942d | Michael Mayo | setListAdapter(new ServerAdapter());
|
101 | 35e7942d | Michael Mayo | } |
102 | 038ac9a4 | koutsoub | } |
103 | 038ac9a4 | koutsoub | |
104 | 038ac9a4 | koutsoub | private void displayLoadingCell() { |
105 | 038ac9a4 | koutsoub | String a[] = new String[1]; |
106 | 038ac9a4 | koutsoub | a[0] = "Loading..."; |
107 | 038ac9a4 | koutsoub | setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell, |
108 | 038ac9a4 | koutsoub | R.id.loading_label, a)); |
109 | 038ac9a4 | koutsoub | getListView().setTextFilterEnabled(true);
|
110 | 038ac9a4 | koutsoub | getListView().setDividerHeight(0); // hide the dividers so it won't look |
111 | 038ac9a4 | koutsoub | // like a list row
|
112 | 038ac9a4 | koutsoub | getListView().setItemsCanFocus(false);
|
113 | 038ac9a4 | koutsoub | } |
114 | 038ac9a4 | koutsoub | |
115 | 038ac9a4 | koutsoub | private void displayNoServersCell() { |
116 | 038ac9a4 | koutsoub | String a[] = new String[1]; |
117 | 038ac9a4 | koutsoub | a[0] = "No Servers"; |
118 | 038ac9a4 | koutsoub | setListAdapter(new ArrayAdapter<String>(this, R.layout.noserverscell, |
119 | 038ac9a4 | koutsoub | R.id.no_servers_label, a)); |
120 | 038ac9a4 | koutsoub | getListView().setTextFilterEnabled(true);
|
121 | 038ac9a4 | koutsoub | getListView().setDividerHeight(0); // hide the dividers so it won't look |
122 | 038ac9a4 | koutsoub | // like a list row
|
123 | 038ac9a4 | koutsoub | getListView().setItemsCanFocus(false);
|
124 | 038ac9a4 | koutsoub | } |
125 | 038ac9a4 | koutsoub | |
126 | 038ac9a4 | koutsoub | private void showAlert(String title, String message) { |
127 | 038ac9a4 | koutsoub | // Can't create handler inside thread that has not called
|
128 | 038ac9a4 | koutsoub | // Looper.prepare()
|
129 | 038ac9a4 | koutsoub | // Looper.prepare();
|
130 | 038ac9a4 | koutsoub | try {
|
131 | 038ac9a4 | koutsoub | AlertDialog alert = new AlertDialog.Builder(this).create(); |
132 | 038ac9a4 | koutsoub | alert.setTitle(title); |
133 | 038ac9a4 | koutsoub | alert.setMessage(message); |
134 | 038ac9a4 | koutsoub | alert.setButton("OK", new DialogInterface.OnClickListener() { |
135 | 038ac9a4 | koutsoub | public void onClick(DialogInterface dialog, int which) { |
136 | 038ac9a4 | koutsoub | return;
|
137 | 038ac9a4 | koutsoub | } |
138 | 038ac9a4 | koutsoub | }); |
139 | 038ac9a4 | koutsoub | alert.show(); |
140 | 038ac9a4 | koutsoub | } catch (Exception e) { |
141 | 038ac9a4 | koutsoub | e.printStackTrace(); |
142 | 038ac9a4 | koutsoub | } |
143 | 038ac9a4 | koutsoub | } |
144 | 038ac9a4 | koutsoub | |
145 | 038ac9a4 | koutsoub | private class LoadServersTask extends |
146 | 038ac9a4 | koutsoub | AsyncTask<Void, Void, ArrayList<Server>> { |
147 | 038ac9a4 | koutsoub | |
148 | 038ac9a4 | koutsoub | private CloudServersException exception;
|
149 | 038ac9a4 | koutsoub | |
150 | 35e7942d | Michael Mayo | @Override
|
151 | 35e7942d | Michael Mayo | protected ArrayList<Server> doInBackground(Void... arg0) { |
152 | 3e6fd0e9 | Michael Mayo | ArrayList<Server> servers = null; |
153 | 3e6fd0e9 | Michael Mayo | try {
|
154 | a597c658 | Adam Menz | servers = (new ServerManager()).createList(true, context); |
155 | 3e6fd0e9 | Michael Mayo | } catch (CloudServersException e) {
|
156 | 038ac9a4 | koutsoub | exception = e; |
157 | 3e6fd0e9 | Michael Mayo | } |
158 | 3e6fd0e9 | Michael Mayo | return servers;
|
159 | 35e7942d | Michael Mayo | } |
160 | 038ac9a4 | koutsoub | |
161 | 35e7942d | Michael Mayo | @Override
|
162 | 35e7942d | Michael Mayo | protected void onPostExecute(ArrayList<Server> result) { |
163 | 3e6fd0e9 | Michael Mayo | if (exception != null) { |
164 | 3e6fd0e9 | Michael Mayo | showAlert("Error", exception.getMessage());
|
165 | 3e6fd0e9 | Michael Mayo | } |
166 | 35e7942d | Michael Mayo | setServerList(result); |
167 | 35e7942d | Michael Mayo | } |
168 | 038ac9a4 | koutsoub | } |
169 | 038ac9a4 | koutsoub | |
170 | 038ac9a4 | koutsoub | @Override
|
171 | 038ac9a4 | koutsoub | public boolean onCreateOptionsMenu(Menu menu) { |
172 | 35e7942d | Michael Mayo | super.onCreateOptionsMenu(menu);
|
173 | 35e7942d | Michael Mayo | MenuInflater inflater = getMenuInflater(); |
174 | d8f3bb33 | Michael Mayo | inflater.inflate(R.menu.servers_list_menu, menu); |
175 | 35e7942d | Michael Mayo | return true; |
176 | 038ac9a4 | koutsoub | } |
177 | 038ac9a4 | koutsoub | |
178 | 038ac9a4 | koutsoub | @Override
|
179 | 038ac9a4 | koutsoub | public boolean onOptionsItemSelected(MenuItem item) { |
180 | 35e7942d | Michael Mayo | switch (item.getItemId()) {
|
181 | 344675da | Michael Mayo | case R.id.add_server:
|
182 | 038ac9a4 | koutsoub | startActivityForResult(new Intent(this, AddServerActivity.class), |
183 | 038ac9a4 | koutsoub | 56); // arbitrary number; never used again |
184 | 35e7942d | Michael Mayo | return true; |
185 | 35e7942d | Michael Mayo | case R.id.refresh:
|
186 | e7534f91 | Adam Menz | servers = null;
|
187 | d8f3bb33 | Michael Mayo | loadServers(); |
188 | 038ac9a4 | koutsoub | return true; |
189 | 35e7942d | Michael Mayo | } |
190 | 35e7942d | Michael Mayo | return false; |
191 | 038ac9a4 | koutsoub | } |
192 | 038ac9a4 | koutsoub | |
193 | 038ac9a4 | koutsoub | // */
|
194 | 038ac9a4 | koutsoub | |
195 | 35e7942d | Michael Mayo | class ServerAdapter extends ArrayAdapter<Server> { |
196 | 35e7942d | Michael Mayo | ServerAdapter() { |
197 | 35e7942d | Michael Mayo | super(ListServersActivity.this, R.layout.listservercell, servers);
|
198 | 35e7942d | Michael Mayo | } |
199 | 038ac9a4 | koutsoub | |
200 | 35e7942d | Michael Mayo | public View getView(int position, View convertView, ViewGroup parent) { |
201 | 038ac9a4 | koutsoub | |
202 | 35e7942d | Michael Mayo | Server server = servers[position]; |
203 | 35e7942d | Michael Mayo | LayoutInflater inflater = getLayoutInflater(); |
204 | 35e7942d | Michael Mayo | View row = inflater.inflate(R.layout.listservercell, parent, false); |
205 | 35e7942d | Michael Mayo | |
206 | 35e7942d | Michael Mayo | TextView label = (TextView) row.findViewById(R.id.label); |
207 | 35e7942d | Michael Mayo | label.setText(server.getName()); |
208 | 35e7942d | Michael Mayo | |
209 | 23cc0515 | Adam Menz | TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
210 | 23cc0515 | Adam Menz | sublabel.setText(server.getPublicIpAddresses()[0]);
|
211 | 35e7942d | Michael Mayo | |
212 | 35e7942d | Michael Mayo | ImageView icon = (ImageView) row.findViewById(R.id.icon); |
213 | d8f3bb33 | Michael Mayo | icon.setImageResource(server.getImage().iconResourceId()); |
214 | d8f3bb33 | Michael Mayo | |
215 | 038ac9a4 | koutsoub | return (row);
|
216 | 35e7942d | Michael Mayo | } |
217 | 35e7942d | Michael Mayo | } |
218 | 038ac9a4 | koutsoub | |
219 | af63e739 | Michael Mayo | @Override
|
220 | af63e739 | Michael Mayo | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
221 | 038ac9a4 | koutsoub | super.onActivityResult(requestCode, resultCode, data);
|
222 | 038ac9a4 | koutsoub | |
223 | 038ac9a4 | koutsoub | if (resultCode == RESULT_OK) {
|
224 | 038ac9a4 | koutsoub | // a sub-activity kicked back, so we want to refresh the server list
|
225 | 038ac9a4 | koutsoub | loadServers(); |
226 | 038ac9a4 | koutsoub | } |
227 | 038ac9a4 | koutsoub | } |
228 | 35e7942d | Michael Mayo | } |