ErrorMessageAdapter.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * Copyright (C) 2014 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.utils;
  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.lib.resources.shares.UpdateRemoteShareOperation;
  27. import com.owncloud.android.operations.CopyFileOperation;
  28. import com.owncloud.android.operations.CreateFolderOperation;
  29. import com.owncloud.android.operations.CreateShareViaLinkOperation;
  30. import com.owncloud.android.operations.CreateShareWithShareeOperation;
  31. import com.owncloud.android.operations.DownloadFileOperation;
  32. import com.owncloud.android.operations.MoveFileOperation;
  33. import com.owncloud.android.operations.RemoveFileOperation;
  34. import com.owncloud.android.operations.RenameFileOperation;
  35. import com.owncloud.android.operations.SynchronizeFileOperation;
  36. import com.owncloud.android.operations.SynchronizeFolderOperation;
  37. import com.owncloud.android.operations.UnshareOperation;
  38. import com.owncloud.android.operations.UpdateSharePermissionsOperation;
  39. import com.owncloud.android.operations.UpdateShareViaLinkOperation;
  40. import com.owncloud.android.operations.UploadFileOperation;
  41. import org.apache.commons.httpclient.ConnectTimeoutException;
  42. import java.io.File;
  43. import java.net.SocketTimeoutException;
  44. /**
  45. * Class to choose proper error messages to show to the user depending on the results of operations,
  46. * always following the same policy
  47. */
  48. public class ErrorMessageAdapter {
  49. public ErrorMessageAdapter() {
  50. }
  51. public static String getErrorCauseMessage(RemoteOperationResult result,
  52. RemoteOperation operation, Resources res) {
  53. String message = null;
  54. if (!result.isSuccess() && isNetworkError(result.getCode())) {
  55. message = getErrorMessage(result, res);
  56. } else if (operation instanceof UploadFileOperation) {
  57. if (result.isSuccess()) {
  58. message = String.format(
  59. res.getString(R.string.uploader_upload_succeeded_content_single),
  60. ((UploadFileOperation) operation).getFileName());
  61. } else {
  62. if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
  63. || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
  64. message = String.format(
  65. res.getString(R.string.error__upload__local_file_not_copied),
  66. ((UploadFileOperation) operation).getFileName(),
  67. res.getString(R.string.app_name));
  68. /*
  69. } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
  70. message = res.getString(R.string.failed_upload_quota_exceeded_text);
  71. */
  72. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  73. message = String.format(res.getString(R.string.forbidden_permissions),
  74. res.getString(R.string.uploader_upload_forbidden_permissions));
  75. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  76. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  77. } else {
  78. message = String.format(
  79. res.getString(R.string.uploader_upload_failed_content_single),
  80. ((UploadFileOperation) operation).getFileName());
  81. }
  82. }
  83. } else if (operation instanceof DownloadFileOperation) {
  84. if (result.isSuccess()) {
  85. message = String.format(
  86. res.getString(R.string.downloader_download_succeeded_content),
  87. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  88. } else {
  89. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  90. message = res.getString(R.string.downloader_download_file_not_found);
  91. } else {
  92. message = String.format(
  93. res.getString(R.string.downloader_download_failed_content), new File(
  94. ((DownloadFileOperation) operation).getSavePath()).getName());
  95. }
  96. }
  97. } else if (operation instanceof RemoveFileOperation) {
  98. if (result.isSuccess()) {
  99. message = res.getString(R.string.remove_success_msg);
  100. } else {
  101. if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  102. // Error --> No permissions
  103. message = String.format(res.getString(R.string.forbidden_permissions),
  104. res.getString(R.string.forbidden_permissions_delete));
  105. } else {
  106. message = res.getString(R.string.remove_fail_msg);
  107. }
  108. }
  109. } else if (operation instanceof RenameFileOperation) {
  110. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  111. message = res.getString(R.string.rename_local_fail_msg);
  112. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  113. // Error --> No permissions
  114. message = String.format(res.getString(R.string.forbidden_permissions),
  115. res.getString(R.string.forbidden_permissions_rename));
  116. } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
  117. message = res.getString(R.string.filename_forbidden_characters);
  118. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  119. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  120. } else {
  121. message = res.getString(R.string.rename_server_fail_msg);
  122. }
  123. } else if (operation instanceof SynchronizeFileOperation) {
  124. if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
  125. message = res.getString(R.string.sync_file_nothing_to_do_msg);
  126. }
  127. } else if (operation instanceof CreateFolderOperation) {
  128. if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
  129. message = res.getString(R.string.filename_forbidden_characters);
  130. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  131. message = String.format(res.getString(R.string.forbidden_permissions),
  132. res.getString(R.string.forbidden_permissions_create));
  133. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  134. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  135. } else {
  136. message = res.getString(R.string.create_dir_fail_msg);
  137. }
  138. } else if (operation instanceof CreateShareViaLinkOperation ||
  139. operation instanceof CreateShareWithShareeOperation) {
  140. if (result.getData() != null && result.getData().size() > 0) {
  141. message = (String) result.getData().get(0); // share API sends its own error messages
  142. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  143. message = res.getString(R.string.share_link_file_no_exist);
  144. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  145. // Error --> No permissions
  146. message = String.format(res.getString(R.string.forbidden_permissions),
  147. res.getString(R.string.share_link_forbidden_permissions));
  148. } else { // Generic error
  149. // Show a Message, operation finished without success
  150. message = res.getString(R.string.share_link_file_error);
  151. }
  152. } else if (operation instanceof UnshareOperation) {
  153. if (result.getData() != null && result.getData().size() > 0) {
  154. message = (String) result.getData().get(0); // share API sends its own error messages
  155. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  156. message = res.getString(R.string.unshare_link_file_no_exist);
  157. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  158. // Error --> No permissions
  159. message = String.format(res.getString(R.string.forbidden_permissions),
  160. res.getString(R.string.unshare_link_forbidden_permissions));
  161. } else { // Generic error
  162. // Show a Message, operation finished without success
  163. message = res.getString(R.string.unshare_link_file_error);
  164. }
  165. } else if (operation instanceof UpdateShareViaLinkOperation ||
  166. operation instanceof UpdateSharePermissionsOperation) {
  167. if (result.getData() != null && result.getData().size() > 0) {
  168. message = (String) result.getData().get(0); // share API sends its own error messages
  169. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  170. message = res.getString(R.string.update_link_file_no_exist);
  171. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  172. // Error --> No permissions
  173. message = String.format(res.getString(R.string.forbidden_permissions),
  174. res.getString(R.string.update_link_forbidden_permissions));
  175. } else { // Generic error
  176. // Show a Message, operation finished without success
  177. message = res.getString(R.string.update_link_file_error);
  178. }
  179. } else if (operation instanceof MoveFileOperation) {
  180. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  181. message = res.getString(R.string.move_file_not_found);
  182. } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
  183. message = res.getString(R.string.move_file_invalid_into_descendent);
  184. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  185. message = res.getString(R.string.move_file_invalid_overwrite);
  186. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  187. message = String.format(res.getString(R.string.forbidden_permissions),
  188. res.getString(R.string.forbidden_permissions_move));
  189. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  190. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  191. } else { // Generic error
  192. // Show a Message, operation finished without success
  193. message = res.getString(R.string.move_file_error);
  194. }
  195. } else if (operation instanceof SynchronizeFolderOperation) {
  196. if (!result.isSuccess()) {
  197. String folderPathName = new File(
  198. ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
  199. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  200. message = String.format(res.getString(R.string.sync_current_folder_was_removed),
  201. folderPathName);
  202. } else { // Generic error
  203. // Show a Message, operation finished without success
  204. message = String.format(res.getString(R.string.sync_folder_failed_content),
  205. folderPathName);
  206. }
  207. }
  208. } else if (operation instanceof CopyFileOperation) {
  209. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  210. message = res.getString(R.string.copy_file_not_found);
  211. } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
  212. message = res.getString(R.string.copy_file_invalid_into_descendent);
  213. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  214. message = res.getString(R.string.copy_file_invalid_overwrite);
  215. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  216. message = String.format(res.getString(R.string.forbidden_permissions),
  217. res.getString(R.string.forbidden_permissions_copy));
  218. } else { // Generic error
  219. // Show a Message, operation finished without success
  220. message = res.getString(R.string.copy_file_error);
  221. }
  222. }
  223. return message;
  224. }
  225. private static String getErrorMessage(RemoteOperationResult result, Resources res) {
  226. String message = null;
  227. if (!result.isSuccess()) {
  228. if (result.getCode() == ResultCode.WRONG_CONNECTION) {
  229. message = res.getString(R.string.network_error_socket_exception);
  230. } else if (result.getCode() == ResultCode.TIMEOUT) {
  231. message = res.getString(R.string.network_error_socket_exception);
  232. if (result.getException() instanceof SocketTimeoutException) {
  233. message = res.getString(R.string.network_error_socket_timeout_exception);
  234. } else if (result.getException() instanceof ConnectTimeoutException) {
  235. message = res.getString(R.string.network_error_connect_timeout_exception);
  236. }
  237. } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
  238. message = res.getString(R.string.network_host_not_available);
  239. }
  240. }
  241. return message;
  242. }
  243. private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
  244. if (code == ResultCode.WRONG_CONNECTION ||
  245. code == ResultCode.TIMEOUT ||
  246. code == ResultCode.HOST_NOT_AVAILABLE) {
  247. return true;
  248. } else
  249. return false;
  250. }
  251. }