瀏覽代碼

Merge pull request #3436 from nextcloud/DocumentProviderRename

Document provider: allow rename
Andy Scherzinger 6 年之前
父節點
當前提交
51eba2df7a

+ 24 - 1
src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -58,6 +58,7 @@ import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.operations.CreateFolderOperation;
 import com.owncloud.android.operations.RefreshFolderOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
+import com.owncloud.android.operations.RenameFileOperation;
 import com.owncloud.android.operations.SynchronizeFileOperation;
 import com.owncloud.android.ui.activity.ConflictsResolveActivity;
 import com.owncloud.android.ui.activity.Preferences;
@@ -154,7 +155,7 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         }
 
         final FileCursor resultCursor = new FileCursor(projection);
-        
+
         for (OCFile file : currentStorageManager.getFolderContent(browsedDir, false)) {
             resultCursor.addFile(file);
         }
@@ -301,6 +302,28 @@ public class DocumentsStorageProvider extends DocumentsProvider {
                 AssetFileDescriptor.UNKNOWN_LENGTH);
     }
 
+    @Override
+    public String renameDocument(String documentId, String displayName) throws FileNotFoundException {
+        long docId = Long.parseLong(documentId);
+        updateCurrentStorageManagerIfNeeded(docId);
+
+        OCFile file = currentStorageManager.getFileById(docId);
+
+        if (file == null) {
+            throw new FileNotFoundException("File " + documentId + " not found!");
+        }
+
+        RemoteOperationResult result = new RenameFileOperation(file.getRemotePath(), displayName)
+            .execute(client, currentStorageManager);
+
+        if (!result.isSuccess()) {
+            throw new FileNotFoundException("Failed to rename document with documentId " + documentId + ": " +
+                                                result.getException());
+        }
+
+        return null;
+    }
+
     @Override
     public Cursor querySearchDocuments(String rootId, String query, String[] projection) {
         updateCurrentStorageManagerIfNeeded(rootId);

+ 4 - 0
src/main/java/org/nextcloud/providers/cursors/FileCursor.java

@@ -57,6 +57,10 @@ public class FileCursor extends MatrixCursor {
             flags = flags | Document.FLAG_DIR_SUPPORTS_CREATE;
         }
 
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            flags = Document.FLAG_SUPPORTS_RENAME | flags;
+        }
+
         newRow().add(Document.COLUMN_DOCUMENT_ID, Long.toString(file.getFileId()))
                 .add(Document.COLUMN_DISPLAY_NAME, file.getFileName())
                 .add(Document.COLUMN_LAST_MODIFIED, file.getModificationTimestamp())