Revision af63e739 src/com/rackspacecloud/android/ViewServerActivity.java

b/src/com/rackspacecloud/android/ViewServerActivity.java
3 3
 */
4 4
package com.rackspacecloud.android;
5 5

  
6
import org.apache.http.HttpResponse;
7

  
6 8
import android.app.Activity;
7 9
import android.app.AlertDialog;
10
import android.app.Dialog;
8 11
import android.content.DialogInterface;
9 12
import android.graphics.Color;
10 13
import android.os.AsyncTask;
11 14
import android.os.Bundle;
12 15
import android.view.View;
16
import android.view.View.OnClickListener;
13 17
import android.widget.Button;
14
import android.widget.EditText;
15 18
import android.widget.ImageView;
16 19
import android.widget.LinearLayout;
17
import android.widget.ProgressBar;
18
import android.widget.Spinner;
19 20
import android.widget.TextView;
20 21

  
21
import com.rackspace.cloud.servers.api.client.Flavor;
22
import com.rackspace.cloud.servers.api.client.Image;
23 22
import com.rackspace.cloud.servers.api.client.Server;
24 23
import com.rackspace.cloud.servers.api.client.ServerManager;
25 24

  
......
31 30
	
32 31
	// TODO: handle rotation on all views
33 32
	
34
	private Image[] images;
35
	private Flavor[] flavors;
36
	private String selectedImageId;
37
	private String selectedFlavorId;
38
	private EditText serverName;
39
	private Spinner imageSpinner;
40
	private Spinner flavorSpinner;
41 33
	private Server server;
42 34
	
43 35
    /** Called when the activity is first created. */
......
49 41
        
50 42
        loadServerData();
51 43
        
44
        setupButtons();
45
        
52 46
        
53 47
        //serverName = (EditText) findViewById(R.id.server_name);
54 48
        //((Button) findViewById(R.id.save_button)).setOnClickListener(this);
......
99 93
        	layout.addView(tv, layoutIndex++);
100 94
    	}
101 95
    	
102
    	// actions
103
    	// Button softReboot = (Button) findViewById(R.id.view_server_soft_reboot_button);
104
    	
105 96
    	ImageView osLogo = (ImageView) findViewById(R.id.view_server_os_logo);
106 97
    	osLogo.setAlpha(100);
107 98
    	osLogo.setImageResource(server.getImage().logoResourceId());
108 99
    }
109 100
    
101
    private void setupButton(int resourceId, OnClickListener onClickListener) {
102
		Button button = (Button) findViewById(resourceId);
103
		button.setOnClickListener(onClickListener);
104
    }
105
    
106
    private void setupButtons() {
107
    	setupButton(R.id.view_server_soft_reboot_button, new OnClickListener() {
108
            public void onClick(View v) {
109
                showDialog(R.id.view_server_soft_reboot_button);
110
            }
111
        });
112
    	
113
    	setupButton(R.id.view_server_hard_reboot_button, new OnClickListener() {
114
            public void onClick(View v) {
115
                showDialog(R.id.view_server_hard_reboot_button);
116
            }
117
    	});
118

  
119
    	setupButton(R.id.view_server_change_password_button, new OnClickListener() {
120
            public void onClick(View v) {
121
                showDialog(R.id.view_server_change_password_button);
122
            }
123
    	});
124

  
125
    	setupButton(R.id.view_server_delete_button, new OnClickListener() {
126
            public void onClick(View v) {
127
                showDialog(R.id.view_server_delete_button);
128
            }
129
    	});
130
    	
131
    }
132
    
