From 63c0d89a77dc05eba7e8ea0ab7c01eedd2f2d55f Mon Sep 17 00:00:00 2001 From: Sarah Thian Date: Thu, 5 Oct 2017 14:47:25 +0800 Subject: [PATCH 1/5] add json params force and enable, parse them on versionCheck --- .../com/eggheadgames/siren/Constants.java | 4 ++ .../java/com/eggheadgames/siren/Siren.java | 54 +++++++++++++------ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/library/src/main/java/com/eggheadgames/siren/Constants.java b/library/src/main/java/com/eggheadgames/siren/Constants.java index 8e14a3c..ff6079b 100644 --- a/library/src/main/java/com/eggheadgames/siren/Constants.java +++ b/library/src/main/java/com/eggheadgames/siren/Constants.java @@ -7,4 +7,8 @@ final class Constants { static final String JSON_MIN_VERSION_CODE = "minVersionCode"; static final String JSON_MIN_VERSION_NAME = "minVersionName"; + + static final String JSON_FORCE_ALERT_TYPE = "force"; + + static final String JSON_ENABLE_VERSION_CHECK = "enable"; } diff --git a/library/src/main/java/com/eggheadgames/siren/Siren.java b/library/src/main/java/com/eggheadgames/siren/Siren.java index 7955821..f39a629 100644 --- a/library/src/main/java/com/eggheadgames/siren/Siren.java +++ b/library/src/main/java/com/eggheadgames/siren/Siren.java @@ -178,37 +178,51 @@ private boolean checkVersionName(JSONObject appJson) throws JSONException{ } getSirenHelper().setLastVerificationDate(mApplicationContext); + // If no config found, assume version check is enabled + Boolean versionCheckEnabled = appJson.has(Constants.JSON_ENABLE_VERSION_CHECK) ? appJson.getBoolean(Constants.JSON_ENABLE_VERSION_CHECK) : true; + if (!versionCheckEnabled) { + return false; + } + + // If no config found, assume force update = false + Boolean forceUpdateEnabled = appJson.has(Constants.JSON_FORCE_ALERT_TYPE) ? appJson.getBoolean(Constants.JSON_FORCE_ALERT_TYPE) : false; String minVersionName = appJson.getString(Constants.JSON_MIN_VERSION_NAME); String currentVersionName = getSirenHelper().getVersionName(mApplicationContext); if (getSirenHelper().isEmpty(minVersionName) || getSirenHelper().isEmpty(currentVersionName) || getSirenHelper().isVersionSkippedByUser(mApplicationContext, minVersionName)) { return false; } + SirenAlertType alertType = null; String[] minVersionNumbers = minVersionName.split("\\."); String[] currentVersionNumbers = currentVersionName.split("\\."); //noinspection ConstantConditions if (minVersionNumbers != null && currentVersionNumbers != null && minVersionNumbers.length == currentVersionNumbers.length) { - int digitVerificationCode = checkVersionDigit(minVersionNumbers, currentVersionNumbers, 0); - if (digitVerificationCode == 0) { - digitVerificationCode = checkVersionDigit(minVersionNumbers, currentVersionNumbers, 1); - if (digitVerificationCode == 0) { - digitVerificationCode = checkVersionDigit(minVersionNumbers, currentVersionNumbers, 2); - if (digitVerificationCode == 0) { - if (checkVersionDigit(minVersionNumbers, currentVersionNumbers, 3) == 1) { - alertType = revisionUpdateAlertType; } - } else if (digitVerificationCode == 1) { - alertType = patchUpdateAlertType; + + Boolean versionUpdateDetected = false; + for (Integer index = 0; index < Math.min(minVersionNumbers.length, currentVersionNumbers.length); index++) { + Integer compareResult = checkVersionDigit(minVersionNumbers, currentVersionNumbers, index); + if (compareResult == 1) { + versionUpdateDetected = true; + if (forceUpdateEnabled) { + alertType = SirenAlertType.FORCE; + } else { + switch (index) { + case 0: alertType = majorUpdateAlertType; break; + case 1: alertType = minorUpdateAlertType; break; + case 2: alertType = patchUpdateAlertType; break; + case 3: alertType = revisionUpdateAlertType; break; + default: alertType = SirenAlertType.OPTION; + } } - } else if (digitVerificationCode == 1) { - alertType = minorUpdateAlertType; + break; + } else if (compareResult == -1) { + return false; } - } else if (digitVerificationCode == 1) { - alertType = majorUpdateAlertType; } - if (alertType != null) { + if (versionUpdateDetected) { showAlert(minVersionName, alertType); return true; } @@ -231,13 +245,21 @@ private int checkVersionDigit(String[] minVersionNumbers, String[] currentVersio private boolean checkVersionCode(JSONObject appJson) throws JSONException{ if (!appJson.isNull(Constants.JSON_MIN_VERSION_CODE)) { int minAppVersionCode = appJson.getInt(Constants.JSON_MIN_VERSION_CODE); + // If no config found, assume version check is enabled + Boolean versionCheckEnabled = appJson.has(Constants.JSON_ENABLE_VERSION_CHECK) ? appJson.getBoolean(Constants.JSON_ENABLE_VERSION_CHECK) : true; + if (!versionCheckEnabled) { + return false; + } + + // If no config found, assume force update = false + Boolean forceUpdateEnabled = appJson.has(Constants.JSON_FORCE_ALERT_TYPE) ? appJson.getBoolean(Constants.JSON_FORCE_ALERT_TYPE) : false; //save last successful verification date getSirenHelper().setLastVerificationDate(mApplicationContext); if (getSirenHelper().getVersionCode(mApplicationContext) < minAppVersionCode && !getSirenHelper().isVersionSkippedByUser(mApplicationContext, String.valueOf(minAppVersionCode))) { - showAlert(String.valueOf(minAppVersionCode), versionCodeUpdateAlertType); + showAlert(String.valueOf(minAppVersionCode), forceUpdateEnabled ? SirenAlertType.FORCE : versionCodeUpdateAlertType); return true; } } From 674d7496fedca10e9d6540721d8adc8cd73b4090 Mon Sep 17 00:00:00 2001 From: Sarah Thian Date: Thu, 5 Oct 2017 14:47:42 +0800 Subject: [PATCH 2/5] add unit tests --- .../com/eggheadgames/siren/SirenTest.java | 23 +++++++++++++++++++ .../com/eggheadgames/siren/TestConstants.java | 2 ++ 2 files changed, 25 insertions(+) diff --git a/library/src/test/java/com/eggheadgames/siren/SirenTest.java b/library/src/test/java/com/eggheadgames/siren/SirenTest.java index 4eb2b93..19440d5 100644 --- a/library/src/test/java/com/eggheadgames/siren/SirenTest.java +++ b/library/src/test/java/com/eggheadgames/siren/SirenTest.java @@ -295,4 +295,27 @@ public void onVersionUpdateWithFreshInstall_shouldNotFireAllertIfVersionUpToDate Mockito.verify(listener, Mockito.never()).onDetectNewVersionWithoutAlert(Mockito.anyString()); } + + @Test + public void onVersionCheckDisabled_shouldNotDoVersionCheck() { + mockResult(TestConstants.jsonVersionCheckDisabled); + Mockito.when(sirenHelper.getVersionName(activity)).thenReturn(TestConstants.appVersionNameTest); + + ISirenListener listener = Mockito.mock(ISirenListener.class); + siren.setSirenListener(listener); + siren.setMajorUpdateAlertType(SirenAlertType.NONE); + siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); + + Mockito.verify(listener, Mockito.never()).onDetectNewVersionWithoutAlert(Mockito.anyString()); + } + + @Test + public void onForceUpdateEnabled_shouldShowForceAlertType() { + mockResult(TestConstants.jsonForceUpdateEnabled); + Mockito.when(sirenHelper.getVersionName(activity)).thenReturn(TestConstants.appVersionNameTest); + + siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL); + + Mockito.verify(siren).getAlertWrapper(eq(SirenAlertType.FORCE), Mockito.anyString()); + } } \ No newline at end of file diff --git a/library/src/test/java/com/eggheadgames/siren/TestConstants.java b/library/src/test/java/com/eggheadgames/siren/TestConstants.java index fde831f..db8936e 100644 --- a/library/src/test/java/com/eggheadgames/siren/TestConstants.java +++ b/library/src/test/java/com/eggheadgames/siren/TestConstants.java @@ -22,4 +22,6 @@ interface TestConstants { String jsonMalformed2 = "{}"; String jsonMalformed3 = "{\"com.example.app\":{\"someField\":\"1.2.1.1\"}}"; + String jsonVersionCheckDisabled = "{\"com.example.app\":{\"minVersionName\":\"2.1.1.1\", \"enable\": false}}"; + String jsonForceUpdateEnabled = "{\"com.example.app\":{\"minVersionName\":\"2.1.1.1\", \"force\": true}}"; } From 917c52fd038e8e8b9fbf36453889fa3a69e3ec9c Mon Sep 17 00:00:00 2001 From: Sarah Thian Date: Fri, 6 Oct 2017 00:41:05 +0800 Subject: [PATCH 3/5] add break to switch default --- library/src/main/java/com/eggheadgames/siren/Siren.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/eggheadgames/siren/Siren.java b/library/src/main/java/com/eggheadgames/siren/Siren.java index f39a629..8b177e6 100644 --- a/library/src/main/java/com/eggheadgames/siren/Siren.java +++ b/library/src/main/java/com/eggheadgames/siren/Siren.java @@ -213,7 +213,7 @@ private boolean checkVersionName(JSONObject appJson) throws JSONException{ case 1: alertType = minorUpdateAlertType; break; case 2: alertType = patchUpdateAlertType; break; case 3: alertType = revisionUpdateAlertType; break; - default: alertType = SirenAlertType.OPTION; + default: alertType = SirenAlertType.OPTION; break; } } break; From 3ab87f8df9a34d4b2785e4b435b43c348acfbb7b Mon Sep 17 00:00:00 2001 From: Sarah Thian Date: Fri, 6 Oct 2017 00:47:12 +0800 Subject: [PATCH 4/5] update documentation on new json parameters --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index ac45985..b4bb8cd 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,18 @@ OR { "com.example.app": { "minVersionCode": 7 } } ``` +Parameters supported on the JSON document: + +- **minVersionName**: The minimum version name required. +- **minVersionCode**: The minimum version code required, minVersionName will take precendence if both specified. +- **enable**: A boolean flag to remotely toggle the version check feature. +- **force**: A boolean flag to remotely set alertType as FORCE on every type of update. + +Example: +```json +{ "com.example.app": { "minVersionCode": 7, "enable": true, "force": false } } +``` + ## Options The **SirenVersionCheckType** controls how often the server is checked for a new version, and hence how often the user will be prompted. You can set it to `IMMEDIATELY`, `DAILY` or `WEEKLY`. From 4f4df36e13bc80fa2c19f8f5ccc6a2df2a10d9be Mon Sep 17 00:00:00 2001 From: mikemee Date: Sat, 18 Nov 2017 03:02:55 -0800 Subject: [PATCH 5/5] Release version 1.5.0 --- library/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/build.gradle b/library/build.gradle index d4ecc7d..b5a23b8 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -11,8 +11,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 25 - versionCode 10 - versionName "1.4.9" + versionCode 11 + versionName "1.5.0" } buildTypes { release {