package cgeo.geocaching.brouter;

import cgeo.geocaching.brouter.util.DefaultFilesUtils;
import cgeo.geocaching.utils.Log;

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

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;

public class InternalRoutingService extends Service {

    private final IInternalRoutingService.Stub myBRouterServiceStub = new IInternalRoutingService.Stub() {
        @Override
        public String getTrackFromParams(final Bundle params) {
            final BRouterWorker worker = new BRouterWorker();

            final String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/cgeo/routing";

            // should be:
            // final Folder base = PersistableFolder.ROUTING_BASE.getFolder();
            // worker.segmentDir = PersistableFolder.ROUTING_TILES.getFolder();

            final String fast = params.getString("fast");
            final boolean isFast = "1".equals(fast) || "true".equals(fast) || "yes".equals(fast);
            final String mode = params.getString("v");

            worker.profileFilename = params.getString("profile");
            if (StringUtils.isBlank(worker.profileFilename)) {
                return ""; // cannot calculate a route without a profile
            }

            worker.rawTrackPath = baseDir + "/" + mode + "_rawtrack.dat";
            worker.nogoList = new ArrayList<>();

            try {
                return worker.getTrackFromParams(params);
            } catch (IllegalArgumentException iae) {
                return iae.getMessage();
            }
        }
    };

    @Override
    public IBinder onBind(final Intent arg0) {
        Log.d(getClass().getSimpleName() + "onBind()");
        DefaultFilesUtils.checkDefaultFiles();
        return myBRouterServiceStub;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(getClass().getSimpleName() + "onCreate()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(getClass().getSimpleName() + "onDestroy()");
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        return START_STICKY;
    }

}

@@ -82,16 +82,56 @@
 sUtils;%0A
+import cgeo.geocaching.utils.FileUtils;%0A
 import c
@@ -246,39 +246,8 @@
 le;%0A
-import android.os.Environment;%0A
 impo
@@ -266,24 +266,45 @@
 s.IBinder;%0A%0A
+import java.io.File;%0A
 import java.
@@ -689,452 +689,212 @@
     
-final String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + %22/cgeo/routing%22;%0A%0A            // should be:%0A            // final Folder base = PersistableFolder.ROUTING_BASE.getFolder();%0A            // worker.segmentDir = PersistableFolder.ROUTING_TILES.getFolder();%0A%0A            final String fast = params.getString(%22fast%22);%0A            final boolean isFast = %221%22.equals(fast) %7C%7C %22true%22.equals(fast) %7C%7C %22yes%22.equals(fast);
+worker.profileFilename = params.getString(%22profile%22);%0A            if (StringUtils.isBlank(worker.profileFilename)) %7B%0A                return %22%22; // cannot calculate a route without a profile%0A            %7D%0A
 %0A   
@@ -945,17 +945,16 @@
 g(%22v%22);%0A
-%0A
         
@@ -968,205 +968,154 @@
 ker.
-profileFilename = params.getString(%22profile%22);%0A            if (StringUtils.isBlank(worker.profileFilename)) %7B%0A                return %22%22; // cannot calculate a route without a profile%0A            %7D%0A
+rawTrackPath = getApplicationContext().getFilesDir().getAbsolutePath() + %22/routing/%22;%0A            FileUtils.mkdirs(new File(worker.rawTrackPath));
 %0A   
@@ -1147,25 +1147,10 @@
 ath 
-= baseDir + %22/%22 
 +
+=
  mod
@@ -1170,16 +1170,17 @@
 k.dat%22;%0A
+%0A
         

move rawtrack files to internal storage (fix #10400)