[tx-robot] updated from transifex
[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.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;
44
45 import org.apache.commons.httpclient.ConnectTimeoutException;
46
47 import java.io.File;
48 import java.net.SocketTimeoutException;
49
50 /**
51 * Class to choose proper error messages to show to the user depending on the results of operations,
52 * always following the same policy
53 */
54
55 public class ErrorMessageAdapter {
56
57 public ErrorMessageAdapter() {
58
59 }
60
61 public static String getErrorCauseMessage(RemoteOperationResult result,
62 RemoteOperation operation, Resources res) {
63
64 String message = null;
65
66 if (!result.isSuccess() && isNetworkError(result.getCode())) {
67 message = getErrorMessage(result, res);
68
69 } else if (operation instanceof UploadFileOperation) {
70
71 if (result.isSuccess()) {
72 message = String.format(
73 res.getString(R.string.uploader_upload_succeeded_content_single),
74 ((UploadFileOperation) operation).getFileName());
75 } else {
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));
82 /*
83 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
84 message = res.getString(R.string.failed_upload_quota_exceeded_text);
85 */
86
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));
90
91 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
92 message = res.getString(R.string.filename_forbidden_charaters_from_server);
93
94 } else {
95 message = String.format(
96 res.getString(R.string.uploader_upload_failed_content_single),
97 ((UploadFileOperation) operation).getFileName());
98 }
99 }
100
101 } else if (operation instanceof DownloadFileOperation) {
102
103 if (result.isSuccess()) {
104 message = String.format(
105 res.getString(R.string.downloader_download_succeeded_content),
106 new File(((DownloadFileOperation) operation).getSavePath()).getName());
107
108 } else {
109 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
110 message = res.getString(R.string.downloader_download_file_not_found);
111
112 } else {
113 message = String.format(
114 res.getString(R.string.downloader_download_failed_content), new File(
115 ((DownloadFileOperation) operation).getSavePath()).getName());
116 }
117 }
118
119 } else if (operation instanceof RemoveFileOperation) {
120 if (result.isSuccess()) {
121 message = res.getString(R.string.remove_success_msg);
122
123 } else {
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));
128 } else {
129 message = res.getString(R.string.remove_fail_msg);
130 }
131 }
132
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);
136
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));
141
142 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
143 message = res.getString(R.string.filename_forbidden_characters);
144
145 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
146 message = res.getString(R.string.filename_forbidden_charaters_from_server);
147
148 } else {
149 message = res.getString(R.string.rename_server_fail_msg);
150 }
151
152 } else if (operation instanceof SynchronizeFileOperation) {
153 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
154 message = res.getString(R.string.sync_file_nothing_to_do_msg);
155 }
156
157 } else if (operation instanceof CreateFolderOperation) {
158 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
159 message = res.getString(R.string.filename_forbidden_characters);
160
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));
164
165 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
166 message = res.getString(R.string.filename_forbidden_charaters_from_server);
167 } else {
168 message = res.getString(R.string.create_dir_fail_msg);
169 }
170 } else if (operation instanceof CreateShareViaLinkOperation ||
171 operation instanceof CreateShareWithShareeOperation) {
172
173 if (result.getData() != null && result.getData().size() > 0) {
174 message = (String) result.getData().get(0); // share API sends its own error messages
175
176 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
177 message = res.getString(R.string.share_link_file_no_exist);
178
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));
183
184 } else { // Generic error
185 // Show a Message, operation finished without success
186 message = res.getString(R.string.share_link_file_error);
187 }
188
189 } else if (operation instanceof UnshareOperation) {
190
191 if (result.getData() != null && result.getData().size() > 0) {
192 message = (String) result.getData().get(0); // share API sends its own error messages
193
194 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
195 message = res.getString(R.string.unshare_link_file_no_exist);
196
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));
201
202 } else { // Generic error
203 // Show a Message, operation finished without success
204 message = res.getString(R.string.unshare_link_file_error);
205 }
206
207 } else if (operation instanceof UpdateShareViaLinkOperation) {
208
209 if (result.getData() != null && result.getData().size() > 0) {
210 message = (String) result.getData().get(0); // share API sends its own error messages
211
212 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
213 message = res.getString(R.string.update_link_file_no_exist);
214
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));
219
220 } else { // Generic error
221 // Show a Message, operation finished without success
222 message = res.getString(R.string.update_link_file_error);
223 }
224
225 } else if (operation instanceof MoveFileOperation) {
226
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);
231
232 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
233 message = res.getString(R.string.move_file_invalid_overwrite);
234
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));
238
239 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
240 message = res.getString(R.string.filename_forbidden_charaters_from_server);
241
242 } else { // Generic error
243 // Show a Message, operation finished without success
244 message = res.getString(R.string.move_file_error);
245 }
246 } else if (operation instanceof SynchronizeFolderOperation) {
247
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),
253 folderPathName);
254
255 } else { // Generic error
256 // Show a Message, operation finished without success
257 message = String.format(res.getString(R.string.sync_folder_failed_content),
258 folderPathName);
259 }
260 }
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);
266
267 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
268 message = res.getString(R.string.copy_file_invalid_overwrite);
269
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));
273
274 } else { // Generic error
275 // Show a Message, operation finished without success
276 message = res.getString(R.string.copy_file_error);
277 }
278 }
279
280 return message;
281 }
282
283 private static String getErrorMessage(RemoteOperationResult result, Resources res) {
284
285 String message = null;
286
287 if (!result.isSuccess()) {
288
289 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
290 message = res.getString(R.string.network_error_socket_exception);
291
292 } else if (result.getCode() == ResultCode.TIMEOUT) {
293 message = res.getString(R.string.network_error_socket_exception);
294
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);
299 }
300
301 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
302 message = res.getString(R.string.network_host_not_available);
303 }
304 }
305
306 return message;
307 }
308
309 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
310 if (code == ResultCode.WRONG_CONNECTION ||
311 code == ResultCode.TIMEOUT ||
312 code == ResultCode.HOST_NOT_AVAILABLE) {
313 return true;
314 } else
315 return false;
316 }
317 }