|
144 | 144 | import static com.ichi2.anki.cardviewer.ViewerCommand.*; |
145 | 145 | import static com.ichi2.anki.reviewer.CardMarker.*; |
146 | 146 |
|
| 147 | +import com.github.zafarkhaja.semver.Version; |
| 148 | + |
147 | 149 | @SuppressWarnings({"PMD.AvoidThrowingRawExceptionTypes","PMD.FieldDeclarationsShouldBeAtStartOfClass"}) |
148 | 150 | public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity implements ReviewerUi, CommandProcessor { |
149 | 151 |
|
@@ -3492,85 +3494,17 @@ private void showDeveloperContact() { |
3492 | 3494 | snackbar.show(); |
3493 | 3495 | } |
3494 | 3496 |
|
3495 | | - /* |
3496 | | - * AnkiDroid JavaScript API Class for major, minor, patch version eg: 1.2.3 |
3497 | | - * major version : 1 |
3498 | | - * minor version : 2 |
3499 | | - * patch version : 3 |
3500 | | - * |
| 3497 | + /** |
3501 | 3498 | * Supplied api version must be equal to current api version to call mark card, toggle flag functions etc. |
3502 | | - * https://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java/27891752 |
3503 | 3499 | */ |
3504 | | - public class Version implements Comparable<Version> { |
3505 | | - |
3506 | | - private String version; |
3507 | | - |
3508 | | - public final String get() { |
3509 | | - return this.version; |
3510 | | - } |
3511 | | - |
3512 | | - public Version(String version) { |
3513 | | - if (version == null) { |
3514 | | - throw new IllegalArgumentException("Version can not be null"); |
3515 | | - } |
3516 | | - if (!version.matches("[0-9]+(\\.[0-9]+)*")) { |
3517 | | - UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.valid_js_api_version, sCurrentJsApiVersion), false); |
3518 | | - throw new IllegalArgumentException("Invalid version format"); |
3519 | | - } |
3520 | | - this.version = version; |
3521 | | - } |
3522 | | - |
3523 | | - @Override |
3524 | | - public int compareTo(Version that) { |
3525 | | - if (that == null) { |
3526 | | - return 1; |
3527 | | - } |
3528 | | - |
3529 | | - String[] thisParts = this.get().split("\\."); |
3530 | | - String[] thatParts = that.get().split("\\."); |
3531 | | - int length = Math.max(thisParts.length, thatParts.length); |
3532 | | - |
3533 | | - for (int i = 0; i < length; i++) { |
3534 | | - int thisPart = i < thisParts.length ? Integer.parseInt(thisParts[i]) : 0; |
3535 | | - int thatPart = i < thatParts.length ? Integer.parseInt(thatParts[i]) : 0; |
3536 | | - |
3537 | | - if (thisPart < thatPart) { |
3538 | | - return -1; |
3539 | | - } |
3540 | | - |
3541 | | - if (thisPart > thatPart) { |
3542 | | - return 1; |
3543 | | - } |
3544 | | - } |
3545 | | - return 0; |
3546 | | - } |
3547 | | - |
3548 | | - @Override |
3549 | | - public boolean equals(Object that) { |
3550 | | - if (this == that) { |
3551 | | - return true; |
3552 | | - } |
3553 | | - |
3554 | | - if (that == null) { |
3555 | | - return false; |
3556 | | - } |
3557 | | - |
3558 | | - if (this.getClass() != that.getClass()) { |
3559 | | - return false; |
3560 | | - } |
3561 | | - |
3562 | | - return this.compareTo((Version) that) == 0; |
3563 | | - } |
3564 | | - } |
3565 | | - |
3566 | 3500 | private boolean requireApiVersion(String apiVer) { |
3567 | 3501 |
|
3568 | | - Version mVersionCurrent = new Version(sCurrentJsApiVersion); |
3569 | | - Version mVersionSupplied = new Version(apiVer); |
| 3502 | + Version mVersionCurrent = Version.valueOf(sCurrentJsApiVersion); |
| 3503 | + Version mVersionSupplied = Version.valueOf(apiVer); |
3570 | 3504 |
|
3571 | 3505 | if (mVersionCurrent.equals(mVersionSupplied)) { |
3572 | 3506 | return true; |
3573 | | - } else if (mVersionCurrent.compareTo(mVersionSupplied) == 1) { // mCurrent > mSupplied |
| 3507 | + } else if (mVersionCurrent.greaterThan(mVersionSupplied)) { |
3574 | 3508 | UIUtils.showThemedToast(AbstractFlashcardViewer.this, getString(R.string.update_js_api_version, sCurrentJsApiVersion), false); |
3575 | 3509 | return false; |
3576 | 3510 | } else { |
|
0 commit comments