package org.gfd.gsmlocation;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.gfd.gsmlocation.model.CellInfo;
import org.microg.nlp.api.LocationBackendService;
import org.microg.nlp.api.LocationHelper;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

public class GSMService extends LocationBackendService {

    protected String TAG = "o.gfd.gsmlp.LocationBackendService";

    protected Lock lock = new ReentrantLock();
    protected Thread worker = null;

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "Binder ONBIND called");
        return super.onBind(intent);
    }

    public void start() {
        if (worker != null && worker.isAlive()) return;

        Log.d(TAG, "Starting location backend");
        Handler handler = new Handler(Looper.getMainLooper());
        final Context ctx = getApplicationContext();
        handler.post(new Runnable() {
            public void run() {
                CellbasedLocationProvider.getInstance().init(ctx);
            }
        });
        try {
            lock.lock();
            if (worker != null) worker.interrupt();
            worker = new Thread() {
                public void run() {
                    Log.d(TAG, "Starting reporter thread");
                    CellbasedLocationProvider lp =
                        CellbasedLocationProvider.getInstance();
                    double lastLng = 0d;
                    double lastLat = 0d;
                    try { while (true) {
                        Thread.sleep(1000);

                        CellInfo[] infos = lp.getAll();

                        if (infos.length == 0) continue;

                        double lng = 0d;
                        double lat = 0d;
                        for(CellInfo c : infos) {
                            lng += c.lng;
                            lat += c.lat;
                        }
                        lng /= infos.length;
                        lat /= infos.length;
                        float acc = (float)(800d / infos.length);
                        if (lng != lastLng || lat != lastLat) {
                            Log.d(TAG, "report (" + lat + "," + lng + ")");
                            lastLng = lng;
                            lastLat = lat;
                            report(LocationHelper.create("gsm", lat, lng, acc));
                        }
                    } } catch (InterruptedException e) {}
                }
            };
            worker.start();
        } catch (Exception e) {
            Log.e(TAG, "Start failed", e);
        } finally {
            try { lock.unlock(); } catch (Exception e) {}
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        start();

        return Service.START_STICKY;
    }

    @Override
    protected void onOpen() {
        super.onOpen();

        start();

        Log.d(TAG, "Binder OPEN called");
    }

    protected void onClose() {
        Log.d(TAG, "Binder CLOSE called");
        super.onClose();
        try {
            lock.lock();
            if (worker != null && worker.isAlive()) worker.interrupt();
            if (worker != null) worker = null;
        } finally {
            try { lock.unlock(); } catch (Exception e) {}
        }
    }
}

@@ -340,16 +340,50 @@
 Intent;%0A
+import android.location.Location;%0A
 import a
@@ -691,152 +691,58 @@
 ll;%0A
-%0A    @Override%0A    public IBinder onBind(Intent intent) %7B%0A        Log.d(TAG, %22Binder ONBIND called%22);%0A        return super.onBind(intent);%0A    %7D
+    protected CellbasedLocationProvider lp = null;
 %0A%0A  
@@ -1397,62 +1397,12 @@
     
-CellbasedLocationProvider lp =%0A                       
+lp =
  Cel
@@ -2765,147 +2765,434 @@
    p
-ublic int onStartCommand(Intent intent, int flags, int startId) %7B%0A        super.onStartCommand(intent, flags, startId);%0A%0A        start(
+rotected Location update() %7B%0A        CellInfo%5B%5D infos = lp.getAll();%0A%0A        if (infos.length == 0) return null;%0A%0A        double lng = 0d;%0A        double lat = 0d;%0A        for(CellInfo c : infos) %7B%0A            lng += c.lng;%0A            lat += c.lat;%0A        %7D%0A        lng /= infos.length;%0A        lat /= infos.length;%0A        float acc = (float)(800d / infos.length);%0A%0A        Log.d(TAG, %22update (%22 + lat + %22,%22 + lng + %22)%22
 );%0A
-%0A
     
@@ -3206,28 +3206,51 @@
 urn 
-Service.START_STICKY
+LocationHelper.create(%22gsm%22, lat, lng, acc)
 ;%0A  
@@ -3741,10 +3741,11 @@
 %7D%0A    %7D%0A
+%0A
 %7D%0A

Add an update method