Run group synchronization in background

This commit is contained in:
PavelRatushny 2017-11-14 14:17:07 +02:00
parent 4efaccc61c
commit 1d11026f56

View file

@ -1,6 +1,7 @@
package net.osmand.plus; package net.osmand.plus;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@ -27,6 +28,8 @@ import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static net.osmand.data.PointDescription.POINT_TYPE_MAP_MARKER; import static net.osmand.data.PointDescription.POINT_TYPE_MAP_MARKER;
@ -41,6 +44,7 @@ public class MapMarkersHelper {
private OsmandApplication ctx; private OsmandApplication ctx;
private MapMarkersDbHelper markersDbHelper; private MapMarkersDbHelper markersDbHelper;
private boolean startFromMyLocation; private boolean startFromMyLocation;
private ExecutorService executorService = Executors.newSingleThreadExecutor();
private MarkersPlanRouteContext planRouteContext; private MarkersPlanRouteContext planRouteContext;
@ -455,50 +459,68 @@ public class MapMarkersHelper {
if (!isGroupSynced(group.getId())) { if (!isGroupSynced(group.getId())) {
return; return;
} }
List<MapMarker> dbMarkers = markersDbHelper.getMarkersFromGroup(group); SyncGroupTask syncGroupTask = new SyncGroupTask(group, enabled);
syncGroupTask.executeOnExecutor(executorService);
}
if (group.getType() == MarkersSyncGroup.FAVORITES_TYPE) { private class SyncGroupTask extends AsyncTask<Void, Void, Void> {
FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName());
if (favGroup == null) {
return;
}
if (!favGroup.visible) {
removeActiveMarkersFromSyncGroup(group.getId());
removeActiveMarkersFromGroup(group.getId());
return;
}
if (group.getColor() == -1) {
group.setColor(favGroup.color);
}
for (FavouritePoint fp : favGroup.points) { private MarkersSyncGroup group;
addNewMarkerIfNeeded(group, dbMarkers, new LatLon(fp.getLatitude(), fp.getLongitude()), fp.getName(), enabled); private boolean enabled;
}
removeOldMarkersIfNeeded(dbMarkers); SyncGroupTask(MarkersSyncGroup group, boolean enabled) {
} else if (group.getType() == MarkersSyncGroup.GPX_TYPE) { this.group = group;
GpxSelectionHelper gpxHelper = ctx.getSelectedGpxHelper(); this.enabled = enabled;
File file = new File(group.getId()); }
if (!file.exists()) {
return;
}
SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(group.getId()); @Override
GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile(); protected Void doInBackground(Void... voids) {
if (gpx == null) { List<MapMarker> dbMarkers = markersDbHelper.getMarkersFromGroup(group);
removeActiveMarkersFromSyncGroup(group.getId());
removeActiveMarkersFromGroup(group.getId());
return;
}
List<WptPt> gpxPoints = new LinkedList<>(gpx.getPoints()); if (group.getType() == MarkersSyncGroup.FAVORITES_TYPE) {
int defColor = ContextCompat.getColor(ctx, R.color.marker_red); FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName());
for (WptPt pt : gpxPoints) { if (favGroup == null) {
group.setColor(pt.getColor(defColor)); return null;
addNewMarkerIfNeeded(group, dbMarkers, new LatLon(pt.lat, pt.lon), pt.name, enabled); }
} if (!favGroup.visible) {
removeActiveMarkersFromSyncGroup(group.getId());
removeActiveMarkersFromGroup(group.getId());
return null;
}
if (group.getColor() == -1) {
group.setColor(favGroup.color);
}
removeOldMarkersIfNeeded(dbMarkers); for (FavouritePoint fp : favGroup.points) {
addNewMarkerIfNeeded(group, dbMarkers, new LatLon(fp.getLatitude(), fp.getLongitude()), fp.getName(), enabled);
}
removeOldMarkersIfNeeded(dbMarkers);
} else if (group.getType() == MarkersSyncGroup.GPX_TYPE) {
GpxSelectionHelper gpxHelper = ctx.getSelectedGpxHelper();
File file = new File(group.getId());
if (!file.exists()) {
return null;
}
SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(group.getId());
GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile();
if (gpx == null) {
removeActiveMarkersFromSyncGroup(group.getId());
removeActiveMarkersFromGroup(group.getId());
return null;
}
List<WptPt> gpxPoints = new LinkedList<>(gpx.getPoints());
int defColor = ContextCompat.getColor(ctx, R.color.marker_red);
for (WptPt pt : gpxPoints) {
group.setColor(pt.getColor(defColor));
addNewMarkerIfNeeded(group, dbMarkers, new LatLon(pt.lat, pt.lon), pt.name, enabled);
}
removeOldMarkersIfNeeded(dbMarkers);
}
return null;
} }
} }