Create ItineraryHelper

This commit is contained in:
Vitaliy 2021-03-15 11:18:02 +02:00
parent eb5d46d441
commit 7218e7d4b5
15 changed files with 198 additions and 162 deletions

View file

@ -39,6 +39,7 @@ import net.osmand.plus.helpers.DayNightHelper;
import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.inapp.InAppPurchaseHelperImpl;
import net.osmand.plus.itinerary.ItineraryHelper;
import net.osmand.plus.liveupdates.LiveUpdatesHelper;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
@ -471,6 +472,7 @@ public class AppInitializer implements IProgress {
app.osmOAuthHelper = startupInit(new OsmOAuthHelper(app), OsmOAuthHelper.class);
app.oprAuthHelper = startupInit(new OprAuthHelper(app), OprAuthHelper.class);
app.onlineRoutingHelper = startupInit(new OnlineRoutingHelper(app), OnlineRoutingHelper.class);
app.itineraryHelper = startupInit(new ItineraryHelper(app), ItineraryHelper.class);
initOpeningHoursParser();
}

View file

@ -272,10 +272,9 @@ public class FavouritesDbHelper {
}
private void runSyncWithMarkers(FavoriteGroup favGroup) {
MapMarkersHelper helper = context.getMapMarkersHelper();
MapMarkersGroup group = helper.getMarkersGroup(favGroup);
MapMarkersGroup group = context.getMapMarkersHelper().getMarkersGroup(favGroup);
if (group != null) {
helper.runSynchronization(group);
context.getItineraryHelper().runSynchronization(group);
}
}

View file

@ -31,7 +31,6 @@ import net.osmand.plus.helpers.GpxUiHelper.GPXInfo;
import net.osmand.plus.helpers.SearchHistoryHelper;
import net.osmand.plus.helpers.enums.MetricsConstants;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.routing.GPXRouteParams.GPXRouteParamsBuilder;
import net.osmand.plus.track.GpxSplitType;
import net.osmand.util.Algorithms;
@ -823,10 +822,9 @@ public class GpxSelectionHelper {
}
private void syncGpxWithMarkers(GPXFile gpxFile) {
MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper();
MapMarkersGroup group = mapMarkersHelper.getMarkersGroup(gpxFile);
MapMarkersGroup group = app.getMapMarkersHelper().getMarkersGroup(gpxFile);
if (group != null) {
mapMarkersHelper.runSynchronization(group);
app.getItineraryHelper().runSynchronization(group);
}
}

View file

