Revision 378fe36a src/com/rackspace/cloud/android/CloudListActivity.java

b/src/com/rackspace/cloud/android/CloudListActivity.java
18 18

  
19 19
import android.app.AlertDialog;
20 20
import android.app.Dialog;
21
import android.app.ProgressDialog;
22 21
import android.content.Context;
23 22
import android.content.DialogInterface;
24 23
import android.content.DialogInterface.OnCancelListener;
25 24
import android.content.Intent;
25
import android.os.AsyncTask;
26 26
import android.os.Bundle;
27 27
import android.view.View;
28 28
import android.view.View.OnClickListener;
29
import android.view.ViewGroup.LayoutParams;
30 29
import android.view.WindowManager;
31 30
import android.widget.Button;
32 31
import android.widget.EditText;
33
import android.widget.ProgressBar;
34 32
import android.widget.Toast;
35 33

  
36
import com.rackspace.cloud.android.R;
37 34
import com.rackspace.cloud.servers.api.client.CloudServersException;
38 35
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
36
import com.rackspace.cloud.servers.api.client.http.HttpBundleNoNetwork;
39 37
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
40 38

  
41 39
/*
......
45 43
 * Also provides many accessory methods that are common
46 44
 * to Activities
47 45
 */
48
public class CloudListActivity extends GaListActivity{
46
public class CloudListActivity extends GaListActivity {
49 47

  
50 48
	private final int PASSWORD_PROMPT = 123;
51 49
	private Context context;
52 50
	private boolean isLoading;
53
	private ProgressDialog pDialog;
51
	private Dialog pDialog;
54 52
	protected AndroidCloudApplication app;
55
	//need to store if the user has successfully logged in
53
	// need to store if the user has successfully logged in
56 54
	private boolean loggedIn;
57
	
55

  
58 56
	@Override
59
	protected void onCreate(Bundle savedInstanceState){
57
	protected void onCreate(Bundle savedInstanceState) {
60 58
		super.onCreate(savedInstanceState);
61
		app = (AndroidCloudApplication)this.getApplication();
62
		//So keyboard doesn't open till user clicks
63
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
59
		app = (AndroidCloudApplication) this.getApplication();
60
		// So keyboard doesn't open till user clicks
61
		this.getWindow().setSoftInputMode(
62
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
64 63
		registerForContextMenu(getListView());
65 64
	}
66
	
65

  
67 66
	@Override
68 67
	protected void onSaveInstanceState(Bundle outState) {
69 68
		super.onSaveInstanceState(outState);
70
	
69

  
71 70
		outState.putBoolean("isLoading", isLoading);
72 71
		outState.putBoolean("loggedIn", loggedIn);
73
		
74
		if(pDialog != null && pDialog.isShowing()){
72

  
73
		if (pDialog != null && pDialog.isShowing()) {
75 74
			hideDialog();
76 75
		}
77 76

  
78 77
	}
79 78

  
80 79
	protected void restoreState(Bundle state) {
81
		context = getApplicationContext();		
82
		if (state != null && state.containsKey("loggedIn")){
80
		context = getApplicationContext();
81
		if (state != null && state.containsKey("loggedIn")) {
83 82
			loggedIn = state.getBoolean("loggedIn");
84
		}
85
		else{
83
		} else {
86 84
			loggedIn = false;
87 85
		}
88
		
86

  
89 87
		/*
90
		 * need to restore the pDialog is was shown before
91
		 * a config change
88
		 * need to restore the pDialog is was shown before a config change
92 89
		 */
93
		if (state != null && state.containsKey("isLoading")){
90
		if (state != null && state.containsKey("isLoading")) {
94 91
			isLoading = state.getBoolean("isLoading");
95
			if(isLoading){
92
			if (isLoading) {
96 93
				showDialog();
97 94
			}
98 95
		}
99
		
96

  
100 97
	}
101
	
98

  
102 99
	@Override
103
	protected void onStart(){
100
	protected void onStart() {
104 101
		super.onStart();
105
		if(isLoading){
102
		if (isLoading) {
106 103
			showDialog();
107 104
		}
108 105
	}
109
	
106

  
110 107
	@Override
111
	protected void onStop(){
108
	protected void onStop() {
112 109
		super.onStop();
113
		if(isLoading){
110
		if (isLoading) {
114 111
			hideDialog();
115 112
			isLoading = true;
116 113
		}
117 114
	}
118
	
115

  
119 116
	@Override
120
	protected void onPause(){
117
	protected void onPause() {
121 118
		super.onPause();
122 119
		Calendar cal = Calendar.getInstance();
123 120
		AndroidCloudApplication.lastPause = cal.getTimeInMillis();
124 121
	}
125
	
122

  
126 123
	@Override
127
	protected void onResume(){
124
	protected void onResume() {
128 125
		super.onResume();
129 126
		Calendar cal = Calendar.getInstance();
130 127
		long curTime = cal.getTimeInMillis();
131
		if(curTime - AndroidCloudApplication.lastPause > 5000){
128
		if (curTime - AndroidCloudApplication.lastPause > 5000) {
132 129
			verifyPassword();
133 130
		}
134 131
	}
135
	
132

  
136 133
	/*
137
	 * if the application is password protected,
138
	 * the user must provide the password before
139
	 * gaining access
134
	 * if the application is password protected, the user must provide the
135
	 * password before gaining access
140 136
	 */
141
	private void verifyPassword(){
137
	private void verifyPassword() {
142 138
		PasswordManager pwManager = new PasswordManager(getSharedPreferences(
143 139
				Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
144
		if(pwManager.hasPassword()){
140
		if (pwManager.hasPassword()) {
145 141
			createCustomDialog(PASSWORD_PROMPT);
146 142
		}
147 143
	}
148 144

  
149
	private boolean rightPassword(String password){
145
	private boolean rightPassword(String password) {
150 146
		PasswordManager pwManager = new PasswordManager(getSharedPreferences(
151 147
				Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
152 148
		return pwManager.verifyEnteredPassword(password);
153 149
	}
154 150

  
155

  
156 151
	/*
157
	 * forces the user to enter a correct password
158
	 * before they gain access to application data
152
	 * forces the user to enter a correct password before they gain access to
153
	 * application data
159 154
	 */
160 155
	private void createCustomDialog(int id) {
161 156
		final Dialog dialog = new Dialog(CloudListActivity.this);
......
166 161
			dialog.setCancelable(false);
167 162
			Button button = (Button) dialog.findViewById(R.id.submit_password);
168 163
			button.setOnClickListener(new OnClickListener() {
169
				public void onClick(View v){
170
					EditText passwordText = ((EditText)dialog.findViewById(R.id.submit_password_text));
171
					if(!rightPassword(passwordText.getText().toString())){
164
				public void onClick(View v) {
165
					EditText passwordText = ((EditText) dialog
166
							.findViewById(R.id.submit_password_text));
167
					if (!rightPassword(passwordText.getText().toString())) {
172 168
						passwordText.setText("");
173 169
						showToast("Password was incorrect.");
174 170
						loggedIn = false;
175
					}
176
					else{
171
					} else {
177 172
						dialog.dismiss();
178 173
						loggedIn = true;
179 174
					}
......
183 178
			dialog.show();
184 179
		}
185 180
	}
186
	
181

  
187 182
	protected final void showAlert(String title, String message) {
188 183
		try {
189 184
			AlertDialog alert = new AlertDialog.Builder(this).create();
......
199 194
			e.printStackTrace();
200 195
		}
201 196
	}
202
	
203
	protected final void showError(String message, HttpBundle bundle){
204
		Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
205
		viewIntent.putExtra("errorMessage", message);
206
		viewIntent.putExtra("response", bundle.getResponseText());
207
		viewIntent.putExtra("request", bundle.getCurlRequest());
208
		startActivity(viewIntent);
197

  
198
	protected final void showError(final String message, HttpBundle bundle) {
199
		new AsyncTask<HttpBundle, Void, HttpBundle>() {
200

  
201
			@Override
202
			protected HttpBundle doInBackground(HttpBundle... params) {
203
				if (params != null && params.length > 0) {
204
					HttpBundle result = params[0];
205
					result.parseResponse();
206
					return result;
207
				}
208
				return null;
209
			}
210

  
211
			@Override
212
			protected void onPostExecute(HttpBundle result) {
213
				// TODO Auto-generated method stub
214
				super.onPostExecute(result);
215
				if (result != null) {
216
					Intent viewIntent = new Intent(getApplicationContext(),
217
							ServerErrorActivity.class);
218
					viewIntent.putExtra("errorMessage", message);
219
					viewIntent.putExtra("response",
220
							result.getResponseTextStatic());
221
					viewIntent.putExtra("request", result.getCurlRequest());
222
					startActivity(viewIntent);
223
				}
224
			}
225

  
226
		}.execute(bundle);
227

  
209 228
	}
210 229
	
230

  
211 231
	protected void showToast(String message) {
212 232
		Context context = getApplicationContext();
213 233
		int duration = Toast.LENGTH_SHORT;
......
215 235
		toast.show();
216 236
	}
217 237

  
218
	protected final CloudServersException parseCloudServersException(HttpResponse response) {
238
	protected final CloudServersException parseCloudServersException(
239
			HttpResponse response) {
219 240
		CloudServersException cse = new CloudServersException();
220 241
		try {
221
		    BasicResponseHandler responseHandler = new BasicResponseHandler();
222
		    String body = responseHandler.handleResponse(response);
223
	    	CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
224
	    	SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
225
	    	XMLReader xmlReader = saxParser.getXMLReader();
226
	    	xmlReader.setContentHandler(parser);
227
	    	xmlReader.parse(new InputSource(new StringReader(body)));		    	
228
	    	cse = parser.getException();		    	
242
			BasicResponseHandler responseHandler = new BasicResponseHandler();
243
			String body = responseHandler.handleResponse(response);
244
			CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
245
			SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
246
			XMLReader xmlReader = saxParser.getXMLReader();
247
			xmlReader.setContentHandler(parser);
248
			xmlReader.parse(new InputSource(new StringReader(body)));
249
			cse = parser.getException();
229 250
		} catch (ClientProtocolException e) {
230 251
			cse = new CloudServersException();
231 252
			cse.setMessage(e.getLocalizedMessage());
......
246 267
	}
247 268

  
248 269
	protected final void hideDialog() {
249
		if(pDialog != null){
270
		if (pDialog != null) {
250 271
			isLoading = false;
251 272
			pDialog.dismiss();
252 273
		}
253 274
	}
254 275

  
255 276
	protected final void showDialog() {
256
		if(pDialog == null || !pDialog.isShowing()){
277
		if (pDialog == null || !pDialog.isShowing()) {
257 278
			isLoading = true;
258
			pDialog = new ProgressDialog(this);
259
			pDialog.setProgressStyle(R.style.NewDialog);
260
			
261
			/*
262
			 * if back is pressed while dialog is showing it will 
263
			 * still finish the activity
264
			 */
265
			pDialog.setOnCancelListener(new OnCancelListener() {
266
				@Override
267
				public void onCancel(DialogInterface dialog) {
268
					finish();
269
				}
270
			});
271
			pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
279
			pDialog = CenteredProgressDialog.show(this, "", "", false, true,
280
					new OnCancelListener() {
281
						@Override
282
						public void onCancel(DialogInterface dialog) {
283
							finish();
284
						}
285
					});
272 286
			pDialog.show();
273
			pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
274 287
		}
275 288
	}
276
	
277
	protected Context getContext(){
289

  
290
	protected Context getContext() {
278 291
		return context;
279 292
	}
280 293
}

Also available in: Unified diff