Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional JSON params for server control of update prompts #26

Merged
merged 5 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it will add more readability if we will use constants instead of "magic numbers".
ie INDEX_MAJOR = 0 ... INDEX_REVISION = 3

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create #30

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a small improvement, we can move the logic of getting Constants.JSON_ENABLE_VERSION_CHECK value to separate method that returns boolean.

Because we have duplication of the logic in the code.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #30

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here it's possible to do the same as with Constants.JSON_ENABLE_VERSION_CHECK logic. But for Constants.JSON_FORCE_ALERT_TYPE

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #30


//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}}";
}