ErrorMessageAdapter.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. package com.owncloud.android.utils;
  20. import android.content.res.Resources;
  21. import com.owncloud.android.R;
  22. import com.owncloud.android.lib.common.operations.RemoteOperation;
  23. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  25. import com.owncloud.android.operations.CopyFileOperation;
  26. import com.owncloud.android.operations.CreateFolderOperation;
  27. import com.owncloud.android.operations.CreateShareViaLinkOperation;
  28. import com.owncloud.android.operations.CreateShareWithShareeOperation;
  29. import com.owncloud.android.operations.DownloadFileOperation;
  30. import com.owncloud.android.operations.MoveFileOperation;
  31. import com.owncloud.android.operations.RemoveFileOperation;
  32. import com.owncloud.android.operations.RenameFileOperation;
  33. import com.owncloud.android.operations.SynchronizeFileOperation;
  34. import com.owncloud.android.operations.SynchronizeFolderOperation;
  35. import com.owncloud.android.operations.UnshareOperation;
  36. import com.owncloud.android.operations.UpdateSharePermissionsOperation;
  37. import com.owncloud.android.operations.UpdateShareViaLinkOperation;
  38. import com.owncloud.android.operations.UploadFileOperation;
  39. import org.apache.commons.httpclient.ConnectTimeoutException;
  40. import java.io.File;
  41. import java.net.SocketTimeoutException;
  42. /**
  43. * Class to choose proper error messages to show to the user depending on the results of operations,
  44. * always following the same policy.
  45. */
  46. public class ErrorMessageAdapter {
  47. public static String getErrorCauseMessage(RemoteOperationResult result,
  48. RemoteOperation operation, Resources res) {
  49. String message = null;
  50. if (!result.isSuccess() && isCommonError(result.getCode())) {
  51. message = getCommonErrorMessage(result, res);
  52. } else if (operation instanceof UploadFileOperation) {
  53. if (result.isSuccess()) {
  54. message = String.format(
  55. res.getString(R.string.uploader_upload_succeeded_content_single),
  56. ((UploadFileOperation) operation).getFileName());
  57. } else {
  58. if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
  59. || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
  60. message = String.format(
  61. res.getString(R.string.error__upload__local_file_not_copied),
  62. ((UploadFileOperation) operation).getFileName(),
  63. res.getString(R.string.app_name));
  64. /*
  65. } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
  66. message = res.getString(R.string.failed_upload_quota_exceeded_text);
  67. */
  68. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  69. message = String.format(res.getString(R.string.forbidden_permissions),
  70. res.getString(R.string.uploader_upload_forbidden_permissions));
  71. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  72. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  73. } else {
  74. message = String.format(
  75. res.getString(R.string.uploader_upload_failed_content_single),
  76. ((UploadFileOperation) operation).getFileName());
  77. }
  78. }
  79. } else if (operation instanceof DownloadFileOperation) {
  80. if (result.isSuccess()) {
  81. message = String.format(
  82. res.getString(R.string.downloader_download_succeeded_content),
  83. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  84. } else {
  85. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  86. message = res.getString(R.string.downloader_download_file_not_found);
  87. } else {
  88. message = String.format(
  89. res.getString(R.string.downloader_download_failed_content), new File(
  90. ((DownloadFileOperation) operation).getSavePath()).getName());
  91. }
  92. }
  93. } else if (operation instanceof RemoveFileOperation) {
  94. if (result.isSuccess()) {
  95. message = res.getString(R.string.remove_success_msg);
  96. } else {
  97. 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_delete));
  101. } else {
  102. message = res.getString(R.string.remove_fail_msg);
  103. }
  104. }
  105. } else if (operation instanceof RenameFileOperation) {
  106. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  107. message = res.getString(R.string.rename_local_fail_msg);
  108. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  109. // Error --> No permissions
  110. message = String.format(res.getString(R.string.forbidden_permissions),
  111. res.getString(R.string.forbidden_permissions_rename));
  112. } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
  113. message = res.getString(R.string.filename_forbidden_characters);
  114. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  115. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  116. } else {
  117. message = res.getString(R.string.rename_server_fail_msg);
  118. }
  119. } else if (operation instanceof SynchronizeFileOperation) {
  120. if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
  121. message = res.getString(R.string.sync_file_nothing_to_do_msg);
  122. }
  123. } else if (operation instanceof CreateFolderOperation) {
  124. if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
  125. message = res.getString(R.string.filename_forbidden_characters);
  126. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  127. message = String.format(res.getString(R.string.forbidden_permissions),
  128. res.getString(R.string.forbidden_permissions_create));
  129. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  130. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  131. } else {
  132. message = res.getString(R.string.create_dir_fail_msg);
  133. }
  134. } else if (operation instanceof CreateShareViaLinkOperation ||
  135. operation instanceof CreateShareWithShareeOperation) {
  136. if (result.getData() != null && result.getData().size() > 0) {
  137. message = (String) result.getData().get(0); // share API sends its own error messages
  138. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  139. message = res.getString(R.string.share_link_file_no_exist);
  140. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  141. // Error --> No permissions
  142. message = String.format(res.getString(R.string.forbidden_permissions),
  143. res.getString(R.string.share_link_forbidden_permissions));
  144. } else { // Generic error
  145. // Show a Message, operation finished without success
  146. message = res.getString(R.string.share_link_file_error);
  147. }
  148. } else if (operation instanceof UnshareOperation) {
  149. if (result.getData() != null && result.getData().size() > 0) {
  150. message = (String) result.getData().get(0); // share API sends its own error messages
  151. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  152. message = res.getString(R.string.unshare_link_file_no_exist);
  153. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  154. // Error --> No permissions
  155. message = String.format(res.getString(R.string.forbidden_permissions),
  156. res.getString(R.string.unshare_link_forbidden_permissions));
  157. } else { // Generic error
  158. // Show a Message, operation finished without success
  159. message = res.getString(R.string.unshare_link_file_error);
  160. }
  161. } else if (operation instanceof UpdateShareViaLinkOperation ||
  162. operation instanceof UpdateSharePermissionsOperation) {
  163. if (result.getData() != null && result.getData().size() > 0) {
  164. message = (String) result.getData().get(0); // share API sends its own error messages
  165. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  166. message = res.getString(R.string.update_link_file_no_exist);
  167. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  168. // Error --> No permissions
  169. message = String.format(res.getString(R.string.forbidden_permissions),
  170. res.getString(R.string.update_link_forbidden_permissions));
  171. } else { // Generic error
  172. // Show a Message, operation finished without success
  173. message = res.getString(R.string.update_link_file_error);
  174. }
  175. } else if (operation instanceof MoveFileOperation) {
  176. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  177. message = res.getString(R.string.move_file_not_found);
  178. } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
  179. message = res.getString(R.string.move_file_invalid_into_descendent);
  180. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  181. message = res.getString(R.string.move_file_invalid_overwrite);
  182. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  183. message = String.format(res.getString(R.string.forbidden_permissions),
  184. res.getString(R.string.forbidden_permissions_move));
  185. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  186. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  187. } else { // Generic error
  188. // Show a Message, operation finished without success
  189. message = res.getString(R.string.move_file_error);
  190. }
  191. } else if (operation instanceof SynchronizeFolderOperation) {
  192. if (!result.isSuccess()) {
  193. String folderPathName = new File(
  194. ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
  195. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  196. message = String.format(res.getString(R.string.sync_current_folder_was_removed),
  197. folderPathName);
  198. } else { // Generic error
  199. // Show a Message, operation finished without success
  200. message = String.format(res.getString(R.string.sync_folder_failed_content),
  201. folderPathName);
  202. }
  203. }
  204. } else if (operation instanceof CopyFileOperation) {
  205. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  206. message = res.getString(R.string.copy_file_not_found);
  207. } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
  208. message = res.getString(R.string.copy_file_invalid_into_descendent);
  209. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  210. message = res.getString(R.string.copy_file_invalid_overwrite);
  211. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  212. message = String.format(res.getString(R.string.forbidden_permissions),
  213. res.getString(R.string.forbidden_permissions_copy));
  214. } else { // Generic error
  215. // Show a Message, operation finished without success
  216. message = res.getString(R.string.copy_file_error);
  217. }
  218. }
  219. return message;
  220. }
  221. private static String getCommonErrorMessage(RemoteOperationResult result, Resources res) {
  222. String message = null;
  223. if (!result.isSuccess()) {
  224. if (result.getCode() == ResultCode.WRONG_CONNECTION) {
  225. message = res.getString(R.string.network_error_socket_exception);
  226. } else if (result.getCode() == ResultCode.TIMEOUT) {
  227. message = res.getString(R.string.network_error_socket_exception);
  228. if (result.getException() instanceof SocketTimeoutException) {
  229. message = res.getString(R.string.network_error_socket_timeout_exception);
  230. } else if (result.getException() instanceof ConnectTimeoutException) {
  231. message = res.getString(R.string.network_error_connect_timeout_exception);
  232. }
  233. } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
  234. message = res.getString(R.string.network_host_not_available);
  235. } else if (result.getCode() == ResultCode.MAINTENANCE_MODE) {
  236. message = res.getString(R.string.maintenance_mode);
  237. }
  238. }
  239. return message;
  240. }
  241. private static boolean isCommonError(RemoteOperationResult.ResultCode code) {
  242. return code == ResultCode.WRONG_CONNECTION ||
  243. code == ResultCode.TIMEOUT ||
  244. code == ResultCode.HOST_NOT_AVAILABLE ||
  245. code == ResultCode.MAINTENANCE_MODE;
  246. }
  247. }