Revision 6b8dad86 src/com/rackspacecloud/android/AddContainerActivity.java
b/src/com/rackspacecloud/android/AddContainerActivity.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 android.app.Activity; |
19 |
import android.app.AlertDialog; |
|
20 |
import android.content.Context; |
|
21 |
import android.content.DialogInterface; |
|
22 |
import android.content.Intent; |
|
23 | 6 |
import android.os.AsyncTask; |
24 | 7 |
import android.os.Bundle; |
25 | 8 |
import android.view.View; |
26 | 9 |
import android.view.View.OnClickListener; |
27 | 10 |
import android.widget.Button; |
28 | 11 |
import android.widget.EditText; |
29 |
import android.widget.ProgressBar; |
|
30 |
import android.widget.TextView; |
|
31 |
import android.widget.Toast; |
|
32 | 12 |
|
33 | 13 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
34 | 14 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
35 | 15 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
36 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
|
37 | 16 |
/** |
38 | 17 |
* |
39 | 18 |
* @author Phillip Toohill |
40 | 19 |
* |
41 | 20 |
*/ |
42 |
public class AddContainerActivity extends GaActivity implements OnClickListener {
|
|
21 |
public class AddContainerActivity extends CloudActivity implements OnClickListener {
|
|
43 | 22 |
|
44 |
private EditText containerName; |
|
45 |
private Context context; |
|
46 |
private boolean isSaving; |
|
23 |
private EditText containerName; |
|
47 | 24 |
|
48 | 25 |
/** Called when the activity is first created. */ |
49 | 26 |
@Override |
50 | 27 |
public void onCreate(Bundle savedInstanceState) { |
51 | 28 |
super.onCreate(savedInstanceState); |
52 | 29 |
trackPageView(PAGE_ADD_CONTAINER); |
53 |
context = getApplicationContext(); |
|
54 | 30 |
setContentView(R.layout.createcontainer); |
55 |
containerName = (EditText) findViewById(R.id.container_name); |
|
56 |
((Button) findViewById(R.id.save_button)).setOnClickListener(this); |
|
57 |
isSaving = savedInstanceState != null && savedInstanceState.containsKey("isSaving") |
|
58 |
&& savedInstanceState.getBoolean("isSaving"); |
|
59 |
if(isSaving){ |
|
60 |
showActivityIndicators(); |
|
61 |
} |
|
31 |
restoreState(savedInstanceState); |
|
62 | 32 |
} |
63 | 33 |
|
64 |
@Override |
|
65 |
protected void onSaveInstanceState(Bundle outState){ |
|
66 |
super.onSaveInstanceState(outState); |
|
67 |
outState.putBoolean("isSaving", isSaving); |
|
68 |
|
|
34 |
protected void restoreState(Bundle state){ |
|
35 |
super.restoreState(state); |
|
36 |
containerName = (EditText) findViewById(R.id.container_name); |
|
37 |
((Button) findViewById(R.id.save_button)).setOnClickListener(this); |
|
69 | 38 |
} |
70 | 39 |
|
71 | 40 |
public void onClick(View arg0) { |
72 | 41 |
if ("".equals(containerName.getText().toString())) { |
73 | 42 |
showAlert("Required Fields Missing", " Container name is required."); |
74 | 43 |
} else { |
75 |
showActivityIndicators(); |
|
76 | 44 |
trackEvent(CATEGORY_CONTAINER, EVENT_CREATE, "", -1); |
77 | 45 |
new CreateContainerTask().execute((Void[]) null); |
78 | 46 |
} |
79 | 47 |
} |
80 |
|
|
81 |
|
|
82 |
private void showAlert(String title, String message) { |
|
83 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
|
84 |
alert.setTitle(title); |
|
85 |
alert.setMessage(message); |
|
86 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
|
87 |
public void onClick(DialogInterface dialog, int which) { |
|
88 |
return; |
|
89 |
} }); |
|
90 |
alert.show(); |
|
91 |
hideActivityIndicators(); |
|
92 |
} |
|
93 |
|
|
94 |
private void showToast(String message) { |
|
95 |
Context context = getApplicationContext(); |
|
96 |
int duration = Toast.LENGTH_SHORT; |
|
97 |
Toast toast = Toast.makeText(context, message, duration); |
|
98 |
toast.show(); |
|
99 |
} |
|
100 |
|
|
101 |
private void setActivityIndicatorsVisibility(int visibility) { |
|
102 |
ProgressBar pb = (ProgressBar) findViewById(R.id.save_container_progress_bar); |
|
103 |
TextView tv = (TextView) findViewById(R.id.saving_container_label); |
|
104 |
pb.setVisibility(visibility); |
|
105 |
tv.setVisibility(visibility); |
|
106 |
} |
|
107 |
|
|
108 |
private void showActivityIndicators() { |
|
109 |
isSaving = true; |
|
110 |
setActivityIndicatorsVisibility(View.VISIBLE); |
|
111 |
} |
|
112 |
|
|
113 |
private void hideActivityIndicators() { |
|
114 |
isSaving = false; |
|
115 |
setActivityIndicatorsVisibility(View.INVISIBLE); |
|
116 |
} |
|
117 |
//using cloudServersException, it works for us too |
|
118 |
private CloudServersException parseCloudServersException(HttpResponse response) { |
|
119 |
CloudServersException cse = new CloudServersException(); |
|
120 |
try { |
|
121 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
|
122 |
String body = responseHandler.handleResponse(response); |
|
123 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser(); |
|
124 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
|
125 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
126 |
xmlReader.setContentHandler(parser); |
|
127 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
128 |
cse = parser.getException(); |
|
129 |
} catch (ClientProtocolException e) { |
|
130 |
cse = new CloudServersException(); |
|
131 |
cse.setMessage(e.getLocalizedMessage()); |
|
132 |
} catch (IOException e) { |
|
133 |
cse = new CloudServersException(); |
|
134 |
cse.setMessage(e.getLocalizedMessage()); |
|
135 |
} catch (ParserConfigurationException e) { |
|
136 |
cse = new CloudServersException(); |
|
137 |
cse.setMessage(e.getLocalizedMessage()); |
|
138 |
} catch (SAXException e) { |
|
139 |
cse = new CloudServersException(); |
|
140 |
cse.setMessage(e.getLocalizedMessage()); |
|
141 |
} catch (FactoryConfigurationError e) { |
|
142 |
cse = new CloudServersException(); |
|
143 |
cse.setMessage(e.getLocalizedMessage()); |
|
144 |
} |
|
145 |
return cse; |
|
146 |
} |
|
147 |
|
|
148 |
private void startFileError(String message, HttpBundle bundle){ |
|
149 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class); |
|
150 |
viewIntent.putExtra("errorMessage", message); |
|
151 |
viewIntent.putExtra("response", bundle.getResponseText()); |
|
152 |
viewIntent.putExtra("request", bundle.getCurlRequest()); |
|
153 |
startActivity(viewIntent); |
|
154 |
} |
|
155 |
|
|
48 |
|
|
156 | 49 |
private class CreateContainerTask extends AsyncTask<Void, Void, HttpBundle> { |
157 | 50 |
private CloudServersException exception; |
158 | 51 |
|
52 |
protected void onPreExecute(){ |
|
53 |
showDialog(); |
|
54 |
} |
|
55 |
|
|
159 | 56 |
@Override |
160 | 57 |
protected HttpBundle doInBackground(Void... arg0) { |
161 | 58 |
HttpBundle bundle = null; |
162 | 59 |
try { |
163 |
bundle = (new ContainerManager(context)).create(containerName.getText());
|
|
60 |
bundle = (new ContainerManager(getContext())).create(containerName.getText());
|
|
164 | 61 |
} catch (CloudServersException e) { |
165 | 62 |
exception = e; |
166 | 63 |
} |
... | ... | |
169 | 66 |
|
170 | 67 |
@Override |
171 | 68 |
protected void onPostExecute(HttpBundle bundle) { |
69 |
hideDialog(); |
|
172 | 70 |
HttpResponse response = bundle.getResponse(); |
173 | 71 |
if (response != null) { |
174 | 72 |
int statusCode = response.getStatusLine().getStatusCode(); |
... | ... | |
178 | 76 |
} else { |
179 | 77 |
CloudServersException cse = parseCloudServersException(response); |
180 | 78 |
if ("".equals(cse.getMessage())) { |
181 |
startFileError("There was a problem creating your container.", bundle);
|
|
79 |
showError("There was a problem creating your container.", bundle);
|
|
182 | 80 |
} else { |
183 |
//if container with same name already exists |
|
184 |
showToast("There was a problem creating your container: " + cse.getMessage() + " See details for more information."); |
|
81 |
showError("There was a problem creating your container: " + cse.getMessage() + " See details for more information.", bundle); |
|
185 | 82 |
} |
186 | 83 |
} |
187 | 84 |
} else if (exception != null) { |
188 |
startFileError("There was a problem creating your container: " + exception.getMessage()+" See details for more information.", bundle);
|
|
85 |
showError("There was a problem creating your container: " + exception.getMessage()+" See details for more information.", bundle);
|
|
189 | 86 |
} |
190 | 87 |
finish(); |
191 | 88 |
} |
Also available in: Unified diff