package org.bostonandroid.umbrellatoday;

import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpStatus;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import android.net.Uri;
import android.widget.TextView;
import android.util.Log;
import org.apache.http.HttpEntity;
import java.io.ByteArrayOutputStream;
import android.content.Intent;
import android.util.Xml;
import android.sax.RootElement;
import android.sax.Element;
import android.sax.EndTextElementListener;

public class UmbrellaForToday extends Activity
{
    public final static String TAG = "UmbrellaForToday";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.today);

        Intent intent = getIntent();
        Uri reportUri = intent.getData();

        ReportRetriever r = new ReportRetriever();
        r.execute(reportUri);
    }

    private class ReportRetriever extends AsyncTask<Uri, Void, Report> {
      protected Report doInBackground(Uri... uris) {
        return retrieveReport(uris[0]);
      }

      protected void onPostExecute(Report report) {
        if (report != null) {
          TextView tv = (TextView)findViewById(R.id.report);
          tv.setText(report.getLocationName() + ": " + report.getAnswer());
        }
        // else bounce back to UmbrellaToday activity with an error message
      }

      private Report retrieveReport(Uri uri) {
        final DefaultHttpClient client = new DefaultHttpClient();
        final HttpGet getRequest = new HttpGet(uri.toString());
        final Report report = new Report();

        RootElement root = new RootElement("forecast");
        root.getChild("answer").setEndTextElementListener(new EndTextElementListener() {
          public void end(String body) {
            report.setAnswer(body);
          }
        });
        Element location = root.getChild("location");
        location.getChild("name").setEndTextElementListener(new EndTextElementListener() {
          public void end(String body) {
            report.setLocationName(body);
          }
        });

        try {
          HttpResponse response = client.execute(getRequest);

          final int statusCode = response.getStatusLine().getStatusCode();
          if (statusCode != HttpStatus.SC_OK) {
            Log.w(TAG, "Error " + statusCode + " retrieving weather report.");
            return null;
          }

          HttpEntity entity = response.getEntity();

          if (entity != null) {
            //ReportHandler handler = new ReportHandler();
          
            Xml.parse(entity.getContent(), Xml.Encoding.UTF_8, root.getContentHandler());

            return report;
          }

        } catch (Exception e) {
          getRequest.abort();
        }
        return null;
      }
    }
}

@@ -1051,24 +1051,115 @@
 etriever();%0A
+        // FIXME: ReportRetriever will bail if this activity was started without an intent%0A
         r.ex
@@ -1573,24 +1573,193 @@
 tAnswer());%0A
+          // TODO: Get activity title from strings.xml, also do something if location%5Bname%5D is null%0A          setTitle(%22UmbrellaToday for %22 + report.getLocationName());%0A
         %7D%0A  
@@ -1766,16 +1766,22 @@
       //
+ TODO:
  else bo
@@ -2974,78 +2974,8 @@
 ) %7B%0A
-            //ReportHandler handler = new ReportHandler();%0A          %0A
     

Set the title of UmbrellaForToday to location[name] as set by the Report.