@ -24,6 +24,13 @@ import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;
import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.IndexConstants;
@ -59,6 +66,7 @@ import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.helpers.enums.DrivingRegion;
import net.osmand.plus.helpers.enums.MetricsConstants;
import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.itinerary.ItineraryHelper;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.measurementtool.MeasurementEditingContext;
@ -99,12 +107,6 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;
import btools.routingapp.BRouterServiceConnection;
import btools.routingapp.IBRouterService;
@ -166,6 +168,7 @@ public class OsmandApplication extends MultiDexApplication {
OprAuthHelper oprAuthHelper;
MeasurementEditingContext measurementEditingContext;
OnlineRoutingHelper onlineRoutingHelper;
ItineraryHelper itineraryHelper;
private Map<String, Builder> customRoutingConfigs = new ConcurrentHashMap<>();
private File externalStorageDirectory;
@ -467,6 +470,10 @@ public class OsmandApplication extends MultiDexApplication {
return onlineRoutingHelper;
}
public ItineraryHelper getItineraryHelper() {
return itineraryHelper;
}
public TransportRoutingHelper getTransportRoutingHelper() {
return transportRoutingHelper;
}

View file

@ -0,0 +1,152 @@
package net.osmand.plus.itinerary;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.PlatformUtil;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.mapmarkers.MapMarker;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.mapmarkers.MapMarkersHelper.OnGroupSyncedListener;
import org.apache.commons.logging.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ItineraryHelper {
private static final Log LOG = PlatformUtil.getLog(ItineraryHelper.class);
private OsmandApplication app;
private MapMarkersHelper markersHelper;
private ExecutorService executorService = Executors.newSingleThreadExecutor();
private Set<OnGroupSyncedListener> syncListeners = new HashSet<>();
public ItineraryHelper(@NonNull OsmandApplication app) {
this.app = app;
markersHelper = app.getMapMarkersHelper();
}
public void addSyncListener(OnGroupSyncedListener listener) {
syncListeners.add(listener);
}
public void removeSyncListener(OnGroupSyncedListener listener) {
syncListeners.remove(listener);
}
public void runSynchronization(final @NonNull MapMarkersGroup group) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
new SyncGroupTask(group).executeOnExecutor(executorService);
}
});
}
private class SyncGroupTask extends AsyncTask<Void, Void, Void> {
private MapMarkersGroup group;
SyncGroupTask(MapMarkersGroup group) {
this.group = group;
}
@Override
protected void onPreExecute() {
if (!syncListeners.isEmpty()) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
for (OnGroupSyncedListener listener : syncListeners) {
listener.onSyncStarted();
}
}
});
}
}
@Override
protected Void doInBackground(Void... voids) {
runGroupSynchronization();
return null;
}
// TODO extract method from Asynctask to Helper directly
private void runGroupSynchronization() {
List<MapMarker> groupMarkers = new ArrayList<>(group.getMarkers());
if (group.getType() == MapMarkersGroup.FAVORITES_TYPE) {
FavoriteGroup favGroup = app.getFavorites().getGroup(group.getName());
if (favGroup == null) {
return;
}
group.setVisible(favGroup.isVisible());
if (!group.isVisible() || group.isDisabled()) {
markersHelper.removeGroupActiveMarkers(group, true);
return;
}
List<FavouritePoint> points = new ArrayList<>(favGroup.getPoints());
for (FavouritePoint fp : points) {
markersHelper.addNewMarkerIfNeeded(group, groupMarkers, new LatLon(fp.getLatitude(), fp.getLongitude()), fp.getName(), fp, null);
}
} else if (group.getType() == MapMarkersGroup.GPX_TYPE) {
GpxSelectionHelper gpxHelper = app.getSelectedGpxHelper();
File file = new File(group.getId());
if (!file.exists()) {
return;
}
String gpxPath = group.getId();
SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(gpxPath);
GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile();
group.setVisible(gpx != null || group.isVisibleUntilRestart());
if (gpx == null || group.isDisabled()) {
markersHelper.removeGroupActiveMarkers(group, true);
return;
}
boolean addAll = group.getWptCategories() == null || group.getWptCategories().isEmpty();
List<WptPt> gpxPoints = new ArrayList<>(gpx.getPoints());
for (WptPt pt : gpxPoints) {
if (addAll || group.getWptCategories().contains(pt.category)
|| (pt.category == null && group.getWptCategories().contains(""))) {
markersHelper.addNewMarkerIfNeeded(group, groupMarkers, new LatLon(pt.lat, pt.lon), pt.name, null, pt);
}
}
}
markersHelper.removeOldMarkersIfPresent(groupMarkers);
}
@Override
protected void onPostExecute(Void aVoid) {
if (!syncListeners.isEmpty()) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
for (OnGroupSyncedListener listener : syncListeners) {
listener.onSyncDone();
}
}
});
}
}
}
}

View file

