package com.icecondor.nest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

//look at android.permission.RECEIVE_BOOT_COMPLETED

public class Pigeon extends Service {
	private static final long UPDATE_INTERVAL = 5000;
	private Timer timer = new Timer();
	static final String appTag = "IcePigeon";
	boolean on_switch = true;
	
	public void onCreate() {
		Log.i(appTag, "*** service created.");
		final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		timer.scheduleAtFixedRate(
			new TimerTask() {
				public void run() {
					Location fix;
					fix = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
					fix = new Location("phoney");
					pushLocation(fix);
				}
			}, 0, UPDATE_INTERVAL);		
	}
	
	public void onStart() {
		Log.i(appTag, "service started!");
	}
	
	@Override
	public IBinder onBind(Intent intent) {
       if (PigeonService.class.getName().equals(intent.getAction())) {
            return mSecondaryBinder;
        }
		return null;
	}
	
	public void pushLocation(Location fix) {
		String URL = "http://10.0.2.2/icecondor/locations"; // use preference
		try {
			Log.i(appTag, "sending fix: lat "+fix.getLatitude()+" long "+fix.getLongitude()+" alt "+fix.getAltitude());
			
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(URL);
			post.setEntity(buildPostParameters(fix));
			HttpResponse response;
			response = client.execute(post);
			Log.i(appTag, "http response: "+response.getStatusLine());
		} catch (NullPointerException t) {
			Log.i(appTag,"no data in location record "+t);
		} catch (ClientProtocolException e) {
			Log.i(appTag, "client protocol exception "+e);
			e.printStackTrace();
		} catch (HttpHostConnectException e) {
			Log.i(appTag, "connection failed "+e);
		} catch (IOException e) {
			Log.i(appTag, "IO exception "+e);
			e.printStackTrace();
		}
	}
	
	private UrlEncodedFormEntity buildPostParameters(Location fix) throws UnsupportedEncodingException {
		ArrayList <NameValuePair> dict = new ArrayList <NameValuePair>();
		dict.add(new BasicNameValuePair("location[latitude]", Double.toString(fix.getLatitude())));
		dict.add(new BasicNameValuePair("location[longitude]", Double.toString(fix.getLongitude())));
		dict.add(new BasicNameValuePair("location[altitude]", Double.toString(fix.getAltitude())));
		return new UrlEncodedFormEntity(dict, HTTP.UTF_8);
	}
	
    private final PigeonService.Stub mSecondaryBinder = new PigeonService.Stub() {
		public boolean isTransmitting() throws RemoteException {
			// TODO Auto-generated method stub
			return false;
		}
		public void startTransmitting() throws RemoteException {
			// TODO Auto-generated method stub
			
		}
		public void stopTransmitting() throws RemoteException {
			// TODO Auto-generated method stub
			
		}
    };


}

@@ -1118,15 +1118,8 @@
 itch
- = true
 ;%0A%09%0A
@@ -1351,24 +1351,47 @@
 oid run() %7B%0A
+%09%09%09%09%09if (on_switch) %7B%0A%09
 %09%09%09%09%09Locatio
@@ -1397,16 +1397,17 @@
 on fix;%0A
+%09
 %09%09%09%09%09fix
@@ -1478,24 +1478,25 @@
 IDER);%0A%09%09%09%09%09
+%09
 fix = new Lo
@@ -1518,16 +1518,17 @@
 );%0A%09%09%09%09%09
+%09
 pushLoca
@@ -1542,14 +1542,21 @@
 x);%0A
+%09
 %09%09%09%09%7D%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D
@@ -1580,16 +1580,36 @@
 VAL);%09%09%0A
+%09%09on_switch = true;%0A
 %09%7D%0A%09%0A%09pu
@@ -3396,58 +3396,24 @@
 %0A%09%09%09
-// TODO Auto-generated method stub%0A%09%09%09return false
+return on_switch
 ;%0A%09%09
@@ -3480,46 +3480,25 @@
 %0A%09%09%09
-// TODO Auto-generated method stub%0A%09%09%09
+on_switch = true;
 %0A%09%09%7D
@@ -3563,46 +3563,26 @@
 %0A%09%09%09
-// TODO Auto-generated method stub%0A%09%09%09
+on_switch = false;
 %0A%09%09%7D
@@ -3589,12 +3589,10 @@
 %0A    %7D;%0A
-%0A%0A
 %7D%0A

implement an On/Off switch in the pigeon service