Skip to content

Commit ef8cdcb

Browse files
committed
Updated AbstractFlashcardViewer.java, 02-strings.xml
1 parent 638512e commit ef8cdcb

2 files changed

Lines changed: 97 additions & 30 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity i
197197
protected String mCardSuppliedApiVersion = "";
198198
protected String mCardSuppliedDeveloperContact = "";
199199

200-
private static final String sCurrentJsApiVersion = "1.0.0";
200+
private static final String sCurrentJsApiVersion = "0.0.1";
201+
private static final String sMinimumJsApiVersion = "0.0.1";
202+
201203
// JS api list enable/disable status
202204
private HashMap<String, Boolean> mJsApiListMap = new HashMap<String, Boolean>();
203205

@@ -3203,14 +3205,16 @@ private boolean filterUrl(String url) {
32033205
if (mJsApiListMap.get("markCard")) {
32043206
executeCommand(COMMAND_MARK);
32053207
} else {
3206-
showDeveloperContact();
3208+
// see 02-string.xml
3209+
showDeveloperContact(1);
32073210
}
32083211
return true;
32093212
}
32103213
// flag card (blue, green, orange, red) using javascript from AnkiDroid webview
32113214
if (url.startsWith("signal:flag_")) {
32123215
if (!mJsApiListMap.get("toggleFlag")) {
3213-
showDeveloperContact();
3216+
// see 02-string.xml
3217+
showDeveloperContact(2);
32143218
return true;
32153219
}
32163220

@@ -3470,15 +3474,31 @@ void handleUrlFromJavascript(String url) {
34703474
}
34713475
}
34723476

3473-
// show developer contact if js api used in card is deprecated
3474-
private void showDeveloperContact() {
3477+
/*
3478+
* see 02-strings.xml
3479+
* Show Error code when mark card or flag card unsupported
3480+
* 1 - mark card
3481+
* 2 - flag card
3482+
*
3483+
* show developer contact if js api used in card is deprecated
3484+
*/
3485+
private void showDeveloperContact(int errorCode) {
3486+
String errorMsg = "";
3487+
switch (errorCode) {
3488+
case 1:
3489+
errorMsg = getString(R.string.anki_js_mark_card_not_supported);
3490+
break;
3491+
case 2:
3492+
errorMsg = getString(R.string.anki_js_flag_card_not_supported);
3493+
break;
3494+
default:
3495+
errorMsg = getString(R.string.anki_js_unknown_error);
3496+
break;
3497+
}
3498+
34753499
View parentLayout = findViewById(android.R.id.content);
34763500
String snackbarMsg;
3477-
if (TextUtils.isEmpty(mCardSuppliedDeveloperContact)) {
3478-
snackbarMsg = getString(R.string.api_version_no_developer_contact);
3479-
} else {
3480-
snackbarMsg = getString(R.string.api_version_developer_contact, mCardSuppliedDeveloperContact );
3481-
}
3501+
snackbarMsg = getString(R.string.api_version_developer_contact, mCardSuppliedDeveloperContact, errorMsg);
34823502

34833503
Snackbar snackbar = Snackbar.make(parentLayout, snackbarMsg, Snackbar.LENGTH_LONG);
34843504
View snackbarView = snackbar.getView();
@@ -3497,20 +3517,54 @@ private void showDeveloperContact() {
34973517
/**
34983518
* Supplied api version must be equal to current api version to call mark card, toggle flag functions etc.
34993519
*/
3500-
private boolean requireApiVersion(String apiVer) {
3520+
private boolean requireApiVersion(String apiVer, String apiDevContact) {
3521+
try {
35013522

3502-
Version mVersionCurrent = Version.valueOf(sCurrentJsApiVersion);
3503-
Version mVersionSupplied = Version.valueOf(apiVer);
3523+
if (TextUtils.isEmpty(apiDevContact)) {
3524+
return false;
3525+
}
35043526

3505-
if (mVersionCurrent.equals(mVersionSupplied)) {
3506-
return true;
3507-
} else if (mVersionCurrent.greaterThan(mVersionSupplied)) {
3508-
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_version, sCurrentJsApiVersion), false);
3509-
return false;
3510-
} else {
3511-
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, sCurrentJsApiVersion), false);
3512-
return false;
3527+
Version mVersionCurrent = Version.valueOf(sCurrentJsApiVersion);
3528+
Version mVersionSupplied = Version.valueOf(apiVer);
3529+
3530+
/*
3531+
* if api major version equals to supplied major version then return true and also check for minor version and patch version
3532+
* show toast for update and contact developer if need updates
3533+
* otherwise return false
3534+
*/
3535+
if (mVersionCurrent.equals(mVersionSupplied)) {
3536+
return true;
3537+
} else if (mVersionCurrent.getMajorVersion() == mVersionSupplied.getMajorVersion()) {
3538+
3539+
if (mVersionCurrent.getMinorVersion() == mVersionSupplied.getMinorVersion()) {
3540+
3541+
if (mVersionCurrent.getPatchVersion() > mVersionSupplied.getPatchVersion()) {
3542+
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_patch_version, mCardSuppliedDeveloperContact), false);
3543+
return true;
3544+
} else {
3545+
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, mCardSuppliedDeveloperContact), false);
3546+
return false;
3547+
}
3548+
3549+
} else if (mVersionCurrent.getMinorVersion() > mVersionSupplied.getMinorVersion()) {
3550+
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_minor_version, mCardSuppliedDeveloperContact), false);
3551+
return true;
3552+
} else {
3553+
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, mCardSuppliedDeveloperContact), false);
3554+
return false;
3555+
}
3556+
3557+
} else if (mVersionCurrent.greaterThan(mVersionSupplied)) {
3558+
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_version, mCardSuppliedDeveloperContact), false);
3559+
return false;
3560+
} else {
3561+
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, mCardSuppliedDeveloperContact), false);
3562+
return false;
3563+
}
3564+
} catch (Exception e) {
3565+
Timber.i(e, "requireApiVersion::exception");
35133566
}
3567+
return false;
35143568
}
35153569

