From d45786a006255351cafb35abcd91a7063174972f Mon Sep 17 00:00:00 2001 From: Raunak Raj <71929976+bajrangCoder@users.noreply.github.com> Date: Sat, 4 Oct 2025 08:57:26 +0530 Subject: [PATCH] Add support for renaming documents in provider Implements the renameDocument method in AlpineDocumentProvider, allowing documents to be renamed with validation and error handling. Also updates document flags to indicate rename support. --- .../src/android/AlpineDocumentProvider.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/plugins/terminal/src/android/AlpineDocumentProvider.java b/src/plugins/terminal/src/android/AlpineDocumentProvider.java index 38fdc6712..f69e58315 100644 --- a/src/plugins/terminal/src/android/AlpineDocumentProvider.java +++ b/src/plugins/terminal/src/android/AlpineDocumentProvider.java @@ -172,6 +172,33 @@ public void deleteDocument(String documentId) throws FileNotFoundException { } } + @Override + public String renameDocument(String documentId, String displayName) throws FileNotFoundException { + File file = getFileForDocId(documentId); + File parent = file.getParentFile(); + if (parent == null) { + throw new FileNotFoundException("Failed to rename root document with id " + documentId); + } + if (displayName == null || displayName.trim().isEmpty()) { + throw new FileNotFoundException("Failed to rename document with id " + documentId); + } + if (displayName.equals(file.getName())) { + return documentId; + } + if (displayName.contains(File.separator)) { + throw new FileNotFoundException("Invalid display name for rename: " + displayName); + } + + File target = new File(parent, displayName); + if (target.exists()) { + throw new FileNotFoundException("Target already exists: " + target.getAbsolutePath()); + } + if (!file.renameTo(target)) { + throw new FileNotFoundException("Failed to rename document with id " + documentId); + } + return getDocIdForFile(target); + } + @Override public String getDocumentType(String documentId) throws FileNotFoundException { File file = getFileForDocId(documentId); @@ -258,6 +285,7 @@ private void includeFile(MatrixCursor result, String docId, File file) throws Fi File parentFile = file.getParentFile(); if (parentFile != null && parentFile.canWrite()) { flags = flags | DocumentsContract.Document.FLAG_SUPPORTS_DELETE; + flags = flags | DocumentsContract.Document.FLAG_SUPPORTS_RENAME; } String displayName = file.getName(); @@ -336,4 +364,4 @@ private static String getMimeType(File file) { return "application/octet-stream"; } } -} \ No newline at end of file +}