GpxSelectionHelper refactoring
This commit is contained in:
parent
93cfc83a4e
commit
1d9d09e377
2 changed files with 93 additions and 80 deletions
|
@ -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<GPXFile, Long> 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<String, Boolean> selectedItems, SelectGpxTaskListener gpxTaskListener) {
|
||||
final Set<GPXFile> originalSelectedItems = new HashSet<>();
|
||||
AsyncTask<GPXFile, ?, ?> 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<GPXFile, GPXFile, String> {
|
||||
public class SelectGpxTask extends AsyncTask<Void, Void, String> {
|
||||
|
||||
final Set<GPXFile> originalSelectedItems = new HashSet<>();
|
||||
private SelectGpxTaskListener gpxTaskListener;
|
||||
Map<String, Boolean> selectedItems;
|
||||
|
||||
SelectGpxTask(SelectGpxTaskListener gpxTaskListener) {
|
||||
SelectGpxTask(Map<String, Boolean> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<GpxInfo> 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<String, Boolean> 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<GpxInfo> originalSelectedItems) {
|
||||
HashMap<String, Boolean> 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) {
|
||||
|
|
Loading…
Reference in a new issue