ErrorMessageAdapter.java 20 KB

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