From 41df9fff9ef87939d3efc87ce679933bd47c40f3 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Tue, 18 Feb 2020 17:18:45 +0200 Subject: [PATCH 1/9] Fix #7763 selected gpx file status --- .../net/osmand/plus/GpxSelectionHelper.java | 71 +++++++++++++++---- .../plus/myplaces/AvailableGPXFragment.java | 37 ++++++++-- 2 files changed, 89 insertions(+), 19 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 00a155c20f..3bef239a9d 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -440,10 +440,20 @@ public class GpxSelectionHelper { } } + private SelectedGpxFile getSelectedFileByLoadedFileName(String path) { + List newList = new ArrayList<>(selectedGPXFiles); + for (SelectedGpxFile s : newList) { + if (path.endsWith("/" + s.getLoadedName())) { + return s; + } + } + return null; + } + public SelectedGpxFile getSelectedFileByPath(String path) { List newList = new ArrayList<>(selectedGPXFiles); for (SelectedGpxFile s : newList) { - if (s.getGpxFile().path.equals(path)) { + if (s.getGpxFile() != null && s.getGpxFile().path.equals(path)) { return s; } } @@ -452,7 +462,10 @@ public class GpxSelectionHelper { public SelectedGpxFile getSelectedFileByName(String path) { for (SelectedGpxFile s : selectedGPXFiles) { - if (s.getGpxFile().path.endsWith("/" + path)) { + if (path.equals(s.getLoadedName())) { + return s; + } + if (s.getLoadedName().isEmpty() && s.getGpxFile().path.endsWith("/" + path)) { return s; } } @@ -591,10 +604,18 @@ public class GpxSelectionHelper { } } else { assert gpx != null; - sf = getSelectedFileByPath(gpx.path); + sf = getSelectedFileByLoadedFileName(gpx.path); + if (sf == null) { + sf = getSelectedFileByPath(gpx.path); + } displayed = sf != null; - if (show && sf == null) { - sf = new SelectedGpxFile(); + if (show) { + if (sf == null) { + sf = new SelectedGpxFile(); + List newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); + newSelectedGPXFiles.add(sf); + selectedGPXFiles = newSelectedGPXFiles; + } if (dataItem != null) { if (dataItem.getColor() != 0) { gpx.setColor(dataItem.getColor()); @@ -602,18 +623,23 @@ public class GpxSelectionHelper { sf.setJoinSegments(dataItem.isJoinSegments()); } sf.setGpxFile(gpx, app); + sf.setLoadedName(""); sf.notShowNavigationDialog = notShowNavigationDialog; sf.selectedByUser = selectedByUser; } } - if (displayed != show) { - List newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); - if (show) { - newSelectedGPXFiles.add(sf); - } else { - newSelectedGPXFiles.remove(sf); + if (sf != null && sf.getLoadedName().isEmpty()) { + if (displayed != show) { + List newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); + if (show) { + if (!newSelectedGPXFiles.contains(sf)) { + newSelectedGPXFiles.add(sf); + } + } else { + newSelectedGPXFiles.remove(sf); + } + selectedGPXFiles = newSelectedGPXFiles; } - selectedGPXFiles = newSelectedGPXFiles; } if (syncGroup) { syncGpxWithMarkers(gpx); @@ -624,6 +650,18 @@ public class GpxSelectionHelper { return sf; } + public void addRemoveSelected(boolean show, SelectedGpxFile sf) { + List newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); + if (show) { + if (!newSelectedGPXFiles.contains(sf)) { + newSelectedGPXFiles.add(sf); + } + } else { + newSelectedGPXFiles.remove(sf); + } + selectedGPXFiles = newSelectedGPXFiles; + } + public SelectedGpxFile selectGpxFile(GPXFile gpx, boolean show, boolean notShowNavigationDialog) { return selectGpxFile(gpx, show, notShowNavigationDialog, true, true, true); } @@ -689,6 +727,15 @@ public class GpxSelectionHelper { private boolean joinSegments; private boolean showCurrentTrack; private boolean splitProcessed = false; + private String loadedName = ""; + + public String getLoadedName() { + return loadedName; + } + + public void setLoadedName(String loadedName) { + this.loadedName = loadedName; + } public void setGpxFile(GPXFile gpxFile, OsmandApplication app) { this.gpxFile = gpxFile; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index a5b0653c5b..43faa235ec 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -204,7 +204,9 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement super.onPause(); updateEnable = false; if (operationTask != null) { - operationTask.cancel(true); + if (!(operationTask instanceof SelectGpxTask)) { + operationTask.cancel(true); + } } if (actionMode != null) { actionMode.finish(); @@ -615,6 +617,17 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement private void runSelection(boolean showOnMap) { operationTask = new SelectGpxTask(showOnMap); originalSelectedItems.addAll(selectedItems); + for (GpxInfo gpxInfo : originalSelectedItems) { + if (!gpxInfo.currentlyRecordingTrack) { + final boolean visible = selectedItems.contains(gpxInfo); + SelectedGpxFile sf = selectedGpxHelper.getSelectedFileByName(gpxInfo.fileName); + if (sf == null) { + sf = new SelectedGpxFile(); + } + sf.setLoadedName(gpxInfo.fileName); + selectedGpxHelper.addRemoveSelected(visible, sf); + } + } operationTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, originalSelectedItems.toArray(new GpxInfo[originalSelectedItems.size()])); } @@ -678,7 +691,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(getString(R.string.local_index_action_do, actionButton.toLowerCase(), - selectedItems.size())); + String.valueOf(selectedItems.size()))); builder.setPositiveButton(actionButton, listener); builder.setNegativeButton(R.string.shared_string_cancel, null); builder.show(); @@ -1547,12 +1560,16 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override protected void onPreExecute() { - getActivity().setProgressBarIndeterminateVisibility(true); + if(getActivity()!=null){ + ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); + } } @Override protected void onPostExecute(String result) { - getActivity().setProgressBarIndeterminateVisibility(false); + if(getActivity()!=null) { + ((FavoritesActivity)getActivity()).setSupportProgressBarIndeterminateVisibility(false); + } Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show(); } } @@ -1593,12 +1610,16 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override protected void onPreExecute() { - getActivity().setProgressBarIndeterminateVisibility(true); + if (getActivity() != null) { + ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); + } } @Override protected void onPostExecute(String result) { - getActivity().setProgressBarIndeterminateVisibility(false); + if (getActivity() != null) { + ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(false); + } allGpxAdapter.refreshSelected(); allGpxAdapter.notifyDataSetChanged(); if (showOnMap && toShow != null) { @@ -1863,7 +1884,9 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement SelectedGpxFile sgpx = getSelectedGpxFile(gpxInfo, app); GPXTrackAnalysis analysis = null; if (sgpx != null) { - analysis = sgpx.getTrackAnalysis(app); + if (sgpx.getLoadedName().isEmpty()) { + analysis = sgpx.getTrackAnalysis(app); + } } else if (gpxInfo.currentlyRecordingTrack) { analysis = app.getSavingTrackHelper().getCurrentTrack().getTrackAnalysis(app); } else if (gpxInfo.file != null) { From 2697095a9933f471bf63b76ecfac5ddd8b8e95b7 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Wed, 19 Feb 2020 12:40:44 +0200 Subject: [PATCH 2/9] Fix remarks remove loadingFileName --- .../net/osmand/plus/GpxSelectionHelper.java | 49 +++---------------- .../plus/myplaces/AvailableGPXFragment.java | 5 +- 2 files changed, 11 insertions(+), 43 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 3bef239a9d..31bec44718 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -440,16 +440,6 @@ public class GpxSelectionHelper { } } - private SelectedGpxFile getSelectedFileByLoadedFileName(String path) { - List newList = new ArrayList<>(selectedGPXFiles); - for (SelectedGpxFile s : newList) { - if (path.endsWith("/" + s.getLoadedName())) { - return s; - } - } - return null; - } - public SelectedGpxFile getSelectedFileByPath(String path) { List newList = new ArrayList<>(selectedGPXFiles); for (SelectedGpxFile s : newList) { @@ -462,10 +452,7 @@ public class GpxSelectionHelper { public SelectedGpxFile getSelectedFileByName(String path) { for (SelectedGpxFile s : selectedGPXFiles) { - if (path.equals(s.getLoadedName())) { - return s; - } - if (s.getLoadedName().isEmpty() && s.getGpxFile().path.endsWith("/" + path)) { + if (s.getGpxFile().path.endsWith("/" + path)) { return s; } } @@ -604,17 +591,11 @@ public class GpxSelectionHelper { } } else { assert gpx != null; - sf = getSelectedFileByLoadedFileName(gpx.path); - if (sf == null) { - sf = getSelectedFileByPath(gpx.path); - } + sf = getSelectedFileByPath(gpx.path); displayed = sf != null; if (show) { if (sf == null) { sf = new SelectedGpxFile(); - List newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); - newSelectedGPXFiles.add(sf); - selectedGPXFiles = newSelectedGPXFiles; } if (dataItem != null) { if (dataItem.getColor() != 0) { @@ -623,22 +604,13 @@ public class GpxSelectionHelper { sf.setJoinSegments(dataItem.isJoinSegments()); } sf.setGpxFile(gpx, app); - sf.setLoadedName(""); sf.notShowNavigationDialog = notShowNavigationDialog; sf.selectedByUser = selectedByUser; } } - if (sf != null && sf.getLoadedName().isEmpty()) { + if (sf != null && sf.isLoaded()) { if (displayed != show) { - List newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); - if (show) { - if (!newSelectedGPXFiles.contains(sf)) { - newSelectedGPXFiles.add(sf); - } - } else { - newSelectedGPXFiles.remove(sf); - } - selectedGPXFiles = newSelectedGPXFiles; + addRemoveSelected(show, sf); } } if (syncGroup) { @@ -727,15 +699,6 @@ public class GpxSelectionHelper { private boolean joinSegments; private boolean showCurrentTrack; private boolean splitProcessed = false; - private String loadedName = ""; - - public String getLoadedName() { - return loadedName; - } - - public void setLoadedName(String loadedName) { - this.loadedName = loadedName; - } public void setGpxFile(GPXFile gpxFile, OsmandApplication app) { this.gpxFile = gpxFile; @@ -745,6 +708,10 @@ public class GpxSelectionHelper { processPoints(app); } + public boolean isLoaded() { + return gpxFile.modifiedTime != 0 || gpxFile.showCurrentTrack; + } + public GPXTrackAnalysis getTrackAnalysis(OsmandApplication app) { if (modifiedTime != gpxFile.modifiedTime) { update(app); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 43faa235ec..74afe1ffc7 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -623,8 +623,9 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement SelectedGpxFile sf = selectedGpxHelper.getSelectedFileByName(gpxInfo.fileName); if (sf == null) { sf = new SelectedGpxFile(); + sf.setGpxFile(new GPXFile(null), app); } - sf.setLoadedName(gpxInfo.fileName); + sf.getGpxFile().path = gpxInfo.file.getPath(); selectedGpxHelper.addRemoveSelected(visible, sf); } } @@ -1884,7 +1885,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement SelectedGpxFile sgpx = getSelectedGpxFile(gpxInfo, app); GPXTrackAnalysis analysis = null; if (sgpx != null) { - if (sgpx.getLoadedName().isEmpty()) { + if (sgpx.isLoaded()) { analysis = sgpx.getTrackAnalysis(app); } } else if (gpxInfo.currentlyRecordingTrack) { From 0159bc77fa95313615435337965d6e4ee795e419 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Wed, 19 Feb 2020 15:01:52 +0200 Subject: [PATCH 3/9] Fix refactoring SelectGpxTask->GpxSelectionHelper --- .../net/osmand/plus/GpxSelectionHelper.java | 47 ++++++ .../plus/myplaces/AvailableGPXFragment.java | 148 ++++++++---------- 2 files changed, 113 insertions(+), 82 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 31bec44718..93eaf4fbb9 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -959,4 +959,51 @@ public class GpxSelectionHelper { return group != null && group.isGeneralTrack(); } } + + public interface SelectGpxTaskListener { + + void onProgressUpdate(GPXFile gpxFile); + + void onPreExecute(); + + void onPostExecute(); + + } + + public static class SelectGpxTask extends AsyncTask { + + private SelectGpxTaskListener gpxTaskListener; + + public SelectGpxTask(SelectGpxTaskListener gpxTaskListener) { + this.gpxTaskListener = gpxTaskListener; + } + + @Override + protected String doInBackground(GPXFile... params) { + for (GPXFile file : params) { + if (!file.showCurrentTrack) { + file = GPXUtilities.loadGPXFile(new File(file.path)); + } + publishProgress(file); + } + return ""; + } + + @Override + protected void onProgressUpdate(GPXFile... values) { + gpxTaskListener.onProgressUpdate(values[0]); + + } + + @Override + protected void onPreExecute() { + gpxTaskListener.onPreExecute(); + } + + @Override + protected void onPostExecute(String result) { + gpxTaskListener.onPostExecute(); + + } + } } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 74afe1ffc7..945d0c58fc 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -69,7 +69,6 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.activities.TrackActivity; @@ -87,7 +86,6 @@ import net.osmand.util.Algorithms; import java.io.File; import java.text.Collator; import java.text.DateFormat; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -97,7 +95,6 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; @@ -120,6 +117,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement private GpxIndexesAdapter allGpxAdapter; private ContextMenuAdapter optionsMenuAdapter; private AsyncTask operationTask; + private AsyncTask selectGpxTask; private GpxSelectionHelper selectedGpxHelper; private OsmandApplication app; private boolean updateEnable; @@ -129,6 +127,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement private View footerView; private boolean importing = false; private View emptyView; + private GpxSelectionHelper.SelectGpxTaskListener gpxTaskListener; @Override public void onAttach(Context activity) { @@ -204,9 +203,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement super.onPause(); updateEnable = false; if (operationTask != null) { - if (!(operationTask instanceof SelectGpxTask)) { - operationTask.cancel(true); - } + operationTask.cancel(true); } if (actionMode != null) { actionMode.finish(); @@ -552,7 +549,9 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement } public void showProgressBar() { - ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); + if (getActivity() != null) { + ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); + } } public void hideProgressBar() { @@ -583,8 +582,46 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement showOnMapMode = true; selectedItems.clear(); selectedGroups.clear(); - final Set originalSelectedItems = allGpxAdapter.getSelectedGpx(); - selectedItems.addAll(originalSelectedItems); + final Set originalSelectedItems; + selectedItems.addAll(allGpxAdapter.getSelectedGpx()); + originalSelectedItems = new HashSet<>(); + for (GpxInfo gpxInfo : selectedItems) { + originalSelectedItems.add(gpxInfo.gpx); + } + gpxTaskListener = new GpxSelectionHelper.SelectGpxTaskListener() { + @Override + public void onProgressUpdate(GPXFile gpxFile) { + + final boolean visible = isContains(gpxFile); + selectedGpxHelper.selectGpxFile(gpxFile, visible, false); + allGpxAdapter.notifyDataSetInvalidated(); + } + + boolean isContains(GPXFile gpxFile) { + for (GpxInfo gpxInfo : selectedItems) { + if (gpxInfo.currentlyRecordingTrack && gpxFile.showCurrentTrack) { + return true; + } + if (gpxInfo.gpx.path.equals(gpxFile.path)) { + return true; + } + } + return false; + } + + @Override + public void onPreExecute() { + showProgressBar(); + } + + @Override + public void onPostExecute() { + hideProgressBar(); + allGpxAdapter.refreshSelected(); + allGpxAdapter.notifyDataSetChanged(); + } + }; + actionMode = getActionBarActivity().startSupportActionMode(new ActionMode.Callback() { @@ -607,29 +644,38 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - runSelection(false); + runSelection(); actionMode.finish(); allGpxAdapter.refreshSelected(); allGpxAdapter.notifyDataSetChanged(); return true; } - private void runSelection(boolean showOnMap) { - operationTask = new SelectGpxTask(showOnMap); - originalSelectedItems.addAll(selectedItems); - for (GpxInfo gpxInfo : originalSelectedItems) { + private void runSelection() { + selectGpxTask = new GpxSelectionHelper.SelectGpxTask(gpxTaskListener); + for (GpxInfo gpxInfo : selectedItems) { if (!gpxInfo.currentlyRecordingTrack) { final boolean visible = selectedItems.contains(gpxInfo); - SelectedGpxFile sf = selectedGpxHelper.getSelectedFileByName(gpxInfo.fileName); + SelectedGpxFile sf = selectedGpxHelper.getSelectedFileByName(gpxInfo.getFileName()); if (sf == null) { sf = new SelectedGpxFile(); sf.setGpxFile(new GPXFile(null), app); } sf.getGpxFile().path = gpxInfo.file.getPath(); selectedGpxHelper.addRemoveSelected(visible, sf); + originalSelectedItems.add(sf.getGpxFile()); + } else { + SelectedGpxFile sf = selectedGpxHelper.getSelectedCurrentRecordingTrack(); + if (sf == null) { + sf = new SelectedGpxFile(); + sf.setGpxFile(new GPXFile(null), app); + sf.getGpxFile().showCurrentTrack = true; + } + originalSelectedItems.add(sf.getGpxFile()); } } - operationTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, originalSelectedItems.toArray(new GpxInfo[originalSelectedItems.size()])); + selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, + originalSelectedItems.toArray(new GPXFile[originalSelectedItems.size()])); } @Override @@ -897,7 +943,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override protected void onPreExecute() { - ((OsmandActionBarActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); + showProgressBar(); listView.setEmptyView(null); allGpxAdapter.clear(); } @@ -929,9 +975,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement protected void onPostExecute(List result) { this.result = result; allGpxAdapter.refreshSelected(); - if (getActivity() != null) { - ((OsmandActionBarActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(false); - } + hideProgressBar(); listView.setEmptyView(emptyView); if (allGpxAdapter.getGroupCount() > 0 && allGpxAdapter.isShowingSelection()) { @@ -1561,76 +1605,16 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override protected void onPreExecute() { - if(getActivity()!=null){ - ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); - } + showProgressBar(); } @Override protected void onPostExecute(String result) { - if(getActivity()!=null) { - ((FavoritesActivity)getActivity()).setSupportProgressBarIndeterminateVisibility(false); - } + hideProgressBar(); Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show(); } } - public class SelectGpxTask extends AsyncTask { - - private boolean showOnMap; - private WptPt toShow; - - public SelectGpxTask(boolean showOnMap) { - this.showOnMap = showOnMap; - } - - @Override - protected String doInBackground(GpxInfo... params) { - for (GpxInfo info : params) { - if (!isCancelled()) { - if (!info.currentlyRecordingTrack) { - info.setGpx(GPXUtilities.loadGPXFile(info.file)); - } - publishProgress(info); - } - } - return ""; - } - - @Override - protected void onProgressUpdate(GpxInfo... values) { - for (GpxInfo g : values) { - final boolean visible = selectedItems.contains(g); - selectedGpxHelper.selectGpxFile(g.gpx, visible, false); - if (visible && toShow == null) { - toShow = g.gpx.findPointToShow(); - } - } - allGpxAdapter.notifyDataSetInvalidated(); - } - - @Override - protected void onPreExecute() { - if (getActivity() != null) { - ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); - } - } - - @Override - protected void onPostExecute(String result) { - if (getActivity() != null) { - ((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(false); - } - allGpxAdapter.refreshSelected(); - allGpxAdapter.notifyDataSetChanged(); - if (showOnMap && toShow != null) { - getMyApplication().getSettings().setMapLocationToShow(toShow.lat, toShow.lon, - getMyApplication().getSettings().getLastKnownMapZoom()); - MapActivity.launchMapActivityMoveToTop(getActivity()); - } - } - } - private class SearchFilter extends Filter { @Override From ab8a90fac33d8f33845cb4f7075ed59d908b9ea3 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Wed, 19 Feb 2020 15:07:57 +0200 Subject: [PATCH 4/9] Fix refactoring SelectGpxTask->GpxSelectionHelper --- OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 80213355ca..945d0c58fc 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -202,7 +202,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement public void onPause() { super.onPause(); updateEnable = false; - if (operationTask != null && !(operationTask instanceof SelectGpxTask)) { + if (operationTask != null) { operationTask.cancel(true); } if (actionMode != null) { From 93cfc83a4e7e4a10c698c1ec3a1d920010cb30d5 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Thu, 20 Feb 2020 17:04:53 +0200 Subject: [PATCH 5/9] Refactoring GpxSelectionHelper --- .../net/osmand/plus/GpxSelectionHelper.java | 50 ++++++++++++-- .../plus/myplaces/AvailableGPXFragment.java | 67 +++++-------------- 2 files changed, 58 insertions(+), 59 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 93eaf4fbb9..7fc6617ffb 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -35,9 +35,11 @@ import org.json.JSONObject; import java.io.File; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; public class GpxSelectionHelper { @@ -443,7 +445,7 @@ public class GpxSelectionHelper { public SelectedGpxFile getSelectedFileByPath(String path) { List newList = new ArrayList<>(selectedGPXFiles); for (SelectedGpxFile s : newList) { - if (s.getGpxFile() != null && s.getGpxFile().path.equals(path)) { + if (s.getGpxFile().path.equals(path)) { return s; } } @@ -960,9 +962,42 @@ public class GpxSelectionHelper { } } + public void runSelection(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { + final Set originalSelectedItems = new HashSet<>(); + AsyncTask selectGpxTask = new SelectGpxTask(gpxTaskListener); + for (String filePath : selectedItems.keySet()) { + if (!filePath.equals(CURRENT_TRACK)) { + boolean visible = false; + if (selectedItems.get(filePath) != null) { + visible = selectedItems.get(filePath); + } + SelectedGpxFile sf = getSelectedFileByPath(filePath); + if (sf == null) { + sf = new SelectedGpxFile(); + sf.setGpxFile(new GPXFile(null), app); + } + sf.getGpxFile().path = filePath; + addRemoveSelected(visible, sf); + if (visible) { + originalSelectedItems.add(sf.getGpxFile()); + } + } else { + SelectedGpxFile sf = getSelectedCurrentRecordingTrack(); + if (sf == null) { + sf = new SelectedGpxFile(); + sf.setGpxFile(new GPXFile(null), app); + sf.getGpxFile().showCurrentTrack = true; + } + originalSelectedItems.add(sf.getGpxFile()); + } + } + selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, originalSelectedItems.toArray(new GPXFile[0])); + } + + public interface SelectGpxTaskListener { - void onProgressUpdate(GPXFile gpxFile); + void onProgressUpdate(); void onPreExecute(); @@ -970,11 +1005,11 @@ public class GpxSelectionHelper { } - public static class SelectGpxTask extends AsyncTask { + public class SelectGpxTask extends AsyncTask { private SelectGpxTaskListener gpxTaskListener; - public SelectGpxTask(SelectGpxTaskListener gpxTaskListener) { + SelectGpxTask(SelectGpxTaskListener gpxTaskListener) { this.gpxTaskListener = gpxTaskListener; } @@ -991,8 +1026,10 @@ public class GpxSelectionHelper { @Override protected void onProgressUpdate(GPXFile... values) { - gpxTaskListener.onProgressUpdate(values[0]); - + for (GPXFile gpxFile : values) { + selectGpxFile(gpxFile, true, false); + gpxTaskListener.onProgressUpdate(); + } } @Override @@ -1003,7 +1040,6 @@ public class GpxSelectionHelper { @Override protected void onPostExecute(String result) { gpxTaskListener.onPostExecute(); - } } } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 945d0c58fc..52d9bba95e 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -91,6 +91,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -105,6 +106,7 @@ import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; public class AvailableGPXFragment extends OsmandExpandableListFragment implements FavoritesFragmentStateHolder { + private static final String CURRENT_TRACK = "currentTrack"; public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); public static final int SEARCH_ID = -1; // public static final int ACTION_ID = 0; @@ -117,7 +119,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement private GpxIndexesAdapter allGpxAdapter; private ContextMenuAdapter optionsMenuAdapter; private AsyncTask operationTask; - private AsyncTask selectGpxTask; private GpxSelectionHelper selectedGpxHelper; private OsmandApplication app; private boolean updateEnable; @@ -582,33 +583,14 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement showOnMapMode = true; selectedItems.clear(); selectedGroups.clear(); - final Set originalSelectedItems; - selectedItems.addAll(allGpxAdapter.getSelectedGpx()); - originalSelectedItems = new HashSet<>(); - for (GpxInfo gpxInfo : selectedItems) { - originalSelectedItems.add(gpxInfo.gpx); - } + final Set originalSelectedItems = allGpxAdapter.getSelectedGpx(); + selectedItems.addAll(originalSelectedItems); gpxTaskListener = new GpxSelectionHelper.SelectGpxTaskListener() { @Override - public void onProgressUpdate(GPXFile gpxFile) { - - final boolean visible = isContains(gpxFile); - selectedGpxHelper.selectGpxFile(gpxFile, visible, false); + public void onProgressUpdate() { allGpxAdapter.notifyDataSetInvalidated(); } - boolean isContains(GPXFile gpxFile) { - for (GpxInfo gpxInfo : selectedItems) { - if (gpxInfo.currentlyRecordingTrack && gpxFile.showCurrentTrack) { - return true; - } - if (gpxInfo.gpx.path.equals(gpxFile.path)) { - return true; - } - } - return false; - } - @Override public void onPreExecute() { showProgressBar(); @@ -624,7 +606,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement actionMode = getActionBarActivity().startSupportActionMode(new ActionMode.Callback() { - @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { enableSelectionMode(true); @@ -644,40 +625,22 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - runSelection(); + HashMap selectedItemsFileNames = new HashMap<>(); + originalSelectedItems.addAll(selectedItems); + for (GpxInfo gpxInfo : originalSelectedItems) { + if (gpxInfo.currentlyRecordingTrack) { + selectedItemsFileNames.put(CURRENT_TRACK, true); + } else { + selectedItemsFileNames.put(gpxInfo.file.getAbsolutePath(), selectedItems.contains(gpxInfo)); + } + } + selectedGpxHelper.runSelection(selectedItemsFileNames, gpxTaskListener); actionMode.finish(); allGpxAdapter.refreshSelected(); allGpxAdapter.notifyDataSetChanged(); return true; } - private void runSelection() { - selectGpxTask = new GpxSelectionHelper.SelectGpxTask(gpxTaskListener); - for (GpxInfo gpxInfo : selectedItems) { - if (!gpxInfo.currentlyRecordingTrack) { - final boolean visible = selectedItems.contains(gpxInfo); - SelectedGpxFile sf = selectedGpxHelper.getSelectedFileByName(gpxInfo.getFileName()); - if (sf == null) { - sf = new SelectedGpxFile(); - sf.setGpxFile(new GPXFile(null), app); - } - sf.getGpxFile().path = gpxInfo.file.getPath(); - selectedGpxHelper.addRemoveSelected(visible, sf); - originalSelectedItems.add(sf.getGpxFile()); - } else { - SelectedGpxFile sf = selectedGpxHelper.getSelectedCurrentRecordingTrack(); - if (sf == null) { - sf = new SelectedGpxFile(); - sf.setGpxFile(new GPXFile(null), app); - sf.getGpxFile().showCurrentTrack = true; - } - originalSelectedItems.add(sf.getGpxFile()); - } - } - selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, - originalSelectedItems.toArray(new GPXFile[originalSelectedItems.size()])); - } - @Override public void onDestroyActionMode(ActionMode mode) { showOnMapMode = false; From 1d9d09e3774073b85a714d29ee347924fad7e858 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 21 Feb 2020 13:31:09 +0200 Subject: [PATCH 6/9] GpxSelectionHelper refactoring --- .../net/osmand/plus/GpxSelectionHelper.java | 105 ++++++++++-------- .../plus/myplaces/AvailableGPXFragment.java | 68 ++++++------ 2 files changed, 93 insertions(+), 80 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 7fc6617ffb..1500dde7fa 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -43,7 +43,7 @@ import java.util.Set; public class GpxSelectionHelper { - private static final String CURRENT_TRACK = "currentTrack"; + public static final String CURRENT_TRACK = "currentTrack"; private static final String FILE = "file"; private static final String BACKUP = "backup"; private static final String BACKUPMODIFIEDTIME = "backupTime"; @@ -56,6 +56,7 @@ public class GpxSelectionHelper { private Map selectedGpxFilesBackUp = new java.util.HashMap<>(); private SavingTrackHelper savingTrackHelper; private final static Log LOG = PlatformUtil.getLog(GpxSelectionHelper.class); + SelectGpxTask selectGpxTask; public GpxSelectionHelper(OsmandApplication osmandApplication, SavingTrackHelper trackHelper) { @@ -711,7 +712,7 @@ public class GpxSelectionHelper { } public boolean isLoaded() { - return gpxFile.modifiedTime != 0 || gpxFile.showCurrentTrack; + return gpxFile.modifiedTime != 0; } public GPXTrackAnalysis getTrackAnalysis(OsmandApplication app) { @@ -963,83 +964,91 @@ public class GpxSelectionHelper { } public void runSelection(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { - final Set originalSelectedItems = new HashSet<>(); - AsyncTask selectGpxTask = new SelectGpxTask(gpxTaskListener); - for (String filePath : selectedItems.keySet()) { - if (!filePath.equals(CURRENT_TRACK)) { - boolean visible = false; - if (selectedItems.get(filePath) != null) { - visible = selectedItems.get(filePath); - } - SelectedGpxFile sf = getSelectedFileByPath(filePath); - if (sf == null) { - sf = new SelectedGpxFile(); - sf.setGpxFile(new GPXFile(null), app); - } - sf.getGpxFile().path = filePath; - addRemoveSelected(visible, sf); - if (visible) { - originalSelectedItems.add(sf.getGpxFile()); - } - } else { - SelectedGpxFile sf = getSelectedCurrentRecordingTrack(); - if (sf == null) { - sf = new SelectedGpxFile(); - sf.setGpxFile(new GPXFile(null), app); - sf.getGpxFile().showCurrentTrack = true; - } - originalSelectedItems.add(sf.getGpxFile()); - } + if (selectGpxTask != null && (selectGpxTask.getStatus() == AsyncTask.Status.RUNNING + || selectGpxTask.getStatus() == AsyncTask.Status.PENDING)) { + selectGpxTask.cancel(false); } - selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, originalSelectedItems.toArray(new GPXFile[0])); + selectGpxTask = new SelectGpxTask(selectedItems, gpxTaskListener); + selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - public interface SelectGpxTaskListener { - void onProgressUpdate(); + void gpxSelectionInProgress(); - void onPreExecute(); + void gpxSelectionStarted(); - void onPostExecute(); + void gpxSelectionFinished(); } - public class SelectGpxTask extends AsyncTask { + public class SelectGpxTask extends AsyncTask { + final Set originalSelectedItems = new HashSet<>(); private SelectGpxTaskListener gpxTaskListener; + Map selectedItems; - SelectGpxTask(SelectGpxTaskListener gpxTaskListener) { + SelectGpxTask(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { this.gpxTaskListener = gpxTaskListener; + this.selectedItems = selectedItems; } @Override - protected String doInBackground(GPXFile... params) { - for (GPXFile file : params) { - if (!file.showCurrentTrack) { - file = GPXUtilities.loadGPXFile(new File(file.path)); + protected String doInBackground(Void... params) { + for (GPXFile gpxFile : originalSelectedItems) { + if (isCancelled()) { + break; } - publishProgress(file); + if (!gpxFile.showCurrentTrack) { + gpxFile = GPXUtilities.loadGPXFile(new File(gpxFile.path)); + } + selectGpxFile(gpxFile, true, false); + publishProgress(); } return ""; } @Override - protected void onProgressUpdate(GPXFile... values) { - for (GPXFile gpxFile : values) { - selectGpxFile(gpxFile, true, false); - gpxTaskListener.onProgressUpdate(); - } + protected void onProgressUpdate(Void... values) { + gpxTaskListener.gpxSelectionInProgress(); } @Override protected void onPreExecute() { - gpxTaskListener.onPreExecute(); + collectSelectedItems(); + gpxTaskListener.gpxSelectionStarted(); + } + + private void collectSelectedItems() { + for (String filePath : selectedItems.keySet()) { + SelectedGpxFile sf; + if (!filePath.equals(CURRENT_TRACK)) { + sf = getSelectedFileByPath(filePath); + if (sf == null) { + sf = new SelectedGpxFile(); + sf.setGpxFile(new GPXFile(null), app); + } + sf.getGpxFile().path = filePath; + } else { + sf = getSelectedCurrentRecordingTrack(); + if (sf == null) { + sf = savingTrackHelper.getCurrentTrack(); + } + } + boolean visible = false; + if (selectedItems.get(filePath) != null) { + visible = selectedItems.get(filePath); + } + if (visible) { + originalSelectedItems.add(sf.getGpxFile()); + } + addRemoveSelected(visible, sf); + } } @Override protected void onPostExecute(String result) { - gpxTaskListener.onPostExecute(); + gpxTaskListener.gpxSelectionFinished(); } } } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 52d9bba95e..7468f24e9c 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -100,13 +100,13 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; +import static net.osmand.plus.GpxSelectionHelper.CURRENT_TRACK; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; public class AvailableGPXFragment extends OsmandExpandableListFragment implements FavoritesFragmentStateHolder { - private static final String CURRENT_TRACK = "currentTrack"; public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); public static final int SEARCH_ID = -1; // public static final int ACTION_ID = 0; @@ -130,6 +130,29 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement private View emptyView; private GpxSelectionHelper.SelectGpxTaskListener gpxTaskListener; + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + gpxTaskListener = new GpxSelectionHelper.SelectGpxTaskListener() { + @Override + public void gpxSelectionInProgress() { + allGpxAdapter.notifyDataSetInvalidated(); + } + + @Override + public void gpxSelectionStarted() { + showProgressBar(); + } + + @Override + public void gpxSelectionFinished() { + hideProgressBar(); + allGpxAdapter.refreshSelected(); + allGpxAdapter.notifyDataSetChanged(); + } + }; + } + @Override public void onAttach(Context activity) { super.onAttach(activity); @@ -585,24 +608,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement selectedGroups.clear(); final Set originalSelectedItems = allGpxAdapter.getSelectedGpx(); selectedItems.addAll(originalSelectedItems); - gpxTaskListener = new GpxSelectionHelper.SelectGpxTaskListener() { - @Override - public void onProgressUpdate() { - allGpxAdapter.notifyDataSetInvalidated(); - } - - @Override - public void onPreExecute() { - showProgressBar(); - } - - @Override - public void onPostExecute() { - hideProgressBar(); - allGpxAdapter.refreshSelected(); - allGpxAdapter.notifyDataSetChanged(); - } - }; actionMode = getActionBarActivity().startSupportActionMode(new ActionMode.Callback() { @@ -625,16 +630,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - HashMap selectedItemsFileNames = new HashMap<>(); - originalSelectedItems.addAll(selectedItems); - for (GpxInfo gpxInfo : originalSelectedItems) { - if (gpxInfo.currentlyRecordingTrack) { - selectedItemsFileNames.put(CURRENT_TRACK, true); - } else { - selectedItemsFileNames.put(gpxInfo.file.getAbsolutePath(), selectedItems.contains(gpxInfo)); - } - } - selectedGpxHelper.runSelection(selectedItemsFileNames, gpxTaskListener); + runSelection(originalSelectedItems); actionMode.finish(); allGpxAdapter.refreshSelected(); allGpxAdapter.notifyDataSetChanged(); @@ -653,6 +649,16 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement allGpxAdapter.notifyDataSetChanged(); } + public void runSelection(Set originalSelectedItems) { + HashMap selectedItemsFileNames = new HashMap<>(); + originalSelectedItems.addAll(selectedItems); + for (GpxInfo gpxInfo : originalSelectedItems) { + String path = gpxInfo.currentlyRecordingTrack ? CURRENT_TRACK : gpxInfo.file.getAbsolutePath(); + selectedItemsFileNames.put(path, selectedItems.contains(gpxInfo)); + } + selectedGpxHelper.runSelection(selectedItemsFileNames, gpxTaskListener); + } + public void openSelectionMode(final int actionResId, int darkIcon, int lightIcon, final DialogInterface.OnClickListener listener) { final int actionIconId = !isLightActionBar() ? darkIcon : lightIcon; @@ -1831,10 +1837,8 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement private static GPXTrackAnalysis getGpxTrackAnalysis(GpxInfo gpxInfo, OsmandApplication app, @Nullable final GpxInfoViewCallback callback) { SelectedGpxFile sgpx = getSelectedGpxFile(gpxInfo, app); GPXTrackAnalysis analysis = null; - if (sgpx != null) { - if (sgpx.isLoaded()) { + if (sgpx != null && sgpx.isLoaded()) { analysis = sgpx.getTrackAnalysis(app); - } } else if (gpxInfo.currentlyRecordingTrack) { analysis = app.getSavingTrackHelper().getCurrentTrack().getTrackAnalysis(app); } else if (gpxInfo.file != null) { From 8e872492c77589883ff2b5280b75d5ca5f309c64 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 21 Feb 2020 14:23:56 +0200 Subject: [PATCH 7/9] Refactoring gpxTaskListener --- .../net/osmand/plus/GpxSelectionHelper.java | 32 ++++++++++++------- .../plus/myplaces/AvailableGPXFragment.java | 5 +-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 1500dde7fa..0da3176e26 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -56,8 +56,12 @@ public class GpxSelectionHelper { private Map selectedGpxFilesBackUp = new java.util.HashMap<>(); private SavingTrackHelper savingTrackHelper; private final static Log LOG = PlatformUtil.getLog(GpxSelectionHelper.class); - SelectGpxTask selectGpxTask; + private SelectGpxTask selectGpxTask; + private SelectGpxTaskListener gpxTaskListener; + public void setGpxTaskListener(SelectGpxTaskListener gpxTaskListener) { + this.gpxTaskListener = gpxTaskListener; + } public GpxSelectionHelper(OsmandApplication osmandApplication, SavingTrackHelper trackHelper) { this.app = osmandApplication; @@ -625,7 +629,7 @@ public class GpxSelectionHelper { return sf; } - public void addRemoveSelected(boolean show, SelectedGpxFile sf) { + private void addRemoveSelected(boolean show, SelectedGpxFile sf) { List newSelectedGPXFiles = new ArrayList<>(selectedGPXFiles); if (show) { if (!newSelectedGPXFiles.contains(sf)) { @@ -963,12 +967,12 @@ public class GpxSelectionHelper { } } - public void runSelection(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { + public void runSelection(Map selectedItems) { if (selectGpxTask != null && (selectGpxTask.getStatus() == AsyncTask.Status.RUNNING || selectGpxTask.getStatus() == AsyncTask.Status.PENDING)) { selectGpxTask.cancel(false); } - selectGpxTask = new SelectGpxTask(selectedItems, gpxTaskListener); + selectGpxTask = new SelectGpxTask(selectedItems); selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -984,12 +988,10 @@ public class GpxSelectionHelper { public class SelectGpxTask extends AsyncTask { - final Set originalSelectedItems = new HashSet<>(); - private SelectGpxTaskListener gpxTaskListener; - Map selectedItems; + private Set originalSelectedItems = new HashSet<>(); + private Map selectedItems; - SelectGpxTask(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { - this.gpxTaskListener = gpxTaskListener; + SelectGpxTask(Map selectedItems) { this.selectedItems = selectedItems; } @@ -1010,13 +1012,17 @@ public class GpxSelectionHelper { @Override protected void onProgressUpdate(Void... values) { - gpxTaskListener.gpxSelectionInProgress(); + if (gpxTaskListener != null) { + gpxTaskListener.gpxSelectionInProgress(); + } } @Override protected void onPreExecute() { collectSelectedItems(); - gpxTaskListener.gpxSelectionStarted(); + if (gpxTaskListener != null) { + gpxTaskListener.gpxSelectionStarted(); + } } private void collectSelectedItems() { @@ -1048,7 +1054,9 @@ public class GpxSelectionHelper { @Override protected void onPostExecute(String result) { - gpxTaskListener.gpxSelectionFinished(); + if (gpxTaskListener != null) { + gpxTaskListener.gpxSelectionFinished(); + } } } } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index 7468f24e9c..f64e63d314 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -205,7 +205,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public void onResume() { super.onResume(); - + selectedGpxHelper.setGpxTaskListener(gpxTaskListener); if (!importing) { if (asyncLoader == null || asyncLoader.getResult() == null) { asyncLoader = new LoadGpxTask(); @@ -225,6 +225,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public void onPause() { super.onPause(); + selectedGpxHelper.setGpxTaskListener(null); updateEnable = false; if (operationTask != null) { operationTask.cancel(true); @@ -656,7 +657,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement String path = gpxInfo.currentlyRecordingTrack ? CURRENT_TRACK : gpxInfo.file.getAbsolutePath(); selectedItemsFileNames.put(path, selectedItems.contains(gpxInfo)); } - selectedGpxHelper.runSelection(selectedItemsFileNames, gpxTaskListener); + selectedGpxHelper.runSelection(selectedItemsFileNames); } public void openSelectionMode(final int actionResId, int darkIcon, int lightIcon, From a58c0069a73800b5454f1c16e08130615206c510 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Fri, 21 Feb 2020 19:03:44 +0200 Subject: [PATCH 8/9] Refactoring SelectGpxTaskListener --- .../src/net/osmand/plus/GpxSelectionHelper.java | 17 +++++------------ .../plus/myplaces/AvailableGPXFragment.java | 4 +--- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 0da3176e26..1c2f17ffe2 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -57,11 +57,6 @@ public class GpxSelectionHelper { private SavingTrackHelper savingTrackHelper; private final static Log LOG = PlatformUtil.getLog(GpxSelectionHelper.class); private SelectGpxTask selectGpxTask; - private SelectGpxTaskListener gpxTaskListener; - - public void setGpxTaskListener(SelectGpxTaskListener gpxTaskListener) { - this.gpxTaskListener = gpxTaskListener; - } public GpxSelectionHelper(OsmandApplication osmandApplication, SavingTrackHelper trackHelper) { this.app = osmandApplication; @@ -967,12 +962,12 @@ public class GpxSelectionHelper { } } - public void runSelection(Map selectedItems) { + public void runSelection(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { if (selectGpxTask != null && (selectGpxTask.getStatus() == AsyncTask.Status.RUNNING || selectGpxTask.getStatus() == AsyncTask.Status.PENDING)) { selectGpxTask.cancel(false); } - selectGpxTask = new SelectGpxTask(selectedItems); + selectGpxTask = new SelectGpxTask(selectedItems, gpxTaskListener); selectGpxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -990,9 +985,11 @@ public class GpxSelectionHelper { private Set originalSelectedItems = new HashSet<>(); private Map selectedItems; + private SelectGpxTaskListener gpxTaskListener; - SelectGpxTask(Map selectedItems) { + SelectGpxTask(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { this.selectedItems = selectedItems; + this.gpxTaskListener = gpxTaskListener; } @Override @@ -1012,17 +1009,13 @@ public class GpxSelectionHelper { @Override protected void onProgressUpdate(Void... values) { - if (gpxTaskListener != null) { gpxTaskListener.gpxSelectionInProgress(); - } } @Override protected void onPreExecute() { collectSelectedItems(); - if (gpxTaskListener != null) { gpxTaskListener.gpxSelectionStarted(); - } } private void collectSelectedItems() { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index f64e63d314..94f20115e7 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -205,7 +205,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public void onResume() { super.onResume(); - selectedGpxHelper.setGpxTaskListener(gpxTaskListener); if (!importing) { if (asyncLoader == null || asyncLoader.getResult() == null) { asyncLoader = new LoadGpxTask(); @@ -225,7 +224,6 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement @Override public void onPause() { super.onPause(); - selectedGpxHelper.setGpxTaskListener(null); updateEnable = false; if (operationTask != null) { operationTask.cancel(true); @@ -657,7 +655,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement String path = gpxInfo.currentlyRecordingTrack ? CURRENT_TRACK : gpxInfo.file.getAbsolutePath(); selectedItemsFileNames.put(path, selectedItems.contains(gpxInfo)); } - selectedGpxHelper.runSelection(selectedItemsFileNames); + selectedGpxHelper.runSelection(selectedItemsFileNames, gpxTaskListener); } public void openSelectionMode(final int actionResId, int darkIcon, int lightIcon, From 55363869c1ba7f8b728f6b4ae65e08763a00e8ad Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Mon, 24 Feb 2020 18:14:42 +0200 Subject: [PATCH 9/9] Fix modified time -1 --- OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 1c2f17ffe2..32116d00ca 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -711,7 +711,7 @@ public class GpxSelectionHelper { } public boolean isLoaded() { - return gpxFile.modifiedTime != 0; + return gpxFile.modifiedTime != -1; } public GPXTrackAnalysis getTrackAnalysis(OsmandApplication app) { @@ -963,8 +963,7 @@ public class GpxSelectionHelper { } public void runSelection(Map selectedItems, SelectGpxTaskListener gpxTaskListener) { - if (selectGpxTask != null && (selectGpxTask.getStatus() == AsyncTask.Status.RUNNING - || selectGpxTask.getStatus() == AsyncTask.Status.PENDING)) { + if (selectGpxTask != null && (selectGpxTask.getStatus() == AsyncTask.Status.RUNNING)) { selectGpxTask.cancel(false); } selectGpxTask = new SelectGpxTask(selectedItems, gpxTaskListener); @@ -1039,6 +1038,9 @@ public class GpxSelectionHelper { visible = selectedItems.get(filePath); } if (visible) { + if (!sf.isShowCurrentTrack()) { + sf.getGpxFile().modifiedTime = -1; + } originalSelectedItems.add(sf.getGpxFile()); } addRemoveSelected(visible, sf);