| 710 | } |
| 711 | |
| 712 | private void rename( |
| 713 | String filename, |
| 714 | String newFile, |
| 715 | CallbackContext callback |
| 716 | ) { |
| 717 | cordova |
| 718 | .getThreadPool() |
| 719 | .execute( |
| 720 | new Runnable() { |
| 721 | public void run() { |
| 722 | String srcUri = null, docId = null; |
| 723 | Uri fileUri = null; |
| 724 | if (filename.contains(SEPARATOR)) { |
| 725 | String splittedStr[] = filename.split(SEPARATOR, 2); |
| 726 | srcUri = splittedStr[0]; |
| 727 | docId = splittedStr[1]; |
| 728 | fileUri = getUri(srcUri, docId); |
| 729 | } else { |
| 730 | srcUri = filename; |
| 731 | fileUri = Uri.parse(filename); |
| 732 | } |
| 733 | |
| 734 | try { |
| 735 | DocumentFile file = DocumentFile.fromTreeUri(context, fileUri); |
| 736 | // If only case change, OS adds '(<number>)' as suffix, to avoid that we need to rename to a temporary name first |
| 737 | if (newFile.equalsIgnoreCase(file.getName())) { |
| 738 | file.renameTo(newFile + "_temp"); |
| 739 | } |
| 740 | |
| 741 | if (file.renameTo(newFile)) { |
| 742 | String name = file.getName(); |
| 743 | docId = DocumentsContract.getDocumentId(file.getUri()); |
| 744 | callback.success(srcUri + SEPARATOR + docId); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | callback.error("Unable to rename: " + filename); |
| 749 | } catch (Exception e) { |
| 750 | callback.error(e.getMessage()); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | ); |
| 755 | } |
| 756 | |
| 757 | private void delete(String filename, CallbackContext callback) { |
| 758 | final ContentResolver contentResolver = context.getContentResolver(); |