Revision 51fdecfb

b/AndroidManifest.xml
98 98
		<activity android:name=".AddFileActivity"></activity>
99 99
		<activity android:name=".ServerErrorActivity" android:theme="@android:style/Theme.Dialog" android:noHistory="true"></activity>
100 100
		<activity android:name=".ErrorDetailsActivity"></activity>
101
		<activity android:name=".ConfirmResizeActivity" android:theme="@android:style/Theme.Dialog"></activity>
101 102

  
102 103

  
103 104
	</application>
b/res/layout/viewresize.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout
3
  xmlns:android="http://schemas.android.com/apk/res/android"
4
  android:orientation="vertical"
5
  android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">
6
    <LinearLayout android:id="@+id/linearLayout3" android:layout_height="wrap_content" android:gravity="center" android:layout_width="wrap_content">
7
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:text="Resize Complete" android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
8
    </LinearLayout>
9
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:gravity="center" android:layout_marginLeft="22dip" android:layout_marginRight="22dip" android:layout_width="wrap_content">
10
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/confirm_resize" android:gravity="center"></TextView>
11
    </LinearLayout>
12
    <LinearLayout android:gravity="center" android:layout_marginTop="10dip" android:layout_marginRight="22dip" android:id="@+id/linearLayout2" android:layout_marginLeft="22dip" android:layout_height="wrap_content" android:layout_width="wrap_content">
13
        <Button android:layout_height="wrap_content" android:text="Confirm" android:layout_marginRight="10dip" android:layout_width="110dip" android:id="@+id/confirm_resize_button"></Button>
14
        <Button android:layout_height="wrap_content" android:layout_width="110dip" android:text="Rollback" android:id="@+id/rollback_server_button"></Button>
15
    </LinearLayout>
16
    
17
</LinearLayout>
b/res/menu/view_server_activity_menu.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<menu
3
  xmlns:android="http://schemas.android.com/apk/res/android">
4
    <item android:id="@+id/refresh_server" android:icon="@drawable/refresh_button" android:title="Refresh"></item>
5
    
6
</menu>
b/res/values/strings.xml
51 51
	if you have a user line for \"root\" in your passwd or shadow file.</string>
52 52
<string name="contact_uk">0800-083-3012</string>
53 53
<string name="contact_us">877-934-0407</string>
54
<string name="confirm_resize">Confirming the resize will destroy the saved copy of your original server.</string>
54 55

  
55 56
</resources>
b/src/com/rackspace/cloud/servers/api/client/ServerManager.java
305 305

  
306 306
		StringEntity tmp = null;
307 307
		try {
308
			tmp = new StringEntity("<confirmResize xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\">");
308
			tmp = new StringEntity("<confirmResize xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\"/>");
309 309
		} catch (UnsupportedEncodingException e) {
310 310
			CloudServersException cse = new CloudServersException();
311 311
			cse.setMessage(e.getLocalizedMessage());
......
335 335
		return bundle;
336 336
	}
337 337

  
338
	public HttpBundle revertResize(Server server, Context context) throws CloudServersException {
339
		HttpResponse resp = null;
340
		CustomHttpClient httpclient = new CustomHttpClient(context);
341
		HttpPost post = new HttpPost(Account.getAccount().getServerUrl() + "/servers/" + server.getId() + "/action.xml");			
342
		post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
343
		post.addHeader("Content-Type", "application/xml");
344
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
338 345

  
346
		StringEntity tmp = null;
347
		try {
348
			tmp = new StringEntity("<revertResize xmlns=\"http://docs.rackspacecloud.com/servers/api/v1.0\"/>");
349
		} catch (UnsupportedEncodingException e) {
350
			CloudServersException cse = new CloudServersException();
351
			cse.setMessage(e.getLocalizedMessage());
352
			throw cse;
353
		}
354
		post.setEntity(tmp);
355
		
356
		HttpBundle bundle = new HttpBundle();
357
		bundle.setCurlRequest(post);
358

  
359
		try {			
360
			resp = httpclient.execute(post);
361
			bundle.setHttpResponse(resp);
362
		} catch (ClientProtocolException e) {
363
			CloudServersException cse = new CloudServersException();
364
			cse.setMessage(e.getLocalizedMessage());
365
			throw cse;
366
		} catch (IOException e) {
367
			CloudServersException cse = new CloudServersException();
368
			cse.setMessage(e.getLocalizedMessage());
369
			throw cse;
370
		} catch (FactoryConfigurationError e) {
371
			CloudServersException cse = new CloudServersException();
372
			cse.setMessage(e.getLocalizedMessage());
373
			throw cse;
374
		}	
375
		return bundle;
376
	}
