Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / AddFileActivity.java @ 0edf6b39

History | View | Annotate | Download (6.6 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.files.api.client.ContainerObjectManager;
19
import com.rackspace.cloud.servers.api.client.CloudServersException;
20
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
21
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
22

    
23
import android.app.Activity;
24
import android.app.AlertDialog;
25
import android.app.ProgressDialog;
26
import android.content.Context;
27
import android.content.DialogInterface;
28
import android.content.Intent;
29
import android.os.AsyncTask;
30
import android.os.Bundle;
31
import android.view.View;
32
import android.view.View.OnClickListener;
33
import android.widget.Button;
34
import android.widget.EditText;
35

    
36
public class AddFileActivity extends GaActivity implements OnClickListener{
37
        
38
        private Context context;        
39
        private EditText fileName;
40
        private EditText contents;
41
        private String containerName;
42
        private String path;
43
        private boolean isAdding;
44
        private ProgressDialog dialog;
45

    
46
        /** Called when the activity is first created. */
47
    @Override
48
    public void onCreate(Bundle savedInstanceState) {
49
        super.onCreate(savedInstanceState);
50
        setContentView(R.layout.addtextfile);
51
        context = getApplicationContext();
52
        containerName = (String) this.getIntent().getExtras().get("Cname");
53
        path = (String) this.getIntent().getExtras().get("curPath");
54
        setUpDialog(savedInstanceState);
55
        setUpInputs();
56
    }
57
    
58
    private void setUpInputs(){
59
            ((Button) findViewById(R.id.new_file_button)).setOnClickListener(this);
60
            fileName = ((EditText)findViewById(R.id.file_name_text));
61
            fileName.append(".txt");
62
            contents = ((EditText)findViewById(R.id.new_file_text));
63
    }
64
    
65
    private void setUpDialog(Bundle savedInstanceState){
66
        isAdding = savedInstanceState != null && savedInstanceState.containsKey("isAdding") 
67
                    && savedInstanceState.getBoolean("isAdding");
68
        if(isAdding){
69
                showDialog();
70
        }
71
        
72
    }
73
    
74
    @Override
75
        protected void onSaveInstanceState(Bundle outState) {
76
                super.onSaveInstanceState(outState);
77
                outState.putBoolean("isAdding", isAdding);
78
                if(isAdding){
79
                        hideDialog();
80
                }
81
        }
82
    
83
    public void onClick(View arg0) {
84
                if ("".equals(fileName.getText().toString())) {
85
                        showAlert("Required Fields Missing", " File name is required.");
86
                } else {
87
                        //showActivityIndicators();
88
                        trackEvent(CATEGORY_FILE, EVENT_CREATE, "", -1);
89
                        new SaveFileTask().execute((Void[]) null);
90
                }
91
        }
92
    
93
    //using cloudServersException, it works for us too
94
        private CloudServersException parseCloudServersException(HttpResponse response) {
95
                CloudServersException cse = new CloudServersException();
96
                try {
97
                    BasicResponseHandler responseHandler = new BasicResponseHandler();
98
                    String body = responseHandler.handleResponse(response);
99
                    CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
100
                    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
101
                    XMLReader xmlReader = saxParser.getXMLReader();
102
                    xmlReader.setContentHandler(parser);
103
                    xmlReader.parse(new InputSource(new StringReader(body)));                            
104
                    cse = parser.getException();                            
105
                } catch (ClientProtocolException e) {
106
                        cse = new CloudServersException();
107
                        cse.setMessage(e.getLocalizedMessage());
108
                } catch (IOException e) {
109
                        cse = new CloudServersException();
110
                        cse.setMessage(e.getLocalizedMessage());
111
                } catch (ParserConfigurationException e) {
112
                        cse = new CloudServersException();
113
                        cse.setMessage(e.getLocalizedMessage());
114
                } catch (SAXException e) {
115
                        cse = new CloudServersException();
116
                        cse.setMessage(e.getLocalizedMessage());
117
                } catch (FactoryConfigurationError e) {
118
                        cse = new CloudServersException();
119
                        cse.setMessage(e.getLocalizedMessage());
120
                }
121
                return cse;
122
        }
123
        
124
        private void showAlert(String title, String message) {
125
            try {
126
                AlertDialog alert = new AlertDialog.Builder(this).create();
127
                alert.setTitle(title);
128
                alert.setMessage(message);
129
                alert.setButton("OK", new DialogInterface.OnClickListener() {
130
              public void onClick(DialogInterface dialog, int which) {
131
                return;
132
            } }); 
133
                alert.show();
134
            } catch (Exception e) {
135
                    e.printStackTrace();
136
            }
137
    }
138
        
139
        private void startFileError(String message, HttpBundle bundle){
140
                Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
141
                viewIntent.putExtra("errorMessage", message);
142
                viewIntent.putExtra("response", bundle.getResponseText());
143
                viewIntent.putExtra("request", bundle.getCurlRequest());
144
                startActivity(viewIntent);
145
        }
146

    
147
        private class SaveFileTask extends AsyncTask<Void, Void, HttpBundle> {
148
            private CloudServersException exception;
149
            
150
            @Override
151
                protected void onPreExecute(){
152
                        isAdding = true;
153
                        showDialog();
154
                }
155
            
156
            @Override
157
                protected HttpBundle doInBackground(Void... arg0) {
158
                    HttpBundle bundle = null;
159
                        try {
160
                                bundle = (new ContainerObjectManager(context)).addObject(containerName, path, fileName.getText().toString(), "text/plain", contents.getText().toString());
161
                        } catch (CloudServersException e) {
162
                                exception = e;
163
                        }
164
                        return bundle;
165
                }
166
            
167
                @Override
168
                protected void onPostExecute(HttpBundle bundle) {
169
                        isAdding = false;
170
                        hideDialog();
171
                        HttpResponse response = bundle.getResponse();
172
                        if (response != null) {
173
                                int statusCode = response.getStatusLine().getStatusCode();
174
                                if (statusCode == 201) {
175
                                        setResult(Activity.RESULT_OK);
176
                                        finish();
177
                                } else {
178
                                        CloudServersException cse = parseCloudServersException(response);
179
                                        if ("".equals(cse.getMessage())) {
180
                                                startFileError("There was a problem creating your file.", bundle);
181
                                        } else {
182
                                                startFileError("There was a problem creating your file: " + cse.getMessage() + " Check file name and try again", bundle);
183
                                        }
184
                                }
185
                        } else if (exception != null) {
186
                                startFileError("There was a problem creating your file: " + exception.getMessage()+" Check file name and try again", bundle);                                
187
                        }                        
188
                }
189
    }
190
        
191
        private void showDialog() {
192
                if(dialog == null || !dialog.isShowing()){
193
                        dialog = ProgressDialog.show(AddFileActivity.this, "", "Adding File...", true);
194
                }
195
    }
196
    
197
    private void hideDialog() {
198
            if(dialog != null){
199
                    dialog.dismiss();
200
            }
201
    }
202
}