diff --git a/app/src/androidTest/java/de/westnordost/streetcomplete/data/osm/persist/OsmQuestDaoTest.java b/app/src/androidTest/java/de/westnordost/streetcomplete/data/osm/persist/OsmQuestDaoTest.java index dde1444b72..3d03947ca4 100644 --- a/app/src/androidTest/java/de/westnordost/streetcomplete/data/osm/persist/OsmQuestDaoTest.java +++ b/app/src/androidTest/java/de/westnordost/streetcomplete/data/osm/persist/OsmQuestDaoTest.java @@ -157,6 +157,20 @@ public void testDeleteAllClosed() assertEquals(2,dao.deleteAllClosed(System.currentTimeMillis() + 10000L)); } + public void testDeleteAllUnsolved() + { + OsmQuest quest1 = createNewQuest(1, Element.Type.NODE); + quest1.setStatus(QuestStatus.NEW); + OsmQuest quest2 = createNewQuest(2, Element.Type.NODE); + quest2.setStatus(QuestStatus.HIDDEN); + OsmQuest quest3 = createNewQuest(3, Element.Type.NODE); + quest3.setStatus(QuestStatus.ANSWERED); + + addToDaos(quest1, quest2, quest3); + + assertEquals(2,dao.deleteAllUnsolved(System.currentTimeMillis() + 10000L)); + } + public void testDeleteReverted() { ElementGeometry geom = new ElementGeometry(new OsmLatLon(5,5)); diff --git a/app/src/main/java/de/westnordost/streetcomplete/ApplicationConstants.java b/app/src/main/java/de/westnordost/streetcomplete/ApplicationConstants.java index 63b9304958..3a2dfe2258 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/ApplicationConstants.java +++ b/app/src/main/java/de/westnordost/streetcomplete/ApplicationConstants.java @@ -29,7 +29,12 @@ public class ApplicationConstants /** a "best before" duration for quests. Quests will not be downloaded again for any tile * before the time expired */ - public static final int REFRESH_QUESTS_AFTER = 7*24*60*60*1000; // one week in ms + public static final long REFRESH_QUESTS_AFTER = 7L*24*60*60*1000; // 1 week in ms + /** the duration after which quests will be deleted from the database if unsolved */ + public static final long DELETE_UNSOLVED_QUESTS_AFTER = 1L*30*24*60*60*1000; // 1 months in ms + + /** the max age of the undo history - one cannot undo changes older than X */ + public static final long MAX_QUEST_UNDO_HISTORY_AGE = 24*60*60*1000; // 1 day in ms public static final String AVATARS_CACHE_DIRECTORY = "osm_user_avatars"; diff --git a/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java b/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java index ca09e326fd..04a934e0d8 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java +++ b/app/src/main/java/de/westnordost/streetcomplete/MainActivity.java @@ -239,6 +239,11 @@ public void onServiceDisconnected(ComponentName className) mapFragment = (QuestsMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment); mapFragment.getMapAsync(BuildConfig.MAPZEN_API_KEY); updateMapQuestOffsets(); + + if(savedInstanceState == null) + { + questController.deleteOld(); + } } @Override public void onStart() diff --git a/app/src/main/java/de/westnordost/streetcomplete/data/AQuestDao.java b/app/src/main/java/de/westnordost/streetcomplete/data/AQuestDao.java index 6b452e2a64..9ce84a9e91 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/data/AQuestDao.java +++ b/app/src/main/java/de/westnordost/streetcomplete/data/AQuestDao.java @@ -177,6 +177,17 @@ public int deleteAllClosed(long olderThan) QuestStatus.CLOSED.name(), QuestStatus.REVERT.name(), String.valueOf(olderThan)}); } + public int deleteAllUnsolved(long olderThan) + { + String statusCol = getQuestStatusColumnName(); + String query = "(" + statusCol + " = ? OR " + statusCol + " = ?) AND " + + getLastChangedColumnName() + " < ?"; + + SQLiteDatabase db = dbHelper.getWritableDatabase(); + return db.delete(getTableName(), query, new String[]{ + QuestStatus.NEW.name(), QuestStatus.HIDDEN.name(), String.valueOf(olderThan)}); + } + public int addAll(Collection quests) { return insertAll(quests, false); diff --git a/app/src/main/java/de/westnordost/streetcomplete/data/QuestController.java b/app/src/main/java/de/westnordost/streetcomplete/data/QuestController.java index 360b9c7b01..0cd1cea20a 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/data/QuestController.java +++ b/app/src/main/java/de/westnordost/streetcomplete/data/QuestController.java @@ -421,4 +421,22 @@ public void upload() { context.startService(new Intent(context, QuestChangesUploadService.class)); } + + public void deleteOld() + { + workerHandler.post(() -> + { + long timestamp = System.currentTimeMillis() - ApplicationConstants.DELETE_UNSOLVED_QUESTS_AFTER; + int deleted = osmQuestDB.deleteAllUnsolved(timestamp); + deleted += osmNoteQuestDB.deleteAllUnsolved(timestamp); + + if(deleted > 0) + { + Log.d(TAG, "Deleted "+ deleted + " old unsolved quests"); + + osmElementDB.deleteUnreferenced(); + geometryDB.deleteUnreferenced(); + } + }); + } } diff --git a/app/src/main/java/de/westnordost/streetcomplete/data/osm/download/OsmQuestDownload.java b/app/src/main/java/de/westnordost/streetcomplete/data/osm/download/OsmQuestDownload.java index 47ff442652..1211b57c84 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/data/osm/download/OsmQuestDownload.java +++ b/app/src/main/java/de/westnordost/streetcomplete/data/osm/download/OsmQuestDownload.java @@ -128,6 +128,7 @@ public boolean download(final OsmElementQuestType questType, BoundingBox bbox, // turns out to be slow if done for every quest type geometryDB.deleteUnreferenced(); elementDB.deleteUnreferenced(); + questType.cleanMetadata(); int obsoleteAmount = previousQuests.size(); Log.i(TAG, getQuestTypeName(questType) + ": " + diff --git a/app/src/main/java/de/westnordost/streetcomplete/data/osm/upload/AOsmQuestChangesUpload.java b/app/src/main/java/de/westnordost/streetcomplete/data/osm/upload/AOsmQuestChangesUpload.java index 8433d8e32a..caed311d8a 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/data/osm/upload/AOsmQuestChangesUpload.java +++ b/app/src/main/java/de/westnordost/streetcomplete/data/osm/upload/AOsmQuestChangesUpload.java @@ -173,8 +173,8 @@ public synchronized void upload(AtomicBoolean cancelState) private void cleanUp(Set questTypes) { - long yesterday = System.currentTimeMillis() - 24 * 60 * 60 * 1000; - int deletedQuests = questDB.deleteAllClosed(yesterday); + long timestamp = System.currentTimeMillis() - ApplicationConstants.MAX_QUEST_UNDO_HISTORY_AGE; + int deletedQuests = questDB.deleteAllClosed(timestamp); if(deletedQuests > 0) { elementGeometryDB.deleteUnreferenced();