root / src / com / rackspacecloud / android / ViewServerActivity.java @ 51fdecfb
History | View | Annotate | Download (26 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspacecloud.android; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.io.StringReader; |
8 |
import java.util.Arrays; |
9 |
import java.util.Iterator; |
10 |
|
11 |
import javax.xml.parsers.FactoryConfigurationError; |
12 |
import javax.xml.parsers.ParserConfigurationException; |
13 |
import javax.xml.parsers.SAXParser; |
14 |
import javax.xml.parsers.SAXParserFactory; |
15 |
|
16 |
import org.apache.http.HttpResponse; |
17 |
import org.apache.http.client.ClientProtocolException; |
18 |
import org.apache.http.impl.client.BasicResponseHandler; |
19 |
import org.xml.sax.InputSource; |
20 |
import org.xml.sax.SAXException; |
21 |
import org.xml.sax.XMLReader; |
22 |
|
23 |
import android.app.Activity; |
24 |
import android.app.AlertDialog; |
25 |
import android.app.Dialog; |
26 |
import android.content.Context; |
27 |
import android.content.DialogInterface; |
28 |
import android.content.Intent; |
29 |
import android.graphics.Color; |
30 |
import android.os.AsyncTask; |
31 |
import android.os.Bundle; |
32 |
import android.util.Log; |
33 |
import android.view.Menu; |
34 |
import android.view.MenuInflater; |
35 |
import android.view.MenuItem; |
36 |
import android.view.View; |
37 |
import android.view.View.OnClickListener; |
38 |
import android.widget.Button; |
39 |
import android.widget.EditText; |
40 |
import android.widget.LinearLayout; |
41 |
import android.widget.TextView; |
42 |
import android.widget.Toast; |
43 |
|
44 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
45 |
import com.rackspace.cloud.servers.api.client.Flavor; |
46 |
import com.rackspace.cloud.servers.api.client.Image; |
47 |
import com.rackspace.cloud.servers.api.client.Server; |
48 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
49 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
50 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
51 |
|
52 |
/**
|
53 |
* @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
54 |
*
|
55 |
*/
|
56 |
public class ViewServerActivity extends Activity { |
57 |
|
58 |
private Server server;
|
59 |
private boolean ipAddressesLoaded; // to prevent polling from loading tons of duplicates |
60 |
private Flavor[] flavors; |
61 |
private String[] flavorNames; |
62 |
private String selectedFlavorId; |
63 |
Context context;
|
64 |
//private boolean imageLoaded;
|
65 |
private String modifiedServerName; |
66 |
private Image[] images; |
67 |
private String[] imageNames; |
68 |
private String selectedImageId; |
69 |
private boolean isPolling; |
70 |
private PollServerTask pollServerTask;
|
71 |
private boolean canPoll; |
72 |
private boolean noAskForConfirm; |
73 |
|
74 |
/** Called when the activity is first created. */
|
75 |
@Override
|
76 |
public void onCreate(Bundle savedInstanceState) { |
77 |
super.onCreate(savedInstanceState);
|
78 |
server = (Server) this.getIntent().getExtras().get("server"); |
79 |
context = getApplicationContext(); |
80 |
setContentView(R.layout.viewserver); |
81 |
restoreState(savedInstanceState); |
82 |
} |
83 |
|
84 |
@Override
|
85 |
protected void onSaveInstanceState(Bundle outState) { |
86 |
super.onSaveInstanceState(outState);
|
87 |
outState.putSerializable("server", server);
|
88 |
outState.putBoolean("noAskForConfirm", noAskForConfirm);
|
89 |
if(pollServerTask != null && isPolling){ |
90 |
pollServerTask.cancel(true);
|
91 |
} |
92 |
outState.putBoolean("wasPolling", isPolling);
|
93 |
} |
94 |
|
95 |
private void restoreState(Bundle state) { |
96 |
if(state != null && state.containsKey("noAskForConfirm")){ |
97 |
noAskForConfirm = state.getBoolean("noAskForConfirm");
|
98 |
} |
99 |
if(state != null && state.containsKey("wasPolling") && state.getBoolean("wasPolling") == true){ |
100 |
pollServerTask = new PollServerTask();
|
101 |
pollServerTask.execute((Void[]) null); |
102 |
} |
103 |
if (server == null && state != null && state.containsKey("server")) { |
104 |
server = (Server) state.getSerializable("server");
|
105 |
//imageLoaded = state.getBoolean("imageLoaded");
|
106 |
} |
107 |
canPoll = true;
|
108 |
loadServerData(); |
109 |
setupButtons(); |
110 |
loadFlavors(); |
111 |
loadImages(); |
112 |
} |
113 |
/*
|
114 |
private void loadImage() {
|
115 |
// hate to do this, but devices run out of memory after a few rotations
|
116 |
// because the background images are so large
|
117 |
if (!imageLoaded) {
|
118 |
ImageView osLogo = (ImageView) findViewById(R.id.view_server_os_logo);
|
119 |
osLogo.setAlpha(100);
|
120 |
osLogo.setImageResource(server.getImage().logoResourceId());
|
121 |
imageLoaded = true;
|
122 |
}
|
123 |
|
124 |
}
|
125 |
*/
|
126 |
|
127 |
/*
|
128 |
* need to manage the polling task
|
129 |
* if the activity is stopped
|
130 |
*/
|
131 |
@Override
|
132 |
public void onStop(){ |
133 |
super.onStop();
|
134 |
if(pollServerTask != null && isPolling){ |
135 |
pollServerTask.cancel(true);
|
136 |
isPolling = true;
|
137 |
} |
138 |
canPoll = false;
|
139 |
|
140 |
} |
141 |
|
142 |
/*
|
143 |
* restart the pollingtask
|
144 |
* if it was running before
|
145 |
*
|
146 |
*/
|
147 |
@Override
|
148 |
public void onStart(){ |
149 |
super.onStart();
|
150 |
if(isPolling){
|
151 |
pollServerTask = new PollServerTask();
|
152 |
pollServerTask.execute((Void[]) null); |
153 |
} |
154 |
canPoll = true;
|
155 |
|
156 |
} |
157 |
|
158 |
private void loadServerData() { |
159 |
TextView name = (TextView) findViewById(R.id.view_server_name); |
160 |
name.setText(server.getName()); |
161 |
|
162 |
TextView os = (TextView) findViewById(R.id.view_server_os); |
163 |
os.setText(server.getImage().getName()); |
164 |
|
165 |
TextView memory = (TextView) findViewById(R.id.view_server_memory); |
166 |
memory.setText(server.getFlavor().getRam() + " MB");
|
167 |
|
168 |
TextView disk = (TextView) findViewById(R.id.view_server_disk); |
169 |
disk.setText(server.getFlavor().getDisk() + " GB");
|
170 |
|
171 |
TextView status = (TextView) findViewById(R.id.view_server_status); |
172 |
|
173 |
if(noAskForConfirm == false){ |
174 |
if(status.getText().toString().contains("VERIFY_RESIZE")){ |
175 |
//show the confimresizeactivity
|
176 |
noAskForConfirm = true;
|
177 |
Intent viewIntent = new Intent(getApplicationContext(), ConfirmResizeActivity.class);
|
178 |
viewIntent.putExtra("server", server);
|
179 |
startActivity(viewIntent); |
180 |
} |
181 |
} |
182 |
|
183 |
// show status and possibly the progress, with polling
|
184 |
if (!"ACTIVE".equals(server.getStatus())) { |
185 |
status.setText(server.getStatus() + " - " + server.getProgress() + "%"); |
186 |
pollServerTask = new PollServerTask();
|
187 |
pollServerTask.execute((Void[]) null); |
188 |
} else {
|
189 |
status.setText(server.getStatus()); |
190 |
} |
191 |
|
192 |
if (!ipAddressesLoaded) {
|
193 |
// public IPs
|
194 |
int layoutIndex = 12; // public IPs start here |
195 |
LinearLayout layout = (LinearLayout) this.findViewById(R.id.view_server_layout);
|
196 |
String publicIps[] = server.getPublicIpAddresses(); |
197 |
for (int i = 0; i < publicIps.length; i++) { |
198 |
TextView tv = new TextView(this.getBaseContext()); |
199 |
tv.setLayoutParams(os.getLayoutParams()); // easy quick styling! :)
|
200 |
tv.setTypeface(tv.getTypeface(), 1); // 1 == bold |
201 |
tv.setTextSize(os.getTextSize()); |
202 |
tv.setTextColor(Color.WHITE);
|
203 |
tv.setText(publicIps[i]); |
204 |
layout.addView(tv, layoutIndex++); |
205 |
} |
206 |
|
207 |
// private IPs
|
208 |
layoutIndex++; // skip over the Private IPs label
|
209 |
String privateIps[] = server.getPrivateIpAddresses(); |
210 |
for (int i = 0; i < privateIps.length; i++) { |
211 |
TextView tv = new TextView(this.getBaseContext()); |
212 |
tv.setLayoutParams(os.getLayoutParams()); // easy quick styling! :)
|
213 |
tv.setTypeface(tv.getTypeface(), 1); // 1 == bold |
214 |
tv.setTextSize(os.getTextSize()); |
215 |
tv.setTextColor(Color.WHITE);
|
216 |
tv.setText(privateIps[i]); |
217 |
layout.addView(tv, layoutIndex++); |
218 |
} |
219 |
ipAddressesLoaded = true;
|
220 |
} |
221 |
|
222 |
//loadImage();
|
223 |
} |
224 |
|
225 |
private void loadFlavors() { |
226 |
flavorNames = new String[Flavor.getFlavors().size()]; |
227 |
flavors = new Flavor[Flavor.getFlavors().size()];
|
228 |
|
229 |
Iterator<Flavor> iter = Flavor.getFlavors().values().iterator();
|
230 |
int i = 0; |
231 |
while (iter.hasNext()) {
|
232 |
Flavor flavor = iter.next(); |
233 |
flavors[i] = flavor; |
234 |
flavorNames[i] = flavor.getName() + ", " + flavor.getDisk() + " GB disk"; |
235 |
i++; |
236 |
} |
237 |
selectedFlavorId = flavors[0].getId();
|
238 |
} |
239 |
|
240 |
private void loadImages() { |
241 |
imageNames = new String[Image.getImages().size()]; |
242 |
images = new Image[Image.getImages().size()]; |
243 |
|
244 |
Iterator<Image> iter = Image.getImages().values().iterator(); |
245 |
int i = 0; |
246 |
while (iter.hasNext()) {
|
247 |
Image image = iter.next();
|
248 |
images[i] = image; |
249 |
imageNames[i] = image.getName(); |
250 |
i++; |
251 |
} |
252 |
//sort arrays so they display nicely in the spinner
|
253 |
Arrays.sort(images);
|
254 |
Arrays.sort(imageNames);
|
255 |
selectedImageId = images[0].getId();
|
256 |
|
257 |
} |
258 |
|
259 |
private void setupButton(int resourceId, OnClickListener onClickListener) { |
260 |
Button button = (Button) findViewById(resourceId); |
261 |
button.setOnClickListener(onClickListener); |
262 |
} |
263 |
|
264 |
private void setupButtons() { |
265 |
setupButton(R.id.view_server_soft_reboot_button, new OnClickListener() {
|
266 |
public void onClick(View v) { |
267 |
showDialog(R.id.view_server_soft_reboot_button); |
268 |
} |
269 |
}); |
270 |
|
271 |
setupButton(R.id.view_server_hard_reboot_button, new OnClickListener() {
|
272 |
public void onClick(View v) { |
273 |
showDialog(R.id.view_server_hard_reboot_button); |
274 |
} |
275 |
}); |
276 |
|
277 |
setupButton(R.id.view_server_resize_button, new OnClickListener() {
|
278 |
public void onClick(View v) { |
279 |
showDialog(R.id.view_server_resize_button); |
280 |
} |
281 |
}); |
282 |
|
283 |
setupButton(R.id.view_server_delete_button, new OnClickListener() {
|
284 |
public void onClick(View v) { |
285 |
showDialog(R.id.view_server_delete_button); |
286 |
} |
287 |
}); |
288 |
|
289 |
setupButton(R.id.view_server_rename_button, new OnClickListener() {
|
290 |
public void onClick(View v) { |
291 |
showDialog(R.id.view_server_rename_button); |
292 |
} |
293 |
}); |
294 |
|
295 |
setupButton(R.id.view_server_rebuild_button, new OnClickListener() {
|
296 |
public void onClick(View v) { |
297 |
showDialog(R.id.view_server_rebuild_button); |
298 |
} |
299 |
}); |
300 |
|
301 |
|
302 |
setupButton(R.id.view_server_backup_button, new OnClickListener() {
|
303 |
public void onClick(View v) { |
304 |
Intent viewIntent = new Intent(v.getContext(), BackupServerActivity.class);
|
305 |
viewIntent.putExtra("server", server);
|
306 |
startActivity(viewIntent); |
307 |
} |
308 |
}); |
309 |
|
310 |
setupButton(R.id.view_server_password_button, new OnClickListener() {
|
311 |
public void onClick(View v) { |
312 |
Intent viewIntent = new Intent(v.getContext(), PasswordServerActivity.class);
|
313 |
viewIntent.putExtra("server", server);
|
314 |
startActivity(viewIntent); |
315 |
} |
316 |
}); |
317 |
|
318 |
setupButton(R.id.view_server_ping_button, new OnClickListener() {
|
319 |
public void onClick(View v) { |
320 |
Intent viewIntent = new Intent(v.getContext(), PingServerActivity.class);
|
321 |
|
322 |
//ping the first public ip
|
323 |
viewIntent.putExtra("ipAddress", server.getPublicIpAddresses()[0]); |
324 |
startActivity(viewIntent); |
325 |
|
326 |
|
327 |
} |
328 |
}); |
329 |
|
330 |
} |
331 |
|
332 |
private void showToast(String message) { |
333 |
Context context = getApplicationContext();
|
334 |
int duration = Toast.LENGTH_SHORT;
|
335 |
Toast toast = Toast.makeText(context, message, duration); |
336 |
toast.show(); |
337 |
} |
338 |
|
339 |
/**
|
340 |
* @return the server
|
341 |
*/
|
342 |
public Server getServer() {
|
343 |
return server;
|
344 |
} |
345 |
|
346 |
/**
|
347 |
* @param server the server to set
|
348 |
*/
|
349 |
public void setServer(Server server) { |
350 |
this.server = server;
|
351 |
} |
352 |
|
353 |
//setup menu for when menu button is pressed
|
354 |
public boolean onCreateOptionsMenu(Menu menu) { |
355 |
super.onCreateOptionsMenu(menu);
|
356 |
MenuInflater inflater = getMenuInflater(); |
357 |
inflater.inflate(R.menu.view_server_activity_menu, menu); |
358 |
return true; |
359 |
} |
360 |
|
361 |
@Override
|
362 |
//in options menu, when add account is selected go to add account activity
|
363 |
public boolean onOptionsItemSelected(MenuItem item) { |
364 |
switch (item.getItemId()) {
|
365 |
case R.id.refresh_server:
|
366 |
loadServerData(); |
367 |
return true; |
368 |
} |
369 |
return false; |
370 |
} |
371 |
|
372 |
private void startServerError(String message, HttpBundle bundle){ |
373 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
374 |
viewIntent.putExtra("errorMessage", message);
|
375 |
viewIntent.putExtra("response", bundle.getResponseText());
|
376 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
377 |
startActivity(viewIntent); |
378 |
} |
379 |
|
380 |
@Override
|
381 |
protected Dialog onCreateDialog(int id) { |
382 |
if(server == null){ |
383 |
return new AlertDialog.Builder(ViewServerActivity.this) |
384 |
.setIcon(R.drawable.alert_dialog_icon) |
385 |
.setTitle("Error")
|
386 |
.setMessage("Server is Busy")
|
387 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
388 |
public void onClick(DialogInterface dialog, int whichButton) { |
389 |
// User clicked Cancel so do some stuff
|
390 |
} |
391 |
}) |
392 |
.create(); |
393 |
} |
394 |
else{
|
395 |
switch (id) {
|
396 |
case R.id.view_server_soft_reboot_button:
|
397 |
return new AlertDialog.Builder(ViewServerActivity.this) |
398 |
.setIcon(R.drawable.alert_dialog_icon) |
399 |
.setTitle("Soft Reboot")
|
400 |
.setMessage("Are you sure you want to perform a soft reboot?")
|
401 |
.setPositiveButton("Reboot Server", new DialogInterface.OnClickListener() { |
402 |
public void onClick(DialogInterface dialog, int whichButton) { |
403 |
// User clicked OK so do some stuff
|
404 |
new SoftRebootServerTask().execute((Void[]) null); |
405 |
} |
406 |
}) |
407 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
408 |
public void onClick(DialogInterface dialog, int whichButton) { |
409 |
// User clicked Cancel so do some stuff
|
410 |
} |
411 |
}) |
412 |
.create(); |
413 |
case R.id.view_server_hard_reboot_button:
|
414 |
return new AlertDialog.Builder(ViewServerActivity.this) |
415 |
.setIcon(R.drawable.alert_dialog_icon) |
416 |
.setTitle("Hard Reboot")
|
417 |
.setMessage("Are you sure you want to perform a hard reboot?")
|
418 |
.setPositiveButton("Reboot Server", new DialogInterface.OnClickListener() { |
419 |
public void onClick(DialogInterface dialog, int whichButton) { |
420 |
// User clicked OK so do some stuff
|
421 |
new HardRebootServerTask().execute((Void[]) null); |
422 |
} |
423 |
}) |
424 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
425 |
public void onClick(DialogInterface dialog, int whichButton) { |
426 |
// User clicked Cancel so do some stuff
|
427 |
} |
428 |
}) |
429 |
.create(); |
430 |
case R.id.view_server_resize_button:
|
431 |
return new AlertDialog.Builder(ViewServerActivity.this) |
432 |
.setItems(flavorNames, new ResizeClickListener())
|
433 |
.setIcon(R.drawable.alert_dialog_icon) |
434 |
.setTitle("Resize Server")
|
435 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
436 |
public void onClick(DialogInterface dialog, int whichButton) { |
437 |
// User clicked Cancel so do some stuff
|
438 |
} |
439 |
}) |
440 |
.create(); |
441 |
case R.id.view_server_delete_button:
|
442 |
return new AlertDialog.Builder(ViewServerActivity.this) |
443 |
.setIcon(R.drawable.alert_dialog_icon) |
444 |
.setTitle("Delete Server")
|
445 |
.setMessage("Are you sure you want to delete this server? This operation cannot be undone and all backups will be deleted.")
|
446 |
.setPositiveButton("Delete Server", new DialogInterface.OnClickListener() { |
447 |
public void onClick(DialogInterface dialog, int whichButton) { |
448 |
// User clicked OK so do some stuff
|
449 |
new DeleteServerTask().execute((Void[]) null); |
450 |
} |
451 |
}) |
452 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
453 |
public void onClick(DialogInterface dialog, int whichButton) { |
454 |
// User clicked Cancel so do some stuff
|
455 |
} |
456 |
}) |
457 |
.create(); |
458 |
case R.id.view_server_rename_button:
|
459 |
final EditText input = new EditText(this); |
460 |
input.setText(server.getName()); |
461 |
return new AlertDialog.Builder(ViewServerActivity.this) |
462 |
.setIcon(R.drawable.alert_dialog_icon) |
463 |
.setView(input) |
464 |
.setTitle("Rename")
|
465 |
.setMessage("Enter new name for server: ")
|
466 |
.setPositiveButton("Rename", new DialogInterface.OnClickListener() { |
467 |
public void onClick(DialogInterface dialog, int whichButton) { |
468 |
// User clicked OK so do some stuff
|
469 |
modifiedServerName = input.getText().toString(); |
470 |
new RenameServerTask().execute((Void[]) null); |
471 |
} |
472 |
}) |
473 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
474 |
public void onClick(DialogInterface dialog, int whichButton) { |
475 |
// User clicked Cancel so do some stuff
|
476 |
} |
477 |
}) |
478 |
.create(); |
479 |
case R.id.view_server_rebuild_button:
|
480 |
return new AlertDialog.Builder(ViewServerActivity.this) |
481 |
.setItems(imageNames, new RebuildClickListener())
|
482 |
.setIcon(R.drawable.alert_dialog_icon) |
483 |
.setTitle("Rebuild Server")
|
484 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
485 |
public void onClick(DialogInterface dialog, int whichButton) { |
486 |
// User clicked Cancel so do some stuff
|
487 |
} |
488 |
}) |
489 |
.create(); |
490 |
} |
491 |
} |
492 |
return new AlertDialog.Builder(ViewServerActivity.this) |
493 |
.setIcon(R.drawable.alert_dialog_icon) |
494 |
.setTitle("Error")
|
495 |
.setMessage("Server is Busy")
|
496 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
497 |
public void onClick(DialogInterface dialog, int whichButton) { |
498 |
// User clicked Cancel so do some stuff
|
499 |
} |
500 |
}) |
501 |
.create(); |
502 |
} |
503 |
|
504 |
private class ResizeClickListener implements android.content.DialogInterface.OnClickListener { |
505 |
|
506 |
public void onClick(DialogInterface dialog, int which) { |
507 |
//need to add one because server flavors state at index 1
|
508 |
selectedFlavorId = which + 1 + ""; |
509 |
new ResizeServerTask().execute((Void[]) null); |
510 |
} |
511 |
|
512 |
} |
513 |
|
514 |
private class RebuildClickListener implements android.content.DialogInterface.OnClickListener { |
515 |
|
516 |
public void onClick(DialogInterface dialog, int which) { |
517 |
selectedImageId = images[which].getId() + "";
|
518 |
new RebuildServerTask().execute((Void[]) null); |
519 |
} |
520 |
|
521 |
} |
522 |
|
523 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
524 |
CloudServersException cse = new CloudServersException();
|
525 |
try {
|
526 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
527 |
String body = responseHandler.handleResponse(response);
|
528 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
529 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
530 |
XMLReader xmlReader = saxParser.getXMLReader(); |
531 |
xmlReader.setContentHandler(parser); |
532 |
xmlReader.parse(new InputSource(new StringReader(body))); |
533 |
cse = parser.getException(); |
534 |
} catch (ClientProtocolException e) {
|
535 |
cse = new CloudServersException();
|
536 |
cse.setMessage(e.getLocalizedMessage()); |
537 |
} catch (IOException e) { |
538 |
cse = new CloudServersException();
|
539 |
cse.setMessage(e.getLocalizedMessage()); |
540 |
} catch (ParserConfigurationException e) { |
541 |
cse = new CloudServersException();
|
542 |
cse.setMessage(e.getLocalizedMessage()); |
543 |
} catch (SAXException e) {
|
544 |
cse = new CloudServersException();
|
545 |
cse.setMessage(e.getLocalizedMessage()); |
546 |
} catch (FactoryConfigurationError e) { |
547 |
cse = new CloudServersException();
|
548 |
cse.setMessage(e.getLocalizedMessage()); |
549 |
} |
550 |
return cse;
|
551 |
} |
552 |
|
553 |
// HTTP request tasks
|
554 |
|
555 |
private class PollServerTask extends AsyncTask<Void, Void, Server> { |
556 |
|
557 |
private Server tempServer;
|
558 |
|
559 |
@Override
|
560 |
protected void onPreExecute(){ |
561 |
isPolling = true;
|
562 |
} |
563 |
|
564 |
@Override
|
565 |
protected Server doInBackground(Void... arg0) { |
566 |
if(isCancelled() || !canPoll){
|
567 |
return null; |
568 |
} |
569 |
try {
|
570 |
tempServer = (new ServerManager()).find(Integer.parseInt(server.getId()), context); |
571 |
} catch (NumberFormatException e) { |
572 |
// we're polling, so need to show exceptions
|
573 |
} catch (CloudServersException e) {
|
574 |
// we're polling, so need to show exceptions
|
575 |
} |
576 |
return tempServer;
|
577 |
} |
578 |
|
579 |
@Override
|
580 |
protected void onPostExecute(Server result) { |
581 |
server = result; |
582 |
if(server != null){ |
583 |
loadServerData(); |
584 |
} |
585 |
isPolling = false;
|
586 |
} |
587 |
|
588 |
@Override
|
589 |
protected void onCancelled (){ |
590 |
isPolling = false;
|
591 |
} |
592 |
|
593 |
} |
594 |
|
595 |
|
596 |
private class SoftRebootServerTask extends AsyncTask<Void, Void, HttpBundle> { |
597 |
|
598 |
private CloudServersException exception;
|
599 |
|
600 |
@Override
|
601 |
//let user know their process has started
|
602 |
protected void onPreExecute(){ |
603 |
showToast("Reboot process has begun");
|
604 |
} |
605 |
|
606 |
@Override
|
607 |
protected HttpBundle doInBackground(Void... arg0) { |
608 |
HttpBundle bundle = null;
|
609 |
try {
|
610 |
bundle = (new ServerManager()).reboot(server, ServerManager.SOFT_REBOOT, context);
|
611 |
} catch (CloudServersException e) {
|
612 |
exception = e; |
613 |
} |
614 |
return bundle;
|
615 |
} |
616 |
|
617 |
@Override
|
618 |
protected void onPostExecute(HttpBundle bundle) { |
619 |
HttpResponse response = bundle.getResponse(); |
620 |
if (response != null) { |
621 |
int statusCode = response.getStatusLine().getStatusCode();
|
622 |
if(statusCode == 202){ showToast("Reboot successful"); } |
623 |
if (statusCode != 202) { |
624 |
CloudServersException cse = parseCloudServersException(response); |
625 |
if ("".equals(cse.getMessage())) { |
626 |
startServerError("There was a problem rebooting your server.", bundle);
|
627 |
} else {
|
628 |
startServerError("There was a problem rebooting your server: " + cse.getMessage(), bundle);
|
629 |
} |
630 |
} |
631 |
} else if (exception != null) { |
632 |
startServerError("There was a problem rebooting your server: " + exception.getMessage(), bundle);
|
633 |
|
634 |
} |
635 |
} |
636 |
} |
637 |
|
638 |
private class HardRebootServerTask extends AsyncTask<Void, Void, HttpBundle> { |
639 |
|
640 |
private CloudServersException exception;
|
641 |
|
642 |
@Override
|
643 |
//let user know their process has started
|
644 |
protected void onPreExecute(){ |
645 |
showToast("Reboot process has begun");
|
646 |
} |
647 |
|
648 |
@Override
|
649 |
protected HttpBundle doInBackground(Void... arg0) { |
650 |
HttpBundle bundle = null;
|
651 |
try {
|
652 |
bundle = (new ServerManager()).reboot(server, ServerManager.HARD_REBOOT, context);
|
653 |
} catch (CloudServersException e) {
|
654 |
exception = e; |
655 |
} |
656 |
return bundle;
|
657 |
} |
658 |
|
659 |
@Override
|
660 |
protected void onPostExecute(HttpBundle bundle) { |
661 |
HttpResponse response = bundle.getResponse(); |
662 |
if (response != null) { |
663 |
int statusCode = response.getStatusLine().getStatusCode();
|
664 |
if(statusCode == 202){ showToast("Reboot successful"); } |
665 |
if (statusCode != 202) { |
666 |
CloudServersException cse = parseCloudServersException(response); |
667 |
if ("".equals(cse.getMessage())) { |
668 |
startServerError("There was a problem rebooting your server.", bundle);
|
669 |
} else {
|
670 |
startServerError("There was a problem rebooting your server: " + cse.getMessage(), bundle);
|
671 |
} |
672 |
} |
673 |
} else if (exception != null) { |
674 |
startServerError("There was a problem rebooting your server: " + exception.getMessage(), bundle);
|
675 |
|
676 |
} |
677 |
} |
678 |
} |
679 |
|
680 |
private class ResizeServerTask extends AsyncTask<Void, Void, HttpBundle> { |
681 |
|
682 |
private CloudServersException exception;
|
683 |
|
684 |
@Override
|
685 |
protected void onPreExecute(){ |
686 |
showToast("Resize process has begun, please confirm your resize after process finishes.");
|
687 |
} |
688 |
|
689 |
@Override
|
690 |
protected HttpBundle doInBackground(Void... arg0) { |
691 |
HttpBundle bundle = null;
|
692 |
try {
|
693 |
bundle = (new ServerManager()).resize(server, Integer.parseInt(selectedFlavorId), context); |
694 |
} catch (CloudServersException e) {
|
695 |
exception = e; |
696 |
} |
697 |
return bundle;
|
698 |
} |
699 |
|
700 |
@Override
|
701 |
protected void onPostExecute(HttpBundle bundle) { |
702 |
HttpResponse response = bundle.getResponse(); |
703 |
if (response != null) { |
704 |
int statusCode = response.getStatusLine().getStatusCode();
|
705 |
if (statusCode == 202) { |
706 |
pollServerTask = new PollServerTask();
|
707 |
pollServerTask.execute((Void[]) null); |
708 |
} else {
|
709 |
CloudServersException cse = parseCloudServersException(response); |
710 |
if ("".equals(cse.getMessage())) { |
711 |
startServerError("There was a problem resizing your server.", bundle);
|
712 |
} else {
|
713 |
startServerError("There was a problem resizing your server: " + cse.getMessage(), bundle);
|
714 |
} |
715 |
} |
716 |
} else if (exception != null) { |
717 |
startServerError("There was a problem resizing your server: " + exception.getMessage(), bundle);
|
718 |
|
719 |
} |
720 |
|
721 |
} |
722 |
} |
723 |
|
724 |
|
725 |
public class DeleteServerTask extends AsyncTask<Void, Void, HttpBundle> { |
726 |
|
727 |
private CloudServersException exception;
|
728 |
|
729 |
@Override
|
730 |
//let user know their process has started
|
731 |
protected void onPreExecute(){ |
732 |
showToast("Delete process has begun");
|
733 |
} |
734 |
@Override
|
735 |
protected HttpBundle doInBackground(Void... arg0) { |
736 |
HttpBundle bundle = null;
|
737 |
try {
|
738 |
bundle = (new ServerManager()).delete(server, context);
|
739 |
} catch (CloudServersException e) {
|
740 |
exception = e; |
741 |
} |
742 |
return bundle;
|
743 |
} |
744 |
|
745 |
@Override
|
746 |
protected void onPostExecute(HttpBundle bundle) { |
747 |
HttpResponse response = bundle.getResponse(); |
748 |
if (response != null) { |
749 |
int statusCode = response.getStatusLine().getStatusCode();
|
750 |
if (statusCode == 202) { |
751 |
showToast("Delete successful");
|
752 |
setResult(Activity.RESULT_OK); |
753 |
finish(); |
754 |
} else {
|
755 |
CloudServersException cse = parseCloudServersException(response); |
756 |
if ("".equals(cse.getMessage())) { |
757 |
startServerError("There was a problem deleting your server.", bundle);
|
758 |
} else {
|
759 |
startServerError("There was a problem deleting your server: " + cse.getMessage(), bundle);
|
760 |
} |
761 |
} |
762 |
} else if (exception != null) { |
763 |
startServerError("There was a problem deleting your server: " + exception.getMessage(), bundle);
|
764 |
} |
765 |
} |
766 |
} |
767 |
|
768 |
private class RenameServerTask extends AsyncTask<Void, Void, HttpBundle> { |
769 |
|
770 |
private CloudServersException exception;
|
771 |
|
772 |
@Override
|
773 |
//let user know their process has started
|
774 |
protected void onPreExecute(){ |
775 |
showToast("Rename process has begun.");
|
776 |
} |
777 |
|
778 |
@Override
|
779 |
protected HttpBundle doInBackground(Void... arg0) { |
780 |
HttpBundle bundle = null;
|
781 |
try {
|
782 |
bundle = (new ServerManager()).rename(server, modifiedServerName, context);
|
783 |
} catch (CloudServersException e) {
|
784 |
exception = e; |
785 |
} |
786 |
return bundle;
|
787 |
} |
788 |
|
789 |
@Override
|
790 |
protected void onPostExecute(HttpBundle bundle) { |
791 |
HttpResponse response = bundle.getResponse(); |
792 |
if (response != null) { |
793 |
int statusCode = response.getStatusLine().getStatusCode();
|
794 |
if (statusCode == 204) { |
795 |
showToast("Rename successful");
|
796 |
pollServerTask = new PollServerTask();
|
797 |
pollServerTask.execute((Void[]) null); |
798 |
} else {
|
799 |
CloudServersException cse = parseCloudServersException(response); |
800 |
if ("".equals(cse.getMessage())) { |
801 |
startServerError("There was a problem renaming your server.", bundle);
|
802 |
} else {
|
803 |
startServerError("There was a problem renaming your server: " + cse.getMessage(), bundle);
|
804 |
} |
805 |
} |
806 |
} |
807 |
else if (exception != null) { |
808 |
startServerError("There was a problem renaming your server: " + exception.getMessage(), bundle);
|
809 |
} |
810 |
} |
811 |
|
812 |
} |
813 |
|
814 |
private class RebuildServerTask extends AsyncTask<Void, Void, HttpBundle> { |
815 |
|
816 |
private CloudServersException exception;
|
817 |
|
818 |
@Override
|
819 |
protected void onPreExecute(){ |
820 |
showToast("Rebuild process has begun");
|
821 |
} |
822 |
|
823 |
@Override
|
824 |
protected HttpBundle doInBackground(Void... arg0) { |
825 |
HttpBundle bundle = null;
|
826 |
try {
|
827 |
bundle = (new ServerManager()).rebuild(server, Integer.parseInt(selectedImageId), context); |
828 |
} catch (CloudServersException e) {
|
829 |
exception = e; |
830 |
} |
831 |
return bundle;
|
832 |
} |
833 |
|
834 |
@Override
|
835 |
protected void onPostExecute(HttpBundle bundle) { |
836 |
HttpResponse response = bundle.getResponse(); |
837 |
if (response != null) { |
838 |
int statusCode = response.getStatusLine().getStatusCode();
|
839 |
if (statusCode == 202) { |
840 |
pollServerTask = new PollServerTask();
|
841 |
pollServerTask.execute((Void[]) null); |
842 |
} else {
|
843 |
CloudServersException cse = parseCloudServersException(response); |
844 |
if ("".equals(cse.getMessage())) { |
845 |
startServerError("There was a problem rebuilding your server.", bundle);
|
846 |
} else {
|
847 |
startServerError("There was a problem rebuilding your server: " + cse.getMessage(), bundle);
|
848 |
} |
849 |
} |
850 |
} else if (exception != null) { |
851 |
startServerError("There was a problem rebuilding your server: " + exception.getMessage(), bundle);
|
852 |
} |
853 |
|
854 |
} |
855 |
} |
856 |
|
857 |
|
858 |
} |