Revision f58bf9f4 src/com/rackspacecloud/android/ViewServerActivity.java
b/src/com/rackspacecloud/android/ViewServerActivity.java | ||
---|---|---|
23 | 23 |
import android.app.AlertDialog; |
24 | 24 |
import android.app.Dialog; |
25 | 25 |
import android.content.DialogInterface; |
26 |
import android.content.Intent; |
|
26 | 27 |
import android.graphics.Color; |
27 | 28 |
import android.os.AsyncTask; |
28 | 29 |
import android.os.Bundle; |
29 | 30 |
import android.view.View; |
30 | 31 |
import android.view.View.OnClickListener; |
31 | 32 |
import android.widget.Button; |
33 |
import android.widget.EditText; |
|
32 | 34 |
import android.widget.ImageView; |
33 | 35 |
import android.widget.LinearLayout; |
34 | 36 |
import android.widget.TextView; |
35 | 37 |
|
36 | 38 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
37 | 39 |
import com.rackspace.cloud.servers.api.client.Flavor; |
40 |
import com.rackspace.cloud.servers.api.client.Image; |
|
38 | 41 |
import com.rackspace.cloud.servers.api.client.Server; |
39 | 42 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
40 | 43 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
... | ... | |
51 | 54 |
private String[] flavorNames; |
52 | 55 |
private String selectedFlavorId; |
53 | 56 |
private boolean imageLoaded; |
57 |
private String modifiedServerName; |
|
58 |
private Image[] images; |
|
59 |
private String[] imageNames; |
|
60 |
private String selectedImageId; |
|
54 | 61 |
|
55 | 62 |
/** Called when the activity is first created. */ |
56 | 63 |
@Override |
... | ... | |
76 | 83 |
loadServerData(); |
77 | 84 |
setupButtons(); |
78 | 85 |
loadFlavors(); |
86 |
loadImages(); |
|
79 | 87 |
} |
80 | 88 |
|
81 | 89 |
private void loadImage() { |
... | ... | |
160 | 168 |
} |
161 | 169 |
selectedFlavorId = flavors[0].getId(); |
162 | 170 |
} |
171 |
|
|
172 |
private void loadImages() { |
|
173 |
imageNames = new String[Image.getImages().size()]; |
|
174 |
images = new Image[Image.getImages().size()]; |
|
175 |
|
|
176 |
Iterator<Image> iter = Image.getImages().values().iterator(); |
|
177 |
int i = 0; |
|
178 |
while (iter.hasNext()) { |
|
179 |
Image image = iter.next(); |
|
180 |
images[i] = image; |
|
181 |
imageNames[i] = image.getName(); |
|
182 |
i++; |
|
183 |
} |
|
184 |
selectedImageId = images[0].getId(); |
|
185 |
|
|
186 |
} |
|
163 | 187 |
|
164 | 188 |
private void setupButton(int resourceId, OnClickListener onClickListener) { |
165 | 189 |
Button button = (Button) findViewById(resourceId); |
... | ... | |
191 | 215 |
} |
192 | 216 |
}); |
193 | 217 |
|
218 |
setupButton(R.id.view_server_rename_button, new OnClickListener() { |
|
219 |
public void onClick(View v) { |
|
220 |
showDialog(R.id.view_server_rename_button); |
|
221 |
} |
|
222 |
}); |
|
223 |
|
|
224 |
setupButton(R.id.view_server_rebuild_button, new OnClickListener() { |
|
225 |
public void onClick(View v) { |
|
226 |
showDialog(R.id.view_server_rebuild_button); |
|
227 |
} |
|
228 |
}); |
|
229 |
|
|
230 |
|
|
231 |
setupButton(R.id.view_server_backup_button, new OnClickListener() { |
|
232 |
public void onClick(View v) { |
|
233 |
Intent viewIntent = new Intent(v.getContext(), BackupServerActivity.class); |
|
234 |
viewIntent.putExtra("server", server); |
|
235 |
startActivity(viewIntent); |
|
236 |
} |
|
237 |
}); |
|
238 |
|
|
239 |
setupButton(R.id.view_server_password_button, new OnClickListener() { |
|
240 |
public void onClick(View v) { |
|
241 |
Intent viewIntent = new Intent(v.getContext(), PasswordServerActivity.class); |
|
242 |
viewIntent.putExtra("server", server); |
|
243 |
startActivity(viewIntent); |
|
244 |
} |
|
245 |
}); |
|
246 |
|
|
247 |
setupButton(R.id.view_server_ping_button, new OnClickListener() { |
|
248 |
public void onClick(View v) { |
|
249 |
Intent viewIntent = new Intent(v.getContext(), PingServerActivity.class); |
|
250 |
|
|
251 |
//ping the first public ip |
|
252 |
viewIntent.putExtra("ipAddress", server.getPublicIpAddresses()[0]); |
|
253 |
startActivity(viewIntent); |
|
254 |
} |
|
255 |
}); |
|
256 |
|
|
194 | 257 |
} |
195 | 258 |
|
196 | 259 |
private void showAlert(String title, String message) { |
... | ... | |
282 | 345 |
// User clicked Cancel so do some stuff |
283 | 346 |
} |
284 | 347 |
}) |
285 |
.create(); |
|
348 |
.create(); |
|
349 |
case R.id.view_server_rename_button: |
|
350 |
final EditText input = new EditText(this); |
|
351 |
input.setText(server.getName()); |
|
352 |
return new AlertDialog.Builder(ViewServerActivity.this) |
|
353 |
.setIcon(R.drawable.alert_dialog_icon) |
|
354 |
.setView(input) |
|
355 |
.setTitle("Rename") |
|
356 |
.setMessage("Enter new name for server: ") |
|
357 |
.setPositiveButton("Rename", new DialogInterface.OnClickListener() { |
|
358 |
public void onClick(DialogInterface dialog, int whichButton) { |
|
359 |
// User clicked OK so do some stuff |
|
360 |
modifiedServerName = input.getText().toString(); |
|
361 |
new RenameServerTask().execute((Void[]) null); |
|
362 |
} |
|
363 |
}) |
|
364 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
|
365 |
public void onClick(DialogInterface dialog, int whichButton) { |
|
366 |
// User clicked Cancel so do some stuff |
|
367 |
} |
|
368 |
}) |
|
369 |
.create(); |
|
370 |
case R.id.view_server_rebuild_button: |
|
371 |
return new AlertDialog.Builder(ViewServerActivity.this) |
|
372 |
.setItems(imageNames, new RebuildClickListener()) |
|
373 |
.setIcon(R.drawable.alert_dialog_icon) |
|
374 |
.setTitle("Rebuild Server") |
|
375 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
|
376 |
public void onClick(DialogInterface dialog, int whichButton) { |
|
377 |
// User clicked Cancel so do some stuff |
|
378 |
} |
|
379 |
}) |
|
380 |
.create(); |
|
286 | 381 |
} |
287 | 382 |
return null; |
288 | 383 |
} |
... | ... | |
296 | 391 |
|
297 | 392 |
} |
298 | 393 |
|
394 |
private class RebuildClickListener implements android.content.DialogInterface.OnClickListener { |
|
395 |
|
|
396 |
public void onClick(DialogInterface dialog, int which) { |
|
397 |
selectedImageId = images[which].getId() + ""; |
|
398 |
new RebuildServerTask().execute((Void[]) null); |
|
399 |
} |
|
400 |
|
|
401 |
} |
|
402 |
|
|
403 |
|
|
404 |
|
|
299 | 405 |
private CloudServersException parseCloudServersException(HttpResponse response) { |
300 | 406 |
CloudServersException cse = new CloudServersException(); |
301 | 407 |
try { |
... | ... | |
456 | 562 |
} |
457 | 563 |
} |
458 | 564 |
|
459 |
private class DeleteServerTask extends AsyncTask<Void, Void, HttpResponse> {
|
|
565 |
public class DeleteServerTask extends AsyncTask<Void, Void, HttpResponse> {
|
|
460 | 566 |
|
461 | 567 |
private CloudServersException exception; |
462 | 568 |
|
... | ... | |
491 | 597 |
} |
492 | 598 |
} |
493 | 599 |
} |
600 |
|
|
601 |
private class RenameServerTask extends AsyncTask<Void, Void, HttpResponse> { |
|
602 |
|
|
603 |
private CloudServersException exception; |
|
604 |
|
|
605 |
@Override |
|
606 |
protected HttpResponse doInBackground(Void... arg0) { |
|
607 |
HttpResponse resp = null; |
|
608 |
try { |
|
609 |
resp = (new ServerManager()).rename(server, modifiedServerName); |
|
610 |
} catch (CloudServersException e) { |
|
611 |
exception = e; |
|
612 |
} |
|
613 |
return resp; |
|
614 |
} |
|
615 |
|
|
616 |
@Override |
|
617 |
protected void onPostExecute(HttpResponse response) { |
|
618 |
if (response != null) { |
|
619 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
620 |
if (statusCode == 204) { |
|
621 |
new PollServerTask().execute((Void[]) null); |
|
622 |
} else { |
|
623 |
CloudServersException cse = parseCloudServersException(response); |
|
624 |
if ("".equals(cse.getMessage())) { |
|
625 |
showAlert("Error", "There was a problem renaming your server."); |
|
626 |
} else { |
|
627 |
showAlert("Error", "There was a problem renaming your server: " + cse.getMessage()); |
|
628 |
} |
|
629 |
} |
|
630 |
} |
|
631 |
else if (exception != null) { |
|
632 |
showAlert("Error", "There was a problem rebooting your server: " + exception.getMessage()); |
|
633 |
} |
|
634 |
} |
|
635 |
|
|
636 |
} |
|
637 |
|
|
638 |
private class RebuildServerTask extends AsyncTask<Void, Void, HttpResponse> { |
|
639 |
|
|
640 |
private CloudServersException exception; |
|
641 |
|
|
642 |
@Override |
|
643 |
protected HttpResponse doInBackground(Void... arg0) { |
|
644 |
HttpResponse resp = null; |
|
645 |
try { |
|
646 |
resp = (new ServerManager()).rebuild(server, Integer.parseInt(selectedImageId)); |
|
647 |
} catch (CloudServersException e) { |
|
648 |
exception = e; |
|
649 |
} |
|
650 |
return resp; |
|
651 |
} |
|
652 |
|
|
653 |
@Override |
|
654 |
protected void onPostExecute(HttpResponse response) { |
|
655 |
if (response != null) { |
|
656 |
int statusCode = response.getStatusLine().getStatusCode(); |
|
657 |
if (statusCode == 202) { |
|
658 |
new PollServerTask().execute((Void[]) null); |
|
659 |
} else { |
|
660 |
CloudServersException cse = parseCloudServersException(response); |
|
661 |
if ("".equals(cse.getMessage())) { |
|
662 |
showAlert("Error", "There was a problem deleting your server."); |
|
663 |
} else { |
|
664 |
showAlert("Error", "There was a problem deleting your server: " + cse.getMessage()); |
|
665 |
} |
|
666 |
} |
|
667 |
} else if (exception != null) { |
|
668 |
showAlert("Error", "There was a problem resizing your server: " + exception.getMessage()); |
|
669 |
|
|
670 |
} |
|
671 |
|
|
672 |
} |
|
673 |
} |
|
674 |
|
|
675 |
|
|
494 | 676 |
} |
Also available in: Unified diff