ErrorMessageAdapter.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.MoveFileOperation;
  30. import com.owncloud.android.operations.RemoveFileOperation;
  31. import com.owncloud.android.operations.RenameFileOperation;
  32. import com.owncloud.android.operations.SynchronizeFileOperation;
  33. import com.owncloud.android.operations.UnshareLinkOperation;
  34. import com.owncloud.android.operations.UploadFileOperation;
  35. /**
  36. * Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
  37. *
  38. * @author masensio
  39. *
  40. */
  41. public class ErrorMessageAdapter {
  42. public ErrorMessageAdapter() {
  43. }
  44. public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) {
  45. String message = null;
  46. if (operation instanceof UploadFileOperation) {
  47. if (result.isSuccess()) {
  48. message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
  49. ((UploadFileOperation) operation).getFileName());
  50. } else {
  51. if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
  52. || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
  53. message = String.format(res.getString(R.string.error__upload__local_file_not_copied),
  54. ((UploadFileOperation) operation).getFileName(),
  55. res.getString(R.string.app_name));
  56. /*
  57. } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
  58. message = res.getString(R.string.failed_upload_quota_exceeded_text);
  59. */
  60. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  61. message = String.format(res.getString(R.string.forbidden_permissions),
  62. res.getString(R.string.uploader_upload_forbidden_permissions));
  63. } else {
  64. message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
  65. ((UploadFileOperation) operation).getFileName());
  66. }
  67. }
  68. } else if (operation instanceof DownloadFileOperation) {
  69. if (result.isSuccess()) {
  70. message = String.format(res.getString(R.string.downloader_download_succeeded_content),
  71. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  72. } else {
  73. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  74. message = res.getString(R.string.downloader_download_file_not_found);
  75. } else {
  76. message = String.format(res.getString(R.string.downloader_download_failed_content), new File(
  77. ((DownloadFileOperation) operation).getSavePath()).getName());
  78. }
  79. }
  80. } else if (operation instanceof RemoveFileOperation) {
  81. if (result.isSuccess()) {
  82. message = res.getString(R.string.remove_success_msg);
  83. } else {
  84. if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  85. // Error --> No permissions
  86. message = String.format(res.getString(R.string.forbidden_permissions),
  87. res.getString(R.string.forbidden_permissions_delete));
  88. } else if (isNetworkError(result.getCode())) {
  89. message = getErrorMessage(result, res);
  90. } else {
  91. message = res.getString(R.string.remove_fail_msg);
  92. }
  93. }
  94. } else if (operation instanceof RenameFileOperation) {
  95. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  96. message = res.getString(R.string.rename_local_fail_msg);
  97. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  98. // Error --> No permissions
  99. message = String.format(res.getString(R.string.forbidden_permissions),
  100. res.getString(R.string.forbidden_permissions_rename));
  101. } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
  102. message = res.getString(R.string.filename_forbidden_characters);
  103. } else if (isNetworkError(result.getCode())) {
  104. message = getErrorMessage(result, res);
  105. } else {
  106. message = res.getString(R.string.rename_server_fail_msg);
  107. }
  108. } else if (operation instanceof SynchronizeFileOperation) {
  109. if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
  110. message = res.getString(R.string.sync_file_nothing_to_do_msg);
  111. }
  112. } else if (operation instanceof CreateFolderOperation) {
  113. if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
  114. message = res.getString(R.string.filename_forbidden_characters);
  115. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  116. message = String.format(res.getString(R.string.forbidden_permissions),
  117. res.getString(R.string.forbidden_permissions_create));
  118. } else if (isNetworkError(result.getCode())) {
  119. message = getErrorMessage(result, res);
  120. } else {
  121. message = res.getString(R.string.create_dir_fail_msg);
  122. }
  123. } else if (operation instanceof CreateShareOperation) {
  124. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  125. message = res.getString(R.string.share_link_file_no_exist);
  126. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  127. // Error --> No permissions
  128. message = String.format(res.getString(R.string.forbidden_permissions),
  129. res.getString(R.string.share_link_forbidden_permissions));
  130. } else if (isNetworkError(result.getCode())) {
  131. message = getErrorMessage(result, res);
  132. } else { // Generic error
  133. // Show a Message, operation finished without success
  134. message = res.getString(R.string.share_link_file_error);
  135. }
  136. } else if (operation instanceof UnshareLinkOperation) {
  137. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
  138. message = res.getString(R.string.unshare_link_file_no_exist);
  139. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  140. // Error --> No permissions
  141. message = String.format(res.getString(R.string.forbidden_permissions),
  142. res.getString(R.string.unshare_link_forbidden_permissions));
  143. } else if (isNetworkError(result.getCode())) {
  144. message = getErrorMessage(result, res);
  145. } else { // Generic error
  146. // Show a Message, operation finished without success
  147. message = res.getString(R.string.unshare_link_file_error);
  148. }
  149. } else if (operation instanceof MoveFileOperation) {
  150. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  151. message = res.getString(R.string.move_file_not_found);
  152. } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
  153. message = res.getString(R.string.move_file_invalid_into_descendent);
  154. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  155. message = res.getString(R.string.move_file_invalid_overwrite);
  156. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  157. message = String.format(res.getString(R.string.forbidden_permissions),
  158. res.getString(R.string.forbidden_permissions_move));
  159. }else { // Generic error
  160. // Show a Message, operation finished without success
  161. message = res.getString(R.string.move_file_error);
  162. }
  163. }
  164. return message;
  165. }
  166. private static String getErrorMessage(RemoteOperationResult result , Resources res) {
  167. String message = null;
  168. if (!result.isSuccess()) {
  169. if (result.getCode() == ResultCode.WRONG_CONNECTION) {
  170. message = res.getString(R.string.network_error_socket_exception);
  171. } else if (result.getCode() == ResultCode.TIMEOUT) {
  172. message = res.getString(R.string.network_error_socket_exception);
  173. if (result.getException() instanceof SocketTimeoutException) {
  174. message = res.getString(R.string.network_error_socket_timeout_exception);
  175. } else if(result.getException() instanceof ConnectTimeoutException) {
  176. message = res.getString(R.string.network_error_connect_timeout_exception);
  177. }
  178. } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
  179. message = res.getString(R.string.network_host_not_available);
  180. }
  181. }
  182. return message;
  183. }
  184. private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
  185. if (code == ResultCode.WRONG_CONNECTION ||
  186. code == ResultCode.TIMEOUT ||
  187. code == ResultCode.HOST_NOT_AVAILABLE) {
  188. return true;
  189. }
  190. else
  191. return false;
  192. }
  193. }