35163570
@VisibleForTesting
@@ -3565,8 +3619,8 @@ public class JavaScriptFunction {
35653619
// list of api that can be accessed
35663620
private final String[] sApiList = {"toggleFlag", "markCard"};
35673621

3568-
// initialize all api with disabled status
3569-
private void preInit() {
3622+
// api disabled when valid api version not provided
3623+
private void disableJsApi() {
35703624
for (int i = 0; i < sApiList.length; i++) {
35713625
mJsApiListMap.put(sApiList[i], false);
35723626
}
@@ -3581,8 +3635,6 @@ private void enableJsApi() {
35813635

35823636
@JavascriptInterface
35833637
public String init(String jsonData) {
3584-
preInit();
3585-
35863638
JSONObject data;
35873639
try {
35883640
data = new JSONObject(jsonData);
@@ -3596,13 +3648,16 @@ public String init(String jsonData) {
35963648
}
35973649

35983650
String apiStatusJson = "";
3599-
if (TextUtils.isEmpty(mCardSuppliedApiVersion) && TextUtils.isEmpty(mCardSuppliedDeveloperContact)) {
3600-
apiStatusJson = sGson.toJson(mJsApiListMap);
3601-
return String.valueOf(apiStatusJson);
3602-
} else if (requireApiVersion(mCardSuppliedApiVersion)) {
3651+
3652+
if (requireApiVersion(mCardSuppliedApiVersion, mCardSuppliedDeveloperContact)) {
36033653
enableJsApi();
36043654
apiStatusJson = sGson.toJson(mJsApiListMap);
3655+
} else {
3656+
disableJsApi();
3657+
apiStatusJson = sGson.toJson(mJsApiListMap);
3658+
return String.valueOf(apiStatusJson);
36053659
}
3660+
36063661
return String.valueOf(apiStatusJson);
36073662
}
36083663

AnkiDroid/src/main/res/values/02-strings.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,21 @@
274274
<!-- in options menu & Navigation Drawer -->
275275
<string name="ankidroid_turn_on_fullscreen_nav_drawer">Please turn on fullscreen to use Show Navigation Drawer using JavaScript.</string>
276276
<string name="ankidroid_turn_on_fullscreen_options_menu">Please turn on fullscreen to use Show Options Menu using JavaScript.</string>
277+
278+
<string name="api_version_developer_contact">This card uses unsupported AnkiDroid features. Contact developer %s, or view the wiki. %s</string>
279+
<string name="invalid_json_data">Card provided invalid data. %s</string>
280+
<string name="valid_js_api_version">Invalid AnkiDroid JS API version. Contact developer %s, or view wiki</string>
281+
<string name="update_js_api_version">AnkiDroid JS API update available. Contact developer %s, or view wiki</string>
282+
<string name="reviewer_invalid_api_version_visit_documentation">View</string>
283+
<string name="update_js_api_patch_version">AnkiDroid JS API patch update available. Contact developer %s, or view wiki.</string>
284+
<string name="update_js_api_minor_version">AnkiDroid JS API minor update available. Contact developer %s, or view wiki.</string>
285+
277286
<!-- Whiteboard save image message in Reviewer -->
278287
<string name="white_board_image_save_failed">Failed to save whiteboard image. %s</string>
279288
<string name="white_board_image_saved">Whiteboard image saved to %s</string>
280289

281-
<string name="title_whiteboard_pen_color">Pen color</string>
290+
<!-- Error codes for AnkiDroid JS API-->
291+
<string name="anki_js_unknown_error">(Error Code:0)</string>
292+
<string name="anki_js_mark_card_not_supported">(Error Code:1)</string>
293+
<string name="anki_js_flag_card_not_supported">(Error Code:2)</string>
282294
</resources>

0 commit comments

Comments
 (0)