Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / CloudListActivity.java @ ecbad159

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.app.ProgressDialog;
22
import android.content.Context;
23
import android.content.DialogInterface;
24
import android.content.DialogInterface.OnCancelListener;
25
import android.content.Intent;
26
import android.os.Bundle;
27
import android.view.View;
28
import android.view.View.OnClickListener;
29
import android.view.ViewGroup.LayoutParams;
30
import android.view.WindowManager;
31
import android.widget.Button;
32
import android.widget.EditText;
33
import android.widget.ProgressBar;
34
import android.widget.Toast;
35

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

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

    
50
        private final int PASSWORD_PROMPT = 123;
51
        private Context context;
52
        private boolean isLoading;
53
        private ProgressDialog pDialog;
54
        protected AndroidCloudApplication app;
55
        //need to store if the user has successfully logged in
56
        private boolean loggedIn;
57
        
58
        @Override
59
        protected void onCreate(Bundle savedInstanceState){
60
                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); 
64
                registerForContextMenu(getListView());
65
        }
66
        
67
        @Override
68
        protected void onSaveInstanceState(Bundle outState) {
69
                super.onSaveInstanceState(outState);
70
        
71
                outState.putBoolean("isLoading", isLoading);
72
                outState.putBoolean("loggedIn", loggedIn);
73
                
74
                if(pDialog != null && pDialog.isShowing()){
75
                        hideDialog();
76
                }
77

    
78
        }
79

    
80
        protected void restoreState(Bundle state) {
81
                context = getApplicationContext();                
82
                if (state != null && state.containsKey("loggedIn")){
83
                        loggedIn = state.getBoolean("loggedIn");
84
                }
85
                else{
86
                        loggedIn = false;
87
                }
88
                
89
                /*
90
                 * need to restore the pDialog is was shown before
91
                 * a config change
92
                 */
93
                if (state != null && state.containsKey("isLoading")){
94
                        isLoading = state.getBoolean("isLoading");
95
                        if(isLoading){
96
                                showDialog();
97
                        }
98
                }
99
                
100
        }
101
        
102
        @Override
103
        protected void onStart(){
104
                super.onStart();
105
                if(isLoading){
106
                        showDialog();
107
                }
108
        }
109
        
110
        @Override
111
        protected void onStop(){
112
                super.onStop();
113
                if(isLoading){
114
                        hideDialog();
115
                        isLoading = true;
116
                }
117
        }
118
        
119
        @Override
120
        protected void onPause(){
121
                super.onPause();
122
                Calendar cal = Calendar.getInstance();
123
                AndroidCloudApplication.lastPause = cal.getTimeInMillis();
124
        }
125
        
126
        @Override
127
        protected void onResume(){
128
                super.onResume();
129
                Calendar cal = Calendar.getInstance();
130
                long curTime = cal.getTimeInMillis();
131
                if(curTime - AndroidCloudApplication.lastPause > 5000){
132
                        verifyPassword();
133
                }
134
        }
135
        
136
        /*
137
         * if the application is password protected,
138
         * the user must provide the password before
139
         * gaining access
140
         */
141
        private void verifyPassword(){
142
                PasswordManager pwManager = new PasswordManager(getSharedPreferences(
143
                                Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
144
                if(pwManager.hasPassword()){
145
                        createCustomDialog(PASSWORD_PROMPT);
146
                }
147
        }
148

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

    
155

    
156
        /*
157
         * forces the user to enter a correct password
158
         * before they gain access to application data
159
         */
160
        private void createCustomDialog(int id) {
161
                final Dialog dialog = new Dialog(CloudListActivity.this);
162
                switch (id) {
163
                case PASSWORD_PROMPT:
164
                        dialog.setContentView(R.layout.passworddialog);
165
                        dialog.setTitle("Enter your password:");
166
                        dialog.setCancelable(false);
167
                        Button button = (Button) dialog.findViewById(R.id.submit_password);
168
                        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())){
172
                                                passwordText.setText("");
173
                                                showToast("Password was incorrect.");
174
                                                loggedIn = false;
175
                                        }
176
                                        else{
177
                                                dialog.dismiss();
178
                                                loggedIn = true;
179
                                        }
180
                                }
181

    
182
                        });
183
                        dialog.show();
184
                }
185
        }
186
        
187
        protected final void showAlert(String title, String message) {
188
                try {
189
                        AlertDialog alert = new AlertDialog.Builder(this).create();
190
                        alert.setTitle(title);
191
                        alert.setMessage(message);
192
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
193
                                public void onClick(DialogInterface dialog, int which) {
194
                                        return;
195
                                }
196
                        });
197
                        alert.show();
198
                } catch (Exception e) {
199
                        e.printStackTrace();
200
                }
201
        }
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);
209
        }
210
        
211
        protected void showToast(String message) {
212
                Context context = getApplicationContext();
213
                int duration = Toast.LENGTH_SHORT;
214
                Toast toast = Toast.makeText(context, message, duration);
215
                toast.show();
216
        }
217

    
218
        protected final CloudServersException parseCloudServersException(HttpResponse response) {
219
                CloudServersException cse = new CloudServersException();
220
                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();                            
229
                } catch (ClientProtocolException e) {
230
                        cse = new CloudServersException();
231
                        cse.setMessage(e.getLocalizedMessage());
232
                } catch (IOException e) {
233
                        cse = new CloudServersException();
234
                        cse.setMessage(e.getLocalizedMessage());
235
                } catch (ParserConfigurationException e) {
236
                        cse = new CloudServersException();
237
                        cse.setMessage(e.getLocalizedMessage());
238
                } catch (SAXException e) {
239
                        cse = new CloudServersException();
240
                        cse.setMessage(e.getLocalizedMessage());
241
                } catch (FactoryConfigurationError e) {
242
                        cse = new CloudServersException();
243
                        cse.setMessage(e.getLocalizedMessage());
244
                }
245
                return cse;
246
        }
247

    
248
        protected final void hideDialog() {
249
                if(pDialog != null){
250
                        isLoading = false;
251
                        pDialog.dismiss();
252
                }
253
        }
254

    
255
        protected final void showDialog() {
256
                if(pDialog == null || !pDialog.isShowing()){
257
                        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);
272
                        pDialog.show();
273
                        pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
274
                }
275
        }
276
        
277
        protected Context getContext(){
278
                return context;
279
        }
280
}