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
.operations
.CopyFileOperation
;
31 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
32 import com
.owncloud
.android
.operations
.CreateShareViaLinkOperation
;
33 import com
.owncloud
.android
.operations
.CreateShareWithShareeOperation
;
34 import com
.owncloud
.android
.operations
.DownloadFileOperation
;
35 import com
.owncloud
.android
.operations
.MoveFileOperation
;
36 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
37 import com
.owncloud
.android
.operations
.RenameFileOperation
;
38 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
39 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
40 import com
.owncloud
.android
.operations
.UnshareOperation
;
41 import com
.owncloud
.android
.operations
.UploadFileOperation
;
43 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
46 import java
.net
.SocketTimeoutException
;
49 * Class to choose proper error messages to show to the user depending on the results of operations,
50 * always following the same policy
53 public class ErrorMessageAdapter
{
55 public ErrorMessageAdapter() {
59 public static String
getErrorCauseMessage(RemoteOperationResult result
,
60 RemoteOperation operation
, Resources res
) {
62 String message
= null
;
64 if (!result
.isSuccess() && isNetworkError(result
.getCode())) {
65 message
= getErrorMessage(result
, res
);
67 } else if (operation
instanceof UploadFileOperation
) {
69 if (result
.isSuccess()) {
70 message
= String
.format(
71 res
.getString(R
.string
.uploader_upload_succeeded_content_single
),
72 ((UploadFileOperation
) operation
).getFileName());
74 if (result
.getCode() == ResultCode
.LOCAL_STORAGE_FULL
75 || result
.getCode() == ResultCode
.LOCAL_STORAGE_NOT_COPIED
) {
76 message
= String
.format(
77 res
.getString(R
.string
.error__upload__local_file_not_copied
),
78 ((UploadFileOperation
) operation
).getFileName(),
79 res
.getString(R
.string
.app_name
));
81 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
82 message = res.getString(R.string.failed_upload_quota_exceeded_text);
85 } else if (result
.getCode() == ResultCode
.FORBIDDEN
) {
86 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
87 res
.getString(R
.string
.uploader_upload_forbidden_permissions
));
89 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
90 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
93 message
= String
.format(
94 res
.getString(R
.string
.uploader_upload_failed_content_single
),
95 ((UploadFileOperation
) operation
).getFileName());
99 } else if (operation
instanceof DownloadFileOperation
) {
101 if (result
.isSuccess()) {
102 message
= String
.format(
103 res
.getString(R
.string
.downloader_download_succeeded_content
),
104 new File(((DownloadFileOperation
) operation
).getSavePath()).getName());
107 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
108 message
= res
.getString(R
.string
.downloader_download_file_not_found
);
111 message
= String
.format(
112 res
.getString(R
.string
.downloader_download_failed_content
), new File(
113 ((DownloadFileOperation
) operation
).getSavePath()).getName());
117 } else if (operation
instanceof RemoveFileOperation
) {
118 if (result
.isSuccess()) {
119 message
= res
.getString(R
.string
.remove_success_msg
);
122 if (result
.getCode().equals(ResultCode
.FORBIDDEN
)) {
123 // Error --> No permissions
124 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
125 res
.getString(R
.string
.forbidden_permissions_delete
));
127 message
= res
.getString(R
.string
.remove_fail_msg
);
131 } else if (operation
instanceof RenameFileOperation
) {
132 if (result
.getCode().equals(ResultCode
.INVALID_LOCAL_FILE_NAME
)) {
133 message
= res
.getString(R
.string
.rename_local_fail_msg
);
135 } else if (result
.getCode().equals(ResultCode
.FORBIDDEN
)) {
136 // Error --> No permissions
137 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
138 res
.getString(R
.string
.forbidden_permissions_rename
));
140 } else if (result
.getCode().equals(ResultCode
.INVALID_CHARACTER_IN_NAME
)) {
141 message
= res
.getString(R
.string
.filename_forbidden_characters
);
143 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
144 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
147 message
= res
.getString(R
.string
.rename_server_fail_msg
);
150 } else if (operation
instanceof SynchronizeFileOperation
) {
151 if (!((SynchronizeFileOperation
) operation
).transferWasRequested()) {
152 message
= res
.getString(R
.string
.sync_file_nothing_to_do_msg
);
155 } else if (operation
instanceof CreateFolderOperation
) {
156 if (result
.getCode() == ResultCode
.INVALID_CHARACTER_IN_NAME
) {
157 message
= res
.getString(R
.string
.filename_forbidden_characters
);
159 } else if (result
.getCode().equals(ResultCode
.FORBIDDEN
)) {
160 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
161 res
.getString(R
.string
.forbidden_permissions_create
));
163 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
164 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
166 message
= res
.getString(R
.string
.create_dir_fail_msg
);
168 } else if (operation
instanceof CreateShareViaLinkOperation
||
169 operation
instanceof CreateShareWithShareeOperation
) {
171 if (result
.getData() != null
&& result
.getData().size() > 0) {
172 message
= (String
) result
.getData().get(0); // share API sends its own error messages
174 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
175 message
= res
.getString(R
.string
.share_link_file_no_exist
);
177 } else if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
178 // Error --> No permissions
179 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
180 res
.getString(R
.string
.share_link_forbidden_permissions
));
182 } else { // Generic error
183 // Show a Message, operation finished without success
184 message
= res
.getString(R
.string
.share_link_file_error
);
187 } else if (operation
instanceof UnshareOperation
) {
189 if (result
.getData() != null
&& result
.getData().size() > 0) {
190 message
= (String
) result
.getData().get(0); // share API sends its own error messages
192 } else if (result
.getCode() == ResultCode
.SHARE_NOT_FOUND
) {
193 message
= res
.getString(R
.string
.unshare_link_file_no_exist
);
195 } else if (result
.getCode() == ResultCode
.SHARE_FORBIDDEN
) {
196 // Error --> No permissions
197 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
198 res
.getString(R
.string
.unshare_link_forbidden_permissions
));
200 } else { // Generic error
201 // Show a Message, operation finished without success
202 message
= res
.getString(R
.string
.unshare_link_file_error
);
204 } else if (operation
instanceof MoveFileOperation
) {
206 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
207 message
= res
.getString(R
.string
.move_file_not_found
);
208 } else if (result
.getCode() == ResultCode
.INVALID_MOVE_INTO_DESCENDANT
) {
209 message
= res
.getString(R
.string
.move_file_invalid_into_descendent
);
211 } else if (result
.getCode() == ResultCode
.INVALID_OVERWRITE
) {
212 message
= res
.getString(R
.string
.move_file_invalid_overwrite
);
214 } else if (result
.getCode() == ResultCode
.FORBIDDEN
) {
215 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
216 res
.getString(R
.string
.forbidden_permissions_move
));
218 } else if (result
.getCode() == ResultCode
.INVALID_CHARACTER_DETECT_IN_SERVER
) {
219 message
= res
.getString(R
.string
.filename_forbidden_charaters_from_server
);
221 } else { // Generic error
222 // Show a Message, operation finished without success
223 message
= res
.getString(R
.string
.move_file_error
);
225 } else if (operation
instanceof SynchronizeFolderOperation
) {
227 if (!result
.isSuccess()) {
228 String folderPathName
= new File(
229 ((SynchronizeFolderOperation
) operation
).getFolderPath()).getName();
230 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
231 message
= String
.format(res
.getString(R
.string
.sync_current_folder_was_removed
),
234 } else { // Generic error
235 // Show a Message, operation finished without success
236 message
= String
.format(res
.getString(R
.string
.sync_folder_failed_content
),
240 } else if (operation
instanceof CopyFileOperation
) {
241 if (result
.getCode() == ResultCode
.FILE_NOT_FOUND
) {
242 message
= res
.getString(R
.string
.copy_file_not_found
);
243 } else if (result
.getCode() == ResultCode
.INVALID_COPY_INTO_DESCENDANT
) {
244 message
= res
.getString(R
.string
.copy_file_invalid_into_descendent
);
246 } else if (result
.getCode() == ResultCode
.INVALID_OVERWRITE
) {
247 message
= res
.getString(R
.string
.copy_file_invalid_overwrite
);
249 } else if (result
.getCode() == ResultCode
.FORBIDDEN
) {
250 message
= String
.format(res
.getString(R
.string
.forbidden_permissions
),
251 res
.getString(R
.string
.forbidden_permissions_copy
));
253 } else { // Generic error
254 // Show a Message, operation finished without success
255 message
= res
.getString(R
.string
.copy_file_error
);
262 private static String
getErrorMessage(RemoteOperationResult result
, Resources res
) {
264 String message
= null
;
266 if (!result
.isSuccess()) {
268 if (result
.getCode() == ResultCode
.WRONG_CONNECTION
) {
269 message
= res
.getString(R
.string
.network_error_socket_exception
);
271 } else if (result
.getCode() == ResultCode
.TIMEOUT
) {
272 message
= res
.getString(R
.string
.network_error_socket_exception
);
274 if (result
.getException() instanceof SocketTimeoutException
) {
275 message
= res
.getString(R
.string
.network_error_socket_timeout_exception
);
276 } else if (result
.getException() instanceof ConnectTimeoutException
) {
277 message
= res
.getString(R
.string
.network_error_connect_timeout_exception
);
280 } else if (result
.getCode() == ResultCode
.HOST_NOT_AVAILABLE
) {
281 message
= res
.getString(R
.string
.network_host_not_available
);
288 private static boolean isNetworkError(RemoteOperationResult
.ResultCode code
) {
289 if (code
== ResultCode
.WRONG_CONNECTION
||
290 code
== ResultCode
.TIMEOUT
||
291 code
== ResultCode
.HOST_NOT_AVAILABLE
) {