Show error messages from server when share-specific errors occur while sharing or...
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / ErrorMessageAdapter.java
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
21
22 package com.owncloud.android.utils;
23
24 import android.content.res.Resources;
25
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;
42
43 import org.apache.commons.httpclient.ConnectTimeoutException;
44
45 import java.io.File;
46 import java.net.SocketTimeoutException;
47
48 /**
49 * Class to choose proper error messages to show to the user depending on the results of operations,
50 * always following the same policy
51 */
52
53 public class ErrorMessageAdapter {
54
55 public ErrorMessageAdapter() {
56
57 }
58
59 public static String getErrorCauseMessage(RemoteOperationResult result,
60 RemoteOperation operation, Resources res) {
61
62 String message = null;
63
64 if (operation instanceof UploadFileOperation) {
65
66 if (result.isSuccess()) {
67 message = String.format(
68 res.getString(R.string.uploader_upload_succeeded_content_single),
69 ((UploadFileOperation) operation).getFileName());
70 } else {
71 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
72 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
73 message = String.format(
74 res.getString(R.string.error__upload__local_file_not_copied),
75 ((UploadFileOperation) operation).getFileName(),
76 res.getString(R.string.app_name));
77 /*
78 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
79 message = res.getString(R.string.failed_upload_quota_exceeded_text);
80 */
81
82 } else if (result.getCode() == ResultCode.FORBIDDEN) {
83 message = String.format(res.getString(R.string.forbidden_permissions),
84 res.getString(R.string.uploader_upload_forbidden_permissions));
85
86 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
87 message = res.getString(R.string.filename_forbidden_charaters_from_server);
88
89 } else {
90 message = String.format(
91 res.getString(R.string.uploader_upload_failed_content_single),
92 ((UploadFileOperation) operation).getFileName());
93 }
94 }
95
96 } else if (operation instanceof DownloadFileOperation) {
97
98 if (result.isSuccess()) {
99 message = String.format(
100 res.getString(R.string.downloader_download_succeeded_content),
101 new File(((DownloadFileOperation) operation).getSavePath()).getName());
102
103 } else {
104 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
105 message = res.getString(R.string.downloader_download_file_not_found);
106
107 } else {
108 message = String.format(
109 res.getString(R.string.downloader_download_failed_content), new File(
110 ((DownloadFileOperation) operation).getSavePath()).getName());
111 }
112 }
113
114 } else if (operation instanceof RemoveFileOperation) {
115 if (result.isSuccess()) {
116 message = res.getString(R.string.remove_success_msg);
117
118 } else {
119 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
120 // Error --> No permissions
121 message = String.format(res.getString(R.string.forbidden_permissions),
122 res.getString(R.string.forbidden_permissions_delete));
123 } else if (isNetworkError(result.getCode())) {
124 message = getErrorMessage(result, res);
125
126 } else {
127 message = res.getString(R.string.remove_fail_msg);
128 }
129 }
130
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);
134
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));
139
140 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
141 message = res.getString(R.string.filename_forbidden_characters);
142
143 } else if (isNetworkError(result.getCode())) {
144 message = getErrorMessage(result, res);
145
146 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
147 message = res.getString(R.string.filename_forbidden_charaters_from_server);
148
149 } else {
150 message = res.getString(R.string.rename_server_fail_msg);
151 }
152
153 } else if (operation instanceof SynchronizeFileOperation) {
154 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
155 message = res.getString(R.string.sync_file_nothing_to_do_msg);
156 }
157
158 } else if (operation instanceof CreateFolderOperation) {
159 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
160 message = res.getString(R.string.filename_forbidden_characters);
161
162 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
163 message = String.format(res.getString(R.string.forbidden_permissions),
164 res.getString(R.string.forbidden_permissions_create));
165
166 } else if (isNetworkError(result.getCode())) {
167 message = getErrorMessage(result, res);
168
169 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
170 message = res.getString(R.string.filename_forbidden_charaters_from_server);
171 } else {
172 message = res.getString(R.string.create_dir_fail_msg);
173 }
174 } else if (operation instanceof CreateShareViaLinkOperation ||
175 operation instanceof CreateShareWithShareeOperation) {
176
177 if (result.getData() != null && result.getData().size() > 0) {
178 message = (String) result.getData().get(0); // share API sends its own error messages
179
180 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
181 message = res.getString(R.string.share_link_file_no_exist);
182
183 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
184 // Error --> No permissions
185 message = String.format(res.getString(R.string.forbidden_permissions),
186 res.getString(R.string.share_link_forbidden_permissions));
187
188 } else if (isNetworkError(result.getCode())) {
189 message = getErrorMessage(result, res);
190
191 } else { // Generic error
192 // Show a Message, operation finished without success
193 message = res.getString(R.string.share_link_file_error);
194 }
195
196 } else if (operation instanceof UnshareOperation) {
197
198 if (result.getData() != null && result.getData().size() > 0) {
199 message = (String) result.getData().get(0); // share API sends its own error messages
200
201 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
202 message = res.getString(R.string.unshare_link_file_no_exist);
203
204 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
205 // Error --> No permissions
206 message = String.format(res.getString(R.string.forbidden_permissions),
207 res.getString(R.string.unshare_link_forbidden_permissions));
208
209 } else if (isNetworkError(result.getCode())) {
210 message = getErrorMessage(result, res);
211
212 } else { // Generic error
213 // Show a Message, operation finished without success
214 message = res.getString(R.string.unshare_link_file_error);
215 }
216 } else if (operation instanceof MoveFileOperation) {
217
218 if(isNetworkError(result.getCode())){
219 message = getErrorMessage(result, res);
220 } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
221 message = res.getString(R.string.move_file_not_found);
222 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
223 message = res.getString(R.string.move_file_invalid_into_descendent);
224
225 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
226 message = res.getString(R.string.move_file_invalid_overwrite);
227
228 } else if (result.getCode() == ResultCode.FORBIDDEN) {
229 message = String.format(res.getString(R.string.forbidden_permissions),
230 res.getString(R.string.forbidden_permissions_move));
231
232 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
233 message = res.getString(R.string.filename_forbidden_charaters_from_server);
234
235 } else { // Generic error
236 // Show a Message, operation finished without success
237 message = res.getString(R.string.move_file_error);
238 }
239 } else if (operation instanceof SynchronizeFolderOperation) {
240
241 if (!result.isSuccess()) {
242 String folderPathName = new File(
243 ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
244 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
245 message = String.format(res.getString(R.string.sync_current_folder_was_removed),
246 folderPathName);
247
248 } else { // Generic error
249 // Show a Message, operation finished without success
250 message = String.format(res.getString(R.string.sync_folder_failed_content),
251 folderPathName);
252 }
253 }
254 } else if (operation instanceof CopyFileOperation) {
255 if(isNetworkError(result.getCode())){
256 message = getErrorMessage(result, res);
257 } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
258 message = res.getString(R.string.copy_file_not_found);
259 } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
260 message = res.getString(R.string.copy_file_invalid_into_descendent);
261
262 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
263 message = res.getString(R.string.copy_file_invalid_overwrite);
264
265 } else if (result.getCode() == ResultCode.FORBIDDEN) {
266 message = String.format(res.getString(R.string.forbidden_permissions),
267 res.getString(R.string.forbidden_permissions_copy));
268
269 } else { // Generic error
270 // Show a Message, operation finished without success
271 message = res.getString(R.string.copy_file_error);
272 }
273 }
274
275 return message;
276 }
277
278 private static String getErrorMessage(RemoteOperationResult result, Resources res) {
279
280 String message = null;
281
282 if (!result.isSuccess()) {
283
284 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
285 message = res.getString(R.string.network_error_socket_exception);
286
287 } else if (result.getCode() == ResultCode.TIMEOUT) {
288 message = res.getString(R.string.network_error_socket_exception);
289
290 if (result.getException() instanceof SocketTimeoutException) {
291 message = res.getString(R.string.network_error_socket_timeout_exception);
292 } else if (result.getException() instanceof ConnectTimeoutException) {
293 message = res.getString(R.string.network_error_connect_timeout_exception);
294 }
295
296 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
297 message = res.getString(R.string.network_host_not_available);
298 }
299 }
300
301 return message;
302 }
303
304 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
305 if (code == ResultCode.WRONG_CONNECTION ||
306 code == ResultCode.TIMEOUT ||
307 code == ResultCode.HOST_NOT_AVAILABLE) {
308 return true;
309 } else
310 return false;
311 }
312 }