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.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
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;

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

        Log.d(TAG, "Starting location backend via looper");

        Looper looper = getApplication().getMainLooper();
        final Context applicationContext = getApplicationContext();

        Handler handler = new Handler(looper) {
            public void handleMessage(Message msg) {
                Log.d(TAG, "Starting location backend in \"UI\" thread");
                CellbasedLocationProvider.getInstance().init(applicationContext);
                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();
                } finally {
                    try { lock.unlock(); } catch (Exception e) {}
                }
            }
        };
        handler.handleMessage(new Message());
    }

    protected void onClose() {
        super.onClose();
        try {
            lock.lock();
            if (worker != null) worker.interrupt();
        } finally {
            try { lock.unlock(); } catch (Exception e) {}
        }
    }
}

@@ -268,103 +268,50 @@
 oid.
-content.Context;%0Aimport android.os.Handler;%0Aimport android.os.Looper;%0Aimport android.os.Message
+app.Service;%0Aimport android.content.Intent
 ;%0Aim
@@ -550,356 +550,144 @@
     
-protected void onOpen() %7B%0A        super.onOpen();%0A%0A        Log.d(TAG, %22Starting location backend via looper%22);%0A%0A        Looper looper = getApplication().getMainLooper();%0A        final Context applicationContext = getApplicationContext();%0A%0A        Handler handler = new Handler(looper) %7B%0A            public void handleMessage(Message msg) %7B%0A        
+@Override%0A    public int onStartCommand(Intent intent, int flags, int startId) %7B%0A        super.onStartCommand(intent, flags, startId);%0A%0A
     
@@ -731,37 +731,12 @@
 kend
- in %5C%22UI%5C%22 thread%22);%0A        
+%22);%0A
     
@@ -784,17 +784,20 @@
 ().init(
-a
+getA
 pplicati
@@ -805,16 +805,18 @@
 nContext
+()
 );%0A     
@@ -818,38 +818,22 @@
         
-        
 try %7B%0A
-        
         
@@ -857,32 +857,24 @@
             
-        
 if (worker !
@@ -909,32 +909,24 @@
             
-        
 worker = new
@@ -953,24 +953,16 @@
         
-        
 public v
@@ -989,32 +989,24 @@
             
-        
 Log.d(TAG, %22
@@ -1049,32 +1049,24 @@
             
-        
 CellbasedLoc
@@ -1084,24 +1084,16 @@
 er lp =%0A
-        
         
@@ -1165,32 +1165,24 @@
             
-        
 double lastL
@@ -1206,32 +1206,24 @@
             
-        
 double lastL
@@ -1247,32 +1247,24 @@
             
-        
 try %7B while 
@@ -1268,32 +1268,24 @@
 le (true) %7B%0A
-        
             
@@ -1337,32 +1337,24 @@
             
-        
 CellInfo%5B%5D i
@@ -1394,32 +1394,24 @@
             
-        
 if (infos.le
@@ -1452,32 +1452,24 @@
             
-        
 double lng =
@@ -1493,32 +1493,24 @@
             
-        
 double lat =
@@ -1538,24 +1538,16 @@
         
-        
 for(Cell
@@ -1560,32 +1560,24 @@
  : infos) %7B%0A
-        
             
@@ -1630,32 +1630,24 @@
             
-        
 lat += c.lat
@@ -1672,34 +1672,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -1739,32 +1739,24 @@
             
-        
 lat /= infos
@@ -1788,24 +1788,16 @@
         
-        
 float ac
@@ -1850,32 +1850,24 @@
             
-        
 if (lng != l
@@ -1890,32 +1890,24 @@
  lastLat) %7B%0A
-        
             
@@ -1994,32 +1994,24 @@
             
-        
 lastLng = ln
@@ -2009,32 +2009,24 @@
 tLng = lng;%0A
-        
             
@@ -2052,32 +2052,24 @@
 tLat = lat;%0A
-        
             
@@ -2161,34 +2161,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -2217,32 +2217,24 @@
 ption e) %7B%7D%0A
-        
             
@@ -2251,35 +2251,19 @@
         
-        %7D;%0A        
+%7D;%0A
         
@@ -2290,24 +2290,16 @@
         
-        
 %7D finall
@@ -2298,32 +2298,24 @@
 %7D finally %7B%0A
-        
             
@@ -2372,87 +2372,46 @@
     
-        %7D%0A            %7D%0A        %7D;%0A        handler.handleMessage(new Message())
+%7D%0A%0A        return Service.START_STICKY
 ;%0A  

Move startup code to startup method