2 * ownCloud Android client application
5 * Copyright (C) 2014 ownCloud Inc.
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.
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.
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/>.
22 package com
.owncloud
.android
.utils
;
24 import android
.content
.res
.Resources
;
26 import com
.owncloud
.android
.R
;
27 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
28 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
29 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
30 import com
.owncloud
.android
.lib
.resources
.shares
.UpdateRemoteShareOperation
;
31 import com
.owncloud
.android
.operations
.CopyFileOperation
;
32 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
33 import com
.owncloud
.android
.operations
.CreateShareViaLinkOperation
;
34 import com
.owncloud
.android
.operations
.CreateShareWithShareeOperation
;
35 import com
.owncloud
.android
.operations
.DownloadFileOperation
;
36 import com
.owncloud
.android
.operations
.MoveFileOperation
;
37 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
38 import com
.owncloud
.android
.operations
.RenameFileOperation
;
39 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
40 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
41 import com
.owncloud
.android
.operations
.UnshareOperation
;
42 import com
.owncloud
.android
.operations
.UpdateShareViaLinkOperation
;
43 import com
.owncloud
.android
.operations
.UploadFileOperation
;
45 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
48 import java
.net
.SocketTimeoutException
;
51 * Class to choose proper error messages to show to the user depending on the results of operations,
52 * always following the same policy
55 public class ErrorMessageAdapter
{
57 public ErrorMessageAdapter() {
61 public static String
getErrorCauseMessage(RemoteOperationResult result
,
62 RemoteOperation operation
, Resources res
) {
64 String message
= null
;
66 if (!result
.isSuccess() && isNetworkError(result
.getCode())) {
67 message
= getErrorMessage(result
, res
);
69 } else if (operation
instanceof UploadFileOperation
) {
71 if (result
.isSuccess()) {
72 message
= String
.format(
73 res
.getString(R
.string
.uploader_upload_succeeded_content_single
),
74 ((UploadFileOperation
) operation
).getFileName());
76 if (result
.getCode() == ResultCode
.LOCAL_STORAGE_FULL
77 || result
.getCode() == ResultCode
.LOCAL_STORAGE_NOT_COPIED
) {
78 message
= String
.format(
79 res
.getString(R
.string
.error__upload__local_file_not_copied
),
80 ((UploadFileOperation
) operation
).getFileName(),
81 res
.getString(R
.string
.app_name
));
83 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
84 message = res.getString(R.string.failed_upload_quota_exceeded_text);
87 } else if (result
.getCode() == ResultCode
.FORBIDDEN
) {
88 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
89 res
.getString(R
.string
.uploader_upload_forbidden_permissions
));
91 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
92 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
95 message
= String
.format(
96 res
.getString(R
.string
.uploader_upload_failed_content_single
),
97 ((UploadFileOperation
) operation
).getFileName());
101 } else if (operation
instanceof DownloadFileOperation
) {
103 if (result
.isSuccess()) {
104 message
= String
.format(
105 res
.getString(R
.string
.downloader_download_succeeded_content
),
106 new File(((DownloadFileOperation
) operation
).getSavePath()).getName());
109 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
110 message
= res
.getString(R
.string
.downloader_download_file_not_found
);
113 message
= String
.format(
114 res
.getString(R
.string
.downloader_download_failed_content
), new File(
115 ((DownloadFileOperation
) operation
).getSavePath()).getName());
119 } else if (operation
instanceof RemoveFileOperation
) {
120 if (result
.isSuccess()) {
121 message
= res
.getString(R
.string
.remove_success_msg
);
124 if (result
.getCode().equals(ResultCode
.FORBIDDEN
)) {
125 // Error --> No permissions
126 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
127 res
.getString(R
.string
.forbidden_permissions_delete
));
129 message
= res
.getString(R
.string
.remove_fail_msg
);
133 } else if (operation
instanceof RenameFileOperation
) {
134 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
135 message
= res
.getString(R
.string
.rename_local_fail_msg
);
137 } else if (result
.getCode().equals(ResultCode
.FORBIDDEN
)) {
138 // Error --> No permissions
139 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
140 res
.getString(R
.string
.forbidden_permissions_rename
));
142 } else if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
143 message
= res
.getString(R
.string
.filename_forbidden_characters
);
145 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
146 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
149 message
= res
.getString(R
.string
.rename_server_fail_msg
);
152 } else if (operation
instanceof SynchronizeFileOperation
) {
153 if (!((SynchronizeFileOperation
) operation
).transferWasRequested()) {
154 message
= res
.getString(R
.string
.sync_file_nothing_to_do_msg
);
157 } else if (operation
instanceof CreateFolderOperation
) {
158 if (result
.getCode() == ResultCode
.INVALID_CHARACTER_IN_NAME
) {
159 message
= res
.getString(R
.string
.filename_forbidden_characters
);
161 } else if (result
.getCode().equals(ResultCode
.FORBIDDEN
)) {
162 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
163 res
.getString(R
.string
.forbidden_permissions_create
));
165 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
166 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
168 message
= res
.getString(R
.string
.create_dir_fail_msg
);
170 } else if (operation
instanceof CreateShareViaLinkOperation
||
171 operation
instanceof CreateShareWithShareeOperation
) {
173 if (result
.getData() != null
&& result
.getData().size() > 0) {
174 message
= (String
) result
.getData().get(0); // share API sends its own error messages
176 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
177 message
= res
.getString(R
.string
.share_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
.share_link_forbidden_permissions
));
184 } else { // Generic error
185 // Show a Message, operation finished without success
186 message
= res
.getString(R
.string
.share_link_file_error
);
189 } else if (operation
instanceof UnshareOperation
) {
191 if (result
.getData() != null
&& result
.getData().size() > 0) {
192 message
= (String
) result
.getData().get(0); // share API sends its own error messages
194 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
195 message
= res
.getString(R
.string
.unshare_link_file_no_exist
);
197 } else if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
198 // Error --> No permissions
199 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
200 res
.getString(R
.string
.unshare_link_forbidden_permissions
));
202 } else { // Generic error
203 // Show a Message, operation finished without success
204 message
= res
.getString(R
.string
.unshare_link_file_error
);
207 } else if (operation
instanceof UpdateShareViaLinkOperation
) {
209 if (result
.getData() != null
&& result
.getData().size() > 0) {
210 message
= (String
) result
.getData().get(0); // share API sends its own error messages
212 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
213 message
= res
.getString(R
.string
.update_link_file_no_exist
);
215 } else if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
216 // Error --> No permissions
217 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
218 res
.getString(R
.string
.update_link_forbidden_permissions
));
220 } else { // Generic error
221 // Show a Message, operation finished without success
222 message
= res
.getString(R
.string
.update_link_file_error
);
225 } else if (operation
instanceof MoveFileOperation
) {
227 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
228 message
= res
.getString(R
.string
.move_file_not_found
);
229 } else if (result
.getCode() == ResultCode
.INVALID_MOVE_INTO_DESCENDANT
) {
230 message
= res
.getString(R
.string
.move_file_invalid_into_descendent
);
232 } else if (result
.getCode() == ResultCode
.INVALID_OVERWRITE
) {
233 message
= res
.getString(R
.string
.move_file_invalid_overwrite
);
235 } else if (result
.getCode() == ResultCode
.FORBIDDEN
) {
236 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
237 res
.getString(R
.string
.forbidden_permissions_move
));
239 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
240 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
242 } else { // Generic error
243 // Show a Message, operation finished without success
244 message
= res
.getString(R
.string
.move_file_error
);
246 } else if (operation
instanceof SynchronizeFolderOperation
) {
248 if (!result
.isSuccess()) {
249 String folderPathName
= new File(
250 ((SynchronizeFolderOperation
) operation
).getFolderPath()).getName();
251 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
252 message
= String
.format(res
.getString(R
.string
.sync_current_folder_was_removed
),
255 } else { // Generic error
256 // Show a Message, operation finished without success
257 message
= String
.format(res
.getString(R
.string
.sync_folder_failed_content
),
261 } else if (operation
instanceof CopyFileOperation
) {
262 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
263 message
= res
.getString(R
.string
.copy_file_not_found
);
264 } else if (result
.getCode() == ResultCode
.INVALID_COPY_INTO_DESCENDANT
) {
265 message
= res
.getString(R
.string
.copy_file_invalid_into_descendent
);
267 } else if (result
.getCode() == ResultCode
.INVALID_OVERWRITE
) {
268 message
= res
.getString(R
.string
.copy_file_invalid_overwrite
);
270 } else if (result
.getCode() == ResultCode
.FORBIDDEN
) {
271 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
272 res
.getString(R
.string
.forbidden_permissions_copy
));
274 } else { // Generic error
275 // Show a Message, operation finished without success
276 message
= res
.getString(R
.string
.copy_file_error
);
283 private static String
getErrorMessage(RemoteOperationResult result
, Resources res
) {
285 String message
= null
;
287 if (!result
.isSuccess()) {
289 if (result
.getCode() == ResultCode
.WRONG_CONNECTION
) {
290 message
= res
.getString(R
.string
.network_error_socket_exception
);
292 } else if (result
.getCode() == ResultCode
.TIMEOUT
) {
293 message
= res
.getString(R
.string
.network_error_socket_exception
);
295 if (result
.getException() instanceof SocketTimeoutException
) {
296 message
= res
.getString(R
.string
.network_error_socket_timeout_exception
);
297 } else if (result
.getException() instanceof ConnectTimeoutException
) {
298 message
= res
.getString(R
.string
.network_error_connect_timeout_exception
);
301 } else if (result
.getCode() == ResultCode
.HOST_NOT_AVAILABLE
) {
302 message
= res
.getString(R
.string
.network_host_not_available
);
309 private static boolean isNetworkError(RemoteOperationResult
.ResultCode code
) {
310 if (code
== ResultCode
.WRONG_CONNECTION
||
311 code
== ResultCode
.TIMEOUT
||
312 code
== ResultCode
.HOST_NOT_AVAILABLE
) {