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

Added Support for Custom Message #43

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ You may want the update dialog to always appear in a certain language, ignoring
Siren.setLanguageLocalization(SirenSupportedLocales.FR)
```

## custom message from server.

You can send a custom message from the server side.

Response from server

```json
{ "com.example.app": { "minVersionName": "4.12.2","message" : "Please update as this version will stop working" } }
```

## Testing Siren

Change the url in your app to point to a test location (e.g. http://myjson.com/ is a convenient test site). Create an appropriate file and run your app with the temporary url.
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/java/com/eggheadgames/siren/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ final class Constants {

static final String JSON_FORCE_ALERT_TYPE = "force";

static final String JSON_MESSAGE = "message";

static final String JSON_ENABLE_VERSION_CHECK = "enable";
}
16 changes: 9 additions & 7 deletions library/src/main/java/com/eggheadgames/siren/Siren.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ protected void handleVerificationResults(String json) {
}

@VisibleForTesting
protected SirenAlertWrapper getAlertWrapper(SirenAlertType alertType, String appVersion) {
protected SirenAlertWrapper getAlertWrapper(SirenAlertType alertType, String appVersion, String message) {
Activity activity = mActivityRef.get();
return new SirenAlertWrapper(activity, mSirenListener, alertType, appVersion, forceLanguageLocalization, getSirenHelper());
return new SirenAlertWrapper(activity, mSirenListener, alertType, appVersion, forceLanguageLocalization, getSirenHelper(), message);
}

protected SirenHelper getSirenHelper() {
Expand All @@ -186,6 +186,7 @@ private boolean checkVersionName(JSONObject appJson) throws JSONException{

// 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 message = appJson.has(Constants.JSON_MESSAGE) ? appJson.getString(Constants.JSON_MESSAGE) : null;
String minVersionName = appJson.getString(Constants.JSON_MIN_VERSION_NAME);
String currentVersionName = getSirenHelper().getVersionName(mApplicationContext);

Expand Down Expand Up @@ -223,7 +224,7 @@ private boolean checkVersionName(JSONObject appJson) throws JSONException{
}

if (versionUpdateDetected) {
showAlert(minVersionName, alertType);
showAlert(minVersionName, alertType, message);
return true;
}
}
Expand Down Expand Up @@ -253,26 +254,27 @@ private boolean checkVersionCode(JSONObject appJson) throws JSONException{

// 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 message = appJson.has(Constants.JSON_MESSAGE) ? appJson.getString(Constants.JSON_MESSAGE) : null;

//save last successful verification date
getSirenHelper().setLastVerificationDate(mApplicationContext);

if (getSirenHelper().getVersionCode(mApplicationContext) < minAppVersionCode
&& !getSirenHelper().isVersionSkippedByUser(mApplicationContext, String.valueOf(minAppVersionCode))) {
showAlert(String.valueOf(minAppVersionCode), forceUpdateEnabled ? SirenAlertType.FORCE : versionCodeUpdateAlertType);
showAlert(String.valueOf(minAppVersionCode), forceUpdateEnabled ? SirenAlertType.FORCE : versionCodeUpdateAlertType, message);
return true;
}
}
return false;
}

private void showAlert(String appVersion, SirenAlertType alertType) {
private void showAlert(String appVersion, SirenAlertType alertType, String message) {
if (alertType == SirenAlertType.NONE) {
if (mSirenListener != null) {
mSirenListener.onDetectNewVersionWithoutAlert(getSirenHelper().getAlertMessage(mApplicationContext, appVersion, forceLanguageLocalization));
mSirenListener.onDetectNewVersionWithoutAlert(getSirenHelper().getAlertMessage(mApplicationContext, appVersion, forceLanguageLocalization, message));
}
} else {
getAlertWrapper(alertType, appVersion).show();
getAlertWrapper(alertType, appVersion, message).show();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ public class SirenAlertWrapper {
private final ISirenListener mSirenListener;
private final SirenAlertType mSirenAlertType;
private final String mMinAppVersion;
private final String mMessage;
private final SirenSupportedLocales mLocale;
private final SirenHelper mSirenHelper;

public SirenAlertWrapper(Activity activity, ISirenListener sirenListener, SirenAlertType sirenAlertType,
String minAppVersion, SirenSupportedLocales locale, SirenHelper sirenHelper) {
String minAppVersion, SirenSupportedLocales locale, SirenHelper sirenHelper, String mMessage) {
this.mSirenListener = sirenListener;
this.mSirenAlertType = sirenAlertType;
this.mMinAppVersion = minAppVersion;
this.mLocale = locale;
this.mSirenHelper = sirenHelper;
this.mActivityRef = new WeakReference<>(activity);
this.mMessage = mMessage;
}

public SirenAlertWrapper(Activity activity, ISirenListener sirenListener, SirenAlertType sirenAlertType,
String minAppVersion, SirenSupportedLocales locale, SirenHelper sirenHelper) {
this(activity, sirenListener, sirenAlertType, minAppVersion, locale, sirenHelper, null);
}

public void show() {
Activity activity = mActivityRef.get();
Expand Down Expand Up @@ -74,7 +80,7 @@ private void setupDialog(final AlertDialog dialog) {
nextTime.setText(mSirenHelper.getLocalizedString(mActivityRef.get(), R.string.next_time, mLocale));
skip.setText(mSirenHelper.getLocalizedString(mActivityRef.get(), R.string.skip_this_version, mLocale));

message.setText(mSirenHelper.getAlertMessage(mActivityRef.get(), mMinAppVersion, mLocale));
message.setText(mSirenHelper.getAlertMessage(mActivityRef.get(), mMinAppVersion, mLocale, mMessage));

if (mSirenAlertType == SirenAlertType.FORCE
|| mSirenAlertType == SirenAlertType.OPTION
Expand Down
8 changes: 8 additions & 0 deletions library/src/main/java/com/eggheadgames/siren/SirenHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ long getLastVerificationDate(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getLong(Constants.PREFERENCES_LAST_CHECK_DATE, 0);
}

@NonNull
String getAlertMessage(Context context, String minAppVersion, SirenSupportedLocales locale, String message) {
if (message != null && !message.isEmpty()){
return message;
}
return getAlertMessage(context,minAppVersion,locale);
}

@NonNull
String getAlertMessage(Context context, String minAppVersion, SirenSupportedLocales locale) {
try {
Expand Down
20 changes: 16 additions & 4 deletions library/src/test/java/com/eggheadgames/siren/SirenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void prepareTest() {
alertWrapper = Mockito.spy(new SirenAlertWrapper(null, null, null, null, null, null));
//Mock SirenHelper class
Mockito.when(sirenHelper.getAlertMessage(Mockito.any(Context.class), Mockito.anyString(), Mockito.any(SirenSupportedLocales.class))).thenReturn("");
Mockito.when(sirenHelper.getAlertMessage(Mockito.any(Context.class), Mockito.anyString(), Mockito.any(SirenSupportedLocales.class), Mockito.anyString())).thenReturn("");
Mockito.when(sirenHelper.getDaysSinceLastCheck(activity)).thenReturn(0);
Mockito.when(sirenHelper.getLocalizedString(Mockito.any(Context.class), Mockito.anyInt(), Mockito.any(SirenSupportedLocales.class))).thenReturn("");
Mockito.when(sirenHelper.getPackageName(activity)).thenReturn(TestConstants.appPackageName);
Expand Down Expand Up @@ -81,7 +82,7 @@ public Long answer(InvocationOnMock invocation) throws Throwable {
siren.mApplicationContext = activity;

Mockito.when(siren.getSirenHelper()).thenReturn(sirenHelper);
Mockito.doReturn(alertWrapper).when(siren).getAlertWrapper(Mockito.any(SirenAlertType.class), Mockito.anyString());
Mockito.doReturn(alertWrapper).when(siren).getAlertWrapper(Mockito.any(SirenAlertType.class), Mockito.anyString(), Mockito.anyString());

mockResult(TestConstants.jsonVersionNameMajorUpdate);
}
Expand Down Expand Up @@ -279,7 +280,7 @@ public void onVersionCodeUpdate_checkVersionCodeAlertType() {

siren.setVersionCodeUpdateAlertType(SirenAlertType.FORCE);
siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL);
Mockito.verify(siren).getAlertWrapper(eq(SirenAlertType.FORCE), Mockito.anyString());
Mockito.verify(siren).getAlertWrapper(eq(SirenAlertType.FORCE), Mockito.anyString(), Mockito.anyString());
}

@Test
Expand Down Expand Up @@ -316,6 +317,17 @@ public void onForceUpdateEnabled_shouldShowForceAlertType() {

siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL);

Mockito.verify(siren).getAlertWrapper(eq(SirenAlertType.FORCE), Mockito.anyString());
Mockito.verify(siren).getAlertWrapper(eq(SirenAlertType.FORCE), Mockito.anyString(), Mockito.anyString());
}
}

@Test
public void onMessageNode_shouldShowCustomMessage() {
mockResult(TestConstants.jsonCustomMessage);
Mockito.when(sirenHelper.getVersionName(activity)).thenReturn(TestConstants.appVersionNameTest);

siren.checkVersion(activity, SirenVersionCheckType.IMMEDIATELY, APP_DESCRIPTION_URL);

Mockito.verify(siren).getAlertWrapper(eq(SirenAlertType.OPTION), Mockito.anyString(), Mockito.anyString());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ interface TestConstants {

String jsonVersionCheckDisabled = "{\"com.example.app\":{\"minVersionName\":\"2.1.1.1\", \"enable\": false}}";
String jsonForceUpdateEnabled = "{\"com.example.app\":{\"minVersionName\":\"2.1.1.1\", \"force\": true}}";
String jsonCustomMessage = "{\"com.example.app\":{\"minVersionName\":\"2.1.1.1\", \"message\": \"This is custom message\"}}";
}