package de.stephanlindauer.criticalmaps.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;

import de.stephanlindauer.criticalmaps.handler.PullServerHandler;
import de.stephanlindauer.criticalmaps.utils.TrackingInfoNotificationBuilder;

public class ServerSyncService extends Service {

    private final int PULL_OTHER_LOCATIONS_TIME = 12 * 1000; // 12 sec -> 5 times a minute

    private Timer timerPullServer;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        startForeground(TrackingInfoNotificationBuilder.NOTIFICATION_ID,
                TrackingInfoNotificationBuilder.getNotification(getApplication()));

        timerPullServer = new Timer();

        TimerTask timerTaskPullServer = new TimerTask() {
            @Override
            public void run() {
                new PullServerHandler().execute();
            }
        };
        timerPullServer.scheduleAtFixedRate(timerTaskPullServer, 0, PULL_OTHER_LOCATIONS_TIME);
    }

    @Override
    public void onDestroy() {
        timerPullServer.cancel();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        stopSelf();
        super.onTaskRemoved(rootIntent);
    }
}

@@ -120,21 +120,99 @@
 roid.os.
-IBind
+Build;%0Aimport android.os.Handler;%0Aimport android.os.IBinder;%0Aimport android.os.Loop
 er;%0A%0Aimp
@@ -1042,42 +1042,633 @@
     
-new PullServerHandler().execute();
+// Since JELLYBEAN AsyncTask makes sure it's started from%0A                // the UI thread. Before that we have do to that ourselves.%0A                if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.JELLY_BEAN) %7B%0A                    Handler handler = new Handler(Looper.getMainLooper());%0A                    handler.post(new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A                            new PullServerHandler().execute();%0A                        %7D%0A                    %7D);%0A                %7D else %7B%0A                    new PullServerHandler().execute();%0A                %7D
 %0A   

fix crash on ICS