Merge pull request #1757 from MilosKozak/update-checker

update logic version checker
This commit is contained in:
AdrianLxM 2019-04-23 21:58:52 +02:00 committed by GitHub
commit 7c890f2129
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 38 deletions

View file

@ -120,7 +120,7 @@ public class MainActivity extends AppCompatActivity {
//Check here if loop plugin is disabled. Else check via constraints //Check here if loop plugin is disabled. Else check via constraints
if (!LoopPlugin.getPlugin().isEnabled(PluginType.LOOP)) if (!LoopPlugin.getPlugin().isEnabled(PluginType.LOOP))
VersionCheckerUtilsKt.checkVersion(); VersionCheckerUtilsKt.triggerCheckVersion();
FabricPrivacy.setUserStats(); FabricPrivacy.setUserStats();
} }

View file

@ -89,6 +89,8 @@ import info.nightscout.androidaps.services.Intents;
import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.FabricPrivacy;
import io.fabric.sdk.android.Fabric; import io.fabric.sdk.android.Fabric;
import static info.nightscout.androidaps.plugins.general.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion;
public class MainApp extends Application { public class MainApp extends Application {
private static Logger log = LoggerFactory.getLogger(L.CORE); private static Logger log = LoggerFactory.getLogger(L.CORE);
@ -149,6 +151,9 @@ public class MainApp extends Application {
registerLocalBroadcastReceiver(); registerLocalBroadcastReceiver();
//trigger here to see the new version on app start after an update
triggerCheckVersion();
if (pluginsList == null) { if (pluginsList == null) {
pluginsList = new ArrayList<>(); pluginsList = new ArrayList<>();
// Register all tabs in app here // Register all tabs in app here

View file

@ -23,7 +23,7 @@ object VersionCheckerPlugin : PluginBase(PluginDescription()
override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> { override fun isClosedLoopAllowed(value: Constraint<Boolean>): Constraint<Boolean> {
checkWarning() checkWarning()
checkUpdate() triggerCheckVersion()
return if (isOldVersion(GRACE_PERIOD_VERY_OLD)) return if (isOldVersion(GRACE_PERIOD_VERY_OLD))
value.set(false, MainApp.gs(R.string.very_old_version), this) value.set(false, MainApp.gs(R.string.very_old_version), this)
else else
@ -34,30 +34,17 @@ object VersionCheckerPlugin : PluginBase(PluginDescription()
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
if (isOldVersion(GRACE_PERIOD_WARNING) && shouldWarnAgain(now)) { if (isOldVersion(GRACE_PERIOD_WARNING) && shouldWarnAgain(now)) {
// store last notification time // store last notification time
SP.putLong(R.string.key_last_versionchecker_warning, now) SP.putLong(R.string.key_last_versionchecker_plugin_warning, now)
//notify //notify
val message = MainApp.gs(R.string.new_version_warning, Math.round(now / TimeUnit.DAYS.toMillis(1).toDouble())) val message = MainApp.gs(R.string.new_version_warning, Math.round((now - SP.getLong(R.string.key_last_time_this_version_detected, now)) / TimeUnit.DAYS.toMillis(1).toDouble()))
val notification = Notification(Notification.OLDVERSION, message, Notification.NORMAL) val notification = Notification(Notification.OLDVERSION, message, Notification.NORMAL)
MainApp.bus().post(EventNewNotification(notification)) MainApp.bus().post(EventNewNotification(notification))
} }
} }
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) = 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<Double>): Constraint<Double> = override fun applyMaxIOBConstraints(maxIob: Constraint<Double>): Constraint<Double> =
if (isOldVersion(GRACE_PERIOD_OLD)) if (isOldVersion(GRACE_PERIOD_OLD))
@ -67,10 +54,9 @@ object VersionCheckerPlugin : PluginBase(PluginDescription()
private fun isOldVersion(gracePeriod: Long): Boolean { private fun isOldVersion(gracePeriod: Long): Boolean {
val now = System.currentTimeMillis() 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 WARN_EVERY = TimeUnit.DAYS.toMillis(1)
val GRACE_PERIOD_WARNING = TimeUnit.DAYS.toMillis(30) val GRACE_PERIOD_WARNING = TimeUnit.DAYS.toMillis(30)
val GRACE_PERIOD_OLD = TimeUnit.DAYS.toMillis(60) val GRACE_PERIOD_OLD = TimeUnit.DAYS.toMillis(60)

View file

@ -15,6 +15,7 @@ import org.apache.http.impl.client.DefaultHttpClient
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.util.concurrent.TimeUnit
// check network connection // check network connection
fun isConnected(): Boolean { fun isConnected(): Boolean {
@ -35,8 +36,22 @@ inline fun InputStream.findVersion(): String? {
private val log = LoggerFactory.getLogger(L.CORE) private val log = LoggerFactory.getLogger(L.CORE)
fun triggerCheckVersion() {
if(!SP.contains(R.string.key_last_time_this_version_detected)) {
// 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, System.currentTimeMillis() - TimeUnit.DAYS.toMillis(30))
}
// 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") @Suppress("DEPRECATION")
fun checkVersion() = if (isConnected()) { private fun checkVersion() = if (isConnected()) {
Thread { Thread {
try { try {
val request = HttpGet("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle") val request = HttpGet("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle")
@ -56,12 +71,17 @@ fun compareWithCurrentVersion(newVersion: String?, currentVersion: String) {
comparison == null -> onVersionNotDetectable() comparison == null -> onVersionNotDetectable()
comparison == 0 -> onSameVersionDetected() comparison == 0 -> onSameVersionDetected()
comparison > 0 -> onNewVersionDetected(currentVersion = currentVersion, newVersion = newVersion) 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() { 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() { fun onVersionNotDetectable() {
@ -69,10 +89,13 @@ fun onVersionNotDetectable() {
} }
fun onNewVersionDetected(currentVersion: String, newVersion: String?) { fun onNewVersionDetected(currentVersion: String, newVersion: String?) {
log.debug("Version ${currentVersion} outdated. Found $newVersion") val now = System.currentTimeMillis()
val notification = Notification(Notification.NEWVERSIONDETECTED, String.format(MainApp.gs(R.string.versionavailable), newVersion.toString()), Notification.LOW) if(now > SP.getLong(R.string.key_last_versionchecker_warning, 0) + WARN_EVERY) {
MainApp.bus().post(EventNewNotification(notification)) log.debug("Version ${currentVersion} outdated. Found $newVersion")
SP.putLong(R.string.key_new_version_available_since, System.currentTimeMillis()) 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 { fun String.versionStrip() = this.mapNotNull {
@ -82,3 +105,7 @@ fun String.versionStrip() = this.mapNotNull {
else -> null else -> null
} }
}.joinToString(separator = "") }.joinToString(separator = "")
val CHECK_EVERY = TimeUnit.DAYS.toMillis(1)
val WARN_EVERY = TimeUnit.DAYS.toMillis(1)

View file

@ -1327,9 +1327,9 @@
<string name="notconfigured">Not configured</string> <string name="notconfigured">Not configured</string>
<string name="profileswitchcreated">Profile switch created</string> <string name="profileswitchcreated">Profile switch created</string>
<string name="versionChecker">Version Checker</string> <string name="versionChecker">Version Checker</string>
<string name="key_new_version_available_since" translatable="false">new_version_available_since</string> <string name="key_last_time_this_version_detected" translatable="false">last_time_this_version_detected</string>
<string name="key_last_versionchecker_warning" translatable="false">last_versionchecker_waring</string> <string name="key_last_versionchecker_warning" translatable="false">last_versionchecker_waring</string>
<string name="key_last_versioncheck" translatable="false">key_last_versioncheck</string> <string name="key_last_versionchecker_plugin_warning" translatable="false">last_versionchecker_plugin_waring</string>
<string name="old_version">old version</string> <string name="old_version">old version</string>
<string name="very_old_version">very old version</string> <string name="very_old_version">very old version</string>

View file

@ -70,7 +70,9 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }
@ -85,7 +87,9 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }
@ -99,7 +103,9 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }
@ -113,7 +119,9 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }
@ -126,7 +134,9 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }
@ -139,7 +149,9 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }
@ -159,7 +171,7 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(0)).post(any()) verify(bus, times(0)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }
@ -179,7 +191,9 @@ class VersionCheckerUtilsKtTest {
verify(bus, times(1)).post(any()) verify(bus, times(1)).post(any())
PowerMockito.verifyStatic(SP::class.java, times(1)) 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) PowerMockito.verifyNoMoreInteractions(SP::class.java)
} }