diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 31d462a007..6779b0a467 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -120,7 +120,7 @@ public class MainActivity extends AppCompatActivity { //Check here if loop plugin is disabled. Else check via constraints if (!LoopPlugin.getPlugin().isEnabled(PluginType.LOOP)) - VersionCheckerUtilsKt.checkVersion(); + VersionCheckerUtilsKt.triggerCheckVersion(); FabricPrivacy.setUserStats(); } diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index e390d12007..37372ae0d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -89,6 +89,8 @@ import info.nightscout.androidaps.services.Intents; import info.nightscout.androidaps.utils.FabricPrivacy; import io.fabric.sdk.android.Fabric; +import static info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion; + public class MainApp extends Application { private static Logger log = LoggerFactory.getLogger(L.CORE); @@ -149,6 +151,9 @@ public class MainApp extends Application { registerLocalBroadcastReceiver(); + //trigger here to see the new version on app start after an update + triggerCheckVersion(); + if (pluginsList == null) { pluginsList = new ArrayList<>(); // Register all tabs in app here diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt index 7be99e2369..fc773aaaaf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerPlugin.kt @@ -23,7 +23,7 @@ object VersionCheckerPlugin : PluginBase(PluginDescription() override fun isClosedLoopAllowed(value: Constraint): Constraint { checkWarning() - checkUpdate() + triggerCheckVersion() return if (isOldVersion(GRACE_PERIOD_VERY_OLD)) value.set(false, MainApp.gs(R.string.very_old_version), this) else @@ -34,7 +34,7 @@ object VersionCheckerPlugin : PluginBase(PluginDescription() val now = System.currentTimeMillis() if (isOldVersion(GRACE_PERIOD_WARNING) && shouldWarnAgain(now)) { // store last notification time - SP.putLong(R.string.key_last_versionchecker_warning, now) + SP.putLong(R.string.key_last_versionchecker_plugin_warning, now) //notify val message = MainApp.gs(R.string.new_version_warning, Math.round(now / TimeUnit.DAYS.toMillis(1).toDouble())) @@ -43,21 +43,8 @@ object VersionCheckerPlugin : PluginBase(PluginDescription() } } - private fun checkUpdate() { - val now = System.currentTimeMillis() - if (shouldCheckVersionAgain(now)) { - // store last notification time - SP.putLong(R.string.key_last_versioncheck, now) - - checkVersion() - } - } - - private fun shouldCheckVersionAgain(now: Long) = - now > SP.getLong(R.string.key_last_versioncheck, 0) + CHECK_EVERY - private fun shouldWarnAgain(now: Long) = - now > SP.getLong(R.string.key_last_versionchecker_warning, 0) + WARN_EVERY + now > SP.getLong(R.string.key_last_versionchecker_plugin_warning, 0) + WARN_EVERY override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint = if (isOldVersion(GRACE_PERIOD_OLD)) @@ -67,10 +54,9 @@ object VersionCheckerPlugin : PluginBase(PluginDescription() private fun isOldVersion(gracePeriod: Long): Boolean { val now = System.currentTimeMillis() - return now > SP.getLong(R.string.key_new_version_available_since, 0) + gracePeriod + return now > SP.getLong(R.string.key_last_time_this_version_detected, 0) + gracePeriod } - val CHECK_EVERY = TimeUnit.DAYS.toMillis(1) val WARN_EVERY = TimeUnit.DAYS.toMillis(1) val GRACE_PERIOD_WARNING = TimeUnit.DAYS.toMillis(30) val GRACE_PERIOD_OLD = TimeUnit.DAYS.toMillis(60) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt index b8cfa7fffc..1b01ea70de 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtils.kt @@ -15,6 +15,7 @@ import org.apache.http.impl.client.DefaultHttpClient import org.slf4j.LoggerFactory import java.io.IOException import java.io.InputStream +import java.util.concurrent.TimeUnit // check network connection fun isConnected(): Boolean { @@ -35,8 +36,16 @@ inline fun InputStream.findVersion(): String? { private val log = LoggerFactory.getLogger(L.CORE) + +fun triggerCheckVersion() { + // If we are good, only check once every day. + if(System.currentTimeMillis() > SP.getLong(R.string.key_last_time_this_version_detected, 0) + CHECK_EVERY){ + checkVersion() + } +} + @Suppress("DEPRECATION") -fun checkVersion() = if (isConnected()) { +private fun checkVersion() = if (isConnected()) { Thread { try { val request = HttpGet("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle") @@ -56,12 +65,17 @@ fun compareWithCurrentVersion(newVersion: String?, currentVersion: String) { comparison == null -> onVersionNotDetectable() comparison == 0 -> onSameVersionDetected() comparison > 0 -> onNewVersionDetected(currentVersion = currentVersion, newVersion = newVersion) - else -> log.debug("Version newer than master. Are you developer?") + else -> onOlderVersionDetected() } } +private fun onOlderVersionDetected() { + log.debug("Version newer than master. Are you developer?") + SP.putLong(R.string.key_last_time_this_version_detected, System.currentTimeMillis()) +} + fun onSameVersionDetected() { - SP.remove(R.string.key_new_version_available_since) + SP.putLong(R.string.key_last_time_this_version_detected, System.currentTimeMillis()) } fun onVersionNotDetectable() { @@ -69,10 +83,13 @@ fun onVersionNotDetectable() { } fun onNewVersionDetected(currentVersion: String, newVersion: String?) { - log.debug("Version ${currentVersion} outdated. Found $newVersion") - val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), newVersion.toString()), Notification.LOW) - MainApp.bus().post(EventNewNotification(notification)) - SP.putLong(R.string.key_new_version_available_since, System.currentTimeMillis()) + val now = System.currentTimeMillis() + if(now > SP.getLong(R.string.key_last_versionchecker_warning, 0) + WARN_EVERY) { + log.debug("Version ${currentVersion} outdated. Found $newVersion") + val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), newVersion.toString()), Notification.LOW) + MainApp.bus().post(EventNewNotification(notification)) + SP.putLong(R.string.key_last_versionchecker_warning, now) + } } fun String.versionStrip() = this.mapNotNull { @@ -81,4 +98,8 @@ fun String.versionStrip() = this.mapNotNull { '.' -> it else -> null } -}.joinToString(separator = "") \ No newline at end of file +}.joinToString(separator = "") + + +val CHECK_EVERY = TimeUnit.DAYS.toMillis(1) +val WARN_EVERY = TimeUnit.DAYS.toMillis(1) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9aa326361b..6722bc7786 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1327,9 +1327,9 @@ Not configured Profile switch created Version Checker - new_version_available_since + last_time_this_version_detected last_versionchecker_waring - key_last_versioncheck + last_versionchecker_plugin_waring old version very old version diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt index 29725e4d9e..2d07e40c1d 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/versionChecker/VersionCheckerUtilsKtTest.kt @@ -70,7 +70,9 @@ class VersionCheckerUtilsKtTest { verify(bus, times(1)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.putLong(eq(R.string.key_new_version_available_since), ArgumentMatchers.anyLong()) + SP.getLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) + PowerMockito.verifyStatic(SP::class.java, times(1)) + SP.putLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) } @@ -85,7 +87,9 @@ class VersionCheckerUtilsKtTest { verify(bus, times(1)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.putLong(eq(R.string.key_new_version_available_since), ArgumentMatchers.anyLong()) + SP.getLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) + PowerMockito.verifyStatic(SP::class.java, times(1)) + SP.putLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) } @@ -99,7 +103,9 @@ class VersionCheckerUtilsKtTest { verify(bus, times(1)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.putLong(eq(R.string.key_new_version_available_since), ArgumentMatchers.anyLong()) + SP.getLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) + PowerMockito.verifyStatic(SP::class.java, times(1)) + SP.putLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) } @@ -113,7 +119,9 @@ class VersionCheckerUtilsKtTest { verify(bus, times(1)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.putLong(eq(R.string.key_new_version_available_since), ArgumentMatchers.anyLong()) + SP.getLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) + PowerMockito.verifyStatic(SP::class.java, times(1)) + SP.putLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) } @@ -126,7 +134,9 @@ class VersionCheckerUtilsKtTest { verify(bus, times(1)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.putLong(eq(R.string.key_new_version_available_since), ArgumentMatchers.anyLong()) + SP.getLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) + PowerMockito.verifyStatic(SP::class.java, times(1)) + SP.putLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) } @@ -139,7 +149,9 @@ class VersionCheckerUtilsKtTest { verify(bus, times(1)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.putLong(eq(R.string.key_new_version_available_since), ArgumentMatchers.anyLong()) + SP.getLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) + PowerMockito.verifyStatic(SP::class.java, times(1)) + SP.putLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) } @@ -159,7 +171,7 @@ class VersionCheckerUtilsKtTest { verify(bus, times(0)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.remove(eq(R.string.key_new_version_available_since)) + SP.putLong(eq(R.string.key_last_time_this_version_detected), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) } @@ -179,7 +191,9 @@ class VersionCheckerUtilsKtTest { verify(bus, times(1)).post(any()) PowerMockito.verifyStatic(SP::class.java, times(1)) - SP.putLong(eq(R.string.key_new_version_available_since), ArgumentMatchers.anyLong()) + SP.getLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) + PowerMockito.verifyStatic(SP::class.java, times(1)) + SP.putLong(eq(R.string.key_last_versionchecker_warning), ArgumentMatchers.anyLong()) PowerMockito.verifyNoMoreInteractions(SP::class.java) }