110 133
    /*
111 134
	@Override
112 135
	public void onClick(View arg0) {
......
136 159
		alert.show();
137 160
    }
138 161
	
139
    private void setActivityIndicatorsVisibility(int visibility) {
140
        ProgressBar pb = (ProgressBar) findViewById(R.id.save_server_progress_bar);
141
    	TextView tv = (TextView) findViewById(R.id.saving_server_label);
142
        pb.setVisibility(visibility);
143
        tv.setVisibility(visibility);
144
    }
145

  
146
    private void showActivityIndicators() {
147
    	setActivityIndicatorsVisibility(View.VISIBLE);
148
    }
149
    
150
    private void hideActivityIndicators() {
151
    	setActivityIndicatorsVisibility(View.INVISIBLE);
152
    }
153
        
154
    
155 162
    /**
156 163
	 * @return the server
157 164
	 */
......
166 173
		this.server = server;
167 174
	}
168 175

  
176
    @Override
177
    protected Dialog onCreateDialog(int id) {
178
        switch (id) {
179
        case R.id.view_server_soft_reboot_button:
180
            return new AlertDialog.Builder(ViewServerActivity.this)
181
        	.setIcon(R.drawable.alert_dialog_icon)
182
        	.setTitle("Soft Reboot")
183
        	.setMessage("Are you sure you want to perform a soft reboot?")
184
        	.setPositiveButton("Reboot Server", new DialogInterface.OnClickListener() {
185
        		public void onClick(DialogInterface dialog, int whichButton) {
186
        			// User clicked OK so do some stuff
187
        			new SoftRebootServerTask().execute((Void[]) null);
188
        		}
189
        	})
190
        	.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
191
        		public void onClick(DialogInterface dialog, int whichButton) {
192
        			// User clicked Cancel so do some stuff
193
        		}
194
        	})
195
        	.create();
196
        case R.id.view_server_hard_reboot_button:
197
            return new AlertDialog.Builder(ViewServerActivity.this)
198
        	.setIcon(R.drawable.alert_dialog_icon)
199
        	.setTitle("Hard Reboot")
200
        	.setMessage("Are you sure you want to perform a hard reboot?")
201
        	.setPositiveButton("Reboot Server", new DialogInterface.OnClickListener() {
202
        		public void onClick(DialogInterface dialog, int whichButton) {
203
        			// User clicked OK so do some stuff
204
        			new HardRebootServerTask().execute((Void[]) null);
205
        		}
206
        	})
207
        	.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
208
        		public void onClick(DialogInterface dialog, int whichButton) {
209
        			// User clicked Cancel so do some stuff
210
        		}
211
        	})
212
        	.create();
213
        case R.id.view_server_change_password_button:
214
            return new AlertDialog.Builder(ViewServerActivity.this)
215
        	.setIcon(R.drawable.alert_dialog_icon)
216
        	.setTitle("Change Password")
217
        	.setMessage("Are you sure you want to perform a hard reboot?")
218
        	.setPositiveButton("Reboot Server", new DialogInterface.OnClickListener() {
219
        		public void onClick(DialogInterface dialog, int whichButton) {
220
        			// User clicked OK so do some stuff
221
        		}
222
        	})
223
        	.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
224
        		public void onClick(DialogInterface dialog, int whichButton) {
225
        			// User clicked Cancel so do some stuff
226
        		}
227
        	})
228
        	.create();
229
        case R.id.view_server_delete_button:
230
            return new AlertDialog.Builder(ViewServerActivity.this)
231
        	.setIcon(R.drawable.alert_dialog_icon)
232
        	.setTitle("Delete Server")
233
        	.setMessage("Are you sure you want to delete this server?  This operation cannot be undone and all backups will be deleted.")
234
        	.setPositiveButton("Delete Server", new DialogInterface.OnClickListener() {
235
        		public void onClick(DialogInterface dialog, int whichButton) {
236
        			// User clicked OK so do some stuff
237
        			new DeleteServerTask().execute((Void[]) null);
238
        		}
239
        	})
240
        	.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
241
        		public void onClick(DialogInterface dialog, int whichButton) {
242
        			// User clicked Cancel so do some stuff
243
        		}
244
        	})
245
        	.create();
246
        }
247
        return null;
248
    }
249

  
250
    // HTTP request tasks
251
    
