From cafbf8a25fe7ff6837d6d6d4175bbe495223f804 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 6 Sep 2017 13:40:02 +0300 Subject: [PATCH] Migrate from settings to db in progress --- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 2 +- .../src/net/osmand/plus/MapMarkersHelper.java | 166 +++++------------- .../controllers/MapMarkerMenuController.java | 4 +- .../plus/mapmarkers/MapMarkersDbHelper.java | 9 +- .../adapters/MapMarkersActiveAdapter.java | 16 +- .../adapters/MapMarkersHistoryAdapter.java | 5 +- .../mapwidgets/MapMarkersWidgetsFactory.java | 4 +- 7 files changed, 58 insertions(+), 148 deletions(-) diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 84f64f21dc..3f734e0e5a 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -797,7 +797,7 @@ public class OsmandAidlApi { List mapMarkers = markersHelper.getMapMarkers(); for (MapMarker m : mapMarkers) { if (m.getOnlyName().equals(marker.getName()) && latLon.equals(new LatLon(m.getLatitude(), m.getLongitude()))) { - markersHelper.removeMapMarker(m); + markersHelper.moveMapMarkerToHistory(m); refreshMap(); return true; } diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index ebf5c04556..9f549dec40 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -6,6 +6,7 @@ import android.support.annotation.Nullable; import net.osmand.data.LatLon; import net.osmand.data.LocationPoint; import net.osmand.data.PointDescription; +import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -19,6 +20,7 @@ public class MapMarkersHelper { private OsmandSettings settings; private List listeners = new ArrayList<>(); private OsmandApplication ctx; + private MapMarkersDbHelper markersDbHelper; private boolean startFromMyLocation; public interface MapMarkerChangedListener { @@ -144,8 +146,9 @@ public class MapMarkersHelper { public MapMarkersHelper(OsmandApplication ctx) { this.ctx = ctx; settings = ctx.getSettings(); + markersDbHelper = ctx.getMapMarkersDbHelper(); startFromMyLocation = settings.ROUTE_MAP_MARKERS_START_MY_LOC.get(); - readFromSettings(); + loadMarkers(); } public boolean isStartFromMyLocation() { @@ -159,55 +162,25 @@ public class MapMarkersHelper { public void lookupAddressAll() { for (MapMarker mapMarker : mapMarkers) { - lookupAddress(mapMarker, false); + lookupAddress(mapMarker); } for (MapMarker mapMarker : mapMarkersHistory) { - lookupAddress(mapMarker, true); + lookupAddress(mapMarker); } } - private void readFromSettings() { + private void loadMarkers() { mapMarkers.clear(); mapMarkersHistory.clear(); - List ips = settings.getMapMarkersPoints(); - List desc = settings.getMapMarkersPointDescriptions(ips.size()); - List colors = settings.getMapMarkersColors(ips.size()); - List selections = settings.getMapMarkersSelections(ips.size()); - List creationDates = settings.getMapMarkersCreationDates(ips.size()); - int colorIndex = 0; - for (int i = 0; i < ips.size(); i++) { - if (colors.size() > i) { - colorIndex = colors.get(i); - } - MapMarker mapMarker = new MapMarker(ips.get(i), - PointDescription.deserializeFromString(desc.get(i), ips.get(i)), colorIndex, - selections.get(i), i); - mapMarker.creationDate = creationDates.get(i); - mapMarkers.add(mapMarker); - } - - ips = settings.getMapMarkersHistoryPoints(); - desc = settings.getMapMarkersHistoryPointDescriptions(ips.size()); - colors = settings.getMapMarkersHistoryColors(ips.size()); - creationDates = settings.getMapMarkersHistoryCreationDates(ips.size()); - for (int i = 0; i < ips.size(); i++) { - if (colors.size() > i) { - colorIndex = colors.get(i); - } - MapMarker mapMarker = new MapMarker(ips.get(i), - PointDescription.deserializeFromString(desc.get(i), ips.get(i)), - colorIndex, false, i); - mapMarker.history = true; - mapMarker.creationDate = creationDates.get(i); - mapMarkersHistory.add(mapMarker); - } + mapMarkers.addAll(markersDbHelper.getActiveMarkers()); + mapMarkersHistory.addAll(markersDbHelper.getMarkersHistory()); if (!ctx.isApplicationInitializing()) { lookupAddressAll(); } } - private void lookupAddress(final MapMarker mapMarker, final boolean history) { + private void lookupAddress(final MapMarker mapMarker) { if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) { cancelPointAddressRequests(mapMarker.point); GeocodingLookupService.AddressLookupRequest lookupRequest = new GeocodingLookupService.AddressLookupRequest(mapMarker.point, new GeocodingLookupService.OnAddressLookupResult() { @@ -218,13 +191,7 @@ public class MapMarkersHelper { } else { mapMarker.pointDescription.setName(address); } - if (history) { - settings.updateMapMarkerHistory(mapMarker.point.getLatitude(), mapMarker.point.getLongitude(), - mapMarker.pointDescription, mapMarker.colorIndex, mapMarker.creationDate); - } else { - settings.updateMapMarker(mapMarker.point.getLatitude(), mapMarker.point.getLongitude(), - mapMarker.pointDescription, mapMarker.colorIndex, mapMarker.selected, mapMarker.creationDate); - } + markersDbHelper.updateMarker(mapMarker); updateMarker(mapMarker); } }, null); @@ -232,15 +199,27 @@ public class MapMarkersHelper { } } - public void removeMapMarker(int index) { - settings.deleteMapMarker(index); - MapMarker mapMarker = mapMarkers.remove(index); - cancelPointAddressRequests(mapMarker.point); - int ind = 0; - for (MapMarker marker : mapMarkers) { - marker.index = ind++; + public void moveMapMarkerToHistory(MapMarker marker) { + if (marker != null) { + marker.history = true; + markersDbHelper.moveMarkerToHistory(marker); + mapMarkers.remove(marker); + mapMarkersHistory.add(marker); + cancelPointAddressRequests(marker.point); + refresh(); + } + } + + public void restoreMarkerFromHistory(MapMarker marker, int position) { + if (marker != null) { + MapMarker next = position >= mapMarkers.size() ? null : mapMarkers.get(position); + marker.history = false; + mapMarkersHistory.remove(marker); + mapMarkers.add(position, marker); + markersDbHelper.restoreMapMarkerFromHistory(marker); + markersDbHelper.changeActiveMarkerPosition(marker, next); + refresh(); } - refresh(); } public List getMapMarkers() { @@ -309,28 +288,19 @@ public class MapMarkersHelper { public void removeActiveMarkers() { cancelAddressRequests(); - for (int i = mapMarkers.size() - 1; i >= 0; i--) { - MapMarker marker = mapMarkers.get(i); - addMapMarkerHistory(marker); - } - settings.clearActiveMapMarkers(); - readFromSettings(); + markersDbHelper.moveAllActiveMarkersToHistory(); + mapMarkers.clear(); + mapMarkersHistory = markersDbHelper.getMarkersHistory(); refresh(); } public void removeMarkersHistory() { cancelAddressRequests(); - settings.clearMapMarkersHistory(); - readFromSettings(); + markersDbHelper.clearAllMarkersHistory(); + mapMarkersHistory.clear(); refresh(); } - public void addMapMarker(MapMarker marker, int index) { - settings.insertMapMarker(marker.getLatitude(), marker.getLongitude(), marker.pointDescription, - marker.colorIndex, marker.selected, marker.creationDate, index); - readFromSettings(); - } - public void addMapMarker(LatLon point, PointDescription historyName) { List points = new ArrayList<>(1); List historyNames = new ArrayList<>(1); @@ -342,13 +312,6 @@ public class MapMarkersHelper { public void addMapMarkers(List points, List historyNames) { if (points.size() > 0) { int colorIndex = -1; - double[] latitudes = new double[points.size()]; - double[] longitudes = new double[points.size()]; - List pointDescriptions = new ArrayList<>(); - int[] colorIndexes = new int[points.size()]; - int[] positions = new int[points.size()]; - boolean[] selections = new boolean[points.size()]; - int[] indexes = new int[points.size()]; for (int i = 0; i < points.size(); i++) { LatLon point = points.get(i); PointDescription historyName = historyNames.get(i); @@ -371,33 +334,18 @@ public class MapMarkersHelper { colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT; } - latitudes[i] = point.getLatitude(); - longitudes[i] = point.getLongitude(); - pointDescriptions.add(pointDescription); - colorIndexes[i] = colorIndex; - positions[i] = -1 - i; - selections[i] = false; - indexes[i] = 0; + MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0); + markersDbHelper.addMarker(marker); } - /* adding map marker to second topbar's row - if (sortedMapMarkers.size() > 0) { - MapMarker firstMarker = sortedMapMarkers.get(0); - settings.updateMapMarker(firstMarker.getLatitude(), firstMarker.getLongitude(), - firstMarker.pointDescription, firstMarker.colorIndex, -points.size(), firstMarker.selected); - } - */ - settings.insertMapMarkers(latitudes, longitudes, pointDescriptions, colorIndexes, positions, - selections, indexes); - readFromSettings(); + loadMarkers(); } } public void updateMapMarker(MapMarker marker, boolean refresh) { if (marker != null) { - settings.updateMapMarker(marker.getLatitude(), marker.getLongitude(), - marker.pointDescription, marker.colorIndex, marker.selected, marker.creationDate); + markersDbHelper.updateMarker(marker); if (refresh) { - readFromSettings(); + loadMarkers(); refresh(); } } @@ -405,35 +353,9 @@ public class MapMarkersHelper { public void moveMapMarker(@Nullable MapMarker marker, LatLon latLon) { if (marker != null) { - settings.moveMapMarker(new LatLon(marker.getLatitude(), marker.getLongitude()), latLon, - marker.pointDescription, marker.colorIndex, marker.selected, marker.creationDate); marker.point = new LatLon(latLon.getLatitude(), latLon.getLongitude()); - readFromSettings(); - refresh(); - } - } - - public void removeMapMarker(MapMarker marker) { - if (marker != null) { - settings.deleteMapMarker(marker.index); - readFromSettings(); - refresh(); - } - } - - public void addMapMarkerHistory(MapMarker marker) { - if (marker != null) { - settings.insertMapMarkerHistory(marker.getLatitude(), marker.getLongitude(), - marker.pointDescription, marker.colorIndex, marker.creationDate, 0); - readFromSettings(); - refresh(); - } - } - - public void removeMapMarkerHistory(MapMarker marker) { - if (marker != null) { - settings.deleteMapMarkerHistory(marker.index); - readFromSettings(); + markersDbHelper.updateMarker(marker); + loadMarkers(); refresh(); } } @@ -470,7 +392,7 @@ public class MapMarkersHelper { } if (markers != null || markersHistory != null) { - readFromSettings(); + loadMarkers(); refresh(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java index 4e0bf8570f..875b8f0fdb 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapMarkerMenuController.java @@ -10,7 +10,6 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.MapMarkerDialogHelper; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuController; -import net.osmand.plus.mapillary.MapillaryPlugin; import net.osmand.util.Algorithms; public class MapMarkerMenuController extends MenuController { @@ -25,8 +24,7 @@ public class MapMarkerMenuController extends MenuController { leftTitleButtonController = new TitleButtonController() { @Override public void buttonPressed() { - markersHelper.removeMapMarker(getMapMarker().index); - markersHelper.addMapMarkerHistory(getMapMarker()); + markersHelper.moveMapMarkerToHistory(getMapMarker()); getMapActivity().getContextMenu().close(); } }; diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index 7f05f4e6ac..0f06b5c028 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -227,7 +227,7 @@ public class MapMarkersDbHelper { private void buildLinkedList(LongSparseArray markers, List res, MapMarker marker) { if (marker != null) { - res.add(0, marker); + res.add(marker); MapMarker prev = markers.get(marker.id); if (prev != null) { buildLinkedList(markers, res, prev); @@ -271,7 +271,7 @@ public class MapMarkersDbHelper { } } - public void moveActiveMarkerToHistory(MapMarker marker) { + public void moveMarkerToHistory(MapMarker marker) { SQLiteConnection db = openConnection(false); if (db != null) { try { @@ -281,7 +281,7 @@ public class MapMarkersDbHelper { "WHERE " + MARKERS_COL_NEXT_KEY + " = ?", new Object[]{marker.nextKey, marker.id}); db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + - MARKERS_COL_ACTIVE + " = ? " + + MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_VISITED + " = ? " + "WHERE " + MARKERS_COL_ID + " = ?", new Object[]{0, marker.visitedDate, marker.id}); } finally { @@ -309,11 +309,12 @@ public class MapMarkersDbHelper { SQLiteConnection db = openConnection(false); if (db != null) { try { + List active = getActiveMarkers(); db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_NEXT_KEY + " = ? " + "WHERE " + MARKERS_COL_ID + " = ?", - new Object[]{1, getActiveMarkers().get(0).id, marker.id}); + new Object[]{1, active.size() > 0 ? active.get(0).id : TAIL_NEXT_VALUE, marker.id}); } finally { db.close(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java index 20cfc06e1a..bafa9e7484 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersActiveAdapter.java @@ -68,7 +68,7 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter index) { - MapMarker marker = helper.getMapMarkers().get(index); - helper.removeMapMarker(marker.index); - helper.addMapMarkerHistory(marker); + helper.moveMapMarkerToHistory(helper.getMapMarkers().get(index)); } }