ErrorMessageAdapter.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /*
  56. } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
  57. message = res.getString(R.string.failed_upload_quota_exceeded_text);
  58. */
  59. } else {
  60. message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
  61. ((UploadFileOperation) operation).getFileName());
  62. }
  63. }
  64. } else if (operation instanceof DownloadFileOperation) {
  65. if (result.isSuccess()) {
  66. message = String.format(res.getString(R.string.downloader_download_succeeded_content),
  67. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  68. } else {
  69. message = String.format(res.getString(R.string.downloader_download_failed_content),
  70. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  71. }
  72. } else if (operation instanceof RemoveFileOperation) {
  73. if (result.isSuccess()) {
  74. message = res.getString(R.string.remove_success_msg);
  75. } else {
  76. if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  77. // Error --> No permissions
  78. message = String.format(res.getString(R.string.forbidden_permissions),
  79. res.getString(R.string.forbidden_permissions_delete));
  80. } else if (isNetworkError(result.getCode())) {
  81. message = getErrorMessage(result, res);
  82. } else {
  83. message = res.getString(R.string.remove_fail_msg);
  84. }
  85. }
  86. } else if (operation instanceof RenameFileOperation) {
  87. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  88. message = res.getString(R.string.rename_local_fail_msg);
  89. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  90. // Error --> No permissions
  91. message = String.format(res.getString(R.string.forbidden_permissions),
  92. res.getString(R.string.forbidden_permissions_rename));
  93. } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
  94. message = res.getString(R.string.filename_forbidden_characters);
  95. } else if (isNetworkError(result.getCode())) {
  96. message = getErrorMessage(result, res);
  97. } else {
  98. message = res.getString(R.string.rename_server_fail_msg);
  99. }
  100. } else if (operation instanceof SynchronizeFileOperation) {
  101. if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
  102. message = res.getString(R.string.sync_file_nothing_to_do_msg);
  103. }
  104. } else if (operation instanceof CreateFolderOperation) {
  105. if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
  106. message = res.getString(R.string.filename_forbidden_characters);
  107. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  108. message = String.format(res.getString(R.string.forbidden_permissions),
  109. res.getString(R.string.forbidden_permissions_create));
  110. } else if (isNetworkError(result.getCode())) {
  111. message = getErrorMessage(result, res);
  112. } else {
  113. message = res.getString(R.string.create_dir_fail_msg);
  114. }
  115. } else if (operation instanceof CreateShareOperation) {
  116. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  117. message = res.getString(R.string.share_link_file_no_exist);
  118. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  119. // Error --> No permissions
  120. message = String.format(res.getString(R.string.forbidden_permissions),
  121. res.getString(R.string.share_link_forbidden_permissions));
  122. } else if (isNetworkError(result.getCode())) {
  123. message = getErrorMessage(result, res);
  124. } else { // Generic error
  125. // Show a Message, operation finished without success
  126. message = res.getString(R.string.share_link_file_error);
  127. }
  128. } else if (operation instanceof UnshareLinkOperation) {
  129. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  130. message = res.getString(R.string.unshare_link_file_no_exist);
  131. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  132. // Error --> No permissions
  133. message = String.format(res.getString(R.string.forbidden_permissions),
  134. res.getString(R.string.unshare_link_forbidden_permissions));
  135. } else if (isNetworkError(result.getCode())) {
  136. message = getErrorMessage(result, res);
  137. } else { // Generic error
  138. // Show a Message, operation finished without success
  139. message = res.getString(R.string.unshare_link_file_error);
  140. }
  141. }
  142. return message;
  143. }
  144. private static String getErrorMessage(RemoteOperationResult result , Resources res) {
  145. String message = null;
  146. if (!result.isSuccess()) {
  147. if (result.getCode() == ResultCode.WRONG_CONNECTION) {
  148. message = res.getString(R.string.network_error_socket_exception);
  149. } else if (result.getCode() == ResultCode.TIMEOUT) {
  150. message = res.getString(R.string.network_error_socket_exception);
  151. if (result.getException() instanceof SocketTimeoutException) {
  152. message = res.getString(R.string.network_error_socket_timeout_exception);
  153. } else if(result.getException() instanceof ConnectTimeoutException) {
  154. message = res.getString(R.string.network_error_connect_timeout_exception);
  155. }
  156. } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
  157. message = res.getString(R.string.network_host_not_available);
  158. }
  159. }
  160. return message;
  161. }
  162. private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
  163. if (code == ResultCode.WRONG_CONNECTION ||
  164. code == ResultCode.TIMEOUT ||
  165. code == ResultCode.HOST_NOT_AVAILABLE) {
  166. return true;
  167. }
  168. else
  169. return false;
  170. }
  171. }