ErrorMessageAdapter.java 15 KB

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