Add deleting old used time items

This commit is contained in:
Jonas L 2019-03-11 00:00:00 +00:00
parent 114b798eaa
commit fe56e19075
4 changed files with 118 additions and 5 deletions

View file

@ -54,6 +54,9 @@ abstract class UsedTimeDao {
@Query("DELETE FROM used_time WHERE category_id = :categoryId") @Query("DELETE FROM used_time WHERE category_id = :categoryId")
abstract fun deleteUsedTimeItems(categoryId: String) 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") @Query("SELECT * FROM used_time LIMIT :pageSize OFFSET :offset")
abstract fun getUsedTimePageSync(offset: Int, pageSize: Int): List<UsedTimeItem> abstract fun getUsedTimePageSync(offset: Int, pageSize: Int): List<UsedTimeItem>

View file

@ -118,6 +118,10 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
private var usedTimeUpdateHelper: UsedTimeItemBatchUpdateHelper? = null private var usedTimeUpdateHelper: UsedTimeItemBatchUpdateHelper? = null
private var previousMainLogicExecutionTime = 0 private var previousMainLogicExecutionTime = 0
private var previousMainLoopEndTime = 0L private var previousMainLoopEndTime = 0L
private val dayChangeTracker = DayChangeTracker(
timeApi = appLogic.timeApi,
longDuration = 1000 * 60 * 10 /* 10 minutes */
)
private val appTitleCache = QueryAppTitleCache(appLogic.platformIntegration) private val appTitleCache = QueryAppTitleCache(appLogic.platformIntegration)
@ -149,6 +153,18 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
try { try {
// get the current time // get the current time
val nowTimestamp = appLogic.timeApi.getCurrentTimeInMillis() 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 // get the categories
val categories = childCategories.get(deviceUserEntry.id).waitForNonNullValue() val categories = childCategories.get(deviceUserEntry.id).waitForNonNullValue()
@ -206,11 +222,6 @@ class BackgroundTaskLogic(val appLogic: AppLogic) {
)) ))
appLogic.platformIntegration.showAppLockScreen(foregroundAppPackageName) appLogic.platformIntegration.showAppLockScreen(foregroundAppPackageName)
} else { } else {
val nowTimezone = TimeZone.getTimeZone(deviceUserEntry.timeZone)
val nowDate = DateInTimezone.newInstance(nowTimestamp, nowTimezone)
val minuteOfWeek = getMinuteOfWeek(nowTimestamp, nowTimezone)
// disable time limits temporarily feature // disable time limits temporarily feature
if (nowTimestamp < deviceUserEntry.disableLimitsUntil) { if (nowTimestamp < deviceUserEntry.disableLimitsUntil) {
appLogic.platformIntegration.setAppStatusMessage(AppStatusMessage( appLogic.platformIntegration.setAppStatusMessage(AppStatusMessage(

View file

@ -0,0 +1,60 @@
/*
* Open TimeLimit Copyright <C> 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 <https://www.gnu.org/licenses/>.
*/
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
}
}

View file

@ -0,0 +1,39 @@
/*
* Open TimeLimit Copyright <C> 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 <https://www.gnu.org/licenses/>.
*/
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()
}
}
}
}