From aab2e7d3a2d54bcfa8149c4c89780fb9f20715e1 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Tue, 5 Jan 2021 21:13:38 +0200 Subject: [PATCH 1/9] Fix travel last modified --- .../osmand/plus/wikivoyage/data/TravelLocalDataHelper.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java index 288b71dd88..2c59c16ed2 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelLocalDataHelper.java @@ -508,14 +508,15 @@ public class TravelLocalDataHelper { BOOKMARKS_COL_ROUTE_ID + " = ?, " + BOOKMARKS_COL_CONTENT_JSON + " = ?, " + BOOKMARKS_COL_CONTENT + " = ?, " + - BOOKMARKS_COL_LAST_MODIFIED + " = ?, " + + BOOKMARKS_COL_LAST_MODIFIED + " = ? " + "WHERE " + BOOKMARKS_COL_ARTICLE_TITLE + " = ? " + " AND " + BOOKMARKS_COL_ROUTE_ID + " = ?" + " AND " + BOOKMARKS_COL_LANG + " = ?" + " AND " + BOOKMARKS_COL_TRAVEL_BOOK + " = ?", new Object[]{newArticle.title, newArticle.lang, newArticle.aggregatedPartOf, - newArticle.imageTitle, travelBook, newArticle.lat, newArticle.lon, - newArticle.routeId, newArticle.content, newArticle.contentsJson, + newArticle.imageTitle, newArticle.getTravelBook(context), newArticle.lat, + newArticle.lon, newArticle.routeId, newArticle.contentsJson, newArticle.content, + newArticle.getLastModified(), odlArticle.title, odlArticle.routeId, odlArticle.lang, travelBook}); } finally { From 01938d8a45111a01661dee0338b0e814e9213e16 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Wed, 6 Jan 2021 19:32:13 +0200 Subject: [PATCH 2/9] Fix travel obf gpx --- .../main/java/net/osmand/data/Amenity.java | 19 +++++-- .../plus/wikivoyage/data/TravelObfHelper.java | 50 ++++++++++++++++++- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index e51f04df16..59b0ef9cfb 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -45,6 +45,8 @@ public class Amenity extends MapObject { public static final String CONTENT_JSON = "content_json"; public static final String ROUTE_ID = "route_id"; public static final String ROUTE_SOURCE = "route_source"; + public static final String COLOR = "color"; + public static final String LANG_YES = "lang_yes"; private String subType; @@ -250,6 +252,19 @@ public class Amenity extends MapObject { return l; } + public String getTagSuffix(String tagBegin) { + String tagSuffix = null; + for (String infoTag : getAdditionalInfoKeys()) { + if (infoTag.startsWith(tagBegin)) { + if (infoTag.length() > tagBegin.length()) { + tagSuffix = infoTag.substring(tagBegin.length()); + break; + } + } + } + return tagSuffix; + } + public String getTagContent(String tag, String lang) { if (lang != null) { String translateName = getAdditionalInfo(tag + ":" + lang); @@ -381,6 +396,4 @@ public class Amenity extends MapObject { } return a; } - - -} +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index 94a9d73033..5acfd4647a 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -20,6 +20,7 @@ import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.osm.PoiCategory; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.helpers.ColorDialogs; import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -48,6 +49,7 @@ public class TravelObfHelper implements TravelHelper { private static final Log LOG = PlatformUtil.getLog(TravelObfHelper.class); private static final String WORLD_WIKIVOYAGE_FILE_NAME = "World_wikivoyage.travel.obf"; public static final String ROUTE_ARTICLE = "route_article"; + public static final String ROUTE_ARTICLE_POINT = "route_article_point"; public static final int POPULAR_ARTICLES_SEARCH_RADIUS = 100000; public static final int ARTICLE_SEARCH_RADIUS = 50000; public static final int MAX_POPULAR_ARTICLES_COUNT = 100; @@ -92,9 +94,10 @@ public class TravelObfHelper implements TravelHelper { List amenities = reader.searchPoi(req); if (amenities.size() > 0) { for (Amenity amenity : amenities) { - if (!Algorithms.isEmpty(amenity.getName(lang))) { + if (amenity.getSubType().equals(ROUTE_ARTICLE) && !Algorithms.isEmpty(amenity.getName(lang))) { TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang); if (article != null) { + article.gpxFile = getGpxFile(article, amenities, lang); popularArticles.add(article); if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) { break; @@ -121,6 +124,48 @@ public class TravelObfHelper implements TravelHelper { return popularArticles; } + private GPXFile getGpxFile(TravelArticle article, List amenities, String lang) { + List list = new ArrayList<>(); + for (Amenity amenity : amenities) { + String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); + if (!lang.equals(amenityLang)) { + continue; + } + if (amenity.getAdditionalInfo(Amenity.ROUTE_ID) != null && + amenity.getAdditionalInfo(Amenity.ROUTE_ID).equals(article.routeId)) { + list.add(amenity); + } + } + GPXFile gpxFile = new GPXFile(null); + for (Amenity a : list) { + GPXUtilities.WptPt wptPt = createWptPt(lang, a); + gpxFile.addPoint(wptPt); + } + return gpxFile; + } + + private GPXUtilities.WptPt createWptPt(String lang, Amenity a) { + GPXUtilities.WptPt wptPt = new GPXUtilities.WptPt(); + wptPt.name = a.getName(); + wptPt.lat = a.getLocation().getLatitude(); + wptPt.lon = a.getLocation().getLongitude(); + wptPt.desc = a.getDescription(lang); + wptPt.link = a.getSite(); + String color = a.getAdditionalInfo("color"); + if (color != null) { + wptPt.setColor(ColorDialogs.getColorByTag(color)); + } + String iconName = a.getAdditionalInfo("gpx_icon"); + if (iconName != null) { + wptPt.setIconName(iconName); + } + String category = a.getTagSuffix("category_"); + if (category != null) { + wptPt.category = Algorithms.capitalizeFirstLetter(category); + } + return wptPt; + } + @Nullable private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang) { TravelArticle article = null; @@ -137,7 +182,8 @@ public class TravelObfHelper implements TravelHelper { return new SearchPoiTypeFilter() { @Override public boolean accept(PoiCategory type, String subcategory) { - return subcategory.equals(ROUTE_ARTICLE); + return subcategory.equals(ROUTE_ARTICLE) + || subcategory.equals(ROUTE_ARTICLE_POINT); } @Override From b86a69664342f9d8f43f42c80bb31fa0dea87d3f Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 8 Jan 2021 13:26:40 +0200 Subject: [PATCH 3/9] Fix travel obf gpx language --- .../main/java/net/osmand/data/Amenity.java | 10 ++ .../plus/wikivoyage/data/TravelObfHelper.java | 145 +++++++++--------- 2 files changed, 84 insertions(+), 71 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index 59b0ef9cfb..52b3233426 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -47,6 +47,7 @@ public class Amenity extends MapObject { public static final String ROUTE_SOURCE = "route_source"; public static final String COLOR = "color"; public static final String LANG_YES = "lang_yes"; + public static final String GPX_ICON = "gpx_icon"; private String subType; @@ -203,6 +204,15 @@ public class Amenity extends MapObject { setAdditionalInfo(PHONE, phone); } + public String getColor() { + return getAdditionalInfo(COLOR); + } + + public String getIcon() { + return getAdditionalInfo(GPX_ICON); + } + + public String getContentLanguage(String tag, String lang, String defLang) { if (lang != null) { String translateName = getAdditionalInfo(tag + ":" + lang); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index 5acfd4647a..4a0f9e4c9c 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -6,7 +6,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import net.osmand.Collator; -import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.IndexConstants; import net.osmand.OsmAndCollator; @@ -44,6 +43,8 @@ import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import static net.osmand.GPXUtilities.*; + public class TravelObfHelper implements TravelHelper { private static final Log LOG = PlatformUtil.getLog(TravelObfHelper.class); @@ -94,10 +95,9 @@ public class TravelObfHelper implements TravelHelper { List amenities = reader.searchPoi(req); if (amenities.size() > 0) { for (Amenity amenity : amenities) { - if (amenity.getSubType().equals(ROUTE_ARTICLE) && !Algorithms.isEmpty(amenity.getName(lang))) { - TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang); + if (ROUTE_ARTICLE.equals(amenity.getSubType()) && !Algorithms.isEmpty(amenity.getName(lang))) { + TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang, amenities); if (article != null) { - article.gpxFile = getGpxFile(article, amenities, lang); popularArticles.add(article); if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) { break; @@ -124,52 +124,10 @@ public class TravelObfHelper implements TravelHelper { return popularArticles; } - private GPXFile getGpxFile(TravelArticle article, List amenities, String lang) { - List list = new ArrayList<>(); - for (Amenity amenity : amenities) { - String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); - if (!lang.equals(amenityLang)) { - continue; - } - if (amenity.getAdditionalInfo(Amenity.ROUTE_ID) != null && - amenity.getAdditionalInfo(Amenity.ROUTE_ID).equals(article.routeId)) { - list.add(amenity); - } - } - GPXFile gpxFile = new GPXFile(null); - for (Amenity a : list) { - GPXUtilities.WptPt wptPt = createWptPt(lang, a); - gpxFile.addPoint(wptPt); - } - return gpxFile; - } - - private GPXUtilities.WptPt createWptPt(String lang, Amenity a) { - GPXUtilities.WptPt wptPt = new GPXUtilities.WptPt(); - wptPt.name = a.getName(); - wptPt.lat = a.getLocation().getLatitude(); - wptPt.lon = a.getLocation().getLongitude(); - wptPt.desc = a.getDescription(lang); - wptPt.link = a.getSite(); - String color = a.getAdditionalInfo("color"); - if (color != null) { - wptPt.setColor(ColorDialogs.getColorByTag(color)); - } - String iconName = a.getAdditionalInfo("gpx_icon"); - if (iconName != null) { - wptPt.setIconName(iconName); - } - String category = a.getTagSuffix("category_"); - if (category != null) { - wptPt.category = Algorithms.capitalizeFirstLetter(category); - } - return wptPt; - } - @Nullable - private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang) { + private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang, List amenityList) { TravelArticle article = null; - Map articles = readArticles(file, amenity); + Map articles = readArticles(file, amenity, amenityList); if (!Algorithms.isEmpty(articles)) { TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier(); cachedArticles.put(newArticleId, articles); @@ -182,8 +140,7 @@ public class TravelObfHelper implements TravelHelper { return new SearchPoiTypeFilter() { @Override public boolean accept(PoiCategory type, String subcategory) { - return subcategory.equals(ROUTE_ARTICLE) - || subcategory.equals(ROUTE_ARTICLE_POINT); + return subcategory.equals(ROUTE_ARTICLE) || subcategory.equals(ROUTE_ARTICLE_POINT); } @Override @@ -194,37 +151,76 @@ public class TravelObfHelper implements TravelHelper { } @NonNull - private Map readArticles(@NonNull File file, @NonNull Amenity amenity) { + private Map readArticles(@NonNull File file, @NonNull Amenity amenity, + @NonNull List amenityList) { Map articles = new HashMap<>(); Set langs = getLanguages(amenity); for (String lang : langs) { - articles.put(lang, readArticle(file, amenity, lang)); + articles.put(lang, readArticle(file, amenity, lang, amenityList)); } return articles; } @NonNull - private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @Nullable String lang) { + private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @Nullable String lang, + @NonNull List amenityList) { TravelArticle res = new TravelArticle(); res.file = file; String title = amenity.getName(lang); res.title = Algorithms.isEmpty(title) ? amenity.getName() : title; res.content = amenity.getDescription(lang); - res.isPartOf = emptyIfNull(amenity.getTagContent(Amenity.IS_PART, lang)); + res.isPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_PART, lang)); res.lat = amenity.getLocation().getLatitude(); res.lon = amenity.getLocation().getLongitude(); - res.imageTitle = emptyIfNull(amenity.getTagContent(Amenity.IMAGE_TITLE, null)); - res.routeId = emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null)); - res.routeSource = emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE, null)); + res.imageTitle = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IMAGE_TITLE, null)); + res.routeId = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null)); + res.routeSource = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE, null)); res.originalId = 0; res.lang = lang; - res.contentsJson = emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang)); - res.aggregatedPartOf = emptyIfNull(amenity.getTagContent(Amenity.IS_AGGR_PART, lang)); + res.contentsJson = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang)); + res.aggregatedPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_AGGR_PART, lang)); + res.gpxFile = getGpxFile(amenity, lang, amenityList); return res; } - private String emptyIfNull(String text) { - return text == null ? "" : text; + @NonNull + private GPXFile getGpxFile(@NonNull Amenity article, @Nullable String lang, @NonNull List amenityList) { + GPXFile gpxFile = new GPXFile(article.getName(), lang, ""); + for (Amenity amenity : amenityList) { + String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); + if (!lang.equals(amenityLang)) { + continue; + } + if (amenity.getAdditionalInfo(Amenity.ROUTE_ID) != null && + amenity.getAdditionalInfo(Amenity.ROUTE_ID).equals(article.getAdditionalInfo(Amenity.ROUTE_ID))) { + WptPt wptPt = createWptPt(amenity, lang); + gpxFile.addPoint(wptPt); + } + } + return gpxFile; + } + + @NonNull + private WptPt createWptPt(@NonNull Amenity amenity, @Nullable String lang) { + WptPt wptPt = new WptPt(); + wptPt.name = amenity.getName(); + wptPt.lat = amenity.getLocation().getLatitude(); + wptPt.lon = amenity.getLocation().getLongitude(); + wptPt.desc = amenity.getDescription(lang); + wptPt.link = amenity.getSite(); + String color = amenity.getColor(); + if (color != null) { + wptPt.setColor(ColorDialogs.getColorByTag(color)); + } + String iconName = amenity.getIcon(); + if (iconName != null) { + wptPt.setIconName(iconName); + } + String category = amenity.getTagSuffix("category_"); + if (category != null) { + wptPt.category = Algorithms.capitalizeFirstLetter(category); + } + return wptPt; } @Override @@ -257,7 +253,7 @@ public class TravelObfHelper implements TravelHelper { for (Amenity amenity : entry.getValue()) { Set nameLangs = getLanguages(amenity); if (nameLangs.contains(appLang)) { - TravelArticle article = readArticle(file, amenity, appLang); + TravelArticle article = readArticle(file, amenity, appLang, entry.getValue()); WikivoyageSearchResult r = new WikivoyageSearchResult(article, new ArrayList<>(nameLangs)); res.add(r); } @@ -373,7 +369,7 @@ public class TravelObfHelper implements TravelHelper { for (Amenity amenity : entry.getValue()) { Set nameLangs = getLanguages(amenity); if (nameLangs.contains(lang)) { - TravelArticle a = readArticle(file, amenity, lang); + TravelArticle a = readArticle(file, amenity, lang, entry.getValue()); WikivoyageSearchResult rs = new WikivoyageSearchResult(a, new ArrayList<>(nameLangs)); List l = navMap.get(rs.isPartOf); if (l == null) { @@ -477,9 +473,7 @@ public class TravelObfHelper implements TravelHelper { } catch (IOException e) { LOG.error(e.getMessage()); } - if (!amenities.isEmpty()) { - article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang); - } + article = getFirstTravelArticle(lang, article, amenities, reader.getFile()); } return article; } @@ -524,8 +518,19 @@ public class TravelObfHelper implements TravelHelper { } catch (IOException e) { LOG.error(e.getMessage()); } - if (!Algorithms.isEmpty(amenities)) { - article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang); + article = getFirstTravelArticle(lang, article, amenities, reader.getFile()); + } + return article; + } + + private TravelArticle getFirstTravelArticle(@NonNull String lang, TravelArticle article, List amenities, + File file) { + if (!Algorithms.isEmpty(amenities)) { + for (Amenity amenity : amenities) { + if (ROUTE_ARTICLE.equals(amenity.getSubType())) { + article = cacheTravelArticles(file, amenity, lang, amenities); + break; + } } } return article; @@ -591,9 +596,7 @@ public class TravelObfHelper implements TravelHelper { public File createGpxFile(@NonNull final TravelArticle article) { final GPXFile gpx = article.getGpxFile(); File file = app.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article)); - if (!file.exists()) { - GPXUtilities.writeGpxFile(file, gpx); - } + writeGpxFile(file, gpx); return file; } From 3f934ce1b280bc2647f22803ac2b5d3799d831de Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 8 Jan 2021 18:59:19 +0200 Subject: [PATCH 4/9] Refactoring obf gpx --- .../WikivoyageArticleDialogFragment.java | 1 + .../plus/wikivoyage/data/TravelArticle.java | 4 + .../plus/wikivoyage/data/TravelDbHelper.java | 5 + .../plus/wikivoyage/data/TravelHelper.java | 6 + .../plus/wikivoyage/data/TravelObfHelper.java | 112 ++++++++++++------ 5 files changed, 89 insertions(+), 39 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index 2d450eaa6b..20556832db 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -309,6 +309,7 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme } webViewClient.setArticle(article); articleToolbarText.setText(article.getTitle()); + article.setGpxFile(getMyApplication().getTravelHelper().getGpxFile(article, selectedLang)); if (article.getGpxFile() != null && article.getGpxFile().getPointsSize() > 0) { trackButton.setVisibility(View.VISIBLE); trackButton.setText(getString(R.string.shared_string_gpx_points) + " (" + article.getGpxFile().getPointsSize() + ")"); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java index 9a5aca4fa1..bfa87423f9 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java @@ -98,6 +98,10 @@ public class TravelArticle { return gpxFile; } + public void setGpxFile(GPXFile gpxFile) { + this.gpxFile = gpxFile; + } + public String getRouteId() { return routeId; } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java index 38380015c3..d36407441a 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java @@ -683,6 +683,11 @@ public class TravelDbHelper implements TravelHelper { return res; } + @Override + public GPXFile getGpxFile(@NonNull TravelArticle article, @Nullable String lang) { + return article.getGpxFile(); + } + public String formatTravelBookName(File tb) { if (tb == null) { return application.getString(R.string.shared_string_none); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java index 87b5bc1e95..20dcfac73e 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java @@ -3,6 +3,7 @@ package net.osmand.plus.wikivoyage.data; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import net.osmand.GPXUtilities; import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier; @@ -12,6 +13,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import static net.osmand.GPXUtilities.*; + public interface TravelHelper { TravelLocalDataHelper getBookmarksHelper(); @@ -49,6 +52,9 @@ public interface TravelHelper { @NonNull ArrayList getArticleLangs(@NonNull TravelArticleIdentifier articleId); + @Nullable + GPXFile getGpxFile(@NonNull TravelArticle article, @Nullable String lang); + @NonNull String getGPXName(@NonNull final TravelArticle article); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index c295000b7b..a28e138ee7 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -7,7 +7,6 @@ import androidx.annotation.Nullable; import net.osmand.Collator; import net.osmand.CollatorStringMatcher.StringMatcherMode; -import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.IndexConstants; import net.osmand.OsmAndCollator; @@ -98,8 +97,8 @@ public class TravelObfHelper implements TravelHelper { List amenities = reader.searchPoi(req); if (amenities.size() > 0) { for (Amenity amenity : amenities) { - if (ROUTE_ARTICLE.equals(amenity.getSubType()) && !Algorithms.isEmpty(amenity.getName(lang))) { - TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang, amenities); + if (!Algorithms.isEmpty(amenity.getName(lang))) { + TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang); if (article != null) { popularArticles.add(article); if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) { @@ -128,9 +127,9 @@ public class TravelObfHelper implements TravelHelper { } @Nullable - private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang, List amenityList) { + private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang) { TravelArticle article = null; - Map articles = readArticles(file, amenity, amenityList); + Map articles = readArticles(file, amenity); if (!Algorithms.isEmpty(articles)) { TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier(); cachedArticles.put(newArticleId, articles); @@ -139,11 +138,12 @@ public class TravelObfHelper implements TravelHelper { return article; } - SearchPoiTypeFilter getSearchRouteArticleFilter() { + @NonNull + private SearchPoiTypeFilter getSearchRouteArticleFilter() { return new SearchPoiTypeFilter() { @Override public boolean accept(PoiCategory type, String subcategory) { - return subcategory.equals(ROUTE_ARTICLE) || subcategory.equals(ROUTE_ARTICLE_POINT); + return subcategory.equals(ROUTE_ARTICLE); } @Override @@ -154,19 +154,32 @@ public class TravelObfHelper implements TravelHelper { } @NonNull - private Map readArticles(@NonNull File file, @NonNull Amenity amenity, - @NonNull List amenityList) { + private SearchPoiTypeFilter getSearchRouteArticlePointsFilter() { + return new SearchPoiTypeFilter() { + @Override + public boolean accept(PoiCategory type, String subcategory) { + return subcategory.equals(ROUTE_ARTICLE_POINT); + } + + @Override + public boolean isEmpty() { + return false; + } + }; + } + + @NonNull + private Map readArticles(@NonNull File file, @NonNull Amenity amenity) { Map articles = new HashMap<>(); Set langs = getLanguages(amenity); for (String lang : langs) { - articles.put(lang, readArticle(file, amenity, lang, amenityList)); + articles.put(lang, readArticle(file, amenity, lang)); } return articles; } @NonNull - private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @Nullable String lang, - @NonNull List amenityList) { + private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @Nullable String lang) { TravelArticle res = new TravelArticle(); res.file = file; String title = amenity.getName(lang); @@ -182,20 +195,17 @@ public class TravelObfHelper implements TravelHelper { res.lang = lang; res.contentsJson = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang)); res.aggregatedPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_AGGR_PART, lang)); - res.gpxFile = getGpxFile(amenity, lang, amenityList); return res; } - @NonNull - private GPXFile getGpxFile(@NonNull Amenity article, @Nullable String lang, @NonNull List amenityList) { - GPXFile gpxFile = new GPXFile(article.getName(), lang, ""); - for (Amenity amenity : amenityList) { - String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); - if (!lang.equals(amenityLang)) { - continue; - } - if (amenity.getAdditionalInfo(Amenity.ROUTE_ID) != null && - amenity.getAdditionalInfo(Amenity.ROUTE_ID).equals(article.getAdditionalInfo(Amenity.ROUTE_ID))) { + + @Override + public GPXFile getGpxFile(@NonNull TravelArticle article, @Nullable String lang) { + GPXFile gpxFile = null; + List pointList = getPointList(article, lang); + if (!Algorithms.isEmpty(pointList)) { + gpxFile = new GPXFile(article.getTitle(), lang, ""); + for (Amenity amenity : pointList) { WptPt wptPt = createWptPt(amenity, lang); gpxFile.addPoint(wptPt); } @@ -203,6 +213,39 @@ public class TravelObfHelper implements TravelHelper { return gpxFile; } + @Nullable + private List getPointList(@NonNull final TravelArticle article, @Nullable final String lang) { + final List pointList = new ArrayList<>(); + for (BinaryMapIndexReader reader : getReaders()) { + try { + final LatLon location = new LatLon(article.getLat(), article.getLon()); + SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( + location, POPULAR_ARTICLES_SEARCH_RADIUS, -1, getSearchRouteArticlePointsFilter(), + new ResultMatcher() { + @Override + public boolean publish(Amenity point) { + String amenityLang = point.getTagSuffix(Amenity.LANG_YES + ":"); + if (lang != null && lang.equals(amenityLang) + && point.getAdditionalInfo(Amenity.ROUTE_ID) != null + && point.getAdditionalInfo(Amenity.ROUTE_ID).equals(article.getRouteId())) { + pointList.add(point); + } + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + }); + reader.searchPoi(req); + } catch (Exception e) { + LOG.error(e.getMessage(), e); + } + } + return pointList; + } + @NonNull private WptPt createWptPt(@NonNull Amenity amenity, @Nullable String lang) { WptPt wptPt = new WptPt(); @@ -270,7 +313,7 @@ public class TravelObfHelper implements TravelHelper { for (Amenity amenity : entry.getValue()) { Set nameLangs = getLanguages(amenity); if (nameLangs.contains(appLang)) { - TravelArticle article = readArticle(file, amenity, appLang, entry.getValue()); + TravelArticle article = readArticle(file, amenity, appLang); ArrayList langs = new ArrayList<>(nameLangs); Collections.sort(langs, new Comparator() { @Override @@ -407,7 +450,7 @@ public class TravelObfHelper implements TravelHelper { for (Amenity amenity : entry.getValue()) { Set nameLangs = getLanguages(amenity); if (nameLangs.contains(lang)) { - TravelArticle a = readArticle(file, amenity, lang, entry.getValue()); + TravelArticle a = readArticle(file, amenity, lang); WikivoyageSearchResult rs = new WikivoyageSearchResult(a, new ArrayList<>(nameLangs)); List l = navMap.get(rs.isPartOf); if (l == null) { @@ -511,7 +554,9 @@ public class TravelObfHelper implements TravelHelper { } catch (IOException e) { LOG.error(e.getMessage()); } - article = getFirstTravelArticle(lang, article, amenities, reader.getFile()); + if (!Algorithms.isEmpty(amenities)) { + article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang); + } } return article; } @@ -556,19 +601,8 @@ public class TravelObfHelper implements TravelHelper { } catch (IOException e) { LOG.error(e.getMessage()); } - article = getFirstTravelArticle(lang, article, amenities, reader.getFile()); - } - return article; - } - - private TravelArticle getFirstTravelArticle(@NonNull String lang, TravelArticle article, List amenities, - File file) { - if (!Algorithms.isEmpty(amenities)) { - for (Amenity amenity : amenities) { - if (ROUTE_ARTICLE.equals(amenity.getSubType())) { - article = cacheTravelArticles(file, amenity, lang, amenities); - break; - } + if (!amenities.isEmpty()) { + article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang); } } return article; From f62cf097a0e7abe35f350ef1e7838a0fbee98439 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 8 Jan 2021 19:04:48 +0200 Subject: [PATCH 5/9] Refactoring obf gpx --- OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index a28e138ee7..1f7a2af18f 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -601,7 +601,7 @@ public class TravelObfHelper implements TravelHelper { } catch (IOException e) { LOG.error(e.getMessage()); } - if (!amenities.isEmpty()) { + if (!Algorithms.isEmpty(amenities)) { article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang); } } From 75e5f7828a0ee727075ed6b880f0fb73d77a002e Mon Sep 17 00:00:00 2001 From: max-klaus Date: Sun, 10 Jan 2021 14:12:50 +0300 Subject: [PATCH 6/9] Fix gpx reading for travel obf --- .../osmand/binary/BinaryMapIndexReader.java | 4 + .../main/java/net/osmand/data/Amenity.java | 14 +- .../wikivoyage/WikivoyageWebViewClient.java | 3 +- .../WikivoyageArticleDialogFragment.java | 8 +- .../plus/wikivoyage/data/TravelArticle.java | 12 +- .../plus/wikivoyage/data/TravelDbHelper.java | 7 +- .../plus/wikivoyage/data/TravelHelper.java | 6 - .../plus/wikivoyage/data/TravelObfHelper.java | 160 +++++++++--------- .../data/WikivoyageSearchResult.java | 6 +- 9 files changed, 99 insertions(+), 121 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java index 7486576b61..8743940a74 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java @@ -1769,6 +1769,10 @@ public class BinaryMapIndexReader { return limit != -1 && searchResults.size() > limit; } + public void setLimit(int limit) { + this.limit = limit; + } + public boolean isCancelled() { if (this.interrupted) { return interrupted; diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index 52b3233426..3ef6a54652 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -208,7 +208,7 @@ public class Amenity extends MapObject { return getAdditionalInfo(COLOR); } - public String getIcon() { + public String getGpxIcon() { return getAdditionalInfo(GPX_ICON); } @@ -262,17 +262,15 @@ public class Amenity extends MapObject { return l; } - public String getTagSuffix(String tagBegin) { - String tagSuffix = null; + public String getTagSuffix(String tagPrefix) { for (String infoTag : getAdditionalInfoKeys()) { - if (infoTag.startsWith(tagBegin)) { - if (infoTag.length() > tagBegin.length()) { - tagSuffix = infoTag.substring(tagBegin.length()); - break; + if (infoTag.startsWith(tagPrefix)) { + if (infoTag.length() > tagPrefix.length()) { + return infoTag.substring(tagPrefix.length()); } } } - return tagSuffix; + return null; } public String getTagContent(String tag, String lang) { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java index c2c42551e2..b102e718f6 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java @@ -14,6 +14,7 @@ import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.WptPt; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; @@ -119,7 +120,7 @@ public class WikivoyageWebViewClient extends WebViewClient { fragmentManager.popBackStackImmediate(); File path = app.getTravelHelper().createGpxFile(article); - GPXUtilities.GPXFile gpxFile = article.getGpxFile(); + GPXFile gpxFile = article.getGpxFile(); gpxFile.path = path.getAbsolutePath(); app.getSelectedGpxHelper().setGpxFileToDisplay(gpxFile); MapActivity.launchMapActivityMoveToTop(activity); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index 20556832db..9e2339f3ee 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -27,6 +27,8 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager.BackStackEntry; import net.osmand.AndroidUtils; +import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.GPXFile; import net.osmand.IndexConstants; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -309,10 +311,10 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme } webViewClient.setArticle(article); articleToolbarText.setText(article.getTitle()); - article.setGpxFile(getMyApplication().getTravelHelper().getGpxFile(article, selectedLang)); - if (article.getGpxFile() != null && article.getGpxFile().getPointsSize() > 0) { + GPXFile gpxFile = article.getGpxFile(); + if (gpxFile != null && gpxFile.getPointsSize() > 0) { trackButton.setVisibility(View.VISIBLE); - trackButton.setText(getString(R.string.shared_string_gpx_points) + " (" + article.getGpxFile().getPointsSize() + ")"); + trackButton.setText(getString(R.string.shared_string_gpx_points) + " (" + gpxFile.getPointsSize() + ")"); } else { trackButton.setVisibility(View.GONE); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java index bfa87423f9..0579af14cc 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java @@ -43,6 +43,7 @@ public class TravelArticle { String aggregatedPartOf; long lastModified; + boolean gpxFileRead; @NonNull public TravelArticleIdentifier generateIdentifier() { @@ -98,10 +99,6 @@ public class TravelArticle { return gpxFile; } - public void setGpxFile(GPXFile gpxFile) { - this.gpxFile = gpxFile; - } - public String getRouteId() { return routeId; } @@ -174,7 +171,6 @@ public class TravelArticle { @Nullable File file; double lat; double lon; - @Nullable String title; @Nullable String routeId; @Nullable String routeSource; @@ -198,7 +194,6 @@ public class TravelArticle { file = article.file; lat = article.lat; lon = article.lon; - title = article.title; routeId = article.routeId; routeSource = article.routeSource; } @@ -207,7 +202,6 @@ public class TravelArticle { public void writeToParcel(Parcel out, int flags) { out.writeDouble(lat); out.writeDouble(lon); - out.writeString(title); out.writeString(routeId); out.writeString(routeSource); out.writeString(file != null ? file.getAbsolutePath() : null); @@ -216,7 +210,6 @@ public class TravelArticle { private void readFromParcel(Parcel in) { lat = in.readDouble(); lon = in.readDouble(); - title = in.readString(); routeId = in.readString(); routeSource = in.readString(); String filePath = in.readString(); @@ -241,14 +234,13 @@ public class TravelArticle { TravelArticleIdentifier that = (TravelArticleIdentifier) o; return areLatLonEqual(that.lat, that.lon, lat, lon) && Algorithms.objectEquals(file, that.file) && - Algorithms.stringsEqual(title, that.title) && Algorithms.stringsEqual(routeId, that.routeId) && Algorithms.stringsEqual(routeSource, that.routeSource); } @Override public int hashCode() { - return Algorithms.hash(file, lat, lon, title, routeId, routeSource); + return Algorithms.hash(file, lat, lon, routeId, routeSource); } private static boolean areLatLonEqual(double lat1, double lon1, double lat2, double lon2) { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java index d36407441a..73bebdf9ad 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java @@ -448,7 +448,7 @@ public class TravelDbHelper implements TravelHelper { } } if (matchLang) { - prev.articleId.title = rs.getArticleTitle(); + prev.articleTitle = rs.getArticleTitle(); prev.isPartOf = rs.getIsPartOf(); } prev.langs.add(matchLang ? 0 : 1, rs.langs.get(0)); @@ -683,11 +683,6 @@ public class TravelDbHelper implements TravelHelper { return res; } - @Override - public GPXFile getGpxFile(@NonNull TravelArticle article, @Nullable String lang) { - return article.getGpxFile(); - } - public String formatTravelBookName(File tb) { if (tb == null) { return application.getString(R.string.shared_string_none); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java index 20dcfac73e..87b5bc1e95 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelHelper.java @@ -3,7 +3,6 @@ package net.osmand.plus.wikivoyage.data; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import net.osmand.GPXUtilities; import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier; @@ -13,8 +12,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import static net.osmand.GPXUtilities.*; - public interface TravelHelper { TravelLocalDataHelper getBookmarksHelper(); @@ -52,9 +49,6 @@ public interface TravelHelper { @NonNull ArrayList getArticleLangs(@NonNull TravelArticleIdentifier articleId); - @Nullable - GPXFile getGpxFile(@NonNull TravelArticle article, @Nullable String lang); - @NonNull String getGPXName(@NonNull final TravelArticle article); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index 1f7a2af18f..d173e20b47 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -45,7 +45,8 @@ import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import static net.osmand.GPXUtilities.*; +import static net.osmand.GPXUtilities.WptPt; +import static net.osmand.GPXUtilities.writeGpxFile; public class TravelObfHelper implements TravelHelper { @@ -93,12 +94,12 @@ public class TravelObfHelper implements TravelHelper { try { final LatLon location = app.getMapViewTrackingUtilities().getMapLocation(); SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( - location, POPULAR_ARTICLES_SEARCH_RADIUS, -1, getSearchRouteArticleFilter(), null); + location, POPULAR_ARTICLES_SEARCH_RADIUS, -1, getSearchFilter(false), null); List amenities = reader.searchPoi(req); if (amenities.size() > 0) { for (Amenity amenity : amenities) { if (!Algorithms.isEmpty(amenity.getName(lang))) { - TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang); + TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang, false); if (article != null) { popularArticles.add(article); if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) { @@ -127,23 +128,23 @@ public class TravelObfHelper implements TravelHelper { } @Nullable - private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang) { + private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang, boolean readPoints) { TravelArticle article = null; - Map articles = readArticles(file, amenity); + Map articles = readArticles(file, amenity, readPoints); if (!Algorithms.isEmpty(articles)) { TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier(); cachedArticles.put(newArticleId, articles); - article = getCachedArticle(newArticleId, lang); + article = getCachedArticle(newArticleId, lang, readPoints); } return article; } @NonNull - private SearchPoiTypeFilter getSearchRouteArticleFilter() { + private SearchPoiTypeFilter getSearchFilter(final boolean articlePoints) { return new SearchPoiTypeFilter() { @Override public boolean accept(PoiCategory type, String subcategory) { - return subcategory.equals(ROUTE_ARTICLE); + return subcategory.equals(articlePoints ? ROUTE_ARTICLE_POINT : ROUTE_ARTICLE); } @Override @@ -154,32 +155,17 @@ public class TravelObfHelper implements TravelHelper { } @NonNull - private SearchPoiTypeFilter getSearchRouteArticlePointsFilter() { - return new SearchPoiTypeFilter() { - @Override - public boolean accept(PoiCategory type, String subcategory) { - return subcategory.equals(ROUTE_ARTICLE_POINT); - } - - @Override - public boolean isEmpty() { - return false; - } - }; - } - - @NonNull - private Map readArticles(@NonNull File file, @NonNull Amenity amenity) { + private Map readArticles(@NonNull File file, @NonNull Amenity amenity, boolean readPoints) { Map articles = new HashMap<>(); Set langs = getLanguages(amenity); for (String lang : langs) { - articles.put(lang, readArticle(file, amenity, lang)); + articles.put(lang, readArticle(file, amenity, lang, readPoints)); } return articles; } @NonNull - private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @Nullable String lang) { + private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @NonNull String lang, boolean readPoints) { TravelArticle res = new TravelArticle(); res.file = file; String title = amenity.getName(lang); @@ -195,40 +181,44 @@ public class TravelObfHelper implements TravelHelper { res.lang = lang; res.contentsJson = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang)); res.aggregatedPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_AGGR_PART, lang)); + if (readPoints) { + res.gpxFile = buildGpxFile(res); + res.gpxFileRead = true; + } return res; } - - @Override - public GPXFile getGpxFile(@NonNull TravelArticle article, @Nullable String lang) { + private GPXFile buildGpxFile(@NonNull TravelArticle article) { GPXFile gpxFile = null; - List pointList = getPointList(article, lang); + List pointList = getPointList(article); if (!Algorithms.isEmpty(pointList)) { - gpxFile = new GPXFile(article.getTitle(), lang, ""); + gpxFile = new GPXFile(article.getTitle(), article.getLang(), ""); for (Amenity amenity : pointList) { - WptPt wptPt = createWptPt(amenity, lang); + WptPt wptPt = createWptPt(amenity, article.getLang()); gpxFile.addPoint(wptPt); } } return gpxFile; } - @Nullable - private List getPointList(@NonNull final TravelArticle article, @Nullable final String lang) { + @NonNull + private List getPointList(@NonNull final TravelArticle article) { final List pointList = new ArrayList<>(); + final String lang = article.getLang(); for (BinaryMapIndexReader reader : getReaders()) { try { - final LatLon location = new LatLon(article.getLat(), article.getLon()); - SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( - location, POPULAR_ARTICLES_SEARCH_RADIUS, -1, getSearchRouteArticlePointsFilter(), - new ResultMatcher() { + if (article.file != null && !article.file.equals(reader.getFile())) { + continue; + } + SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0, + Algorithms.emptyIfNull(article.routeId), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, + getSearchFilter(false), new ResultMatcher() { + @Override - public boolean publish(Amenity point) { - String amenityLang = point.getTagSuffix(Amenity.LANG_YES + ":"); - if (lang != null && lang.equals(amenityLang) - && point.getAdditionalInfo(Amenity.ROUTE_ID) != null - && point.getAdditionalInfo(Amenity.ROUTE_ID).equals(article.getRouteId())) { - pointList.add(point); + public boolean publish(Amenity amenity) { + String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); + if (lang.equals(amenityLang)) { + pointList.add(amenity); } return false; } @@ -237,8 +227,18 @@ public class TravelObfHelper implements TravelHelper { public boolean isCancelled() { return false; } - }); - reader.searchPoi(req); + }, null); + + if (!Double.isNaN(article.lat)) { + req.setBBoxRadius(article.lat, article.lon, ARTICLE_SEARCH_RADIUS); + if (!Algorithms.isEmpty(article.title)) { + reader.searchPoiByName(req); + } else { + reader.searchPoi(req); + } + } else { + reader.searchPoi(req); + } } catch (Exception e) { LOG.error(e.getMessage(), e); } @@ -258,7 +258,7 @@ public class TravelObfHelper implements TravelHelper { if (color != null) { wptPt.setColor(ColorDialogs.getColorByTag(color)); } - String iconName = amenity.getIcon(); + String iconName = amenity.getGpxIcon(); if (iconName != null) { wptPt.setIconName(iconName); } @@ -284,7 +284,7 @@ public class TravelObfHelper implements TravelHelper { for (BinaryMapIndexReader reader : getReaders()) { try { SearchRequest searchRequest = BinaryMapIndexReader.buildSearchPoiRequest(0, 0, searchQuery, - 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, getSearchRouteArticleFilter(), new ResultMatcher() { + 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, getSearchFilter(false), new ResultMatcher() { @Override public boolean publish(Amenity object) { List otherNames = object.getAllNames(false); @@ -313,7 +313,7 @@ public class TravelObfHelper implements TravelHelper { for (Amenity amenity : entry.getValue()) { Set nameLangs = getLanguages(amenity); if (nameLangs.contains(appLang)) { - TravelArticle article = readArticle(file, amenity, appLang); + TravelArticle article = readArticle(file, amenity, appLang, false); ArrayList langs = new ArrayList<>(nameLangs); Collections.sort(langs, new Comparator() { @Override @@ -367,7 +367,7 @@ public class TravelObfHelper implements TravelHelper { Collections.sort(list, new Comparator() { @Override public int compare(WikivoyageSearchResult res1, WikivoyageSearchResult res2) { - return collator.compare(res1.articleId.title, res2.articleId.title); + return collator.compare(res1.articleTitle, res2.articleTitle); } }); } @@ -407,7 +407,7 @@ public class TravelObfHelper implements TravelHelper { for (BinaryMapIndexReader reader : getReaders()) { try { SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, - Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, getSearchRouteArticleFilter(), new ResultMatcher() { + Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, getSearchFilter(false), new ResultMatcher() { @Override public boolean publish(Amenity amenity) { @@ -450,7 +450,7 @@ public class TravelObfHelper implements TravelHelper { for (Amenity amenity : entry.getValue()) { Set nameLangs = getLanguages(amenity); if (nameLangs.contains(lang)) { - TravelArticle a = readArticle(file, amenity, lang); + TravelArticle a = readArticle(file, amenity, lang, false); WikivoyageSearchResult rs = new WikivoyageSearchResult(a, new ArrayList<>(nameLangs)); List l = navMap.get(rs.isPartOf); if (l == null) { @@ -487,12 +487,12 @@ public class TravelObfHelper implements TravelHelper { @Override public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) { - TravelArticle article = getCachedArticle(articleId, lang); + TravelArticle article = getCachedArticle(articleId, lang, true); return article == null ? localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang) : article; } @Nullable - private TravelArticle getCachedArticle(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) { + private TravelArticle getCachedArticle(@NonNull TravelArticleIdentifier articleId, @NonNull String lang, boolean forceReadPoints) { TravelArticle article = null; Map articles = cachedArticles.get(articleId); if (articles != null) { @@ -508,54 +508,45 @@ public class TravelObfHelper implements TravelHelper { } } } - return article == null ? findArticleById(articleId, lang) : article; + if (article == null) { + article = findArticleById(articleId, lang); + } + if (article != null && !article.gpxFileRead && forceReadPoints) { + article.gpxFile = buildGpxFile(article); + article.gpxFileRead = true; + } + return article; } private TravelArticle findArticleById(@NonNull final TravelArticleIdentifier articleId, final String lang) { TravelArticle article = null; - final boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT); - final List amenities = new ArrayList<>(); + boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT); + List amenities = null; for (BinaryMapIndexReader reader : getReaders()) { try { if (articleId.file != null && !articleId.file.equals(reader.getFile()) && !isDbArticle) { continue; } SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0, - Algorithms.emptyIfNull(articleId.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, - getSearchRouteArticleFilter(), new ResultMatcher() { - boolean done = false; - - @Override - public boolean publish(Amenity amenity) { - if (Algorithms.stringsEqual(articleId.routeId, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null))) - && Algorithms.stringsEqual(articleId.routeSource, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE, null))) || isDbArticle) { - amenities.add(amenity); - done = true; - } - return false; - } - - @Override - public boolean isCancelled() { - return done; - } - }, null); + Algorithms.emptyIfNull(articleId.routeId), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, + getSearchFilter(false), null, null); + req.setLimit(1); if (!Double.isNaN(articleId.lat)) { req.setBBoxRadius(articleId.lat, articleId.lon, ARTICLE_SEARCH_RADIUS); - if (!Algorithms.isEmpty(articleId.title)) { - reader.searchPoiByName(req); + if (!Algorithms.isEmpty(articleId.routeId)) { + amenities = reader.searchPoiByName(req); } else { - reader.searchPoi(req); + amenities = reader.searchPoi(req); } } else { - reader.searchPoi(req); + amenities = reader.searchPoi(req); } } catch (IOException e) { LOG.error(e.getMessage()); } if (!Algorithms.isEmpty(amenities)) { - article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang); + article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang, true); } } return article; @@ -570,7 +561,7 @@ public class TravelObfHelper implements TravelHelper { @Nullable @Override public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon, @NonNull final String lang) { - QuadRect rect = latLon != null ? MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), ARTICLE_SEARCH_RADIUS) : new QuadRect(); + QuadRect rect = MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), ARTICLE_SEARCH_RADIUS); return getArticleByTitle(title, rect, lang); } @@ -596,13 +587,14 @@ public class TravelObfHelper implements TravelHelper { for (BinaryMapIndexReader reader : getReaders()) { try { SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( - x, y, title, left, right, top, bottom, getSearchRouteArticleFilter(), null, null); + x, y, title, left, right, top, bottom, getSearchFilter(false), null, null); + req.setLimit(1); amenities = reader.searchPoiByName(req); } catch (IOException e) { LOG.error(e.getMessage()); } if (!Algorithms.isEmpty(amenities)) { - article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang); + article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang, true); } } return article; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java index 5482bfee6c..e67fea4eaa 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java @@ -15,6 +15,7 @@ public class WikivoyageSearchResult { TravelArticleIdentifier articleId; + String articleTitle; String imageTitle; String isPartOf; @@ -32,8 +33,7 @@ public class WikivoyageSearchResult { public WikivoyageSearchResult(String routeId, String articleTitle, String isPartOf, String imageTitle, @Nullable List langs) { TravelArticle article = new TravelArticle(); article.routeId = routeId; - article.title = articleTitle; - + this.articleTitle = articleTitle; this.articleId = article.generateIdentifier(); this.imageTitle = imageTitle; this.isPartOf = isPartOf; @@ -47,7 +47,7 @@ public class WikivoyageSearchResult { } public String getArticleTitle() { - return articleId.title; + return articleTitle; } public String getArticleRouteId() { From ce256d199479bd85de11f92ccde7cfd4a53f9790 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Sun, 10 Jan 2021 15:24:52 +0300 Subject: [PATCH 7/9] Revert some changes of travel obf --- .../plus/wikivoyage/data/TravelArticle.java | 4 ++ .../plus/wikivoyage/data/TravelObfHelper.java | 44 +++++++++++++------ .../data/WikivoyageSearchResult.java | 4 +- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java index 0579af14cc..0989a7c2e1 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelArticle.java @@ -171,6 +171,7 @@ public class TravelArticle { @Nullable File file; double lat; double lon; + @Nullable String title; @Nullable String routeId; @Nullable String routeSource; @@ -194,6 +195,7 @@ public class TravelArticle { file = article.file; lat = article.lat; lon = article.lon; + title = article.title; routeId = article.routeId; routeSource = article.routeSource; } @@ -202,6 +204,7 @@ public class TravelArticle { public void writeToParcel(Parcel out, int flags) { out.writeDouble(lat); out.writeDouble(lon); + out.writeString(title); out.writeString(routeId); out.writeString(routeSource); out.writeString(file != null ? file.getAbsolutePath() : null); @@ -210,6 +213,7 @@ public class TravelArticle { private void readFromParcel(Parcel in) { lat = in.readDouble(); lon = in.readDouble(); + title = in.readString(); routeId = in.readString(); routeSource = in.readString(); String filePath = in.readString(); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index d173e20b47..a224c61e00 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -62,7 +62,7 @@ public class TravelObfHelper implements TravelHelper { private final Collator collator; private List popularArticles = new ArrayList<>(); - private Map> cachedArticles = new ConcurrentHashMap<>(); + private final Map> cachedArticles = new ConcurrentHashMap<>(); private final TravelLocalDataHelper localDataHelper; public TravelObfHelper(OsmandApplication app) { @@ -211,13 +211,15 @@ public class TravelObfHelper implements TravelHelper { continue; } SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0, - Algorithms.emptyIfNull(article.routeId), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, - getSearchFilter(false), new ResultMatcher() { + Algorithms.emptyIfNull(article.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, + getSearchFilter(true), new ResultMatcher() { @Override public boolean publish(Amenity amenity) { String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); - if (lang.equals(amenityLang)) { + if (lang != null && lang.equals(amenityLang) + && Algorithms.stringsEqual(article.routeId, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null))) + && Algorithms.stringsEqual(article.routeSource, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE, null)))) { pointList.add(amenity); } return false; @@ -367,7 +369,7 @@ public class TravelObfHelper implements TravelHelper { Collections.sort(list, new Comparator() { @Override public int compare(WikivoyageSearchResult res1, WikivoyageSearchResult res2) { - return collator.compare(res1.articleTitle, res2.articleTitle); + return collator.compare(res1.getArticleTitle(), res2.getArticleTitle()); } }); } @@ -520,27 +522,43 @@ public class TravelObfHelper implements TravelHelper { private TravelArticle findArticleById(@NonNull final TravelArticleIdentifier articleId, final String lang) { TravelArticle article = null; - boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT); - List amenities = null; + final boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT); + final List amenities = new ArrayList<>(); for (BinaryMapIndexReader reader : getReaders()) { try { if (articleId.file != null && !articleId.file.equals(reader.getFile()) && !isDbArticle) { continue; } SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0, - Algorithms.emptyIfNull(articleId.routeId), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, - getSearchFilter(false), null, null); + Algorithms.emptyIfNull(articleId.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, + getSearchFilter(false), new ResultMatcher() { + boolean done = false; + + @Override + public boolean publish(Amenity amenity) { + if (Algorithms.stringsEqual(articleId.routeId, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null))) + && Algorithms.stringsEqual(articleId.routeSource, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE, null))) || isDbArticle) { + amenities.add(amenity); + done = true; + } + return false; + } + + @Override + public boolean isCancelled() { + return done; + } + }, null); - req.setLimit(1); if (!Double.isNaN(articleId.lat)) { req.setBBoxRadius(articleId.lat, articleId.lon, ARTICLE_SEARCH_RADIUS); if (!Algorithms.isEmpty(articleId.routeId)) { - amenities = reader.searchPoiByName(req); + reader.searchPoiByName(req); } else { - amenities = reader.searchPoi(req); + reader.searchPoi(req); } } else { - amenities = reader.searchPoi(req); + reader.searchPoi(req); } } catch (IOException e) { LOG.error(e.getMessage()); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java index e67fea4eaa..595edd34c4 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java @@ -33,7 +33,7 @@ public class WikivoyageSearchResult { public WikivoyageSearchResult(String routeId, String articleTitle, String isPartOf, String imageTitle, @Nullable List langs) { TravelArticle article = new TravelArticle(); article.routeId = routeId; - this.articleTitle = articleTitle; + article.title = articleTitle; this.articleId = article.generateIdentifier(); this.imageTitle = imageTitle; this.isPartOf = isPartOf; @@ -47,7 +47,7 @@ public class WikivoyageSearchResult { } public String getArticleTitle() { - return articleTitle; + return articleId.title; } public String getArticleRouteId() { From 0c6803533c4039785066a410d153bfb5ddc53b5e Mon Sep 17 00:00:00 2001 From: max-klaus Date: Sun, 10 Jan 2021 15:28:26 +0300 Subject: [PATCH 8/9] Revert again --- OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java | 2 +- .../net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java index 73bebdf9ad..38380015c3 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelDbHelper.java @@ -448,7 +448,7 @@ public class TravelDbHelper implements TravelHelper { } } if (matchLang) { - prev.articleTitle = rs.getArticleTitle(); + prev.articleId.title = rs.getArticleTitle(); prev.isPartOf = rs.getIsPartOf(); } prev.langs.add(matchLang ? 0 : 1, rs.langs.get(0)); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java index 595edd34c4..d76f46e88b 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/WikivoyageSearchResult.java @@ -15,7 +15,6 @@ public class WikivoyageSearchResult { TravelArticleIdentifier articleId; - String articleTitle; String imageTitle; String isPartOf; From c01db4853f4111e0b63f4ca77d36080ad783a961 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Mon, 11 Jan 2021 18:03:11 +0200 Subject: [PATCH 9/9] Fix TravelObfHelper getPointList() --- .../osmand/plus/wikivoyage/data/TravelObfHelper.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java index a224c61e00..484fc2a34d 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/data/TravelObfHelper.java @@ -217,9 +217,9 @@ public class TravelObfHelper implements TravelHelper { @Override public boolean publish(Amenity amenity) { String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); - if (lang != null && lang.equals(amenityLang) - && Algorithms.stringsEqual(article.routeId, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null))) - && Algorithms.stringsEqual(article.routeSource, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE, null)))) { + if (Algorithms.stringsEqual(lang, amenityLang) + && Algorithms.stringsEqual(article.routeId, + Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null)))) { pointList.add(amenity); } return false; @@ -231,16 +231,11 @@ public class TravelObfHelper implements TravelHelper { } }, null); - if (!Double.isNaN(article.lat)) { - req.setBBoxRadius(article.lat, article.lon, ARTICLE_SEARCH_RADIUS); if (!Algorithms.isEmpty(article.title)) { reader.searchPoiByName(req); } else { reader.searchPoi(req); } - } else { - reader.searchPoi(req); - } } catch (Exception e) { LOG.error(e.getMessage(), e); }