From fe56e19075657ff1c3f691afa0bd11c7b64cc637 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Mon, 11 Mar 2019 00:00:00 +0000 Subject: [PATCH] Add deleting old used time items --- .../timelimit/android/data/dao/UsedTimeDao.kt | 3 + .../android/logic/BackgroundTaskLogic.kt | 21 +++++-- .../android/logic/DayChangeTracker.kt | 60 +++++++++++++++++++ .../android/logic/UsedTimeDeleter.kt | 39 ++++++++++++ 4 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/io/timelimit/android/logic/DayChangeTracker.kt create mode 100644 app/src/main/java/io/timelimit/android/logic/UsedTimeDeleter.kt diff --git a/app/src/main/java/io/timelimit/android/data/dao/UsedTimeDao.kt b/app/src/main/java/io/timelimit/android/data/dao/UsedTimeDao.kt index bf6d96f..187e397 100644 --- a/app/src/main/java/io/timelimit/android/data/dao/UsedTimeDao.kt +++ b/app/src/main/java/io/timelimit/android/data/dao/UsedTimeDao.kt @@ -54,6 +54,9 @@ abstract class UsedTimeDao { @Query("DELETE FROM used_time WHERE category_id = :categoryId") abstract fun deleteUsedTimeItems(categoryId: String) + @Query("DELETE FROM used_time WHERE day_of_epoch < :lastDayToKeep") + abstract fun deleteOldUsedTimeItems(lastDayToKeep: Int) + @Query("SELECT * FROM used_time LIMIT :pageSize OFFSET :offset") abstract fun getUsedTimePageSync(offset: Int, pageSize: Int): List diff --git a/app/src/main/java/io/timelimit/android/logic/BackgroundTaskLogic.kt b/app/src/main/java/io/timelimit/android/logic/BackgroundTaskLogic.kt index 934263d..77ac13a 100644 --- a/app/src/main/java/io/timelimit/android/logic/BackgroundTaskLogic.kt +++ b/app/src/main/java/io/timelimit/android/logic/BackgroundTaskLogic.kt @@ -118,6 +118,10 @@ class BackgroundTaskLogic(val appLogic: AppLogic) { private var usedTimeUpdateHelper: UsedTimeItemBatchUpdateHelper? = null private var previousMainLogicExecutionTime = 0 private var previousMainLoopEndTime = 0L + private val dayChangeTracker = DayChangeTracker( + timeApi = appLogic.timeApi, + longDuration = 1000 * 60 * 10 /* 10 minutes */ + ) private val appTitleCache = QueryAppTitleCache(appLogic.platformIntegration) @@ -149,6 +153,18 @@ class BackgroundTaskLogic(val appLogic: AppLogic) { try { // get the current time val nowTimestamp = appLogic.timeApi.getCurrentTimeInMillis() + val nowTimezone = TimeZone.getTimeZone(deviceUserEntry.timeZone) + + val nowDate = DateInTimezone.newInstance(nowTimestamp, nowTimezone) + val minuteOfWeek = getMinuteOfWeek(nowTimestamp, nowTimezone) + + // eventually remove old used time data + if (dayChangeTracker.reportDayChange(nowDate.dayOfEpoch) == DayChangeTracker.DayChange.NowSinceLongerTime) { + UsedTimeDeleter.deleteOldUsedTimeItems( + database = appLogic.database, + date = nowDate + ) + } // get the categories val categories = childCategories.get(deviceUserEntry.id).waitForNonNullValue() @@ -206,11 +222,6 @@ class BackgroundTaskLogic(val appLogic: AppLogic) { )) appLogic.platformIntegration.showAppLockScreen(foregroundAppPackageName) } else { - val nowTimezone = TimeZone.getTimeZone(deviceUserEntry.timeZone) - - val nowDate = DateInTimezone.newInstance(nowTimestamp, nowTimezone) - val minuteOfWeek = getMinuteOfWeek(nowTimestamp, nowTimezone) - // disable time limits temporarily feature if (nowTimestamp < deviceUserEntry.disableLimitsUntil) { appLogic.platformIntegration.setAppStatusMessage(AppStatusMessage( diff --git a/app/src/main/java/io/timelimit/android/logic/DayChangeTracker.kt b/app/src/main/java/io/timelimit/android/logic/DayChangeTracker.kt new file mode 100644 index 0000000..f025c9f --- /dev/null +++ b/app/src/main/java/io/timelimit/android/logic/DayChangeTracker.kt @@ -0,0 +1,60 @@ +/* + * Open TimeLimit Copyright 2019 Jonas Lochmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.timelimit.android.logic + +import io.timelimit.android.integration.time.TimeApi + +class DayChangeTracker (private val timeApi: TimeApi, private val longDuration: Long) { + private var lastDayOfEpoch = -1 + private var lastDayOfEpochChange = -1L + private var lastReportedDayChangeDuration = -1L + + fun reportDayChange(newDay: Int): DayChange { + val uptime = timeApi.getCurrentUptimeInMillis() + + return if (lastDayOfEpoch != newDay) { + lastDayOfEpochChange = uptime + lastDayOfEpoch = newDay + lastReportedDayChangeDuration = 0 + + DayChange.Now + } else { + val newDayChangeDuration = uptime - lastDayOfEpochChange + + try { + if (newDayChangeDuration >= longDuration && lastReportedDayChangeDuration < longDuration) { + DayChange.NowSinceLongerTime + } else { + DayChange.No + } + } finally { + lastReportedDayChangeDuration = newDayChangeDuration + } + } + } + + enum class DayChange { + No, + Now, + NowSinceLongerTime + } + + fun reset() { + lastDayOfEpoch = -1 + lastDayOfEpochChange = -1 + lastReportedDayChangeDuration = -1 + } +} diff --git a/app/src/main/java/io/timelimit/android/logic/UsedTimeDeleter.kt b/app/src/main/java/io/timelimit/android/logic/UsedTimeDeleter.kt new file mode 100644 index 0000000..607bf84 --- /dev/null +++ b/app/src/main/java/io/timelimit/android/logic/UsedTimeDeleter.kt @@ -0,0 +1,39 @@ +/* + * Open TimeLimit Copyright 2019 Jonas Lochmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.timelimit.android.logic + +import io.timelimit.android.async.Threads +import io.timelimit.android.data.Database +import io.timelimit.android.data.transaction +import io.timelimit.android.date.DateInTimezone + +object UsedTimeDeleter { + fun deleteOldUsedTimeItems(database: Database, date: DateInTimezone) { + Threads.database.execute { + database.transaction().use { + if (database.config().getOwnDeviceIdSync() == null) { + // not configured + // => no need to delete anything + return@execute + } + + database.usedTimes().deleteOldUsedTimeItems(lastDayToKeep = date.dayOfEpoch - date.dayOfWeek) + + it.setSuccess() + } + } + } +} \ No newline at end of file