-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
apiVersioning and developerContact implementation for AnkiDroid functions call from WebView #6521
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
bc30c1d
Revert "Fork Update"
krmanik 6a3339c
Update fork
krmanik c8181c3
Merge branch 'master' of https://github.com/ankidroid/Anki-Android
krmanik 0d37a81
Merge branch 'master' of https://github.com/ankidroid/Anki-Android
krmanik 6184bef
api versioning
krmanik 7254e0b
Updated AbstractFlashcardViewer.java, 02-strings.xml
krmanik 939e6c1
Added Vesrion Compare using Comparable
krmanik 638512e
Added Java SemVer for JS API
krmanik ef8cdcb
Updated AbstractFlashcardViewer.java, 02-strings.xml
krmanik 474b616
Updated 02-strings.xml
krmanik d8b7c41
Updated 02-strings.xml
krmanik 6544a1e
Updated 02-strings.xml
krmanik e3571aa
Merge branch 'master' into js-api-version
krmanik 75dc239
Updated 02-strings.xml
krmanik e68e5a5
Updated AbstractFlashcardViewer.java
krmanik fe482d7
Updated AbstractFlashcardViewer.java
krmanik 80079d9
Handle when API not initialised
krmanik e2f068a
Update JS API
krmanik 401cddc
Update js api
krmanik 1399b90
Merge branch 'master' of https://github.com/ankidroid/Anki-Android in…
krmanik 7204912
Update 02-strings.xml
krmanik 793063e
Update AbstractFlashcardViewer.java
krmanik c055f95
init js api
krmanik 6a2b548
Updated
krmanik c0ff6c3
Updated
krmanik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |||||
|
|
||||||
| import androidx.annotation.CheckResult; | ||||||
| import androidx.annotation.IdRes; | ||||||
| import androidx.annotation.IntDef; | ||||||
| import androidx.annotation.NonNull; | ||||||
| import androidx.annotation.Nullable; | ||||||
| import androidx.annotation.VisibleForTesting; | ||||||
|
|
@@ -84,6 +85,8 @@ | |||||
|
|
||||||
| import com.afollestad.materialdialogs.MaterialDialog; | ||||||
| import com.afollestad.materialdialogs.util.TypefaceHelper; | ||||||
| import com.google.android.material.snackbar.Snackbar; | ||||||
| import com.google.gson.Gson; | ||||||
| import com.ichi2.anim.ActivityTransitionAnimation; | ||||||
| import com.ichi2.anim.ViewAnimation; | ||||||
| import com.ichi2.anki.dialogs.TagsDialog; | ||||||
|
|
@@ -123,9 +126,12 @@ | |||||
| import java.io.FileOutputStream; | ||||||
| import java.io.IOException; | ||||||
| import java.io.UnsupportedEncodingException; | ||||||
| import java.lang.annotation.Retention; | ||||||
| import java.lang.annotation.RetentionPolicy; | ||||||
| import java.lang.ref.WeakReference; | ||||||
| import java.net.URLDecoder; | ||||||
| import java.util.ArrayList; | ||||||
| import java.util.HashMap; | ||||||
| import java.util.HashSet; | ||||||
| import java.util.LinkedHashSet; | ||||||
| import java.util.Set; | ||||||
|
|
@@ -142,6 +148,8 @@ | |||||
| import static com.ichi2.anki.reviewer.CardMarker.*; | ||||||
| import static com.ichi2.async.CollectionTask.TASK_TYPE.*; | ||||||
|
|
||||||
| import com.github.zafarkhaja.semver.Version; | ||||||
|
|
||||||
| @SuppressWarnings({"PMD.AvoidThrowingRawExceptionTypes","PMD.FieldDeclarationsShouldBeAtStartOfClass"}) | ||||||
| public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity implements ReviewerUi, CommandProcessor { | ||||||
|
|
||||||
|
|
@@ -189,6 +197,20 @@ public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity i | |||||
|
|
||||||
| // ETA | ||||||
| private int eta; | ||||||
| // JavaScript Versioning | ||||||
| protected String mCardSuppliedApiVersion = ""; | ||||||
| protected String mCardSuppliedDeveloperContact = ""; | ||||||
|
david-allison marked this conversation as resolved.
Outdated
david-allison marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| private static final String sCurrentJsApiVersion = "0.0.1"; | ||||||
| private static final String sMinimumJsApiVersion = "0.0.1"; | ||||||
|
|
||||||
| // JS API ERROR CODE | ||||||
| private static final int ankiJsErrorCodeDefault = 0; | ||||||
| private static final int ankiJsErrorCodeMarkCard = 1; | ||||||
| private static final int ankiJsErrorCodeFlagCard = 2; | ||||||
|
|
||||||
| // JS api list enable/disable status | ||||||
| private HashMap<String, Boolean> mJsApiListMap = new HashMap<String, Boolean>(); | ||||||
|
david-allison marked this conversation as resolved.
|
||||||
|
|
||||||
| private boolean isInFullscreen; | ||||||
|
|
||||||
|
|
@@ -3189,11 +3211,28 @@ private boolean filterUrl(String url) { | |||||
| } | ||||||
| // mark card using javascript | ||||||
| if (url.startsWith("signal:mark_current_card")) { | ||||||
| executeCommand(COMMAND_MARK); | ||||||
| if (isAnkiApiNull("markCard")) { | ||||||
| showDeveloperContact(ankiJsErrorCodeDefault); | ||||||
| return true; | ||||||
| } else if (mJsApiListMap.get("markCard")) { | ||||||
|
david-allison marked this conversation as resolved.
|
||||||
| executeCommand(COMMAND_MARK); | ||||||
| } else { | ||||||
| // see 02-string.xml | ||||||
| showDeveloperContact(ankiJsErrorCodeMarkCard); | ||||||
| } | ||||||
| return true; | ||||||
| } | ||||||
| // flag card (blue, green, orange, red) using javascript from AnkiDroid webview | ||||||
| if (url.startsWith("signal:flag_")) { | ||||||
| if (isAnkiApiNull("toggleFlag")) { | ||||||
| showDeveloperContact(ankiJsErrorCodeDefault); | ||||||
| return true; | ||||||
| } else if (!mJsApiListMap.get("toggleFlag")) { | ||||||
| // see 02-string.xml | ||||||
| showDeveloperContact(ankiJsErrorCodeFlagCard); | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| String mFlag = url.replaceFirst("signal:flag_",""); | ||||||
| switch (mFlag) { | ||||||
| case "none": executeCommand(COMMAND_UNSET_FLAG); | ||||||
|
|
@@ -3450,6 +3489,81 @@ void handleUrlFromJavascript(String url) { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Check if value null | ||||||
| private boolean isAnkiApiNull(String api) { | ||||||
| if (mJsApiListMap.get(api) == null) { | ||||||
|
david-allison marked this conversation as resolved.
Outdated
|
||||||
| return true; | ||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| * 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) { | ||||||
|
david-allison marked this conversation as resolved.
|
||||||
| String errorMsg = getString(R.string.anki_js_error_code, errorCode); | ||||||
|
|
||||||
| View parentLayout = findViewById(android.R.id.content); | ||||||
| String snackbarMsg; | ||||||
| snackbarMsg = getString(R.string.api_version_developer_contact, mCardSuppliedDeveloperContact, errorMsg); | ||||||
|
|
||||||
| Snackbar snackbar = Snackbar.make(parentLayout, snackbarMsg, Snackbar.LENGTH_LONG); | ||||||
| View snackbarView = snackbar.getView(); | ||||||
| TextView snackTextView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text); | ||||||
| snackTextView.setTextColor(Color.WHITE); | ||||||
| snackTextView.setMaxLines(3); | ||||||
|
|
||||||
| snackbar.setActionTextColor(Color.MAGENTA) | ||||||
| .setAction(getString(R.string.reviewer_invalid_api_version_visit_documentation), view -> { | ||||||
| openUrl(Uri.parse("https://github.com/ankidroid/Anki-Android/wiki")); | ||||||
| }); | ||||||
|
|
||||||
| snackbar.show(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Supplied api version must be equal to current api version to call mark card, toggle flag functions etc. | ||||||
| */ | ||||||
| private boolean requireApiVersion(String apiVer, String apiDevContact) { | ||||||
| try { | ||||||
|
|
||||||
| if (TextUtils.isEmpty(apiDevContact)) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| Version mVersionCurrent = Version.valueOf(sCurrentJsApiVersion); | ||||||
|
david-allison marked this conversation as resolved.
|
||||||
| 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 (mVersionSupplied.equals(mVersionCurrent)) { | ||||||
| return true; | ||||||
|
david-allison marked this conversation as resolved.
|
||||||
| } else if (mVersionSupplied.lessThan(mVersionCurrent)) { | ||||||
| UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_version, mCardSuppliedDeveloperContact), false); | ||||||
|
|
||||||
| if (mVersionSupplied.greaterThanOrEqualTo(Version.valueOf(sMinimumJsApiVersion))) { | ||||||
| return true; | ||||||
| } else { | ||||||
| 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"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| @VisibleForTesting | ||||||
| void loadInitialCard() { | ||||||
| CollectionTask.launchCollectionTask(ANSWER_CARD, mAnswerCardHandler(false), | ||||||
|
|
@@ -3498,6 +3612,49 @@ protected void showTagsDialog() { | |||||
| */ | ||||||
| public class JavaScriptFunction { | ||||||
|
|
||||||
| private final Gson sGson = new Gson(); | ||||||
|
mikehardy marked this conversation as resolved.
Outdated
|
||||||
| // list of api that can be accessed | ||||||
| private final String[] sApiList = {"toggleFlag", "markCard"}; | ||||||
|
|
||||||
| // api disabled when valid api version not provided | ||||||
| private void disableJsApi() { | ||||||
| for (int i = 0; i < sApiList.length; i++) { | ||||||
| mJsApiListMap.put(sApiList[i], false); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // if supplied api version match then enable api | ||||||
| private void enableJsApi() { | ||||||
| for (int i = 0; i < sApiList.length; i++) { | ||||||
| mJsApiListMap.put(sApiList[i], true); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| @JavascriptInterface | ||||||
| public String init(String jsonData) { | ||||||
| JSONObject data; | ||||||
| String apiStatusJson = ""; | ||||||
|
|
||||||
| try { | ||||||
| data = new JSONObject(jsonData); | ||||||
| if (!(data == JSONObject.NULL)) { | ||||||
| mCardSuppliedApiVersion = data.optString("version", ""); | ||||||
| mCardSuppliedDeveloperContact = data.optString("developer", ""); | ||||||
|
|
||||||
| if (requireApiVersion(mCardSuppliedApiVersion, mCardSuppliedDeveloperContact)) { | ||||||
| enableJsApi(); | ||||||
| } else { | ||||||
| disableJsApi(); | ||||||
| } | ||||||
| apiStatusJson = sGson.toJson(mJsApiListMap); | ||||||
| } | ||||||
|
|
||||||
| } catch (JSONException j) { | ||||||
| UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.invalid_json_data, j.getLocalizedMessage()), false); | ||||||
| } | ||||||
| return String.valueOf(apiStatusJson); | ||||||
| } | ||||||
|
|
||||||
| @JavascriptInterface | ||||||
| public String ankiGetNewCardCount() { | ||||||
| return newCount.toString(); | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.