@@ -172,6 +172,33 @@ public void deleteDocument(String documentId) throws FileNotFoundException {
172172 }
173173 }
174174
175+ @ Override
176+ public String renameDocument (String documentId , String displayName ) throws FileNotFoundException {
177+ File file = getFileForDocId (documentId );
178+ File parent = file .getParentFile ();
179+ if (parent == null ) {
180+ throw new FileNotFoundException ("Failed to rename root document with id " + documentId );
181+ }
182+ if (displayName == null || displayName .trim ().isEmpty ()) {
183+ throw new FileNotFoundException ("Failed to rename document with id " + documentId );
184+ }
185+ if (displayName .equals (file .getName ())) {
186+ return documentId ;
187+ }
188+ if (displayName .contains (File .separator )) {
189+ throw new FileNotFoundException ("Invalid display name for rename: " + displayName );
190+ }
191+
192+ File target = new File (parent , displayName );
193+ if (target .exists ()) {
194+ throw new FileNotFoundException ("Target already exists: " + target .getAbsolutePath ());
195+ }
196+ if (!file .renameTo (target )) {
197+ throw new FileNotFoundException ("Failed to rename document with id " + documentId );
198+ }
199+ return getDocIdForFile (target );
200+ }
201+
175202 @ Override
176203 public String getDocumentType (String documentId ) throws FileNotFoundException {
177204 File file = getFileForDocId (documentId );
@@ -258,6 +285,7 @@ private void includeFile(MatrixCursor result, String docId, File file) throws Fi
258285 File parentFile = file .getParentFile ();
259286 if (parentFile != null && parentFile .canWrite ()) {
260287 flags = flags | DocumentsContract .Document .FLAG_SUPPORTS_DELETE ;
288+ flags = flags | DocumentsContract .Document .FLAG_SUPPORTS_RENAME ;
261289 }
262290
263291 String displayName = file .getName ();
@@ -336,4 +364,4 @@ private static String getMimeType(File file) {
336364 return "application/octet-stream" ;
337365 }
338366 }
339- }
367+ }
0 commit comments