377
	
339 378
	public HttpBundle delete(Server server, Context context) throws CloudServersException {
340 379
		HttpResponse resp = null;
341 380
		CustomHttpClient httpclient = new CustomHttpClient(context);
b/src/com/rackspacecloud/android/ConfirmResizeActivity.java
1
package com.rackspacecloud.android;
2

  
3
import java.io.IOException;
4
import java.io.StringReader;
5

  
6
import javax.xml.parsers.FactoryConfigurationError;
7
import javax.xml.parsers.ParserConfigurationException;
8
import javax.xml.parsers.SAXParser;
9
import javax.xml.parsers.SAXParserFactory;
10

  
11
import org.apache.http.HttpResponse;
12
import org.apache.http.client.ClientProtocolException;
13
import org.apache.http.impl.client.BasicResponseHandler;
14
import org.xml.sax.InputSource;
15
import org.xml.sax.SAXException;
16
import org.xml.sax.XMLReader;
17

  
18
import com.rackspace.cloud.servers.api.client.CloudServersException;
19
import com.rackspace.cloud.servers.api.client.Server;
20
import com.rackspace.cloud.servers.api.client.ServerManager;
21
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
22
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
23

  
24
import android.app.Activity;
25
import android.content.Context;
26
import android.content.Intent;
27
import android.os.AsyncTask;
28
import android.os.Bundle;
29
import android.view.View;
30
import android.view.View.OnClickListener;
31
import android.view.Window;
32
import android.widget.Button;
33
import android.widget.Toast;
34

  
35
public class ConfirmResizeActivity extends Activity {
36

  
37
	private Context context;
38
	private Server server;
39
	
40
	/** Called when the activity is first created. */
41
	@Override
42
	public void onCreate(Bundle savedInstanceState) {
43
		super.onCreate(savedInstanceState);
44
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
45
		setContentView(R.layout.viewresize);     
46
		server = (Server) this.getIntent().getExtras().get("server");
47
		context = getApplicationContext();
48
		restoreState(savedInstanceState);
49
	}
50
	
51
	@Override
52
	protected void onSaveInstanceState(Bundle outState) {
53
		super.onSaveInstanceState(outState);
54
		outState.putSerializable("server", server);
55
	}
56
	
57
	private void restoreState(Bundle state) {
58
		if (server == null && state != null && state.containsKey("server")) {
59
			server = (Server) state.getSerializable("server");
60
		}
61
		setupButtons();
62
	}
63

  
64
	private void setupButtons(){
65
		Button confirm = (Button)findViewById(R.id.confirm_resize_button);
66
		confirm.setOnClickListener(new OnClickListener() {
67

  
68
			@Override
69
			public void onClick(View v) {
70
				new ConfirmResizeTask().execute((Void[]) null);
71
				finish();
72
			}
73
		});
74

  
75
		Button rollback = (Button)findViewById(R.id.rollback_server_button);
76
		rollback.setOnClickListener(new OnClickListener() {
77

  
78
			@Override
79
			public void onClick(View v) {
80
				new RollbackResizeTask().execute((Void[]) null);	
81
				finish();
82
			}
83
		});
84
	}
85
	
86
	private CloudServersException parseCloudServersException(HttpResponse response) {
87
		CloudServersException cse = new CloudServersException();
88
		try {
89
			BasicResponseHandler responseHandler = new BasicResponseHandler();
90
			String body = responseHandler.handleResponse(response);
91
			CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
92
			SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
93
			XMLReader xmlReader = saxParser.getXMLReader();
94
			xmlReader.setContentHandler(parser);
95
			xmlReader.parse(new InputSource(new StringReader(body)));		    	
96
			cse = parser.getException();		    	
97
		} catch (ClientProtocolException e) {
98
			cse = new CloudServersException();
99
			cse.setMessage(e.getLocalizedMessage());
100
		} catch (IOException e) {
101
			cse = new CloudServersException();
102
			cse.setMessage(e.getLocalizedMessage());
103
		} catch (ParserConfigurationException e) {
104
			cse = new CloudServersException();
105
			cse.setMessage(e.getLocalizedMessage());
106
		} catch (SAXException e) {
107
			cse = new CloudServersException();
108
			cse.setMessage(e.getLocalizedMessage());
109
		} catch (FactoryConfigurationError e) {
110
			cse = new CloudServersException();
111
			cse.setMessage(e.getLocalizedMessage());
112
		}
113
		return cse;
114
	}
115
	
116
	private void startServerError(String message, HttpBundle bundle){
117
		Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
118
		viewIntent.putExtra("errorMessage", message);
119
		viewIntent.putExtra("response", bundle.getResponseText());
120
		viewIntent.putExtra("request", bundle.getCurlRequest());
121
		startActivity(viewIntent);
122
	}
123
	
124
	private void showToast(String message) {
125
		Context context = getApplicationContext();
126
		int duration = Toast.LENGTH_SHORT;
127
		Toast toast = Toast.makeText(context, message, duration);
128
		toast.show();
129
	}
130
	
131
	private class ConfirmResizeTask extends AsyncTask<Void, Void, HttpBundle> {
132

  
133
		private CloudServersException exception;
134

  
135
		@Override
136
		//let user know their process has started
137
		protected void onPreExecute(){
138
			showToast("Confirm process has begun");
139
		}
140

  
141
		@Override
142
		protected HttpBundle doInBackground(Void... arg0) {
143
			HttpBundle bundle = null;
144
			try {
145
				bundle = (new ServerManager()).confirmResize(server, context);
146
			} catch (CloudServersException e) {
147
				exception = e;
148
			}
149
			return bundle;
150
		}
151

  
152
		@Override
153
		protected void onPostExecute(HttpBundle bundle) {
154
			HttpResponse response = bundle.getResponse();
155
			if (response != null) {
156
				int statusCode = response.getStatusLine().getStatusCode();	
157
				if(statusCode == 204){ showToast("Server resize was successfully confirmed."); }
158
				else {
159
					CloudServersException cse = parseCloudServersException(response);
160
					if ("".equals(cse.getMessage())) {
161
						startServerError("There was a problem confirming your resize.", bundle);
162
					} else {
163
						startServerError("There was a problem confirming your resize." + cse.getMessage(), bundle);
164
					}
165
				}
166
			} else if (exception != null) {
167
				startServerError("There was a problem confirming your resize." + exception.getMessage(), bundle);
168

  
169
			}
170
		}
171
	}
172
	
173
	
174
	private class RollbackResizeTask extends AsyncTask<Void, Void, HttpBundle> {
175

  
176
		private CloudServersException exception;
177

  
178
		@Override
179
		//let user know their process has started
180
		protected void onPreExecute(){
181
			showToast("Reverting your server.");
182
		}
183

  
184
		@Override
185
		protected HttpBundle doInBackground(Void... arg0) {
186
			HttpBundle bundle = null;
187
			try {
188
				bundle = (new ServerManager()).revertResize(server, context);
189
			} catch (CloudServersException e) {
190
				exception = e;
191
			}
192
			return bundle;
193
		}
194

  
195
		@Override
196
		protected void onPostExecute(HttpBundle bundle) {
197
			HttpResponse response = bundle.getResponse();
198
			if (response != null) {
199
				int statusCode = response.getStatusLine().getStatusCode();	
200
				if(statusCode == 202){ showToast("Server was successfully reverted."); }
201
				else {
202
					CloudServersException cse = parseCloudServersException(response);
203
					if ("".equals(cse.getMessage())) {
204
						startServerError("There was a problem reverting your server.", bundle);
205
					} else {
206
						startServerError("There was a problem reverting your server." + cse.getMessage(), bundle);
207
					}
208
				}
209
			} else if (exception != null) {
210
				startServerError("There was a problem reverting your server." + exception.getMessage(), bundle);
211

  
212
			}
213
		}
214
	}
215
	
216
}
b/src/com/rackspacecloud/android/ViewServerActivity.java
30 30
import android.os.AsyncTask;
31 31
import android.os.Bundle;
32 32
import android.util.Log;
33
import android.view.Menu;
34
import android.view.MenuInflater;
35
import android.view.MenuItem;
33 36
import android.view.View;
34 37
import android.view.View.OnClickListener;
35 38
import android.widget.Button;
......
66 69
	private boolean isPolling;
67 70
	private PollServerTask pollServerTask;
68 71
	private boolean canPoll;
69

  
72
	private boolean noAskForConfirm;
73
	
70 74
	/** Called when the activity is first created. */
71 75
	@Override
72 76
	public void onCreate(Bundle savedInstanceState) {
......
81 85
	protected void onSaveInstanceState(Bundle outState) {
82 86
		super.onSaveInstanceState(outState);
83 87
		outState.putSerializable("server", server);
88
		outState.putBoolean("noAskForConfirm", noAskForConfirm);
84 89
		if(pollServerTask != null && isPolling){
85 90
			pollServerTask.cancel(true);
86 91
		}
......
88 93
	}
89 94

  
90 95
	private void restoreState(Bundle state) {
96
		if(state != null && state.containsKey("noAskForConfirm")){
97
			noAskForConfirm = state.getBoolean("noAskForConfirm");
98
		}
91 99
		if(state != null && state.containsKey("wasPolling") && state.getBoolean("wasPolling") == true){
92 100
			pollServerTask = new PollServerTask();
93 101
			pollServerTask.execute((Void[]) null);
......
162 170

  
163 171
		TextView status = (TextView) findViewById(R.id.view_server_status);
164 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
		
165 183
		// show status and possibly the progress, with polling
166 184
		if (!"ACTIVE".equals(server.getStatus())) {
167 185
			status.setText(server.getStatus() + " - " + server.getProgress() + "%");
......
331 349
	public void setServer(Server server) {
332 350
		this.server = server;
333 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
    } 
334 371

  
335 372
	private void startServerError(String message, HttpBundle bundle){
336 373
		Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
......
339 376
		viewIntent.putExtra("request", bundle.getCurlRequest());
340 377
		startActivity(viewIntent);
341 378
	}
342
	
343
	private String prints(Object[] arr){
344
		String result = "";
345
		for(int i = 0; i < arr.length; i++){
346
			result += arr[i] + " ";
347
		}
348
		return result;
349
	}
350 379

  
351 380
	@Override
352 381
	protected Dialog onCreateDialog(int id) {
......
399 428
				})
400 429
				.create();
401 430
			case R.id.view_server_resize_button:
402
				Log.d("info", "the flavor names are " + prints(flavorNames));
403 431
				return new AlertDialog.Builder(ViewServerActivity.this)
404 432
				.setItems(flavorNames, new ResizeClickListener())
405 433
				.setIcon(R.drawable.alert_dialog_icon)

Also available in: Unified diff