Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / CloudListActivity.java @ 1634500c

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
        }
65
        
66
        @Override
67
        protected void onSaveInstanceState(Bundle outState) {
68
                super.onSaveInstanceState(outState);
69
        
70
                outState.putBoolean("isLoading", isLoading);
71
                outState.putBoolean("loggedIn", loggedIn);
72
                
73
                if(pDialog != null && pDialog.isShowing()){
74
                        hideDialog();
75
                }
76

    
77
        }
78

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

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

    
154

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

    
181
                        });
182
                        dialog.show();
183
                }
184
        }
185
        
186
        protected final void showAlert(String title, String message) {
187
                try {
188
                        AlertDialog alert = new AlertDialog.Builder(this).create();
189
                        alert.setTitle(title);
190
                        alert.setMessage(message);
191
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
192
                                public void onClick(DialogInterface dialog, int which) {
193
                                        return;
194
                                }
195
                        });
196
                        alert.show();
197
                } catch (Exception e) {
198
                        e.printStackTrace();
199
                }
200
        }
201
        
202
        protected final void showError(String message, HttpBundle bundle){
203
                Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
204
                viewIntent.putExtra("errorMessage", message);
205
                viewIntent.putExtra("response", bundle.getResponseText());
206
                viewIntent.putExtra("request", bundle.getCurlRequest());
207
                startActivity(viewIntent);
208
        }
209
        
210
        protected void showToast(String message) {
211
                Context context = getApplicationContext();
212
                int duration = Toast.LENGTH_SHORT;
213
                Toast toast = Toast.makeText(context, message, duration);
214
                toast.show();
215
        }
216

    
217
        protected final CloudServersException parseCloudServersException(HttpResponse response) {
218
                CloudServersException cse = new CloudServersException();
219
                try {
220
                    BasicResponseHandler responseHandler = new BasicResponseHandler();
221
                    String body = responseHandler.handleResponse(response);
222
                    CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
223
                    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
224
                    XMLReader xmlReader = saxParser.getXMLReader();
225
                    xmlReader.setContentHandler(parser);
226
                    xmlReader.parse(new InputSource(new StringReader(body)));                            
227
                    cse = parser.getException();                            
228
                } catch (ClientProtocolException e) {
229
                        cse = new CloudServersException();
230
                        cse.setMessage(e.getLocalizedMessage());
231
                } catch (IOException e) {
232
                        cse = new CloudServersException();
233
                        cse.setMessage(e.getLocalizedMessage());
234
                } catch (ParserConfigurationException e) {
235
                        cse = new CloudServersException();
236
                        cse.setMessage(e.getLocalizedMessage());
237
                } catch (SAXException e) {
238
                        cse = new CloudServersException();
239
                        cse.setMessage(e.getLocalizedMessage());
240
                } catch (FactoryConfigurationError e) {
241
                        cse = new CloudServersException();
242
                        cse.setMessage(e.getLocalizedMessage());
243
                }
244
                return cse;
245
        }
246

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

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