123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- package com.owncloud.android.utils;
- import android.content.res.Resources;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import com.owncloud.android.R;
- import com.owncloud.android.lib.common.operations.RemoteOperation;
- import com.owncloud.android.lib.common.operations.RemoteOperationResult;
- import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
- import com.owncloud.android.operations.CopyFileOperation;
- import com.owncloud.android.operations.CreateFolderOperation;
- import com.owncloud.android.operations.CreateShareViaLinkOperation;
- import com.owncloud.android.operations.CreateShareWithShareeOperation;
- import com.owncloud.android.operations.DownloadFileOperation;
- import com.owncloud.android.operations.MoveFileOperation;
- import com.owncloud.android.operations.RemoveFileOperation;
- import com.owncloud.android.operations.RenameFileOperation;
- import com.owncloud.android.operations.SynchronizeFileOperation;
- import com.owncloud.android.operations.SynchronizeFolderOperation;
- import com.owncloud.android.operations.UnshareOperation;
- import com.owncloud.android.operations.UpdateSharePermissionsOperation;
- import com.owncloud.android.operations.UpdateShareViaLinkOperation;
- import com.owncloud.android.operations.UploadFileOperation;
- import org.apache.commons.httpclient.ConnectTimeoutException;
- import java.io.File;
- import java.net.SocketTimeoutException;
- public final class ErrorMessageAdapter {
- private ErrorMessageAdapter() {
-
- }
-
- @NonNull
- public static String getErrorCauseMessage(
- RemoteOperationResult result,
- RemoteOperation operation,
- Resources res
- ) {
- String message = getSpecificMessageForResultAndOperation(result, operation, res);
- if (message == null || message.length() <= 0) {
- message = getCommonMessageForResult(result, res);
- }
- if (message == null || message.length() <= 0) {
- message = getGenericErrorMessageForOperation(operation, res);
- }
- if (message == null) {
- if (result.isSuccess()) {
- message = res.getString(R.string.common_ok);
- } else {
- message = res.getString(R.string.common_error_unknown);
- }
- }
- return message;
- }
-
- @Nullable
- private static String getSpecificMessageForResultAndOperation(
- RemoteOperationResult result,
- RemoteOperation operation,
- Resources res
- ) {
- String message = null;
- if (operation instanceof UploadFileOperation) {
- message = getMessageForUploadFileOperation(result, (UploadFileOperation) operation, res);
- } else if (operation instanceof DownloadFileOperation) {
- message = getMessageForDownloadFileOperation(result, (DownloadFileOperation) operation, res);
- } else if (operation instanceof RemoveFileOperation) {
- message = getMessageForRemoveFileOperation(result, res);
- } 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 CreateFolderOperation) {
- message = getMessageForCreateFolderOperation(result, res);
- } else if (operation instanceof CreateShareViaLinkOperation ||
- operation instanceof CreateShareWithShareeOperation) {
- message = getMessageForCreateShareOperations(result, res);
- } else if (operation instanceof UnshareOperation) {
- message = getMessageForUnshareOperation(result, res);
- } else if (operation instanceof UpdateShareViaLinkOperation ||
- operation instanceof UpdateSharePermissionsOperation) {
- message = getMessageForUpdateShareOperations(result, res);
- } else if (operation instanceof MoveFileOperation) {
- message = getMessageForMoveFileOperation(result, res);
- } else if (operation instanceof SynchronizeFolderOperation) {
- message = getMessageForSynchronizeFolderOperation(result, (SynchronizeFolderOperation) operation, res);
- } else if (operation instanceof CopyFileOperation) {
- message = getMessageForCopyFileOperation(result, res);
- }
- return message;
- }
- 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;
- }
- 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 (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;
- }
- private static String getMessageForUpdateShareOperations(RemoteOperationResult result, Resources res) {
- if (result.getData() != null && result.getData().size() > 0) {
- return (String) result.getData().get(0);
- } 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) {
-
- return String.format(res.getString(R.string.forbidden_permissions),
- res.getString(R.string.update_link_forbidden_permissions));
- }
- return null;
- }
- private static String getMessageForUnshareOperation(RemoteOperationResult result, Resources res) {
- if (result.getData() != null && result.getData().size() > 0) {
- return (String) result.getData().get(0);
- } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
- return res.getString(R.string.unshare_link_file_no_exist);
- } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
-
- return 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 (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
- return res.getString(R.string.copy_file_invalid_into_descendent);
- } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
- return res.getString(R.string.copy_file_invalid_overwrite);
- } else if (result.getCode() == ResultCode.FORBIDDEN) {
- return String.format(res.getString(R.string.forbidden_permissions),
- res.getString(R.string.forbidden_permissions_copy));
- }
- return null;
- }
- private static String getMessageForCreateShareOperations(RemoteOperationResult result, Resources res) {
- if (result.getData() != null && result.getData().size() > 0) {
- return (String) result.getData().get(0);
- } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
- return res.getString(R.string.share_link_file_no_exist);
- } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
-
- return String.format(res.getString(R.string.forbidden_permissions),
- res.getString(R.string.share_link_forbidden_permissions));
- }
- return null;
- }
- 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().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);
- }
- return null;
- }
- 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)) {
-
- 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)) {
-
- return String.format(res.getString(R.string.forbidden_permissions),
- res.getString(R.string.forbidden_permissions_delete));
- }
- }
- 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) {
- return res.getString(R.string.downloader_download_file_not_found);
- }
- }
- return null;
- }
- 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) {
- 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 null;
- }
-
- @Nullable
- private static String getCommonMessageForResult(RemoteOperationResult result, Resources res) {
- String message = null;
- if (!result.isSuccess()) {
- if (result.getCode() == ResultCode.WRONG_CONNECTION) {
- message = res.getString(R.string.network_error_socket_exception);
- } else if (result.getCode() == ResultCode.TIMEOUT) {
- message = res.getString(R.string.network_error_socket_exception);
- if (result.getException() instanceof SocketTimeoutException) {
- message = res.getString(R.string.network_error_socket_timeout_exception);
- } else if (result.getException() instanceof ConnectTimeoutException) {
- message = res.getString(R.string.network_error_connect_timeout_exception);
- }
- } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
- message = res.getString(R.string.network_host_not_available);
- } else if (result.getCode() == ResultCode.MAINTENANCE_MODE) {
- message = res.getString(R.string.maintenance_mode);
- } else if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) {
- message = res.getString(R.string.uploads_view_upload_status_failed_ssl_certificate_not_trusted);
- } else if (result.getCode() == ResultCode.BAD_OC_VERSION) {
- message = res.getString(R.string.auth_bad_oc_version_title);
- } else if (result.getCode() == ResultCode.INCORRECT_ADDRESS) {
- message = res.getString(R.string.auth_incorrect_address_title);
- } else if (result.getCode() == ResultCode.SSL_ERROR) {
- message = res.getString(R.string.auth_ssl_general_error_title);
- } else if (result.getCode() == ResultCode.UNAUTHORIZED) {
- message = res.getString(R.string.auth_unauthorized);
- } else if (result.getCode() == ResultCode.INSTANCE_NOT_CONFIGURED) {
- message = res.getString(R.string.auth_not_configured_title);
- } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
- message = res.getString(R.string.auth_incorrect_path_title);
- } else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
- message = res.getString(R.string.auth_oauth_error);
- } else if (result.getCode() == ResultCode.OAUTH2_ERROR_ACCESS_DENIED) {
- message = res.getString(R.string.auth_oauth_error_access_denied);
- } else if (result.getCode() == ResultCode.ACCOUNT_NOT_NEW) {
- message = res.getString(R.string.auth_account_not_new);
- } else if (result.getCode() == ResultCode.ACCOUNT_NOT_THE_SAME) {
- message = res.getString(R.string.auth_account_not_the_same);
- }
- else if (result.getHttpPhrase() != null && result.getHttpPhrase().length() > 0) {
-
- message = result.getHttpPhrase();
- }
- }
- return message;
- }
-
- @Nullable
- private static String getGenericErrorMessageForOperation(RemoteOperation operation, Resources res) {
- String message = null;
- if (operation instanceof UploadFileOperation) {
- message = String.format(
- res.getString(R.string.uploader_upload_failed_content_single),
- ((UploadFileOperation) operation).getFileName());
- } else if (operation instanceof DownloadFileOperation) {
- message = String.format(
- res.getString(R.string.downloader_download_failed_content),
- new File(((DownloadFileOperation) operation).getSavePath()).getName()
- );
- } else if (operation instanceof RemoveFileOperation) {
- message = res.getString(R.string.remove_fail_msg);
- } else if (operation instanceof RenameFileOperation) {
- message = res.getString(R.string.rename_server_fail_msg);
- } else if (operation instanceof CreateFolderOperation) {
- message = res.getString(R.string.create_dir_fail_msg);
- } else if (operation instanceof CreateShareViaLinkOperation ||
- operation instanceof CreateShareWithShareeOperation
- ) {
- message = res.getString(R.string.share_link_file_error);
- } else if (operation instanceof UnshareOperation) {
- message = res.getString(R.string.unshare_link_file_error);
- } else if (operation instanceof UpdateShareViaLinkOperation ||
- operation instanceof UpdateSharePermissionsOperation
- ) {
- message = res.getString(R.string.update_link_file_error);
- } else if (operation instanceof MoveFileOperation) {
- message = res.getString(R.string.move_file_error);
- } else if (operation instanceof SynchronizeFolderOperation) {
- String folderPathName = new File(
- ((SynchronizeFolderOperation) operation).getFolderPath()
- ).getName();
- message = String.format(res.getString(R.string.sync_folder_failed_content), folderPathName);
- } else if (operation instanceof CopyFileOperation) {
- message = res.getString(R.string.copy_file_error);
- }
- return message;
- }
- }
|