@ -17,8 +17,6 @@ import net.osmand.GPXUtilities.WptPt;
import net.osmand.data.LatLon;
import net.osmand.data.WptLocationPoint;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
@ -26,6 +24,7 @@ import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.base.PointImageDrawable;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor.OnDismissListener;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.track.SaveGpxAsyncTask;
import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.util.Algorithms;
@ -220,10 +219,9 @@ public class WptPtEditorFragment extends PointEditorFragment {
private void syncGpx(GPXFile gpxFile) {
OsmandApplication app = getMyApplication();
if (app != null) {
MapMarkersHelper helper = app.getMapMarkersHelper();
MapMarkersGroup group = helper.getMarkersGroup(gpxFile);
MapMarkersGroup group = app.getMapMarkersHelper().getMarkersGroup(gpxFile);
if (group != null) {
helper.runSynchronization(group);
app.getItineraryHelper().runSynchronization(group);
}
}
}

View file

@ -20,8 +20,6 @@ import net.osmand.data.FavouritePoint.BackgroundType;
import net.osmand.data.LatLon;
import net.osmand.data.WptLocationPoint;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
@ -30,6 +28,7 @@ import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.base.PointImageDrawable;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor.OnDismissListener;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.track.SaveGpxAsyncTask;
import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.util.Algorithms;
@ -236,10 +235,9 @@ public class WptPtEditorFragmentNew extends PointEditorFragmentNew {
private void syncGpx(GPXFile gpxFile) {
OsmandApplication app = getMyApplication();
if (app != null) {
MapMarkersHelper helper = app.getMapMarkersHelper();
MapMarkersGroup group = helper.getMarkersGroup(gpxFile);
MapMarkersGroup group = app.getMapMarkersHelper().getMarkersGroup(gpxFile);
if (group != null) {
helper.runSynchronization(group);
app.getItineraryHelper().runSynchronization(group);
}
}
}

View file

@ -171,7 +171,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
MapMarkersHelper helper = getMyApplication().getMapMarkersHelper();
MapMarkersGroup group = helper.getMarkersGroup(gpxFile);
if (group != null) {
helper.runSynchronization(group);
getMyApplication().getItineraryHelper().runSynchronization(group);
}
}

View file

@ -218,13 +218,13 @@ public class MapMarkersDialogFragment extends DialogFragment implements OnGroupS
@Override
public void onResume() {
super.onResume();
getMyApplication().getMapMarkersHelper().addSyncListener(this);
getMyApplication().getItineraryHelper().addSyncListener(this);
}
@Override
public void onPause() {
super.onPause();
getMyApplication().getMapMarkersHelper().removeSyncListener(this);
getMyApplication().getItineraryHelper().removeSyncListener(this);
}
@Override

View file

@ -1,7 +1,5 @@
package net.osmand.plus.mapmarkers;
import android.os.AsyncTask;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -20,7 +18,6 @@ import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.GeocodingLookupService;
import net.osmand.plus.GeocodingLookupService.AddressLookupRequest;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -41,15 +38,12 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static net.osmand.GPXUtilities.GPX_TIME_FORMAT;
import static net.osmand.data.PointDescription.POINT_TYPE_MAP_MARKER;
@ -78,14 +72,11 @@ public class MapMarkersHelper {
private OsmandApplication ctx;
private MapMarkersDbHelper markersDbHelper;
private ExecutorService executorService = Executors.newSingleThreadExecutor();
private List<MapMarker> mapMarkers = new ArrayList<>();
private List<MapMarker> mapMarkersHistory = new ArrayList<>();
private List<MapMarkersGroup> mapMarkersGroups = new ArrayList<>();
private List<MapMarkerChangedListener> listeners = new ArrayList<>();
private Set<OnGroupSyncedListener> syncListeners = new HashSet<>();
private MarkersPlanRouteContext planRouteContext;
@ -186,7 +177,7 @@ public class MapMarkersHelper {
public void syncAllGroupsAsync() {
for (MapMarkersGroup gr : mapMarkersGroups) {
if (gr.getId() != null && gr.getName() != null) {
runSynchronization(gr);
ctx.getItineraryHelper().runSynchronization(gr);
}
}
}
@ -303,15 +294,6 @@ public class MapMarkersHelper {
});
}
public void runSynchronization(final @NonNull MapMarkersGroup group) {
ctx.runInUIThread(new Runnable() {
@Override
public void run() {
new SyncGroupTask(group).executeOnExecutor(executorService);
}
});
}
public MapMarkersGroup getMarkersGroup(GPXFile gpx) {
if (gpx == null || gpx.path == null) {
return null;
@ -363,7 +345,7 @@ public class MapMarkersHelper {
if (gr.isDisabled()) {
updateGroupDisabled(gr, false);
}
runSynchronization(gr);
ctx.getItineraryHelper().runSynchronization(gr);
}
private void addGroupInternally(MapMarkersGroup gr) {
@ -415,7 +397,7 @@ public class MapMarkersHelper {
}
}
private void removeGroupActiveMarkers(MapMarkersGroup group, boolean updateGroup) {
public void removeGroupActiveMarkers(MapMarkersGroup group, boolean updateGroup) {
if (group != null) {
markersDbHelper.removeActiveMarkersFromGroup(group.getId());
removeFromMapMarkersList(group.getActiveMarkers());
@ -636,7 +618,7 @@ public class MapMarkersHelper {
return null;
}
private void addNewMarkerIfNeeded(@NonNull MapMarkersGroup group,
public void addNewMarkerIfNeeded(@NonNull MapMarkersGroup group,
@NonNull List<MapMarker> groupMarkers,
@NonNull LatLon latLon,
@NonNull String name,
@ -671,7 +653,7 @@ public class MapMarkersHelper {
}
}
private void removeOldMarkersIfPresent(List<MapMarker> markers) {
public void removeOldMarkersIfPresent(List<MapMarker> markers) {
if (!markers.isEmpty()) {
boolean needRefresh = false;
for (MapMarker marker : markers) {
@ -970,14 +952,6 @@ public class MapMarkersHelper {
}
}
public void addSyncListener(OnGroupSyncedListener listener) {
syncListeners.add(listener);
}
public void removeSyncListener(OnGroupSyncedListener listener) {
syncListeners.remove(listener);
}
public void addListener(MapMarkerChangedListener l) {
if (!listeners.contains(l)) {
listeners.add(l);
@ -1194,93 +1168,4 @@ public class MapMarkersHelper {
void onSyncDone();
}
private class SyncGroupTask extends AsyncTask<Void, Void, Void> {
private MapMarkersGroup group;
SyncGroupTask(MapMarkersGroup group) {
this.group = group;
}
@Override
protected void onPreExecute() {
if (!syncListeners.isEmpty()) {
ctx.runInUIThread(new Runnable() {
@Override
public void run() {
for (OnGroupSyncedListener listener : syncListeners) {
listener.onSyncStarted();
}
}
});
}
}
@Override
protected Void doInBackground(Void... voids) {
runGroupSynchronization();
return null;
}
// TODO extract method from Asynctask to Helper directly
private void runGroupSynchronization() {
List<MapMarker> groupMarkers = new ArrayList<>(group.getMarkers());
if (group.getType() == MapMarkersGroup.FAVORITES_TYPE) {
FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName());
if (favGroup == null) {
return;
}
group.setVisible(favGroup.isVisible());
if (!group.isVisible() || group.isDisabled()) {
removeGroupActiveMarkers(group, true);
return;
}
List<FavouritePoint> points = new ArrayList<>(favGroup.getPoints());
for (FavouritePoint fp : points) {
addNewMarkerIfNeeded(group, groupMarkers, new LatLon(fp.getLatitude(), fp.getLongitude()), fp.getName(), fp, null);
}
} else if (group.getType() == MapMarkersGroup.GPX_TYPE) {
GpxSelectionHelper gpxHelper = ctx.getSelectedGpxHelper();
File file = new File(group.getId());
if (!file.exists()) {
return;
}
String gpxPath = group.getId();
SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(gpxPath);
GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile();
group.setVisible(gpx != null || group.isVisibleUntilRestart());
if (gpx == null || group.isDisabled()) {
removeGroupActiveMarkers(group, true);
return;
}
boolean addAll = group.getWptCategories() == null || group.getWptCategories().isEmpty();
List<WptPt> gpxPoints = new ArrayList<>(gpx.getPoints());
for (WptPt pt : gpxPoints) {
if (addAll || group.getWptCategories().contains(pt.category)
|| (pt.category == null && group.getWptCategories().contains(""))) {
addNewMarkerIfNeeded(group, groupMarkers, new LatLon(pt.lat, pt.lon), pt.name, null, pt);
}
}
}
removeOldMarkersIfPresent(groupMarkers);
}
@Override
protected void onPostExecute(Void aVoid) {
if (!syncListeners.isEmpty()) {
ctx.runInUIThread(new Runnable() {
@Override
public void run() {
for (OnGroupSyncedListener listener : syncListeners) {
listener.onSyncDone();
}
}
});
}
}
}
}

View file

@ -147,7 +147,7 @@ public class SelectWptCategoriesBottomSheetDialogFragment extends MenuBottomShee
group = mapMarkersHelper.addOrEnableGroup(gpxFile);
}
mapMarkersHelper.updateGroupWptCategories(group, selectedCategories);
mapMarkersHelper.runSynchronization(group);
app.getItineraryHelper().runSynchronization(group);
}
private boolean isAllChecked() {

View file

@ -478,7 +478,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
if(!disabled) {
mapMarkersHelper.enableGroup(group);
} else {
mapMarkersHelper.runSynchronization(group);
app.getItineraryHelper().runSynchronization(group);
}
if (disabled) {

View file

@ -9,7 +9,6 @@ import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import java.io.File;
import java.lang.ref.WeakReference;
@ -65,10 +64,9 @@ public class DeletePointsTask extends AsyncTask<Void, Void, Void> {
}
private void syncGpx(GPXFile gpxFile) {
MapMarkersHelper helper = app.getMapMarkersHelper();
MapMarkersGroup group = helper.getMarkersGroup(gpxFile);
MapMarkersGroup group = app.getMapMarkersHelper().getMarkersGroup(gpxFile);
if (group != null) {
helper.runSynchronization(group);
app.getItineraryHelper().runSynchronization(group);
}
}

View file

@ -236,7 +236,7 @@ public class EditTrackGroupDialogFragment extends MenuBottomSheetDialogFragment
} else {
mapMarkersHelper.updateGroupWptCategories(markersGroup, selectedCategories);
if (!groupCreated) {
mapMarkersHelper.runSynchronization(markersGroup);
app.getItineraryHelper().runSynchronization(markersGroup);
}
}
}
@ -524,10 +524,9 @@ public class EditTrackGroupDialogFragment extends MenuBottomSheetDialogFragment
}
private void syncGpx(GPXFile gpxFile) {
MapMarkersHelper markersHelper = app.getMapMarkersHelper();
MapMarkersGroup group = markersHelper.getMarkersGroup(gpxFile);
MapMarkersGroup group = app.getMapMarkersHelper().getMarkersGroup(gpxFile);
if (group != null) {
markersHelper.runSynchronization(group);
app.getItineraryHelper().runSynchronization(group);
}
}
}

View file

@ -1079,7 +1079,7 @@ public class GPXLayer extends OsmandMapLayer implements IContextMenuProvider, IM
private void syncGpx(GPXFile gpxFile) {
MapMarkersGroup group = view.getApplication().getMapMarkersHelper().getMarkersGroup(gpxFile);
if (group != null) {
mapMarkersHelper.runSynchronization(group);
view.getApplication().getItineraryHelper().runSynchronization(group);
}
}
}