AndyScherzinger пре 7 година
родитељ
комит
508eacbc0b
1 измењених фајлова са 193 додато и 131 уклоњено
  1. 193 131
      src/main/java/com/owncloud/android/utils/ErrorMessageAdapter.java

+ 193 - 131
src/main/java/com/owncloud/android/utils/ErrorMessageAdapter.java

@@ -54,9 +54,6 @@ import java.net.SocketTimeoutException;
  * always following the same policy
  */
 public class ErrorMessageAdapter {
-
-    public ErrorMessageAdapter() { }
-
     /**
      * Return an internationalized user message corresponding to an operation result
      * and the operation performed.
@@ -114,188 +111,252 @@ public class ErrorMessageAdapter {
         String message = null;
 
         if (operation instanceof UploadFileOperation) {
+            message = getMessageForUploadFileOperation(result, (UploadFileOperation) operation, res);
 
-            if (result.isSuccess()) {
-                message = String.format(
-                        res.getString(R.string.uploader_upload_succeeded_content_single),
-                        ((UploadFileOperation) operation).getFileName());
-            } else {
-
-                if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
-                        || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
-                    message = String.format(
-                            res.getString(R.string.error__upload__local_file_not_copied),
-                            ((UploadFileOperation) operation).getFileName(),
-                            res.getString(R.string.app_name));
+        } else if (operation instanceof DownloadFileOperation) {
+            message = getMessageForDownloadFileOperation(result, (DownloadFileOperation) operation, res);
 
-                } else if (result.getCode() == ResultCode.FORBIDDEN) {
-                    message = String.format(res.getString(R.string.forbidden_permissions),
-                            res.getString(R.string.uploader_upload_forbidden_permissions));
+        } else if (operation instanceof RemoveFileOperation) {
+            message = getMessageForRemoveFileOperation(result, res);
 
-                } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
-                    message = res.getString(R.string.filename_forbidden_charaters_from_server);
+        } else if (operation instanceof RenameFileOperation) {
+            message = getMessageForRenameFileOperation(result, res);
 
-                }
+        } else if (operation instanceof SynchronizeFileOperation) {
+            if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
+                message = res.getString(R.string.sync_file_nothing_to_do_msg);
             }
 
-        } else if (operation instanceof DownloadFileOperation) {
+        } else if (operation instanceof CreateFolderOperation) {
+            message = getMessageForCreateFolderOperation(result, res);
 
-            if (result.isSuccess()) {
-                message = String.format(
-                        res.getString(R.string.downloader_download_succeeded_content),
-                        new File(((DownloadFileOperation) operation).getSavePath()).getName());
+        } else if (operation instanceof CreateShareViaLinkOperation ||
+                operation instanceof CreateShareWithShareeOperation) {
 
-            } else {
-                if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
-                    message = res.getString(R.string.downloader_download_file_not_found);
+            message = getMessageForCreateShareOperations(result, res);
 
-                }
-            }
+        } else if (operation instanceof UnshareOperation) {
 
-        } else if (operation instanceof RemoveFileOperation) {
-            if (result.isSuccess()) {
-                message = res.getString(R.string.remove_success_msg);
+            message = getMessageForUnshareOperation(result, res);
 
-            } else {
-                if (result.getCode().equals(ResultCode.FORBIDDEN)) {
-                    // Error --> No permissions
-                    message = String.format(res.getString(R.string.forbidden_permissions),
-                            res.getString(R.string.forbidden_permissions_delete));
-                }
-            }
+        } else if (operation instanceof UpdateShareViaLinkOperation ||
+                operation instanceof UpdateSharePermissionsOperation) {
 
-        } else if (operation instanceof RenameFileOperation) {
-            if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
-                message = res.getString(R.string.rename_local_fail_msg);
+            message = getMessageForUpdateShareOperations(result, res);
 
-            } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
-                // Error --> No permissions
-                message = String.format(res.getString(R.string.forbidden_permissions),
-                        res.getString(R.string.forbidden_permissions_rename));
+        } else if (operation instanceof MoveFileOperation) {
 
-            } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
-                message = res.getString(R.string.filename_forbidden_characters);
+            message = getMessageForMoveFileOperation(result, res);
 
-            } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
-                message = res.getString(R.string.filename_forbidden_charaters_from_server);
+        } else if (operation instanceof SynchronizeFolderOperation) {
 
-            }
+            message = getMessageForSynchronizeFolderOperation(result, (SynchronizeFolderOperation) operation, res);
 
-        } else if (operation instanceof SynchronizeFileOperation) {
-            if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
-                message = res.getString(R.string.sync_file_nothing_to_do_msg);
-            }
+        } else if (operation instanceof CopyFileOperation) {
+            message = getMessageForCopyFileOperation(result, res);
+        }
 
-        } else if (operation instanceof CreateFolderOperation) {
-            if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
-                message = res.getString(R.string.filename_forbidden_characters);
+        return message;
+    }
 
-            } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
-                message = String.format(res.getString(R.string.forbidden_permissions),
-                        res.getString(R.string.forbidden_permissions_create));
+    private static String getMessageForSynchronizeFolderOperation(
+            RemoteOperationResult result,
+            SynchronizeFolderOperation operation,
+            Resources res
+    ) {
+        if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND) {
+            return String.format(
+                    res.getString(R.string.sync_current_folder_was_removed),
+                    new File(operation.getFolderPath()).getName()
+            );
+        }
+        return
+                null;
+    }
 
-            } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
-                message = res.getString(R.string.filename_forbidden_charaters_from_server);
+    private static String getMessageForMoveFileOperation(RemoteOperationResult result, Resources res) {
+        if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
+            return res.getString(R.string.move_file_not_found);
+        } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
+            return res.getString(R.string.move_file_invalid_into_descendent);
 
-            }
+        } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
+            return res.getString(R.string.move_file_invalid_overwrite);
 
-        } else if (operation instanceof CreateShareViaLinkOperation ||
-                operation instanceof CreateShareWithShareeOperation) {
+        } else if (result.getCode() == ResultCode.FORBIDDEN) {
+            return String.format(res.getString(R.string.forbidden_permissions),
+                    res.getString(R.string.forbidden_permissions_move));
+
+        } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
+            return res.getString(R.string.filename_forbidden_charaters_from_server);
+        }
+        return null;
+    }
 
-            if (result.getData() != null && result.getData().size() > 0) {
-                message = (String) result.getData().get(0);     // share API sends its own error messages
+    private static String getMessageForUpdateShareOperations(RemoteOperationResult result, Resources res) {
+        if (result.getData() != null && result.getData().size() > 0) {
+            return (String) result.getData().get(0);     // share API sends its own error messages
 
-            } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND)  {
-                message = res.getString(R.string.share_link_file_no_exist);
+        } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
+            return res.getString(R.string.update_link_file_no_exist);
 
-            } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
-                // Error --> No permissions
-                message = String.format(res.getString(R.string.forbidden_permissions),
-                        res.getString(R.string.share_link_forbidden_permissions));
+        } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
+            // Error --> No permissions
+            return String.format(res.getString(R.string.forbidden_permissions),
+                    res.getString(R.string.update_link_forbidden_permissions));
 
-            }
+        }
+        return null;
+    }
 
-        } else if (operation instanceof UnshareOperation) {
+    private static String getMessageForUnshareOperation(RemoteOperationResult result, Resources res) {
+        if (result.getData() != null && result.getData().size() > 0) {
+            return (String) result.getData().get(0);     // share API sends its own error messages
 
-            if (result.getData() != null && result.getData().size() > 0) {
-                message = (String) result.getData().get(0);     // share API sends its own error messages
+        } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
+            return res.getString(R.string.unshare_link_file_no_exist);
 
-            } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
-                message = res.getString(R.string.unshare_link_file_no_exist);
+        } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
+            // Error --> No permissions
+            return String.format(res.getString(R.string.forbidden_permissions),
+                    res.getString(R.string.unshare_link_forbidden_permissions));
 
-            } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
-                // Error --> No permissions
-                message = String.format(res.getString(R.string.forbidden_permissions),
-                        res.getString(R.string.unshare_link_forbidden_permissions));
+        }
+        return null;
+    }
 
-            }
+    private static String getMessageForCopyFileOperation(RemoteOperationResult result, Resources res) {
+        if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
+            return res.getString(R.string.copy_file_not_found);
 
-        } else if (operation instanceof UpdateShareViaLinkOperation ||
-                operation instanceof UpdateSharePermissionsOperation) {
+        } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
+            return res.getString(R.string.copy_file_invalid_into_descendent);
 
-            if (result.getData() != null && result.getData().size() > 0) {
-                message = (String) result.getData().get(0);     // share API sends its own error messages
+        } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
+            return res.getString(R.string.copy_file_invalid_overwrite);
 
-            } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
-                message = res.getString(R.string.update_link_file_no_exist);
+        } else if (result.getCode() == ResultCode.FORBIDDEN) {
+            return String.format(res.getString(R.string.forbidden_permissions),
+                    res.getString(R.string.forbidden_permissions_copy));
 
-            } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
-                // Error --> No permissions
-                message = String.format(res.getString(R.string.forbidden_permissions),
-                        res.getString(R.string.update_link_forbidden_permissions));
+        }
+        return null;
+    }
 
-            }
+    private static String getMessageForCreateShareOperations(RemoteOperationResult result, Resources res) {
+        if (result.getData() != null && result.getData().size() > 0) {
+            return (String) result.getData().get(0);     // share API sends its own error messages
 
-        } else if (operation instanceof MoveFileOperation) {
+        } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND)  {
+            return res.getString(R.string.share_link_file_no_exist);
 
-            if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
-                message = res.getString(R.string.move_file_not_found);
-            } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
-                message = res.getString(R.string.move_file_invalid_into_descendent);
+        } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
+            // Error --> No permissions
+            return String.format(res.getString(R.string.forbidden_permissions),
+                    res.getString(R.string.share_link_forbidden_permissions));
 
-            } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
-                message = res.getString(R.string.move_file_invalid_overwrite);
+        }
+        return null;
+    }
 
-            } else if (result.getCode() == ResultCode.FORBIDDEN) {
-                message = String.format(res.getString(R.string.forbidden_permissions),
-                        res.getString(R.string.forbidden_permissions_move));
+    private static String getMessageForCreateFolderOperation(RemoteOperationResult result, Resources res) {
+        if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
+            return res.getString(R.string.filename_forbidden_characters);
 
-            } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
-                message = res.getString(R.string.filename_forbidden_charaters_from_server);
+        } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
+            return String.format(res.getString(R.string.forbidden_permissions),
+                    res.getString(R.string.forbidden_permissions_create));
 
-            }
+        } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
+            return res.getString(R.string.filename_forbidden_charaters_from_server);
 
-        } else if (operation instanceof SynchronizeFolderOperation) {
+        }
+        return null;
+    }
 
-            if (!result.isSuccess()) {
-                String folderPathName = new File(
-                        ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
-                if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
-                    message = String.format(
-                            res.getString(R.string.sync_current_folder_was_removed),
-                            folderPathName
-                    );
-                }
+    private static String getMessageForRenameFileOperation(RemoteOperationResult result, Resources res) {
+        if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
+            return res.getString(R.string.rename_local_fail_msg);
+
+        } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
+            // Error --> No permissions
+            return String.format(res.getString(R.string.forbidden_permissions),
+                    res.getString(R.string.forbidden_permissions_rename));
+
+        } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
+            return res.getString(R.string.filename_forbidden_characters);
+
+        } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
+            return res.getString(R.string.filename_forbidden_charaters_from_server);
+
+        }
+
+        return null;
+    }
+
+    private static String getMessageForRemoveFileOperation(RemoteOperationResult result, Resources res) {
+        if (result.isSuccess()) {
+            return res.getString(R.string.remove_success_msg);
+
+        } else {
+            if (result.getCode().equals(ResultCode.FORBIDDEN)) {
+                // Error --> No permissions
+                return String.format(res.getString(R.string.forbidden_permissions),
+                        res.getString(R.string.forbidden_permissions_delete));
             }
+        }
 
-        } else if (operation instanceof CopyFileOperation) {
+        return null;
+    }
+
+    private static String getMessageForDownloadFileOperation(
+            RemoteOperationResult result,
+            DownloadFileOperation operation,
+            Resources res
+    ) {
+        if (result.isSuccess()) {
+            return String.format(
+                    res.getString(R.string.downloader_download_succeeded_content),
+                    new File(operation.getSavePath()).getName());
+
+        } else {
             if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
-                message = res.getString(R.string.copy_file_not_found);
+                return res.getString(R.string.downloader_download_file_not_found);
 
-            } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
-                message = res.getString(R.string.copy_file_invalid_into_descendent);
+            }
+        }
+        return null;
+    }
 
-            } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
-                message = res.getString(R.string.copy_file_invalid_overwrite);
+    private static String getMessageForUploadFileOperation(
+            RemoteOperationResult result,
+            UploadFileOperation operation,
+            Resources res
+    ) {
+        if (result.isSuccess()) {
+            return String.format(
+                    res.getString(R.string.uploader_upload_succeeded_content_single),
+                    operation.getFileName());
+        } else {
+
+            if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
+                    || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
+                return String.format(
+                        res.getString(R.string.error__upload__local_file_not_copied),
+                        operation.getFileName(),
+                        res.getString(R.string.app_name));
 
             } else if (result.getCode() == ResultCode.FORBIDDEN) {
-                message = String.format(res.getString(R.string.forbidden_permissions),
-                        res.getString(R.string.forbidden_permissions_copy));
+                return String.format(res.getString(R.string.forbidden_permissions),
+                        res.getString(R.string.uploader_upload_forbidden_permissions));
+
+            } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
+                return res.getString(R.string.filename_forbidden_charaters_from_server);
 
             }
         }
 
-        return message;
+        return null;
     }
 
 
@@ -409,7 +470,8 @@ public class ErrorMessageAdapter {
         } else if (operation instanceof DownloadFileOperation) {
             message = String.format(
                     res.getString(R.string.downloader_download_failed_content),
-                    new File(((DownloadFileOperation) operation).getSavePath()).getName());
+                    new File(((DownloadFileOperation) operation).getSavePath()).getName()
+            );
 
         } else if (operation instanceof RemoveFileOperation) {
             message = res.getString(R.string.remove_fail_msg);