252
	private class PollServerTask extends AsyncTask<Void, Void, HttpResponse> {
253
    	
254
		@Override
255
		protected HttpResponse doInBackground(Void... arg0) {
256
			return (new ServerManager()).reboot(server, ServerManager.SOFT_REBOOT);
257
		}
258
    	
259
		@Override
260
		protected void onPostExecute(HttpResponse response) {
261
			//setServerList(result);
262
			//this.
263
			//hideActivityIndicators();
264
			//response.getStatusLine().getStatusCode()
265
			System.out.println("done");
266
			
267
			int statusCode = response.getStatusLine().getStatusCode();
268
			
269
			if (statusCode == 202) {
270
				// all good
271
			} else {
272
				// TODO: friendlier error handling
273
				showAlert("Error", "There was a problem rebooting your server: " + response.getStatusLine());
274
			}
275
			
276
		}
277
    }
278

  
279
    
280
	private class SoftRebootServerTask extends AsyncTask<Void, Void, HttpResponse> {
281
    	
282
		@Override
283
		protected HttpResponse doInBackground(Void... arg0) {
284
			return (new ServerManager()).reboot(server, ServerManager.SOFT_REBOOT);
285
		}
286
    	
287
		@Override
288
		protected void onPostExecute(HttpResponse response) {
289
			//setServerList(result);
290
			//this.
291
			//hideActivityIndicators();
292
			//response.getStatusLine().getStatusCode()
293
			System.out.println("done");
294
			
295
			int statusCode = response.getStatusLine().getStatusCode();
296
			
297
			if (statusCode == 202) {
298
				// all good
299
			} else {
300
				// TODO: friendlier error handling
301
				showAlert("Error", "There was a problem rebooting your server: " + response.getStatusLine());
302
			}
303
			
304
		}
305
    }
306

  
307
	private class HardRebootServerTask extends AsyncTask<Void, Void, HttpResponse> {
308
    	
309
		@Override
310
		protected HttpResponse doInBackground(Void... arg0) {
311
			return (new ServerManager()).reboot(server, ServerManager.HARD_REBOOT);
312
		}
313
    	
314
		@Override
315
		protected void onPostExecute(HttpResponse response) {
316
			//setServerList(result);
317
			//this.
318
			//hideActivityIndicators();
319
			//response.getStatusLine().getStatusCode()
320
			System.out.println("done");
321
			
322
			int statusCode = response.getStatusLine().getStatusCode();
323
			
324
			if (statusCode == 202) {
325
				// all good
326
			} else {
327
				// TODO: friendlier error handling
328
				showAlert("Error", "There was a problem rebooting your server: " + response.getStatusLine());
329
			}
330
			
331
		}
332
    }
169 333

  
170
	private class SaveServerTask extends AsyncTask<Void, Void, Server> {
334
	private class DeleteServerTask extends AsyncTask<Void, Void, HttpResponse> {
171 335
    	
172 336
		@Override
173
		protected Server doInBackground(Void... arg0) {
174
			(new ServerManager()).create(server);
175
			return server;
337
		protected HttpResponse doInBackground(Void... arg0) {
338
			return (new ServerManager()).delete(server);
176 339
		}
177 340
    	
178 341
		@Override
179
		protected void onPostExecute(Server result) {
342
		protected void onPostExecute(HttpResponse response) {
180 343
			//setServerList(result);
181 344
			//this.
182
			hideActivityIndicators();
345
			//hideActivityIndicators();
346
			//response.getStatusLine().getStatusCode()
183 347
			System.out.println("done");
348
			
349
			int statusCode = response.getStatusLine().getStatusCode();
350
			
351
			if (statusCode == 202) {
352
				setResult(Activity.RESULT_OK);
353
				finish();
354
			} else {
355
				// TODO: friendlier error handling
356
				showAlert("Error", "There was a problem deleting your server: " + response.getStatusLine());
357
			}
358
			
184 359
		}
185 360
    }
186 361
}

Also available in: Unified diff