package org.thaddeus.followme;

import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.widget.Toast;
import java.lang.NumberFormatException;


public class FollowService extends Service {
	
	// the default server is in a local network
	private final static String DEFAULT_SERVER = "192.168.0.15";

	private LocationManager locationManager = null;
	private MyLocationListener myListener;
	private String provider;
	
	// server name or IP
	private String server = DEFAULT_SERVER;
	// in minuts
	private int period = 60;
	
	@Override
	public void onCreate() {
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		//TODO use also button
		showMessage("Starting service");
		
		period = 0;
		Bundle bundle = intent.getBundleExtra("args");
		
		if(bundle != null) {
			server = bundle.getStringArrayList("serviceArgs").get(0);
			try {
		        period = Integer.parseInt(bundle.getStringArrayList("serviceArgs").get(1));
		    } catch(NumberFormatException e) {
				period = 0;
			}
		}
		
		if(period <= 0 || "".equals(server)) {
		    return START_NOT_STICKY;
		}
		
		if(period > 1440) {
		    // Period can be more than one day
		    period = 1440;
		}
		
		initGPS();
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		showMessage("Stopping service");
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	/** Start the tracking */
	private void initGPS() {
		if(locationManager==null) {
			locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
			Criteria criteria = new Criteria();
			criteria.setAccuracy(Criteria.ACCURACY_FINE);
			provider = locationManager.getBestProvider(criteria, true);
		}
		if(provider != null) {
			Location location = locationManager.getLastKnownLocation(provider);
		    myListener=new MyLocationListener(this, server);
		    
		    if(location!=null) {
				myListener.onLocationChanged(location);
			}
			// condition for updating pos : at least 20m and 2 minuts
			//locationManager.requestLocationUpdates(provider, period*60*1000, 20, myListener);
			//for testing
			locationManager.requestLocationUpdates(provider, 2*1000, 1, myListener);
		}
	}
	
	private void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

@@ -502,16 +502,99 @@
 8.0.15%22;
+%0A%09// the default period is an hour%0A%09private final static int DEFAULT_PERIOD = 60;%0A%09
 %0A%0A%09priva
@@ -752,25 +752,8 @@
 rver
- = DEFAULT_SERVER
 ;%0A%09/
@@ -783,21 +783,16 @@
 t period
- = 60
 ;%0A%09%0A%09@Ov
@@ -980,47 +980,28 @@
 d);%0A
+%0A
 %09%09
-//TODO use also button%0A%09%09showM
+String m
 essage
-(
+ = 
 %22Sta
@@ -1010,25 +1010,24 @@
 ing service%22
-)
 ;%0A%09%09%0A%09%09perio
@@ -1353,73 +1353,53 @@
 %3C= 0
- %7C%7C %22%22.equals(server)) %7B%0A%09%09    return START_NOT_STICKY;%0A%09%09%7D%0A%09%09%0A%09%09
+) %7B%0A%09%09    period = DEFAULT_PERIOD;%0A%09%09%7D else  
 if(p
@@ -1483,16 +1483,144 @@
 %0A%09%09%7D%0A%09%09%0A
+%09%09if (%22%22.equals(server)) %7B%0A%09%09    server = DEFAULT_SERVER;%0A%09%09%09message += %22%5Cnwith default server%22;%0A%09%09%7D%0A%09%09%0A%09%09showMessage(message);%0A
 %09%09initGP

Use defaults values if needed when starting service