ErrorMessageAdapter.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * Copyright (C) 2016 ownCloud GmbH.
  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 android.support.annotation.NonNull;
  23. import android.support.annotation.Nullable;
  24. import com.owncloud.android.R;
  25. import com.owncloud.android.lib.common.operations.RemoteOperation;
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  27. import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
  28. import com.owncloud.android.operations.CopyFileOperation;
  29. import com.owncloud.android.operations.CreateFolderOperation;
  30. import com.owncloud.android.operations.CreateShareViaLinkOperation;
  31. import com.owncloud.android.operations.CreateShareWithShareeOperation;
  32. import com.owncloud.android.operations.DownloadFileOperation;
  33. import com.owncloud.android.operations.MoveFileOperation;
  34. import com.owncloud.android.operations.RemoveFileOperation;
  35. import com.owncloud.android.operations.RenameFileOperation;
  36. import com.owncloud.android.operations.SynchronizeFileOperation;
  37. import com.owncloud.android.operations.SynchronizeFolderOperation;
  38. import com.owncloud.android.operations.UnshareOperation;
  39. import com.owncloud.android.operations.UpdateSharePermissionsOperation;
  40. import com.owncloud.android.operations.UpdateShareViaLinkOperation;
  41. import com.owncloud.android.operations.UploadFileOperation;
  42. import org.apache.commons.httpclient.ConnectTimeoutException;
  43. import java.io.File;
  44. import java.net.SocketTimeoutException;
  45. /**
  46. * Class to choose proper error messages to show to the user depending on the results of operations,
  47. * always following the same policy
  48. */
  49. public class ErrorMessageAdapter {
  50. public ErrorMessageAdapter() { }
  51. /**
  52. * Return an internationalized user message corresponding to an operation result
  53. * and the operation performed.
  54. *
  55. * @param result Result of a {@link RemoteOperation} performed.
  56. * @param operation Operation performed.
  57. * @param res Reference to app resources, for i18n.
  58. * @return User message corresponding to 'result' and 'operation'.
  59. */
  60. @NonNull
  61. public static String getErrorCauseMessage(
  62. RemoteOperationResult result,
  63. RemoteOperation operation,
  64. Resources res
  65. ) {
  66. String message = getSpecificMessageForResultAndOperation(result, operation, res);
  67. if (message == null || message.length() <= 0) {
  68. message = getCommonMessageForResult(result, res);
  69. }
  70. if (message == null || message.length() <= 0) {
  71. message = getGenericErrorMessageForOperation(operation, res);
  72. }
  73. if (message == null) {
  74. if (result.isSuccess()) {
  75. message = res.getString(R.string.common_ok);
  76. } else {
  77. message = res.getString(R.string.common_error_unknown);
  78. }
  79. }
  80. return message;
  81. }
  82. /**
  83. * Return a user message corresponding to an operation result and specific for the operation
  84. * performed.
  85. *
  86. * @param result Result of a {@link RemoteOperation} performed.
  87. * @param operation Operation performed.
  88. * @param res Reference to app resources, for i18n.
  89. * @return User message corresponding to 'result' and 'operation', or NULL if there is no
  90. * specific message for both.
  91. */
  92. @Nullable
  93. private static String getSpecificMessageForResultAndOperation(
  94. RemoteOperationResult result,
  95. RemoteOperation operation,
  96. Resources res
  97. ) {
  98. String message = null;
  99. if (operation instanceof UploadFileOperation) {
  100. if (result.isSuccess()) {
  101. message = String.format(
  102. res.getString(R.string.uploader_upload_succeeded_content_single),
  103. ((UploadFileOperation) operation).getFileName());
  104. } else {
  105. if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
  106. || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
  107. message = String.format(
  108. res.getString(R.string.error__upload__local_file_not_copied),
  109. ((UploadFileOperation) operation).getFileName(),
  110. res.getString(R.string.app_name));
  111. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  112. message = String.format(res.getString(R.string.forbidden_permissions),
  113. res.getString(R.string.uploader_upload_forbidden_permissions));
  114. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  115. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  116. }
  117. }
  118. } else if (operation instanceof DownloadFileOperation) {
  119. if (result.isSuccess()) {
  120. message = String.format(
  121. res.getString(R.string.downloader_download_succeeded_content),
  122. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  123. } else {
  124. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  125. message = res.getString(R.string.downloader_download_file_not_found);
  126. }
  127. }
  128. } else if (operation instanceof RemoveFileOperation) {
  129. if (result.isSuccess()) {
  130. message = res.getString(R.string.remove_success_msg);
  131. } else {
  132. if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  133. // Error --> No permissions
  134. message = String.format(res.getString(R.string.forbidden_permissions),
  135. res.getString(R.string.forbidden_permissions_delete));
  136. }
  137. }
  138. } else if (operation instanceof RenameFileOperation) {
  139. if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
  140. message = res.getString(R.string.rename_local_fail_msg);
  141. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  142. // Error --> No permissions
  143. message = String.format(res.getString(R.string.forbidden_permissions),
  144. res.getString(R.string.forbidden_permissions_rename));
  145. } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
  146. message = res.getString(R.string.filename_forbidden_characters);
  147. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  148. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  149. }
  150. } else if (operation instanceof SynchronizeFileOperation) {
  151. if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
  152. message = res.getString(R.string.sync_file_nothing_to_do_msg);
  153. }
  154. } else if (operation instanceof CreateFolderOperation) {
  155. if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
  156. message = res.getString(R.string.filename_forbidden_characters);
  157. } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
  158. message = String.format(res.getString(R.string.forbidden_permissions),
  159. res.getString(R.string.forbidden_permissions_create));
  160. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  161. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  162. }
  163. } else if (operation instanceof CreateShareViaLinkOperation ||
  164. operation instanceof CreateShareWithShareeOperation) {
  165. if (result.getData() != null && result.getData().size() > 0) {
  166. message = (String) result.getData().get(0); // share API sends its own error messages
  167. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  168. message = res.getString(R.string.share_link_file_no_exist);
  169. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  170. // Error --> No permissions
  171. message = String.format(res.getString(R.string.forbidden_permissions),
  172. res.getString(R.string.share_link_forbidden_permissions));
  173. }
  174. } else if (operation instanceof UnshareOperation) {
  175. if (result.getData() != null && result.getData().size() > 0) {
  176. message = (String) result.getData().get(0); // share API sends its own error messages
  177. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  178. message = res.getString(R.string.unshare_link_file_no_exist);
  179. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  180. // Error --> No permissions
  181. message = String.format(res.getString(R.string.forbidden_permissions),
  182. res.getString(R.string.unshare_link_forbidden_permissions));
  183. }
  184. } else if (operation instanceof UpdateShareViaLinkOperation ||
  185. operation instanceof UpdateSharePermissionsOperation) {
  186. if (result.getData() != null && result.getData().size() > 0) {
  187. message = (String) result.getData().get(0); // share API sends its own error messages
  188. } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
  189. message = res.getString(R.string.update_link_file_no_exist);
  190. } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
  191. // Error --> No permissions
  192. message = String.format(res.getString(R.string.forbidden_permissions),
  193. res.getString(R.string.update_link_forbidden_permissions));
  194. }
  195. } else if (operation instanceof MoveFileOperation) {
  196. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  197. message = res.getString(R.string.move_file_not_found);
  198. } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
  199. message = res.getString(R.string.move_file_invalid_into_descendent);
  200. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  201. message = res.getString(R.string.move_file_invalid_overwrite);
  202. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  203. message = String.format(res.getString(R.string.forbidden_permissions),
  204. res.getString(R.string.forbidden_permissions_move));
  205. } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
  206. message = res.getString(R.string.filename_forbidden_charaters_from_server);
  207. }
  208. } else if (operation instanceof SynchronizeFolderOperation) {
  209. if (!result.isSuccess()) {
  210. String folderPathName = new File(
  211. ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
  212. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  213. message = String.format(
  214. res.getString(R.string.sync_current_folder_was_removed),
  215. folderPathName
  216. );
  217. }
  218. }
  219. } else if (operation instanceof CopyFileOperation) {
  220. if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  221. message = res.getString(R.string.copy_file_not_found);
  222. } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
  223. message = res.getString(R.string.copy_file_invalid_into_descendent);
  224. } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
  225. message = res.getString(R.string.copy_file_invalid_overwrite);
  226. } else if (result.getCode() == ResultCode.FORBIDDEN) {
  227. message = String.format(res.getString(R.string.forbidden_permissions),
  228. res.getString(R.string.forbidden_permissions_copy));
  229. }
  230. }
  231. return message;
  232. }
  233. /**
  234. * Return a user message corresponding to an operation result with no knowledge about the operation
  235. * performed.
  236. *
  237. * @param result Result of a {@link RemoteOperation} performed.
  238. * @param res Reference to app resources, for i18n.
  239. * @return User message corresponding to 'result'.
  240. */
  241. @Nullable
  242. private static String getCommonMessageForResult(RemoteOperationResult result, Resources res) {
  243. String message = null;
  244. if (!result.isSuccess()) {
  245. if (result.getCode() == ResultCode.WRONG_CONNECTION) {
  246. message = res.getString(R.string.network_error_socket_exception);
  247. } else if (result.getCode() == ResultCode.TIMEOUT) {
  248. message = res.getString(R.string.network_error_socket_exception);
  249. if (result.getException() instanceof SocketTimeoutException) {
  250. message = res.getString(R.string.network_error_socket_timeout_exception);
  251. } else if (result.getException() instanceof ConnectTimeoutException) {
  252. message = res.getString(R.string.network_error_connect_timeout_exception);
  253. }
  254. } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
  255. message = res.getString(R.string.network_host_not_available);
  256. } else if (result.getCode() == ResultCode.MAINTENANCE_MODE) {
  257. message = res.getString(R.string.maintenance_mode);
  258. } else if (result.getCode() == ResultCode.SSL_RECOVERABLE_PEER_UNVERIFIED) {
  259. message = res.getString(
  260. R.string.uploads_view_upload_status_failed_ssl_certificate_not_trusted
  261. );
  262. } else if (result.getCode() == ResultCode.BAD_OC_VERSION) {
  263. message = res.getString(
  264. R.string.auth_bad_oc_version_title
  265. );
  266. } else if (result.getCode() == ResultCode.INCORRECT_ADDRESS) {
  267. message = res.getString(
  268. R.string.auth_incorrect_address_title
  269. );
  270. } else if (result.getCode() == ResultCode.SSL_ERROR) {
  271. message = res.getString(
  272. R.string.auth_ssl_general_error_title
  273. );
  274. } else if (result.getCode() == ResultCode.UNAUTHORIZED) {
  275. message = res.getString(
  276. R.string.auth_unauthorized
  277. );
  278. } else if (result.getCode() == ResultCode.INSTANCE_NOT_CONFIGURED) {
  279. message = res.getString(
  280. R.string.auth_not_configured_title
  281. );
  282. } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
  283. message = res.getString(
  284. R.string.auth_incorrect_path_title
  285. );
  286. } else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
  287. message = res.getString(
  288. R.string.auth_oauth_error
  289. );
  290. } else if (result.getCode() == ResultCode.OAUTH2_ERROR_ACCESS_DENIED) {
  291. message = res.getString(
  292. R.string.auth_oauth_error_access_denied
  293. );
  294. } else if (result.getCode() == ResultCode.ACCOUNT_NOT_NEW) {
  295. message = res.getString(
  296. R.string.auth_account_not_new
  297. );
  298. } else if (result.getCode() == ResultCode.ACCOUNT_NOT_THE_SAME) {
  299. message = res.getString(
  300. R.string.auth_account_not_the_same
  301. );
  302. }
  303. else if (result.getHttpPhrase() != null && result.getHttpPhrase().length() > 0) {
  304. // last chance: error message from server
  305. message = result.getHttpPhrase();
  306. }
  307. }
  308. return message;
  309. }
  310. /**
  311. * Return a user message corresponding to a generic error for a given operation.
  312. *
  313. * @param operation Operation performed.
  314. * @param res Reference to app resources, for i18n.
  315. * @return User message corresponding to a generic error of 'operation'.
  316. */
  317. @Nullable
  318. private static String getGenericErrorMessageForOperation(RemoteOperation operation, Resources res) {
  319. String message = null;
  320. if (operation instanceof UploadFileOperation) {
  321. message = String.format(
  322. res.getString(R.string.uploader_upload_failed_content_single),
  323. ((UploadFileOperation) operation).getFileName());
  324. } else if (operation instanceof DownloadFileOperation) {
  325. message = String.format(
  326. res.getString(R.string.downloader_download_failed_content),
  327. new File(((DownloadFileOperation) operation).getSavePath()).getName());
  328. } else if (operation instanceof RemoveFileOperation) {
  329. message = res.getString(R.string.remove_fail_msg);
  330. } else if (operation instanceof RenameFileOperation) {
  331. message = res.getString(R.string.rename_server_fail_msg);
  332. } else if (operation instanceof CreateFolderOperation) {
  333. message = res.getString(R.string.create_dir_fail_msg);
  334. } else if (operation instanceof CreateShareViaLinkOperation ||
  335. operation instanceof CreateShareWithShareeOperation
  336. ) {
  337. message = res.getString(R.string.share_link_file_error);
  338. } else if (operation instanceof UnshareOperation) {
  339. message = res.getString(R.string.unshare_link_file_error);
  340. } else if (operation instanceof UpdateShareViaLinkOperation ||
  341. operation instanceof UpdateSharePermissionsOperation
  342. ) {
  343. message = res.getString(R.string.update_link_file_error);
  344. } else if (operation instanceof MoveFileOperation) {
  345. message = res.getString(R.string.move_file_error);
  346. } else if (operation instanceof SynchronizeFolderOperation) {
  347. String folderPathName = new File(
  348. ((SynchronizeFolderOperation) operation).getFolderPath()
  349. ).getName();
  350. message = String.format(res.getString(R.string.sync_folder_failed_content), folderPathName);
  351. } else if (operation instanceof CopyFileOperation) {
  352. message = res.getString(R.string.copy_file_error);
  353. }
  354. return message;
  355. }
  356. }