From 0159bc77fa95313615435337965d6e4ee795e419 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Wed, 19 Feb 2020 15:01:52 +0200 Subject: [PATCH] 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