Revision 038ac9a4 src/com/rackspacecloud/android/AddFileActivity.java
b/src/com/rackspacecloud/android/AddFileActivity.java | ||
---|---|---|
33 | 33 |
import android.widget.Button; |
34 | 34 |
import android.widget.EditText; |
35 | 35 |
|
36 |
public class AddFileActivity extends GaActivity implements OnClickListener{ |
|
37 |
|
|
38 |
private Context context;
|
|
36 |
public class AddFileActivity extends GaActivity implements OnClickListener {
|
|
37 |
|
|
38 |
private Context context; |
|
39 | 39 |
private EditText fileName; |
40 | 40 |
private EditText contents; |
41 | 41 |
private String containerName; |
... | ... | |
44 | 44 |
private ProgressDialog dialog; |
45 | 45 |
|
46 | 46 |
/** Called when the activity is first created. */ |
47 |
@Override |
|
48 |
public void onCreate(Bundle savedInstanceState) { |
|
49 |
super.onCreate(savedInstanceState); |
|
50 |
trackPageView(PAGE_ADD_OBJECT); |
|
51 |
setContentView(R.layout.addtextfile); |
|
52 |
context = getApplicationContext(); |
|
53 |
containerName = (String) this.getIntent().getExtras().get("Cname"); |
|
54 |
path = (String) this.getIntent().getExtras().get("curPath"); |
|
55 |
setUpDialog(savedInstanceState); |
|
56 |
setUpInputs(); |
|
57 |
} |
|
58 |
|
|
59 |
private void setUpInputs(){ |
|
60 |
((Button) findViewById(R.id.new_file_button)).setOnClickListener(this); |
|
61 |
fileName = ((EditText)findViewById(R.id.file_name_text)); |
|
62 |
fileName.append(".txt"); |
|
63 |
contents = ((EditText)findViewById(R.id.new_file_text)); |
|
64 |
} |
|
65 |
|
|
66 |
private void setUpDialog(Bundle savedInstanceState){ |
|
67 |
isAdding = savedInstanceState != null && savedInstanceState.containsKey("isAdding") |
|
68 |
&& savedInstanceState.getBoolean("isAdding"); |
|
69 |
if(isAdding){ |
|
70 |
showDialog(); |
|
71 |
} |
|
72 |
|
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
47 |
@Override |
|
48 |
public void onCreate(Bundle savedInstanceState) { |
|
49 |
super.onCreate(savedInstanceState); |
|
50 |
trackPageView(PAGE_ADD_OBJECT); |
|
51 |
setContentView(R.layout.addtextfile); |
|
52 |
context = getApplicationContext(); |
|
53 |
containerName = (String) this.getIntent().getExtras().get("Cname"); |
|
54 |
path = (String) this.getIntent().getExtras().get("curPath"); |
|
55 |
setUpDialog(savedInstanceState); |
|
56 |
setUpInputs(); |
|
57 |
} |
|
58 |
|
|
59 |
private void setUpInputs() { |
|
60 |
((Button) findViewById(R.id.new_file_button)).setOnClickListener(this); |
|
61 |
fileName = ((EditText) findViewById(R.id.file_name_text)); |
|
62 |
fileName.append(".txt"); |
|
63 |
contents = ((EditText) findViewById(R.id.new_file_text)); |
|
64 |
} |
|
65 |
|
|
66 |
private void setUpDialog(Bundle savedInstanceState) { |
|
67 |
isAdding = savedInstanceState != null |
|
68 |
&& savedInstanceState.containsKey("isAdding") |
|
69 |
&& savedInstanceState.getBoolean("isAdding"); |
|
70 |
if (isAdding) { |
|
71 |
showDialog(); |
|
72 |
} |
|
73 |
|
|
74 |
} |
|
75 |
|
|
76 |
@Override |
|
76 | 77 |
protected void onSaveInstanceState(Bundle outState) { |
77 | 78 |
super.onSaveInstanceState(outState); |
78 | 79 |
outState.putBoolean("isAdding", isAdding); |
79 |
if(isAdding){
|
|
80 |
if (isAdding) {
|
|
80 | 81 |
hideDialog(); |
81 | 82 |
} |
82 | 83 |
} |
83 |
|
|
84 |
public void onClick(View arg0) {
|
|
84 |
|
|
85 |
public void onClick(View arg0) {
|
|
85 | 86 |
if ("".equals(fileName.getText().toString())) { |
86 | 87 |
showAlert("Required Fields Missing", " File name is required."); |
87 | 88 |
} else { |
88 |
//showActivityIndicators(); |
|
89 |
// showActivityIndicators();
|
|
89 | 90 |
trackEvent(CATEGORY_FILE, EVENT_CREATE, "", -1); |
90 | 91 |
new SaveFileTask().execute((Void[]) null); |
91 | 92 |
} |
92 | 93 |
} |
93 |
|
|
94 |
//using cloudServersException, it works for us too |
|
95 |
private CloudServersException parseCloudServersException(HttpResponse response) { |
|
94 |
|
|
95 |
// using cloudServersException, it works for us too |
|
96 |
private CloudServersException parseCloudServersException( |
|
97 |
HttpResponse response) { |
|
96 | 98 |
CloudServersException cse = new CloudServersException(); |
97 | 99 |
try { |
98 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
|
99 |
String body = responseHandler.handleResponse(response);
|
|
100 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
|
101 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
|
|
102 |
XMLReader xmlReader = saxParser.getXMLReader();
|
|
103 |
xmlReader.setContentHandler(parser);
|
|
104 |
xmlReader.parse(new InputSource(new StringReader(body)));
|
|
105 |
cse = parser.getException();
|
|
100 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
|
101 |
String body = responseHandler.handleResponse(response);
|
|
102 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
|
103 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
|
|
104 |
XMLReader xmlReader = saxParser.getXMLReader();
|
|
105 |
xmlReader.setContentHandler(parser);
|
|
106 |
xmlReader.parse(new InputSource(new StringReader(body)));
|
|
107 |
cse = parser.getException();
|
|
106 | 108 |
} catch (ClientProtocolException e) { |
107 | 109 |
cse = new CloudServersException(); |
108 | 110 |
cse.setMessage(e.getLocalizedMessage()); |
... | ... | |
121 | 123 |
} |
122 | 124 |
return cse; |
123 | 125 |
} |
124 |
|
|
126 |
|
|
125 | 127 |
private void showAlert(String title, String message) { |
126 |
try { |
|
127 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
|
128 |
alert.setTitle(title); |
|
129 |
alert.setMessage(message); |
|
130 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
|
131 |
public void onClick(DialogInterface dialog, int which) { |
|
132 |
return; |
|
133 |
} }); |
|
134 |
alert.show(); |
|
135 |
} catch (Exception e) { |
|
136 |
e.printStackTrace(); |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 |
private void startFileError(String message, HttpBundle bundle){ |
|
141 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class); |
|
128 |
try { |
|
129 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
|
130 |
alert.setTitle(title); |
|
131 |
alert.setMessage(message); |
|
132 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
|
133 |
public void onClick(DialogInterface dialog, int which) { |
|
134 |
return; |
|
135 |
} |
|
136 |
}); |
|
137 |
alert.show(); |
|
138 |
} catch (Exception e) { |
|
139 |
e.printStackTrace(); |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
private void startFileError(String message, HttpBundle bundle) { |
|
144 |
Intent viewIntent = new Intent(getApplicationContext(), |
|
145 |
ServerErrorActivity.class); |
|
142 | 146 |
viewIntent.putExtra("errorMessage", message); |
143 | 147 |
viewIntent.putExtra("response", bundle.getResponseText()); |
144 | 148 |
viewIntent.putExtra("request", bundle.getCurlRequest()); |
... | ... | |
146 | 150 |
} |
147 | 151 |
|
148 | 152 |
private class SaveFileTask extends AsyncTask<Void, Void, HttpBundle> { |
149 |
private CloudServersException exception;
|
|
150 |
|
|
151 |
@Override
|
|
152 |
protected void onPreExecute(){ |
|
153 |
private CloudServersException exception;
|
|
154 |
|
|
155 |
@Override
|
|
156 |
protected void onPreExecute() {
|
|
153 | 157 |
isAdding = true; |
154 | 158 |
showDialog(); |
155 | 159 |
} |
156 |
|
|
157 |
@Override
|
|
160 |
|
|
161 |
@Override
|
|
158 | 162 |
protected HttpBundle doInBackground(Void... arg0) { |
159 |
HttpBundle bundle = null;
|
|
163 |
HttpBundle bundle = null;
|
|
160 | 164 |
try { |
161 |
bundle = (new ContainerObjectManager(context)).addObject(containerName, path, fileName.getText().toString(), "text/plain", contents.getText().toString()); |
|
165 |
bundle = (new ContainerObjectManager(context)).addObject( |
|
166 |
containerName, path, fileName.getText().toString(), |
|
167 |
"text/plain", contents.getText().toString()); |
|
162 | 168 |
} catch (CloudServersException e) { |
163 | 169 |
exception = e; |
164 | 170 |
} |
165 | 171 |
return bundle; |
166 | 172 |
} |
167 |
|
|
173 |
|
|
168 | 174 |
@Override |
169 | 175 |
protected void onPostExecute(HttpBundle bundle) { |
170 | 176 |
isAdding = false; |
... | ... | |
178 | 184 |
} else { |
179 | 185 |
CloudServersException cse = parseCloudServersException(response); |
180 | 186 |
if ("".equals(cse.getMessage())) { |
181 |
startFileError("There was a problem creating your file.", bundle); |
|
187 |
startFileError( |
|
188 |
"There was a problem creating your file.", |
|
189 |
bundle); |
|
182 | 190 |
} else { |
183 |
startFileError("There was a problem creating your file: " + cse.getMessage() + " Check file name and try again", bundle); |
|
191 |
startFileError( |
|
192 |
"There was a problem creating your file: " |
|
193 |
+ cse.getMessage() |
|
194 |
+ " Check file name and try again", |
|
195 |
bundle); |
|
184 | 196 |
} |
185 | 197 |
} |
186 | 198 |
} else if (exception != null) { |
187 |
startFileError("There was a problem creating your file: " + exception.getMessage()+" Check file name and try again", bundle); |
|
188 |
} |
|
199 |
startFileError("There was a problem creating your file: " |
|
200 |
+ exception.getMessage() |
|
201 |
+ " Check file name and try again", bundle); |
|
202 |
} |
|
189 | 203 |
} |
190 |
}
|
|
191 |
|
|
204 |
}
|
|
205 |
|
|
192 | 206 |
private void showDialog() { |
193 |
if(dialog == null || !dialog.isShowing()){ |
|
194 |
dialog = ProgressDialog.show(AddFileActivity.this, "", "Adding File...", true); |
|
207 |
if (dialog == null || !dialog.isShowing()) { |
|
208 |
dialog = ProgressDialog.show(AddFileActivity.this, "", |
|
209 |
"Adding File...", true); |
|
195 | 210 |
} |
196 |
}
|
|
197 |
|
|
198 |
private void hideDialog() {
|
|
199 |
if(dialog != null){
|
|
200 |
dialog.dismiss();
|
|
201 |
}
|
|
202 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
private void hideDialog() {
|
|
214 |
if (dialog != null) {
|
|
215 |
dialog.dismiss();
|
|
216 |
}
|
|
217 |
}
|
|
203 | 218 |
} |
Also available in: Unified diff