Skip to content

Commit

Permalink
Merge pull request #32 from eggheadgames/release-1-5-0
Browse files Browse the repository at this point in the history
Release 1.5.0 to master
  • Loading branch information
eggheadgames authored Nov 18, 2017
2 parents d5907fd + 4f4df36 commit 53eff89
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 18 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 10
versionName "1.4.9"
versionCode 11
versionName "1.5.0"
}
buildTypes {
release {
Expand Down
4 changes: 4 additions & 0 deletions library/src/main/java/com/eggheadgames/siren/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
54 changes: 38 additions & 16 deletions library/src/main/java/com/eggheadgames/siren/Siren.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; break;
}
}
} 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;
}
Expand All @@ -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;
}
}
Expand Down
23 changes: 23 additions & 0 deletions library/src/test/java/com/eggheadgames/siren/SirenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}";
}

0 comments on commit 53eff89

Please sign in to comment.