From 620d1eeec1f2773a0be88ef1dc7066522bf33c34 Mon Sep 17 00:00:00 2001 From: crimean Date: Sat, 29 Sep 2018 12:38:41 +0300 Subject: [PATCH] Improve core sample startup time --- .../sample1/CurrentPositionHelper.java | 19 +++++++++++++++++-- .../sample1/search/QuickSearchHelper.java | 12 ++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java index db679b750b..351f1891d6 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/CurrentPositionHelper.java @@ -8,6 +8,7 @@ import net.osmand.PlatformUtil; import net.osmand.ResultMatcher; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion; +import net.osmand.binary.CachedOsmandIndexes; import net.osmand.binary.GeocodingUtilities; import net.osmand.binary.GeocodingUtilities.GeocodingResult; import net.osmand.binary.RouteDataObject; @@ -48,16 +49,30 @@ public class CurrentPositionHelper { File appPath = app.getAppPath(null); SampleUtils.collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files); + CachedOsmandIndexes cachedOsmandIndexes = new CachedOsmandIndexes(); + File indCache = app.getAppPath("ind_core.cache"); + if (indCache.exists()) { + try { + cachedOsmandIndexes.readFromFile(indCache, CachedOsmandIndexes.VERSION); + } catch (Exception e) { + } + } readers.clear(); for (File f : files) { try { - RandomAccessFile mf = new RandomAccessFile(f.getPath(), "r"); - BinaryMapIndexReader reader = new BinaryMapIndexReader(mf, f); + BinaryMapIndexReader reader = cachedOsmandIndexes.getReader(f); readers.add(reader); } catch (IOException e) { e.printStackTrace(); } } + if (files.size() > 0 && (!indCache.exists() || indCache.canWrite())) { + try { + cachedOsmandIndexes.writeToFile(indCache); + } catch (Exception e) { + log.error("Index file could not be written", e); + } + } } public Location getLastAskedLocation() { diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchHelper.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchHelper.java index fcc3fe644e..57a0edb8ed 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchHelper.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/search/QuickSearchHelper.java @@ -2,6 +2,7 @@ package net.osmand.core.samples.android.sample1.search; import net.osmand.IndexConstants; import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.binary.CachedOsmandIndexes; import net.osmand.core.samples.android.sample1.SampleApplication; import net.osmand.core.samples.android.sample1.SampleUtils; import net.osmand.search.SearchUICore; @@ -51,11 +52,18 @@ public class QuickSearchHelper { SampleUtils.collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files); SampleUtils.collectFiles(app.getAppPath(IndexConstants.WIKI_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files); + CachedOsmandIndexes cachedOsmandIndexes = new CachedOsmandIndexes(); + File indCache = app.getAppPath("ind_core.cache"); + if (indCache.exists()) { + try { + cachedOsmandIndexes.readFromFile(indCache, CachedOsmandIndexes.VERSION); + } catch (Exception e) { + } + } List readers = new ArrayList<>(); for (File f : files) { try { - RandomAccessFile mf = new RandomAccessFile(f.getPath(), "r"); - BinaryMapIndexReader reader = new BinaryMapIndexReader(mf, f); + BinaryMapIndexReader reader = cachedOsmandIndexes.getReader(f); readers.add(reader); } catch (IOException e) { e.printStackTrace();