From 972fdbfe9e40853afc09eb06f83045c95acfb53c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 5 Oct 2021 17:55:19 +0200 Subject: [PATCH] 2.6.2 version check --- app/build.gradle | 2 +- .../versionChecker/AllowedVersions.kt | 45 +++++++++++++++++++ .../versionChecker/VersionCheckerUtils.kt | 4 +- .../versionChecker/AllowedVersionsTest.kt | 20 +++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt diff --git a/app/build.gradle b/app/build.gradle index 893f13632c..4ad7f8cd49 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -109,7 +109,7 @@ android { targetSdkVersion 28 multiDexEnabled true versionCode 1500 - version "2.6.1.4" + version "2.6.2" buildConfigField "String", "VERSION", '"' + version + '"' buildConfigField "String", "BUILDVERSION", '"' + generateGitBuild() + '-' + generateDate() + '"' buildConfigField "String", "REMOTE", '"' + generateGitRemote() + '"' diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt new file mode 100644 index 0000000000..ae320cf929 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersions.kt @@ -0,0 +1,45 @@ +package info.nightscout.androidaps.plugins.constraints.versionChecker + +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject + +class AllowedVersions { + + fun generateSupportedVersions(): String = + JSONArray() + .put(JSONObject().apply { + put("minAndroid", 1) // 1.0 + put("maxAndroid", 23) // 6.0.1 + }) + .put(JSONObject().apply { + put("minAndroid", 24) // 7.0 + put("maxAndroid", 25) // 7.1.2 + put("supported", "2.6.2") + }) + .put(JSONObject().apply { + put("minAndroid", 26) // 8.0 + put("maxAndroid", 27) // 8.1 + put("supported", "2.8.2") + }) + .put(JSONObject().apply { + put("minAndroid", 28) // 9.0 + put("maxAndroid", 99) + put("supported", "2.8.2") + }) + .toString() + + fun findByApi(definition: String?, api: Int): JSONObject? { + if (definition == null) return null + try { + val array = JSONArray(definition) + for (i in 0 until array.length()) { + val record = array[i] as JSONObject + if (api in record.getInt("minAndroid")..record.getInt("maxAndroid")) return record + } + } catch (e: JSONException) { + } + return null + } + +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt index 91f1438d4a..5ecf83f3fa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/versionChecker/VersionCheckerUtils.kt @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.constraints.versionChecker import android.content.Context import android.net.ConnectivityManager +import android.os.Build import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R @@ -39,7 +40,8 @@ fun triggerCheckVersion() { private fun checkVersion() = if (isConnected()) { Thread { try { - val version: String? = findVersion(URL("https://raw.githubusercontent.com/MilosKozak/AndroidAPS/master/app/build.gradle").readText()) + val definition: String = URL("https://raw.githubusercontent.com/nightscout/AndroidAPS/versions/definition.json").readText() + val version: String? = AllowedVersions().findByApi(definition, Build.VERSION.SDK_INT)?.optString("supported") compareWithCurrentVersion(version, BuildConfig.VERSION_NAME) } catch (e: IOException) { log.debug("Github master version check error: $e") diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt new file mode 100644 index 0000000000..d472f0ebc2 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/versionChecker/AllowedVersionsTest.kt @@ -0,0 +1,20 @@ +package info.nightscout.androidaps.plugins.constraints.versionChecker + +import org.junit.Assert.* +import org.junit.Test + +class AllowedVersionsTest { + + @Test + fun generateSupportedVersionsTest() { + val definition = AllowedVersions().generateSupportedVersions() + assertNull(AllowedVersions().findByApi(definition, 0)) + assertFalse(AllowedVersions().findByApi(definition, 1)?.has("supported") ?: true) + assertFalse(AllowedVersions().findByApi(definition, 23)?.has("supported") ?: true) + assertEquals("2.6.2", AllowedVersions().findByApi(definition, 24)?.getString("supported")) + assertEquals("2.6.2", AllowedVersions().findByApi(definition, 25)?.getString("supported")) + assertEquals("2.8.2", AllowedVersions().findByApi(definition, 26)?.getString("supported")) + assertEquals("2.8.2", AllowedVersions().findByApi(definition, 27)?.getString("supported")) + assertEquals("2.8.2", AllowedVersions().findByApi(definition, 28)?.getString("supported")) + } +} \ No newline at end of file