use of de.westnordost.streetcomplete.data.osm.OsmQuest in project StreetComplete by westnordost.
the class QuestController method hideQuest.
/**
* Make the given quest invisible asynchronously (per user interaction).
*/
public void hideQuest(final long questId, final QuestGroup group) {
workerHandler.post(() -> {
if (group == QuestGroup.OSM) {
OsmQuest q = osmQuestDB.get(questId);
if (q == null)
return;
q.setStatus(QuestStatus.HIDDEN);
osmQuestDB.update(q);
relay.onQuestRemoved(q.getId(), group);
} else if (group == QuestGroup.OSM_NOTE) {
OsmNoteQuest q = osmNoteQuestDB.get(questId);
if (q == null)
return;
q.setStatus(QuestStatus.HIDDEN);
osmNoteQuestDB.update(q);
relay.onQuestRemoved(q.getId(), group);
}
});
}
use of de.westnordost.streetcomplete.data.osm.OsmQuest in project StreetComplete by westnordost.
the class QuestController method retrieveNextAt.
public void retrieveNextAt(long questId, QuestGroup group) {
workerHandler.post(() -> {
if (group == QuestGroup.OSM) {
OsmQuest osmQuest = osmQuestDB.getNextNewAt(questId, getQuestTypeNames());
if (osmQuest == null)
return;
Element element = osmElementDB.get(osmQuest.getElementType(), osmQuest.getElementId());
if (element == null)
return;
relay.onQuestSelected(osmQuest, group, element);
}
});
}
use of de.westnordost.streetcomplete.data.osm.OsmQuest in project StreetComplete by westnordost.
the class OsmQuestDownloadTest method testDeleteObsoleteQuests.
public void testDeleteObsoleteQuests() {
LatLon pos = new OsmLatLon(3.0, 4.0);
ElementWithGeometry node4 = new ElementWithGeometry();
node4.element = new OsmNode(4, 0, pos, null);
node4.geometry = new ElementGeometry(pos);
// questType mock will only "find" the Node #4
OsmElementQuestType questType = new ListBackedQuestType(Collections.singletonList(node4));
// in the quest database mock, there are quests for node 4 and node 5
List<OsmQuest> quests = new ArrayList<>();
quests.add(new OsmQuest(12L, questType, Element.Type.NODE, 4, QuestStatus.NEW, null, null, new Date(), new ElementGeometry(pos)));
quests.add(new OsmQuest(13L, questType, Element.Type.NODE, 5, QuestStatus.NEW, null, null, new Date(), new ElementGeometry(pos)));
when(osmQuestDao.getAll(any(BoundingBox.class), any(QuestStatus.class), anyString(), any(Element.Type.class), anyLong())).thenReturn(quests);
doAnswer(invocation -> {
Collection<Long> deletedQuests = (Collection<Long>) (invocation.getArguments()[0]);
assertEquals(1, deletedQuests.size());
assertEquals(13L, (long) deletedQuests.iterator().next());
return 1;
}).when(osmQuestDao).deleteAll(any(Collection.class));
OsmQuestDownload dl = new OsmQuestDownload(geometryDb, elementDb, osmQuestDao, countryBoundariesFuture);
VisibleQuestListener listener = mock(VisibleQuestListener.class);
dl.setQuestListener(listener);
// -> we expect that quest with node #5 is removed
dl.download(questType, new BoundingBox(0, 0, 1, 1), null);
verify(osmQuestDao).deleteAll(any(Collection.class));
verify(listener).onQuestsRemoved(any(Collection.class), any(QuestGroup.class));
}
use of de.westnordost.streetcomplete.data.osm.OsmQuest in project StreetComplete by westnordost.
the class OsmQuestDownload method download.
public boolean download(final OsmElementQuestType questType, BoundingBox bbox, final Set<LatLon> blacklistedPositions) {
if (!isQuestTypeEnabledForBoundingBox(questType, bbox)) {
Log.i(TAG, getQuestTypeName(questType) + ": " + "Skipped because it is disabled for this country");
return true;
}
final ArrayList<ElementGeometryDao.Row> geometryRows = new ArrayList<>();
final Map<OsmElementKey, Element> elements = new HashMap<>();
final ArrayList<OsmQuest> quests = new ArrayList<>();
final Map<OsmElementKey, Long> previousQuests = getPreviousQuestsIdsByElementKey(questType, bbox);
long time = System.currentTimeMillis();
boolean success = questType.download(bbox, (element, geometry) -> {
if (mayCreateQuestFrom(questType, element, geometry, blacklistedPositions)) {
Element.Type elementType = element.getType();
long elementId = element.getId();
OsmQuest quest = new OsmQuest(questType, elementType, elementId, geometry);
geometryRows.add(new ElementGeometryDao.Row(elementType, elementId, quest.getGeometry()));
quests.add(quest);
OsmElementKey elementKey = new OsmElementKey(elementType, elementId);
elements.put(elementKey, element);
previousQuests.remove(elementKey);
}
});
if (!success)
return false;
// geometry and elements must be put into DB first because quests have foreign keys on it
geometryDB.putAll(geometryRows);
elementDB.putAll(elements.values());
int newQuestsByQuestType = osmQuestDB.addAll(quests);
if (questListener != null) {
Iterator<OsmQuest> it = quests.iterator();
while (it.hasNext()) {
// it is null if this quest is already in the DB, so don't call onQuestCreated
if (it.next().getId() == null)
it.remove();
}
if (!quests.isEmpty())
questListener.onQuestsCreated(quests, QuestGroup.OSM);
}
if (!previousQuests.isEmpty()) {
if (questListener != null) {
questListener.onQuestsRemoved(previousQuests.values(), QuestGroup.OSM);
}
osmQuestDB.deleteAll(previousQuests.values());
}
// note: this could be done after ALL osm quest types have been downloaded if this
// turns out to be slow if done for every quest type
geometryDB.deleteUnreferenced();
elementDB.deleteUnreferenced();
int obsoleteAmount = previousQuests.size();
Log.i(TAG, getQuestTypeName(questType) + ": " + "Added " + newQuestsByQuestType + " new and " + "removed " + obsoleteAmount + " already resolved quests." + " (Total: " + quests.size() + ")" + " in " + (System.currentTimeMillis() - time) + "ms");
return true;
}
use of de.westnordost.streetcomplete.data.osm.OsmQuest in project StreetComplete by westnordost.
the class AOsmQuestDao method createObjectFrom.
@Override
protected OsmQuest createObjectFrom(Cursor cursor) {
int colQuestId = cursor.getColumnIndexOrThrow(Columns.QUEST_ID), colElementId = cursor.getColumnIndexOrThrow(Columns.ELEMENT_ID), colElementType = cursor.getColumnIndexOrThrow(Columns.ELEMENT_TYPE), colQuestStatus = cursor.getColumnIndexOrThrow(Columns.QUEST_STATUS), colQuestType = cursor.getColumnIndexOrThrow(Columns.QUEST_TYPE), colChanges = cursor.getColumnIndexOrThrow(Columns.TAG_CHANGES), colChangesSource = cursor.getColumnIndexOrThrow(Columns.CHANGES_SOURCE), colLastChange = cursor.getColumnIndexOrThrow(Columns.LAST_UPDATE);
long questId = cursor.getLong(colQuestId);
long elementId = cursor.getLong(colElementId);
Element.Type elementType = Element.Type.valueOf(cursor.getString(colElementType));
QuestStatus questStatus = QuestStatus.valueOf(cursor.getString(colQuestStatus));
String questTypeName = cursor.getString(colQuestType);
OsmElementQuestType questType = (OsmElementQuestType) questTypeRegistry.getByName(questTypeName);
if (questType == null) {
throw new IllegalArgumentException("The quest type " + questTypeName + " does not exist!");
}
StringMapChanges changes = null;
if (!cursor.isNull(colChanges)) {
changes = serializer.toObject(cursor.getBlob(colChanges), StringMapChanges.class);
}
String changesSource = null;
if (!cursor.isNull(colChangesSource)) {
changesSource = cursor.getString(colChangesSource);
}
ElementGeometry geometry = ElementGeometryDao.createObjectFrom(serializer, cursor);
Date lastChange = new Date(cursor.getLong(colLastChange));
return new OsmQuest(questId, questType, elementType, elementId, questStatus, changes, changesSource, lastChange, geometry);
}
Aggregations