ErrorMessageAdapter.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* ownCloud Android client application
  2. * Copyright (C) 2014 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.utils;
  18. import java.io.File;
  19. import java.net.SocketTimeoutException;
  20. import org.apache.commons.httpclient.ConnectTimeoutException;
  21. import android.content.res.Resources;
  22. import com.owncloud.android.R;
  23. import com.owncloud.android.lib.common.operations.RemoteOperation;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  26. import com.owncloud.android.operations.CreateFolderOperation;
  27. import com.owncloud.android.operations.CreateShareOperation;
  28. import com.owncloud.android.operations.DownloadFileOperation;
  29. import com.owncloud.android.operations.RemoveFileOperation;
  30. import com.owncloud.android.operations.RenameFileOperation;
  31. import com.owncloud.android.operations.SynchronizeFileOperation;
  32. import com.owncloud.android.operations.UnshareLinkOperation;
  33. import com.owncloud.android.operations.UploadFileOperation;
  34. /**
  35. * Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
  36. *
  37. * @author masensio
  38. *
  39. */
  40. public class ErrorMessageAdapter {
  41. public ErrorMessageAdapter() {
  42. }
  43. public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) {
  44. String message = null;
  45. if (operation instanceof UploadFileOperation) {
  46. if (result.isSuccess()) {
  47. message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
  48. ((UploadFileOperation) operation).getFileName());
  49. } else {
  50. if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
  51. || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
  52. message = String.format(res.getString(R.string.error__upload__local_file_not_copied),
  53. ((UploadFileOperation) operation).getFileName(),
  54. res.getString(R.string.app_name));
  55. } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
  56. message = res.getString(R.string.failed_upload_quota_exceeded_text);
  57. } else {
  58. message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
  59. ((UploadFileOperation) operation).getFileName());
  60. }
  61. }
  62. } else if (operation instanceof DownloadFileOperation) {
  63. if (result.isSuccess()) {
  64. message = String.format(res.getString(R.string.downloader_download_succeeded_content),
  65. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  66. } else {
  67. message = String.format(res.getString(R.string.downloader_download_failed_content),
  68. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  69. }
  70. } else if (operation instanceof RemoveFileOperation) {
  71. if (result.isSuccess()) {
  72. message = res.getString(R.string.remove_success_msg);
  73. } else {
  74. if (isNetworkError(result.getCode())) {
  75. message = getErrorMessage(result, res);
  76. } else {
  77. message = res.getString(R.string.remove_fail_msg);
  78. }
  79. }
  80. } else if (operation instanceof RenameFileOperation) {
  81. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  82. message = res.getString(R.string.rename_local_fail_msg);
  83. } if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
  84. message = res.getString(R.string.filename_forbidden_characters);
  85. } else if (isNetworkError(result.getCode())) {
  86. message = getErrorMessage(result, res);
  87. } else {
  88. message = res.getString(R.string.rename_server_fail_msg);
  89. }
  90. } else if (operation instanceof SynchronizeFileOperation) {
  91. if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
  92. message = res.getString(R.string.sync_file_nothing_to_do_msg);
  93. }
  94. } else if (operation instanceof CreateFolderOperation) {
  95. if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
  96. message = res.getString(R.string.filename_forbidden_characters);
  97. } else if (isNetworkError(result.getCode())) {
  98. message = getErrorMessage(result, res);
  99. } else {
  100. message = res.getString(R.string.create_dir_fail_msg);
  101. }
  102. } else if (operation instanceof CreateShareOperation) {
  103. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  104. message = res.getString(R.string.share_link_file_no_exist);
  105. } else if (isNetworkError(result.getCode())) {
  106. message = getErrorMessage(result, res);
  107. } else { // Generic error
  108. // Show a Message, operation finished without success
  109. message = res.getString(R.string.share_link_file_error);
  110. }
  111. } else if (operation instanceof UnshareLinkOperation) {
  112. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  113. message = res.getString(R.string.unshare_link_file_no_exist);
  114. } else if (isNetworkError(result.getCode())) {
  115. message = getErrorMessage(result, res);
  116. } else { // Generic error
  117. // Show a Message, operation finished without success
  118. message = res.getString(R.string.unshare_link_file_error);
  119. }
  120. }
  121. return message;
  122. }
  123. private static String getErrorMessage(RemoteOperationResult result , Resources res) {
  124. String message = null;
  125. if (!result.isSuccess()) {
  126. if (result.getCode() == ResultCode.WRONG_CONNECTION) {
  127. message = res.getString(R.string.network_error_socket_exception);
  128. } else if (result.getCode() == ResultCode.TIMEOUT) {
  129. message = res.getString(R.string.network_error_socket_exception);
  130. if (result.getException() instanceof SocketTimeoutException) {
  131. message = res.getString(R.string.network_error_socket_timeout_exception);
  132. } else if(result.getException() instanceof ConnectTimeoutException) {
  133. message = res.getString(R.string.network_error_connect_timeout_exception);
  134. }
  135. } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
  136. message = res.getString(R.string.network_host_not_available);
  137. }
  138. }
  139. return message;
  140. }
  141. private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
  142. if (code == ResultCode.WRONG_CONNECTION ||
  143. code == ResultCode.TIMEOUT ||
  144. code == ResultCode.HOST_NOT_AVAILABLE) {
  145. return true;
  146. }
  147. else
  148. return false;
  149. }
  150. }