From 631057cd5fcec746ce90e289a2c61a29487d3b32 Mon Sep 17 00:00:00 2001 From: Arndt Date: Sat, 26 Jul 2014 12:25:44 +0200 Subject: [PATCH] extended configuration (to hande Kitkat issues) --- .../java/btools/router/RoutingHelper.java | 58 +++++++++++++++ .../main/java/btools/mapcreator/OsmNodeP.java | 4 -- .../java/btools/mapaccess/NodesCache.java | 29 ++++++-- .../btools/mapaccess/StorageConfigHelper.java | 55 ++++++++++++++ brouter-routing-app/assets/segments3.zip | Bin 0 -> 496 bytes .../java/btools/routingapp/AppLogger.java | 68 ++++++++++++++++++ .../btools/routingapp/BInstallerView.java | 14 +++- .../btools/routingapp/BRouterService.java | 4 +- .../java/btools/routingapp/BRouterView.java | 33 +++------ .../btools/routingapp/CoordinateReader.java | 26 ++++++- 10 files changed, 251 insertions(+), 40 deletions(-) create mode 100644 brouter-core/src/main/java/btools/router/RoutingHelper.java create mode 100644 brouter-mapaccess/src/main/java/btools/mapaccess/StorageConfigHelper.java create mode 100644 brouter-routing-app/assets/segments3.zip create mode 100644 brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java diff --git a/brouter-core/src/main/java/btools/router/RoutingHelper.java b/brouter-core/src/main/java/btools/router/RoutingHelper.java new file mode 100644 index 0000000..6edea02 --- /dev/null +++ b/brouter-core/src/main/java/btools/router/RoutingHelper.java @@ -0,0 +1,58 @@ +/** + * static helper class for handling datafiles + * + * @author ab + */ +package btools.router; + +import java.io.File; + +import btools.mapaccess.StorageConfigHelper; + +public final class RoutingHelper +{ + public static File getAdditionalMaptoolDir( String segmentDir ) + { + return StorageConfigHelper.getAdditionalMaptoolDir(segmentDir); + } + + public static File getSecondarySegmentDir( String segmentDir ) + { + return StorageConfigHelper.getSecondarySegmentDir(segmentDir); + } + + + public static boolean hasDirectoryAnyDatafiles( String segmentDir ) + { + if ( hasAnyDatafiles( new File( segmentDir ) ) ) + { + return true; + } + // check secondary, too + File secondary = StorageConfigHelper.getSecondarySegmentDir( segmentDir ); + if ( secondary != null ) + { + return hasAnyDatafiles( secondary ); + } + return false; + } + + private static boolean hasAnyDatafiles( File dir ) + { + String[] fileNames = dir.list(); + for( String fileName : fileNames ) + { + if ( fileName.endsWith( ".rd5" ) ) return true; + } + File carSubset = new File( dir, "carsubset" ); + if ( carSubset.isDirectory() ) + { + fileNames = carSubset.list(); + for( String fileName : fileNames ) + { + if ( fileName.endsWith( ".cd5" ) ) return true; + } + } + return false; + } +} diff --git a/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java b/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java index 0d5fb69..52bef07 100644 --- a/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java +++ b/brouter-map-creator/src/main/java/btools/mapcreator/OsmNodeP.java @@ -5,12 +5,8 @@ */ package btools.mapcreator; -import java.io.ByteArrayOutputStream; -import java.io.DataOutputStream; import java.io.IOException; -import sun.security.pkcs.SigningCertificateInfo; - import btools.util.ByteDataWriter; public class OsmNodeP implements Comparable diff --git a/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java b/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java index 612ca1f..2b8d40f 100644 --- a/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java +++ b/brouter-mapaccess/src/main/java/btools/mapaccess/NodesCache.java @@ -13,7 +13,9 @@ import java.util.List; public final class NodesCache { - private String segmentDir; + private File segmentDir; + private File secondarySegmentsDir = null; + private OsmNodesMap nodesMap; private int lookupVersion; private int lookupMinorVersion; @@ -33,21 +35,25 @@ public final class NodesCache private long cacheSum = 0; private boolean garbageCollectionEnabled = false; + public NodesCache( String segmentDir, OsmNodesMap nodesMap, int lookupVersion, int minorVersion, boolean varLen, boolean carMode, NodesCache oldCache ) { - this.segmentDir = segmentDir; + this.segmentDir = new File( segmentDir ); this.nodesMap = nodesMap; this.lookupVersion = lookupVersion; this.lookupMinorVersion = minorVersion; this.readVarLength = varLen; this.carMode = carMode; + if ( !this.segmentDir.isDirectory() ) throw new RuntimeException( "segment directory " + segmentDir + " does not exist" ); + if ( oldCache != null ) { fileCache = oldCache.fileCache; iobuffer = oldCache.iobuffer; oom_carsubset_hint = oldCache.oom_carsubset_hint; + secondarySegmentsDir = oldCache.secondarySegmentsDir; // re-use old, virgin caches fileRows = oldCache.fileRows; @@ -65,8 +71,20 @@ public final class NodesCache fileCache = new HashMap(4); fileRows = new OsmFile[180][]; iobuffer = new byte[65636]; + secondarySegmentsDir = StorageConfigHelper.getSecondarySegmentDir( segmentDir ); } } + + private File getFileFromSegmentDir( String filename ) + { + File f = new File( segmentDir, filename ); + if ( secondarySegmentsDir != null && !f.exists() ) + { + File f2 = new File( secondarySegmentsDir, filename ); + if ( f2.exists() ) return f2; + } + return f; + } // if the cache sum exceeded a threshold, // clean all ghosts and enable garbage collection @@ -185,9 +203,6 @@ public final class NodesCache private OsmFile fileForSegment( int lonDegree, int latDegree ) throws Exception { - File base = new File( segmentDir ); - if ( !base.isDirectory() ) throw new RuntimeException( "segment directory " + segmentDir + " does not exist" ); - int lonMod5 = lonDegree % 5; int latMod5 = latDegree % 5; int tileIndex = lonMod5 * 5 + latMod5; @@ -207,12 +222,12 @@ public final class NodesCache File f = null; if ( carMode ) { - File carFile = new File( new File( base, "carsubset" ), filenameBase + ".cd5" ); + File carFile = getFileFromSegmentDir( "carsubset/" + filenameBase + ".cd5" ); if ( carFile.exists() ) f = carFile; } if ( f == null ) { - File fullFile = new File( base, filenameBase + ".rd5" ); + File fullFile = getFileFromSegmentDir( filenameBase + ".rd5" ); if ( fullFile.exists() ) f = fullFile; if ( carMode && f != null ) oom_carsubset_hint = true; } diff --git a/brouter-mapaccess/src/main/java/btools/mapaccess/StorageConfigHelper.java b/brouter-mapaccess/src/main/java/btools/mapaccess/StorageConfigHelper.java new file mode 100644 index 0000000..6e2a382 --- /dev/null +++ b/brouter-mapaccess/src/main/java/btools/mapaccess/StorageConfigHelper.java @@ -0,0 +1,55 @@ +/** + * Access to the storageconfig.txt config file + * + * @author ab + */ +package btools.mapaccess; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +public class StorageConfigHelper +{ + public static File getSecondarySegmentDir( String segmentDir ) + { + return getStorageLocation( segmentDir, "secondary_segment_dir=" ); + } + + public static File getAdditionalMaptoolDir( String segmentDir ) + { + return getStorageLocation( segmentDir, "additional_maptool_dir=" ); + } + + private static File getStorageLocation( String segmentDir, String tag ) + { + File res = null; + BufferedReader br = null; + String configFile = segmentDir + "/storageconfig.txt"; + try + { + br = new BufferedReader( new FileReader (configFile ) ); + for(;;) + { + String line = br.readLine(); + if ( line == null ) break; + line = line.trim(); + if ( line.startsWith( "#") ) continue; + if ( line.startsWith( tag ) ) + { + String path = line.substring( tag.length() ).trim(); + res = path.startsWith( "/" ) ? new File( path ) : new File( new File( segmentDir ) , path ); + if ( !res.exists() ) res = null; + break; + } + } + } + catch( Exception e ) {} + finally + { + if ( br != null ) try { br.close(); } catch( Exception ee ) {} + } + return res; + } + +} diff --git a/brouter-routing-app/assets/segments3.zip b/brouter-routing-app/assets/segments3.zip new file mode 100644 index 0000000000000000000000000000000000000000..4d25c44c61668c33cfd824d31660b2ba071446e9 GIT binary patch literal 496 zcmWIWW@Zs#U}E54P*3{hl7H^d{~SgJh679t41x>{48GHSuLZZq#TKGt9CbFCzMT&$gP4JW*?`uTg(snyP#UpmU&tKJ!< z7dGK!;g=bGRj)F8CLL^NQDoz^;BmiEyum$jf-EDyoDEl4l#5@)Z@D$aD!NsT$D6hU zi5&3LIJo7#@GpVt7ef6XV)9>|6}{TpXq2GzqM~cxwKCftB6X74?Iy1T{|DF2_*9?|VQzvni z`8lp|dajY~tafTr)8qq&w_kjJApeY z<;3exjgzn6%@i@e+;_8Pq1r@K>sq_qkcCqNH-AgL$m7pqwSnb(c}QSM@a1_|?|d)m z+qzNLbmEd{YqCH2-n>!tkvqVfkx7IBcjN%$2M83vB$lWO@MdKL$uR<Laj8_H* E01w;Hc>n+a literal 0 HcmV?d00001 diff --git a/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java b/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java new file mode 100644 index 0000000..7e75fef --- /dev/null +++ b/brouter-routing-app/src/main/java/btools/routingapp/AppLogger.java @@ -0,0 +1,68 @@ +package btools.routingapp; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Date; + +import android.os.Environment; + + +/** + * static logger interface to be used in the android app + */ +public class AppLogger +{ + private static FileWriter debugLogWriter = null; + private static boolean initDone = false; + + private static void init() + { + try + { + // open logfile if existing + File sd = Environment.getExternalStorageDirectory(); + if ( sd == null ) return; + File debugLog = new File( sd, "brouterapp.txt" ); + if ( debugLog.exists() ) + { + debugLogWriter = new FileWriter( debugLog, true ); + } + } + catch( IOException ioe ) {} + } + + /** + * log an info trace to the app log file, if any + */ + public static boolean isLogging() + { + if ( !initDone ) + { + initDone = true; + init(); + log( "logging started at " + new Date() ); + } + return debugLogWriter != null; + } + + /** + * log an info trace to the app log file, if any + */ + public static void log( String msg ) + { + if ( isLogging() ) + { + try + { + debugLogWriter.write( msg ); + debugLogWriter.write( '\n' ); + debugLogWriter.flush(); + } + catch( IOException e ) + { + throw new RuntimeException( "cannot write appdebug.txt: " + e ); + } + } + } +} diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java index 9ef2416..258dada 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BInstallerView.java @@ -25,6 +25,7 @@ import android.view.MotionEvent; import android.view.View; import android.widget.Toast; import btools.mapaccess.PhysicalFile; +import btools.router.RoutingHelper; public class BInstallerView extends View { @@ -209,8 +210,15 @@ public class BInstallerView extends View { clearTileSelection( MASK_INSTALLED_CD5 | MASK_INSTALLED_RD5 ); - scanExistingFiles( new File( baseDir + "/brouter/segments2" ), ".rd5", MASK_INSTALLED_RD5 ); - scanExistingFiles( new File( baseDir + "/brouter/segments2/carsubset" ), ".cd5", MASK_INSTALLED_CD5 ); + scanExistingFiles( new File( baseDir + "/brouter/segments3" ), ".rd5", MASK_INSTALLED_RD5 ); + scanExistingFiles( new File( baseDir + "/brouter/segments3/carsubset" ), ".cd5", MASK_INSTALLED_CD5 ); + + File secondary = RoutingHelper.getSecondarySegmentDir( baseDir + "/brouter/segments3" ); + if ( secondary != null ) + { + scanExistingFiles( secondary, ".rd5", MASK_INSTALLED_RD5 ); + scanExistingFiles( new File( secondary, "carsubset" ), ".cd5", MASK_INSTALLED_CD5 ); + } StatFs stat = new StatFs(baseDir); availableSize = (long)stat.getAvailableBlocks()*stat.getBlockSize(); @@ -599,7 +607,7 @@ float tx, ty; input = connection.getInputStream(); int slidx = surl.lastIndexOf( "segments3/" ); - fname = baseDir + "/brouter/segments2/" + surl.substring( slidx+10 ); + fname = baseDir + "/brouter/segments3/" + surl.substring( slidx+10 ); tmp_file = new File( fname + "_tmp" ); if ( new File( fname ).exists() ) return "internal error: file exists: " + fname; output = new FileOutputStream( tmp_file ); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java index 634427a..40f4fd4 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterService.java @@ -57,7 +57,7 @@ public class BRouterService extends Service { String modesFile = baseDir + "/brouter/modes/serviceconfig.dat"; br = new BufferedReader( new FileReader (modesFile ) ); - worker.segmentDir = baseDir + "/brouter/segments2"; + worker.segmentDir = baseDir + "/brouter/segments3"; for(;;) { String line = br.readLine(); @@ -67,7 +67,7 @@ public class BRouterService extends Service worker.profilePath = baseDir + "/brouter/profiles2/" + smc.profile + ".brf"; worker.rawTrackPath = baseDir + "/brouter/modes/" + mode_key + "_rawtrack.dat"; - CoordinateReader cor = CoordinateReader.obtainValidReader( baseDir ); + CoordinateReader cor = CoordinateReader.obtainValidReader( baseDir, worker.segmentDir ); worker.nogoList = new ArrayList(); // veto nogos by profiles veto list for(OsmNodeNamed nogo : cor.nogopoints ) diff --git a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java index 337fc56..aa4c652 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/BRouterView.java @@ -11,6 +11,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; @@ -36,6 +37,7 @@ import btools.router.OsmNodeNamed; import btools.router.OsmTrack; import btools.router.RoutingContext; import btools.router.RoutingEngine; +import btools.router.RoutingHelper; public class BRouterView extends View { @@ -71,6 +73,7 @@ public class BRouterView extends View private int[] imgPixels; + public void startSimulation() { } @@ -138,18 +141,19 @@ public class BRouterView extends View ConfigHelper.writeBaseDir( getContext(), baseDir ); } String basedir = fbd.getAbsolutePath(); + AppLogger.log( "using basedir: " + basedir ); // create missing directories assertDirectoryExists( "project directory", basedir + "/brouter", null ); - segmentDir = basedir + "/brouter/segments2"; - assertDirectoryExists( "data directory", segmentDir, null ); + segmentDir = basedir + "/brouter/segments3"; + assertDirectoryExists( "data directory", segmentDir, "segments3.zip" ); assertDirectoryExists( "carsubset directory", segmentDir + "/carsubset", null ); profileDir = basedir + "/brouter/profiles2"; assertDirectoryExists( "profile directory", profileDir, "profiles2.zip" ); modesDir = basedir + "/brouter/modes"; assertDirectoryExists( "modes directory", modesDir, "modes.zip" ); - cor = CoordinateReader.obtainValidReader( basedir ); + cor = CoordinateReader.obtainValidReader( basedir, segmentDir ); wpList = cor.waypoints; nogoList = cor.nogopoints; nogoVetoList = new ArrayList(); @@ -176,23 +180,7 @@ public class BRouterView extends View } } - boolean segmentFound = false; - String[] fileNames = new File( segmentDir ).list(); - for( String fileName : fileNames ) - { - if ( fileName.endsWith( ".rd5" ) ) segmentFound = true; - } - File carSubset = new File( segmentDir, "carsubset" ); - if ( carSubset.isDirectory() ) - { - fileNames = carSubset.list(); - for( String fileName : fileNames ) - { - if ( fileName.endsWith( ".cd5" ) ) segmentFound = true; - } - } - - fileNames = new File( profileDir ).list(); + String[] fileNames = new File( profileDir ).list(); ArrayList profiles = new ArrayList(); boolean lookupsFound = false; @@ -216,7 +204,7 @@ public class BRouterView extends View + " contains no routing profiles (*.brf)." + " see www.dr-brenschede.de/brouter for setup instructions." ); } - if ( !segmentFound ) + if ( !RoutingHelper.hasDirectoryAnyDatafiles( segmentDir ) ) { ((BRouterActivity)getContext()).startDownloadManager(); waitingForSelection = true; @@ -295,7 +283,6 @@ public class BRouterView extends View String[] wpts = new String[allpoints.size()]; int i = 0; for( OsmNodeNamed wp : allpoints.values() ) wpts[i++] = wp.name; -System.out.println( "calling selectWaypoint..." ); ((BRouterActivity)getContext()).selectWaypoint( wpts ); } } @@ -304,7 +291,6 @@ System.out.println( "calling selectWaypoint..." ); { wpList.add( cor.allpoints.get( waypoint ) ); cor.allpoints.remove( waypoint ); -System.out.println( "updateWaypointList: " + waypoint + " wpList.size()=" + wpList.size() ); } public void finishWaypointSelection() @@ -410,6 +396,7 @@ System.out.println( "updateWaypointList: " + waypoint + " wpList.size()=" + wpLi } } + private void assertDirectoryExists( String message, String path, String assetZip ) { File f = new File( path ); diff --git a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java index ff12e86..e730324 100644 --- a/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java +++ b/brouter-routing-app/src/main/java/btools/routingapp/CoordinateReader.java @@ -2,12 +2,14 @@ package btools.routingapp; import java.io.File; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Environment; import btools.router.OsmNodeNamed; +import btools.router.RoutingHelper; /** * Read coordinates from a gpx-file @@ -96,7 +98,7 @@ public abstract class CoordinateReader protected abstract void readPointmap() throws Exception; - public static CoordinateReader obtainValidReader( String basedir ) throws Exception + public static CoordinateReader obtainValidReader( String basedir, String segmentDir ) throws Exception { CoordinateReader cor = null; ArrayList rl = new ArrayList(); @@ -117,10 +119,32 @@ public abstract class CoordinateReader } } + // eventually add explicit directory + File additional = RoutingHelper.getAdditionalMaptoolDir(segmentDir); + if ( additional != null ) + { + String base3 = additional.getAbsolutePath(); + + AppLogger.log( "additional maptool-base from storage-config: " + base3 ); + + rl.add( new CoordinateReaderOsmAnd(base3) ); + rl.add( new CoordinateReaderLocus(base3) ); + rl.add( new CoordinateReaderOrux(base3) ); + } + long tmax = 0; for( CoordinateReader r : rl ) { long t = r.getTimeStamp(); + + if ( t != 0 ) + { + if ( AppLogger.isLogging() ) + { + AppLogger.log( "found coordinate source at " + r.basedir + r.rootdir + " with timestamp " + new Date( t ) ); + } + } + if ( t > tmax ) { tmax = t;