Эх сурвалжийг харах

Document provider: allow copy/move of file/folder
only on >= 5.0

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>

tobiasKaminsky 6 жил өмнө
parent
commit
3ca95252c4

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

@@ -56,7 +56,9 @@ import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.operations.CopyFileOperation;
 import com.owncloud.android.operations.CreateFolderOperation;
 import com.owncloud.android.operations.CreateFolderOperation;
+import com.owncloud.android.operations.MoveFileOperation;
 import com.owncloud.android.operations.RefreshFolderOperation;
 import com.owncloud.android.operations.RefreshFolderOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.operations.RemoveFileOperation;
 import com.owncloud.android.operations.RenameFileOperation;
 import com.owncloud.android.operations.RenameFileOperation;
@@ -146,7 +148,13 @@ public class DocumentsStorageProvider extends DocumentsProvider {
 
 
         Account account = currentStorageManager.getAccount();
         Account account = currentStorageManager.getAccount();
 
 
-        if (Device.getNetworkType(getContext()).equals(JobRequest.NetworkType.UNMETERED)) {
+        Context context = getContext();
+
+        if (context == null) {
+            throw new FileNotFoundException("Context may not be null");
+        }
+
+        if (Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
             RemoteOperationResult result = new RefreshFolderOperation(browsedDir, System.currentTimeMillis(), false,
             RemoteOperationResult result = new RefreshFolderOperation(browsedDir, System.currentTimeMillis(), false,
                                                                       false, true, currentStorageManager, account,
                                                                       false, true, currentStorageManager, account,
                                                                       getContext()).execute(client);
                                                                       getContext()).execute(client);
@@ -326,6 +334,82 @@ public class DocumentsStorageProvider extends DocumentsProvider {
         return null;
         return null;
     }
     }
 
 
+    @Override
+    public String copyDocument(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
+        long sourceId = Long.parseLong(sourceDocumentId);
+        long targetId = Long.parseLong(targetParentDocumentId);
+        updateCurrentStorageManagerIfNeeded(sourceId);
+
+        OCFile file = currentStorageManager.getFileById(sourceId);
+        OCFile targetFolder = currentStorageManager.getFileById(targetId);
+
+        if (file == null) {
+            throw new FileNotFoundException("File " + sourceDocumentId + " not found!");
+        }
+
+        if (targetFolder == null) {
+            throw new FileNotFoundException("File " + targetParentDocumentId + " not found!");
+        }
+
+        RemoteOperationResult result = new CopyFileOperation(file.getRemotePath(), targetFolder.getRemotePath())
+            .execute(client, currentStorageManager);
+
+        if (!result.isSuccess()) {
+            throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId
+                                                + " to " + targetParentDocumentId);
+        }
+
+        Account account = currentStorageManager.getAccount();
+
+        RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder, System.currentTimeMillis(),
+                                                                        false, false, true, currentStorageManager,
+                                                                        account, getContext()).execute(client);
+
+        if (!updateParent.isSuccess()) {
+            throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId
+                                                + " to " + targetParentDocumentId);
+        }
+
+        String newPath = targetFolder.getRemotePath() + file.getFileName();
+
+        if (file.isFolder()) {
+            newPath = newPath + "/";
+        }
+        OCFile newFile = currentStorageManager.getFileByPath(newPath);
+
+        return String.valueOf(newFile.getFileId());
+    }
+
+    @Override
+    public String moveDocument(String sourceDocumentId, String sourceParentDocumentId, String targetParentDocumentId)
+        throws FileNotFoundException {
+        long sourceId = Long.parseLong(sourceDocumentId);
+        long targetId = Long.parseLong(targetParentDocumentId);
+        updateCurrentStorageManagerIfNeeded(sourceId);
+
+        OCFile file = currentStorageManager.getFileById(sourceId);
+
+        if (file == null) {
+            throw new FileNotFoundException("File " + sourceDocumentId + " not found!");
+        }
+
+        OCFile targetFolder = currentStorageManager.getFileById(targetId);
+
+        if (targetFolder == null) {
+            throw new FileNotFoundException("File " + targetParentDocumentId + " not found!");
+        }
+
+        RemoteOperationResult result = new MoveFileOperation(file.getRemotePath(), targetFolder.getRemotePath())
+            .execute(client, currentStorageManager);
+
+        if (!result.isSuccess()) {
+            throw new FileNotFoundException("Failed to move document with documentId " + sourceDocumentId
+                                                + " to " + targetParentDocumentId);
+        }
+
+        return String.valueOf(file.getFileId());
+    }
+
     @Override
     @Override
     public Cursor querySearchDocuments(String rootId, String query, String[] projection) {
     public Cursor querySearchDocuments(String rootId, String query, String[] projection) {
         updateCurrentStorageManagerIfNeeded(rootId);
         updateCurrentStorageManagerIfNeeded(rootId);

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

@@ -53,6 +53,10 @@ public class FileCursor extends MatrixCursor {
             Document.FLAG_SUPPORTS_WRITE |
             Document.FLAG_SUPPORTS_WRITE |
             (imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0);
             (imagePath != null ? Document.FLAG_SUPPORTS_THUMBNAIL : 0);
 
 
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+            flags = Document.FLAG_SUPPORTS_COPY | Document.FLAG_SUPPORTS_MOVE | Document.FLAG_SUPPORTS_REMOVE | flags;
+        }
+
         if (file.isFolder()) {
         if (file.isFolder()) {
             flags = flags | Document.FLAG_DIR_SUPPORTS_CREATE;
             flags = flags | Document.FLAG_DIR_SUPPORTS_CREATE;
         }
         }