diff --git a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java index 65e69094ac..f5232d80c0 100644 --- a/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java +++ b/OsmAnd-java/src/main/java/net/osmand/GPXUtilities.java @@ -287,17 +287,17 @@ public class GPXUtilities { public Object renderer; - public List splitByDistance(double meters) { - return split(getDistanceMetric(), getTimeSplit(), meters); + public List splitByDistance(double meters, boolean joinSegments) { + return split(getDistanceMetric(), getTimeSplit(), meters, joinSegments); } - public List splitByTime(int seconds) { - return split(getTimeSplit(), getDistanceMetric(), seconds); + public List splitByTime(int seconds, boolean joinSegments) { + return split(getTimeSplit(), getDistanceMetric(), seconds, joinSegments); } - private List split(SplitMetric metric, SplitMetric secondaryMetric, double metricLimit) { + private List split(SplitMetric metric, SplitMetric secondaryMetric, double metricLimit, boolean joinSegments) { List splitSegments = new ArrayList<>(); - splitSegment(metric, secondaryMetric, metricLimit, splitSegments, this); + splitSegment(metric, secondaryMetric, metricLimit, splitSegments, this, joinSegments); return convert(splitSegments); } @@ -853,7 +853,7 @@ public class GPXUtilities { private static void splitSegment(SplitMetric metric, SplitMetric secondaryMetric, double metricLimit, List splitSegments, - TrkSegment segment) { + TrkSegment segment, boolean joinSegments) { double currentMetricEnd = metricLimit; double secondaryMetricEnd = 0; SplitSegment sp = new SplitSegment(segment, 0, 0); @@ -862,8 +862,11 @@ public class GPXUtilities { for (int k = 0; k < segment.points.size(); k++) { WptPt point = segment.points.get(k); if (k > 0) { - double currentSegment = metric.metric(prev, point); - secondaryMetricEnd += secondaryMetric.metric(prev, point); + double currentSegment = 0; + if (!(segment.generalSegment && joinSegments && point.firstPoint)) { + currentSegment = metric.metric(prev, point); + secondaryMetricEnd += secondaryMetric.metric(prev, point); + } while (total + currentSegment > currentMetricEnd) { double p = currentMetricEnd - total; double cf = (p / currentSegment); diff --git a/OsmAnd/res/layout/gpx_item_altitude.xml b/OsmAnd/res/layout/gpx_item_altitude.xml index 884018d248..1af518effa 100644 --- a/OsmAnd/res/layout/gpx_item_altitude.xml +++ b/OsmAnd/res/layout/gpx_item_altitude.xml @@ -34,7 +34,7 @@ android:gravity="center_vertical" android:paddingLeft="@dimen/content_padding" android:paddingRight="@dimen/content_padding" - android:text="@string/gpx_join_gaps" + android:text="@string/join_segments" android:textColor="?android:attr/textColorPrimary" android:textSize="@dimen/default_list_text_size" osmand:typeface="@string/font_roboto_regular" /> diff --git a/OsmAnd/res/layout/gpx_item_general.xml b/OsmAnd/res/layout/gpx_item_general.xml index b7894c13f9..697665facf 100644 --- a/OsmAnd/res/layout/gpx_item_general.xml +++ b/OsmAnd/res/layout/gpx_item_general.xml @@ -34,7 +34,7 @@ android:gravity="center_vertical" android:paddingLeft="@dimen/content_padding" android:paddingRight="@dimen/content_padding" - android:text="@string/gpx_join_gaps" + android:text="@string/join_segments" android:textColor="?android:attr/textColorPrimary" android:textSize="@dimen/default_list_text_size" osmand:typeface="@string/font_roboto_regular" /> diff --git a/OsmAnd/res/layout/gpx_item_speed.xml b/OsmAnd/res/layout/gpx_item_speed.xml index 5969fe376d..a515cc467e 100644 --- a/OsmAnd/res/layout/gpx_item_speed.xml +++ b/OsmAnd/res/layout/gpx_item_speed.xml @@ -34,7 +34,7 @@ android:gravity="center_vertical" android:paddingLeft="@dimen/content_padding" android:paddingRight="@dimen/content_padding" - android:text="@string/gpx_join_gaps" + android:text="@string/join_segments" android:textColor="?android:attr/textColorPrimary" android:textSize="@dimen/default_list_text_size" osmand:typeface="@string/font_roboto_regular" /> diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 9604fb9e4c..9010c03e24 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -13,6 +13,7 @@ --> Node networks Show node network cycle routes + Join segments Download map dialog Dialogs and notifications Control popups, dialogs and notifications that OsmAnd show during usage. diff --git a/OsmAnd/src/net/osmand/plus/GPXDatabase.java b/OsmAnd/src/net/osmand/plus/GPXDatabase.java index c3e53caa65..965ec9450d 100644 --- a/OsmAnd/src/net/osmand/plus/GPXDatabase.java +++ b/OsmAnd/src/net/osmand/plus/GPXDatabase.java @@ -16,7 +16,7 @@ import java.util.List; public class GPXDatabase { private static final String DB_NAME = "gpx_database"; - private static final int DB_VERSION = 9; + private static final int DB_VERSION = 10; private static final String GPX_TABLE_NAME = "gpxTable"; private static final String GPX_COL_NAME = "fileName"; private static final String GPX_COL_DIR = "fileDir"; @@ -54,6 +54,8 @@ public class GPXDatabase { private static final String GPX_COL_SHOW_AS_MARKERS = "showAsMarkers"; + private static final String GPX_COL_JOIN_SEGMENTS = "joinSegments"; + public static final int GPX_SPLIT_TYPE_NO_SPLIT = -1; public static final int GPX_SPLIT_TYPE_DISTANCE = 1; public static final int GPX_SPLIT_TYPE_TIME = 2; @@ -86,7 +88,8 @@ public class GPXDatabase { GPX_COL_SPLIT_INTERVAL + " double, " + GPX_COL_API_IMPORTED + " int, " + // 1 = true, 0 = false GPX_COL_WPT_CATEGORY_NAMES + " TEXT, " + - GPX_COL_SHOW_AS_MARKERS + " int);"; // 1 = true, 0 = false + GPX_COL_SHOW_AS_MARKERS + " int, " + // 1 = true, 0 = false + GPX_COL_JOIN_SEGMENTS + " int);"; // 1 = true, 0 = false private static final String GPX_TABLE_SELECT = "SELECT " + GPX_COL_NAME + ", " + @@ -113,7 +116,8 @@ public class GPXDatabase { GPX_COL_SPLIT_INTERVAL + ", " + GPX_COL_API_IMPORTED + ", " + GPX_COL_WPT_CATEGORY_NAMES + ", " + - GPX_COL_SHOW_AS_MARKERS + + GPX_COL_SHOW_AS_MARKERS + ", " + + GPX_COL_JOIN_SEGMENTS + " FROM " + GPX_TABLE_NAME; private static final String GPX_TABLE_UPDATE_ANALYSIS = "UPDATE " + @@ -148,6 +152,7 @@ public class GPXDatabase { private double splitInterval; private boolean apiImported; private boolean showAsMarkers; + private boolean joinSegments; public GpxDataItem(File file, GPXTrackAnalysis analysis) { this.file = file; @@ -200,6 +205,14 @@ public class GPXDatabase { this.showAsMarkers = showAsMarkers; } + public boolean isJoinSegments() { + return joinSegments; + } + + public void setJoinSegments(boolean joinSegments) { + this.joinSegments = joinSegments; + } + @Override public int hashCode() { return file != null ? file.hashCode() : 0; @@ -322,6 +335,12 @@ public class GPXDatabase { " SET " + GPX_COL_SHOW_AS_MARKERS + " = ? " + "WHERE " + GPX_COL_SHOW_AS_MARKERS + " IS NULL", new Object[]{0}); } + if (oldVersion < 10) { + db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_JOIN_SEGMENTS + " int"); + db.execSQL("UPDATE " + GPX_TABLE_NAME + + " SET " + GPX_COL_JOIN_SEGMENTS + " = ? " + + "WHERE " + GPX_COL_JOIN_SEGMENTS + " IS NULL", new Object[]{0}); + } db.execSQL("CREATE INDEX IF NOT EXISTS " + GPX_INDEX_NAME_DIR + " ON " + GPX_TABLE_NAME + " (" + GPX_COL_NAME + ", " + GPX_COL_DIR + ");"); } @@ -404,6 +423,25 @@ public class GPXDatabase { return false; } + public boolean updateJoinSegments(GpxDataItem item, boolean joinSegments) { + SQLiteConnection db = openConnection(false); + if (db != null) { + try { + String fileName = getFileName(item.file); + String fileDir = getFileDir(item.file); + db.execSQL("UPDATE " + GPX_TABLE_NAME + " SET " + + GPX_COL_JOIN_SEGMENTS + " = ? " + + " WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?", + new Object[]{joinSegments ? 1 : 0, fileName, fileDir}); + item.setJoinSegments(joinSegments); + } finally { + db.close(); + } + return true; + } + return false; + } + public boolean updateSplit(@NonNull GpxDataItem item, int splitType, double splitInterval) { SQLiteConnection db = openConnection(false); if (db != null){ @@ -480,12 +518,12 @@ public class GPXDatabase { } if (a != null) { db.execSQL( - "INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - new Object[]{ fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime, + "INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + new Object[] {fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime, a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown, a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, color, item.file.lastModified(), item.splitType, item.splitInterval, item.apiImported ? 1 : 0, - Algorithms.encodeStringSet(item.analysis.wptCategoryNames), item.showAsMarkers ? 1 : 0}); + Algorithms.encodeStringSet(item.analysis.wptCategoryNames), item.showAsMarkers ? 1 : 0, item.joinSegments ? 1 : 0}); } else { db.execSQL("INSERT INTO " + GPX_TABLE_NAME + "(" + GPX_COL_NAME + ", " + @@ -495,9 +533,10 @@ public class GPXDatabase { GPX_COL_SPLIT_TYPE + ", " + GPX_COL_SPLIT_INTERVAL + ", " + GPX_COL_API_IMPORTED + ", " + - GPX_COL_SHOW_AS_MARKERS + - ") VALUES (?, ?, ?, ?, ?, ?, ?, ?)", - new Object[]{fileName, fileDir, color, 0, item.splitType, item.splitInterval, item.apiImported ? 1 : 0, item.showAsMarkers ? 1 : 0}); + GPX_COL_SHOW_AS_MARKERS + ", " + + GPX_COL_JOIN_SEGMENTS + + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", + new Object[] {fileName, fileDir, color, 0, item.splitType, item.splitInterval, item.apiImported ? 1 : 0, item.showAsMarkers ? 1 : 0, item.joinSegments ? 1 : 0}); } } @@ -575,6 +614,7 @@ public class GPXDatabase { boolean apiImported = query.getInt(22) == 1; String wptCategoryNames = query.getString(23); boolean showAsMarkers = query.getInt(24) == 1; + boolean joinSegments = query.getInt(25) == 1; GPXTrackAnalysis a = new GPXTrackAnalysis(); a.totalDistance = totalDistance; @@ -615,6 +655,7 @@ public class GPXDatabase { item.splitInterval = splitInterval; item.apiImported = apiImported; item.showAsMarkers = showAsMarkers; + item.joinSegments = joinSegments; return item; } diff --git a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java index aba5c65056..1c1c6cf8bf 100644 --- a/OsmAnd/src/net/osmand/plus/GpxDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxDbHelper.java @@ -89,6 +89,12 @@ public class GpxDbHelper { return res; } + public boolean updateJoinSegments(@NonNull GpxDataItem item, boolean joinSegments) { + boolean res = db.updateJoinSegments(item, joinSegments); + putToCache(item); + return res; + } + public boolean remove(File file) { boolean res = db.remove(file); itemsCache.remove(file); diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index ba47591bc6..20d57aeb4f 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -10,6 +10,7 @@ import android.support.v4.content.ContextCompat; import java.util.Map; import java.util.Map.Entry; + import net.osmand.GPXUtilities; import net.osmand.IProgress; import net.osmand.PlatformUtil; @@ -168,12 +169,12 @@ public class GpxSelectionHelper { selectedGpxFile.setDisplayGroups(groups, app); } else if (dataItem.getSplitType() == GPXDatabase.GPX_SPLIT_TYPE_DISTANCE) { for (GpxDisplayGroup model : groups) { - model.splitByDistance(app, dataItem.getSplitInterval()); + model.splitByDistance(app, dataItem.getSplitInterval(), dataItem.isJoinSegments()); } selectedGpxFile.setDisplayGroups(groups, app); } else if (dataItem.getSplitType() == GPXDatabase.GPX_SPLIT_TYPE_TIME) { for (GpxDisplayGroup model : groups) { - model.splitByTime(app, (int) dataItem.getSplitInterval()); + model.splitByTime(app, (int) dataItem.getSplitInterval(), dataItem.isJoinSegments()); } selectedGpxFile.setDisplayGroups(groups, app); } @@ -316,6 +317,10 @@ public class GpxSelectionHelper { } private static void processGroupTrack(@NonNull OsmandApplication app, @NonNull GpxDisplayGroup group) { + processGroupTrack(app, group, false); + } + + private static void processGroupTrack(@NonNull OsmandApplication app, @NonNull GpxDisplayGroup group, boolean joinSegments) { if (group.track == null) { return; } @@ -333,14 +338,14 @@ public class GpxSelectionHelper { GPXTrackAnalysis[] as; boolean split = true; if (group.splitDistance > 0) { - List trackSegments = r.splitByDistance(group.splitDistance); + List trackSegments = r.splitByDistance(group.splitDistance, joinSegments); as = trackSegments.toArray(new GPXTrackAnalysis[trackSegments.size()]); } else if (group.splitTime > 0) { - List trackSegments = r.splitByTime(group.splitTime); + List trackSegments = r.splitByTime(group.splitTime, joinSegments); as = trackSegments.toArray(new GPXTrackAnalysis[trackSegments.size()]); } else { split = false; - as = new GPXTrackAnalysis[]{GPXTrackAnalysis.segment(0, r)}; + as = new GPXTrackAnalysis[] {GPXTrackAnalysis.segment(0, r)}; } for (GPXTrackAnalysis analysis : as) { GpxDisplayItem item = new GpxDisplayItem(); @@ -868,18 +873,18 @@ public class GpxSelectionHelper { processGroupTrack(app, this); } - public void splitByDistance(OsmandApplication app, double meters) { + public void splitByDistance(OsmandApplication app, double meters, boolean joinSegments) { list.clear(); splitDistance = meters; splitTime = -1; - processGroupTrack(app, this); + processGroupTrack(app, this, joinSegments); } - public void splitByTime(OsmandApplication app, int seconds) { + public void splitByTime(OsmandApplication app, int seconds, boolean joinSegments) { list.clear(); splitDistance = -1; splitTime = seconds; - processGroupTrack(app, this); + processGroupTrack(app, this, joinSegments); } public int getColor() { diff --git a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java index 974ab4b786..1efb9eaf84 100644 --- a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java @@ -410,6 +410,17 @@ public class TrackActivity extends TabActivity { } } + public boolean setJoinSegments(boolean joinSegments) { + if (gpxDataItem != null) { + return app.getGpxDbHelper().updateJoinSegments(gpxDataItem, joinSegments); + } + return false; + } + + public boolean isJoinSegments() { + return gpxDataItem != null && gpxDataItem.isJoinSegments(); + } + private static class GPXFileLoaderTask extends AsyncTask { private OsmandApplication app; diff --git a/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNotesPlugin.java b/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNotesPlugin.java index 3a175b6bf4..1e5328f2d6 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNotesPlugin.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNotesPlugin.java @@ -112,8 +112,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin { private static final int TAKE_VIDEO_NOTE_ITEM_ORDER = 4300; private static final int TAKE_PHOTO_NOTE_ITEM_ORDER = 4500; - private static Method mRegisterMediaButtonEventReceiver; - private static Method mUnregisterMediaButtonEventReceiver; +// private static Method mRegisterMediaButtonEventReceiver; +// private static Method mUnregisterMediaButtonEventReceiver; private OsmandApplication app; private TextInfoWidget recordControl; @@ -513,24 +513,24 @@ public class AudioVideoNotesPlugin extends OsmandPlugin { } - private static void initializeRemoteControlRegistrationMethods() { - try { +// private static void initializeRemoteControlRegistrationMethods() { +// try { // API 8 - if (mRegisterMediaButtonEventReceiver == null) { - mRegisterMediaButtonEventReceiver = AudioManager.class.getMethod("registerMediaButtonEventReceiver", - new Class[]{ComponentName.class}); - } - if (mUnregisterMediaButtonEventReceiver == null) { - mUnregisterMediaButtonEventReceiver = AudioManager.class.getMethod("unregisterMediaButtonEventReceiver", - new Class[]{ComponentName.class}); - } - /* success, this device will take advantage of better remote */ - /* control event handling */ - } catch (NoSuchMethodException nsme) { - /* failure, still using the legacy behavior, but this app */ - /* is future-proof! */ - } - } +// if (mRegisterMediaButtonEventReceiver == null) { +// mRegisterMediaButtonEventReceiver = AudioManager.class.getMethod("registerMediaButtonEventReceiver", +// new Class[]{ComponentName.class}); +// } +// if (mUnregisterMediaButtonEventReceiver == null) { +// mUnregisterMediaButtonEventReceiver = AudioManager.class.getMethod("unregisterMediaButtonEventReceiver", +// new Class[]{ComponentName.class}); +// } +// /* success, this device will take advantage of better remote */ +// /* control event handling */ +// } catch (NoSuchMethodException nsme) { +// /* failure, still using the legacy behavior, but this app */ +// /* is future-proof! */ +// } +// } @Override public String getId() { @@ -579,11 +579,11 @@ public class AudioVideoNotesPlugin extends OsmandPlugin { @Override public boolean init(@NonNull final OsmandApplication app, Activity activity) { - initializeRemoteControlRegistrationMethods(); - AudioManager am = (AudioManager) app.getSystemService(Context.AUDIO_SERVICE); - if (am != null) { - registerMediaListener(am); - } +// initializeRemoteControlRegistrationMethods(); +// AudioManager am = (AudioManager) app.getSystemService(Context.AUDIO_SERVICE); +// if (am != null) { +// registerMediaListener(am); +// } return true; } @@ -602,30 +602,30 @@ public class AudioVideoNotesPlugin extends OsmandPlugin { return currentRecording; } - private void registerMediaListener(AudioManager am) { +// private void registerMediaListener(AudioManager am) { +// +// ComponentName receiver = new ComponentName(app.getPackageName(), MediaRemoteControlReceiver.class.getName()); +// try { +// if (mRegisterMediaButtonEventReceiver == null) { +// return; +// } +// mRegisterMediaButtonEventReceiver.invoke(am, receiver); +// } catch (Exception ite) { +// log.error(ite.getMessage(), ite); +// } +// } - ComponentName receiver = new ComponentName(app.getPackageName(), MediaRemoteControlReceiver.class.getName()); - try { - if (mRegisterMediaButtonEventReceiver == null) { - return; - } - mRegisterMediaButtonEventReceiver.invoke(am, receiver); - } catch (Exception ite) { - log.error(ite.getMessage(), ite); - } - } - - private void unregisterMediaListener(AudioManager am) { - ComponentName receiver = new ComponentName(app.getPackageName(), MediaRemoteControlReceiver.class.getName()); - try { - if (mUnregisterMediaButtonEventReceiver == null) { - return; - } - mUnregisterMediaButtonEventReceiver.invoke(am, receiver); - } catch (Exception ite) { - log.error(ite.getMessage(), ite); - } - } +// private void unregisterMediaListener(AudioManager am) { +// ComponentName receiver = new ComponentName(app.getPackageName(), MediaRemoteControlReceiver.class.getName()); +// try { +// if (mUnregisterMediaButtonEventReceiver == null) { +// return; +// } +// mUnregisterMediaButtonEventReceiver.invoke(am, receiver); +// } catch (Exception ite) { +// log.error(ite.getMessage(), ite); +// } +// } @Override public void registerLayerContextMenuActions(final OsmandMapTileView mapView, ContextMenuAdapter adapter, final MapActivity mapActivity) { @@ -869,8 +869,8 @@ public class AudioVideoNotesPlugin extends OsmandPlugin { @Override public void mapActivityResume(MapActivity activity) { this.mapActivity = activity; - ((AudioManager) activity.getSystemService(Context.AUDIO_SERVICE)).registerMediaButtonEventReceiver( - new ComponentName(activity, MediaRemoteControlReceiver.class)); +// ((AudioManager) activity.getSystemService(Context.AUDIO_SERVICE)).registerMediaButtonEventReceiver( +// new ComponentName(activity, MediaRemoteControlReceiver.class)); if (runAction != -1) { takeAction(activity, actionLon, actionLat, runAction); runAction = -1; @@ -1712,10 +1712,10 @@ public class AudioVideoNotesPlugin extends OsmandPlugin { @Override public void disable(OsmandApplication app) { - AudioManager am = (AudioManager) app.getSystemService(Context.AUDIO_SERVICE); - if (am != null) { - unregisterMediaListener(am); - } +// AudioManager am = (AudioManager) app.getSystemService(Context.AUDIO_SERVICE); +// if (am != null) { +// unregisterMediaListener(am); +// } } @Override diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SplitSegmentDialogFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/SplitSegmentDialogFragment.java index 37e4e6a904..dc2202ec9c 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/SplitSegmentDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/SplitSegmentDialogFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.myplaces; +import android.app.Dialog; import android.content.DialogInterface; import android.content.res.ColorStateList; import android.graphics.Paint; @@ -26,6 +27,7 @@ import android.widget.ProgressBar; import android.widget.TextView; import net.osmand.AndroidUtils; +import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup; @@ -57,7 +59,10 @@ public class SplitSegmentDialogFragment extends DialogFragment { private SplitSegmentsAdapter adapter; private View headerView; + private GpxDisplayItem gpxItem; private GpxDisplayItemType[] filterTypes = {GpxDisplayItemType.TRACK_SEGMENT}; + private GPXUtilities.TrkSegment trkSegment; + private List options = new ArrayList<>(); private List distanceSplit = new ArrayList<>(); private TIntArrayList timeSplit = new TIntArrayList(); @@ -68,6 +73,7 @@ public class SplitSegmentDialogFragment extends DialogFragment { private Rect minMaxSpeedTextBounds; private ListView listView; private ProgressBar progressBar; + private boolean joinSegments; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -87,7 +93,6 @@ public class SplitSegmentDialogFragment extends DialogFragment { listView.setBackgroundColor(getResources().getColor( trackActivity.getMyApplication().getSettings().isLightContent() ? R.color.activity_background_color_light : R.color.activity_background_color_dark)); - trackActivity.onAttachFragment(this); } @Override @@ -178,6 +183,27 @@ public class SplitSegmentDialogFragment extends DialogFragment { updateContent(); } + @Override + public void onDestroyView() { + Dialog dialog = getDialog(); + if (dialog != null && getRetainInstance()) { + dialog.setDismissMessage(null); + } + super.onDestroyView(); + } + + public void setGpxItem(GpxDisplayItem gpxItem) { + this.gpxItem = gpxItem; + } + + public void setTrkSegment(GPXUtilities.TrkSegment trkSegment) { + this.trkSegment = trkSegment; + } + + public void setJoinSegments(boolean joinSegments) { + this.joinSegments = joinSegments; + } + private void updateHeader() { final View splitIntervalView = headerView.findViewById(R.id.split_interval_view); @@ -226,8 +252,7 @@ public class SplitSegmentDialogFragment extends DialogFragment { if (getTrackActivity() != null) { adapter.clear(); adapter.setNotifyOnChange(false); - GpxDisplayItem overviewSegments = getOverviewSegment(); - adapter.add(overviewSegments); + adapter.add(gpxItem); List splitSegments = getSplitSegments(); adapter.addAll(splitSegments); adapter.notifyDataSetChanged(); @@ -370,26 +395,32 @@ public class SplitSegmentDialogFragment extends DialogFragment { List splitSegments = new ArrayList<>(); if (trackActivity != null) { List result = trackActivity.getGpxFile(true); - if (result != null && result.size() > 0) { - if (result.get(0).isSplitDistance() || result.get(0).isSplitTime()) { - splitSegments.addAll(result.get(0).getModifiableList()); + if (result != null && result.size() > 0 && trkSegment.points.size() > 0) { + for (GpxDisplayGroup group : result) { + splitSegments.addAll(collectDisplayItemsFromGroup(group)); } } } return splitSegments; } - @Nullable - private GpxDisplayItem getOverviewSegment() { - TrackActivity trackActivity = getTrackActivity(); - GpxDisplayItem overviewSegment = null; - if (trackActivity != null) { - List result = trackActivity.getGpxFile(false); - if (result.size() > 0 && result.get(0).getModifiableList().size() > 0) { - overviewSegment = result.get(0).getModifiableList().get(0); + private List collectDisplayItemsFromGroup(GpxDisplayGroup group) { + List splitSegments = new ArrayList<>(); + boolean generalTrack = gpxItem.isGeneralTrack(); + boolean generalGroup = group.isGeneralTrack(); + if ((group.isSplitDistance() || group.isSplitTime()) && (!generalGroup && !generalTrack || generalGroup && generalTrack)) { + boolean itemsForSelectedSegment = false; + for (GpxDisplayItem item : group.getModifiableList()) { + itemsForSelectedSegment = trkSegment.points.get(0).equals(item.locationStart) || itemsForSelectedSegment; + if (itemsForSelectedSegment) { + splitSegments.add(item); + } + if (trkSegment.points.get(trkSegment.points.size() - 1).equals(item.locationEnd)) { + break; + } } } - return overviewSegment; + return splitSegments; } protected boolean hasFilterType(GpxDisplayItemType filterType) { @@ -446,7 +477,7 @@ public class SplitSegmentDialogFragment extends DialogFragment { overviewTextView.setTextColor(defaultTextColor); overviewTextView.setText(app.getString(R.string.shared_string_overview)); if (currentGpxDisplayItem != null) { - overviewTextView.setText(app.getString(R.string.shared_string_overview) + " (" + Integer.toString(currentGpxDisplayItem.analysis.points) + ")"); + overviewTextView.setText(app.getString(R.string.shared_string_overview) + " (" + Integer.toString(currentGpxDisplayItem.analysis.points) + ")"); ((TextView) convertView.findViewById(R.id.fragment_count_text)).setText(app.getString(R.string.shared_string_time_span) + ": " + Algorithms.formatDuration((int) (currentGpxDisplayItem.analysis.timeSpan / 1000), app.accessibilityEnabled())); } } else { @@ -498,7 +529,8 @@ public class SplitSegmentDialogFragment extends DialogFragment { TextView distanceOrTimeSpanText = (TextView) convertView.findViewById(R.id.distance_or_time_span_text); if (position == 0) { distanceOrTimeSpanImageView.setImageDrawable(ic.getIcon(R.drawable.ic_action_track_16, app.getSettings().isLightContent() ? R.color.gpx_split_segment_icon_color : 0)); - distanceOrTimeSpanValue.setText(OsmAndFormatter.getFormattedDistance(analysis.totalDistance, app)); + float totalDistance = joinSegments && gpxItem.isGeneralTrack() ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; + distanceOrTimeSpanValue.setText(OsmAndFormatter.getFormattedDistance(totalDistance, app)); distanceOrTimeSpanText.setText(app.getString(R.string.distance)); } else { if (currentGpxDisplayItem.group.isSplitDistance()) { @@ -706,9 +738,9 @@ public class SplitSegmentDialogFragment extends DialogFragment { if (selectedSplitInterval == 0) { model.noSplit(app); } else if (distanceSplit.get(selectedSplitInterval) > 0) { - model.splitByDistance(app, distanceSplit.get(selectedSplitInterval)); + model.splitByDistance(app, distanceSplit.get(selectedSplitInterval), joinSegments); } else if (timeSplit.get(selectedSplitInterval) > 0) { - model.splitByTime(app, timeSplit.get(selectedSplitInterval)); + model.splitByTime(app, timeSplit.get(selectedSplitInterval), joinSegments); } } @@ -716,9 +748,13 @@ public class SplitSegmentDialogFragment extends DialogFragment { } } - public static boolean showInstance(@NonNull TrackActivity trackActivity) { + public static boolean showInstance(@NonNull TrackActivity trackActivity, @NonNull GpxDisplayItem gpxItem, GPXUtilities.TrkSegment trkSegment) { try { SplitSegmentDialogFragment fragment = new SplitSegmentDialogFragment(); + fragment.setGpxItem(gpxItem); + fragment.setTrkSegment(trkSegment); + fragment.setRetainInstance(true); + fragment.setJoinSegments(trackActivity.isJoinSegments()); fragment.show(trackActivity.getSupportFragmentManager(), TAG); return true; } catch (RuntimeException e) { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java index cb255cb162..f6c62bdbe1 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackActivityFragmentAdapter.java @@ -992,6 +992,7 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener { private List distanceSplit; private TIntArrayList timeSplit; private int selectedSplitInterval; + private boolean joinSegments; SplitTrackAsyncTask(@NonNull TrackActivity activity, @NonNull TrackActivityFragmentAdapter fragmentAdapter, @@ -1006,6 +1007,7 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener { selectedSplitInterval = fragmentAdapter.selectedSplitInterval; distanceSplit = fragmentAdapter.distanceSplit; timeSplit = fragmentAdapter.timeSplit; + joinSegments = activity.isJoinSegments(); } @Override @@ -1042,9 +1044,9 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener { if (selectedSplitInterval == 0) { model.noSplit(app); } else if (distanceSplit.get(selectedSplitInterval) > 0) { - model.splitByDistance(app, distanceSplit.get(selectedSplitInterval)); + model.splitByDistance(app, distanceSplit.get(selectedSplitInterval), joinSegments); } else if (timeSplit.get(selectedSplitInterval) > 0) { - model.splitByTime(app, timeSplit.get(selectedSplitInterval)); + model.splitByTime(app, timeSplit.get(selectedSplitInterval), joinSegments); } } return null; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java index 07003dc819..c85755daa0 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackSegmentFragment.java @@ -93,7 +93,6 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit private boolean updateEnable; private boolean chartClicked; - private boolean joinGapsEnabled; private IconPopupMenu generalPopupMenu; private IconPopupMenu altitudePopupMenu; @@ -719,10 +718,13 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit view.findViewById(R.id.gpx_join_gaps_container).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - joinGapsEnabled = !joinGapsEnabled; - for (int i = 0; i < getCount(); i++) { - View view = getViewAtPosition(i); - updateJoinGapsInfo(view, i); + TrackActivity activity = getTrackActivity(); + if (activity != null && activity.setJoinSegments(!activity.isJoinSegments())) { + updateSplitView(); + for (int i = 0; i < getCount(); i++) { + View view = getViewAtPosition(i); + updateJoinGapsInfo(view, i); + } } } }); @@ -856,10 +858,13 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit view.findViewById(R.id.gpx_join_gaps_container).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - joinGapsEnabled = !joinGapsEnabled; - for (int i = 0; i < getCount(); i++) { - View view = getViewAtPosition(i); - updateJoinGapsInfo(view, i); + TrackActivity activity = getTrackActivity(); + if (activity != null && activity.setJoinSegments(!activity.isJoinSegments())) { + updateSplitView(); + for (int i = 0; i < getCount(); i++) { + View view = getViewAtPosition(i); + updateJoinGapsInfo(view, i); + } } } }); @@ -962,10 +967,13 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit view.findViewById(R.id.gpx_join_gaps_container).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - joinGapsEnabled = !joinGapsEnabled; - for (int i = 0; i < getCount(); i++) { - View view = getViewAtPosition(i); - updateJoinGapsInfo(view, i); + TrackActivity activity = getTrackActivity(); + if (activity != null && activity.setJoinSegments(!activity.isJoinSegments())) { + updateSplitView(); + for (int i = 0; i < getCount(); i++) { + View view = getViewAtPosition(i); + updateJoinGapsInfo(view, i); + } } } }); @@ -1138,22 +1146,24 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit } void updateJoinGapsInfo(View view, int position) { - if (view != null) { + TrackActivity activity = getTrackActivity(); + if (view != null && activity != null) { GPXTrackAnalysis analysis = gpxItem.analysis; GPXTabItemType tabType = tabTypes[position]; boolean visible = gpxItem.isGeneralTrack() && analysis != null && tabType.equals(GPXTabItemType.GPX_TAB_ITEM_GENERAL); AndroidUiHelper.updateVisibility(view.findViewById(R.id.gpx_join_gaps_container), visible); - ((SwitchCompat) view.findViewById(R.id.gpx_join_gaps_switch)).setChecked(joinGapsEnabled); + boolean joinSegments = activity.isJoinSegments(); + ((SwitchCompat) view.findViewById(R.id.gpx_join_gaps_switch)).setChecked(joinSegments); if (analysis != null) { if (tabType.equals(GPXTabItemType.GPX_TAB_ITEM_GENERAL)) { - float totalDistance = joinGapsEnabled && gpxItem.isGeneralTrack() ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; - float timeSpan = joinGapsEnabled && gpxItem.isGeneralTrack() ? analysis.timeSpanWithoutGaps : analysis.timeSpan; + float totalDistance = joinSegments && gpxItem.isGeneralTrack() ? analysis.totalDistanceWithoutGaps : analysis.totalDistance; + float timeSpan = joinSegments && gpxItem.isGeneralTrack() ? analysis.timeSpanWithoutGaps : analysis.timeSpan; ((TextView) view.findViewById(R.id.distance_text)).setText(OsmAndFormatter.getFormattedDistance(totalDistance, app)); ((TextView) view.findViewById(R.id.duration_text)).setText(Algorithms.formatDuration((int) (timeSpan / 1000), app.accessibilityEnabled())); } else if (tabType.equals(GPXTabItemType.GPX_TAB_ITEM_SPEED)) { - long timeMoving = joinGapsEnabled && gpxItem.isGeneralTrack() ? analysis.timeMovingWithoutGaps : analysis.timeMoving; - float totalDistanceMoving = joinGapsEnabled && gpxItem.isGeneralTrack() ? analysis.totalDistanceMovingWithoutGaps : analysis.totalDistanceMoving; + long timeMoving = joinSegments && gpxItem.isGeneralTrack() ? analysis.timeMovingWithoutGaps : analysis.timeMoving; + float totalDistanceMoving = joinSegments && gpxItem.isGeneralTrack() ? analysis.totalDistanceMovingWithoutGaps : analysis.totalDistanceMoving; ((TextView) view.findViewById(R.id.time_moving_text)).setText(Algorithms.formatDuration((int) (timeMoving / 1000), app.accessibilityEnabled())); ((TextView) view.findViewById(R.id.distance_text)).setText(OsmAndFormatter.getFormattedDistance(totalDistanceMoving, app)); @@ -1177,7 +1187,7 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit private TrkSegment getTrkSegment() { for (Track t : gpxItem.group.getGpx().tracks) { - if (!t.generalTrack) { + if (!t.generalTrack && !gpxItem.isGeneralTrack() || t.generalTrack && gpxItem.isGeneralTrack()) { for (TrkSegment s : t.segments) { if (s.points.size() > 0 && s.points.get(0).equals(gpxItem.analysis.locationStart)) { return s; @@ -1245,12 +1255,12 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit MapActivity.launchMapActivityMoveToTop(getActivity()); } - } - void openSplitIntervalScreen() { - TrackActivity activity = getTrackActivity(); - if (activity != null) { - SplitSegmentDialogFragment.showInstance(activity); + private void openSplitIntervalScreen() { + TrackActivity activity = getTrackActivity(); + if (activity != null) { + SplitSegmentDialogFragment.showInstance(activity, gpxItem, getTrkSegment()); + } } }