package net.tevp.postcode;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.widget.TextView;
import android.location.Location;
import android.widget.Button;
import android.view.View;

public class Postcode extends Activity implements PostcodeListener {	
	TextView tv;
	PostcodeBackend pb;
	String lastPostcode = null;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.Postcode);

		pb = (PostcodeBackend) getLastNonConfigurationInstance();
		if (pb == null)
			pb = new PostcodeBackend();
		newPostcode(false);

		final Postcode self = this;
		((Button) findViewById(R.id.btnUpdate)).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				self.newPostcode(true);
			}
		});
    }

	private void newPostcode(boolean mustBeNew)
	{
		if (lastPostcode!= null)
			setText(lastPostcode+"\n(updating...)");
		else
			setText("Finding location..");
		pb.getPostcode(this,this,mustBeNew);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		newPostcode(false);
	}

	public static void main(String[] args) throws Exception {
		String[] coords = args[0].split(",");
		assert coords.length == 2;
		System.out.println(PostcodeBackend.get(Double.parseDouble(coords[0]),Double.parseDouble(coords[1])));
	}

	private void setText(final String text)
	{
		final TextView myTV = tv;		
		runOnUiThread(new Runnable()
		{
			public void run()
			{
				myTV.setText(text);
			}
		});
	}

	public void postcodeChange(String postcode)
	{
		lastPostcode = postcode;
		setText(postcode);
	}

	public void updatedLocation(Location l)
	{
		if (lastPostcode!= null)
			setText(lastPostcode+"\nFound new location, looking for postcode...");
		else
			setText("Found location, looking for postcode...");
	}

	public void locationFindFail()
	{
		setText("Can't find location. Enable GPS, or wait until you're outside");
	}

	public void postcodeLookupFail()
	{
		setText("Failed to lookup postcode. Please check you've got a working internet connection");
	}

	@Override
	public Object onRetainNonConfigurationInstance()
	{
		return pb;
	}
}

@@ -229,16 +229,75 @@
 ew.View;
+%0Aimport java.text.SimpleDateFormat;%0A%0Aimport java.util.Date;
 %0A%0Apublic
@@ -424,16 +424,155 @@
 = null;%0A
+%09PostcodeState ps = null;%0A%0A%09private class PostcodeState%0A%09%7B%0A%09%09public PostcodeBackend pb;%0A%09%09public String text;%0A%09%09public Date timestamp;%0A%09%7D%0A%0A
 %09%0A%09/** C
@@ -824,17 +824,17 @@
 e);%0A%0A%09%09p
-b
+s
  = (Post
@@ -837,23 +837,21 @@
 Postcode
-Backend
+State
 ) getLas
@@ -883,80 +883,8 @@
 ();%0A
-%09%09if (pb == null)%0A%09%09%09pb = new PostcodeBackend();%0A%09%09newPostcode(false);%0A%0A
 %09%09fi
@@ -1348,25 +1348,261 @@
 ;%0A%09%09
-newPostcode(false
+if (ps == null %7C%7C ps.pb == null)%0A%09%09%09pb = new PostcodeBackend();%0A%09%09else%0A%09%09%09pb = ps.pb;%0A%09%09%0A%09%09if (ps == null%0A%09%09%09%09%7C%7C (new Date().getTime()-ps.timestamp.getTime()) %3E 300 * 1000 // more than 5 minutes old%0A%09%09%09%09)%0A%09%09%09newPostcode(false);%0A%09%09else%0A%09%09%09setText(ps.text
 );%0A%09
@@ -2640,16 +2640,138 @@
 ce()%0A%09%7B%0A
+%09%09PostcodeState ps = new PostcodeState();%0A%09%09ps.pb = pb;%0A%09%09ps.text = tv.getText().toString();%0A%09%09ps.timestamp = new Date();%0A
 %09%09return
@@ -2772,16 +2772,16 @@
 return p
-b
+s
 ;%0A%09%7D%0A%7D%0A

Save a PostcodeState including current text