Fix refactoring SelectGpxTask->GpxSelectionHelper

This commit is contained in:
Dima-1 2020-02-19 15:01:52 +02:00
parent 2697095a99
commit 0159bc77fa
2 changed files with 113 additions and 82 deletions

View file

@ -959,4 +959,51 @@ public class GpxSelectionHelper {
return group != null && group.isGeneralTrack(); return group != null && group.isGeneralTrack();
} }
} }
public interface SelectGpxTaskListener {
void onProgressUpdate(GPXFile gpxFile);
void onPreExecute();
void onPostExecute();
}
public static class SelectGpxTask extends AsyncTask<GPXFile, GPXFile, String> {
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();
}
}
} }

View file

@ -69,7 +69,6 @@ import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.TrackActivity; import net.osmand.plus.activities.TrackActivity;
@ -87,7 +86,6 @@ import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
import java.text.Collator; import java.text.Collator;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -97,7 +95,6 @@ import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -120,6 +117,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
private GpxIndexesAdapter allGpxAdapter; private GpxIndexesAdapter allGpxAdapter;
private ContextMenuAdapter optionsMenuAdapter; private ContextMenuAdapter optionsMenuAdapter;
private AsyncTask<GpxInfo, ?, ?> operationTask; private AsyncTask<GpxInfo, ?, ?> operationTask;
private AsyncTask<GPXFile, ?, ?> selectGpxTask;
private GpxSelectionHelper selectedGpxHelper; private GpxSelectionHelper selectedGpxHelper;
private OsmandApplication app; private OsmandApplication app;
private boolean updateEnable; private boolean updateEnable;
@ -129,6 +127,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
private View footerView; private View footerView;
private boolean importing = false; private boolean importing = false;
private View emptyView; private View emptyView;
private GpxSelectionHelper.SelectGpxTaskListener gpxTaskListener;
@Override @Override
public void onAttach(Context activity) { public void onAttach(Context activity) {
@ -204,9 +203,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
super.onPause(); super.onPause();
updateEnable = false; updateEnable = false;
if (operationTask != null) { if (operationTask != null) {
if (!(operationTask instanceof SelectGpxTask)) { operationTask.cancel(true);
operationTask.cancel(true);
}
} }
if (actionMode != null) { if (actionMode != null) {
actionMode.finish(); actionMode.finish();
@ -552,7 +549,9 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
} }
public void showProgressBar() { public void showProgressBar() {
((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); if (getActivity() != null) {
((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true);
}
} }
public void hideProgressBar() { public void hideProgressBar() {
@ -583,8 +582,46 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
showOnMapMode = true; showOnMapMode = true;
selectedItems.clear(); selectedItems.clear();
selectedGroups.clear(); selectedGroups.clear();
final Set<GpxInfo> originalSelectedItems = allGpxAdapter.getSelectedGpx(); final Set<GPXFile> originalSelectedItems;
selectedItems.addAll(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() { actionMode = getActionBarActivity().startSupportActionMode(new ActionMode.Callback() {
@ -607,29 +644,38 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
@Override @Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
runSelection(false); runSelection();
actionMode.finish(); actionMode.finish();
allGpxAdapter.refreshSelected(); allGpxAdapter.refreshSelected();
allGpxAdapter.notifyDataSetChanged(); allGpxAdapter.notifyDataSetChanged();
return true; return true;
} }
private void runSelection(boolean showOnMap) { private void runSelection() {
operationTask = new SelectGpxTask(showOnMap); selectGpxTask = new GpxSelectionHelper.SelectGpxTask(gpxTaskListener);
originalSelectedItems.addAll(selectedItems); for (GpxInfo gpxInfo : selectedItems) {
for (GpxInfo gpxInfo : originalSelectedItems) {
if (!gpxInfo.currentlyRecordingTrack) { if (!gpxInfo.currentlyRecordingTrack) {
final boolean visible = selectedItems.contains(gpxInfo); final boolean visible = selectedItems.contains(gpxInfo);
SelectedGpxFile sf = selectedGpxHelper.getSelectedFileByName(gpxInfo.fileName); SelectedGpxFile sf = selectedGpxHelper.getSelectedFileByName(gpxInfo.getFileName());
if (sf == null) { if (sf == null) {
sf = new SelectedGpxFile(); sf = new SelectedGpxFile();
sf.setGpxFile(new GPXFile(null), app); sf.setGpxFile(new GPXFile(null), app);
} }
sf.getGpxFile().path = gpxInfo.file.getPath(); sf.getGpxFile().path = gpxInfo.file.getPath();
selectedGpxHelper.addRemoveSelected(visible, sf); 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 @Override
@ -897,7 +943,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
((OsmandActionBarActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true); showProgressBar();
listView.setEmptyView(null); listView.setEmptyView(null);
allGpxAdapter.clear(); allGpxAdapter.clear();
} }
@ -929,9 +975,7 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
protected void onPostExecute(List<GpxInfo> result) { protected void onPostExecute(List<GpxInfo> result) {
this.result = result; this.result = result;
allGpxAdapter.refreshSelected(); allGpxAdapter.refreshSelected();
if (getActivity() != null) { hideProgressBar();
((OsmandActionBarActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(false);
}
listView.setEmptyView(emptyView); listView.setEmptyView(emptyView);
if (allGpxAdapter.getGroupCount() > 0 && if (allGpxAdapter.getGroupCount() > 0 &&
allGpxAdapter.isShowingSelection()) { allGpxAdapter.isShowingSelection()) {
@ -1561,76 +1605,16 @@ public class AvailableGPXFragment extends OsmandExpandableListFragment implement
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
if(getActivity()!=null){ showProgressBar();
((FavoritesActivity) getActivity()).setSupportProgressBarIndeterminateVisibility(true);
}
} }
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(String result) {
if(getActivity()!=null) { hideProgressBar();
((FavoritesActivity)getActivity()).setSupportProgressBarIndeterminateVisibility(false);
}
Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
} }
} }
public class SelectGpxTask extends AsyncTask<GpxInfo, GpxInfo, String> {
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 { private class SearchFilter extends Filter {
@Override @Override