Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / CloudActivity.java @ 6ae5d8db

History | View | Annotate | Download (5.2 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.Activity;
23
import android.app.AlertDialog;
24
import android.app.ProgressDialog;
25
import android.content.Context;
26
import android.content.DialogInterface;
27
import android.content.Intent;
28
import android.content.DialogInterface.OnCancelListener;
29
import android.os.Bundle;
30
import android.view.WindowManager;
31
import android.view.ViewGroup.LayoutParams;
32
import android.widget.ProgressBar;
33
import android.widget.Toast;
34

    
35
/*
36
 * CloudActivity manages the display and hiding of 
37
 * pDialog. 
38
 * 
39
 * Also provides many accessory methods that are common
40
 * to Activities
41
 */
42
public abstract class CloudActivity extends Activity{
43

    
44
        protected Context context;
45
        private boolean isLoading;
46
        private ProgressDialog pDialog;
47
        
48
        @Override
49
        protected void onCreate(Bundle savedInstanceState){
50
                super.onCreate(savedInstanceState);
51
                
52
                //So keyboard doesn't open till user clicks
53
                this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
54
        }
55
        
56
        @Override
57
        protected void onSaveInstanceState(Bundle outState) {
58
                super.onSaveInstanceState(outState);
59
        
60
                outState.putBoolean("isLoading", isLoading);
61
                
62
                if(pDialog != null && pDialog.isShowing()){
63
                        hideDialog();
64
                }
65

    
66
        }
67

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

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

    
145
        protected final void hideDialog() {
146

    
147
                if(pDialog != null){
148
                        isLoading = false;
149
                        pDialog.dismiss();
150
                }
151
        }
152

    
153
        protected final void showDialog() {
154
                if(pDialog == null || !pDialog.isShowing()){
155
                        isLoading = true;
156
                        pDialog = new ProgressDialog(this, R.style.NewDialog);
157

    
158
                        /*
159
                         * if back is pressed while dialog is showing it will 
160
                         * still finish the activity
161
                         */
162
                        pDialog.setOnCancelListener(new OnCancelListener() {
163
                                @Override
164
                                public void onCancel(DialogInterface dialog) {
165
                                        finish();
166
                                }
167
                        });
168

    
169
                        pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
170
                        pDialog.show();
171
                        pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
172
                }
173
        }
174
        
175
        protected Context getContext(){
176
                return context;
177
        }
178
}