Skip to content

Commit

Permalink
Merge branch 'r4.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chumva committed Nov 29, 2024
2 parents 5b14c7c + dc7fbd6 commit 736cff9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<File, String, String> {

public static final String DEFAULT_ACTIVITY_TAG = "road_cycling";

private final OsmandApplication app;
private final GpxDbHelper gpxDbHelper;
private final OpenstreetmapRemoteUtil remoteUtil;
Expand All @@ -32,13 +33,15 @@ public class UploadGPXFilesTask extends AsyncTask<File, String, String> {
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);
Expand All @@ -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++;
Expand All @@ -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<String> 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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 736cff9

Please sign in to comment.