Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / CloudListActivity.java @ 7193acee

History | View | Annotate | Download (7.9 kB)

1
package com.rackspace.cloud.android;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.util.Calendar;
6

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

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

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

    
34
import com.rackspace.cloud.servers.api.client.CloudServersException;
35
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
36
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
37

    
38
/*
39
 * CloudActivity manages the display and hiding of 
40
 * pDialog. 
41
 * 
42
 * Also provides many accessory methods that are common
43
 * to Activities
44
 */
45
public class CloudListActivity extends GaListActivity {
46

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

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

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

    
69
                outState.putBoolean("isLoading", isLoading);
70
                outState.putBoolean("loggedIn", loggedIn);
71

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

    
76
        }
77

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

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

    
96
        }
97

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

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

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

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

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

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

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

    
176
                        });
177
                        dialog.show();
178
                }
179
        }
180

    
181
        protected final void showAlert(String title, String message) {
182
                try {
183
                        AlertDialog alert = new AlertDialog.Builder(this).create();
184
                        alert.setTitle(title);
185
                        alert.setMessage(message);
186
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
187
                                public void onClick(DialogInterface dialog, int which) {
188
                                        return;
189
                                }
190
                        });
191
                        alert.show();
192
                } catch (Exception e) {
193
                        e.printStackTrace();
194
                }
195
        }
196

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

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

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

    
225
                }.execute(bundle);
226

    
227
        }
228
        
229

    
230
        protected void showToast(String message) {
231
                Context context = getApplicationContext();
232
                int duration = Toast.LENGTH_SHORT;
233
                Toast toast = Toast.makeText(context, message, duration);
234
                toast.show();
235
        }
236

    
237
        protected final CloudServersException parseCloudServersException(
238
                        HttpResponse response) {
239
                CloudServersException cse = new CloudServersException();
240
                try {
241
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
242
                        String body = responseHandler.handleResponse(response);
243
                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
244
                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
245
                        XMLReader xmlReader = saxParser.getXMLReader();
246
                        xmlReader.setContentHandler(parser);
247
                        xmlReader.parse(new InputSource(new StringReader(body)));
248
                        cse = parser.getException();
249
                } catch (ClientProtocolException e) {
250
                        cse = new CloudServersException();
251
                        cse.setMessage(e.getLocalizedMessage());
252
                } catch (IOException e) {
253
                        cse = new CloudServersException();
254
                        cse.setMessage(e.getLocalizedMessage());
255
                } catch (ParserConfigurationException e) {
256
                        cse = new CloudServersException();
257
                        cse.setMessage(e.getLocalizedMessage());
258
                } catch (SAXException e) {
259
                        cse = new CloudServersException();
260
                        cse.setMessage(e.getLocalizedMessage());
261
                } catch (FactoryConfigurationError e) {
262
                        cse = new CloudServersException();
263
                        cse.setMessage(e.getLocalizedMessage());
264
                }
265
                return cse;
266
        }
267

    
268
        protected final void hideDialog() {
269
                if (pDialog != null) {
270
                        isLoading = false;
271
                        pDialog.dismiss();
272
                }
273
        }
274

    
275
        protected final void showDialog() {
276
                if (pDialog == null || !pDialog.isShowing()) {
277
                        isLoading = true;
278
                        pDialog = CenteredProgressDialog.show(this, "", "", false, true,
279
                                        new OnCancelListener() {
280
                                                @Override
281
                                                public void onCancel(DialogInterface dialog) {
282
                                                        finish();
283
                                                }
284
                                        });
285
                        pDialog.show();
286
                }
287
        }
288

    
289
        protected Context getContext() {
290
                return context;
291
        }
292
}