Skip to content

Commit

Permalink
Updated AbstractFlashcardViewer.java, 02-strings.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
krmanik committed Jul 1, 2020
1 parent 638512e commit ef8cdcb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 30 deletions.
113 changes: 84 additions & 29 deletions AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity i
protected String mCardSuppliedApiVersion = "";
protected String mCardSuppliedDeveloperContact = "";

private static final String sCurrentJsApiVersion = "1.0.0";
private static final String sCurrentJsApiVersion = "0.0.1";
private static final String sMinimumJsApiVersion = "0.0.1";

// JS api list enable/disable status
private HashMap<String, Boolean> mJsApiListMap = new HashMap<String, Boolean>();

Expand Down Expand Up @@ -3203,14 +3205,16 @@ private boolean filterUrl(String url) {
if (mJsApiListMap.get("markCard")) {
executeCommand(COMMAND_MARK);
} else {
showDeveloperContact();
// see 02-string.xml
showDeveloperContact(1);
}
return true;
}
// flag card (blue, green, orange, red) using javascript from AnkiDroid webview
if (url.startsWith("signal:flag_")) {
if (!mJsApiListMap.get("toggleFlag")) {
showDeveloperContact();
// see 02-string.xml
showDeveloperContact(2);
return true;
}

Expand Down Expand Up @@ -3470,15 +3474,31 @@ void handleUrlFromJavascript(String url) {
}
}

// show developer contact if js api used in card is deprecated
private void showDeveloperContact() {
/*
* see 02-strings.xml
* Show Error code when mark card or flag card unsupported
* 1 - mark card
* 2 - flag card
*
* show developer contact if js api used in card is deprecated
*/
private void showDeveloperContact(int errorCode) {
String errorMsg = "";
switch (errorCode) {
case 1:
errorMsg = getString(R.string.anki_js_mark_card_not_supported);
break;
case 2:
errorMsg = getString(R.string.anki_js_flag_card_not_supported);
break;
default:
errorMsg = getString(R.string.anki_js_unknown_error);
break;
}

View parentLayout = findViewById(android.R.id.content);
String snackbarMsg;
if (TextUtils.isEmpty(mCardSuppliedDeveloperContact)) {
snackbarMsg = getString(R.string.api_version_no_developer_contact);
} else {
snackbarMsg = getString(R.string.api_version_developer_contact, mCardSuppliedDeveloperContact );
}
snackbarMsg = getString(R.string.api_version_developer_contact, mCardSuppliedDeveloperContact, errorMsg);

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

Version mVersionCurrent = Version.valueOf(sCurrentJsApiVersion);
Version mVersionSupplied = Version.valueOf(apiVer);
if (TextUtils.isEmpty(apiDevContact)) {
return false;
}

if (mVersionCurrent.equals(mVersionSupplied)) {
return true;
} else if (mVersionCurrent.greaterThan(mVersionSupplied)) {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_version, sCurrentJsApiVersion), false);
return false;
} else {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, sCurrentJsApiVersion), false);
return false;
Version mVersionCurrent = Version.valueOf(sCurrentJsApiVersion);
Version mVersionSupplied = Version.valueOf(apiVer);

/*
* if api major version equals to supplied major version then return true and also check for minor version and patch version
* show toast for update and contact developer if need updates
* otherwise return false
*/
if (mVersionCurrent.equals(mVersionSupplied)) {
return true;
} else if (mVersionCurrent.getMajorVersion() == mVersionSupplied.getMajorVersion()) {

if (mVersionCurrent.getMinorVersion() == mVersionSupplied.getMinorVersion()) {

if (mVersionCurrent.getPatchVersion() > mVersionSupplied.getPatchVersion()) {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_patch_version, mCardSuppliedDeveloperContact), false);
return true;
} else {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, mCardSuppliedDeveloperContact), false);
return false;
}

} else if (mVersionCurrent.getMinorVersion() > mVersionSupplied.getMinorVersion()) {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_minor_version, mCardSuppliedDeveloperContact), false);
return true;
} else {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, mCardSuppliedDeveloperContact), false);
return false;
}

} else if (mVersionCurrent.greaterThan(mVersionSupplied)) {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_version, mCardSuppliedDeveloperContact), false);
return false;
} else {
UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, mCardSuppliedDeveloperContact), false);
return false;
}
} catch (Exception e) {
Timber.i(e, "requireApiVersion::exception");
}
return false;
}

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

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

@JavascriptInterface
public String init(String jsonData) {
preInit();

JSONObject data;
try {
data = new JSONObject(jsonData);
Expand All @@ -3596,13 +3648,16 @@ public String init(String jsonData) {
}

String apiStatusJson = "";
if (TextUtils.isEmpty(mCardSuppliedApiVersion) && TextUtils.isEmpty(mCardSuppliedDeveloperContact)) {
apiStatusJson = sGson.toJson(mJsApiListMap);
return String.valueOf(apiStatusJson);
} else if (requireApiVersion(mCardSuppliedApiVersion)) {

if (requireApiVersion(mCardSuppliedApiVersion, mCardSuppliedDeveloperContact)) {
enableJsApi();
apiStatusJson = sGson.toJson(mJsApiListMap);
} else {
disableJsApi();
apiStatusJson = sGson.toJson(mJsApiListMap);
return String.valueOf(apiStatusJson);
}

return String.valueOf(apiStatusJson);
}

Expand Down
14 changes: 13 additions & 1 deletion AnkiDroid/src/main/res/values/02-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,21 @@
<!-- in options menu & Navigation Drawer -->
<string name="ankidroid_turn_on_fullscreen_nav_drawer">Please turn on fullscreen to use Show Navigation Drawer using JavaScript.</string>
<string name="ankidroid_turn_on_fullscreen_options_menu">Please turn on fullscreen to use Show Options Menu using JavaScript.</string>

<string name="api_version_developer_contact">This card uses unsupported AnkiDroid features. Contact developer %s, or view the wiki. %s</string>
<string name="invalid_json_data">Card provided invalid data. %s</string>
<string name="valid_js_api_version">Invalid AnkiDroid JS API version. Contact developer %s, or view wiki</string>
<string name="update_js_api_version">AnkiDroid JS API update available. Contact developer %s, or view wiki</string>
<string name="reviewer_invalid_api_version_visit_documentation">View</string>
<string name="update_js_api_patch_version">AnkiDroid JS API patch update available. Contact developer %s, or view wiki.</string>
<string name="update_js_api_minor_version">AnkiDroid JS API minor update available. Contact developer %s, or view wiki.</string>

<!-- Whiteboard save image message in Reviewer -->
<string name="white_board_image_save_failed">Failed to save whiteboard image. %s</string>
<string name="white_board_image_saved">Whiteboard image saved to %s</string>

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

0 comments on commit ef8cdcb

Please sign in to comment.