Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / CloudListActivity.java @ b2a2d2f1

History | View | Annotate | Download (5.1 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5

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

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

    
18
import com.rackspace.cloud.servers.api.client.CloudServersException;
19
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
20
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
21

    
22
import android.app.AlertDialog;
23
import android.app.ProgressDialog;
24
import android.content.Context;
25
import android.content.DialogInterface;
26
import android.content.Intent;
27
import android.content.DialogInterface.OnCancelListener;
28
import android.os.Bundle;
29
import android.view.WindowManager;
30
import android.view.ViewGroup.LayoutParams;
31
import android.widget.ProgressBar;
32
import android.widget.Toast;
33

    
34
public class CloudListActivity extends GaListActivity{
35

    
36
        private Context context;
37
        private boolean isLoading;
38
        private ProgressDialog pDialog;
39
        
40
        @Override
41
        protected void onCreate(Bundle savedInstanceState){
42
                super.onCreate(savedInstanceState);
43
                
44
                //So keyboard doesn't open till user clicks
45
                this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
46
        }
47
        
48
        @Override
49
        protected void onSaveInstanceState(Bundle outState) {
50
                super.onSaveInstanceState(outState);
51
        
52
                outState.putBoolean("isLoading", isLoading);
53
                
54
                if(pDialog != null && pDialog.isShowing()){
55
                        hideDialog();
56
                }
57

    
58
        }
59

    
60
        protected void restoreState(Bundle state) {
61
                context = getApplicationContext();
62
                
63
                /*
64
                 * need to restore the pDialog is was shown before
65
                 * a config change
66
                 */
67
                if (state != null && state.containsKey("isLoading")){
68
                        isLoading = state.getBoolean("isLoading");
69
                        if(isLoading){
70
                                showDialog();
71
                        }
72
                }
73
                
74
        }
75
        
76
        protected final void showAlert(String title, String message) {
77
                try {
78
                        AlertDialog alert = new AlertDialog.Builder(this).create();
79
                        alert.setTitle(title);
80
                        alert.setMessage(message);
81
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
82
                                public void onClick(DialogInterface dialog, int which) {
83
                                        return;
84
                                }
85
                        });
86
                        alert.show();
87
                } catch (Exception e) {
88
                        e.printStackTrace();
89
                }
90
        }
91
        
92
        protected final void showError(String message, HttpBundle bundle){
93
                Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
94
                viewIntent.putExtra("errorMessage", message);
95
                viewIntent.putExtra("response", bundle.getResponseText());
96
                viewIntent.putExtra("request", bundle.getCurlRequest());
97
                startActivity(viewIntent);
98
        }
99
        
100
        protected void showToast(String message) {
101
                Context context = getApplicationContext();
102
                int duration = Toast.LENGTH_SHORT;
103
                Toast toast = Toast.makeText(context, message, duration);
104
                toast.show();
105
        }
106

    
107
        protected final CloudServersException parseCloudServersException(HttpResponse response) {
108
                CloudServersException cse = new CloudServersException();
109
                try {
110
                    BasicResponseHandler responseHandler = new BasicResponseHandler();
111
                    String body = responseHandler.handleResponse(response);
112
                    CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
113
                    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
114
                    XMLReader xmlReader = saxParser.getXMLReader();
115
                    xmlReader.setContentHandler(parser);
116
                    xmlReader.parse(new InputSource(new StringReader(body)));                            
117
                    cse = parser.getException();                            
118
                } catch (ClientProtocolException e) {
119
                        cse = new CloudServersException();
120
                        cse.setMessage(e.getLocalizedMessage());
121
                } catch (IOException e) {
122
                        cse = new CloudServersException();
123
                        cse.setMessage(e.getLocalizedMessage());
124
                } catch (ParserConfigurationException e) {
125
                        cse = new CloudServersException();
126
                        cse.setMessage(e.getLocalizedMessage());
127
                } catch (SAXException e) {
128
                        cse = new CloudServersException();
129
                        cse.setMessage(e.getLocalizedMessage());
130
                } catch (FactoryConfigurationError e) {
131
                        cse = new CloudServersException();
132
                        cse.setMessage(e.getLocalizedMessage());
133
                }
134
                return cse;
135
        }
136

    
137
        protected final void hideDialog() {
138
                if(pDialog != null){
139
                        isLoading = false;
140
                        pDialog.dismiss();
141
                }
142
        }
143

    
144
        protected final void showDialog() {
145
                if(pDialog == null || !pDialog.isShowing()){
146
                        isLoading = true;
147
                        pDialog = new ProgressDialog(this);
148
                        pDialog.setProgressStyle(R.style.NewDialog);
149
                        
150
                        /*
151
                         * if back is pressed while dialog is showing it will 
152
                         * still finish the activity
153
                         */
154
                        pDialog.setOnCancelListener(new OnCancelListener() {
155
                                @Override
156
                                public void onCancel(DialogInterface dialog) {
157
                                        finish();
158
                                }
159
                        });
160

    
161
                        pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
162
                        pDialog.show();
163
                        pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
164
                }
165
        }
166
        
167
        protected Context getContext(){
168
                return context;
169
        }
170
        
171
}