|
78 | 78 | import java.io.InputStream; |
79 | 79 | import java.io.OutputStream; |
80 | 80 | import java.io.IOException; |
| 81 | +import java.io.BufferedReader; |
| 82 | +import java.io.InputStreamReader; |
81 | 83 |
|
82 | 84 | import android.os.Build; |
83 | 85 | import android.os.Environment; |
@@ -169,6 +171,8 @@ public boolean execute( |
169 | 171 | case "decode": |
170 | 172 | case "encode": |
171 | 173 | case "copyToUri": |
| 174 | + case "compare-file-text": |
| 175 | + case "compare-texts": |
172 | 176 | break; |
173 | 177 | case "get-configuration": |
174 | 178 | getConfiguration(callbackContext); |
@@ -505,6 +509,12 @@ public void run() { |
505 | 509 | case "encode": |
506 | 510 | encode(arg1, arg2, callbackContext); |
507 | 511 | break; |
| 512 | + case "compare-file-text": |
| 513 | + compareFileText(arg1, arg2, arg3, callbackContext); |
| 514 | + break; |
| 515 | + case "compare-texts": |
| 516 | + compareTexts(arg1, arg2, callbackContext); |
| 517 | + break; |
508 | 518 | default: |
509 | 519 | break; |
510 | 520 | } |
@@ -616,6 +626,146 @@ private void encode( |
616 | 626 | } |
617 | 627 | } |
618 | 628 |
|
| 629 | + /** |
| 630 | + * Compares file content with provided text. |
| 631 | + * This method runs in a background thread to avoid blocking the UI. |
| 632 | + * |
| 633 | + * @param fileUri The URI of the file to read (file:// or content://) |
| 634 | + * @param encoding The character encoding to use when reading the file |
| 635 | + * @param currentText The text to compare against the file content |
| 636 | + * @param callback Returns 1 if texts are different, 0 if same |
| 637 | + */ |
| 638 | + private void compareFileText( |
| 639 | + String fileUri, |
| 640 | + String encoding, |
| 641 | + String currentText, |
| 642 | + CallbackContext callback |
| 643 | + ) { |
| 644 | + try { |
| 645 | + if (fileUri == null || fileUri.isEmpty()) { |
| 646 | + callback.error("File URI is required"); |
| 647 | + return; |
| 648 | + } |
| 649 | + |
| 650 | + if (encoding == null || encoding.isEmpty()) { |
| 651 | + encoding = "UTF-8"; |
| 652 | + } |
| 653 | + |
| 654 | + if (!Charset.isSupported(encoding)) { |
| 655 | + callback.error("Charset not supported: " + encoding); |
| 656 | + return; |
| 657 | + } |
| 658 | + |
| 659 | + Uri uri = Uri.parse(fileUri); |
| 660 | + Charset charset = Charset.forName(encoding); |
| 661 | + String fileContent; |
| 662 | + |
| 663 | + // Handle file:// URIs |
| 664 | + if ("file".equalsIgnoreCase(uri.getScheme())) { |
| 665 | + File file = new File(uri.getPath()); |
| 666 | + |
| 667 | + // Validate file |
| 668 | + if (!file.exists()) { |
| 669 | + callback.error("File does not exist"); |
| 670 | + return; |
| 671 | + } |
| 672 | + if (!file.isFile()) { |
| 673 | + callback.error("Path is not a file"); |
| 674 | + return; |
| 675 | + } |
| 676 | + if (!file.canRead()) { |
| 677 | + callback.error("File is not readable"); |
| 678 | + return; |
| 679 | + } |
| 680 | + |
| 681 | + Path path = file.toPath(); |
| 682 | + fileContent = new String(Files.readAllBytes(path), charset); |
| 683 | + |
| 684 | + } else { |
| 685 | + // Handle content:// URIs |
| 686 | + InputStream inputStream = null; |
| 687 | + try { |
| 688 | + inputStream = context.getContentResolver().openInputStream(uri); |
| 689 | + |
| 690 | + if (inputStream == null) { |
| 691 | + callback.error("Cannot open file"); |
| 692 | + return; |
| 693 | + } |
| 694 | + |
| 695 | + StringBuilder sb = new StringBuilder(); |
| 696 | + try (BufferedReader reader = new BufferedReader( |
| 697 | + new InputStreamReader(inputStream, charset))) { |
| 698 | + char[] buffer = new char[8192]; |
| 699 | + int charsRead; |
| 700 | + while ((charsRead = reader.read(buffer)) != -1) { |
| 701 | + sb.append(buffer, 0, charsRead); |
| 702 | + } |
| 703 | + } |
| 704 | + fileContent = sb.toString(); |
| 705 | + |
| 706 | + } finally { |
| 707 | + if (inputStream != null) { |
| 708 | + try { |
| 709 | + inputStream.close(); |
| 710 | + } catch (IOException ignored) {} |
| 711 | + } |
| 712 | + } |
| 713 | + } |
| 714 | + |
| 715 | + // check length first |
| 716 | + if (fileContent.length() != currentText.length()) { |
| 717 | + callback.success(1); // Changed |
| 718 | + return; |
| 719 | + } |
| 720 | + |
| 721 | + // Full comparison |
| 722 | + if (fileContent.equals(currentText)) { |
| 723 | + callback.success(0); // Not changed |
| 724 | + } else { |
| 725 | + callback.success(1); // Changed |
| 726 | + } |
| 727 | + |
| 728 | + } catch (Exception e) { |
| 729 | + callback.error(e.toString()); |
| 730 | + } |
| 731 | + } |
| 732 | + |
| 733 | + /** |
| 734 | + * Compares two text strings. |
| 735 | + * This method runs in a background thread to avoid blocking the UI |
| 736 | + * for large string comparisons. |
| 737 | + * |
| 738 | + * @param text1 First text to compare |
| 739 | + * @param text2 Second text to compare |
| 740 | + * @param callback Returns 1 if texts are different, 0 if same |
| 741 | + */ |
| 742 | + private void compareTexts( |
| 743 | + String text1, |
| 744 | + String text2, |
| 745 | + CallbackContext callback |
| 746 | + ) { |
| 747 | + try { |
| 748 | + if (text1 == null) text1 = ""; |
| 749 | + if (text2 == null) text2 = ""; |
| 750 | + |
| 751 | + // check length first |
| 752 | + if (text1.length() != text2.length()) { |
| 753 | + callback.success(1); // Changed |
| 754 | + return; |
| 755 | + } |
| 756 | + |
| 757 | + // Full comparison |
| 758 | + if (text1.equals(text2)) { |
| 759 | + callback.success(0); // Not changed |
| 760 | + } else { |
| 761 | + callback.success(1); // Changed |
| 762 | + } |
| 763 | + |
| 764 | + } catch (Exception e) { |
| 765 | + callback.error(e.toString()); |
| 766 | + } |
| 767 | + } |
| 768 | + |
619 | 769 | private void getAvailableEncodings(CallbackContext callback) { |
620 | 770 | try { |
621 | 771 | Map < String, Charset > charsets = Charset.availableCharsets(); |
|
0 commit comments