Search in sources :

Example 21 with OsmQuest

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);
        }
    });
}
Also used : OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest) OsmNoteQuest(de.westnordost.streetcomplete.data.osmnotes.OsmNoteQuest)

Example 22 with OsmQuest

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);
        }
    });
}
Also used : Element(de.westnordost.osmapi.map.data.Element) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest)

Example 23 with OsmQuest

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));
}
Also used : OsmNode(de.westnordost.osmapi.map.data.OsmNode) OsmElementQuestType(de.westnordost.streetcomplete.data.osm.OsmElementQuestType) ArrayList(java.util.ArrayList) QuestGroup(de.westnordost.streetcomplete.data.QuestGroup) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest) QuestStatus(de.westnordost.streetcomplete.data.QuestStatus) Date(java.util.Date) VisibleQuestListener(de.westnordost.streetcomplete.data.VisibleQuestListener) LatLon(de.westnordost.osmapi.map.data.LatLon) OsmLatLon(de.westnordost.osmapi.map.data.OsmLatLon) OsmElementQuestType(de.westnordost.streetcomplete.data.osm.OsmElementQuestType) BoundingBox(de.westnordost.osmapi.map.data.BoundingBox) Matchers.anyLong(org.mockito.Matchers.anyLong) ElementGeometry(de.westnordost.streetcomplete.data.osm.ElementGeometry) Collection(java.util.Collection) OsmLatLon(de.westnordost.osmapi.map.data.OsmLatLon)

Example 24 with OsmQuest

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;
}
Also used : HashMap(java.util.HashMap) Element(de.westnordost.osmapi.map.data.Element) ArrayList(java.util.ArrayList) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest) OsmElementKey(de.westnordost.streetcomplete.data.osm.persist.OsmElementKey) ElementGeometryDao(de.westnordost.streetcomplete.data.osm.persist.ElementGeometryDao)

Example 25 with OsmQuest

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);
}
Also used : OsmElementQuestType(de.westnordost.streetcomplete.data.osm.OsmElementQuestType) StringMapChanges(de.westnordost.streetcomplete.data.osm.changes.StringMapChanges) Element(de.westnordost.osmapi.map.data.Element) ElementGeometry(de.westnordost.streetcomplete.data.osm.ElementGeometry) OsmQuest(de.westnordost.streetcomplete.data.osm.OsmQuest) QuestStatus(de.westnordost.streetcomplete.data.QuestStatus) Date(java.util.Date)

Aggregations

OsmQuest (de.westnordost.streetcomplete.data.osm.OsmQuest)31 Element (de.westnordost.osmapi.map.data.Element)11 ElementGeometry (de.westnordost.streetcomplete.data.osm.ElementGeometry)9 OsmLatLon (de.westnordost.osmapi.map.data.OsmLatLon)7 Point (android.graphics.Point)6 OsmQuestDao (de.westnordost.streetcomplete.data.osm.persist.OsmQuestDao)6 MergedElementDao (de.westnordost.streetcomplete.data.osm.persist.MergedElementDao)5 TestQuestType (de.westnordost.streetcomplete.data.osm.persist.test.TestQuestType)5 ArrayList (java.util.ArrayList)5 TestQuestType2 (de.westnordost.streetcomplete.data.osm.persist.test.TestQuestType2)4 DownloadedTilesDao (de.westnordost.streetcomplete.data.tiles.DownloadedTilesDao)4 MapDataDao (de.westnordost.osmapi.map.MapDataDao)3 OpenChangesetsDao (de.westnordost.streetcomplete.data.changesets.OpenChangesetsDao)3 StringMapChanges (de.westnordost.streetcomplete.data.osm.changes.StringMapChanges)3 HashMap (java.util.HashMap)3 SharedPreferences (android.content.SharedPreferences)2 BoundingBox (de.westnordost.osmapi.map.data.BoundingBox)2 QuestStatus (de.westnordost.streetcomplete.data.QuestStatus)2 OsmElementQuestType (de.westnordost.streetcomplete.data.osm.OsmElementQuestType)2 ElementGeometryDao (de.westnordost.streetcomplete.data.osm.persist.ElementGeometryDao)2