Add MarkersDbHelperLegacy

This commit is contained in:
Vitaliy 2021-04-22 19:16:23 +03:00
parent 0256796d23
commit a259064d29
3 changed files with 253 additions and 175 deletions

View file

@ -8,7 +8,6 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection;
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper;
import net.osmand.util.Algorithms;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -16,26 +15,27 @@ import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.Set; import java.util.Set;
public class MapMarkersDbHelper { public class MapMarkersDbHelper {
private static final int DB_VERSION = 13; static final int DB_VERSION = 13;
public static final String DB_NAME = "map_markers_db"; public static final String DB_NAME = "map_markers_db";
private static final String MARKERS_TABLE_NAME = "map_markers"; protected static final String MARKERS_TABLE_NAME = "map_markers";
private static final String MARKERS_COL_ID = "marker_id"; private static final String MARKERS_COL_ID = "marker_id";
private static final String MARKERS_COL_LAT = "marker_lat"; private static final String MARKERS_COL_LAT = "marker_lat";
private static final String MARKERS_COL_LON = "marker_lon"; private static final String MARKERS_COL_LON = "marker_lon";
private static final String MARKERS_COL_DESCRIPTION = "marker_description"; private static final String MARKERS_COL_DESCRIPTION = "marker_description";
private static final String MARKERS_COL_ACTIVE = "marker_active"; protected static final String MARKERS_COL_ACTIVE = "marker_active";
private static final String MARKERS_COL_ADDED = "marker_added"; private static final String MARKERS_COL_ADDED = "marker_added";
private static final String MARKERS_COL_VISITED = "marker_visited"; private static final String MARKERS_COL_VISITED = "marker_visited";
private static final String MARKERS_COL_GROUP_NAME = "group_name"; private static final String MARKERS_COL_GROUP_NAME = "group_name";
private static final String MARKERS_COL_GROUP_KEY = "group_key"; protected static final String MARKERS_COL_GROUP_KEY = "group_key";
private static final String MARKERS_COL_COLOR = "marker_color"; private static final String MARKERS_COL_COLOR = "marker_color";
private static final String MARKERS_COL_NEXT_KEY = "marker_next_key"; private static final String MARKERS_COL_NEXT_KEY = "marker_next_key";
private static final String MARKERS_COL_DISABLED = "marker_disabled"; protected static final String MARKERS_COL_DISABLED = "marker_disabled";
private static final String MARKERS_COL_SELECTED = "marker_selected"; private static final String MARKERS_COL_SELECTED = "marker_selected";
private static final String MARKERS_COL_MAP_OBJECT_NAME = "marker_map_object_name"; private static final String MARKERS_COL_MAP_OBJECT_NAME = "marker_map_object_name";
@ -73,54 +73,40 @@ public class MapMarkersDbHelper {
MARKERS_COL_MAP_OBJECT_NAME + MARKERS_COL_MAP_OBJECT_NAME +
" FROM " + MARKERS_TABLE_NAME; " FROM " + MARKERS_TABLE_NAME;
private static final String GROUPS_TABLE_NAME = "map_markers_groups";
private static final String GROUPS_COL_ID = "group_id";
private static final String GROUPS_COL_NAME = "group_name";
private static final String GROUPS_COL_TYPE = "group_type";
private static final String GROUPS_COL_DISABLED = "group_disabled";
private static final String GROUPS_COL_CATEGORIES = "group_categories";
private static final String GROUPS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
GROUPS_TABLE_NAME + " (" +
GROUPS_COL_ID + " TEXT PRIMARY KEY, " +
GROUPS_COL_NAME + " TEXT, " +
GROUPS_COL_TYPE + " int, " +
GROUPS_COL_DISABLED + " int, " + // 1 = true, 0 = false
GROUPS_COL_CATEGORIES + " TEXT);";
private static final String GROUPS_TABLE_SELECT = "SELECT " +
GROUPS_COL_ID + ", " +
GROUPS_COL_NAME + ", " +
GROUPS_COL_TYPE + ", " +
GROUPS_COL_DISABLED + ", " +
GROUPS_COL_CATEGORIES +
" FROM " + GROUPS_TABLE_NAME;
public static final String TAIL_NEXT_VALUE = "tail_next"; public static final String TAIL_NEXT_VALUE = "tail_next";
public static final String HISTORY_NEXT_VALUE = "history_next"; public static final String HISTORY_NEXT_VALUE = "history_next";
private final OsmandApplication context; private final OsmandApplication app;
private final MarkersDbHelperLegacy helperLegacy;
public MapMarkersDbHelper(OsmandApplication context) { public MapMarkersDbHelper(OsmandApplication app) {
this.context = context; this.app = app;
this.helperLegacy = new MarkersDbHelperLegacy(app, this);
} }
private SQLiteConnection openConnection(boolean readonly) { public MarkersDbHelperLegacy getHelperLegacy() {
SQLiteConnection conn = context.getSQLiteAPI().getOrCreateDatabase(DB_NAME, readonly); return helperLegacy;
}
protected SQLiteConnection openConnection(boolean readonly) {
SQLiteConnection conn = app.getSQLiteAPI().getOrCreateDatabase(DB_NAME, readonly);
if (conn == null) { if (conn == null) {
return null; return null;
} }
if (conn.getVersion() < DB_VERSION) { if (conn.getVersion() < DB_VERSION) {
if (readonly) { if (readonly) {
conn.close(); conn.close();
conn = context.getSQLiteAPI().getOrCreateDatabase(DB_NAME, false); conn = app.getSQLiteAPI().getOrCreateDatabase(DB_NAME, false);
} }
int version = conn.getVersion(); if (conn != null) {
conn.setVersion(DB_VERSION); int version = conn.getVersion();
if (version == 0) { conn.setVersion(DB_VERSION);
onCreate(conn); if (version == 0) {
} else { onCreate(conn);
onUpgrade(conn, version, DB_VERSION); } else {
onUpgrade(conn, version, DB_VERSION);
}
} }
} }
return conn; return conn;
@ -128,18 +114,15 @@ public class MapMarkersDbHelper {
private void onCreate(SQLiteConnection db) { private void onCreate(SQLiteConnection db) {
db.execSQL(MARKERS_TABLE_CREATE); db.execSQL(MARKERS_TABLE_CREATE);
db.execSQL(GROUPS_TABLE_CREATE); helperLegacy.onCreate(db);
} }
private void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) { private void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) {
helperLegacy.onUpgrade(db, oldVersion, newVersion);
if (oldVersion < 8) { if (oldVersion < 8) {
db.execSQL("ALTER TABLE " + MARKERS_TABLE_NAME + " ADD " + MARKERS_COL_DISABLED + " int"); db.execSQL("ALTER TABLE " + MARKERS_TABLE_NAME + " ADD " + MARKERS_COL_DISABLED + " int");
db.execSQL("ALTER TABLE " + GROUPS_TABLE_NAME + " ADD " + GROUPS_COL_DISABLED + " int");
} }
if (oldVersion < 9) { if (oldVersion < 9) {
db.execSQL("UPDATE " + GROUPS_TABLE_NAME +
" SET " + GROUPS_COL_DISABLED + " = ? " +
"WHERE " + GROUPS_COL_DISABLED + " IS NULL", new Object[]{0});
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + db.execSQL("UPDATE " + MARKERS_TABLE_NAME +
" SET " + MARKERS_COL_DISABLED + " = ? " + " SET " + MARKERS_COL_DISABLED + " = ? " +
"WHERE " + MARKERS_COL_DISABLED + " IS NULL", new Object[]{0}); "WHERE " + MARKERS_COL_DISABLED + " IS NULL", new Object[]{0});
@ -155,68 +138,6 @@ public class MapMarkersDbHelper {
if (oldVersion < 12) { if (oldVersion < 12) {
db.execSQL("ALTER TABLE " + MARKERS_TABLE_NAME + " ADD " + MARKERS_COL_MAP_OBJECT_NAME + " TEXT"); db.execSQL("ALTER TABLE " + MARKERS_TABLE_NAME + " ADD " + MARKERS_COL_MAP_OBJECT_NAME + " TEXT");
} }
if (oldVersion < 13) {
db.execSQL("ALTER TABLE " + GROUPS_TABLE_NAME + " ADD " + GROUPS_COL_CATEGORIES + " TEXT");
}
}
public void addGroup(MapMarkersGroup group) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("INSERT INTO " + GROUPS_TABLE_NAME + " VALUES (?, ?, ?, ?, ?)",
new Object[]{group.getId(), group.getName(), group.getType().getTypeId(), group.isDisabled(), group.getWptCategoriesString()});
} finally {
db.close();
}
}
}
public Map<String, MapMarkersGroup> getAllGroupsMap() {
Map<String, MapMarkersGroup> res = new LinkedHashMap<>();
SQLiteConnection db = openConnection(true);
if (db != null) {
try {
SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT, null);
if (query != null && query.moveToFirst()) {
do {
MapMarkersGroup group = readGroup(query);
res.put(group.getId(), group);
} while (query.moveToNext());
}
if(query != null) {
query.close();
}
} finally {
db.close();
}
}
return res;
}
private MapMarkersGroup readGroup(SQLiteCursor query) {
String id = query.getString(0);
String name = query.getString(1);
int type = query.getInt(2);
boolean disabled = query.getInt(3) == 1;
String categories = query.getString(4);
MapMarkersGroup res = new MapMarkersGroup(id, name, ItineraryType.findTypeForId(type));
res.setDisabled(disabled);
res.setWptCategories(categories == null ? null : Algorithms.decodeStringSet(categories));
return res;
}
public void removeMarkersGroup(String id) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("DELETE FROM " + GROUPS_TABLE_NAME + " WHERE " + GROUPS_COL_ID + " = ?", new Object[]{id});
} finally {
db.close();
}
}
} }
public void removeActiveMarkersFromGroup(String groupId) { public void removeActiveMarkersFromGroup(String groupId) {
@ -226,49 +147,7 @@ public class MapMarkersDbHelper {
db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME + db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME +
" WHERE " + MARKERS_COL_GROUP_KEY + " = ?" + " WHERE " + MARKERS_COL_GROUP_KEY + " = ?" +
" AND " + MARKERS_COL_ACTIVE + " = ?", " AND " + MARKERS_COL_ACTIVE + " = ?",
new Object[]{groupId, 1}); new Object[] {groupId, 1});
} finally {
db.close();
}
}
}
public void updateGroupDisabled(String id, boolean disabled) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("UPDATE " + GROUPS_TABLE_NAME +
" SET " + GROUPS_COL_DISABLED + " = ? " +
"WHERE " + GROUPS_COL_ID + " = ?", new Object[]{disabled ? 1 : 0, id});
db.execSQL("UPDATE " + MARKERS_TABLE_NAME +
" SET " + MARKERS_COL_DISABLED + " = ? " +
"WHERE " + MARKERS_COL_GROUP_KEY + " = ?", new Object[]{disabled ? 1 : 0, id});
} finally {
db.close();
}
}
}
public void updateGroupCategories(String id, String categories) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("UPDATE " + GROUPS_TABLE_NAME +
" SET " + GROUPS_COL_CATEGORIES + " = ? " +
"WHERE " + GROUPS_COL_ID + " = ?", new Object[]{categories, id});
} finally {
db.close();
}
}
}
public void removeDisabledGroups() {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("DELETE FROM " + GROUPS_TABLE_NAME + " WHERE " + GROUPS_COL_DISABLED + " = ? ", new Object[]{1});
db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME
+ " WHERE " + MARKERS_COL_DISABLED + " = ? AND " + MARKERS_COL_ACTIVE + " = ?", new Object[]{1, 1});
} finally { } finally {
db.close(); db.close();
} }
@ -280,7 +159,9 @@ public class MapMarkersDbHelper {
if (db != null) { if (db != null) {
try { try {
for (MapMarker marker : markers) { for (MapMarker marker : markers) {
insertLast(db, marker); if (marker.groupKey == null) {
insertLast(db, marker);
}
} }
} finally { } finally {
db.close(); db.close();
@ -302,15 +183,15 @@ public class MapMarkersDbHelper {
private void insertLast(SQLiteConnection db, MapMarker marker) { private void insertLast(SQLiteConnection db, MapMarker marker) {
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
if (marker.id == null) { if (marker.id == null) {
marker.id = MapMarkersHelper.getMarkerId(context, marker); marker.id = String.valueOf(currentTime) + String.valueOf(new Random().nextInt(900) + 100);
} }
marker.creationDate = currentTime; marker.creationDate = currentTime;
String descr = PointDescription.serializeToString(marker.getOriginalPointDescription()); String descr = PointDescription.serializeToString(marker.getOriginalPointDescription());
int active = marker.history ? 0 : 1; int active = marker.history ? 0 : 1;
PointDescription pointDescription = marker.getOriginalPointDescription(); PointDescription pointDescription = marker.getOriginalPointDescription();
if (pointDescription != null && !pointDescription.isSearchingAddress(context)) { if (pointDescription != null && !pointDescription.isSearchingAddress(app)) {
SearchHistoryHelper.getInstance(context) SearchHistoryHelper.getInstance(app)
.addNewItemToHistory(marker.getLatitude(), marker.getLongitude(), pointDescription); .addNewItemToHistory(marker.getLatitude(), marker.getLongitude(), pointDescription);
} }
@ -342,13 +223,18 @@ public class MapMarkersDbHelper {
@Nullable @Nullable
public MapMarker getMarker(String id) { public MapMarker getMarker(String id) {
return getMarker(id, false);
}
@Nullable
public MapMarker getMarker(String id, boolean legacy) {
MapMarker res = null; MapMarker res = null;
SQLiteConnection db = openConnection(true); SQLiteConnection db = openConnection(true);
if (db != null) { if (db != null) {
try { try {
SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ID + " = ?", new String[]{id}); SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ID + " = ?", new String[]{id});
if (query != null && query.moveToFirst()) { if (query != null && query.moveToFirst()) {
res = readItem(query); res = readItem(query, legacy);
} }
if(query != null) { if(query != null) {
query.close(); query.close();
@ -361,6 +247,10 @@ public class MapMarkersDbHelper {
} }
public List<MapMarker> getActiveMarkers() { public List<MapMarker> getActiveMarkers() {
return getActiveMarkers(false);
}
public List<MapMarker> getActiveMarkers(boolean legacy) {
Map<String, MapMarker> markers = new LinkedHashMap<>(); Map<String, MapMarker> markers = new LinkedHashMap<>();
Set<String> nextKeys = new HashSet<>(); Set<String> nextKeys = new HashSet<>();
SQLiteConnection db = openConnection(true); SQLiteConnection db = openConnection(true);
@ -370,9 +260,11 @@ public class MapMarkersDbHelper {
new String[]{String.valueOf(1)}); new String[]{String.valueOf(1)});
if (query != null && query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
MapMarker marker = readItem(query); MapMarker marker = readItem(query, legacy);
markers.put(marker.id, marker); if (marker != null) {
nextKeys.add(marker.nextKey); markers.put(marker.id, marker);
nextKeys.add(marker.nextKey);
}
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) { if(query != null) {
@ -385,7 +277,13 @@ public class MapMarkersDbHelper {
return buildLinkedList(markers, nextKeys); return buildLinkedList(markers, nextKeys);
} }
private MapMarker readItem(SQLiteCursor query) { @Nullable
private MapMarker readItem(SQLiteCursor query, boolean legacy) {
String groupKey = query.getString(8);
if (groupKey != null && !legacy) {
return null;
}
String id = query.getString(0); String id = query.getString(0);
double lat = query.getDouble(1); double lat = query.getDouble(1);
double lon = query.getDouble(2); double lon = query.getDouble(2);
@ -394,7 +292,6 @@ public class MapMarkersDbHelper {
long added = query.getLong(5); long added = query.getLong(5);
long visited = query.getLong(6); long visited = query.getLong(6);
String groupName = query.getString(7); String groupName = query.getString(7);
String groupKey = query.getString(8);
int colorIndex = query.getInt(9); int colorIndex = query.getInt(9);
String nextKey = query.getString(10); String nextKey = query.getString(10);
boolean selected = query.getInt(12) == 1; boolean selected = query.getInt(12) == 1;
@ -523,6 +420,10 @@ public class MapMarkersDbHelper {
} }
public List<MapMarker> getMarkersHistory() { public List<MapMarker> getMarkersHistory() {
return getMarkersHistory(false);
}
public List<MapMarker> getMarkersHistory(boolean legacy) {
List<MapMarker> markers = new ArrayList<>(); List<MapMarker> markers = new ArrayList<>();
SQLiteConnection db = openConnection(true); SQLiteConnection db = openConnection(true);
if (db != null) { if (db != null) {
@ -531,7 +432,10 @@ public class MapMarkersDbHelper {
new String[]{String.valueOf(0)}); new String[]{String.valueOf(0)});
if (query != null && query.moveToFirst()) { if (query != null && query.moveToFirst()) {
do { do {
markers.add(readItem(query)); MapMarker marker = readItem(query, legacy);
if (marker != null) {
markers.add(marker);
}
} while (query.moveToNext()); } while (query.moveToNext());
} }
if(query != null) { if(query != null) {

View file

@ -111,7 +111,7 @@ public class MapMarkersHelper {
saveHelper = new ItineraryDataHelper(app, this); saveHelper = new ItineraryDataHelper(app, this);
markersDbHelper = app.getMapMarkersDbHelper(); markersDbHelper = app.getMapMarkersDbHelper();
planRouteContext = new MarkersPlanRouteContext(app); planRouteContext = new MarkersPlanRouteContext(app);
markersDbHelper.removeDisabledGroups(); markersDbHelper.getHelperLegacy().removeDisabledGroups();
loadMarkers(); loadMarkers();
loadGroups(); loadGroups();
} }
@ -133,7 +133,7 @@ public class MapMarkersHelper {
} }
private void loadGroups() { private void loadGroups() {
Map<String, MapMarkersGroup> groupsMap = markersDbHelper.getAllGroupsMap(); Map<String, MapMarkersGroup> groupsMap = markersDbHelper.getHelperLegacy().getAllGroupsMap();
List<MapMarker> allMarkers = new ArrayList<>(mapMarkers); List<MapMarker> allMarkers = new ArrayList<>(mapMarkers);
allMarkers.addAll(mapMarkersHistory); allMarkers.addAll(mapMarkersHistory);
@ -141,7 +141,7 @@ public class MapMarkersHelper {
while (iterator.hasNext()) { while (iterator.hasNext()) {
MapMarkersGroup group = iterator.next().getValue(); MapMarkersGroup group = iterator.next().getValue();
if (group.getType() == ItineraryType.TRACK && !new File(group.getId()).exists()) { if (group.getType() == ItineraryType.TRACK && !new File(group.getId()).exists()) {
markersDbHelper.removeMarkersGroup(group.getId()); markersDbHelper.getHelperLegacy().removeMarkersGroup(group.getId());
iterator.remove(); iterator.remove();
} }
} }
@ -366,7 +366,7 @@ public class MapMarkersHelper {
} }
private void addGroupInternally(MapMarkersGroup gr) { private void addGroupInternally(MapMarkersGroup gr) {
markersDbHelper.addGroup(gr); markersDbHelper.getHelperLegacy().addGroup(gr);
addHistoryMarkersToGroup(gr); addHistoryMarkersToGroup(gr);
addToGroupsList(gr); addToGroupsList(gr);
} }
@ -390,7 +390,7 @@ public class MapMarkersHelper {
public void removeMarkersGroup(MapMarkersGroup group) { public void removeMarkersGroup(MapMarkersGroup group) {
if (group != null) { if (group != null) {
markersDbHelper.removeMarkersGroup(group.getId()); markersDbHelper.getHelperLegacy().removeMarkersGroup(group.getId());
removeGroupActiveMarkers(group, false); removeGroupActiveMarkers(group, false);
removeFromGroupsList(group); removeFromGroupsList(group);
} }
@ -399,7 +399,7 @@ public class MapMarkersHelper {
public void updateGroupDisabled(@NonNull MapMarkersGroup group, boolean disabled) { public void updateGroupDisabled(@NonNull MapMarkersGroup group, boolean disabled) {
String id = group.getId(); String id = group.getId();
if (id != null) { if (id != null) {
markersDbHelper.updateGroupDisabled(id, disabled); markersDbHelper.getHelperLegacy().updateGroupDisabled(id, disabled);
group.setDisabled(disabled); group.setDisabled(disabled);
} }
} }
@ -409,7 +409,7 @@ public class MapMarkersHelper {
if (id != null) { if (id != null) {
group.setWptCategories(wptCategories); group.setWptCategories(wptCategories);
if (wptCategories != null) { if (wptCategories != null) {
markersDbHelper.updateGroupCategories(id, group.getWptCategoriesString()); markersDbHelper.getHelperLegacy().updateGroupCategories(id, group.getWptCategoriesString());
} }
} }
} }
@ -642,7 +642,7 @@ public class MapMarkersHelper {
Iterator<MapMarker> iterator = groupMarkers.iterator(); Iterator<MapMarker> iterator = groupMarkers.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
MapMarker marker = iterator.next(); MapMarker marker = iterator.next();
if (marker.id.equals(getMarkerId(app, marker))) { if (marker.id.equals(getMarkerId(app, group, marker))) {
exists = true; exists = true;
marker.favouritePoint = favouritePoint; marker.favouritePoint = favouritePoint;
marker.wptPt = wptPt; marker.wptPt = wptPt;
@ -698,7 +698,9 @@ public class MapMarkersHelper {
public void addMarker(MapMarker marker) { public void addMarker(MapMarker marker) {
if (marker != null) { if (marker != null) {
markersDbHelper.addMarker(marker); if (marker.groupKey == null) {
markersDbHelper.addMarker(marker);
}
if (marker.history) { if (marker.history) {
addToMapMarkersHistoryList(marker); addToMapMarkersHistoryList(marker);
sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC); sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
@ -909,7 +911,7 @@ public class MapMarkersHelper {
MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0); MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0);
if (group != null) { if (group != null) {
marker.id = getMarkerId(app, marker); marker.id = getMarkerId(app, group, marker);
if (markersDbHelper.getMarker(marker.id) != null) { if (markersDbHelper.getMarker(marker.id) != null) {
continue; continue;
} }
@ -921,7 +923,9 @@ public class MapMarkersHelper {
marker.favouritePoint = favouritePoint; marker.favouritePoint = favouritePoint;
marker.wptPt = wptPt; marker.wptPt = wptPt;
marker.mapObjectName = mapObjName; marker.mapObjectName = mapObjName;
markersDbHelper.addMarker(marker); if (marker.groupKey == null) {
markersDbHelper.addMarker(marker);
}
addToMapMarkersList(0, marker); addToMapMarkersList(0, marker);
addedMarkers.add(marker); addedMarkers.add(marker);
reorderActiveMarkersIfNeeded(); reorderActiveMarkersIfNeeded();
@ -931,9 +935,8 @@ public class MapMarkersHelper {
} }
} }
public static String getMarkerId(OsmandApplication app, MapMarker marker) { public static String getMarkerId(OsmandApplication app, MapMarkersGroup group, MapMarker marker) {
String groupId = marker.groupKey == null ? "" : marker.groupKey; return group.getId() + marker.getName(app) + createShortLinkString(marker.point.getLatitude(), marker.point.getLongitude(), 15);
return groupId + marker.getName(app) + createShortLinkString(marker.point.getLatitude(), marker.point.getLongitude(), 15);
} }
public void updateMapMarker(MapMarker marker, boolean refresh) { public void updateMapMarker(MapMarker marker, boolean refresh) {

View file

@ -0,0 +1,171 @@
package net.osmand.plus.mapmarkers;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.api.SQLiteAPI.SQLiteConnection;
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.util.Algorithms;
import java.util.LinkedHashMap;
import java.util.Map;
import static net.osmand.plus.mapmarkers.MapMarkersDbHelper.MARKERS_COL_ACTIVE;
import static net.osmand.plus.mapmarkers.MapMarkersDbHelper.MARKERS_COL_DISABLED;
import static net.osmand.plus.mapmarkers.MapMarkersDbHelper.MARKERS_COL_GROUP_KEY;
import static net.osmand.plus.mapmarkers.MapMarkersDbHelper.MARKERS_TABLE_NAME;
public class MarkersDbHelperLegacy {
private static final String GROUPS_TABLE_NAME = "map_markers_groups";
private static final String GROUPS_COL_ID = "group_id";
private static final String GROUPS_COL_NAME = "group_name";
private static final String GROUPS_COL_TYPE = "group_type";
private static final String GROUPS_COL_DISABLED = "group_disabled";
private static final String GROUPS_COL_CATEGORIES = "group_categories";
private static final String GROUPS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
GROUPS_TABLE_NAME + " (" +
GROUPS_COL_ID + " TEXT PRIMARY KEY, " +
GROUPS_COL_NAME + " TEXT, " +
GROUPS_COL_TYPE + " int, " +
GROUPS_COL_DISABLED + " int, " + // 1 = true, 0 = false
GROUPS_COL_CATEGORIES + " TEXT);";
private static final String GROUPS_TABLE_SELECT = "SELECT " +
GROUPS_COL_ID + ", " +
GROUPS_COL_NAME + ", " +
GROUPS_COL_TYPE + ", " +
GROUPS_COL_DISABLED + ", " +
GROUPS_COL_CATEGORIES +
" FROM " + GROUPS_TABLE_NAME;
private final OsmandApplication app;
private final MapMarkersDbHelper markersDbHelper;
public MarkersDbHelperLegacy(OsmandApplication app, MapMarkersDbHelper markersDbHelper) {
this.app = app;
this.markersDbHelper = markersDbHelper;
}
public void onCreate(SQLiteConnection db) {
db.execSQL(GROUPS_TABLE_CREATE);
}
public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) {
if (oldVersion < 8) {
db.execSQL("ALTER TABLE " + GROUPS_TABLE_NAME + " ADD " + GROUPS_COL_DISABLED + " int");
}
if (oldVersion < 9) {
db.execSQL("UPDATE " + GROUPS_TABLE_NAME +
" SET " + GROUPS_COL_DISABLED + " = ? " +
"WHERE " + GROUPS_COL_DISABLED + " IS NULL", new Object[] {0});
}
if (oldVersion < 13) {
db.execSQL("ALTER TABLE " + GROUPS_TABLE_NAME + " ADD " + GROUPS_COL_CATEGORIES + " TEXT");
}
}
private SQLiteConnection openConnection(boolean readonly) {
return markersDbHelper.openConnection(readonly);
}
public void updateGroupDisabled(String id, boolean disabled) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("UPDATE " + GROUPS_TABLE_NAME +
" SET " + GROUPS_COL_DISABLED + " = ? " +
"WHERE " + GROUPS_COL_ID + " = ?", new Object[] {disabled ? 1 : 0, id});
db.execSQL("UPDATE " + MARKERS_TABLE_NAME +
" SET " + MARKERS_COL_DISABLED + " = ? " +
"WHERE " + MARKERS_COL_GROUP_KEY + " = ?", new Object[] {disabled ? 1 : 0, id});
} finally {
db.close();
}
}
}
public void removeDisabledGroups() {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("DELETE FROM " + GROUPS_TABLE_NAME + " WHERE " + GROUPS_COL_DISABLED + " = ? ", new Object[] {1});
db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME
+ " WHERE " + MARKERS_COL_DISABLED + " = ? AND " + MARKERS_COL_ACTIVE + " = ?", new Object[] {1, 1});
} finally {
db.close();
}
}
}
public void addGroup(MapMarkersGroup group) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("INSERT INTO " + GROUPS_TABLE_NAME + " VALUES (?, ?, ?, ?, ?)",
new Object[] {group.getId(), group.getName(), group.getType().getTypeId(), group.isDisabled(), group.getWptCategoriesString()});
} finally {
db.close();
}
}
}
public Map<String, MapMarkersGroup> getAllGroupsMap() {
Map<String, MapMarkersGroup> res = new LinkedHashMap<>();
SQLiteConnection db = openConnection(true);
if (db != null) {
try {
SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT, null);
if (query != null && query.moveToFirst()) {
do {
MapMarkersGroup group = readGroup(query);
res.put(group.getId(), group);
} while (query.moveToNext());
}
if (query != null) {
query.close();
}
} finally {
db.close();
}
}
return res;
}
private MapMarkersGroup readGroup(SQLiteCursor query) {
String id = query.getString(0);
String name = query.getString(1);
int type = query.getInt(2);
boolean disabled = query.getInt(3) == 1;
String categories = query.getString(4);
MapMarkersGroup res = new MapMarkersGroup(id, name, ItineraryType.findTypeForId(type));
res.setDisabled(disabled);
res.setWptCategories(categories == null ? null : Algorithms.decodeStringSet(categories));
return res;
}
public void removeMarkersGroup(String id) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("DELETE FROM " + GROUPS_TABLE_NAME + " WHERE " + GROUPS_COL_ID + " = ?", new Object[] {id});
} finally {
db.close();
}
}
}
public void updateGroupCategories(String id, String categories) {
SQLiteConnection db = openConnection(false);
if (db != null) {
try {
db.execSQL("UPDATE " + GROUPS_TABLE_NAME +
" SET " + GROUPS_COL_CATEGORIES + " = ? " +
"WHERE " + GROUPS_COL_ID + " = ?", new Object[] {categories, id});
} finally {
db.close();
}
}
}
}