From def6c51fa2cd1de391f720b3e48d249ae5adcbed Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 24 Aug 2023 16:51:03 +0200 Subject: [PATCH] VersionChecker: fix message, add logging --- .../di/PluginsConstraintsModule.kt | 2 +- .../versionChecker/VersionCheckerPlugin.kt | 24 +++++++++---------- .../versionChecker/VersionCheckerUtilsImpl.kt | 18 +++++++++----- .../src/main/res/values/strings.xml | 6 ++--- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/di/PluginsConstraintsModule.kt b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/di/PluginsConstraintsModule.kt index c3da1e525a..92a17231c1 100644 --- a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/di/PluginsConstraintsModule.kt +++ b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/di/PluginsConstraintsModule.kt @@ -21,7 +21,7 @@ abstract class PluginsConstraintsModule { @Module interface Bindings { - @Binds fun bindProcessedDeviceStatusData(versionCheckerUtils: VersionCheckerUtilsImpl): VersionCheckerUtils + @Binds fun bindVersionCheckerUtils(versionCheckerUtils: VersionCheckerUtilsImpl): VersionCheckerUtils @Binds fun bindBgQualityCheck(bgQualityCheck: BgQualityCheckPlugin): BgQualityCheck @Binds fun bindsConstraints(constraintsImpl: info.nightscout.plugins.constraints.ConstraintsImpl): Constraints } diff --git a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerPlugin.kt b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerPlugin.kt index 47b67fa3ab..5bf36f822d 100644 --- a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerPlugin.kt +++ b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerPlugin.kt @@ -44,7 +44,7 @@ class VersionCheckerPlugin @Inject constructor( enum class GracePeriod(val warning: Long, val old: Long, val veryOld: Long) { RELEASE(30, 60, 90), - RC(1, 7, 14) + RC(2, 7, 14) } private val gracePeriod: GracePeriod @@ -63,7 +63,7 @@ class VersionCheckerPlugin @Inject constructor( override fun isClosedLoopAllowed(value: Constraint): Constraint { checkWarning() versionCheckerUtils.triggerCheckVersion() - if (isOldVersion(gracePeriod.veryOld.daysToMillis())) + if (lastCheckOlderThan(gracePeriod.veryOld.daysToMillis())) value.set(aapsLogger, false, rh.gs(R.string.very_old_version), this) val endDate = sp.getLong(rh.gs(info.nightscout.core.utils.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0) if (endDate != 0L && dateUtil.now() > endDate) @@ -72,7 +72,7 @@ class VersionCheckerPlugin @Inject constructor( } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint = - if (isOldVersion(gracePeriod.old.daysToMillis())) + if (lastCheckOlderThan(gracePeriod.old.daysToMillis())) maxIob.set(aapsLogger, 0.0, rh.gs(R.string.old_version), this) else maxIob @@ -80,19 +80,19 @@ class VersionCheckerPlugin @Inject constructor( private fun checkWarning() { val now = dateUtil.now() - if (!sp.contains(R.string.key_last_versionchecker_plugin_warning)) { - sp.putLong(R.string.key_last_versionchecker_plugin_warning, now) + if (!sp.contains(R.string.key_last_versionchecker_plugin_warning_timestamp)) { + sp.putLong(R.string.key_last_versionchecker_plugin_warning_timestamp, now) return } - if (isOldVersion(gracePeriod.warning.daysToMillis()) && shouldWarnAgain()) { + if (lastCheckOlderThan(gracePeriod.warning.daysToMillis()) && shouldWarnAgain()) { // store last notification time - sp.putLong(R.string.key_last_versionchecker_plugin_warning, now) + sp.putLong(R.string.key_last_versionchecker_plugin_warning_timestamp, now) //notify val message = rh.gs( R.string.new_version_warning, - ((now - sp.getLong(R.string.key_last_time_this_version_detected_as_ok, now)) / 1L.daysToMillis().toDouble()).roundToInt(), + ((now - sp.getLong(R.string.key_last_successful_version_check_timestamp, now)) / 1L.daysToMillis().toDouble()).roundToInt(), gracePeriod.old, gracePeriod.veryOld ) @@ -102,7 +102,7 @@ class VersionCheckerPlugin @Inject constructor( val endDate = sp.getLong(rh.gs(info.nightscout.core.utils.R.string.key_app_expiration) + "_" + config.VERSION_NAME, 0) if (endDate != 0L && dateUtil.now() > endDate && shouldWarnAgain()) { // store last notification time - sp.putLong(R.string.key_last_versionchecker_plugin_warning, now) + sp.putLong(R.string.key_last_versionchecker_plugin_warning_timestamp, now) //notify uiInteraction.addNotification(Notification.VERSION_EXPIRE, rh.gs(R.string.application_expired), Notification.URGENT) @@ -110,10 +110,10 @@ class VersionCheckerPlugin @Inject constructor( } private fun shouldWarnAgain() = - dateUtil.now() > sp.getLong(R.string.key_last_versionchecker_plugin_warning, 0) + WARN_EVERY + dateUtil.now() > sp.getLong(R.string.key_last_versionchecker_plugin_warning_timestamp, 0) + WARN_EVERY - private fun isOldVersion(gracePeriod: Long): Boolean = - dateUtil.now() > sp.getLong(R.string.key_last_time_this_version_detected_as_ok, 0) + gracePeriod + private fun lastCheckOlderThan(gracePeriod: Long): Boolean = + dateUtil.now() > sp.getLong(R.string.key_last_successful_version_check_timestamp, 0) + gracePeriod private fun Long.daysToMillis() = TimeUnit.DAYS.toMillis(this) } diff --git a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerUtilsImpl.kt b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerUtilsImpl.kt index c688ec10d9..7c9b507ea5 100644 --- a/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerUtilsImpl.kt +++ b/plugins/constraints/src/main/java/info/nightscout/plugins/constraints/versionChecker/VersionCheckerUtilsImpl.kt @@ -36,13 +36,13 @@ class VersionCheckerUtilsImpl @Inject constructor( override fun triggerCheckVersion() { - if (!sp.contains(R.string.key_last_time_this_version_detected_as_ok)) { + if (!sp.contains(R.string.key_last_successful_version_check_timestamp)) { // On a new installation, set it as 30 days old in order to warn that there is a new version. - sp.putLong(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now() - TimeUnit.DAYS.toMillis(30)) + setLastCheckTimestamp(dateUtil.now() - TimeUnit.DAYS.toMillis(30)) } // If we are good, only check once every day. - if (dateUtil.now() > sp.getLong(R.string.key_last_time_this_version_detected_as_ok, 0) + CHECK_EVERY) { + if (dateUtil.now() > sp.getLong(R.string.key_last_successful_version_check_timestamp, 0) + CHECK_EVERY) { checkVersion() } } @@ -78,6 +78,7 @@ class VersionCheckerUtilsImpl @Inject constructor( val newVersionElements = newVersion.toNumberList() val currentVersionElements = currentVersion.toNumberList() + aapsLogger.debug(LTag.CORE, "Compare versions: $currentVersion $currentVersionElements, $newVersion $newVersionElements") if (newVersionElements.isNullOrEmpty()) { onVersionNotDetectable() return @@ -106,15 +107,15 @@ class VersionCheckerUtilsImpl @Inject constructor( private fun onOlderVersionDetected() { aapsLogger.debug(LTag.CORE, "Version newer than master. Are you developer?") - sp.putLong(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now()) + setLastCheckTimestamp(dateUtil.now()) } private fun onSameVersionDetected() { - sp.putLong(R.string.key_last_time_this_version_detected_as_ok, dateUtil.now()) + setLastCheckTimestamp(dateUtil.now()) } private fun onVersionNotDetectable() { - aapsLogger.debug(LTag.CORE, "fetch failed") + aapsLogger.debug(LTag.CORE, "Fetch failed") } private fun onNewVersionDetected(currentVersion: String, newVersion: String?) { @@ -135,6 +136,11 @@ class VersionCheckerUtilsImpl @Inject constructor( } } + private fun setLastCheckTimestamp(timestamp: Long) { + aapsLogger.debug(LTag.CORE, "Setting key_last_successful_version_check_timestamp ${dateUtil.dateAndTimeAndSecondsString(timestamp)}") + sp.putLong(R.string.key_last_successful_version_check_timestamp, timestamp) + } + private fun String?.toNumberList() = this?.numericVersionPart().takeIf { !it.isNullOrBlank() }?.split(".")?.map { it.toInt() } diff --git a/plugins/constraints/src/main/res/values/strings.xml b/plugins/constraints/src/main/res/values/strings.xml index c6aa95b54d..c858aaf85f 100644 --- a/plugins/constraints/src/main/res/values/strings.xml +++ b/plugins/constraints/src/main/res/values/strings.xml @@ -20,11 +20,11 @@ old version very old version Application expired - New version for at least %1$d days available! Fallback to LGS after %2$d days, loop will be disabled after %3$d days - last_time_this_version_detected + New version has not been checked for at least %1$d days! Fallback to LGS after %2$d days, loop will be disabled after %3$d days. Restore internet connectivity! + last_successful_version_check_timestamp last_versionchecker_warning last_expired_version_checker_warning - last_versionchecker_plugin_waring + last_versionchecker_plugin_warning_timestamp last_revoked_certs_check We have detected that you are running an invalid version. Loop disabled! Version %1$s available