Revision 6b8dad86 src/com/rackspacecloud/android/ConfirmResizeActivity.java
b/src/com/rackspacecloud/android/ConfirmResizeActivity.java | ||
---|---|---|
1 | 1 |
package com.rackspacecloud.android; |
2 | 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 | 3 |
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 | 4 |
|
18 | 5 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
19 | 6 |
import com.rackspace.cloud.servers.api.client.Server; |
20 | 7 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
21 | 8 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
22 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
|
23 | 9 |
|
24 |
import android.app.Activity; |
|
25 |
import android.content.Context; |
|
26 |
import android.content.Intent; |
|
27 | 10 |
import android.os.AsyncTask; |
28 | 11 |
import android.os.Bundle; |
29 | 12 |
import android.view.View; |
30 | 13 |
import android.view.View.OnClickListener; |
31 | 14 |
import android.view.Window; |
32 | 15 |
import android.widget.Button; |
33 |
import android.widget.Toast; |
|
34 | 16 |
|
35 |
public class ConfirmResizeActivity extends Activity { |
|
17 |
public class ConfirmResizeActivity extends CloudActivity {
|
|
36 | 18 |
|
37 |
private Context context; |
|
38 | 19 |
private Server server; |
39 | 20 |
|
40 | 21 |
/** Called when the activity is first created. */ |
... | ... | |
44 | 25 |
requestWindowFeature(Window.FEATURE_NO_TITLE); |
45 | 26 |
setContentView(R.layout.viewresize); |
46 | 27 |
server = (Server) this.getIntent().getExtras().get("server"); |
47 |
context = getApplicationContext(); |
|
48 | 28 |
restoreState(savedInstanceState); |
49 | 29 |
} |
50 | 30 |
|
31 |
|
|
51 | 32 |
@Override |
52 | 33 |
protected void onSaveInstanceState(Bundle outState) { |
53 | 34 |
super.onSaveInstanceState(outState); |
54 | 35 |
outState.putSerializable("server", server); |
55 | 36 |
} |
56 | 37 |
|
57 |
private void restoreState(Bundle state) { |
|
38 |
protected void restoreState(Bundle state) { |
|
39 |
super.restoreState(state); |
|
58 | 40 |
if (server == null && state != null && state.containsKey("server")) { |
59 | 41 |
server = (Server) state.getSerializable("server"); |
60 | 42 |
} |
... | ... | |
83 | 65 |
}); |
84 | 66 |
} |
85 | 67 |
|
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 | 68 |
private class ConfirmResizeTask extends AsyncTask<Void, Void, HttpBundle> { |
132 | 69 |
|
133 | 70 |
private CloudServersException exception; |
... | ... | |
142 | 79 |
protected HttpBundle doInBackground(Void... arg0) { |
143 | 80 |
HttpBundle bundle = null; |
144 | 81 |
try { |
145 |
bundle = (new ServerManager()).confirmResize(server, context);
|
|
82 |
bundle = (new ServerManager()).confirmResize(server, getContext());
|
|
146 | 83 |
} catch (CloudServersException e) { |
147 | 84 |
exception = e; |
148 | 85 |
} |
... | ... | |
158 | 95 |
else { |
159 | 96 |
CloudServersException cse = parseCloudServersException(response); |
160 | 97 |
if ("".equals(cse.getMessage())) { |
161 |
startServerError("There was a problem confirming your resize.", bundle);
|
|
98 |
showError("There was a problem confirming your resize.", bundle);
|
|
162 | 99 |
} else { |
163 |
startServerError("There was a problem confirming your resize." + cse.getMessage(), bundle);
|
|
100 |
showError("There was a problem confirming your resize." + cse.getMessage(), bundle);
|
|
164 | 101 |
} |
165 | 102 |
} |
166 | 103 |
} else if (exception != null) { |
167 |
startServerError("There was a problem confirming your resize." + exception.getMessage(), bundle); |
|
168 |
|
|
104 |
showError("There was a problem confirming your resize." + exception.getMessage(), bundle); |
|
169 | 105 |
} |
170 | 106 |
} |
171 | 107 |
} |
... | ... | |
185 | 121 |
protected HttpBundle doInBackground(Void... arg0) { |
186 | 122 |
HttpBundle bundle = null; |
187 | 123 |
try { |
188 |
bundle = (new ServerManager()).revertResize(server, context);
|
|
124 |
bundle = (new ServerManager()).revertResize(server, getContext());
|
|
189 | 125 |
} catch (CloudServersException e) { |
190 | 126 |
exception = e; |
191 | 127 |
} |
... | ... | |
201 | 137 |
else { |
202 | 138 |
CloudServersException cse = parseCloudServersException(response); |
203 | 139 |
if ("".equals(cse.getMessage())) { |
204 |
startServerError("There was a problem reverting your server.", bundle);
|
|
140 |
showError("There was a problem reverting your server.", bundle);
|
|
205 | 141 |
} else { |
206 |
startServerError("There was a problem reverting your server." + cse.getMessage(), bundle);
|
|
142 |
showError("There was a problem reverting your server." + cse.getMessage(), bundle);
|
|
207 | 143 |
} |
208 | 144 |
} |
209 | 145 |
} else if (exception != null) { |
210 |
startServerError("There was a problem reverting your server." + exception.getMessage(), bundle);
|
|
146 |
showError("There was a problem reverting your server." + exception.getMessage(), bundle);
|
|
211 | 147 |
|
212 | 148 |
} |
213 | 149 |
} |
Also available in: Unified diff