diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 3a75558fe5..ecbd2293df 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -67,6 +67,7 @@ import net.osmand.plus.voice.MediaCommandPlayerImpl; import net.osmand.plus.voice.TTSCommandPlayerImpl; import net.osmand.plus.wikivoyage.data.TravelDbHelper; import net.osmand.plus.wikivoyage.data.TravelHelper; +import net.osmand.plus.wikivoyage.data.TravelObfHelper; import net.osmand.render.RenderingRulesStorage; import net.osmand.router.RoutingConfiguration; import net.osmand.util.Algorithms; @@ -459,7 +460,7 @@ public class AppInitializer implements IProgress { app.mapViewTrackingUtilities = startupInit(new MapViewTrackingUtilities(app), MapViewTrackingUtilities.class); // TODOTRAVEL_OBF_HELPER check ResourceManager and use TravelObfHelper - app.travelHelper = new TravelDbHelper(app); + app.travelHelper = new TravelObfHelper(app); //new TravelDbHelper(app); app.travelHelper.initializeDataOnAppStartup(); app.travelHelper = startupInit(app.travelHelper, TravelHelper.class); diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 0069699dbd..eb88b16e52 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -636,6 +636,7 @@ public class ResourceManager { collectFiles(roadsPath, IndexConstants.BINARY_MAP_INDEX_EXT, files); if (Version.isPaidVersion(context)) { collectFiles(context.getAppPath(IndexConstants.WIKI_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files); + collectFiles(context.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files); } if (OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null || InAppPurchaseHelper.isSubscribedToLiveUpdates(context)) { collectFiles(context.getAppPath(IndexConstants.SRTM_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files); @@ -1134,7 +1135,7 @@ public class ResourceManager { return readers.toArray(new BinaryMapIndexReader[0]); } - public BinaryMapIndexReader[] getTravelFiles() { + public List getTravelFiles() { Collection fileReaders = getFileReaders(); List readers = new ArrayList<>(fileReaders.size()); for (BinaryMapReaderResource res : fileReaders) { @@ -1148,7 +1149,7 @@ public class ResourceManager { } } } - return readers.toArray(new BinaryMapIndexReader[0]); + return readers; } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java new file mode 100644 index 0000000000..205d2378c7 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -0,0 +1,211 @@ +package net.osmand.plus.wikivoyage.data; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import net.osmand.CollatorStringMatcher; +import net.osmand.GPXUtilities; +import net.osmand.IndexConstants; +import net.osmand.PlatformUtil; +import net.osmand.ResultMatcher; +import net.osmand.binary.BinaryMapIndexReader; +import net.osmand.data.Amenity; +import net.osmand.data.MapObject; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.api.SQLiteAPI; +import net.osmand.util.Algorithms; + +import org.apache.commons.logging.Log; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class TravelObfHelper implements TravelHelper { + + private static final Log LOG = PlatformUtil.getLog(TravelObfHelper.class); + + private static final int POPULAR_LIMIT = 25; + + private final OsmandApplication application; + + private TravelLocalDataHelper localDataHelper; + private SQLiteAPI.SQLiteConnection connection = null; + + private File selectedTravelBook = null; + private List files; + private List existingTravelBooks = new ArrayList<>(); + private List popularArticles = new ArrayList(); + + + public TravelObfHelper(OsmandApplication application) { + this.application = application; + localDataHelper = new TravelLocalDataHelper(application); + } + + public TravelLocalDataHelper getBookmarksHelper() { + return localDataHelper; + } + + @Override + public boolean isAnyTravelBookPresent() { + return selectedTravelBook != null; + } + + public void initializeDataOnAppStartup() { + List files = getPossibleFiles(); + String travelBook = application.getSettings().SELECTED_TRAVEL_BOOK.get(); + existingTravelBooks.clear(); + if (files != null && !files.isEmpty()) { + for (File f : files) { + existingTravelBooks.add(f); + if (selectedTravelBook == null) { + selectedTravelBook = f; + } else if (Algorithms.objectEquals(travelBook, f.getName())) { + selectedTravelBook = f; + } + } + } else { + selectedTravelBook = null; + } + } + + @Nullable + private List getPossibleFiles() { + File[] files = application.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR).listFiles(); + if (files != null) { + List res = new ArrayList<>(); + for (File file : files) { + if (file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT)) { + res.add(file); + } + } + return res; + } + return null; + } + + public void initializeDataToDisplay() { + localDataHelper.refreshCachedData(); + loadPopularArticles(); + } + + + public String getSelectedTravelBookName() { + if (selectedTravelBook != null) { + return selectedTravelBook.getName(); + } + return null; + } + + public List getExistingTravelBooks() { + return existingTravelBooks; + } + + @NonNull + public List search(final String searchQuery) { + // TODO remove + this.files = application.getResourceManager().getTravelFiles(); + List res = new ArrayList<>(); + List searchObjects = new ArrayList<>(); + for (BinaryMapIndexReader reader : files) { + try { + BinaryMapIndexReader.SearchRequest searchRequest = BinaryMapIndexReader. + buildSearchPoiRequest(0, 0, searchQuery, + 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, null); + + searchObjects = reader.searchPoiByName(searchRequest); + } catch (IOException e) { + LOG.error(e); + } + } + for (MapObject obj : searchObjects) { + //TODO map + WikivoyageSearchResult r = new WikivoyageSearchResult(); + r.articleTitles = Collections.singletonList(obj.getName()); + r.langs = Collections.singletonList(obj.getName()); + r.imageTitle = (obj.getName()); + r.isPartOf = Collections.singletonList(obj.getName()); + r.routeId = "routeid";//obj.getId(); + res.add(r); + } + return res; + } + + @NonNull + public List getPopularArticles() { + return popularArticles; + } + + @Override + public Map> getNavigationMap(TravelArticle article) { + return null; + } + + @Override + public TravelArticle getArticleById(String routeId, String lang) { + return null; + } + + @Override + public TravelArticle getArticleByTitle(String title, String lang) { + return null; + } + + @Override + public String getArticleId(String title, String lang) { + return null; + } + + @Override + public ArrayList getArticleLangs(String articleId) { + return null; + } + + @NonNull + public List loadPopularArticles() { + popularArticles = new ArrayList<>(); + return popularArticles; + } + + public String formatTravelBookName(File tb) { + if (tb == null) { + return application.getString(R.string.shared_string_none); + } + String nm = tb.getName(); + return nm.substring(0, nm.indexOf('.')).replace('_', ' '); + } + + public String getGPXName(TravelArticle article) { + return article.getTitle().replace('/', '_').replace('\'', '_') + .replace('\"', '_') + IndexConstants.GPX_FILE_EXT; + } + + public File createGpxFile(TravelArticle article) { + final GPXUtilities.GPXFile gpx = article.getGpxFile(); + File file = application.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article)); + if (!file.exists()) { + GPXUtilities.writeGpxFile(file, gpx); + } + return file; + } + + // might use in future + protected static class PopularArticle { + String tripId; + String title; + String lang; + int popIndex; + int order; + double lat; + double lon; + + public boolean isLocationSpecified() { + return !Double.isNaN(lat) && !Double.isNaN(lon); + } + } +}