package net.tevp.Postcode;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.location.*;
import android.content.Context;
import android.widget.TextView;

public class Postcode extends Activity implements LocationListener {	
	TextView tv;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria c = new Criteria();
        String provider = lm.getBestProvider(c, false);
        lm.requestLocationUpdates(provider, 1000, 20, this);
                
        Location l = lm.getLastKnownLocation(provider);
        
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.Postcode);
        if (l!= null)
        	tv.setText(get(l.getLatitude(),l.getLongitude()));
        else
        	tv.setText("Postcode is unknown");
    }

    static Geohash e = new Geohash();

	static String get(double lat, double lon)
	{
		String s = e.encode(lat, lon);
		URL what;
		try {
			what = new URL("http://whatismypostcode.appspot.com/"+s);
		} catch (MalformedURLException e1) {
			return "malformed for"+s;
		}
		InputStream data;
		try {
			data = what.openStream();

			byte[] out = new byte[10];
			data.read(out,0,10);
			return new String(out);//+s+" - "+Double.toString(lat)+", "+Double.toString(lon);
		} catch (IOException e1) {
			return "ioexception "+e1.getMessage();
		}
	}

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

	public void onLocationChanged(Location l) {
		tv.setText(get(l.getLatitude(),l.getLongitude()));		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}


@@ -138,16 +138,36 @@
 tream;%0A%0A
+import org.json.*;%0A%0A
 import a
@@ -1156,16 +1156,20 @@
 hash();%0A
+    
 %0A%09static
@@ -1255,24 +1255,27 @@
 hat;%0A%09%09try %7B
+%09%09%09
 %0A%09%09%09what = n
@@ -1281,16 +1281,30 @@
 new URL(
+String.format(
 %22http://
@@ -1308,23 +1308,22 @@
 ://w
-hatismy
+ww.uk-
 postcode
 .app
@@ -1322,24 +1322,45 @@
 code
-.appspot.com/%22+s
+s.com/latlng/%25.8f,%25.8f.json%22,lat,lon)
 );%0A%09
@@ -1456,196 +1456,422 @@
 ;%0A%09%09
-try %7B%0A%09%09%09data = what.openStream();%0A%0A%09%09%09byte%5B%5D out = new byte%5B10%5D;%0A%09%09%09data.read(out,0,10);%0A%09%09%09return new String(out);//+s+%22 - %22+Double.toString(lat)+%22, %22+Double.toString(lon
+int max = 1000, sofar=0;%0A%09%09%0A%09%09byte%5B%5D out = new byte%5Bmax%5D;%0A%09%09try %7B%0A%09%09%09data = what.openStream();%0A%0A%09%09%09while (sofar%3Cmax)%0A%09%09%09%7B%0A%09%09%09%09int ret = data.read(out,sofar,max-sofar);%0A%09%09%09%09if (ret == -1)%0A%09%09%09%09%09break;%0A%09%09%09%09sofar += ret;%0A%09%09%09%7D%0A%09%09%7D catch (IOException e1) %7B%0A%09%09%09return %22ioexception %22+e1.getMessage();%0A%09%09%7D %0A%09%09try %0A%09%09%7B%0A%09%09%09JSONObject js = new JSONObject(new String(out));%0A%09%09%09return js.getString(%22postcode%22
 );%0A%09%09%7D
- 
+%0A%09%09
 catch (
-IO
+JSON
 Exce
@@ -1869,33 +1869,33 @@
 (JSONException e
-1
+2
 ) %7B%0A%09%09%09return %22i
@@ -1885,34 +1885,36 @@
 2) %7B%0A%09%09%09return %22
-io
+json
 exception %22+e1.g
@@ -1902,33 +1902,33 @@
 sonexception %22+e
-1
+2
 .getMessage();%0A%09
@@ -1920,24 +1920,45 @@
 getMessage()
++%22%5Cn%22+new String(out)
 ;%0A%09%09%7D%0A%09%7D%0A%0A%09p

Use json data from ukpostcodes.com