ErrorMessageAdapter.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.operations.CopyFileOperation;
  27. import com.owncloud.android.operations.CreateFolderOperation;
  28. import com.owncloud.android.operations.CreateShareOperation;
  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.UnshareLinkOperation;
  36. import com.owncloud.android.operations.UploadFileOperation;
  37. import org.apache.commons.httpclient.ConnectTimeoutException;
  38. import java.io.File;
  39. import java.net.SocketTimeoutException;
  40. /**
  41. * Class to choose proper error messages to show to the user depending on the results of operations,
  42. * always following the same policy
  43. */
  44. public class ErrorMessageAdapter {
  45. public ErrorMessageAdapter() {
  46. }
  47. public static String getErrorCauseMessage(RemoteOperationResult result,
  48. RemoteOperation operation, Resources res) {
  49. String message = null;
  50. if (operation instanceof UploadFileOperation) {
  51. if (result.isSuccess()) {
  52. message = String.format(
  53. res.getString(R.string.uploader_upload_succeeded_content_single),
  54. ((UploadFileOperation) operation).getFileName());
  55. } else {
  56. if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
  57. || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
  58. message = String.format(
  59. res.getString(R.string.error__upload__local_file_not_copied),
  60. ((UploadFileOperation) operation).getFileName(),
  61. res.getString(R.string.app_name));
  62. /*
  63. } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
  64. message = res.getString(R.string.failed_upload_quota_exceeded_text);
  65. */
  66. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  67. message = String.format(res.getString(R.string.forbidden_permissions),
  68. res.getString(R.string.uploader_upload_forbidden_permissions));
  69. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  70. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  71. } else {
  72. message = String.format(
  73. res.getString(R.string.uploader_upload_failed_content_single),
  74. ((UploadFileOperation) operation).getFileName());
  75. }
  76. }
  77. } else if (operation instanceof DownloadFileOperation) {
  78. if (result.isSuccess()) {
  79. message = String.format(
  80. res.getString(R.string.downloader_download_succeeded_content),
  81. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  82. } else {
  83. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  84. message = res.getString(R.string.downloader_download_file_not_found);
  85. } else {
  86. message = String.format(
  87. res.getString(R.string.downloader_download_failed_content), new File(
  88. ((DownloadFileOperation) operation).getSavePath()).getName());
  89. }
  90. }
  91. } else if (operation instanceof RemoveFileOperation) {
  92. if (result.isSuccess()) {
  93. message = res.getString(R.string.remove_success_msg);
  94. } else {
  95. if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  96. // Error --> No permissions
  97. message = String.format(res.getString(R.string.forbidden_permissions),
  98. res.getString(R.string.forbidden_permissions_delete));
  99. } else if (isNetworkError(result.getCode())) {
  100. message = getErrorMessage(result, res);
  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 (isNetworkError(result.getCode())) {
  115. message = getErrorMessage(result, res);
  116. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  117. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  118. } else {
  119. message = res.getString(R.string.rename_server_fail_msg);
  120. }
  121. } else if (operation instanceof SynchronizeFileOperation) {
  122. if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
  123. message = res.getString(R.string.sync_file_nothing_to_do_msg);
  124. }
  125. } else if (operation instanceof CreateFolderOperation) {
  126. if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
  127. message = res.getString(R.string.filename_forbidden_characters);
  128. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  129. message = String.format(res.getString(R.string.forbidden_permissions),
  130. res.getString(R.string.forbidden_permissions_create));
  131. } else if (isNetworkError(result.getCode())) {
  132. message = getErrorMessage(result, res);
  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 CreateShareOperation) {
  139. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> 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 if (isNetworkError(result.getCode())) {
  146. message = getErrorMessage(result, res);
  147. } else { // Generic error
  148. // Show a Message, operation finished without success
  149. message = res.getString(R.string.share_link_file_error);
  150. }
  151. } else if (operation instanceof UnshareLinkOperation) {
  152. if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> 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 if (isNetworkError(result.getCode())) {
  159. message = getErrorMessage(result, res);
  160. } else { // Generic error
  161. // Show a Message, operation finished without success
  162. message = res.getString(R.string.unshare_link_file_error);
  163. }
  164. } else if (operation instanceof MoveFileOperation) {
  165. if(isNetworkError(result.getCode())){
  166. message = getErrorMessage(result, res);
  167. } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  168. message = res.getString(R.string.move_file_not_found);
  169. } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
  170. message = res.getString(R.string.move_file_invalid_into_descendent);
  171. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  172. message = res.getString(R.string.move_file_invalid_overwrite);
  173. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  174. message = String.format(res.getString(R.string.forbidden_permissions),
  175. res.getString(R.string.forbidden_permissions_move));
  176. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  177. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  178. } else { // Generic error
  179. // Show a Message, operation finished without success
  180. message = res.getString(R.string.move_file_error);
  181. }
  182. } else if (operation instanceof SynchronizeFolderOperation) {
  183. if (!result.isSuccess()) {
  184. String folderPathName = new File(
  185. ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
  186. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  187. message = String.format(res.getString(R.string.sync_current_folder_was_removed),
  188. folderPathName);
  189. } else { // Generic error
  190. // Show a Message, operation finished without success
  191. message = String.format(res.getString(R.string.download_folder_failed_content),
  192. folderPathName);
  193. }
  194. }
  195. } else if (operation instanceof CopyFileOperation) {
  196. if(isNetworkError(result.getCode())){
  197. message = getErrorMessage(result, res);
  198. } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  199. message = res.getString(R.string.copy_file_not_found);
  200. } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
  201. message = res.getString(R.string.copy_file_invalid_into_descendent);
  202. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  203. message = res.getString(R.string.copy_file_invalid_overwrite);
  204. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  205. message = String.format(res.getString(R.string.forbidden_permissions),
  206. res.getString(R.string.forbidden_permissions_copy));
  207. } else { // Generic error
  208. // Show a Message, operation finished without success
  209. message = res.getString(R.string.copy_file_error);
  210. }
  211. }
  212. return message;
  213. }
  214. private static String getErrorMessage(RemoteOperationResult result, Resources res) {
  215. String message = null;
  216. if (!result.isSuccess()) {
  217. if (result.getCode() == ResultCode.WRONG_CONNECTION) {
  218. message = res.getString(R.string.network_error_socket_exception);
  219. } else if (result.getCode() == ResultCode.TIMEOUT) {
  220. message = res.getString(R.string.network_error_socket_exception);
  221. if (result.getException() instanceof SocketTimeoutException) {
  222. message = res.getString(R.string.network_error_socket_timeout_exception);
  223. } else if (result.getException() instanceof ConnectTimeoutException) {
  224. message = res.getString(R.string.network_error_connect_timeout_exception);
  225. }
  226. } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
  227. message = res.getString(R.string.network_host_not_available);
  228. }
  229. }
  230. return message;
  231. }
  232. private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
  233. if (code == ResultCode.WRONG_CONNECTION ||
  234. code == ResultCode.TIMEOUT ||
  235. code == ResultCode.HOST_NOT_AVAILABLE) {
  236. return true;
  237. } else
  238. return false;
  239. }
  240. }