diff --git a/OsmAnd-api/src/net/osmand/aidlapi/OsmAndCustomizationConstants.java b/OsmAnd-api/src/net/osmand/aidlapi/OsmAndCustomizationConstants.java index af48552bc4..53e838a9a7 100644 --- a/OsmAnd-api/src/net/osmand/aidlapi/OsmAndCustomizationConstants.java +++ b/OsmAnd-api/src/net/osmand/aidlapi/OsmAndCustomizationConstants.java @@ -11,6 +11,7 @@ public interface OsmAndCustomizationConstants { String DRAWER_MY_PLACES_ID = DRAWER_ITEM_ID_SCHEME + "my_places"; String DRAWER_SEARCH_ID = DRAWER_ITEM_ID_SCHEME + "search"; String DRAWER_DIRECTIONS_ID = DRAWER_ITEM_ID_SCHEME + "directions"; + String DRAWER_TRIP_RECORDING_ID = DRAWER_ITEM_ID_SCHEME + "trip_recording"; String DRAWER_CONFIGURE_MAP_ID = DRAWER_ITEM_ID_SCHEME + "configure_map"; String DRAWER_DOWNLOAD_MAPS_ID = DRAWER_ITEM_ID_SCHEME + "download_maps"; String DRAWER_OSMAND_LIVE_ID = DRAWER_ITEM_ID_SCHEME + "osmand_live"; diff --git a/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java index 033beb3789..882231d8a6 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/OsmandRegions.java @@ -436,6 +436,7 @@ public class OsmandRegions { cx /= object.getPointsLength(); cy /= object.getPointsLength(); rd.regionCenter = new LatLon(MapUtils.get31LatitudeY((int) cy), MapUtils.get31LongitudeX((int) cx)); + rd.boundingBox = findBoundingBox(object); } rd.regionParentFullName = mapIndexFields.get(mapIndexFields.parentFullName, object); @@ -461,6 +462,43 @@ public class OsmandRegions { return rd; } + private QuadRect findBoundingBox(BinaryMapDataObject object) { + if (object.getPointsLength() == 0) { + return new QuadRect(0, 0, 0, 0); + } + + double currentX = object.getPoint31XTile(0); + double currentY = object.getPoint31YTile(0); + double minX = currentX; + double maxX = currentX; + double minY = currentY; + double maxY = currentY; + + if (object.getPointsLength() > 1) { + for (int i = 1; i < object.getPointsLength(); i++) { + currentX = object.getPoint31XTile(i); + currentY = object.getPoint31YTile(i); + if (currentX > maxX) { + maxX = currentX; + } else if (currentX < minX) { + minX = currentX; + } + if (currentY > maxY) { + maxY = currentY; + } else if (currentY < minY) { + minY = currentY; + } + } + } + + minX = MapUtils.get31LongitudeX((int) minX); + maxX = MapUtils.get31LongitudeX((int) maxX); + double revertedMinY = MapUtils.get31LatitudeY((int) maxY); + double revertedMaxY = MapUtils.get31LatitudeY((int) minY); + + return new QuadRect(minX, revertedMinY, maxX, revertedMaxY); + } + private String getSearchIndex(BinaryMapDataObject object) { MapIndex mi = object.getMapIndex(); TIntObjectIterator it = object.getObjectNames().iterator(); diff --git a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java index 38fbc40c3d..99eca34f19 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/WorldRegion.java @@ -1,6 +1,7 @@ package net.osmand.map; import net.osmand.data.LatLon; +import net.osmand.data.QuadRect; import net.osmand.util.Algorithms; import java.io.Serializable; @@ -40,6 +41,7 @@ public class WorldRegion implements Serializable { protected String regionDownloadName; protected boolean regionMapDownload; protected LatLon regionCenter; + protected QuadRect boundingBox; public static class RegionParams { protected String regionLeftHandDriving; @@ -182,4 +184,11 @@ public class WorldRegion implements Serializable { } return res; } + + public boolean containsRegion(WorldRegion region) { + if (this.boundingBox != null && region.boundingBox != null) { + return this.boundingBox.contains(region.boundingBox); + } + return false; + } } \ No newline at end of file diff --git a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java index a9d85ce118..83b7c14b44 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java @@ -127,6 +127,17 @@ public class Algorithms { return def; } + public static double parseDoubleSilently(String input, double def) { + if (input != null && input.length() > 0) { + try { + return Double.parseDouble(input); + } catch (NumberFormatException e) { + return def; + } + } + return def; + } + public static String getFileNameWithoutExtension(File f) { return getFileNameWithoutExtension(f.getName()); } diff --git a/OsmAnd/build.gradle b/OsmAnd/build.gradle index 00e0954c54..eb23647924 100644 --- a/OsmAnd/build.gradle +++ b/OsmAnd/build.gradle @@ -85,6 +85,7 @@ android { } amazonFree { java.srcDirs = ["src-nogms", "src-google"] + manifest.srcFile "AndroidManifest-gplayFree.xml" } amazonFull { java.srcDirs = ["src-nogms", "src-google"] diff --git a/OsmAnd/res/drawable-mdpi/btn_background_active_dark.xml b/OsmAnd/res/drawable-mdpi/btn_background_active_dark.xml new file mode 100644 index 0000000000..88873ec3b4 --- /dev/null +++ b/OsmAnd/res/drawable-mdpi/btn_background_active_dark.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable-mdpi/btn_background_inactive_dark.xml b/OsmAnd/res/drawable-mdpi/btn_background_inactive_dark.xml new file mode 100644 index 0000000000..07c3aed3e9 --- /dev/null +++ b/OsmAnd/res/drawable-mdpi/btn_background_inactive_dark.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable-mdpi/btn_background_inactive_light.xml b/OsmAnd/res/drawable-mdpi/btn_background_inactive_light.xml new file mode 100644 index 0000000000..03ca0abfe5 --- /dev/null +++ b/OsmAnd/res/drawable-mdpi/btn_background_inactive_light.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable-mdpi/btn_background_stroked_active_dark.xml b/OsmAnd/res/drawable-mdpi/btn_background_stroked_active_dark.xml new file mode 100644 index 0000000000..1cdfa50a92 --- /dev/null +++ b/OsmAnd/res/drawable-mdpi/btn_background_stroked_active_dark.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable-mdpi/btn_background_stroked_active_light.xml b/OsmAnd/res/drawable-mdpi/btn_background_stroked_active_light.xml new file mode 100644 index 0000000000..543dd7ef1f --- /dev/null +++ b/OsmAnd/res/drawable-mdpi/btn_background_stroked_active_light.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable-mdpi/btn_background_stroked_inactive_dark.xml b/OsmAnd/res/drawable-mdpi/btn_background_stroked_inactive_dark.xml new file mode 100644 index 0000000000..f669c9d34f --- /dev/null +++ b/OsmAnd/res/drawable-mdpi/btn_background_stroked_inactive_dark.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable-mdpi/btn_background_stroked_inactive_light.xml b/OsmAnd/res/drawable-mdpi/btn_background_stroked_inactive_light.xml new file mode 100644 index 0000000000..feacf12888 --- /dev/null +++ b/OsmAnd/res/drawable-mdpi/btn_background_stroked_inactive_light.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable/btn_background_active_light.xml b/OsmAnd/res/drawable/btn_background_active_light.xml new file mode 100644 index 0000000000..4d8d8e82a5 --- /dev/null +++ b/OsmAnd/res/drawable/btn_background_active_light.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable/btn_unstroked_dark.xml b/OsmAnd/res/drawable/btn_unstroked_dark.xml new file mode 100644 index 0000000000..b1debd42fd --- /dev/null +++ b/OsmAnd/res/drawable/btn_unstroked_dark.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/drawable/btn_unstroked_light.xml b/OsmAnd/res/drawable/btn_unstroked_light.xml new file mode 100644 index 0000000000..07db445277 --- /dev/null +++ b/OsmAnd/res/drawable/btn_unstroked_light.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/bottom_sheet_button_with_icon.xml b/OsmAnd/res/layout/bottom_sheet_button_with_icon.xml new file mode 100644 index 0000000000..b184a3bea7 --- /dev/null +++ b/OsmAnd/res/layout/bottom_sheet_button_with_icon.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/bottom_sheet_select_segment.xml b/OsmAnd/res/layout/bottom_sheet_select_segment.xml new file mode 100644 index 0000000000..ee4dfec006 --- /dev/null +++ b/OsmAnd/res/layout/bottom_sheet_select_segment.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/dialog_edit_gpx_description.xml b/OsmAnd/res/layout/dialog_edit_gpx_description.xml index 1d2428f82b..66c0d57f0a 100644 --- a/OsmAnd/res/layout/dialog_edit_gpx_description.xml +++ b/OsmAnd/res/layout/dialog_edit_gpx_description.xml @@ -21,11 +21,13 @@ android:layout_marginLeft="@dimen/card_padding" android:padding="@dimen/context_menu_padding_margin_small" android:paddingStart="@dimen/context_menu_padding_margin_small" - android:paddingEnd="@dimen/context_menu_padding_margin_small"> + android:paddingEnd="@dimen/context_menu_padding_margin_small" + android:background="@null"> @@ -43,18 +45,20 @@ osmand:typeface="@string/font_roboto_medium" /> + android:layout_gravity="center_vertical"> + android:layout_height="match_parent" + android:layout_marginStart="@dimen/content_padding" + android:layout_marginLeft="@dimen/content_padding" + android:layout_marginTop="@dimen/content_padding_half" + android:layout_marginRight="@dimen/content_padding" + android:layout_marginEnd="@dimen/content_padding" + android:layout_marginBottom="@dimen/content_padding_half"> + android:orientation="vertical"> - + android:layout_height="@dimen/action_bar_height" + android:background="?attr/pstsTabBackground" + android:orientation="horizontal" + android:gravity="center_vertical"> - + - + + + + + + + + + + + + + + android:layout_height="match_parent" + android:background="?attr/activity_background_basic"> + android:visibility="gone" + tools:visibility="visible"> + osmand:typeface="@string/font_roboto_medium" /> diff --git a/OsmAnd/res/layout/favorites_list_item.xml b/OsmAnd/res/layout/favorites_list_item.xml index d53497c0f5..106e532e1e 100644 --- a/OsmAnd/res/layout/favorites_list_item.xml +++ b/OsmAnd/res/layout/favorites_list_item.xml @@ -1,158 +1,158 @@ - + - + - + - + - + - + - - + + - + - + - + - + - + - + - - - + + + - + - - + + \ No newline at end of file diff --git a/OsmAnd/res/layout/gpx_description_preview_card.xml b/OsmAnd/res/layout/gpx_description_preview_card.xml index a3b0ab8b25..551110d028 100644 --- a/OsmAnd/res/layout/gpx_description_preview_card.xml +++ b/OsmAnd/res/layout/gpx_description_preview_card.xml @@ -68,19 +68,18 @@ android:orientation="horizontal"> + android:layout_marginStart="@dimen/content_padding_half" + android:layout_marginLeft="@dimen/content_padding_half"> + android:layout_marginRight="@dimen/content_padding_half" + android:layout_marginEnd="@dimen/content_padding_half" + android:layout_gravity="end"> @@ -142,7 +140,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" - android:background="?attr/selectableItemBackgroundBorderless" + android:duplicateParentState="true" android:padding="@dimen/bottom_sheet_content_padding_small" android:paddingStart="@dimen/bottom_sheet_content_padding_small" android:paddingEnd="@dimen/bottom_sheet_content_padding_small" diff --git a/OsmAnd/res/layout/gpx_overview_fragment.xml b/OsmAnd/res/layout/gpx_overview_fragment.xml index 3b205ae60e..e613260e86 100644 --- a/OsmAnd/res/layout/gpx_overview_fragment.xml +++ b/OsmAnd/res/layout/gpx_overview_fragment.xml @@ -38,21 +38,23 @@ android:layout_height="wrap_content" android:layout_marginStart="@dimen/content_padding" android:layout_marginLeft="@dimen/content_padding" - android:gravity="center" - android:orientation="horizontal" - android:paddingTop="@dimen/dash_margin" - android:paddingBottom="@dimen/dash_margin"> + android:layout_marginTop="@dimen/dash_margin" + android:layout_marginBottom="@dimen/dash_margin" + android:orientation="horizontal"> + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/gpx_track_item.xml b/OsmAnd/res/layout/gpx_track_item.xml index 69bfbb6119..26a7dc6f3f 100644 --- a/OsmAnd/res/layout/gpx_track_item.xml +++ b/OsmAnd/res/layout/gpx_track_item.xml @@ -51,6 +51,7 @@ @@ -31,6 +30,7 @@ android:layout_height="match_parent" android:layout_weight="1" android:background="@null" + android:letterSpacing="@dimen/description_letter_spacing" android:lines="1" android:textColor="?android:attr/textColorPrimary" android:textSize="@dimen/default_desc_text_size" @@ -61,8 +61,11 @@ android:layout_height="wrap_content" android:background="@null" android:ellipsize="end" + android:letterSpacing="@dimen/description_letter_spacing" + android:gravity="start|center_vertical" android:lines="1" android:maxWidth="@dimen/grid_menu_item_width" + android:minWidth="@dimen/map_route_buttons_width" android:textColor="?android:attr/textColorSecondary" android:textSize="@dimen/default_desc_text_size" tools:text="@string/distance" /> diff --git a/OsmAnd/res/layout/settings_group_title.xml b/OsmAnd/res/layout/settings_group_title.xml index 32629ed2de..456a3aace1 100644 --- a/OsmAnd/res/layout/settings_group_title.xml +++ b/OsmAnd/res/layout/settings_group_title.xml @@ -73,6 +73,7 @@ android:paddingBottom="@dimen/content_padding_small"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/OsmAnd/res/values/sizes.xml b/OsmAnd/res/values/sizes.xml index a086849297..107bb45da7 100644 --- a/OsmAnd/res/values/sizes.xml +++ b/OsmAnd/res/values/sizes.xml @@ -413,4 +413,7 @@ 32dp 24dp + + 5sp + 3sp \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index a706cf4b6b..c8c73ec331 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -12,6 +12,14 @@ --> + Application restart required to apply some settings. + On pause + Are you sure you want to stop recording?\nAll unsaved data will be lost. + Track recording stopped + Save and stop recording + Stop without saving + Delete %1$d files? + All regions Restart Routing could avoid strong uphills Don\'t rotate map view if speed is less than a threshold @@ -39,6 +47,9 @@ Hillshade / Slope / Contour lines Select edits for upload Uploaded %1$d of %2$d + Segment %1$d + %1$s contains more than one segment, you need to select the needed part for the navigation. + Select segments Uploading %1$d of %2$d Upload completed Uploading diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 258ed7be38..2bb6f5caec 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -62,6 +62,8 @@ import net.osmand.plus.mapmarkers.MarkersPlanRouteContext; import net.osmand.plus.measurementtool.MeasurementToolFragment; import net.osmand.plus.measurementtool.StartPlanRouteBottomSheet; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; +import net.osmand.plus.monitoring.TripRecordingActiveBottomSheet; +import net.osmand.plus.monitoring.TripRecordingBottomSheet; import net.osmand.plus.osmedit.dialogs.DismissRouteBottomSheetFragment; import net.osmand.plus.profiles.ProfileDataObject; import net.osmand.plus.profiles.ProfileDataUtils; @@ -98,6 +100,7 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_CONFIGURE_P import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_CONFIGURE_SCREEN_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DASHBOARD_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DIRECTIONS_ID; +import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_TRIP_RECORDING_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DIVIDER_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DOWNLOAD_MAPS_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_HELP_ID; @@ -356,10 +359,10 @@ public class MapActivityActions implements DialogProvider { } public void addActionsToAdapter(final double latitude, - final double longitude, - final ContextMenuAdapter adapter, - Object selectedObj, - boolean configureMenu) { + final double longitude, + final ContextMenuAdapter adapter, + Object selectedObj, + boolean configureMenu) { ItemBuilder itemBuilder = new ItemBuilder(); adapter.addItem(itemBuilder @@ -522,6 +525,7 @@ public class MapActivityActions implements DialogProvider { GPXRouteParamsBuilder params = new GPXRouteParamsBuilder(result, settings); params.setCalculateOsmAndRouteParts(settings.GPX_ROUTE_CALC_OSMAND_PARTS.get()); params.setCalculateOsmAndRoute(settings.GPX_ROUTE_CALC.get()); + params.setSelectedSegment(settings.GPX_ROUTE_SEGMENT.get()); List ps = params.getPoints(settings.getContext()); mapActivity.getRoutingHelper().setGpxParams(params); settings.FOLLOW_THE_GPX_ROUTE.set(result.path); @@ -539,17 +543,17 @@ public class MapActivityActions implements DialogProvider { } public void enterRoutePlanningModeGivenGpx(GPXFile gpxFile, LatLon from, PointDescription fromName, - boolean useIntermediatePointsByDefault, boolean showMenu) { + boolean useIntermediatePointsByDefault, boolean showMenu) { enterRoutePlanningModeGivenGpx(gpxFile, from, fromName, useIntermediatePointsByDefault, showMenu, MapRouteInfoMenu.DEFAULT_MENU_STATE); } public void enterRoutePlanningModeGivenGpx(GPXFile gpxFile, LatLon from, PointDescription fromName, - boolean useIntermediatePointsByDefault, boolean showMenu, int menuState) { + boolean useIntermediatePointsByDefault, boolean showMenu, int menuState) { enterRoutePlanningModeGivenGpx(gpxFile, null, from, fromName, useIntermediatePointsByDefault, showMenu, menuState); } public void enterRoutePlanningModeGivenGpx(GPXFile gpxFile, ApplicationMode appMode, LatLon from, PointDescription fromName, - boolean useIntermediatePointsByDefault, boolean showMenu, int menuState) { + boolean useIntermediatePointsByDefault, boolean showMenu, int menuState) { settings.USE_INTERMEDIATE_POINTS_NAVIGATION.set(useIntermediatePointsByDefault); OsmandApplication app = mapActivity.getMyApplication(); TargetPointsHelper targets = app.getTargetPointsHelper(); @@ -839,6 +843,26 @@ public class MapActivityActions implements DialogProvider { } }).createItem()); + final OsmandMonitoringPlugin monitoringPlugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); + if (monitoringPlugin != null) { + optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.map_widget_monitoring, mapActivity) + .setId(DRAWER_TRIP_RECORDING_ID) + .setIcon(R.drawable.ic_action_track_recordable) + .setListener(new ItemClickListener() { + @Override + public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int pos, boolean isChecked, int[] viewCoordinates) { + app.logEvent("trip_recording_open"); + MapActivity.clearPrevActivityIntent(); + if (monitoringPlugin.hasDataToSave() || monitoringPlugin.wasTrackMonitored()) { + TripRecordingActiveBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), monitoringPlugin.getCurrentTrack()); + } else { + TripRecordingBottomSheet.showInstance(mapActivity.getSupportFragmentManager()); + } + return true; + } + }).createItem()); + } + optionsMenuHelper.addItem(new ItemBuilder().setTitleId(R.string.get_directions, mapActivity) .setId(DRAWER_DIRECTIONS_ID) @@ -1087,7 +1111,7 @@ public class MapActivityActions implements DialogProvider { } private String getProfileDescription(OsmandApplication app, ApplicationMode mode, - Map profilesObjects, String defaultDescription) { + Map profilesObjects, String defaultDescription) { String description = defaultDescription; String routingProfileKey = mode.getRoutingProfile(); diff --git a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java index e173a49ad1..a4149d1590 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java @@ -19,6 +19,7 @@ import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.Version; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; @@ -78,6 +79,9 @@ public class SavingTrackHelper extends SQLiteOpenHelper { private SelectedGpxFile currentTrack; private int points; private int trkPoints = 0; + private long lastTimeFileSaved; + + private ApplicationMode lastRoutingApplicationMode; public SavingTrackHelper(OsmandApplication ctx) { super(ctx, DATABASE_NAME, null, DATABASE_VERSION); @@ -255,6 +259,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { GPXTrackAnalysis analysis = gpx.getAnalysis(fout.lastModified()); GpxDataItem item = new GpxDataItem(fout, analysis); ctx.getGpxDbHelper().add(item); + lastTimeFileSaved = fout.lastModified(); } } } @@ -440,12 +445,15 @@ public class SavingTrackHelper extends SQLiteOpenHelper { } else { heading = NO_HEADING; } + if (ctx.getRoutingHelper().isFollowingMode()) { + lastRoutingApplicationMode = settings.getApplicationMode(); + } boolean record = false; if (location != null && OsmAndLocationProvider.isNotSimulatedLocation(location) && OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null) { if (settings.SAVE_TRACK_TO_GPX.get() && locationTime - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get() - && ctx.getRoutingHelper().isFollowingMode()) { + && lastRoutingApplicationMode == settings.getApplicationMode()) { record = true; } else if (settings.SAVE_GLOBAL_TRACK_TO_GPX.get() && locationTime - lastTimeUpdated > settings.SAVE_GLOBAL_TRACK_INTERVAL.get()) { @@ -705,9 +713,10 @@ public class SavingTrackHelper extends SQLiteOpenHelper { } public boolean getIsRecording() { + OsmandSettings settings = ctx.getSettings(); return OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null - && ctx.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.get() - || (ctx.getSettings().SAVE_TRACK_TO_GPX.get() && ctx.getRoutingHelper().isFollowingMode()); + && settings.SAVE_GLOBAL_TRACK_TO_GPX.get() || settings.SAVE_TRACK_TO_GPX.get() + && lastRoutingApplicationMode == settings.getApplicationMode(); } public float getDistance() { @@ -730,6 +739,14 @@ public class SavingTrackHelper extends SQLiteOpenHelper { return lastTimeUpdated; } + public long getLastTimeFileSaved() { + return lastTimeFileSaved; + } + + public void setLastTimeFileSaved(long lastTimeFileSaved) { + this.lastTimeFileSaved = lastTimeFileSaved; + } + public GPXFile getCurrentGpx() { return currentTrack.getGpxFile(); } diff --git a/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java b/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java index d898505b76..46708ce5ae 100644 --- a/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java +++ b/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java @@ -134,6 +134,9 @@ public class FailSafeFuntions { if(settings.GPX_ROUTE_CALC.get()) { gpxRoute.setCalculateOsmAndRoute(true); } + if (settings.GPX_ROUTE_SEGMENT.get() != -1) { + gpxRoute.setSelectedSegment(settings.GPX_ROUTE_SEGMENT.get()); + } } else { gpxRoute = null; } diff --git a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java index 88800d8fa0..86e42b0d51 100644 --- a/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/base/MenuBottomSheetDialogFragment.java @@ -380,7 +380,7 @@ public abstract class MenuBottomSheetDialogFragment extends BottomSheetDialogFra AndroidUiHelper.updateVisibility(dismissButton, buttonTextId != DEFAULT_VALUE); } - private void setupRightButton() { + protected void setupRightButton() { rightButton = buttonsContainer.findViewById(R.id.right_bottom_button); rightButton.getLayoutParams().height = getRightButtonHeight(); int buttonTextId = getRightBottomButtonTextId(); diff --git a/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java new file mode 100644 index 0000000000..60b507c972 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/base/SelectMultipleItemsBottomSheet.java @@ -0,0 +1,301 @@ +package net.osmand.plus.base; + +import android.content.res.ColorStateList; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.content.ContextCompat; +import androidx.core.widget.CompoundButtonCompat; +import androidx.fragment.app.FragmentManager; + +import net.osmand.AndroidUtils; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton.Builder; +import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.SimpleDividerItem; +import net.osmand.util.Algorithms; +import net.osmand.view.ThreeStateCheckbox; + +import java.util.ArrayList; +import java.util.List; + +import static net.osmand.view.ThreeStateCheckbox.State.CHECKED; +import static net.osmand.view.ThreeStateCheckbox.State.MISC; +import static net.osmand.view.ThreeStateCheckbox.State.UNCHECKED; + +public class SelectMultipleItemsBottomSheet extends MenuBottomSheetDialogFragment { + + private OsmandApplication app; + private UiUtilities uiUtilities; + + private TextView title; + private TextView description; + private TextView applyButtonTitle; + private TextView checkBoxTitle; + private TextView selectedSize; + private ThreeStateCheckbox checkBox; + + private int activeColorRes; + private int secondaryColorRes; + + private final List allItems = new ArrayList<>(); + private final List selectedItems = new ArrayList<>(); + private SelectionUpdateListener selectionUpdateListener; + private OnApplySelectionListener onApplySelectionListener; + + public static final String TAG = SelectMultipleItemsBottomSheet.class.getSimpleName(); + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { + View mainView = super.onCreateView(inflater, parent, savedInstanceState); + onSelectedItemsChanged(); + return mainView; + } + + @Override + public void createMenuItems(Bundle savedInstanceState) { + app = requiredMyApplication(); + uiUtilities = app.getUIUtilities(); + activeColorRes = nightMode ? R.color.icon_color_active_dark : R.color.icon_color_active_light; + secondaryColorRes = nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light; + + items.add(createTitleItem()); + items.add(new SimpleDividerItem(app)); + createListItems(); + } + + private BaseBottomSheetItem createTitleItem() { + LayoutInflater themedInflater = UiUtilities.getInflater(requireContext(), nightMode); + View view = themedInflater.inflate(R.layout.settings_group_title, null); + + checkBox = view.findViewById(R.id.check_box); + checkBoxTitle = view.findViewById(R.id.check_box_title); + description = view.findViewById(R.id.description); + selectedSize = view.findViewById(R.id.selected_size); + title = view.findViewById(R.id.title); + View selectAllButton = view.findViewById(R.id.select_all_button); + selectAllButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + checkBox.performClick(); + boolean checked = checkBox.getState() == CHECKED; + if (checked) { + selectedItems.addAll(allItems); + } else { + selectedItems.clear(); + } + onSelectedItemsChanged(); + updateItems(checked); + } + }); + return new SimpleBottomSheetItem.Builder().setCustomView(view).create(); + } + + private void createListItems() { + for (final SelectableItem item : allItems) { + boolean checked = selectedItems.contains(item); + final BottomSheetItemWithCompoundButton[] uiItem = new BottomSheetItemWithCompoundButton[1]; + final Builder builder = (BottomSheetItemWithCompoundButton.Builder) new Builder(); + builder.setChecked(checked) + .setButtonTintList(AndroidUtils.createCheckedColorStateList(app, secondaryColorRes, activeColorRes)) + .setLayoutId(R.layout.bottom_sheet_item_with_descr_and_checkbox_56dp) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean checked = !uiItem[0].isChecked(); + uiItem[0].setChecked(checked); + SelectableItem tag = (SelectableItem) uiItem[0].getTag(); + if (checked) { + selectedItems.add(tag); + } else { + selectedItems.remove(tag); + } + onSelectedItemsChanged(); + } + }) + .setTag(item); + setupListItem(builder, item); + uiItem[0] = builder.create(); + items.add(uiItem[0]); + } + } + + @Override + protected void setupRightButton() { + super.setupRightButton(); + applyButtonTitle = rightButton.findViewById(R.id.button_text); + } + + @Override + protected void onRightBottomButtonClick() { + if (onApplySelectionListener != null) { + onApplySelectionListener.onSelectionApplied(selectedItems); + } + dismiss(); + } + + private void onSelectedItemsChanged() { + updateSelectAllButton(); + updateSelectedSizeView(); + updateApplyButtonEnable(); + if (selectionUpdateListener != null) { + selectionUpdateListener.onSelectionUpdate(); + } + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_apply; + } + + @Override + protected boolean useVerticalButtons() { + return true; + } + + private void setupListItem(Builder builder, SelectableItem item) { + builder.setTitle(item.title); + builder.setDescription(item.description); + builder.setIcon(uiUtilities.getIcon(item.iconId, activeColorRes)); + } + + private void updateSelectAllButton() { + String checkBoxTitle; + if (Algorithms.isEmpty(selectedItems)) { + checkBox.setState(UNCHECKED); + checkBoxTitle = getString(R.string.shared_string_select_all); + } else { + checkBox.setState(selectedItems.containsAll(allItems) ? CHECKED : MISC); + checkBoxTitle = getString(R.string.shared_string_deselect_all); + } + int checkBoxColor = checkBox.getState() == UNCHECKED ? secondaryColorRes : activeColorRes; + CompoundButtonCompat.setButtonTintList(checkBox, ColorStateList.valueOf(ContextCompat.getColor(app, checkBoxColor))); + this.checkBoxTitle.setText(checkBoxTitle); + } + + private void updateSelectedSizeView() { + String selected = String.valueOf(selectedItems.size()); + String all = String.valueOf(allItems.size()); + selectedSize.setText(getString(R.string.ltr_or_rtl_combine_via_slash, selected, all)); + } + + private void updateApplyButtonEnable() { + if (Algorithms.isEmpty(selectedItems)) { + rightButton.setEnabled(false); + } else { + rightButton.setEnabled(true); + } + } + + private void updateItems(boolean checked) { + for (BaseBottomSheetItem item : items) { + if (item instanceof BottomSheetItemWithCompoundButton) { + ((BottomSheetItemWithCompoundButton) item).setChecked(checked); + } + } + } + + public void setTitle(@NonNull String title) { + this.title.setText(title); + } + + public void setDescription(@NonNull String description) { + this.description.setText(description); + } + + public void setConfirmButtonTitle(@NonNull String confirmButtonTitle) { + applyButtonTitle.setText(confirmButtonTitle); + } + + private void setItems(List allItems) { + if (!Algorithms.isEmpty(allItems)) { + this.allItems.addAll(allItems); + } + } + + private void setSelectedItems(List selected) { + if (!Algorithms.isEmpty(selected)) { + this.selectedItems.addAll(selected); + } + } + + public List getSelectedItems() { + return selectedItems; + } + + @Override + public void onPause() { + super.onPause(); + if (requireActivity().isChangingConfigurations()) { + dismiss(); + } + } + + public static SelectMultipleItemsBottomSheet showInstance(@NonNull AppCompatActivity activity, + @NonNull List items, + @Nullable List selected, + boolean usedOnMap) { + SelectMultipleItemsBottomSheet fragment = new SelectMultipleItemsBottomSheet(); + fragment.setUsedOnMap(usedOnMap); + fragment.setItems(items); + fragment.setSelectedItems(selected); + FragmentManager fm = activity.getSupportFragmentManager(); + fragment.show(fm, TAG); + return fragment; + } + + public void setSelectionUpdateListener(SelectionUpdateListener selectionUpdateListener) { + this.selectionUpdateListener = selectionUpdateListener; + } + + public void setOnApplySelectionListener(OnApplySelectionListener onApplySelectionListener) { + this.onApplySelectionListener = onApplySelectionListener; + } + + public interface SelectionUpdateListener { + void onSelectionUpdate(); + } + + public interface OnApplySelectionListener { + void onSelectionApplied(List selectedItems); + } + + public static class SelectableItem { + private String title; + private String description; + private int iconId; + private Object object; + + public void setTitle(String title) { + this.title = title; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setIconId(int iconId) { + this.iconId = iconId; + } + + public void setObject(Object object) { + this.object = object; + } + + public Object getObject() { + return object; + } + } + +} diff --git a/OsmAnd/src/net/osmand/plus/download/AbstractDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/AbstractDownloadActivity.java index 9bcb0ebb6c..93b887ea1c 100644 --- a/OsmAnd/src/net/osmand/plus/download/AbstractDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/AbstractDownloadActivity.java @@ -17,7 +17,7 @@ public class AbstractDownloadActivity extends ActionBarProgressActivity { downloadValidationManager.startDownload(this, indexItem); } - public void makeSureUserCancelDownload(IndexItem item) { + public void makeSureUserCancelDownload(DownloadItem item) { downloadValidationManager.makeSureUserCancelDownload(this, item); } } diff --git a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java index 16067b7c04..885e28fdf6 100644 --- a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java @@ -56,7 +56,8 @@ public class CustomIndexItem extends IndexItem { } @Override - public File getTargetFile(OsmandApplication ctx) { + @NonNull + public File getTargetFile(@NonNull OsmandApplication ctx) { String basename = getTranslatedBasename(); if (!Algorithms.isEmpty(subfolder)) { basename = subfolder + "/" + basename; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index d076f329f0..bc92fc050f 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -1,7 +1,6 @@ package net.osmand.plus.download; import android.Manifest; -import android.annotation.SuppressLint; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index 298e3fc287..071a856454 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -2,6 +2,8 @@ package net.osmand.plus.download; import android.content.Context; +import androidx.annotation.NonNull; + import net.osmand.AndroidUtils; import net.osmand.IndexConstants; import net.osmand.map.OsmandRegions; @@ -18,6 +20,7 @@ import java.io.IOException; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Locale; @@ -113,6 +116,10 @@ public class DownloadActivityType { public static DownloadActivityType getIndexType(String tagName) { return byTag.get(tagName); } + + public static Collection values() { + return byTag.values(); + } protected static String addVersionToExt(String ext, int version) { return "_" + version + ext; @@ -318,7 +325,7 @@ public class DownloadActivityType { } } - public String getVisibleDescription(IndexItem indexItem, Context ctx) { + public String getVisibleDescription(DownloadItem downloadItem, Context ctx) { if (this == SRTM_COUNTRY_FILE) { return ctx.getString(R.string.download_srtm_maps); } else if (this == WIKIPEDIA_FILE) { @@ -337,20 +344,20 @@ public class DownloadActivityType { return ""; } - public String getVisibleName(IndexItem indexItem, Context ctx, OsmandRegions osmandRegions, boolean includingParent) { + public String getVisibleName(DownloadItem downloadItem, Context ctx, OsmandRegions osmandRegions, boolean includingParent) { if (this == VOICE_FILE) { - String fileName = indexItem.fileName; + String fileName = downloadItem.getFileName(); if (fileName.endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP)) { - return FileNameTranslationHelper.getVoiceName(ctx, getBasename(indexItem)); + return FileNameTranslationHelper.getVoiceName(ctx, getBasename(downloadItem)); } else if (fileName.endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) { - return FileNameTranslationHelper.getVoiceName(ctx, getBasename(indexItem)); + return FileNameTranslationHelper.getVoiceName(ctx, getBasename(downloadItem)); } - return getBasename(indexItem); + return getBasename(downloadItem); } if (this == FONT_FILE) { - return FileNameTranslationHelper.getFontName(ctx, getBasename(indexItem)); + return FileNameTranslationHelper.getFontName(ctx, getBasename(downloadItem)); } - final String basename = getBasename(indexItem); + final String basename = getBasename(downloadItem); if (basename.endsWith(FileNameTranslationHelper.WIKI_NAME)) { return FileNameTranslationHelper.getWikiName(ctx, basename); } @@ -441,9 +448,11 @@ public class DownloadActivityType { return fileName; } + @NonNull + public String getBasename(@NonNull DownloadItem downloadItem) { + String fileName = downloadItem.getFileName(); + if (Algorithms.isEmpty(fileName)) return fileName; - public String getBasename(IndexItem indexItem) { - String fileName = indexItem.fileName; if (fileName.endsWith(IndexConstants.EXTRA_ZIP_EXT)) { return fileName.substring(0, fileName.length() - IndexConstants.EXTRA_ZIP_EXT.length()); } @@ -458,7 +467,7 @@ public class DownloadActivityType { if (fileName.endsWith(IndexConstants.SQLITE_EXT)) { return fileName.substring(0, fileName.length() - IndexConstants.SQLITE_EXT.length()); } - if (indexItem.getType() == WIKIVOYAGE_FILE && + if (downloadItem.getType() == WIKIVOYAGE_FILE && fileName.endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT)) { return fileName.substring(0, fileName.length() - IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT.length()); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 9d47f57762..20ac836139 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -239,6 +239,16 @@ public class DownloadIndexesThread { } } + public void cancelDownload(DownloadItem item) { + if (item instanceof MultipleIndexItem) { + MultipleIndexItem multipleIndexItem = (MultipleIndexItem) item; + cancelDownload(multipleIndexItem.getAllIndexes()); + } else if (item instanceof IndexItem) { + IndexItem indexItem = (IndexItem) item; + cancelDownload(indexItem); + } + } + public void cancelDownload(IndexItem item) { app.logMapDownloadEvent("cancel", item); if (currentDownloadingItem == item) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadItem.java b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java new file mode 100644 index 0000000000..366f3554a7 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/DownloadItem.java @@ -0,0 +1,81 @@ +package net.osmand.plus.download; + +import android.content.Context; + +import androidx.annotation.NonNull; + +import net.osmand.map.OsmandRegions; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; + +import java.io.File; +import java.util.List; +import java.util.Locale; + +public abstract class DownloadItem { + + protected DownloadActivityType type; + protected DownloadResourceGroup relatedGroup; + + public DownloadItem(DownloadActivityType type) { + this.type = type; + } + + public DownloadActivityType getType() { + return type; + } + + public void setRelatedGroup(DownloadResourceGroup relatedGroup) { + this.relatedGroup = relatedGroup; + } + + public DownloadResourceGroup getRelatedGroup() { + return relatedGroup; + } + + @NonNull + public String getSizeDescription(Context ctx) { + return getFormattedMb(ctx, getSizeToDownloadInMb()); + } + + public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { + return type.getVisibleName(this, ctx, osmandRegions, true); + } + + public String getVisibleName(Context ctx, OsmandRegions osmandRegions, boolean includingParent) { + return type.getVisibleName(this, ctx, osmandRegions, includingParent); + } + + public String getVisibleDescription(OsmandApplication ctx) { + return type.getVisibleDescription(this, ctx); + } + + @NonNull + public String getBasename() { + return type.getBasename(this); + } + + protected abstract double getSizeToDownloadInMb(); + + public abstract double getArchiveSizeMB(); + + public abstract boolean isDownloaded(); + + public abstract boolean isOutdated(); + + public abstract boolean hasActualDataToDownload(); + + public abstract boolean isDownloading(@NonNull DownloadIndexesThread thread); + + public abstract String getFileName(); + + @NonNull + public abstract List getDownloadedFiles(@NonNull OsmandApplication app); + + @NonNull + public static String getFormattedMb(@NonNull Context ctx, double sizeInMb) { + String size = String.format(Locale.US, "%.2f", sizeInMb); + return ctx.getString(R.string.ltr_or_rtl_combine_via_space, size, "MB"); + } + +} diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java index 416e20bc94..5d34c95ec4 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java @@ -8,6 +8,7 @@ import net.osmand.map.OsmandRegions; import net.osmand.map.WorldRegion; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.util.Algorithms; import java.util.ArrayList; import java.util.Collections; @@ -20,8 +21,8 @@ public class DownloadResourceGroup { private final DownloadResourceGroupType type; private final DownloadResourceGroup parentGroup; - // ASSERT: individualResources are not empty if and only if groups are empty - private final List individualResources; + // ASSERT: individualDownloadItems are not empty if and only if groups are empty + private final List individualDownloadItems; private final List groups; protected final String id; @@ -107,10 +108,10 @@ public class DownloadResourceGroup { public DownloadResourceGroup(DownloadResourceGroup parentGroup, DownloadResourceGroupType type, String id) { boolean flat = type.containsIndexItem(); if (flat) { - this.individualResources = new ArrayList(); + this.individualDownloadItems = new ArrayList(); this.groups = null; } else { - this.individualResources = null; + this.individualDownloadItems = null; this.groups = new ArrayList(); } this.id = id; @@ -173,7 +174,7 @@ public class DownloadResourceGroup { DownloadResourceGroup regionMaps = getSubGroupById(DownloadResourceGroupType.REGION_MAPS.getDefaultId()); if(regionMaps != null && regionMaps.size() == 1 && parentGroup != null && parentGroup.getParentGroup() != null && isEmpty(getSubGroupById(DownloadResourceGroupType.SUBREGIONS.getDefaultId()))) { - IndexItem item = regionMaps.individualResources.get(0); + IndexItem item = regionMaps.getIndividualResources().get(0); DownloadResourceGroup screenParent = parentGroup.getParentGroup(); if(item.getType() == DownloadActivityType.HILLSHADE_FILE) { DownloadResourceGroup hillshades = @@ -183,7 +184,7 @@ public class DownloadResourceGroup { screenParent.addGroup(hillshades); } hillshades.addItem(item); - regionMaps.individualResources.remove(0); + regionMaps.individualDownloadItems.remove(0); } else if (item.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { DownloadResourceGroup hillshades = screenParent .getSubGroupById(DownloadResourceGroupType.SRTM_HEADER.getDefaultId()); @@ -192,7 +193,7 @@ public class DownloadResourceGroup { screenParent.addGroup(hillshades); } hillshades.addItem(item); - regionMaps.individualResources.remove(0); + regionMaps.individualDownloadItems.remove(0); } } @@ -221,35 +222,38 @@ public class DownloadResourceGroup { } } groups.add(g); - if (g.individualResources != null) { - final net.osmand.Collator collator = OsmAndCollator.primaryCollator(); - final OsmandApplication app = getRoot().app; - final OsmandRegions osmandRegions = app.getRegions(); - Collections.sort(g.individualResources, new Comparator() { - @Override - public int compare(IndexItem lhs, IndexItem rhs) { - int lli = lhs.getType().getOrderIndex(); - int rri = rhs.getType().getOrderIndex(); - if(lli < rri) { - return -1; - } else if(lli > rri) { - return 1; - } + sortDownloadItems(g.individualDownloadItems); + } - return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), - rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); + protected void sortDownloadItems(List items) { + if (Algorithms.isEmpty(items)) return; + final net.osmand.Collator collator = OsmAndCollator.primaryCollator(); + final OsmandApplication app = getRoot().app; + final OsmandRegions osmandRegions = app.getRegions(); + Collections.sort(items, new Comparator() { + @Override + public int compare(DownloadItem firstItem, DownloadItem secondItem) { + int firstOrder = firstItem.getType().getOrderIndex(); + int secondOrder = secondItem.getType().getOrderIndex(); + if(firstOrder < secondOrder) { + return -1; + } else if(firstOrder > secondOrder) { + return 1; } - }); - } + String firstName = firstItem.getVisibleName(app, osmandRegions); + String secondName = secondItem.getVisibleName(app, osmandRegions); + return collator.compare(firstName, secondName); + } + }); } - public void addItem(IndexItem i) { + public void addItem(DownloadItem i) { i.setRelatedGroup(this); - individualResources.add(i); + individualDownloadItems.add(i); } public boolean isEmpty() { - return isEmpty(individualResources) && isEmpty(groups); + return isEmpty(individualDownloadItems) && isEmpty(groups); } private boolean isEmpty(List l) { @@ -265,7 +269,7 @@ public class DownloadResourceGroup { } public int size() { - return groups != null ? groups.size() : individualResources.size(); + return groups != null ? groups.size() : individualDownloadItems.size(); } public DownloadResourceGroup getGroupByIndex(int ind) { @@ -275,9 +279,9 @@ public class DownloadResourceGroup { return null; } - public IndexItem getItemByIndex(int ind) { - if (individualResources != null && ind >= 0 && ind < individualResources.size()) { - return individualResources.get(ind); + public DownloadItem getItemByIndex(int ind) { + if (individualDownloadItems != null && ind >= 0 && ind < individualDownloadItems.size()) { + return individualDownloadItems.get(ind); } return null; } @@ -306,8 +310,20 @@ public class DownloadResourceGroup { } public List getIndividualResources() { + List individualResources = new ArrayList<>(); + if (individualDownloadItems != null) { + for (DownloadItem item : individualDownloadItems) { + if (item instanceof IndexItem) { + individualResources.add((IndexItem) item); + } + } + } return individualResources; } + + public List getIndividualDownloadItems() { + return individualDownloadItems; + } public WorldRegion getRegion() { return region; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 56d137a696..66a6621851 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -25,10 +25,14 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.ParseException; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; + +import static net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType.REGION_MAPS; public class DownloadResources extends DownloadResourceGroup { private static final String TAG = DownloadResources.class.getSimpleName(); @@ -40,7 +44,7 @@ public class DownloadResources extends DownloadResourceGroup { private Map indexFileNames = new LinkedHashMap<>(); private Map indexActivatedFileNames = new LinkedHashMap<>(); private List rawResources; - private Map > groupByRegion; + private Map> groupByRegion; private List itemsToUpdate = new ArrayList<>(); public static final String WORLD_SEAMARKS_KEY = "world_seamarks"; public static final String WORLD_SEAMARKS_NAME = "World_seamarks"; @@ -55,7 +59,7 @@ public class DownloadResources extends DownloadResourceGroup { this.region = app.getRegions().getWorldRegion(); this.app = app; } - + public List getItemsToUpdate() { return itemsToUpdate; } @@ -260,7 +264,7 @@ public class DownloadResources extends DownloadResourceGroup { } private Map listWithAlternatives(final java.text.DateFormat dateFormat, File file, - final String ext, final Map files) { + final String ext, final Map files) { if (file.isDirectory()) { file.list(new FilenameFilter() { @Override @@ -292,7 +296,7 @@ public class DownloadResources extends DownloadResourceGroup { } return file; } - + private void prepareFilesToUpdate() { List filtered = rawResources; if (filtered != null) { @@ -307,7 +311,7 @@ public class DownloadResources extends DownloadResourceGroup { } } } - + protected boolean prepareData(List resources) { this.rawResources = resources; @@ -332,18 +336,18 @@ public class DownloadResources extends DownloadResourceGroup { DownloadResourceGroup nauticalMapsGroup = new DownloadResourceGroup(this, DownloadResourceGroupType.NAUTICAL_MAPS_GROUP); DownloadResourceGroup nauticalMapsScreen = new DownloadResourceGroup(nauticalMapsGroup, DownloadResourceGroupType.NAUTICAL_MAPS); DownloadResourceGroup nauticalMaps = new DownloadResourceGroup(nauticalMapsGroup, DownloadResourceGroupType.NAUTICAL_MAPS_HEADER); - + DownloadResourceGroup wikivoyageMapsGroup = new DownloadResourceGroup(this, DownloadResourceGroupType.TRAVEL_GROUP); DownloadResourceGroup wikivoyageMapsScreen = new DownloadResourceGroup(wikivoyageMapsGroup, DownloadResourceGroupType.WIKIVOYAGE_MAPS); DownloadResourceGroup wikivoyageMaps = new DownloadResourceGroup(wikivoyageMapsGroup, DownloadResourceGroupType.WIKIVOYAGE_HEADER); - Map > groupByRegion = new LinkedHashMap>(); + Map> groupByRegion = new LinkedHashMap<>(); OsmandRegions regs = app.getRegions(); for (IndexItem ii : resources) { if (ii.getType() == DownloadActivityType.VOICE_FILE) { - if (ii.getFileName().endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)){ + if (ii.getFileName().endsWith(IndexConstants.TTSVOICE_INDEX_EXT_JS)) { voiceTTS.addItem(ii); - } else if (ii.getFileName().endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP)){ + } else if (ii.getFileName().endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP)) { voiceRec.addItem(ii); } continue; @@ -377,7 +381,7 @@ public class DownloadResources extends DownloadResourceGroup { groupByRegion.get(wg).add(ii); } else { if (ii.getFileName().startsWith("World_")) { - if (ii.getFileName().toLowerCase().startsWith(WORLD_SEAMARKS_KEY) || + if (ii.getFileName().toLowerCase().startsWith(WORLD_SEAMARKS_KEY) || ii.getFileName().toLowerCase().startsWith(WORLD_SEAMARKS_OLD_KEY)) { nauticalMaps.addItem(ii); } else { @@ -402,22 +406,22 @@ public class DownloadResources extends DownloadResourceGroup { LinkedList parent = new LinkedList(); DownloadResourceGroup worldSubregions = new DownloadResourceGroup(this, DownloadResourceGroupType.SUBREGIONS); addGroup(worldSubregions); - for(WorldRegion rg : region.getSubregions()) { + for (WorldRegion rg : region.getSubregions()) { queue.add(rg); parent.add(worldSubregions); } - while(!queue.isEmpty()) { + while (!queue.isEmpty()) { WorldRegion reg = queue.pollFirst(); DownloadResourceGroup parentGroup = parent.pollFirst(); List subregions = reg.getSubregions(); DownloadResourceGroup mainGrp = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION, reg.getRegionId()); mainGrp.region = reg; parentGroup.addGroup(mainGrp); - + List list = groupByRegion.get(reg); - if(list != null) { - DownloadResourceGroup flatFiles = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.REGION_MAPS); - for(IndexItem ii : list) { + if (list != null) { + DownloadResourceGroup flatFiles = new DownloadResourceGroup(mainGrp, REGION_MAPS); + for (IndexItem ii : list) { flatFiles.addItem(ii); } mainGrp.addGroup(flatFiles); @@ -425,10 +429,10 @@ public class DownloadResources extends DownloadResourceGroup { DownloadResourceGroup subRegions = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.SUBREGIONS); mainGrp.addGroup(subRegions); // add to processing queue - for(WorldRegion rg : subregions) { + for (WorldRegion rg : subregions) { queue.add(rg); parent.add(subRegions); - } + } } // Possible improvements // 1. if there is no subregions no need to create resource group REGIONS_MAPS - objection raise diversity and there is no value @@ -455,8 +459,8 @@ public class DownloadResources extends DownloadResourceGroup { } otherGroup.addGroup(voiceScreenTTS); otherGroup.addGroup(voiceScreenRec); - - + + if (fonts.getIndividualResources() != null) { otherGroup.addGroup(fontScreen); } @@ -465,9 +469,85 @@ public class DownloadResources extends DownloadResourceGroup { createHillshadeSRTMGroups(); trimEmptyGroups(); updateLoadedFiles(); + collectMultipleIndexesItems(region); return true; } + private void collectMultipleIndexesItems(@NonNull WorldRegion region) { + List subRegions = region.getSubregions(); + if (Algorithms.isEmpty(subRegions)) return; + + DownloadResourceGroup group = getRegionMapsGroup(region); + if (group != null) { + boolean listModified = false; + List indexesList = group.getIndividualResources(); + List regionsToCollect = removeDuplicateRegions(subRegions); + for (DownloadActivityType type : DownloadActivityType.values()) { + if (!doesListContainIndexWithType(indexesList, type)) { + List indexesFromSubRegions = collectIndexesOfType(regionsToCollect, type); + if (indexesFromSubRegions != null) { + group.addItem(new MultipleIndexItem(region, indexesFromSubRegions, type)); + listModified = true; + } + } + } + if (listModified) { + sortDownloadItems(group.getIndividualDownloadItems()); + } + } + for (WorldRegion subRegion : subRegions) { + collectMultipleIndexesItems(subRegion); + } + } + + private DownloadResourceGroup getRegionMapsGroup(WorldRegion region) { + DownloadResourceGroup group = getRegionGroup(region); + if (group != null) { + return group.getSubGroupById(REGION_MAPS.getDefaultId()); + } + return null; + } + + @Nullable + private List collectIndexesOfType(@NonNull List regions, + @NonNull DownloadActivityType type) { + List collectedIndexes = new ArrayList<>(); + for (WorldRegion region : regions) { + List regionIndexes = getIndexItems(region); + boolean found = false; + if (regionIndexes != null) { + for (IndexItem index : regionIndexes) { + if (index.getType() == type) { + found = true; + collectedIndexes.add(index); + break; + } + } + } + if (!found) return null; + } + return collectedIndexes; + } + + private List removeDuplicateRegions(List regions) { + Set duplicates = new HashSet<>(); + for (int i = 0; i < regions.size() - 1; i++) { + WorldRegion r1 = regions.get(i); + for (int j = i + 1; j < regions.size(); j++) { + WorldRegion r2 = regions.get(j); + if (r1.containsRegion(r2)) { + duplicates.add(r2); + } else if (r2.containsRegion(r1)) { + duplicates.add(r1); + } + } + } + for (WorldRegion region : duplicates) { + regions.remove(region); + } + return regions; + } + private void buildRegionsGroups(WorldRegion region, DownloadResourceGroup group) { LinkedList queue = new LinkedList(); LinkedList parent = new LinkedList(); @@ -485,7 +565,7 @@ public class DownloadResources extends DownloadResourceGroup { CustomRegion customRegion = (CustomRegion) reg; List indexItems = customRegion.loadIndexItems(); if (!Algorithms.isEmpty(indexItems)) { - DownloadResourceGroup flatFiles = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.REGION_MAPS); + DownloadResourceGroup flatFiles = new DownloadResourceGroup(mainGrp, REGION_MAPS); for (IndexItem ii : indexItems) { flatFiles.addItem(ii); } @@ -557,7 +637,11 @@ public class DownloadResources extends DownloadResourceGroup { return res; } - public static List findIndexItemsAt(OsmandApplication app, List names, DownloadActivityType type, boolean includeDownloaded, int limit) { + public static List findIndexItemsAt(OsmandApplication app, + List names, + DownloadActivityType type, + boolean includeDownloaded, + int limit) { List res = new ArrayList<>(); OsmandRegions regions = app.getRegions(); DownloadIndexesThread downloadThread = app.getDownloadThread(); @@ -573,8 +657,12 @@ public class DownloadResources extends DownloadResourceGroup { return res; } - private static boolean isIndexItemDownloaded(DownloadIndexesThread downloadThread, DownloadActivityType type, WorldRegion downloadRegion, List res) { - List otherIndexItems = new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); + private static boolean isIndexItemDownloaded(DownloadIndexesThread downloadThread, + DownloadActivityType type, + WorldRegion downloadRegion, + List res) { + List otherIndexItems = + new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); for (IndexItem indexItem : otherIndexItems) { if (indexItem.getType() == type && indexItem.isDownloaded()) { return true; @@ -584,8 +672,24 @@ public class DownloadResources extends DownloadResourceGroup { && isIndexItemDownloaded(downloadThread, type, downloadRegion.getSuperregion(), res); } - private static boolean addIndexItem(DownloadIndexesThread downloadThread, DownloadActivityType type, WorldRegion downloadRegion, List res) { - List otherIndexItems = new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); + private boolean doesListContainIndexWithType(List indexItems, + DownloadActivityType type) { + if (indexItems != null) { + for (IndexItem indexItem : indexItems) { + if (indexItem.getType() == type) { + return true; + } + } + } + return false; + } + + private static boolean addIndexItem(DownloadIndexesThread downloadThread, + DownloadActivityType type, + WorldRegion downloadRegion, + List res) { + List otherIndexItems = + new ArrayList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); for (IndexItem indexItem : otherIndexItems) { if (indexItem.getType() == type && !res.contains(indexItem)) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java index 8e03b2c1e5..6b12fb6713 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadValidationManager.java @@ -192,7 +192,7 @@ public class DownloadValidationManager { } - public void makeSureUserCancelDownload(FragmentActivity ctx, final IndexItem item) { + public void makeSureUserCancelDownload(FragmentActivity ctx, final DownloadItem item) { AlertDialog.Builder bld = new AlertDialog.Builder(ctx); bld.setTitle(ctx.getString(R.string.shared_string_cancel)); bld.setMessage(R.string.confirm_interrupt_download); diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 650ab0679b..da0103fdb0 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -1,24 +1,24 @@ package net.osmand.plus.download; -import android.content.Context; - import androidx.annotation.NonNull; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; -import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.helpers.FileNameTranslationHelper; +import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import java.io.File; import java.io.IOException; import java.text.DateFormat; +import java.util.Collections; import java.util.Date; +import java.util.List; -public class IndexItem implements Comparable { +public class IndexItem extends DownloadItem implements Comparable { private static final Log log = PlatformUtil.getLog(IndexItem.class); String description; @@ -27,43 +27,43 @@ public class IndexItem implements Comparable { long timestamp; long contentSize; long containerSize; - DownloadActivityType type; boolean extra; // Update information boolean outdated; boolean downloaded; long localTimestamp; - DownloadResourceGroup relatedGroup; - - public IndexItem(String fileName, String description, long timestamp, String size, long contentSize, - long containerSize, @NonNull DownloadActivityType tp) { + public IndexItem(String fileName, + String description, + long timestamp, + String size, + long contentSize, + long containerSize, + @NonNull DownloadActivityType type) { + super(type); this.fileName = fileName; this.description = description; this.timestamp = timestamp; this.size = size; this.contentSize = contentSize; this.containerSize = containerSize; - this.type = tp; - } - - public DownloadActivityType getType() { - return type; - } - - public void setRelatedGroup(DownloadResourceGroup relatedGroup) { - this.relatedGroup = relatedGroup; - } - - public DownloadResourceGroup getRelatedGroup() { - return relatedGroup; } + @Override public String getFileName() { return fileName; } + @NonNull + @Override + public List getDownloadedFiles(@NonNull OsmandApplication app) { + File targetFile = getTargetFile(app); + if (targetFile.exists()) { + return Collections.singletonList(targetFile); + } + return Collections.emptyList(); + } public String getDescription() { return description; @@ -89,10 +89,10 @@ public class IndexItem implements Comparable { return ((double)containerSize) / (1 << 20); } - public String getSizeDescription(Context ctx) { - return ctx.getString(R.string.ltr_or_rtl_combine_via_space, size, "MB"); + @Override + protected double getSizeToDownloadInMb() { + return Algorithms.parseDoubleSilently(size, 0.0); } - public DownloadEntry createDownloadEntry(OsmandApplication ctx) { String fileName = this.fileName; @@ -132,11 +132,8 @@ public class IndexItem implements Comparable { return type.getTargetFileName(this); } - public String getBasename() { - return type.getBasename(this); - } - - public File getTargetFile(OsmandApplication ctx) { + @NonNull + public File getTargetFile(@NonNull OsmandApplication ctx) { String basename = getTranslatedBasename(); return new File(type.getDownloadFolder(ctx, this), basename + type.getUnzipExtension(ctx, this)); } @@ -169,6 +166,10 @@ public class IndexItem implements Comparable { } return ""; } + + public String getDate(@NonNull DateFormat dateFormat, boolean remote) { + return remote ? getRemoteDate(dateFormat) : getLocalDate(dateFormat); + } public String getRemoteDate(DateFormat dateFormat) { if(timestamp <= 0) { @@ -178,7 +179,7 @@ public class IndexItem implements Comparable { } - public String getLocalDate(DateFormat dateFormat) { + private String getLocalDate(@NonNull DateFormat dateFormat) { if(localTimestamp <= 0) { return ""; } @@ -190,7 +191,7 @@ public class IndexItem implements Comparable { && getType() != DownloadActivityType.HILLSHADE_FILE && getType() != DownloadActivityType.SLOPE_FILE; } - + public void setOutdated(boolean outdated) { this.outdated = outdated; } @@ -198,7 +199,12 @@ public class IndexItem implements Comparable { public void setDownloaded(boolean downloaded) { this.downloaded = downloaded; } - + + @Override + public boolean hasActualDataToDownload() { + return !isDownloaded() || isOutdated(); + } + public void setLocalTimestamp(long localTimestamp) { this.localTimestamp = localTimestamp; } @@ -211,20 +217,11 @@ public class IndexItem implements Comparable { return downloaded; } - public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { - return type.getVisibleName(this, ctx, osmandRegions, true); + @Override + public boolean isDownloading(@NonNull DownloadIndexesThread thread) { + return thread.isDownloading(this); } - public String getVisibleName(Context ctx, OsmandRegions osmandRegions, boolean includingParent) { - return type.getVisibleName(this, ctx, osmandRegions, includingParent); - } - - public String getVisibleDescription(OsmandApplication clctx) { - return type.getVisibleDescription(this, clctx); - } - - - public String getDate(java.text.DateFormat format) { return format.format(new Date(timestamp)); } diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java new file mode 100644 index 0000000000..5f5ff9c297 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexItem.java @@ -0,0 +1,122 @@ +package net.osmand.plus.download; + +import androidx.annotation.NonNull; + +import net.osmand.map.WorldRegion; +import net.osmand.plus.OsmandApplication; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public class MultipleIndexItem extends DownloadItem { + + private final List items; + + public MultipleIndexItem(@NonNull WorldRegion region, + @NonNull List items, + @NonNull DownloadActivityType type) { + super(type); + this.items = items; + } + + public List getAllIndexes() { + return items; + } + + @Override + public boolean isOutdated() { + for (IndexItem item : items) { + if (item.isOutdated()) { + return true; + } + } + return false; + } + + @Override + public boolean isDownloaded() { + for (IndexItem item : items) { + if (item.isDownloaded()) { + return true; + } + } + return false; + } + + @Override + public boolean isDownloading(@NonNull DownloadIndexesThread thread) { + for (IndexItem item : items) { + if (thread.isDownloading(item)) { + return true; + } + } + return false; + } + + @Override + public String getFileName() { + // The file name is used in many places. + // But in the case of a Multiple Indexes element it's not use in most cases. + // File is not created for Multiple Indexes element, + // and all file names are available in internal IndexItem elements. + + // The only one place where a filename may be needed + // is to generate the base and display names. + // Since these names are generated based on the filename. + // But now we don't need a name for display, + // because on all screens where we now use multiple elements item, + // for display used a type name instead of a file name. + + // Later, if you need a file name, + // you can try to create it based on the WorldRegion + // and file name of one of the internal IndexItem elements. + return ""; + } + + @NonNull + @Override + public List getDownloadedFiles(@NonNull OsmandApplication app) { + List result = new ArrayList<>(); + for (IndexItem item : items) { + result.addAll(item.getDownloadedFiles(app)); + } + return result; + } + + public List getIndexesToDownload() { + List indexesToDownload = new ArrayList<>(); + for (IndexItem item : items) { + if (item.hasActualDataToDownload()) { + indexesToDownload.add(item); + } + } + return indexesToDownload; + } + + @Override + public boolean hasActualDataToDownload() { + return getIndexesToDownload().size() > 0; + } + + @Override + public double getSizeToDownloadInMb() { + double totalSizeMb = 0.0d; + for (IndexItem item : items) { + if (item.hasActualDataToDownload()) { + totalSizeMb += item.getSizeToDownloadInMb(); + } + } + return totalSizeMb; + } + + @Override + public double getArchiveSizeMB() { + double result = 0.0d; + for (IndexItem item : items) { + result += item.getArchiveSizeMB(); + } + return result; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java b/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java new file mode 100644 index 0000000000..c7e9a05b66 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/MultipleIndexesUiHelper.java @@ -0,0 +1,113 @@ +package net.osmand.plus.download; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; + +import net.osmand.map.OsmandRegions; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.base.SelectMultipleItemsBottomSheet; +import net.osmand.plus.base.SelectMultipleItemsBottomSheet.OnApplySelectionListener; +import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectableItem; +import net.osmand.plus.base.SelectMultipleItemsBottomSheet.SelectionUpdateListener; + +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.List; + +public class MultipleIndexesUiHelper { + + public static void showDialog(@NonNull MultipleIndexItem multipleIndexItem, + @NonNull AppCompatActivity activity, + @NonNull final OsmandApplication app, + @NonNull DateFormat dateFormat, + boolean showRemoteDate, + @NonNull final SelectItemsToDownloadListener listener) { + List indexesToDownload = getIndexesToDownload(multipleIndexItem); + List allItems = new ArrayList<>(); + List selectedItems = new ArrayList<>(); + OsmandRegions osmandRegions = app.getRegions(); + for (IndexItem indexItem : multipleIndexItem.getAllIndexes()) { + SelectableItem selectableItem = new SelectableItem(); + selectableItem.setTitle(indexItem.getVisibleName(app, osmandRegions)); + + String size = indexItem.getSizeDescription(app); + String date = indexItem.getDate(dateFormat, showRemoteDate); + String description = app.getString(R.string.ltr_or_rtl_combine_via_bold_point, size, date); + selectableItem.setDescription(description); + + selectableItem.setIconId(indexItem.getType().getIconResource()); + selectableItem.setObject(indexItem); + allItems.add(selectableItem); + + if (indexesToDownload.contains(indexItem)) { + selectedItems.add(selectableItem); + } + } + + final SelectMultipleItemsBottomSheet dialog = + SelectMultipleItemsBottomSheet.showInstance(activity, allItems, selectedItems, true); + + dialog.setSelectionUpdateListener(new SelectionUpdateListener() { + @Override + public void onSelectionUpdate() { + dialog.setTitle(app.getString(R.string.welmode_download_maps)); + String total = app.getString(R.string.shared_string_total); + double sizeToDownload = getDownloadSizeInMb(dialog.getSelectedItems()); + String size = DownloadItem.getFormattedMb(app, sizeToDownload); + String description = + app.getString(R.string.ltr_or_rtl_combine_via_colon, total, size); + dialog.setDescription(description); + String btnTitle = app.getString(R.string.shared_string_download); + if (sizeToDownload > 0) { + btnTitle = app.getString(R.string.ltr_or_rtl_combine_via_dash, btnTitle, size); + } + dialog.setConfirmButtonTitle(btnTitle); + } + }); + + dialog.setOnApplySelectionListener(new OnApplySelectionListener() { + @Override + public void onSelectionApplied(List selectedItems) { + List indexItems = new ArrayList<>(); + for (SelectableItem item : selectedItems) { + Object obj = item.getObject(); + if (obj instanceof IndexItem) { + indexItems.add((IndexItem) obj); + } + } + listener.onItemsToDownloadSelected(indexItems); + } + }); + } + + private static List getIndexesToDownload(MultipleIndexItem multipleIndexItem) { + if (multipleIndexItem.hasActualDataToDownload()) { + // download left regions + return multipleIndexItem.getIndexesToDownload(); + } else { + // download all regions again + return multipleIndexItem.getAllIndexes(); + } + } + + private static double getDownloadSizeInMb(@NonNull List selectableItems) { + List indexItems = new ArrayList<>(); + for (SelectableItem i : selectableItems) { + Object obj = i.getObject(); + if (obj instanceof IndexItem) { + indexItems.add((IndexItem) obj); + } + } + double totalSizeMb = 0.0d; + for (IndexItem item : indexItems) { + totalSizeMb += item.getSizeToDownloadInMb(); + } + return totalSizeMb; + } + + public interface SelectItemsToDownloadListener { + void onItemsToDownloadSelected(List items); + } + +} diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java index a212dd5e31..d2e281bbfc 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupAdapter.java @@ -10,9 +10,9 @@ import android.widget.TextView; import net.osmand.plus.R; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.download.CustomIndexItem; +import net.osmand.plus.download.DownloadItem; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadResourceGroup; -import net.osmand.plus.download.IndexItem; import java.util.ArrayList; import java.util.List; @@ -52,9 +52,9 @@ public class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapte public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final Object child = getChild(groupPosition, childPosition); - if (child instanceof IndexItem) { + if (child instanceof DownloadItem) { - IndexItem item = (IndexItem) child; + DownloadItem item = (DownloadItem) child; DownloadResourceGroup group = getGroupObj(groupPosition); ItemViewHolder viewHolder; if (convertView != null && convertView.getTag() instanceof ItemViewHolder) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 388efca2bb..fd5d4bc365 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -35,6 +35,7 @@ import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; +import net.osmand.plus.download.DownloadItem; import net.osmand.plus.download.DownloadResourceGroup; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.DownloadValidationManager; @@ -504,10 +505,10 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow DownloadItemFragment downloadItemFragment = DownloadItemFragment.createInstance(regionId, childPosition); ((DownloadActivity) getActivity()).showDialog(getActivity(), downloadItemFragment); - } else if (child instanceof IndexItem) { - IndexItem indexItem = (IndexItem) child; + } else if (child instanceof DownloadItem) { + DownloadItem downloadItem = (DownloadItem) child; ItemViewHolder vh = (ItemViewHolder) v.getTag(); - OnClickListener ls = vh.getRightButtonAction(indexItem, vh.getClickAction(indexItem)); + OnClickListener ls = vh.getRightButtonAction(downloadItem, vh.getClickAction(downloadItem)); ls.onClick(v); return true; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 70041a525e..1a4449132d 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -17,11 +17,14 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.PopupMenu; import androidx.core.view.ViewCompat; +import net.osmand.map.OsmandRegions; import net.osmand.map.WorldRegion; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.LocalIndexHelper.LocalIndexType; @@ -31,11 +34,15 @@ import net.osmand.plus.activities.PluginsFragment; import net.osmand.plus.chooseplan.ChoosePlanDialogFragment; import net.osmand.plus.download.CityItem; import net.osmand.plus.download.CustomIndexItem; +import net.osmand.plus.download.DownloadItem; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadResourceGroup; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; +import net.osmand.plus.download.MultipleIndexesUiHelper; +import net.osmand.plus.download.MultipleIndexesUiHelper.SelectItemsToDownloadListener; +import net.osmand.plus.download.MultipleIndexItem; import net.osmand.plus.download.ui.LocalIndexesFragment.LocalIndexOperationTask; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.inapp.InAppPurchaseHelper; @@ -43,6 +50,7 @@ import net.osmand.util.Algorithms; import java.io.File; import java.text.DateFormat; +import java.util.List; public class ItemViewHolder { @@ -59,17 +67,17 @@ public class ItemViewHolder { private boolean depthContoursPurchased; protected final DownloadActivity context; - + private int textColorPrimary; private int textColorSecondary; - + boolean showTypeInDesc; boolean showTypeInName; boolean showParentRegionName; boolean showRemoteDate; boolean silentCancelDownload; boolean showProgressInDesc; - + private DateFormat dateFormat; @@ -81,7 +89,7 @@ public class ItemViewHolder { ASK_FOR_FULL_VERSION_PURCHASE, ASK_FOR_DEPTH_CONTOURS_PURCHASE } - + public ItemViewHolder(View view, DownloadActivity context) { this.context = context; @@ -104,28 +112,28 @@ public class ItemViewHolder { theme.resolveAttribute(android.R.attr.textColorSecondary, typedValue, true); textColorSecondary = typedValue.data; } - + public void setShowRemoteDate(boolean showRemoteDate) { this.showRemoteDate = showRemoteDate; } - - + + public void setShowParentRegionName(boolean showParentRegionName) { this.showParentRegionName = showParentRegionName; } - + public void setShowProgressInDescr(boolean b) { showProgressInDesc = b; } - + public void setSilentCancelDownload(boolean silentCancelDownload) { this.silentCancelDownload = silentCancelDownload; } - + public void setShowTypeInDesc(boolean showTypeInDesc) { this.showTypeInDesc = showTypeInDesc; } - + public void setShowTypeInName(boolean showTypeInName) { this.showTypeInName = showTypeInName; } @@ -137,24 +145,24 @@ public class ItemViewHolder { depthContoursPurchased = InAppPurchaseHelper.isDepthContoursPurchased(context.getMyApplication()); } - public void bindIndexItem(final IndexItem indexItem) { - bindIndexItem(indexItem, null); + public void bindIndexItem(final DownloadItem downloadItem) { + bindIndexItem(downloadItem, null); } - public void bindIndexItem(final IndexItem indexItem, final String cityName) { + public void bindIndexItem(final DownloadItem downloadItem, final String cityName) { initAppStatusVariables(); - boolean isDownloading = context.getDownloadThread().isDownloading(indexItem); + boolean isDownloading = downloadItem.isDownloading(context.getDownloadThread()); int progress = -1; - if (context.getDownloadThread().getCurrentDownloadingItem() == indexItem) { + if (context.getDownloadThread().getCurrentDownloadingItem() == downloadItem) { progress = context.getDownloadThread().getCurrentDownloadingItemProgress(); } - boolean disabled = checkDisabledAndClickAction(indexItem); + boolean disabled = checkDisabledAndClickAction(downloadItem); /// name and left item String name; if(showTypeInName) { - name = indexItem.getType().getString(context); + name = downloadItem.getType().getString(context); } else { - name = indexItem.getVisibleName(context, context.getMyApplication().getRegions(), showParentRegionName); + name = downloadItem.getVisibleName(context, context.getMyApplication().getRegions(), showParentRegionName); } String text = (!Algorithms.isEmpty(cityName) && !cityName.equals(name) ? cityName + "\n" : "") + name; nameTextView.setText(text); @@ -164,68 +172,104 @@ public class ItemViewHolder { nameTextView.setTextColor(textColorSecondary); } int color = textColorSecondary; - if(indexItem.isDownloaded() && !isDownloading) { - int colorId = indexItem.isOutdated() ? R.color.color_distance : R.color.color_ok; + if(downloadItem.isDownloaded() && !isDownloading) { + int colorId = downloadItem.isOutdated() ? R.color.color_distance : R.color.color_ok; color = context.getResources().getColor(colorId); } - if (indexItem.isDownloaded()) { + if (downloadItem.isDownloaded()) { leftImageView.setImageDrawable(getContentIcon(context, - indexItem.getType().getIconResource(), color)); + downloadItem.getType().getIconResource(), color)); } else if (disabled) { leftImageView.setImageDrawable(getContentIcon(context, - indexItem.getType().getIconResource(), textColorSecondary)); + downloadItem.getType().getIconResource(), textColorSecondary)); } else { leftImageView.setImageDrawable(getContentIcon(context, - indexItem.getType().getIconResource())); + downloadItem.getType().getIconResource())); } descrTextView.setTextColor(textColorSecondary); if (!isDownloading) { progressBar.setVisibility(View.GONE); descrTextView.setVisibility(View.VISIBLE); - if (indexItem instanceof CustomIndexItem && (((CustomIndexItem) indexItem).getSubName(context) != null)) { - descrTextView.setText(((CustomIndexItem) indexItem).getSubName(context)); - } else if (indexItem.getType() == DownloadActivityType.DEPTH_CONTOUR_FILE && !depthContoursPurchased) { + if (downloadItem instanceof CustomIndexItem && (((CustomIndexItem) downloadItem).getSubName(context) != null)) { + descrTextView.setText(((CustomIndexItem) downloadItem).getSubName(context)); + } else if (downloadItem.getType() == DownloadActivityType.DEPTH_CONTOUR_FILE && !depthContoursPurchased) { descrTextView.setText(context.getString(R.string.depth_contour_descr)); - } else if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE - || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE - || indexItem.getType() == DownloadActivityType.SLOPE_FILE) && srtmDisabled) { + } else if ((downloadItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE + || downloadItem.getType() == DownloadActivityType.HILLSHADE_FILE + || downloadItem.getType() == DownloadActivityType.SLOPE_FILE) && srtmDisabled) { if (showTypeInName) { descrTextView.setText(""); } else { - descrTextView.setText(indexItem.getType().getString(context)); + descrTextView.setText(downloadItem.getType().getString(context)); } - } else if (showTypeInDesc) { - descrTextView.setText(indexItem.getType().getString(context) + - " • " + indexItem.getSizeDescription(context) + - " • " + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); + } else if (downloadItem instanceof MultipleIndexItem) { + MultipleIndexItem item = (MultipleIndexItem) downloadItem; + String allRegionsHeader = context.getString(R.string.shared_strings_all_regions); + String regionsHeader = context.getString(R.string.regions); + String allRegionsCount = String.valueOf(item.getAllIndexes().size()); + String leftToDownloadCount = String.valueOf(item.getIndexesToDownload().size()); + String header; + String count; + if (item.hasActualDataToDownload()) { + if (!item.isDownloaded()) { + header = allRegionsHeader; + count = leftToDownloadCount; + } else { + header = regionsHeader; + count = String.format( + context.getString(R.string.ltr_or_rtl_combine_via_slash), + leftToDownloadCount, + allRegionsCount); + } + } else { + header = allRegionsHeader; + count = allRegionsCount; + } + String fullDescription = + context.getString(R.string.ltr_or_rtl_combine_via_colon, header, count); + if (item.hasActualDataToDownload()) { + fullDescription = context.getString( + R.string.ltr_or_rtl_combine_via_bold_point, fullDescription, + item.getSizeDescription(context)); + } + descrTextView.setText(fullDescription); } else { - descrTextView.setText(indexItem.getSizeDescription(context) + " • " + - (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); + IndexItem item = (IndexItem) downloadItem; + String pattern = context.getString(R.string.ltr_or_rtl_combine_via_bold_point); + String type = item.getType().getString(context); + String size = item.getSizeDescription(context); + String date = item.getDate(dateFormat, showRemoteDate); + String fullDescription = String.format(pattern, size, date); + if (showTypeInDesc) { + fullDescription = String.format(pattern, type, fullDescription); + } + descrTextView.setText(fullDescription); } } else { progressBar.setVisibility(View.VISIBLE); progressBar.setIndeterminate(progress == -1); progressBar.setProgress(progress); - + if (showProgressInDesc) { - double mb = indexItem.getArchiveSizeMB(); + double mb = downloadItem.getArchiveSizeMB(); String v ; if (progress != -1) { v = context.getString(R.string.value_downloaded_of_max, mb * progress / 100, mb); } else { v = context.getString(R.string.file_size_in_mb, mb); } - if(showTypeInDesc && indexItem.getType() == DownloadActivityType.ROADS_FILE) { - descrTextView.setText(indexItem.getType().getString(context) + " • " + v); - } else { - descrTextView.setText(v); + String fullDescription = v; + if(showTypeInDesc && downloadItem.getType() == DownloadActivityType.ROADS_FILE) { + fullDescription = context.getString(R.string.ltr_or_rtl_combine_via_bold_point, + downloadItem.getType().getString(context), fullDescription); } + descrTextView.setText(fullDescription); descrTextView.setVisibility(View.VISIBLE); } else { descrTextView.setVisibility(View.GONE); } - + } } @@ -241,44 +285,7 @@ public class ItemViewHolder { } } - protected void download(IndexItem indexItem, DownloadResourceGroup parentOptional) { - boolean handled = false; - if(parentOptional != null) { - WorldRegion region = DownloadResourceGroup.getRegion(parentOptional); - context.setDownloadItem(region, indexItem.getTargetFile(context.getMyApplication()).getAbsolutePath()); - } - if (indexItem.getType() == DownloadActivityType.ROADS_FILE && parentOptional != null) { - for (IndexItem ii : parentOptional.getIndividualResources()) { - if (ii.getType() == DownloadActivityType.NORMAL_FILE) { - if (ii.isDownloaded()) { - handled = true; - confirmDownload(indexItem); - } - break; - } - } - } - if(!handled) { - context.startDownload(indexItem); - } - } - private void confirmDownload(final IndexItem indexItem) { - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(R.string.are_you_sure); - builder.setMessage(R.string.confirm_download_roadmaps); - builder.setNegativeButton(R.string.shared_string_cancel, null).setPositiveButton( - R.string.shared_string_download, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (indexItem != null) { - context.startDownload(indexItem); - } - } - }); - builder.show(); - } - - private boolean checkDisabledAndClickAction(final IndexItem item) { + private boolean checkDisabledAndClickAction(final DownloadItem item) { RightButtonAction clickAction = getClickAction(item); boolean disabled = clickAction != RightButtonAction.DOWNLOAD; OnClickListener action = getRightButtonAction(item, clickAction); @@ -290,48 +297,54 @@ public class ItemViewHolder { } else { rightButton.setVisibility(View.GONE); rightImageButton.setVisibility(View.VISIBLE); - final boolean isDownloading = context.getDownloadThread().isDownloading(item); + final boolean isDownloading = item.isDownloading(context.getDownloadThread()); if (isDownloading) { rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_remove_dark)); rightImageButton.setContentDescription(context.getString(R.string.shared_string_cancel)); - } else if(item.isDownloaded() && !item.isOutdated()) { + } else if(!item.hasActualDataToDownload()) { rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_overflow_menu_white)); rightImageButton.setContentDescription(context.getString(R.string.shared_string_more)); } else { - rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_import)); + rightImageButton.setImageDrawable(getContentIcon(context, getDownloadActionIconId(item))); rightImageButton.setContentDescription(context.getString(R.string.shared_string_download)); } rightImageButton.setOnClickListener(action); } - + return disabled; } + private int getDownloadActionIconId(@NonNull DownloadItem item) { + return item instanceof MultipleIndexItem ? + R.drawable.ic_action_multi_download : + R.drawable.ic_action_import; + } + @SuppressLint("DefaultLocale") - public RightButtonAction getClickAction(final IndexItem indexItem) { + public RightButtonAction getClickAction(final DownloadItem item) { RightButtonAction clickAction = RightButtonAction.DOWNLOAD; - if (indexItem.getBasename().toLowerCase().equals(DownloadResources.WORLD_SEAMARKS_KEY) + if (item.getBasename().toLowerCase().equals(DownloadResources.WORLD_SEAMARKS_KEY) && nauticalPluginDisabled) { clickAction = RightButtonAction.ASK_FOR_SEAMARKS_PLUGIN; - } else if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE - || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE - || indexItem.getType() == DownloadActivityType.SLOPE_FILE) && srtmDisabled) { + } else if ((item.getType() == DownloadActivityType.SRTM_COUNTRY_FILE + || item.getType() == DownloadActivityType.HILLSHADE_FILE + || item.getType() == DownloadActivityType.SLOPE_FILE) && srtmDisabled) { if (srtmNeedsInstallation) { clickAction = RightButtonAction.ASK_FOR_SRTM_PLUGIN_PURCHASE; } else { clickAction = RightButtonAction.ASK_FOR_SRTM_PLUGIN_ENABLE; } - } else if (indexItem.getType() == DownloadActivityType.WIKIPEDIA_FILE + } else if (item.getType() == DownloadActivityType.WIKIPEDIA_FILE && !Version.isPaidVersion(context.getMyApplication())) { clickAction = RightButtonAction.ASK_FOR_FULL_VERSION_PURCHASE; - } else if (indexItem.getType() == DownloadActivityType.DEPTH_CONTOUR_FILE && !depthContoursPurchased) { + } else if (item.getType() == DownloadActivityType.DEPTH_CONTOUR_FILE && !depthContoursPurchased) { clickAction = RightButtonAction.ASK_FOR_DEPTH_CONTOURS_PURCHASE; } return clickAction; } - public OnClickListener getRightButtonAction(final IndexItem item, final RightButtonAction clickAction) { + public OnClickListener getRightButtonAction(final DownloadItem item, final RightButtonAction clickAction) { if (clickAction != RightButtonAction.DOWNLOAD) { return new View.OnClickListener() { @Override @@ -370,7 +383,7 @@ public class ItemViewHolder { } }; } else { - final boolean isDownloading = context.getDownloadThread().isDownloading(item); + final boolean isDownloading = item.isDownloading(context.getDownloadThread()); return new View.OnClickListener() { @Override public void onClick(View v) { @@ -380,8 +393,8 @@ public class ItemViewHolder { } else { context.makeSureUserCancelDownload(item); } - } else if(item.isDownloaded() && !item.isOutdated()){ - contextMenu(v, item, item.getRelatedGroup()); + } else if(!item.hasActualDataToDownload()){ + showContextMenu(v, item, item.getRelatedGroup()); } else { download(item, item.getRelatedGroup()); } @@ -390,52 +403,21 @@ public class ItemViewHolder { } } - protected void contextMenu(View v, final IndexItem indexItem, final DownloadResourceGroup parentOptional) { - final PopupMenu optionsMenu = new PopupMenu(context, v); + protected void showContextMenu(View v, + final DownloadItem downloadItem, + final DownloadResourceGroup parentOptional) { + OsmandApplication app = context.getMyApplication(); + PopupMenu optionsMenu = new PopupMenu(context, v); MenuItem item; - - final File fl = indexItem.getTargetFile(context.getMyApplication()); - if (fl.exists()) { - item = optionsMenu.getMenu().add(R.string.shared_string_remove).setIcon( - context.getMyApplication().getUIUtilities().getThemedIcon(R.drawable.ic_action_remove_dark)); + + final List downloadedFiles = downloadItem.getDownloadedFiles(app); + if (!Algorithms.isEmpty(downloadedFiles)) { + item = optionsMenu.getMenu().add(R.string.shared_string_remove) + .setIcon(getContentIcon(context, R.drawable.ic_action_remove_dark)); item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { - LocalIndexType tp = LocalIndexType.MAP_DATA; - if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) { - tp = LocalIndexType.TILES_DATA; - } else if (indexItem.getType() == DownloadActivityType.SLOPE_FILE) { - tp = LocalIndexType.TILES_DATA; - } else if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { - tp = LocalIndexType.MAP_DATA; - } else if (indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { - tp = LocalIndexType.SRTM_DATA; - } else if (indexItem.getType() == DownloadActivityType.WIKIPEDIA_FILE) { - tp = LocalIndexType.MAP_DATA; - } else if (indexItem.getType() == DownloadActivityType.WIKIVOYAGE_FILE) { - tp = LocalIndexType.MAP_DATA; - } else if (indexItem.getType() == DownloadActivityType.TRAVEL_FILE) { - tp = LocalIndexType.MAP_DATA; - } else if (indexItem.getType() == DownloadActivityType.FONT_FILE) { - tp = LocalIndexType.FONT_DATA; - } else if (indexItem.getType() == DownloadActivityType.VOICE_FILE) { - tp = indexItem.getBasename().contains("tts") ? LocalIndexType.TTS_VOICE_DATA - : LocalIndexType.VOICE_DATA; - } - final LocalIndexInfo info = new LocalIndexInfo(tp, fl, false, context.getMyApplication()); - AlertDialog.Builder confirm = new AlertDialog.Builder(context); - confirm.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new LocalIndexOperationTask(context, null, LocalIndexOperationTask.DELETE_OPERATION) - .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, info); - } - }); - confirm.setNegativeButton(R.string.shared_string_no, null); - String fn = FileNameTranslationHelper.getFileName(context, context.getMyApplication().getRegions(), - indexItem.getVisibleName(context, context.getMyApplication().getRegions())); - confirm.setMessage(context.getString(R.string.delete_confirmation_msg, fn)); - confirm.show(); + confirmRemove(downloadItem, downloadedFiles); return true; } }); @@ -445,14 +427,143 @@ public class ItemViewHolder { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { - download(indexItem, parentOptional); + download(downloadItem, parentOptional); return true; } }); - + optionsMenu.show(); } + protected void download(DownloadItem item, DownloadResourceGroup parentOptional) { + boolean handled = false; + if (parentOptional != null && item instanceof IndexItem) { + IndexItem indexItem = (IndexItem) item; + WorldRegion region = DownloadResourceGroup.getRegion(parentOptional); + context.setDownloadItem(region, indexItem.getTargetFile(context.getMyApplication()).getAbsolutePath()); + } + if (item.getType() == DownloadActivityType.ROADS_FILE && parentOptional != null) { + for (IndexItem ii : parentOptional.getIndividualResources()) { + if (ii.getType() == DownloadActivityType.NORMAL_FILE) { + if (ii.isDownloaded()) { + handled = true; + confirmDownload(item); + } + break; + } + } + } + if(!handled) { + startDownload(item); + } + } + private void confirmDownload(final DownloadItem item) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.are_you_sure); + builder.setMessage(R.string.confirm_download_roadmaps); + builder.setNegativeButton(R.string.shared_string_cancel, null).setPositiveButton( + R.string.shared_string_download, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (item != null) { + startDownload(item); + } + } + }); + builder.show(); + } + + private void startDownload(DownloadItem item) { + if (item instanceof MultipleIndexItem) { + selectIndexesToDownload((MultipleIndexItem) item); + } else if (item instanceof IndexItem) { + IndexItem indexItem = (IndexItem) item; + context.startDownload(indexItem); + } + } + + private void selectIndexesToDownload(MultipleIndexItem item) { + OsmandApplication app = context.getMyApplication(); + MultipleIndexesUiHelper.showDialog(item, context, app, dateFormat, showRemoteDate, + new SelectItemsToDownloadListener() { + @Override + public void onItemsToDownloadSelected(List indexes) { + IndexItem[] indexesArray = new IndexItem[indexes.size()]; + context.startDownload(indexes.toArray(indexesArray)); + } + } + ); + } + + private void confirmRemove(@NonNull final DownloadItem downloadItem, + @NonNull final List downloadedFiles) { + OsmandApplication app = context.getMyApplication(); + AlertDialog.Builder confirm = new AlertDialog.Builder(context); + + String message; + if (downloadedFiles.size() > 1) { + message = context.getString(R.string.delete_number_files_question, downloadedFiles.size()); + } else { + OsmandRegions regions = app.getRegions(); + String visibleName = downloadItem.getVisibleName(context, regions); + String fileName = FileNameTranslationHelper.getFileName(context, regions, visibleName); + message = context.getString(R.string.delete_confirmation_msg, fileName); + } + confirm.setMessage(message); + + confirm.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + LocalIndexType type = getLocalIndexType(downloadItem); + remove(type, downloadedFiles); + } + }); + confirm.setNegativeButton(R.string.shared_string_no, null); + + confirm.show(); + } + + private void remove(@NonNull LocalIndexType type, + @NonNull List filesToDelete) { + OsmandApplication app = context.getMyApplication(); + LocalIndexOperationTask removeTask = new LocalIndexOperationTask( + context, + null, + LocalIndexOperationTask.DELETE_OPERATION); + LocalIndexInfo[] params = new LocalIndexInfo[filesToDelete.size()]; + for (int i = 0; i < filesToDelete.size(); i++) { + File file = filesToDelete.get(i); + params[i] = new LocalIndexInfo(type, file, false, app); + } + removeTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); + } + + @NonNull + private LocalIndexType getLocalIndexType(@NonNull DownloadItem downloadItem) { + LocalIndexType type = LocalIndexType.MAP_DATA; + if (downloadItem.getType() == DownloadActivityType.HILLSHADE_FILE) { + type = LocalIndexType.TILES_DATA; + } else if (downloadItem.getType() == DownloadActivityType.SLOPE_FILE) { + type = LocalIndexType.TILES_DATA; + } else if (downloadItem.getType() == DownloadActivityType.ROADS_FILE) { + type = LocalIndexType.MAP_DATA; + } else if (downloadItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { + type = LocalIndexType.SRTM_DATA; + } else if (downloadItem.getType() == DownloadActivityType.WIKIPEDIA_FILE) { + type = LocalIndexType.MAP_DATA; + } else if (downloadItem.getType() == DownloadActivityType.WIKIVOYAGE_FILE) { + type = LocalIndexType.MAP_DATA; + } else if (downloadItem.getType() == DownloadActivityType.TRAVEL_FILE) { + type = LocalIndexType.MAP_DATA; + } else if (downloadItem.getType() == DownloadActivityType.FONT_FILE) { + type = LocalIndexType.FONT_DATA; + } else if (downloadItem.getType() == DownloadActivityType.VOICE_FILE) { + type = downloadItem.getBasename().contains("tts") ? LocalIndexType.TTS_VOICE_DATA + : LocalIndexType.VOICE_DATA; + } + return type; + } + private Drawable getContentIcon(DownloadActivity context, int resourceId) { return context.getMyApplication().getUIUtilities().getThemedIcon(resourceId); } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java index f7362edc76..faf39a56f7 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java @@ -153,7 +153,8 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download for (IndexItem indexItem : indexItems) { downloadsSize += indexItem.getSize(); } - String updateAllText = getActivity().getString(R.string.update_all, downloadsSize >> 20); + String updateAllText = getActivity().getString( + R.string.update_all, String.valueOf(downloadsSize >> 20)); updateAllButton.setText(updateAllText); updateAllButton.setOnClickListener(new View.OnClickListener() { @Override diff --git a/OsmAnd/src/net/osmand/plus/helpers/TrackSelectSegmentAdapter.java b/OsmAnd/src/net/osmand/plus/helpers/TrackSelectSegmentAdapter.java new file mode 100644 index 0000000000..498f638081 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/helpers/TrackSelectSegmentAdapter.java @@ -0,0 +1,134 @@ +package net.osmand.plus.helpers; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import net.osmand.GPXUtilities.TrkSegment; +import net.osmand.GPXUtilities.WptPt; +import net.osmand.plus.OsmAndFormatter; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.helpers.TrackSelectSegmentAdapter.TrackViewHolder; +import net.osmand.util.MapUtils; + +import java.util.List; + +public class TrackSelectSegmentAdapter extends RecyclerView.Adapter { + + private final OsmandApplication app; + private final LayoutInflater themedInflater; + private final UiUtilities iconsCache; + private final List segments; + private OnItemClickListener onItemClickListener; + + public TrackSelectSegmentAdapter(Context ctx, List segments) { + app = (OsmandApplication) ctx.getApplicationContext(); + themedInflater = UiUtilities.getInflater(ctx, app.getDaynightHelper().isNightModeForMapControls()); + iconsCache = app.getUIUtilities(); + this.segments = segments; + } + + @NonNull + @Override + public TrackViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = themedInflater.inflate(R.layout.gpx_segment_list_item, parent, false); + ImageView distanceIcon = view.findViewById(R.id.distance_icon); + distanceIcon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_split_interval)); + ImageView timeIcon = view.findViewById(R.id.time_icon); + timeIcon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_time_moving_16)); + return new TrackViewHolder(view); + } + + @Override + public void onBindViewHolder(@NonNull final TrackViewHolder holder, int position) { + holder.icon.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_split_interval)); + + TrkSegment segment = segments.get(position); + + String segmentTitle = app.getResources().getString(R.string.segments_count, position + 1); + holder.name.setText(segmentTitle); + + double distance = getDistance(segment); + long time = getSegmentTime(segment); + if (time != 1) { + holder.time.setText(OsmAndFormatter.getFormattedDurationShort((int) (time / 1000))); + } else { + holder.time.setText(""); + } + holder.distance.setText(OsmAndFormatter.getFormattedDistance((float) distance, app)); + holder.itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (onItemClickListener != null) { + onItemClickListener.onItemClick(holder.getAdapterPosition()); + } + } + }); + } + + @Override + public int getItemCount() { + return segments.size(); + } + + public static long getSegmentTime(TrkSegment segment) { + long startTime = Long.MAX_VALUE; + long endTime = Long.MIN_VALUE; + for (int i = 0; i < segment.points.size(); i++) { + WptPt point = segment.points.get(i); + long time = point.time; + if (time != 0) { + startTime = Math.min(startTime, time); + endTime = Math.max(endTime, time); + } + } + return endTime - startTime; + } + + public static double getDistance(TrkSegment segment) { + double distance = 0; + WptPt prevPoint = null; + for (int i = 0; i < segment.points.size(); i++) { + WptPt point = segment.points.get(i); + if (prevPoint != null) { + distance += MapUtils.getDistance(prevPoint.getLatitude(), prevPoint.getLongitude(), point.getLatitude(), point.getLongitude()); + } + prevPoint = point; + } + return distance; + } + + public void setAdapterListener(OnItemClickListener onItemClickListener) { + this.onItemClickListener = onItemClickListener; + } + + public interface OnItemClickListener { + + void onItemClick(int position); + + } + + static class TrackViewHolder extends RecyclerView.ViewHolder { + + ImageView icon; + TextView name; + TextView distance; + TextView time; + + TrackViewHolder(View itemView) { + super(itemView); + icon = itemView.findViewById(R.id.icon); + name = itemView.findViewById(R.id.name); + distance = itemView.findViewById(R.id.distance); + time = itemView.findViewById(R.id.time_interval); + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/monitoring/ClearRecordedDataBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/monitoring/ClearRecordedDataBottomSheetFragment.java new file mode 100644 index 0000000000..e52462fca7 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/monitoring/ClearRecordedDataBottomSheetFragment.java @@ -0,0 +1,99 @@ +package net.osmand.plus.monitoring; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; +import net.osmand.plus.monitoring.TripRecordingActiveBottomSheet.ItemType; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; + +public class ClearRecordedDataBottomSheetFragment extends MenuBottomSheetDialogFragment { + + public static final String TAG = ClearRecordedDataBottomSheetFragment.class.getSimpleName(); + + private OsmandApplication app; + + @Override + public void createMenuItems(Bundle savedInstanceState) { + app = requiredMyApplication(); + LayoutInflater inflater = UiUtilities.getInflater(app, nightMode); + int verticalBig = getResources().getDimensionPixelSize(R.dimen.dialog_content_margin); + int verticalSmall = getResources().getDimensionPixelSize(R.dimen.content_padding_small); + + items.add(new BottomSheetItemWithDescription.Builder() + .setDescription(app.getString(R.string.clear_recorded_data_warning)) + .setDescriptionColorId(!nightMode ? R.color.text_color_primary_light : R.color.text_color_primary_dark) + .setDescriptionMaxLines(2) + .setTitle(app.getString(R.string.clear_recorded_data)) + .setLayoutId(R.layout.bottom_sheet_item_title_with_description) + .create()); + + items.add(new DividerSpaceItem(app, verticalBig)); + + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(TripRecordingActiveBottomSheet.createButton(inflater, ItemType.CLEAR_DATA, nightMode)) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + app.getSavingTrackHelper().clearRecordedData(true); + dismiss(); + } + }) + .create()); + + items.add(new DividerSpaceItem(app, verticalBig)); + + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(TripRecordingActiveBottomSheet.createButton(inflater, ItemType.CANCEL, nightMode)) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + dismiss(); + } + }) + .create()); + + items.add(new DividerSpaceItem(app, verticalSmall)); + } + + @Override + public void onResume() { + super.onResume(); + Fragment target = getTargetFragment(); + if (target instanceof TripRecordingActiveBottomSheet) { + ((TripRecordingActiveBottomSheet) target).hide(); + } + } + + @Override + public void onPause() { + super.onPause(); + Fragment target = getTargetFragment(); + if (target instanceof TripRecordingActiveBottomSheet) { + ((TripRecordingActiveBottomSheet) target).show(); + } + } + + @Override + protected boolean hideButtonsContainer() { + return true; + } + + public static void showInstance(@NonNull FragmentManager fragmentManager, @NonNull Fragment target) { + if (!fragmentManager.isStateSaved()) { + ClearRecordedDataBottomSheetFragment fragment = new ClearRecordedDataBottomSheetFragment(); + fragment.setTargetFragment(target, 0); + fragment.show(fragmentManager, TAG); + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java index 42bae2ab62..a3d2b78bd5 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java @@ -1,9 +1,7 @@ package net.osmand.plus.monitoring; -import android.Manifest; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; @@ -19,18 +17,18 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.AppCompatCheckBox; -import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; import com.google.android.material.slider.Slider; import net.osmand.AndroidUtils; import net.osmand.Location; import net.osmand.ValueHolder; +import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.NavigationService; import net.osmand.plus.OsmAndFormatter; -import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmAndTaskManager.OsmAndTaskRunnable; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -52,8 +50,6 @@ import net.osmand.util.Algorithms; import java.lang.ref.WeakReference; import java.util.List; -import gnu.trove.list.array.TIntArrayList; - import static net.osmand.plus.UiUtilities.CompoundButtonType.PROFILE_DEPENDENT; public class OsmandMonitoringPlugin extends OsmandPlugin { @@ -162,9 +158,9 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } } - public static final int[] SECONDS = new int[] {0, 1, 2, 3, 5, 10, 15, 20, 30, 60, 90}; - public static final int[] MINUTES = new int[] {2, 3, 5}; - public static final int[] MAX_INTERVAL_TO_SEND_MINUTES = new int[] {1, 2, 5, 10, 15, 20, 30, 60, 90, 2 * 60, 3 * 60, 4 * 60, 6 * 60, 12 * 60, 24 * 60}; + public static final int[] SECONDS = new int[]{0, 1, 2, 3, 5, 10, 15, 20, 30, 60, 90}; + public static final int[] MINUTES = new int[]{2, 3, 5}; + public static final int[] MAX_INTERVAL_TO_SEND_MINUTES = new int[]{1, 2, 5, 10, 15, 20, 30, 60, 90, 2 * 60, 3 * 60, 4 * 60, 6 * 60, 12 * 60, 24 * 60}; @Override public SettingsScreenType getSettingsScreenType() { @@ -182,9 +178,10 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { private TextInfoWidget createMonitoringControl(final MapActivity map) { monitoringControl = new TextInfoWidget(map) { long lastUpdateTime; + @Override public boolean updateInfo(DrawSettings drawSettings) { - if(isSaving){ + if (isSaving) { setText(map.getString(R.string.shared_string_save), ""); setIcons(R.drawable.widget_monitoring_rec_big_day, R.drawable.widget_monitoring_rec_big_night); return true; @@ -212,7 +209,7 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } final boolean liveMonitoringEnabled = liveMonitoringHelper.isLiveMonitoringEnabled(); - if(globalRecord) { + if (globalRecord) { //indicates global recording (+background recording) if (liveMonitoringEnabled) { dn = R.drawable.widget_live_monitoring_rec_big_night; @@ -317,8 +314,27 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } } + public SelectedGpxFile getCurrentTrack() { + return app.getSavingTrackHelper().getCurrentTrack(); + } + + public boolean wasTrackMonitored() { + return settings.SAVE_GLOBAL_TRACK_TO_GPX.get(); + } + + public boolean hasDataToSave() { + return app.getSavingTrackHelper().hasDataToSave(); + } + public void controlDialog(final Activity activity, final boolean showTrackSelection) { - final boolean wasTrackMonitored = settings.SAVE_GLOBAL_TRACK_TO_GPX.get(); + FragmentManager fragmentManager = ((FragmentActivity) activity).getSupportFragmentManager(); + if (hasDataToSave() || wasTrackMonitored()) { + TripRecordingActiveBottomSheet.showInstance(fragmentManager, getCurrentTrack()); + } else { + TripRecordingBottomSheet.showInstance(fragmentManager); + } + + /*final boolean wasTrackMonitored = settings.SAVE_GLOBAL_TRACK_TO_GPX.get(); final boolean nightMode; if (activity instanceof MapActivity) { nightMode = app.getDaynightHelper().isNightModeForMapControls(); @@ -415,8 +431,8 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { run.run(); } }); - bld.show(); - } +// bld.show(); + }*/ } public void saveCurrentTrack() { @@ -478,7 +494,7 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } } - public void stopRecording(){ + public void stopRecording() { settings.SAVE_GLOBAL_TRACK_TO_GPX.set(false); if (app.getNavigationService() != null) { app.getNavigationService().stopIfNeeded(app, NavigationService.USED_BY_GPX); @@ -536,7 +552,7 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } public static LinearLayout createIntervalChooseLayout(final OsmandApplication app, - final Context uiCtx, + final Context uiCtx, final String patternMsg, final int[] seconds, final int[] minutes, final ValueHolder choice, final ValueHolder v, @@ -567,11 +583,11 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { public void onValueChange(@NonNull Slider slider, float value, boolean fromUser) { String s; int progress = (int) value; - if(progress == 0) { + if (progress == 0) { s = uiCtx.getString(R.string.int_continuosly); v.value = 0; } else { - if(progress < secondsLength) { + if (progress < secondsLength) { s = seconds[progress] + " " + uiCtx.getString(R.string.int_seconds); v.value = seconds[progress] * 1000; } else { @@ -596,7 +612,7 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } } } - + ll.setOrientation(LinearLayout.VERTICAL); ll.addView(tv); ll.addView(sliderContainer); diff --git a/OsmAnd/src/net/osmand/plus/monitoring/StopTrackRecordingBottomFragment.java b/OsmAnd/src/net/osmand/plus/monitoring/StopTrackRecordingBottomFragment.java new file mode 100644 index 0000000000..ac3ffaf2a5 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/monitoring/StopTrackRecordingBottomFragment.java @@ -0,0 +1,138 @@ +package net.osmand.plus.monitoring; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; +import net.osmand.plus.monitoring.TripRecordingActiveBottomSheet.ItemType; +import net.osmand.plus.settings.backend.OsmandSettings; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; + +public class StopTrackRecordingBottomFragment extends MenuBottomSheetDialogFragment { + + public static final String TAG = StopTrackRecordingBottomFragment.class.getSimpleName(); + + private OsmandApplication app; + private MapActivity mapActivity; + private OsmandSettings settings; + private OsmandMonitoringPlugin plugin; + private ItemType tag = ItemType.CANCEL; + + public void setMapActivity(MapActivity mapActivity) { + this.mapActivity = mapActivity; + } + + @Override + public void createMenuItems(Bundle savedInstanceState) { + app = requiredMyApplication(); + settings = app.getSettings(); + plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class); + LayoutInflater inflater = UiUtilities.getInflater(app, nightMode); + int verticalBig = getResources().getDimensionPixelSize(R.dimen.dialog_content_margin); + int verticalSmall = getResources().getDimensionPixelSize(R.dimen.content_padding_small); + + items.add(new BottomSheetItemWithDescription.Builder() + .setDescription(app.getString(R.string.track_recording_description)) + .setDescriptionColorId(!nightMode ? R.color.text_color_primary_light : R.color.text_color_primary_dark) + .setDescriptionMaxLines(4) + .setTitle(app.getString(R.string.track_recording_title)) + .setLayoutId(R.layout.bottom_sheet_item_title_with_description) + .create()); + + items.add(new DividerSpaceItem(app, verticalBig)); + + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(TripRecordingActiveBottomSheet.createButton(inflater, ItemType.STOP_AND_DISCARD, nightMode)) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + tag = ItemType.STOP_AND_DISCARD; + if (plugin != null && settings.SAVE_GLOBAL_TRACK_TO_GPX.get()) { + plugin.stopRecording(); + app.getNotificationHelper().refreshNotifications(); + } + app.getSavingTrackHelper().clearRecordedData(true); + dismiss(); + } + }) + .create()); + + items.add(new DividerSpaceItem(app, verticalBig)); + + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(TripRecordingActiveBottomSheet.createButton(inflater, ItemType.SAVE_AND_STOP, nightMode)) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + tag = ItemType.SAVE_AND_STOP; + if (plugin != null && settings.SAVE_GLOBAL_TRACK_TO_GPX.get()) { + plugin.saveCurrentTrack(null, mapActivity); + app.getNotificationHelper().refreshNotifications(); + } + dismiss(); + } + }) + .create()); + + items.add(new DividerSpaceItem(app, verticalSmall)); + + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(TripRecordingActiveBottomSheet.createButton(inflater, ItemType.CANCEL, nightMode)) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + tag = ItemType.CANCEL; + dismiss(); + } + }) + .create()); + + items.add(new DividerSpaceItem(app, verticalSmall)); + } + + @Override + public void onResume() { + super.onResume(); + Fragment target = getTargetFragment(); + if (target instanceof TripRecordingActiveBottomSheet) { + ((TripRecordingActiveBottomSheet) target).hide(); + } + } + + @Override + public void onPause() { + super.onPause(); + if (tag == ItemType.CANCEL) { + Fragment target = getTargetFragment(); + if (target instanceof TripRecordingActiveBottomSheet) { + ((TripRecordingActiveBottomSheet) target).show(); + } + } + } + + @Override + protected boolean hideButtonsContainer() { + return true; + } + + public static void showInstance(MapActivity mapActivity, @NonNull FragmentManager fragmentManager, @NonNull Fragment target) { + if (!fragmentManager.isStateSaved()) { + StopTrackRecordingBottomFragment fragment = new StopTrackRecordingBottomFragment(); + fragment.setMapActivity(mapActivity); + fragment.setTargetFragment(target, 0); + fragment.show(fragmentManager, TAG); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingActiveBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingActiveBottomSheet.java new file mode 100644 index 0000000000..6dcca6d355 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingActiveBottomSheet.java @@ -0,0 +1,645 @@ +package net.osmand.plus.monitoring; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.Dialog; +import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.Typeface; +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.util.TypedValue; +import android.text.format.DateUtils; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.ColorRes; +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.appcompat.content.res.AppCompatResources; +import androidx.appcompat.widget.AppCompatImageView; +import androidx.appcompat.widget.SwitchCompat; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.core.content.ContextCompat; +import androidx.core.graphics.drawable.DrawableCompat; +import androidx.fragment.app.FragmentManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.snackbar.Snackbar; + +import net.osmand.AndroidUtils; +import net.osmand.GPXUtilities.GPXFile; +import net.osmand.PlatformUtil; +import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.UiUtilities.DialogButtonType; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.SavingTrackHelper; +import net.osmand.plus.activities.SavingTrackHelper.SaveGpxResult; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.helpers.AndroidUiHelper; +import net.osmand.plus.helpers.FontCache; +import net.osmand.plus.myplaces.SaveCurrentTrackTask; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.track.GpxBlockStatisticsBuilder; +import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener; +import net.osmand.plus.track.TrackAppearanceFragment; +import net.osmand.plus.widgets.TextViewEx; +import net.osmand.util.Algorithms; + +import org.apache.commons.logging.Log; + +import java.lang.ref.WeakReference; + +import static net.osmand.plus.UiUtilities.CompoundButtonType.PROFILE_DEPENDENT; + +public class TripRecordingActiveBottomSheet extends MenuBottomSheetDialogFragment { + + public static final String TAG = TripRecordingActiveBottomSheet.class.getSimpleName(); + private static final Log log = PlatformUtil.getLog(TripRecordingActiveBottomSheet.class); + private static final String UPDATE_CURRENT_GPX_FILE = "update_current_gpx_file"; + private static final int GENERAL_UPDATE_GPS_INTERVAL = 1000; + private static final int GENERAL_UPDATE_SAVE_INTERVAL = 1000; + + private OsmandApplication app; + private OsmandSettings settings; + private SavingTrackHelper helper; + private SelectedGpxFile selectedGpxFile; + + private View statusContainer; + private View buttonSave; + private GpxBlockStatisticsBuilder blockStatisticsBuilder; + + private final Handler handler = new Handler(); + private Runnable updatingGPS; + private Runnable updatingTimeTrackSaved; + + private GPXFile getGPXFile() { + return selectedGpxFile.getGpxFile(); + } + + public void setSelectedGpxFile(SelectedGpxFile selectedGpxFile) { + this.selectedGpxFile = selectedGpxFile; + } + + public boolean hasDataToSave() { + return app.getSavingTrackHelper().hasDataToSave(); + } + + public boolean searchingGPS() { + return app.getLocationProvider().getLastKnownLocation() == null; + } + + public boolean wasTrackMonitored() { + return settings.SAVE_GLOBAL_TRACK_TO_GPX.get(); + } + + public static void showInstance(@NonNull FragmentManager fragmentManager, SelectedGpxFile selectedGpxFile) { + if (!fragmentManager.isStateSaved()) { + TripRecordingActiveBottomSheet fragment = new TripRecordingActiveBottomSheet(); + fragment.setSelectedGpxFile(selectedGpxFile); + fragment.show(fragmentManager, TAG); + } + } + + @Override + public void createMenuItems(Bundle savedInstanceState) { + app = requiredMyApplication(); + settings = app.getSettings(); + helper = app.getSavingTrackHelper(); + LayoutInflater inflater = UiUtilities.getInflater(getContext(), nightMode); + final FragmentManager fragmentManager = getFragmentManager(); + + View itemView = inflater.inflate(R.layout.trip_recording_active_fragment, null, false); + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(itemView) + .create()); + + View buttonClear = itemView.findViewById(R.id.button_clear); + View buttonSegment = itemView.findViewById(R.id.button_segment); + buttonSave = itemView.findViewById(R.id.button_save); + final View buttonPause = itemView.findViewById(R.id.button_pause); + View buttonStop = itemView.findViewById(R.id.button_stop); + + createItem(buttonClear, ItemType.CLEAR_DATA, hasDataToSave(), null); + createItem(buttonSegment, ItemType.START_SEGMENT, wasTrackMonitored(), null); + createItem(buttonPause, wasTrackMonitored() ? ItemType.PAUSE : ItemType.RESUME, true, null); + createItem(buttonStop, ItemType.STOP, true, null); + + statusContainer = itemView.findViewById(R.id.status_container); + updateStatus(); + + RecyclerView statBlocks = itemView.findViewById(R.id.block_statistics); + if (savedInstanceState != null) { + if (savedInstanceState.containsKey(UPDATE_CURRENT_GPX_FILE) + && savedInstanceState.getBoolean(UPDATE_CURRENT_GPX_FILE)) { + selectedGpxFile = app.getSavingTrackHelper().getCurrentTrack(); + } + } + blockStatisticsBuilder = new GpxBlockStatisticsBuilder(app, selectedGpxFile); + blockStatisticsBuilder.setBlocksView(statBlocks); + blockStatisticsBuilder.setBlocksClickable(false); + blockStatisticsBuilder.initStatBlocks(null, ContextCompat.getColor(app, getActiveTextColorId(nightMode)), nightMode); + + LinearLayout showTrackContainer = itemView.findViewById(R.id.show_track_on_map); + showTrackContainer.setMinimumHeight(app.getResources().getDimensionPixelSize(R.dimen.bottom_sheet_list_item_height)); + + final LinearLayout buttonShow = showTrackContainer.findViewById(R.id.basic_item_body); + TextView showTrackTitle = buttonShow.findViewById(R.id.title); + Integer showTitle = ItemType.SHOW_TRACK.getTitleId(); + if (showTitle != null) { + showTrackTitle.setText(showTitle); + } + showTrackTitle.setTextColor(ContextCompat.getColor(app, getActiveIconColorId(nightMode))); + showTrackTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.default_desc_text_size)); + Typeface typeface = FontCache.getFont(app, app.getResources().getString(R.string.font_roboto_medium)); + showTrackTitle.setTypeface(typeface); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + float letterSpacing = AndroidUtils.getFloatValueFromRes(app, R.dimen.description_letter_spacing); + showTrackTitle.setLetterSpacing(letterSpacing); + } + final SwitchCompat showTrackOnMapButton = buttonShow.findViewById(R.id.switch_button); + showTrackOnMapButton.setChecked(app.getSelectedGpxHelper().getSelectedCurrentRecordingTrack() != null); + UiUtilities.setupCompoundButton(showTrackOnMapButton, nightMode, PROFILE_DEPENDENT); + + final LinearLayout buttonAppearance = showTrackContainer.findViewById(R.id.additional_button); + View divider = buttonAppearance.getChildAt(0); + AndroidUiHelper.setVisibility(View.GONE, divider); + int marginS = app.getResources().getDimensionPixelSize(R.dimen.context_menu_padding_margin_small); + UiUtilities.setMargins(buttonAppearance, marginS, 0, 0, 0); + String width = settings.CURRENT_TRACK_WIDTH.get(); + boolean showArrows = settings.CURRENT_TRACK_SHOW_ARROWS.get(); + int color = settings.CURRENT_TRACK_COLOR.get(); + Drawable appearanceDrawable = TrackAppearanceFragment.getTrackIcon(app, width, showArrows, color); + AppCompatImageView appearanceIcon = buttonAppearance.findViewById(R.id.icon_after_divider); + int marginTrackIconH = app.getResources().getDimensionPixelSize(R.dimen.content_padding_small); + UiUtilities.setMargins(appearanceIcon, marginTrackIconH, 0, marginTrackIconH, 0); + appearanceIcon.setImageDrawable(appearanceDrawable); + buttonAppearance.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (showTrackOnMapButton.isChecked()) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + hide(); + SelectedGpxFile selectedGpxFile = app.getSavingTrackHelper().getCurrentTrack(); + TrackAppearanceFragment.showInstance(mapActivity, selectedGpxFile, TripRecordingActiveBottomSheet.this); + } + } + } + }); + createItem(buttonAppearance, ItemType.APPEARANCE, showTrackOnMapButton.isChecked(), null); + setShowOnMapBackground(buttonShow, app, showTrackOnMapButton.isChecked(), nightMode); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + buttonShow.setBackgroundTintList(null); + } + buttonShow.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean checked = !showTrackOnMapButton.isChecked(); + showTrackOnMapButton.setChecked(checked); + app.getSelectedGpxHelper().selectGpxFile(app.getSavingTrackHelper().getCurrentGpx(), checked, false); + createItem(buttonAppearance, ItemType.APPEARANCE, checked, null); + setShowOnMapBackground(buttonShow, app, checked, nightMode); + } + }); + + buttonClear.findViewById(R.id.button_container).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (fragmentManager != null && hasDataToSave()) { + ClearRecordedDataBottomSheetFragment.showInstance(fragmentManager, TripRecordingActiveBottomSheet.this); + } + } + }); + + buttonSegment.findViewById(R.id.button_container).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (wasTrackMonitored()) { + blockStatisticsBuilder.stopUpdatingStatBlocks(); + helper.startNewSegment(); + blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); + } + } + }); + + buttonSave.findViewById(R.id.button_container).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (hasDataToSave()) { + final GPXFile gpxFile = getGPXFile(); + new SaveCurrentTrackTask(app, gpxFile, createSaveListener(new Runnable() { + @Override + public void run() { + blockStatisticsBuilder.stopUpdatingStatBlocks(); + blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); + stopUpdatingTimeTrackSaved(); + runUpdatingTimeTrackSaved(); + } + })).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + } + }); + + buttonPause.findViewById(R.id.button_container).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean wasTrackMonitored = !wasTrackMonitored(); + if (!wasTrackMonitored) { + blockStatisticsBuilder.stopUpdatingStatBlocks(); + } else { + blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); + } + settings.SAVE_GLOBAL_TRACK_TO_GPX.set(wasTrackMonitored); + updateStatus(); + createItem(buttonPause, wasTrackMonitored ? ItemType.PAUSE : ItemType.RESUME, true, null); + } + }); + + buttonStop.findViewById(R.id.button_container).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (fragmentManager != null) { + StopTrackRecordingBottomFragment.showInstance(getMapActivity(), fragmentManager, TripRecordingActiveBottomSheet.this); + } + } + }); + } + + @Override + public void onSaveInstanceState(@NonNull Bundle outState) { + super.onSaveInstanceState(outState); + outState.putBoolean(UPDATE_CURRENT_GPX_FILE, true); + } + + private void updateStatus() { + TextView statusTitle = statusContainer.findViewById(R.id.text_status); + AppCompatImageView statusIcon = statusContainer.findViewById(R.id.icon_status); + ItemType status = searchingGPS() ? ItemType.SEARCHING_GPS : !wasTrackMonitored() ? ItemType.ON_PAUSE : ItemType.RECORDING; + Integer titleId = status.getTitleId(); + if (titleId != null) { + statusTitle.setText(titleId); + } + int colorText = status.equals(ItemType.SEARCHING_GPS) ? getSecondaryTextColorId(nightMode) : getOsmandIconColorId(nightMode); + statusTitle.setTextColor(ContextCompat.getColor(app, colorText)); + Integer iconId = status.getIconId(); + if (iconId != null) { + int colorDrawable = ContextCompat.getColor(app, + status.equals(ItemType.SEARCHING_GPS) ? getSecondaryIconColorId(nightMode) : getOsmandIconColorId(nightMode)); + Drawable statusDrawable = UiUtilities.tintDrawable(AppCompatResources.getDrawable(app, iconId), colorDrawable); + statusIcon.setImageDrawable(statusDrawable); + } + } + + private void createItem(View view, ItemType type, boolean enabled, @Nullable String description) { + view.setTag(type); + LinearLayout button = view.findViewById(R.id.button_container); + + AppCompatImageView icon = view.findViewById(R.id.icon); + if (icon != null) { + setTintedIcon(icon, enabled, nightMode, type); + } + + TextView title = view.findViewById(R.id.button_text); + Integer titleId = type.getTitleId(); + if (title != null && titleId != null) { + title.setText(titleId); + setTextColor(title, enabled, nightMode, type); + } + + TextViewEx desc = view.findViewById(R.id.desc); + if (desc != null) { + boolean isShowDesc = !Algorithms.isBlank(description); + int marginDesc = isShowDesc ? 0 : app.getResources().getDimensionPixelSize(R.dimen.context_menu_padding_margin_medium); + AndroidUiHelper.updateVisibility(desc, isShowDesc); + if (title != null) { + UiUtilities.setMargins(title, 0, marginDesc, 0, marginDesc); + } + desc.setText(description); + setTextColor(desc, false, nightMode, type); + } + + setItemBackground(button != null ? button : (LinearLayout) view, enabled); + } + + protected static View createButton(LayoutInflater inflater, ItemType type, boolean nightMode) { + View button = inflater.inflate(R.layout.bottom_sheet_button_with_icon, null); + button.setTag(type); + Context context = button.getContext(); + LinearLayout container = button.findViewById(R.id.button_container); + container.setClickable(false); + container.setFocusable(false); + + FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); + int horizontal = context.getResources().getDimensionPixelSize(R.dimen.content_padding); + params.setMargins(horizontal, 0, horizontal, 0); + button.setLayoutParams(params); + + if (type.getTitleId() != null) { + UiUtilities.setupDialogButton(nightMode, button, type.getEffect(), type.getTitleId()); + } + + TextViewEx title = button.findViewById(R.id.button_text); + int margin = context.getResources().getDimensionPixelSize(R.dimen.context_menu_padding_margin_medium); + UiUtilities.setMargins(title, 0, margin, 0, margin); + + int colorRes; + if (type.getEffect() == UiUtilities.DialogButtonType.SECONDARY_HARMFUL) { + colorRes = R.color.color_osm_edit_delete; + } else { + colorRes = nightMode ? R.color.dlg_btn_secondary_text_dark : R.color.dlg_btn_secondary_text_light; + } + AppCompatImageView icon = button.findViewById(R.id.icon); + if (type.getIconId() != null) { + Drawable drawable = AppCompatResources.getDrawable(context, type.getIconId()); + UiUtilities.tintDrawable(drawable, ContextCompat.getColor(context, colorRes)); + icon.setImageDrawable(drawable); + } + + return button; + } + + private String getTimeTrackSaved() { + long timeTrackSaved = helper.getLastTimeFileSaved(); + if (timeTrackSaved != 0) { + long now = System.currentTimeMillis(); + CharSequence time = DateUtils.getRelativeTimeSpanString(timeTrackSaved, now, DateUtils.MINUTE_IN_MILLIS); + return String.valueOf(time); + } else { + return null; + } + } + + @Override + public void onResume() { + super.onResume(); + blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded(); + runUpdatingGPS(); + runUpdatingTimeTrackSaved(); + } + + @Override + public void onPause() { + super.onPause(); + blockStatisticsBuilder.stopUpdatingStatBlocks(); + stopUpdatingGPS(); + stopUpdatingTimeTrackSaved(); + } + + public void stopUpdatingGPS() { + handler.removeCallbacks(updatingGPS); + } + + public void runUpdatingGPS() { + updatingGPS = new Runnable() { + @Override + public void run() { + int interval = app.getSettings().SAVE_GLOBAL_TRACK_INTERVAL.get(); + updateStatus(); + handler.postDelayed(this, Math.max(GENERAL_UPDATE_GPS_INTERVAL, interval)); + } + }; + handler.post(updatingGPS); + } + + public void stopUpdatingTimeTrackSaved() { + handler.removeCallbacks(updatingTimeTrackSaved); + } + + public void runUpdatingTimeTrackSaved() { + updatingTimeTrackSaved = new Runnable() { + @Override + public void run() { + String time = getTimeTrackSaved(); + createItem(buttonSave, ItemType.SAVE, hasDataToSave(), !Algorithms.isEmpty(time) ? time : null); + handler.postDelayed(this, GENERAL_UPDATE_SAVE_INTERVAL); + } + }; + handler.post(updatingTimeTrackSaved); + } + + private SaveGpxListener createSaveListener(@Nullable final Runnable callback) { + return new SaveGpxListener() { + + @Override + public void gpxSavingStarted() { + } + + @Override + public void gpxSavingFinished(Exception errorMessage) { + String gpxFileName = Algorithms.getFileWithoutDirs(getGPXFile().path); + final MapActivity mapActivity = getMapActivity(); + final Context context = getContext(); + final SaveGpxResult result = helper.saveDataToGpx(app.getAppCustomization().getTracksDir()); + if (mapActivity != null && context != null) { + final WeakReference mapActivityRef = new WeakReference<>(mapActivity); + final FragmentManager fragmentManager = mapActivityRef.get().getSupportFragmentManager(); + @SuppressLint({"StringFormatInvalid", "LocalSuppress"}) + Snackbar snackbar = Snackbar.make(getView(), + app.getResources().getString(R.string.shared_string_file_is_saved, gpxFileName), + Snackbar.LENGTH_LONG) + .setAction(R.string.shared_string_rename, new View.OnClickListener() { + @Override + public void onClick(View view) { + fragmentManager.beginTransaction().remove(TripRecordingActiveBottomSheet.this).commitAllowingStateLoss(); + SaveGPXBottomSheetFragment.showInstance(fragmentManager, result.getFilenames()); + } + }); + View view = snackbar.getView(); + CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams(); + params.gravity = Gravity.TOP; + AndroidUtils.setMargins(params, 0, AndroidUtils.getStatusBarHeight(context), 0, 0); + view.setLayoutParams(params); + UiUtilities.setupSnackbar(snackbar, nightMode); + snackbar.show(); + if (callback != null) { + callback.run(); + } + } + } + }; + } + + @Nullable + public MapActivity getMapActivity() { + Activity activity = getActivity(); + if (activity instanceof MapActivity) { + return (MapActivity) activity; + } + return null; + } + + public void show() { + Dialog dialog = getDialog(); + if (dialog != null) { + dialog.show(); + } + } + + public void hide() { + Dialog dialog = getDialog(); + if (dialog != null) { + dialog.hide(); + } + } + + public enum ItemType { + SHOW_TRACK(R.string.shared_string_show_on_map, null, null), + APPEARANCE(null, null, null), + SEARCHING_GPS(R.string.searching_gps, R.drawable.ic_action_gps_info, null), + RECORDING(R.string.recording_default_name, R.drawable.ic_action_track_recordable, null), + ON_PAUSE(R.string.on_pause, R.drawable.ic_pause, null), + CLEAR_DATA(R.string.clear_recorded_data, R.drawable.ic_action_delete_dark, UiUtilities.DialogButtonType.SECONDARY_HARMFUL), + START_SEGMENT(R.string.gpx_start_new_segment, R.drawable.ic_action_new_segment, null), + SAVE(R.string.shared_string_save, R.drawable.ic_action_save_to_file, null), + PAUSE(R.string.shared_string_pause, R.drawable.ic_pause, null), + RESUME(R.string.shared_string_resume, R.drawable.ic_play_dark, null), + STOP(R.string.shared_string_control_stop, R.drawable.ic_action_rec_stop, null), + STOP_AND_DISCARD(R.string.track_recording_stop_without_saving, R.drawable.ic_action_rec_stop, DialogButtonType.SECONDARY_HARMFUL), + SAVE_AND_STOP(R.string.track_recording_save_and_stop, R.drawable.ic_action_save_to_file, DialogButtonType.SECONDARY), + CANCEL(R.string.shared_string_cancel, R.drawable.ic_action_close, DialogButtonType.SECONDARY); + + @StringRes + private final Integer titleId; + @DrawableRes + private final Integer iconId; + private final DialogButtonType effect; + + ItemType(@Nullable @StringRes Integer titleId, @Nullable @DrawableRes Integer iconId, @Nullable DialogButtonType effect) { + this.titleId = titleId; + this.iconId = iconId; + this.effect = effect; + } + + @Nullable + public Integer getTitleId() { + return titleId; + } + + @Nullable + public Integer getIconId() { + return iconId; + } + + @Nullable + public DialogButtonType getEffect() { + return effect; + } + } + + private void setItemBackground(LinearLayout view, boolean enabled) { + Drawable background = AppCompatResources.getDrawable(app, R.drawable.btn_background_inactive_light); + if (background != null && enabled) { + ColorStateList iconColorStateList = AndroidUtils.createPressedColorStateList( + app, getInactiveButtonColorId(nightMode), getActiveButtonColorId(nightMode) + ); + DrawableCompat.setTintList(background, iconColorStateList); + } else { + UiUtilities.tintDrawable(background, ContextCompat.getColor(app, getInactiveButtonColorId(nightMode))); + } + view.setBackgroundDrawable(background); + } + + private static void setShowOnMapBackground(LinearLayout view, Context context, boolean checked, boolean nightMode) { + Drawable background = AppCompatResources.getDrawable(context, + nightMode ? checked ? R.drawable.btn_background_inactive_dark : R.drawable.btn_background_stroked_inactive_dark + : checked ? R.drawable.btn_background_inactive_light : R.drawable.btn_background_stroked_inactive_light); + view.setBackgroundDrawable(background); + } + + public void setTextColor(TextView tv, boolean enabled, boolean nightMode, ItemType type) { + if (tv != null) { + int activeColorId = type == ItemType.CLEAR_DATA ? R.color.color_osm_edit_delete : getActiveTextColorId(nightMode); + int normalColorId = enabled ? activeColorId : getSecondaryTextColorId(nightMode); + ColorStateList textColorStateList = AndroidUtils.createPressedColorStateList(app, normalColorId, getPressedColorId(nightMode)); + tv.setTextColor(textColorStateList); + } + } + + public void setTintedIcon(AppCompatImageView iv, boolean enabled, boolean nightMode, ItemType type) { + Integer iconId = type.getIconId(); + if (iv != null && iconId != null) { + Drawable icon = AppCompatResources.getDrawable(app, iconId); + int activeColorId = type == ItemType.CLEAR_DATA ? R.color.color_osm_edit_delete : getActiveIconColorId(nightMode); + int normalColorId = enabled ? activeColorId : getSecondaryIconColorId(nightMode); + ColorStateList iconColorStateList = AndroidUtils.createPressedColorStateList(app, normalColorId, getPressedColorId(nightMode)); + if (icon != null) { + DrawableCompat.setTintList(icon, iconColorStateList); + } + iv.setImageDrawable(icon); + if (type == ItemType.STOP) { + int stopSize = iv.getResources().getDimensionPixelSize(R.dimen.bottom_sheet_icon_margin_large); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(stopSize, stopSize); + iv.setLayoutParams(params); + } + } + } + + @ColorRes + private static int getActiveTextColorId(boolean nightMode) { + return nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light; + } + + @ColorRes + private static int getSecondaryTextColorId(boolean nightMode) { + return nightMode ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light; + } + + @ColorRes + private static int getActiveIconColorId(boolean nightMode) { + return nightMode ? R.color.icon_color_active_dark : R.color.icon_color_active_light; + } + + @ColorRes + private static int getSecondaryIconColorId(boolean nightMode) { + return nightMode ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light; + } + + @ColorRes + private static int getActiveButtonColorId(boolean nightMode) { + return nightMode ? R.color.active_buttons_and_links_bg_pressed_dark : R.color.active_buttons_and_links_bg_pressed_light; + } + + @ColorRes + private static int getInactiveButtonColorId(boolean nightMode) { + return nightMode ? R.color.inactive_buttons_and_links_bg_dark : R.color.inactive_buttons_and_links_bg_light; + } + + @ColorRes + private static int getOsmandIconColorId(boolean nightMode) { + return nightMode ? R.color.icon_color_osmand_dark : R.color.icon_color_osmand_light; + } + + @ColorRes + private static int getPressedColorId(boolean nightMode) { + return nightMode ? R.color.active_buttons_and_links_text_dark : R.color.active_buttons_and_links_text_light; + } + + @Override + protected int getDismissButtonHeight() { + return getResources().getDimensionPixelSize(R.dimen.bottom_sheet_cancel_button_height); + } + + @Override + protected int getDismissButtonTextId() { + return R.string.shared_string_close; + } + + @Override + protected boolean useVerticalButtons() { + return true; + } +} diff --git a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java index 178e662176..640d01e4c9 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/TripRecordingBottomSheet.java @@ -32,6 +32,7 @@ import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities.DialogButtonType; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; import net.osmand.plus.helpers.AndroidUiHelper; @@ -246,12 +247,12 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment { } @Override - protected int getRightButtonHeight(){ + protected int getRightButtonHeight() { return getResources().getDimensionPixelSize(R.dimen.bottom_sheet_cancel_button_height); } @Override - protected int getDismissButtonHeight(){ + protected int getDismissButtonHeight() { return getResources().getDimensionPixelSize(R.dimen.bottom_sheet_cancel_button_height); } @@ -277,9 +278,14 @@ public class TripRecordingBottomSheet extends MenuBottomSheetDialogFragment { @Override protected void onRightBottomButtonClick() { - app.getSavingTrackHelper().startNewSegment(); + SavingTrackHelper helper = app.getSavingTrackHelper(); + helper.startNewSegment(); settings.SAVE_GLOBAL_TRACK_TO_GPX.set(true); app.startNavigationService(NavigationService.USED_BY_GPX); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + TripRecordingActiveBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), helper.getCurrentTrack()); + } dismiss(); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java b/OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java index 84959d17f7..2d943b307a 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java @@ -51,7 +51,10 @@ public class SaveCurrentTrackTask extends AsyncTask { } for (final String f : files.keySet()) { File fout = new File(dir, f + IndexConstants.GPX_FILE_EXT); - GPXUtilities.writeGpxFile(fout, gpx); + Exception exception = GPXUtilities.writeGpxFile(fout, gpx); + if (exception == null) { + app.getSavingTrackHelper().setLastTimeFileSaved(fout.lastModified()); + } } return shouldClearPath; } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java index 7b4067c2f7..5104e98c03 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/BugBottomSheetDialog.java @@ -64,6 +64,9 @@ public class BugBottomSheetDialog extends MenuBottomSheetDialogFragment { textBox.setDefaultHintTextColor(colorStateList); noteText = osmNoteView.findViewById(R.id.name_edit_text); noteText.setText(text); + if (noteText.requestFocus()) { + AndroidUtils.showSoftKeyboard(getActivity(), noteText); + } BaseBottomSheetItem editOsmNote = new BaseBottomSheetItem.Builder() .setCustomView(osmNoteView) diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java index e75dad04de..82a8ea2d88 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java @@ -62,10 +62,11 @@ import net.osmand.plus.routing.RouteProvider; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.settings.backend.ApplicationMode; -import net.osmand.plus.views.layers.MapControlsLayer; +import net.osmand.plus.track.TrackSelectSegmentBottomSheet; +import net.osmand.plus.track.TrackSelectSegmentBottomSheet.OnSegmentSelectedListener; +import net.osmand.plus.views.layers.MapControlsLayer.MapControlsThemeInfoProvider; import net.osmand.plus.widgets.popup.PopUpMenuHelper; import net.osmand.plus.widgets.popup.PopUpMenuItem; -import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -75,7 +76,7 @@ import java.util.List; public class FollowTrackFragment extends ContextMenuScrollFragment implements CardListener, - IRouteInformationListener, MapControlsLayer.MapControlsThemeInfoProvider { + IRouteInformationListener, MapControlsThemeInfoProvider, OnSegmentSelectedListener { public static final String TAG = FollowTrackFragment.class.getName(); @@ -210,20 +211,8 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca if (gpxFile == null || selectingTrack) { setupTracksCard(); } else { - String fileName = null; - File file = null; - if (!Algorithms.isEmpty(gpxFile.path)) { - file = new File(gpxFile.path); - fileName = file.getName(); - } else if (!Algorithms.isEmpty(gpxFile.tracks)) { - fileName = gpxFile.tracks.get(0).name; - } - if (Algorithms.isEmpty(fileName)) { - fileName = app.getString(R.string.shared_string_gpx_track); - } sortButton.setVisibility(View.GONE); - GPXInfo gpxInfo = new GPXInfo(fileName, file != null ? file.lastModified() : 0, file != null ? file.length() : 0); - TrackEditCard importTrackCard = new TrackEditCard(mapActivity, gpxInfo); + TrackEditCard importTrackCard = new TrackEditCard(mapActivity, gpxFile); importTrackCard.setListener(this); cardsContainer.addView(importTrackCard.build(mapActivity)); @@ -490,14 +479,26 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca String fileName = gpxInfo.getFileName(); SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByName(fileName); if (selectedGpxFile != null) { - selectTrackToFollow(selectedGpxFile.getGpxFile()); - updateSelectionMode(false); + GPXFile gpxFile = selectedGpxFile.getGpxFile(); + if (gpxFile.getNonEmptySegmentsCount() > 1) { + TrackSelectSegmentBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), gpxFile, this); + } else { + selectTrackToFollow(gpxFile); + updateSelectionMode(false); + } } else { CallbackWithObject callback = new CallbackWithObject() { @Override public boolean processResult(GPXFile[] result) { - selectTrackToFollow(result[0]); - updateSelectionMode(false); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + if (result[0].getNonEmptySegmentsCount() > 1) { + TrackSelectSegmentBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), result[0], FollowTrackFragment.this); + } else { + selectTrackToFollow(result[0]); + updateSelectionMode(false); + } + } return true; } }; @@ -716,4 +717,16 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca protected String getThemeInfoProviderTag() { return TAG; } + + @Override + public void onSegmentSelect(GPXFile gpxFile, int selectedSegment) { + selectTrackToFollow(gpxFile); + GPXRouteParamsBuilder paramsBuilder = app.getRoutingHelper().getCurrentGPXRoute(); + if (paramsBuilder != null) { + paramsBuilder.setSelectedSegment(selectedSegment); + app.getSettings().GPX_ROUTE_SEGMENT.set(selectedSegment); + app.getRoutingHelper().onSettingsChanged(true); + } + updateSelectionMode(false); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java index 83a866c49e..f88f499125 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java @@ -1675,6 +1675,13 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener if (Algorithms.isEmpty(fileName)) { fileName = app.getString(R.string.shared_string_gpx_track); } + GPXRouteParamsBuilder routeParams = app.getRoutingHelper().getCurrentGPXRoute(); + + if (gpxFile.getNonEmptySegmentsCount() > 1 && routeParams != null && routeParams.getSelectedSegment() != -1) { + int selectedSegmentCount = routeParams.getSelectedSegment() + 1; + int totalSegmentCount = routeParams.getFile().getNonEmptyTrkSegments(false).size(); + fileName = app.getResources().getString(R.string.of, selectedSegmentCount, totalSegmentCount) + ", " + fileName; + } title.setText(GpxUiHelper.getGpxTitle(fileName)); description.setText(R.string.follow_track); buttonDescription.setText(R.string.shared_string_add); diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/TrackEditCard.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/TrackEditCard.java index 556ebe4caa..6a88e39e06 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/TrackEditCard.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/TrackEditCard.java @@ -4,26 +4,33 @@ import android.graphics.drawable.ColorDrawable; import android.view.View; import android.widget.ImageButton; import android.widget.LinearLayout; +import android.widget.TextView; import net.osmand.AndroidUtils; +import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.GPXFile; import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GpxDbHelper.GpxDataItemCallback; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXInfo; +import net.osmand.plus.helpers.TrackSelectSegmentAdapter; +import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.util.Algorithms; import java.io.File; +import java.util.List; public class TrackEditCard extends BaseCard { - private GPXInfo gpxInfo; + private final GPXFile gpxFile; - public TrackEditCard(MapActivity mapActivity, GPXInfo gpxInfo) { + public TrackEditCard(MapActivity mapActivity, GPXFile gpxFile) { super(mapActivity); - this.gpxInfo = gpxInfo; + this.gpxFile = gpxFile; } @Override @@ -50,11 +57,47 @@ public class TrackEditCard extends BaseCard { @Override protected void updateContent() { - String fileName = Algorithms.getFileWithoutDirs(gpxInfo.getFileName()); - String title = GpxUiHelper.getGpxTitle(fileName); + String fileName = null; + File file = null; + if (!Algorithms.isEmpty(gpxFile.path)) { + file = new File(gpxFile.path); + fileName = gpxFile.path; + } else if (!Algorithms.isEmpty(gpxFile.tracks)) { + fileName = gpxFile.tracks.get(0).name; + } + if (Algorithms.isEmpty(fileName)) { + fileName = app.getString(R.string.shared_string_gpx_track); + } + + GPXInfo gpxInfo = new GPXInfo(gpxFile.path, file != null ? file.lastModified() : 0, file != null ? file.length() : 0); GpxDataItem dataItem = getDataItem(gpxInfo); + String title = GpxUiHelper.getGpxTitle(Algorithms.getFileWithoutDirs(fileName)); + GPXRouteParamsBuilder routeParams = app.getRoutingHelper().getCurrentGPXRoute(); + if (gpxFile.getNonEmptySegmentsCount() > 1 && routeParams != null && routeParams.getSelectedSegment() != -1) { + int selectedSegmentCount = routeParams.getSelectedSegment() + 1; + int totalSegmentCount = routeParams.getFile().getNonEmptyTrkSegments(false).size(); + title = app.getResources().getString(R.string.of, selectedSegmentCount, totalSegmentCount) + ", " + title; + } GpxUiHelper.updateGpxInfoView(view, title, gpxInfo, dataItem, false, app); + if (gpxFile.getNonEmptySegmentsCount() > 1 && routeParams != null && routeParams.getSelectedSegment() != -1) { + TextView distanceView = view.findViewById(R.id.distance); + TextView timeView = view.findViewById(R.id.time); + TextView pointsView = view.findViewById(R.id.points_count); + List segments = gpxFile.getNonEmptyTrkSegments(false); + GPXUtilities.TrkSegment segment = segments.get(routeParams.getSelectedSegment()); + int point = segment.points.size(); + double distance = TrackSelectSegmentAdapter.getDistance(segment); + long time = TrackSelectSegmentAdapter.getSegmentTime(segment); + if (time != 1) { + timeView.setText(OsmAndFormatter.getFormattedDurationShort((int) (time / 1000))); + } else { + timeView.setText(""); + } + distanceView.setText(OsmAndFormatter.getFormattedDistance((float) distance, app)); + pointsView.setText(String.valueOf(point)); + } + ImageButton editButton = view.findViewById(R.id.show_on_map); editButton.setVisibility(View.VISIBLE); editButton.setImageDrawable(getContentIcon(R.drawable.ic_action_edit_dark)); diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java index b3b2a11ab6..051a5218fa 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java @@ -8,7 +8,6 @@ import android.util.Base64; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.Route; -import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.WptPt; import net.osmand.Location; @@ -20,15 +19,15 @@ import net.osmand.data.LatLon; import net.osmand.data.LocationPoint; import net.osmand.data.WptLocationPoint; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.onlinerouting.OnlineRoutingHelper; -import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine.OnlineRoutingResponse; -import net.osmand.plus.settings.backend.OsmandSettings; -import net.osmand.plus.settings.backend.CommonPreference; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.TargetPointsHelper.TargetPoint; +import net.osmand.plus.onlinerouting.OnlineRoutingHelper; +import net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine.OnlineRoutingResponse; import net.osmand.plus.render.NativeOsmandLibrary; import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.plus.settings.backend.CommonPreference; +import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.router.GeneralRouter; import net.osmand.router.GeneralRouter.RoutingParameter; import net.osmand.router.GeneralRouter.RoutingParameterType; @@ -161,6 +160,7 @@ public class RouteProvider { private boolean leftSide; private boolean passWholeRoute; private boolean calculateOsmAndRouteParts; + private int selectedSegment = -1; public GPXRouteParamsBuilder(GPXFile file, OsmandSettings settings) { leftSide = settings.DRIVING_REGION.get().leftHandDriving; @@ -191,6 +191,14 @@ public class RouteProvider { this.calculateOsmAndRoute = calculateOsmAndRoute; } + public int getSelectedSegment() { + return selectedSegment; + } + + public void setSelectedSegment(int selectedSegment) { + this.selectedSegment = selectedSegment; + } + public void setPassWholeRoute(boolean passWholeRoute) { this.passWholeRoute = passWholeRoute; } @@ -278,8 +286,9 @@ public class RouteProvider { wpt.add(new WptLocationPoint(w)); } } + int selectedSegment = builder.getSelectedSegment(); if (OSMAND_ROUTER_V2.equals(file.author)) { - route = parseOsmAndGPXRoute(points, file); + route = parseOsmAndGPXRoute(points, file, selectedSegment); routePoints = file.getRoutePoints(); if (reverse) { Collections.reverse(points); @@ -287,7 +296,7 @@ public class RouteProvider { } addMissingTurns = route != null && route.isEmpty(); } else if (file.isCloudmadeRouteFile() || OSMAND_ROUTER.equals(file.author)) { - directions = parseOsmAndGPXRoute(points, file, OSMAND_ROUTER.equals(file.author), builder.leftSide, 10); + directions = parseOsmAndGPXRoute(points, file, OSMAND_ROUTER.equals(file.author), builder.leftSide, 10, selectedSegment); if (OSMAND_ROUTER.equals(file.author) && file.hasRtePt()) { // For files generated by OSMAND_ROUTER use directions contained unaltered addMissingTurns = false; @@ -301,12 +310,16 @@ public class RouteProvider { } else { // first of all check tracks if (!useIntermediatePointsRTE) { - for (Track tr : file.tracks) { - if (!tr.generalTrack) { - for (TrkSegment tkSeg : tr.segments) { - for (WptPt pt : tkSeg.points) { - points.add(createLocation(pt)); - } + List segments = file.getNonEmptyTrkSegments(false); + if (selectedSegment != -1 && segments.size() > selectedSegment) { + TrkSegment segment = segments.get(selectedSegment); + for (WptPt p : segment.points) { + points.add(createLocation(p)); + } + } else { + for (TrkSegment tkSeg : segments) { + for (WptPt p : tkSeg.points) { + points.add(createLocation(p)); } } } @@ -998,35 +1011,49 @@ public class RouteProvider { return new RouteCalculationResult("Empty result"); } - private static List parseOsmAndGPXRoute(List points, GPXFile gpxFile) { - for (Track tr : gpxFile.tracks) { - for (TrkSegment ts : tr.segments) { + private static List parseOsmAndGPXRoute(List points, GPXFile gpxFile, int selectedSegment) { + List segments = gpxFile.getNonEmptyTrkSegments(false); + if (selectedSegment != -1 && segments.size() > selectedSegment) { + TrkSegment segment = segments.get(selectedSegment); + for (WptPt p : segment.points) { + points.add(createLocation(p)); + } + RouteImporter routeImporter = new RouteImporter(segment); + return routeImporter.importRoute(); + } else { + for (TrkSegment ts : segments) { for (WptPt p : ts.points) { points.add(createLocation(p)); } } + RouteImporter routeImporter = new RouteImporter(gpxFile); + return routeImporter.importRoute(); } - RouteImporter routeImporter = new RouteImporter(gpxFile); - return routeImporter.importRoute(); } private static List parseOsmAndGPXRoute(List points, GPXFile gpxFile, boolean osmandRouter, - boolean leftSide, float defSpeed) { + boolean leftSide, float defSpeed, int selectedSegment) { List directions = null; if (!osmandRouter) { for (WptPt pt : gpxFile.getPoints()) { points.add(createLocation(pt)); } } else { - for (Track tr : gpxFile.tracks) { - for (TrkSegment ts : tr.segments) { + List segments = gpxFile.getNonEmptyTrkSegments(false); + if (selectedSegment != -1 && segments.size() > selectedSegment) { + TrkSegment segment = segments.get(selectedSegment); + for (WptPt p : segment.points) { + points.add(createLocation(p)); + } + } else { + for (TrkSegment ts : segments) { for (WptPt p : ts.points) { points.add(createLocation(p)); } } } } - float[] distanceToEnd = new float[points.size()]; + float[] distanceToEnd = new float[points.size()]; for (int i = points.size() - 2; i >= 0; i--) { distanceToEnd[i] = distanceToEnd[i + 1] + points.get(i).distanceTo(points.get(i + 1)); } @@ -1307,7 +1334,7 @@ public class RouteProvider { GPXFile gpxFile = GPXUtilities.loadGPXFile(gpxStream); - dir = parseOsmAndGPXRoute(res, gpxFile, true, params.leftSide, params.mode.getDefaultSpeed()); + dir = parseOsmAndGPXRoute(res, gpxFile, true, params.leftSide, params.mode.getDefaultSpeed(), -1); if (dir != null) { addMissingTurns = false; diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index 45cefd2723..9652fc3bad 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -45,10 +45,8 @@ import net.osmand.plus.helpers.enums.DrivingRegion; import net.osmand.plus.helpers.enums.MetricsConstants; import net.osmand.plus.helpers.enums.SpeedConstants; import net.osmand.plus.helpers.enums.TracksSortByMode; -import net.osmand.plus.mapillary.MapillaryPlugin; import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format; import net.osmand.plus.mapmarkers.MapMarkersMode; -import net.osmand.plus.openplacereviews.OpenPlaceReviewsPlugin; import net.osmand.plus.profiles.LocationIcon; import net.osmand.plus.profiles.NavigationIcon; import net.osmand.plus.profiles.ProfileIconColors; @@ -1389,6 +1387,7 @@ public class OsmandSettings { public final OsmandPreference GPX_ROUTE_CALC_OSMAND_PARTS = new BooleanPreference(this, "gpx_routing_calculate_osmand_route", true).makeGlobal().makeShared().cache(); public final OsmandPreference GPX_CALCULATE_RTEPT = new BooleanPreference(this, "gpx_routing_calculate_rtept", true).makeGlobal().makeShared().cache(); public final OsmandPreference GPX_ROUTE_CALC = new BooleanPreference(this, "calc_gpx_route", false).makeGlobal().makeShared().cache(); + public final OsmandPreference GPX_ROUTE_SEGMENT = new IntPreference(this, "gpx_route_segment", -1).makeGlobal().makeShared().cache(); public final OsmandPreference SHOW_START_FINISH_ICONS = new BooleanPreference(this, "show_start_finish_icons", true).makeGlobal().makeShared().cache(); public final OsmandPreference AVOID_TOLL_ROADS = new BooleanPreference(this, "avoid_toll_roads", false).makeProfile().cache(); @@ -2594,7 +2593,7 @@ public class OsmandSettings { public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE"; - public static final String[] TTS_AVAILABLE_VOICES = new String[]{ + public static final String[] TTS_AVAILABLE_VOICES = new String[] { "de", "en", "es", "fr", "it", "ja", "nl", "pl", "pt", "ru", "zh" }; // this value string is synchronized with settings_pref.xml preference name diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java index abcd5afc0a..29f90c2cfe 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java @@ -102,6 +102,8 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { } }); if (needRestart) { + description.append("\n\n"); + description.append(app.getString(R.string.app_restart_required)); setupRestartButton(root); } if (Build.VERSION.SDK_INT >= 21) { diff --git a/OsmAnd/src/net/osmand/plus/track/DescriptionCard.java b/OsmAnd/src/net/osmand/plus/track/DescriptionCard.java index 56e8cfd2e5..5db16f7f45 100644 --- a/OsmAnd/src/net/osmand/plus/track/DescriptionCard.java +++ b/OsmAnd/src/net/osmand/plus/track/DescriptionCard.java @@ -1,16 +1,16 @@ package net.osmand.plus.track; +import android.content.Context; +import android.os.Build; import android.view.View; import android.widget.FrameLayout; import android.widget.LinearLayout; -import androidx.annotation.NonNull; -import androidx.appcompat.widget.AppCompatImageView; - import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; import com.squareup.picasso.RequestCreator; +import net.osmand.AndroidUtils; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.PicassoUtils; @@ -19,10 +19,13 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.widgets.TextViewEx; +import net.osmand.plus.wikipedia.WikiArticleHelper; import net.osmand.util.Algorithms; +import androidx.annotation.NonNull; +import androidx.appcompat.widget.AppCompatImageView; + import static net.osmand.plus.myplaces.TrackActivityFragmentAdapter.getMetadataImageLink; -import static net.osmand.plus.wikipedia.WikiArticleHelper.getFirstParagraph; public class DescriptionCard extends BaseCard { @@ -59,8 +62,9 @@ public class DescriptionCard extends BaseCard { private void showAddBtn() { LinearLayout descriptionContainer = view.findViewById(R.id.description_container); - FrameLayout addBtn = view.findViewById(R.id.btn_add); + View addBtn = view.findViewById(R.id.btn_add); + setupButton(addBtn); addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -81,7 +85,8 @@ public class DescriptionCard extends BaseCard { TextViewEx tvDescription = view.findViewById(R.id.description); tvDescription.setText(getFirstParagraph(descriptionHtml)); - TextViewEx readBtn = view.findViewById(R.id.btn_read_full); + View readBtn = view.findViewById(R.id.btn_read_full); + setupButton(readBtn); readBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -89,7 +94,8 @@ public class DescriptionCard extends BaseCard { } }); - TextViewEx editBtn = view.findViewById(R.id.btn_edit); + View editBtn = view.findViewById(R.id.btn_edit); + setupButton(editBtn); editBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -98,6 +104,25 @@ public class DescriptionCard extends BaseCard { }); } + private String getFirstParagraph(String descriptionHtml) { + if (descriptionHtml != null) { + String firstParagraph = WikiArticleHelper.getPartialContent(descriptionHtml); + if (!Algorithms.isEmpty(firstParagraph)) { + return firstParagraph; + } + } + return descriptionHtml; + } + + private void setupButton(View button) { + Context ctx = button.getContext(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + AndroidUtils.setBackground(ctx, button, nightMode, R.drawable.ripple_light, R.drawable.ripple_dark); + } else { + AndroidUtils.setBackground(ctx, button, nightMode, R.drawable.btn_unstroked_light, R.drawable.btn_unstroked_dark); + } + } + private void setupImage(final String imageUrl) { if (imageUrl == null) { return; diff --git a/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java b/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java index 0e005b0d8c..d75416052e 100644 --- a/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java +++ b/OsmAnd/src/net/osmand/plus/track/GpxBlockStatisticsBuilder.java @@ -11,6 +11,7 @@ import androidx.annotation.ColorInt; import androidx.annotation.ColorRes; import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatImageView; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -18,6 +19,7 @@ import androidx.recyclerview.widget.RecyclerView; import net.osmand.AndroidUtils; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXTrackAnalysis; +import net.osmand.PlatformUtil; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmAndFormatter; @@ -30,17 +32,24 @@ import net.osmand.plus.myplaces.SegmentActionsListener; import net.osmand.plus.widgets.TextViewEx; import net.osmand.util.Algorithms; +import org.apache.commons.logging.Log; + import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class GpxBlockStatisticsBuilder { + private static final Log log = PlatformUtil.getLog(GpxBlockStatisticsBuilder.class); + private static final int GENERAL_UPDATE_INTERVAL = 1000; + private final OsmandApplication app; private RecyclerView blocksView; private final SelectedGpxFile selectedGpxFile; private BlockStatisticsAdapter adapter; private final List items = new ArrayList<>(); + private boolean blocksClickable = true; private final Handler handler = new Handler(); private Runnable updatingItems; @@ -51,28 +60,34 @@ public class GpxBlockStatisticsBuilder { this.selectedGpxFile = selectedGpxFile; } + public boolean isUpdateRunning() { + return updateRunning; + } + + public void setBlocksClickable(boolean blocksClickable) { + this.blocksClickable = blocksClickable; + } + public void setBlocksView(RecyclerView blocksView) { this.blocksView = blocksView; } - private GpxDisplayItem getDisplayItem(GPXFile gpxFile) { - return GpxUiHelper.makeGpxDisplayItem(app, gpxFile); + @Nullable + public GpxDisplayItem getDisplayItem(GPXFile gpxFile) { + return gpxFile.tracks.size() > 0 ? GpxUiHelper.makeGpxDisplayItem(app, gpxFile) : null; } private GPXFile getGPXFile() { return selectedGpxFile.getGpxFile(); } - public void initStatBlocks(SegmentActionsListener actionsListener, @ColorInt int activeColor, boolean nightMode) { + public void initStatBlocks(@Nullable SegmentActionsListener actionsListener, @ColorInt int activeColor, boolean nightMode) { initItems(); - boolean isNotEmpty = !Algorithms.isEmpty(items); - AndroidUiHelper.updateVisibility(blocksView, isNotEmpty); - if (isNotEmpty) { - adapter = new BlockStatisticsAdapter(getDisplayItem(getGPXFile()), actionsListener, activeColor, nightMode); - adapter.setItems(items); - blocksView.setLayoutManager(new LinearLayoutManager(app, LinearLayoutManager.HORIZONTAL, false)); - blocksView.setAdapter(adapter); - } + adapter = new BlockStatisticsAdapter(getDisplayItem(getGPXFile()), actionsListener, activeColor, nightMode); + adapter.setItems(items); + blocksView.setLayoutManager(new LinearLayoutManager(app, LinearLayoutManager.HORIZONTAL, false)); + blocksView.setAdapter(adapter); + AndroidUiHelper.updateVisibility(blocksView, !Algorithms.isEmpty(items)); } public void stopUpdatingStatBlocks() { @@ -80,30 +95,29 @@ public class GpxBlockStatisticsBuilder { updateRunning = false; } - public void runUpdatingStatBlocks() { - updatingItems = new Runnable() { - @Override - public void run() { - if (adapter != null) { + public void runUpdatingStatBlocksIfNeeded() { + if (!isUpdateRunning()) { + updatingItems = new Runnable() { + @Override + public void run() { initItems(); - adapter.setItems(items); + if (adapter != null) { + adapter.setItems(items); + } AndroidUiHelper.updateVisibility(blocksView, !Algorithms.isEmpty(items)); + int interval = app.getSettings().SAVE_GLOBAL_TRACK_INTERVAL.get(); + updateRunning = handler.postDelayed(this, Math.max(GENERAL_UPDATE_INTERVAL, interval)); } - int interval = app.getSettings().SAVE_GLOBAL_TRACK_INTERVAL.get(); - handler.postDelayed(this, Math.max(1000, interval)); - } - }; - updateRunning = handler.post(updatingItems); + }; + updateRunning = handler.post(updatingItems); + } } public void initItems() { GPXFile gpxFile = getGPXFile(); - GpxDisplayItem gpxDisplayItem = null; + GpxDisplayItem gpxDisplayItem = getDisplayItem(gpxFile); GPXTrackAnalysis analysis = null; boolean withoutGaps = true; - if (gpxFile.tracks.size() > 0) { - gpxDisplayItem = getDisplayItem(gpxFile); - } if (gpxDisplayItem != null) { analysis = gpxDisplayItem.analysis; withoutGaps = !selectedGpxFile.isJoinSegments() && gpxDisplayItem.isGeneralTrack(); @@ -165,7 +179,7 @@ public class GpxBlockStatisticsBuilder { } } - public class StatBlock { + public static class StatBlock { private final String title; private final String value; private final int imageResId; @@ -201,6 +215,9 @@ public class GpxBlockStatisticsBuilder { @ColorInt private final int activeColor; private final boolean nightMode; + private final int minWidthPx; + private final int maxWidthPx; + private final int textSize; public BlockStatisticsAdapter(GpxDisplayItem displayItem, SegmentActionsListener actionsListener, @ColorInt int activeColor, boolean nightMode) { @@ -208,6 +225,9 @@ public class GpxBlockStatisticsBuilder { this.actionsListener = actionsListener; this.activeColor = activeColor; this.nightMode = nightMode; + minWidthPx = AndroidUtils.dpToPx(app, 60f); + maxWidthPx = AndroidUtils.dpToPx(app, 120f); + textSize = app.getResources().getDimensionPixelSize(R.dimen.default_desc_text_size); } @Override @@ -227,17 +247,15 @@ public class GpxBlockStatisticsBuilder { public void onBindViewHolder(BlockStatisticsViewHolder holder, int position) { final StatBlock item = items.get(position); holder.valueText.setText(item.value); - holder.titleText.setText(item.title); - if (updateRunning) { - holder.titleText.setWidth(app.getResources().getDimensionPixelSize(R.dimen.map_route_buttons_width)); - } holder.valueText.setTextColor(activeColor); + holder.titleText.setText(item.title); holder.titleText.setTextColor(app.getResources().getColor(R.color.text_color_secondary_light)); + holder.titleText.setWidth(calculateWidthWithin(item.title, item.value)); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GPXTrackAnalysis analysis = displayItem != null ? displayItem.analysis : null; - if (analysis != null) { + if (blocksClickable && analysis != null && actionsListener != null) { ArrayList list = new ArrayList<>(); if (analysis.hasElevationData || analysis.isSpeedSpecified() || analysis.hasSpeedData) { if (item.firstType != null) { @@ -264,9 +282,14 @@ public class GpxBlockStatisticsBuilder { this.items.addAll(items); notifyDataSetChanged(); } + + public int calculateWidthWithin(String... texts) { + int textWidth = AndroidUtils.getTextMaxWidth(textSize, Arrays.asList(texts)); + return Math.min(maxWidthPx, Math.max(minWidthPx, textWidth)); + } } - private class BlockStatisticsViewHolder extends RecyclerView.ViewHolder { + private static class BlockStatisticsViewHolder extends RecyclerView.ViewHolder { private final TextViewEx valueText; private final TextView titleText; diff --git a/OsmAnd/src/net/osmand/plus/track/GpxEditDescriptionDialogFragment.java b/OsmAnd/src/net/osmand/plus/track/GpxEditDescriptionDialogFragment.java index a79a392f8e..b8ef1c24d2 100644 --- a/OsmAnd/src/net/osmand/plus/track/GpxEditDescriptionDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/GpxEditDescriptionDialogFragment.java @@ -1,14 +1,19 @@ package net.osmand.plus.track; +import android.app.Activity; +import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.Window; +import net.osmand.AndroidUtils; import net.osmand.GPXUtilities.GPXFile; import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; @@ -26,6 +31,7 @@ import java.io.File; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; @@ -63,7 +69,50 @@ public class GpxEditDescriptionDialogFragment extends BaseOsmAndDialogFragment { } }); - view.findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() { + setupSaveButton(view); + + Bundle args = getArguments(); + if (args != null) { + htmlCode = args.getString(CONTENT_KEY); + if (htmlCode != null) { + editableHtml.append(htmlCode); + } + } + + return view; + } + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + Activity ctx = getActivity(); + int themeId = isNightMode(true) ? R.style.OsmandDarkTheme_DarkActionbar : R.style.OsmandLightTheme_DarkActionbar_LightStatusBar; + Dialog dialog = new Dialog(ctx, themeId); + Window window = dialog.getWindow(); + if (window != null) { + if (!getSettings().DO_NOT_USE_ANIMATIONS.get()) { + window.getAttributes().windowAnimations = R.style.Animations_Alpha; + } + if (Build.VERSION.SDK_INT >= 21) { + int statusBarColor = isNightMode(true) ? R.color.activity_background_color_dark : R.color.activity_background_color_light; + window.setStatusBarColor(ContextCompat.getColor(ctx, statusBarColor)); + } + } + return dialog; + } + + private boolean shouldClose() { + Editable editable = editableHtml.getText(); + if (htmlCode == null || editable == null || editable.toString() == null) { + return true; + } + return htmlCode.equals(editable.toString()); + } + + private void setupSaveButton(View view) { + View btnSave = view.findViewById(R.id.btn_save); + + btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Editable editable = editableHtml.getText(); @@ -73,23 +122,12 @@ public class GpxEditDescriptionDialogFragment extends BaseOsmAndDialogFragment { } }); - Bundle args = getArguments(); - if (args != null) { - htmlCode = args.getString(CONTENT_KEY); - if (htmlCode != null) { - editableHtml.setText(htmlCode); - } + Context ctx = btnSave.getContext(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + AndroidUtils.setBackground(ctx, btnSave, isNightMode(true), R.drawable.ripple_light, R.drawable.ripple_dark); + } else { + AndroidUtils.setBackground(ctx, btnSave, isNightMode(true), R.drawable.btn_unstroked_light, R.drawable.btn_unstroked_dark); } - - return view; - } - - private boolean shouldClose() { - Editable editable = editableHtml.getText(); - if (htmlCode == null || editable == null || editable.toString() == null) { - return true; - } - return htmlCode.equals(editable.toString()); } private void showDismissDialog() { diff --git a/OsmAnd/src/net/osmand/plus/track/GpxReadDescriptionDialogFragment.java b/OsmAnd/src/net/osmand/plus/track/GpxReadDescriptionDialogFragment.java index 1aad2dd87d..c9171111d8 100644 --- a/OsmAnd/src/net/osmand/plus/track/GpxReadDescriptionDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/GpxReadDescriptionDialogFragment.java @@ -1,28 +1,20 @@ package net.osmand.plus.track; +import android.app.Activity; +import android.app.Dialog; import android.content.Context; import android.graphics.Color; -import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.util.Base64; import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.Window; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.widget.AppCompatImageView; -import androidx.appcompat.widget.Toolbar; -import androidx.core.content.ContextCompat; -import androidx.fragment.app.FragmentActivity; -import androidx.fragment.app.FragmentManager; +import android.widget.TextView; import com.squareup.picasso.Callback; import com.squareup.picasso.Picasso; @@ -35,11 +27,17 @@ import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.base.BaseOsmAndDialogFragment; import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.widgets.TextViewEx; import net.osmand.plus.widgets.WebViewEx; import net.osmand.plus.wikivoyage.WikivoyageUtils; import net.osmand.util.Algorithms; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatImageView; +import androidx.core.content.ContextCompat; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; + public class GpxReadDescriptionDialogFragment extends BaseOsmAndDialogFragment { public static final String TAG = GpxReadDescriptionDialogFragment.class.getSimpleName(); @@ -47,7 +45,6 @@ public class GpxReadDescriptionDialogFragment extends BaseOsmAndDialogFragment { private static final String TITLE_KEY = "title_key"; private static final String IMAGE_URL_KEY = "image_url_key"; private static final String CONTENT_KEY = "content_key"; - private static final int EDIT_ID = 1; private WebViewEx webView; @@ -93,34 +90,6 @@ public class GpxReadDescriptionDialogFragment extends BaseOsmAndDialogFragment { loadWebviewData(); } - @Override - public boolean onOptionsItemSelected(@NonNull MenuItem item) { - if (item.getItemId() == EDIT_ID) { - FragmentActivity activity = getActivity(); - if (activity != null) { - GpxEditDescriptionDialogFragment.showInstance(activity, contentHtml, this); - } - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { - menu.clear(); - OsmandApplication app = getMyApplication(); - int color = AndroidUtils.resolveAttribute(app, R.attr.pstsTextColor); - MenuItem menuItem = menu.add(0, EDIT_ID, 0, app.getString(R.string.shared_string_edit)); - menuItem.setIcon(getIcon(R.drawable.ic_action_edit_dark, color)); - menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { - @Override - public boolean onMenuItemClick(MenuItem item) { - return onOptionsItemSelected(item); - } - }); - menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); - } - @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -129,30 +98,49 @@ public class GpxReadDescriptionDialogFragment extends BaseOsmAndDialogFragment { outState.putString(CONTENT_KEY, contentHtml); } - private void setupToolbar(View view) { - Toolbar toolbar = view.findViewById(R.id.toolbar); - getMyActivity().setSupportActionBar(toolbar); - setHasOptionsMenu(true); - toolbar.setClickable(true); - - Context ctx = getMyActivity(); - int iconColor = AndroidUtils.resolveAttribute(ctx, R.attr.pstsTextColor); - Drawable icBack = getMyApplication().getUIUtilities().getIcon(R.drawable.ic_arrow_back, iconColor); - toolbar.setNavigationIcon(icBack); - toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up); - - if (!Algorithms.isEmpty(title)) { - toolbar.setTitle(title); - int titleColor = AndroidUtils.resolveAttribute(ctx, R.attr.pstsTextColor); - toolbar.setTitleTextColor(ContextCompat.getColor(ctx, titleColor)); + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + Activity ctx = getActivity(); + int themeId = isNightMode(true) ? R.style.OsmandDarkTheme_DarkActionbar : R.style.OsmandLightTheme_DarkActionbar_LightStatusBar; + Dialog dialog = new Dialog(ctx, themeId); + Window window = dialog.getWindow(); + if (window != null) { + if (!getSettings().DO_NOT_USE_ANIMATIONS.get()) { + window.getAttributes().windowAnimations = R.style.Animations_Alpha; + } + if (Build.VERSION.SDK_INT >= 21) { + int statusBarColor = isNightMode(true) ? R.color.status_bar_color_dark : R.color.status_bar_color_light; + window.setStatusBarColor(ContextCompat.getColor(ctx, statusBarColor)); + } } + return dialog; + } - toolbar.setNavigationOnClickListener(new View.OnClickListener() { + private void setupToolbar(View view) { + View back = view.findViewById(R.id.toolbar_back); + back.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(final View v) { + public void onClick(View v) { dismiss(); } }); + + View edit = view.findViewById(R.id.toolbar_edit); + edit.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + FragmentActivity activity = getActivity(); + if (activity != null) { + GpxEditDescriptionDialogFragment.showInstance(activity, contentHtml, GpxReadDescriptionDialogFragment.this); + } + } + }); + + TextView toolbarTitle = view.findViewById(R.id.toolbar_title); + if (!Algorithms.isEmpty(title)) { + toolbarTitle.setText(title); + } } private void setupImage(View view) { @@ -216,15 +204,23 @@ public class GpxReadDescriptionDialogFragment extends BaseOsmAndDialogFragment { private void loadWebviewData() { String content = contentHtml; if (content != null) { - content = isNightMode(false) ? getColoredContent(content) : content; + content = isNightMode(true) ? getColoredContent(content) : content; String encoded = Base64.encodeToString(content.getBytes(), Base64.NO_PADDING); webView.loadData(encoded, "text/html", "base64"); } } private void setupDependentViews(final View view) { - TextViewEx readBtn = view.findViewById(R.id.btn_edit); - readBtn.setOnClickListener(new View.OnClickListener() { + View editBtn = view.findViewById(R.id.btn_edit); + + Context ctx = editBtn.getContext(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + AndroidUtils.setBackground(ctx, editBtn, isNightMode(true), R.drawable.ripple_light, R.drawable.ripple_dark); + } else { + AndroidUtils.setBackground(ctx, editBtn, isNightMode(true), R.drawable.btn_unstroked_light, R.drawable.btn_unstroked_dark); + } + + editBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentActivity activity = getActivity(); @@ -234,7 +230,7 @@ public class GpxReadDescriptionDialogFragment extends BaseOsmAndDialogFragment { } }); AndroidUiHelper.setVisibility(View.VISIBLE, - readBtn, view.findViewById(R.id.divider), view.findViewById(R.id.bottom_empty_space)); + editBtn, view.findViewById(R.id.divider), view.findViewById(R.id.bottom_empty_space)); int backgroundColor = isNightMode(false) ? R.color.activity_background_color_dark : R.color.activity_background_color_light; view.findViewById(R.id.root).setBackgroundResource(backgroundColor); diff --git a/OsmAnd/src/net/osmand/plus/track/OverviewCard.java b/OsmAnd/src/net/osmand/plus/track/OverviewCard.java index bb56b43f89..25fa38e04e 100644 --- a/OsmAnd/src/net/osmand/plus/track/OverviewCard.java +++ b/OsmAnd/src/net/osmand/plus/track/OverviewCard.java @@ -43,6 +43,10 @@ public class OverviewCard extends BaseCard { private final SelectedGpxFile selectedGpxFile; private final GpxBlockStatisticsBuilder blockStatisticsBuilder; + public GpxBlockStatisticsBuilder getBlockStatisticsBuilder() { + return blockStatisticsBuilder; + } + public OverviewCard(@NonNull MapActivity mapActivity, @NonNull SegmentActionsListener actionsListener, SelectedGpxFile selectedGpxFile) { super(mapActivity); this.actionsListener = actionsListener; diff --git a/OsmAnd/src/net/osmand/plus/track/TrackAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/track/TrackAppearanceFragment.java index 522103ac02..0ad5475337 100644 --- a/OsmAnd/src/net/osmand/plus/track/TrackAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/TrackAppearanceFragment.java @@ -44,6 +44,7 @@ import net.osmand.plus.dialogs.GpxAppearanceAdapter; import net.osmand.plus.dialogs.GpxAppearanceAdapter.AppearanceListItem; import net.osmand.plus.dialogs.GpxAppearanceAdapter.GpxAppearanceAdapterType; import net.osmand.plus.helpers.AndroidUiHelper; +import net.osmand.plus.monitoring.TripRecordingActiveBottomSheet; import net.osmand.plus.monitoring.TripRecordingBottomSheet; import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.routepreparationmenu.cards.BaseCard.CardListener; @@ -383,6 +384,8 @@ public class TrackAppearanceFragment extends ContextMenuScrollFragment implement Fragment target = getTargetFragment(); if (target instanceof TripRecordingBottomSheet) { ((TripRecordingBottomSheet) target).show(); + } else if (target instanceof TripRecordingActiveBottomSheet) { + ((TripRecordingActiveBottomSheet) target).show(); } } diff --git a/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java b/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java index ff2ec67140..e0a7290b33 100644 --- a/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/track/TrackMenuFragment.java @@ -81,6 +81,7 @@ import net.osmand.plus.myplaces.TrackActivityFragmentAdapter; import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.routepreparationmenu.cards.BaseCard.CardListener; +import net.osmand.plus.routing.RouteProvider; import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener; import net.osmand.plus.views.AddGpxPointBottomSheetHelper.NewGpxPoint; import net.osmand.plus.widgets.IconPopupMenu; @@ -114,7 +115,7 @@ import static net.osmand.plus.track.TrackPointsCard.OPEN_WAYPOINT_INDEX; public class TrackMenuFragment extends ContextMenuScrollFragment implements CardListener, SegmentActionsListener, RenameCallback, OnTrackFileMoveListener, OnPointsDeleteListener, - OsmAndLocationListener, OsmAndCompassListener { + OsmAndLocationListener, OsmAndCompassListener, TrackSelectSegmentBottomSheet.OnSegmentSelectedListener { public static final String OPEN_TRACK_MENU = "open_track_menu"; public static final String RETURN_SCREEN_NAME = "return_screen_name"; @@ -160,6 +161,7 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card private int toolbarHeightPx; private boolean mapPositionAdjusted; + public enum TrackMenuType { OVERVIEW(R.id.action_overview, R.string.shared_string_overview), TRACK(R.id.action_track, R.string.shared_string_gpx_tracks), @@ -175,6 +177,7 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card public final int titleId; } + @Override public int getMainLayoutId() { return R.layout.track_menu; @@ -231,7 +234,8 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } displayHelper.setGpx(selectedGpxFile.getGpxFile()); String fileName = Algorithms.getFileWithoutDirs(getGpx().path); - gpxTitle = GpxUiHelper.getGpxTitle(fileName); + gpxTitle = !isCurrentRecordingTrack() ? GpxUiHelper.getGpxTitle(fileName) + : app.getResources().getString(R.string.shared_string_currently_recording_track); toolbarHeightPx = getResources().getDimensionPixelSize(R.dimen.dashboard_map_toolbar); FragmentActivity activity = requireMyActivity(); @@ -330,8 +334,13 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card overviewCard.setListener(this); headerContainer.addView(overviewCard.build(getMapActivity())); } + GpxBlockStatisticsBuilder blocksBuilder = overviewCard.getBlockStatisticsBuilder(); + if (isCurrentRecordingTrack()) { + blocksBuilder.runUpdatingStatBlocksIfNeeded(); + } } else { if (overviewCard != null && overviewCard.getView() != null) { + overviewCard.getBlockStatisticsBuilder().stopUpdatingStatBlocks(); headerContainer.removeView(overviewCard.getView()); } boolean isOptions = menuType == TrackMenuType.OPTIONS; @@ -544,6 +553,10 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } updateControlsVisibility(true); startLocationUpdate(); + GpxBlockStatisticsBuilder blockStats = overviewCard.getBlockStatisticsBuilder(); + if (menuType == TrackMenuType.OVERVIEW && isCurrentRecordingTrack()) { + blockStats.runUpdatingStatBlocksIfNeeded(); + } } @Override @@ -555,6 +568,7 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } updateControlsVisibility(false); stopLocationUpdate(); + overviewCard.getBlockStatisticsBuilder().stopUpdatingStatBlocks(); } @Override @@ -591,8 +605,8 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card MapActivity mapActivity = getMapActivity(); View view = overviewCard.getView(); if (mapActivity != null && view != null) { - TextView distanceText = (TextView) view.findViewById(R.id.distance); - ImageView direction = (ImageView) view.findViewById(R.id.direction); + TextView distanceText = view.findViewById(R.id.distance); + ImageView direction = view.findViewById(R.id.direction); app.getUIUtilities().updateLocationView(updateLocationViewCache, direction, distanceText, latLon); } } @@ -723,23 +737,12 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card TrackAppearanceFragment.showInstance(mapActivity, selectedGpxFile, this); } else if (buttonIndex == DIRECTIONS_BUTTON_INDEX) { MapActivityActions mapActions = mapActivity.getMapActions(); - if (app.getRoutingHelper().isFollowingMode()) { - mapActions.stopNavigationActionConfirm(null, new Runnable() { - @Override - public void run() { - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { - mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(gpxFile, null, - null, null, true, true, MenuState.HEADER_ONLY); - } - } - }); + if (gpxFile.getNonEmptySegmentsCount() > 1) { + TrackSelectSegmentBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), gpxFile, this); } else { - mapActions.stopNavigationWithoutConfirm(); - mapActions.enterRoutePlanningModeGivenGpx(gpxFile, null, null, - null, true, true, MenuState.HEADER_ONLY); + startNavigationForGPX(gpxFile, mapActions); + dismiss(); } - dismiss(); } if (buttonIndex == JOIN_GAPS_BUTTON_INDEX) { displayHelper.setJoinSegments(!displayHelper.isJoinSegments()); @@ -830,6 +833,25 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } } + private void startNavigationForGPX(final GPXFile gpxFile, MapActivityActions mapActions) { + if (app.getRoutingHelper().isFollowingMode()) { + mapActions.stopNavigationActionConfirm(null, new Runnable() { + @Override + public void run() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(gpxFile, null, + null, null, true, true, MenuState.HEADER_ONLY); + } + } + }); + } else { + mapActions.stopNavigationWithoutConfirm(); + mapActions.enterRoutePlanningModeGivenGpx(gpxFile, null, null, + null, true, true, MenuState.HEADER_ONLY); + } + } + public void updateToolbar(int y, boolean animated) { final MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { @@ -1066,6 +1088,21 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card } } + @Override + public void onSegmentSelect(GPXFile gpxFile, int selectedSegment) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + startNavigationForGPX(gpxFile, mapActivity.getMapActions()); + app.getSettings().GPX_ROUTE_SEGMENT.set(selectedSegment); + RouteProvider.GPXRouteParamsBuilder paramsBuilder = app.getRoutingHelper().getCurrentGPXRoute(); + if (paramsBuilder != null) { + paramsBuilder.setSelectedSegment(selectedSegment); + app.getRoutingHelper().onSettingsChanged(true); + } + dismiss(); + } + } + private void editSegment(TrkSegment segment) { GPXFile gpxFile = getGpx(); openPlanRoute(new GpxData(gpxFile)); @@ -1120,6 +1157,10 @@ public class TrackMenuFragment extends ContextMenuScrollFragment implements Card }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } + private boolean isCurrentRecordingTrack() { + return app.getSavingTrackHelper().getCurrentTrack() == selectedGpxFile; + } + private void hide() { try { MapActivity mapActivity = getMapActivity(); diff --git a/OsmAnd/src/net/osmand/plus/track/TrackSelectSegmentBottomSheet.java b/OsmAnd/src/net/osmand/plus/track/TrackSelectSegmentBottomSheet.java new file mode 100644 index 0000000000..b49c2eb898 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/track/TrackSelectSegmentBottomSheet.java @@ -0,0 +1,142 @@ +package net.osmand.plus.track; + +import android.content.Context; +import android.graphics.Typeface; +import android.os.Bundle; +import android.text.Spannable; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; +import android.util.TypedValue; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import net.osmand.AndroidUtils; +import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.GPXFile; +import net.osmand.GPXUtilities.TrkSegment; +import net.osmand.plus.OsmAndFormatter; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.helpers.FontCache; +import net.osmand.plus.helpers.TrackSelectSegmentAdapter; +import net.osmand.plus.helpers.TrackSelectSegmentAdapter.OnItemClickListener; +import net.osmand.plus.widgets.style.CustomTypefaceSpan; +import net.osmand.util.Algorithms; + +import java.util.List; + +public class TrackSelectSegmentBottomSheet extends MenuBottomSheetDialogFragment { + + public static final String TAG = TrackSelectSegmentBottomSheet.class.getSimpleName(); + + private OsmandApplication app; + private GPXFile gpxFile; + + @Override + public void createMenuItems(Bundle savedInstanceState) { + app = requiredMyApplication(); + Context context = requireContext(); + + LayoutInflater inflater = UiUtilities.getInflater(context, nightMode); + View itemView = inflater.inflate(R.layout.bottom_sheet_select_segment, null, false); + + String titleGpxTrack = Algorithms.getFileWithoutDirs(gpxFile.path); + Typeface typeface = FontCache.getRobotoMedium(app); + String selectSegmentDescription = getString(R.string.select_segments_description, titleGpxTrack); + SpannableString gpxTrackName = new SpannableString(selectSegmentDescription); + int startIndex = selectSegmentDescription.indexOf(titleGpxTrack); + int descriptionColor = getResolvedColor(nightMode ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light); + int endIndex = startIndex + titleGpxTrack.length(); + gpxTrackName.setSpan(new CustomTypefaceSpan(typeface), startIndex, endIndex, 0); + gpxTrackName.setSpan(new ForegroundColorSpan(descriptionColor), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + + items.add(new BaseBottomSheetItem.Builder() + .setCustomView(itemView) + .create()); + + LinearLayout gpxTrackContainer = itemView.findViewById(R.id.gpx_track_container); + GPXUtilities.GPXTrackAnalysis analysis = gpxFile.getAnalysis(0); + + ImageView icon = gpxTrackContainer.findViewById(R.id.icon); + int sidePadding = AndroidUtils.dpToPx(context, 16f); + int bottomTopPadding = AndroidUtils.dpToPx(context, 2f); + + LinearLayout readContainer = gpxTrackContainer.findViewById(R.id.read_section); + readContainer.setPadding(0, bottomTopPadding, 0, bottomTopPadding); + TextView name = gpxTrackContainer.findViewById(R.id.name); + TextView description = itemView.findViewById(R.id.description); + TextView distance = gpxTrackContainer.findViewById(R.id.distance); + TextView pointsCount = gpxTrackContainer.findViewById(R.id.points_count); + TextView time = gpxTrackContainer.findViewById(R.id.time); + LinearLayout container = gpxTrackContainer.findViewById(R.id.container); + LinearLayout containerNameAndReadSection = gpxTrackContainer.findViewById(R.id.name_and_read_section_container); + container.setPadding(sidePadding, 0, 0, 0); + containerNameAndReadSection.setPadding(sidePadding, 0, 0, 0); + icon.setImageDrawable(app.getUIUtilities().getThemedIcon(R.drawable.ic_action_polygom_dark)); + name.setText(titleGpxTrack); + description.setText(gpxTrackName); + distance.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); + distance.setText(OsmAndFormatter.getFormattedDistance(analysis.totalDistance, app)); + pointsCount.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); + pointsCount.setText(String.valueOf(analysis.wptPoints)); + time.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); + time.setText(analysis.isTimeSpecified() ? Algorithms.formatDuration((int) (analysis.timeSpan / 1000), app.accessibilityEnabled()) : ""); + + RecyclerView recyclerView = itemView.findViewById(R.id.gpx_segment_list); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + recyclerView.setNestedScrollingEnabled(false); + + List segments = gpxFile.getNonEmptyTrkSegments(false); + TrackSelectSegmentAdapter adapterSegments = new TrackSelectSegmentAdapter(context, segments); + adapterSegments.setAdapterListener(new OnItemClickListener() { + @Override + public void onItemClick(int position) { + Fragment fragment = getTargetFragment(); + if (fragment instanceof OnSegmentSelectedListener) { + ((OnSegmentSelectedListener) fragment).onSegmentSelect(gpxFile, position); + } + dismiss(); + } + }); + recyclerView.setAdapter(adapterSegments); + + gpxTrackContainer.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Fragment fragment = getTargetFragment(); + if (fragment instanceof OnSegmentSelectedListener) { + ((OnSegmentSelectedListener) fragment).onSegmentSelect(gpxFile, -1); + } + dismiss(); + } + }); + + } + + public interface OnSegmentSelectedListener { + void onSegmentSelect(GPXFile gpxFile, int selectedSegment); + } + + public static void showInstance(@NonNull FragmentManager fragmentManager, @NonNull GPXFile gpxFile, @Nullable Fragment target) { + if (!fragmentManager.isStateSaved()) { + TrackSelectSegmentBottomSheet fragment = new TrackSelectSegmentBottomSheet(); + fragment.setRetainInstance(true); + fragment.setTargetFragment(target, 0); + fragment.gpxFile = gpxFile; + fragment.show(fragmentManager, TAG); + } + } +} \ No newline at end of file