diff --git a/OsmAnd/res/layout/wikivoyage_travel_gpx_card.xml b/OsmAnd/res/layout/wikivoyage_travel_gpx_card.xml index eb97f02c0b..44077a0dcd 100644 --- a/OsmAnd/res/layout/wikivoyage_travel_gpx_card.xml +++ b/OsmAnd/res/layout/wikivoyage_travel_gpx_card.xml @@ -10,17 +10,13 @@ android:id="@+id/background_view" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginTop="@dimen/content_padding_small" - android:layout_marginStart="@dimen/content_padding" - android:layout_marginEnd="@dimen/content_padding" - android:background="?attr/wikivoyage_travel_card_bg"> + android:background="?attr/wikivoyage_card_bg_color"> + + + + - + android:orientation="vertical" + android:paddingTop="@dimen/content_padding_half" + android:paddingBottom="@dimen/content_padding_small"> + + + + + + android:layout_height="wrap_content" + android:paddingTop="@dimen/content_padding_small_half" + android:paddingBottom="@dimen/content_padding_small_half"> loadPopularArticles() { String lang = app.getLanguage(); List popularArticles = new ArrayList<>(); - for (BinaryMapIndexReader reader : getReaders()) { + final List> amenities = new ArrayList<>(); + final LatLon location = app.getMapViewTrackingUtilities().getMapLocation(); + for (final BinaryMapIndexReader reader : getReaders()) { try { - final LatLon location = app.getMapViewTrackingUtilities().getMapLocation(); - SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( - 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, false); - if (article != null) { - popularArticles.add(article); - if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) { - break; - } - } - } - } - Collections.sort(popularArticles, new Comparator() { - @Override - public int compare(TravelArticle article1, TravelArticle article2) { - int d1 = (int) (MapUtils.getDistance(article1.getLat(), article1.getLon(), - location.getLatitude(), location.getLongitude())); - int d2 = (int) (MapUtils.getDistance(article2.getLat(), article2.getLon(), - location.getLatitude(), location.getLongitude())); - return d1 < d2 ? -1 : (d1 == d2 ? 0 : 1); - } - }); - } + searchAmenity(amenities, location, reader, POPULAR_ARTICLES_SEARCH_RADIUS, -1, ROUTE_ARTICLE); + searchAmenity(amenities, location, reader, GPX_TRACKS_SEARCH_RADIUS, 15, ROUTE_TRACK); } catch (Exception e) { LOG.error(e.getMessage(), e); } } + + if (amenities.size() > 0) { + Collections.sort(amenities, new Comparator>() { + @Override + public int compare(Pair article1, Pair article2) { + int d1 = (int) (MapUtils.getDistance(((Amenity) article1.second).getLocation(), location)); + int d2 = (int) (MapUtils.getDistance(((Amenity) article2.second).getLocation(), location)); + return d1 < d2 ? -1 : (d1 == d2 ? 0 : 1); + } + }); + for (Pair amenity : amenities) { + if (!Algorithms.isEmpty(amenity.second.getName(lang))) { + TravelArticle article = cacheTravelArticles(amenity.first, amenity.second, lang, false); + if (article != null) { + popularArticles.add(article); + if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) { + break; + } + } + } + } + } this.popularArticles = popularArticles; return popularArticles; } + private void searchAmenity(final List> amenitiesList, LatLon location, + final BinaryMapIndexReader reader, int searchRadius, int zoom, + String searchFilter) throws IOException { + reader.searchPoi(BinaryMapIndexReader.buildSearchPoiRequest( + location, searchRadius, zoom, getSearchFilter(searchFilter), new ResultMatcher() { + @Override + public boolean publish(Amenity object) { + amenitiesList.add(new Pair<>(reader.getFile(), object)); + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + })); + } + @Nullable private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang, boolean readPoints) { TravelArticle article = null; - Map articles = readArticles(file, amenity, false); + Map articles; + if (amenity.getSubType().equals(ROUTE_TRACK)) { + articles = readRoutePoint(file, amenity); + } else { + articles = readArticles(file, amenity, false); + } if (!Algorithms.isEmpty(articles)) { TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier(); cachedArticles.put(newArticleId, articles); @@ -139,12 +165,38 @@ public class TravelObfHelper implements TravelHelper { return article; } - @NonNull - private SearchPoiTypeFilter getSearchFilter(final boolean articlePoints) { - return new SearchPoiTypeFilter() { + private Map readRoutePoint(File file, Amenity amenity) { + Map articles = new HashMap<>(); + TravelGpx res = new TravelGpx(); + String title = amenity.getName("en"); + res.title = capitalizeFirstLetter(getGpxTitle(Algorithms.isEmpty(title) ? amenity.getName() : title)); + res.routeId = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID)); + try { + res.totalDistance = Float.parseFloat(Algorithms.emptyIfNull(amenity.getTagContent("distance"))); + } catch (NumberFormatException e) { + LOG.debug(e.getMessage(), e); + } + try { + res.diffElevationUp = Double.parseDouble(Algorithms.emptyIfNull(amenity.getTagContent("diff_ele_up"))); + } catch (NumberFormatException e) { + LOG.debug(e.getMessage(), e); + } + try { + res.diffElevationDown = Double.parseDouble(Algorithms.emptyIfNull(amenity.getTagContent("diff_ele_down"))); + } catch (NumberFormatException e) { + LOG.debug(e.getMessage(), e); + } + res.user = Algorithms.emptyIfNull(amenity.getTagContent("user")); + articles.put("en", res); + return articles; + } + + + private BinaryMapIndexReader.SearchPoiTypeFilter getSearchFilter(final String filterSubcategory) { + return new BinaryMapIndexReader.SearchPoiTypeFilter() { @Override public boolean accept(PoiCategory type, String subcategory) { - return subcategory.equals(articlePoints ? ROUTE_ARTICLE_POINT : ROUTE_ARTICLE); + return subcategory.equals(filterSubcategory); } @Override @@ -175,9 +227,9 @@ public class TravelObfHelper implements TravelHelper { res.isParentOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_PARENT_OF, lang)); res.lat = amenity.getLocation().getLatitude(); res.lon = amenity.getLocation().getLongitude(); - 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.imageTitle = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IMAGE_TITLE)); + res.routeId = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID)); + res.routeSource = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE)); res.originalId = 0; res.lang = lang; res.contentsJson = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang)); @@ -214,14 +266,14 @@ public class TravelObfHelper implements TravelHelper { } SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0, Algorithms.emptyIfNull(article.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, - getSearchFilter(true), new ResultMatcher() { + getSearchFilter(ROUTE_ARTICLE_POINT), new ResultMatcher() { @Override public boolean publish(Amenity amenity) { String amenityLang = amenity.getTagSuffix(Amenity.LANG_YES + ":"); if (Algorithms.stringsEqual(lang, amenityLang) && Algorithms.stringsEqual(article.routeId, - Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null)))) { + Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID)))) { pointList.add(amenity); } return false; @@ -263,7 +315,7 @@ public class TravelObfHelper implements TravelHelper { } String category = amenity.getTagSuffix("category_"); if (category != null) { - wptPt.category = Algorithms.capitalizeFirstLetter(category); + wptPt.category = capitalizeFirstLetter(category); } return wptPt; } @@ -283,7 +335,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, getSearchFilter(false), new ResultMatcher() { + 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, getSearchFilter(ROUTE_ARTICLE), new ResultMatcher() { @Override public boolean publish(Amenity object) { List otherNames = object.getAllNames(false); @@ -458,7 +510,7 @@ public class TravelObfHelper implements TravelHelper { for (BinaryMapIndexReader reader : getReaders()) { try { SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( - 0, 0, title, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, getSearchFilter(false), + 0, 0, title, 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, getSearchFilter(ROUTE_ARTICLE), new ResultMatcher() { boolean done = false; @@ -531,12 +583,13 @@ public class TravelObfHelper implements TravelHelper { } SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0, Algorithms.emptyIfNull(articleId.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, - getSearchFilter(false), new ResultMatcher() { + getSearchFilter(ROUTE_ARTICLE), new ResultMatcher() { boolean done = false; @Override public boolean publish(Amenity amenity) { - if (Algorithms.stringsEqual(articleId.routeId, Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID, null))) || isDbArticle) { + if (Algorithms.stringsEqual(articleId.routeId, + Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID))) || isDbArticle) { amenities.add(amenity); done = true; } @@ -604,7 +657,7 @@ public class TravelObfHelper implements TravelHelper { for (BinaryMapIndexReader reader : getReaders()) { try { SearchRequest req = BinaryMapIndexReader.buildSearchPoiRequest( - x, y, title, left, right, top, bottom, getSearchFilter(false), + x, y, title, left, right, top, bottom, getSearchFilter(ROUTE_ARTICLE), new ResultMatcher() { boolean done = false; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index e520487740..ce6f4475af 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -180,20 +180,21 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv List popularArticles = app.getTravelHelper().getPopularArticles(); for (TravelArticle article : popularArticles) { - if (article instanceof TravelGpx) { - items.add(new TravelGpxCard(app, nightMode, (TravelGpx) article, fm)); - } else { - items.add(new ArticleTravelCard(app, nightMode, article, activity.getSupportFragmentManager())); + if (article instanceof TravelGpx) { + items.add(new TravelGpxCard(app, nightMode, (TravelGpx) article, activity.getSupportFragmentManager())); + } else { + items.add(new ArticleTravelCard(app, nightMode, article, activity.getSupportFragmentManager())); + } + } + items.add(new StartEditingTravelCard(activity, nightMode)); + adapter.setItems(items); + final DownloadIndexesThread downloadThread = app.getDownloadThread(); + if (!downloadThread.getIndexes().isDownloadedFromInternet) { + waitForIndexes = true; + downloadThread.runReloadIndexFilesSilent(); + } else { + checkDownloadIndexes(); } - } - items.add(new StartEditingTravelCard(activity, nightMode)); - adapter.setItems(items); - final DownloadIndexesThread downloadThread = app.getDownloadThread(); - if (!downloadThread.getIndexes().isDownloadedFromInternet) { - waitForIndexes = true; - downloadThread.runReloadIndexFilesSilent(); - } else { - checkDownloadIndexes(); } } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelGpxCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelGpxCard.java index 32790cc094..608b0b812f 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelGpxCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelGpxCard.java @@ -8,11 +8,11 @@ import androidx.annotation.NonNull; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.RecyclerView; +import net.osmand.AndroidUtils; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.wikipedia.WikiArticleHelper; -import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment; import net.osmand.plus.wikivoyage.data.TravelGpx; import net.osmand.plus.wikivoyage.data.TravelLocalDataHelper; @@ -38,17 +38,21 @@ public class TravelGpxCard extends BaseTravelCard { if (viewHolder instanceof TravelGpxVH) { final TravelGpxVH holder = (TravelGpxVH) viewHolder; holder.title.setText(article.getTitle()); - holder.content.setText(WikiArticleHelper.getPartialContent(article.getContent())); - holder.distance.setText(OsmAndFormatter.getFormattedDistance(article.totalDistance,app)); - holder.diffElevationUp.setText(OsmAndFormatter.getFormattedAlt(article.diffElevationUp,app)); - holder.diffElevationDown.setText(OsmAndFormatter.getFormattedAlt(article.diffElevationDown,app)); + Drawable icon = getActiveIcon(R.drawable.ic_action_user_account_16); + holder.user.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); + holder.user.setText(WikiArticleHelper.getPartialContent(article.user)); + AndroidUtils.setBackground(app, holder.user, nightMode, R.drawable.btn_border_bg_light, R.drawable.btn_border_bg_dark); + holder.distance.setText(OsmAndFormatter.getFormattedDistance(article.totalDistance, app)); + holder.diffElevationUp.setText(OsmAndFormatter.getFormattedAlt(article.diffElevationUp, app)); + holder.diffElevationDown.setText(OsmAndFormatter.getFormattedAlt(article.diffElevationDown, app)); holder.leftButton.setText(app.getString(R.string.shared_string_view)); View.OnClickListener readClickListener = new View.OnClickListener() { @Override public void onClick(View v) { if (fragmentManager != null) { - WikivoyageArticleDialogFragment.showInstance(app, fragmentManager, - article.generateIdentifier(), article.getLang()); +// WikivoyageArticleDialogFragment.showInstance(app, fragmentManager, +// article.generateIdentifier(), article.getLang()); + app.getTravelHelper().createGpxFile(article); } } }; @@ -86,7 +90,7 @@ public class TravelGpxCard extends BaseTravelCard { public static class TravelGpxVH extends RecyclerView.ViewHolder { final TextView title; - final TextView content; + final TextView user; final TextView distance; final TextView diffElevationUp; final TextView diffElevationDown; @@ -98,7 +102,7 @@ public class TravelGpxCard extends BaseTravelCard { public TravelGpxVH(final View itemView) { super(itemView); title = itemView.findViewById(R.id.title); - content = itemView.findViewById(R.id.content); + user = itemView.findViewById(R.id.user_name); distance = itemView.findViewById(R.id.distance); diffElevationUp = itemView.findViewById(R.id.diff_ele_up); diffElevationDown = itemView.findViewById(R.id.diff_ele_down);