diff --git a/OsmAnd-java/src/main/java/net/osmand/data/TransportRoute.java b/OsmAnd-java/src/main/java/net/osmand/data/TransportRoute.java index b58237dfe0..a353148bce 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/TransportRoute.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/TransportRoute.java @@ -1,5 +1,10 @@ package net.osmand.data; +import net.osmand.osm.edit.Node; +import net.osmand.osm.edit.Way; +import net.osmand.util.Algorithms; +import net.osmand.util.MapUtils; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -7,11 +12,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import net.osmand.osm.edit.Node; -import net.osmand.osm.edit.Way; -import net.osmand.util.Algorithms; -import net.osmand.util.MapUtils; - public class TransportRoute extends MapObject { private List forwardStops = new ArrayList(); private String ref; @@ -221,17 +221,19 @@ public class TransportRoute extends MapObject { return d; } - public String getAdjustedRouteRef() { - if (ref != null) { - int charPos = ref.lastIndexOf(':'); + public String getAdjustedRouteRef(boolean small) { + String adjustedRef = getRef(); + if (adjustedRef != null) { + int charPos = adjustedRef.lastIndexOf(':'); if (charPos != -1) { - ref = ref.substring(0, charPos); + adjustedRef = adjustedRef.substring(0, charPos); } - if (ref.length() > 4) { - ref = ref.substring(0, 4); + int maxRefLength = small ? 5 : 8; + if (adjustedRef.length() > maxRefLength) { + adjustedRef = adjustedRef.substring(0, maxRefLength - 1) + "…"; } } - return ref; + return adjustedRef; } public boolean compareRoute(TransportRoute thatObj) { diff --git a/OsmAnd/res/layout/transport_stop_route_item.xml b/OsmAnd/res/layout/transport_stop_route_item.xml index 2e0440c0a8..fdbc20636a 100644 --- a/OsmAnd/res/layout/transport_stop_route_item.xml +++ b/OsmAnd/res/layout/transport_stop_route_item.xml @@ -14,6 +14,7 @@ android:textSize="@dimen/default_sub_text_size_small" tools:text="3" android:gravity="center" + android:maxLines="1" android:id="@+id/transport_stop_route_text" android:background="@drawable/transport_stop_route_bg" android:layout_width="match_parent" diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 56d70ab19c..d08abc29e0 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -1339,14 +1339,16 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo if (!transportBadgesCreated) { List localTransportStopRoutes = menu.getLocalTransportStopRoutes(); List nearbyTransportStopRoutes = menu.getNearbyTransportStopRoutes(); - int localColumnsPerRow = getRoutesBadgesColumnsPerRow(null); int maxLocalRows = 0; - if (localTransportStopRoutes != null) { + if (localTransportStopRoutes != null && !localTransportStopRoutes.isEmpty()) { List localFilteredTransportStopRoutes = filterTransportRoutes(localTransportStopRoutes); + int minBadgeWidth = getMinBadgeWidth(localFilteredTransportStopRoutes); + int localColumnsPerRow = getRoutesBadgesColumnsPerRow(null, minBadgeWidth); maxLocalRows = (int) Math.round(Math.ceil((double) localFilteredTransportStopRoutes.size() / localColumnsPerRow)); + localTransportStopRoutesGrid.setColumnWidth(minBadgeWidth); updateLocalRoutesBadges(localFilteredTransportStopRoutes, localColumnsPerRow); } - if (nearbyTransportStopRoutes != null) { + if (nearbyTransportStopRoutes != null && !nearbyTransportStopRoutes.isEmpty()) { updateNearbyRoutesBadges(maxLocalRows, filterNearbyTransportRoutes(nearbyTransportStopRoutes, localTransportStopRoutes)); } transportBadgesCreated = true; @@ -1380,7 +1382,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo String nearInDistance = getString(R.string.transport_nearby_routes) + " " + OsmAndFormatter.getFormattedDistance(TransportStopController.SHOW_STOPS_RADIUS_METERS, getMyApplication()) + ":"; nearbyRoutesWithinTv.setText(nearInDistance); - int nearbyColumnsPerRow = getRoutesBadgesColumnsPerRow(nearInDistance); + int minBadgeWidth = getMinBadgeWidth(nearbyTransportStopRoutes); + int nearbyColumnsPerRow = getRoutesBadgesColumnsPerRow(nearInDistance, minBadgeWidth); int maxNearbyRows = Math.min(3, 6 - maxLocalRows); int nearbyMaxItems = maxNearbyRows * nearbyColumnsPerRow - 1; TransportStopRouteAdapter adapter; @@ -1389,6 +1392,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo } else { adapter = createTransportStopRouteAdapter(nearbyTransportStopRoutes, false); } + nearbyTransportStopRoutesGrid.setColumnWidth(minBadgeWidth); nearbyTransportStopRoutesGrid.setAdapter(adapter); nearbyTransportStopRoutesGrid.setVisibility(View.VISIBLE); nearbyRoutesLayout.setVisibility(View.VISIBLE); @@ -1397,9 +1401,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo } } - private int getRoutesBadgesColumnsPerRow(@Nullable String nearInDistance) { + private int getRoutesBadgesColumnsPerRow(@Nullable String nearInDistance, int minBadgeWidth) { try { - double badgeWidth = getResources().getDimension(R.dimen.context_menu_transport_grid_item_width); double gridSpacing = getResources().getDimension(R.dimen.context_menu_transport_grid_spacing); double gridPadding = getResources().getDimension(R.dimen.context_menu_padding_margin_default); int availableSpace; @@ -1410,11 +1413,31 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo double paddingTv = getResources().getDimension(R.dimen.context_menu_padding_margin_small); availableSpace = (int) (routesBadgesContainer.getWidth() - gridPadding * 2 - paddingTv - textWidth); } - return (int) ((availableSpace + gridSpacing) / (badgeWidth + gridSpacing)); + return (int) ((availableSpace + gridSpacing) / (minBadgeWidth + gridSpacing)); } catch (Resources.NotFoundException e) { return -1; } } + + private int getMinBadgeWidth(List transportStopRoutes) { + try { + int minBadgeWidth = getResources().getDimensionPixelSize(R.dimen.context_menu_transport_grid_item_width); + int textPadding = getResources().getDimensionPixelSize(R.dimen.context_menu_subtitle_margin); + float textSizeSmall = getResources().getDimensionPixelSize(R.dimen.default_sub_text_size_small); + + for (TransportStopRoute transportStopRoute : transportStopRoutes) { + String routeRef = transportStopRoute.route.getAdjustedRouteRef(false); + int textWidth = AndroidUtils.getTextWidth(textSizeSmall, routeRef) + textPadding * 2; + if (textWidth > minBadgeWidth) { + minBadgeWidth = textWidth; + } + } + + return minBadgeWidth; + } catch (Resources.NotFoundException e) { + return dpToPx(32); + } + } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void runLayoutListener() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java index e9d029ed40..48aea13a2d 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java @@ -796,6 +796,7 @@ public class MenuBuilder { transportRect.setTypeface(FontCache.getRobotoMedium(view.getContext())); transportRect.setTextColor(Color.WHITE); transportRect.setTextSize(10); + transportRect.setMaxLines(1); GradientDrawable shape = new GradientDrawable(); shape.setShape(GradientDrawable.RECTANGLE); @@ -805,7 +806,7 @@ public class MenuBuilder { transportRect.setTextColor(UiUtilities.getContrastColor(app, bgColor, true)); transportRect.setBackgroundDrawable(shape); - transportRect.setText(route.route.getAdjustedRouteRef()); + transportRect.setText(route.route.getAdjustedRouteRef(true)); baseView.addView(transportRect); LinearLayout infoView = new LinearLayout(view.getContext()); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java index be3fa3b7d6..5ba498bbba 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java @@ -45,7 +45,7 @@ public class TransportStopRouteAdapter extends ArrayAdapter { int bgColor = 0; if (object instanceof TransportStopRoute) { TransportStopRoute transportStopRoute = (TransportStopRoute) object; - routeRef = transportStopRoute.route.getAdjustedRouteRef(); + routeRef = transportStopRoute.route.getAdjustedRouteRef(false); bgColor = transportStopRoute.getColor(app, nightMode); } else if (object instanceof String) { routeRef = (String) object; diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java index a75a44ca42..eebfa1eaae 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/RouteDetailsFragment.java @@ -1453,7 +1453,7 @@ public class RouteDetailsFragment extends ContextMenuFragment implements PublicT OsmandApplication app = mapActivity.getMyApplication(); LinearLayout convertView = (LinearLayout) mapActivity.getLayoutInflater().inflate(R.layout.transport_stop_route_item_with_icon, null, false); if (transportStopRoute != null) { - String routeRef = transportStopRoute.route.getAdjustedRouteRef(); + String routeRef = transportStopRoute.route.getAdjustedRouteRef(true); int bgColor = transportStopRoute.getColor(app, isNightMode()); TextView transportStopRouteTextView = (TextView) convertView.findViewById(R.id.transport_stop_route_text); diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/PublicTransportCard.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/PublicTransportCard.java index 792cbd59ea..2bcd283f00 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/PublicTransportCard.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/PublicTransportCard.java @@ -272,7 +272,7 @@ public class PublicTransportCard extends BaseCard { TransportRoute transportRoute = segment.route; TransportStopRoute transportStopRoute = TransportStopRoute.getTransportStopRoute(transportRoute, segment.getStart()); - String routeRef = segment.route.getAdjustedRouteRef(); + String routeRef = segment.route.getAdjustedRouteRef(true); int bgColor = transportStopRoute.getColor(app, nightMode); TextView transportStopRouteTextView = (TextView) bageView.findViewById(R.id.transport_stop_route_text);