diff --git a/OsmAnd/src/net/osmand/plus/plugins/osmedit/asynctasks/UploadGPXFilesTask.java b/OsmAnd/src/net/osmand/plus/plugins/osmedit/asynctasks/UploadGPXFilesTask.java index 4ea49b357e1..c6a8b4f7f1c 100644 --- a/OsmAnd/src/net/osmand/plus/plugins/osmedit/asynctasks/UploadGPXFilesTask.java +++ b/OsmAnd/src/net/osmand/plus/plugins/osmedit/asynctasks/UploadGPXFilesTask.java @@ -19,11 +19,12 @@ import java.io.File; import java.lang.ref.WeakReference; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; public class UploadGPXFilesTask extends AsyncTask { - public static final String DEFAULT_ACTIVITY_TAG = "road_cycling"; - private final OsmandApplication app; private final GpxDbHelper gpxDbHelper; private final OpenstreetmapRemoteUtil remoteUtil; @@ -32,13 +33,15 @@ public class UploadGPXFilesTask extends AsyncTask { private final String commonTags; private final String visibility; private final String commonDescription; + private final String defaultActivity; private final UploadGpxListener listener; public UploadGPXFilesTask(@NonNull Activity activity, @NonNull String commonDescription, @NonNull String commonTags, @Nullable UploadVisibility visibility, - @Nullable UploadGpxListener listener) { + @Nullable UploadGpxListener listener, + @Nullable String defaultActivity) { app = (OsmandApplication) activity.getApplication(); this.gpxDbHelper = app.getGpxDbHelper(); this.remoteUtil = new OpenstreetmapRemoteUtil(app); @@ -47,20 +50,17 @@ public UploadGPXFilesTask(@NonNull Activity activity, this.commonTags = commonTags; this.visibility = visibility != null ? visibility.asUrlParam() : UploadVisibility.PRIVATE.asUrlParam(); this.listener = listener; + this.defaultActivity = defaultActivity; } @Override protected String doInBackground(File... params) { int count = 0; int total = 0; - String prevActivity = DEFAULT_ACTIVITY_TAG; for (File file : params) { if (!isCancelled() && file != null) { String activity = getGpxActivity(file); - if (!Algorithms.isEmpty(activity)) { - prevActivity = activity; - } - String tags = getGpxTags(prevActivity); + String tags = getGpxTags(activity); String description = getGpxDescription(file); String warning = remoteUtil.uploadGPXFile(tags, description, visibility, file); total++; @@ -82,17 +82,36 @@ private String getGpxDescription(@NonNull File file) { } @NonNull - private String getGpxTags(@NonNull String activity) { - if (!commonTags.contains(activity)) { - if (commonTags.contains(DEFAULT_ACTIVITY_TAG)) { - return commonTags.replace(DEFAULT_ACTIVITY_TAG, activity); - } else { - return commonTags + ", " + DEFAULT_ACTIVITY_TAG; - } + private String getGpxTags(@Nullable String activity) { + if (Algorithms.isEmpty(activity)) { + return removeDefaultActivity(); + } else if (!commonTags.contains(activity)) { + return addActivity(activity); } return commonTags; } + public String removeDefaultActivity() { + String removedDefaultActivity = commonTags; + if (!Algorithms.isEmpty(defaultActivity) && commonTags.contains(defaultActivity)) { + List tagList = Arrays.stream(commonTags.split(",")) + .map(String::trim) + .filter(tag -> !tag.equals(defaultActivity)) + .collect(Collectors.toList()); + + removedDefaultActivity = String.join(", ", tagList); + } + return removedDefaultActivity; + } + + public String addActivity(@Nullable String activity) { + if (!Algorithms.isEmpty(defaultActivity) && commonTags.contains(defaultActivity)) { + return commonTags.replace(defaultActivity, activity); + } else { + return commonTags + ", " + activity; + } + } + @Nullable private String getGpxActivity(@NonNull File file) { GpxDataItem item = gpxDbHelper.getItem(new KFile(file.getPath())); diff --git a/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/SendGpxBottomSheetFragment.java b/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/SendGpxBottomSheetFragment.java index 570b2026718..3d13e313a82 100644 --- a/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/SendGpxBottomSheetFragment.java +++ b/OsmAnd/src/net/osmand/plus/plugins/osmedit/dialogs/SendGpxBottomSheetFragment.java @@ -1,7 +1,6 @@ package net.osmand.plus.plugins.osmedit.dialogs; import static net.osmand.plus.plugins.osmedit.OsmEditingPlugin.OSMAND_TAG; -import static net.osmand.plus.plugins.osmedit.asynctasks.UploadGPXFilesTask.DEFAULT_ACTIVITY_TAG; import static net.osmand.plus.settings.fragments.BaseSettingsFragment.OPEN_SETTINGS; import static net.osmand.plus.settings.fragments.SettingsScreenType.OPEN_STREET_MAP_EDITING; @@ -36,6 +35,10 @@ import net.osmand.plus.utils.UiUtilities; import net.osmand.plus.widgets.chips.ChipItem; import net.osmand.plus.widgets.chips.HorizontalChipsView; +import net.osmand.shared.gpx.GpxDataItem; +import net.osmand.shared.gpx.GpxDbHelper; +import net.osmand.shared.gpx.GpxParameter; +import net.osmand.shared.io.KFile; import net.osmand.util.Algorithms; import java.io.File; @@ -54,6 +57,7 @@ public class SendGpxBottomSheetFragment extends MenuBottomSheetDialogFragment im private TextInputEditText tagsField; private TextInputEditText messageField; + private String firstActivity; @Override public void onCreate(Bundle savedInstanceState) { @@ -92,9 +96,28 @@ private void setupTagsRow(@NonNull View view) { } } + @Nullable + private String getFirstActivity() { + GpxDbHelper gpxDbHelper = requiredMyApplication().getGpxDbHelper(); + for (File file : files) { + GpxDataItem gpxDataItem = gpxDbHelper.getItem(new KFile(file.getPath())); + String activity = gpxDataItem != null ? gpxDataItem.getParameter(GpxParameter.ACTIVITY_TYPE) : null; + + if (!Algorithms.isEmpty(activity)) { + return activity; + } + } + return null; + } + @NonNull private String getDefaultTags() { - return OSMAND_TAG + ", " + DEFAULT_ACTIVITY_TAG; + String defaultTags = OSMAND_TAG; + firstActivity = getFirstActivity(); + if (!Algorithms.isEmpty(firstActivity)) { + return defaultTags + ", " + firstActivity; + } + return defaultTags; } private void setupVisibilityRow(@NonNull View view) { @@ -160,7 +183,7 @@ protected void onRightBottomButtonClick() { String description = descrText != null ? descrText.toString() : ""; String tags = tagsText != null ? tagsText.toString() : ""; - UploadGPXFilesTask task = new UploadGPXFilesTask(activity, description, tags, uploadVisibility, this); + UploadGPXFilesTask task = new UploadGPXFilesTask(activity, description, tags, uploadVisibility, this, firstActivity); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, files); } dismiss();