Merge branch '1193_uploader_layout' of https://github.com/owncloud/android into beta
[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 (!result.isSuccess() && isNetworkError(result.getCode())) {
65 message = getErrorMessage(result, res);
66
67 } else if (operation instanceof UploadFileOperation) {
68
69 if (result.isSuccess()) {
70 message = String.format(
71 res.getString(R.string.uploader_upload_succeeded_content_single),
72 ((UploadFileOperation) operation).getFileName());
73 } else {
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));
80 /*
81 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
82 message = res.getString(R.string.failed_upload_quota_exceeded_text);
83 */
84
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));
88
89 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
90 message = res.getString(R.string.filename_forbidden_charaters_from_server);
91
92 } else {
93 message = String.format(
94 res.getString(R.string.uploader_upload_failed_content_single),
95 ((UploadFileOperation) operation).getFileName());
96 }
97 }
98
99 } else if (operation instanceof DownloadFileOperation) {
100
101 if (result.isSuccess()) {
102 message = String.format(
103 res.getString(R.string.downloader_download_succeeded_content),
104 new File(((DownloadFileOperation) operation).getSavePath()).getName());
105
106 } else {
107 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
108 message = res.getString(R.string.downloader_download_file_not_found);
109
110 } else {
111 message = String.format(
112 res.getString(R.string.downloader_download_failed_content), new File(
113 ((DownloadFileOperation) operation).getSavePath()).getName());
114 }
115 }
116
117 } else if (operation instanceof RemoveFileOperation) {
118 if (result.isSuccess()) {
119 message = res.getString(R.string.remove_success_msg);
120
121 } else {
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));
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 (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
144 message = res.getString(R.string.filename_forbidden_charaters_from_server);
145
146 } else {
147 message = res.getString(R.string.rename_server_fail_msg);
148 }
149
150 } else if (operation instanceof SynchronizeFileOperation) {
151 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
152 message = res.getString(R.string.sync_file_nothing_to_do_msg);
153 }
154
155 } else if (operation instanceof CreateFolderOperation) {
156 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
157 message = res.getString(R.string.filename_forbidden_characters);
158
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));
162
163 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
164 message = res.getString(R.string.filename_forbidden_charaters_from_server);
165 } else {
166 message = res.getString(R.string.create_dir_fail_msg);
167 }
168 } else if (operation instanceof CreateShareViaLinkOperation ||
169 operation instanceof CreateShareWithShareeOperation) {
170
171 if (result.getData() != null && result.getData().size() > 0) {
172 message = (String) result.getData().get(0); // share API sends its own error messages
173
174 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
175 message = res.getString(R.string.share_link_file_no_exist);
176
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));
181
182 } else { // Generic error
183 // Show a Message, operation finished without success
184 message = res.getString(R.string.share_link_file_error);
185 }
186
187 } else if (operation instanceof UnshareOperation) {
188
189 if (result.getData() != null && result.getData().size() > 0) {
190 message = (String) result.getData().get(0); // share API sends its own error messages
191
192 } else if (result.getCode() == ResultCode.SHARE_NOT_FOUND) {
193 message = res.getString(R.string.unshare_link_file_no_exist);
194
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));
199
200 } else { // Generic error
201 // Show a Message, operation finished without success
202 message = res.getString(R.string.unshare_link_file_error);
203 }
204 } else if (operation instanceof MoveFileOperation) {
205
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);
210
211 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
212 message = res.getString(R.string.move_file_invalid_overwrite);
213
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));
217
218 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
219 message = res.getString(R.string.filename_forbidden_charaters_from_server);
220
221 } else { // Generic error
222 // Show a Message, operation finished without success
223 message = res.getString(R.string.move_file_error);
224 }
225 } else if (operation instanceof SynchronizeFolderOperation) {
226
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),
232 folderPathName);
233
234 } else { // Generic error
235 // Show a Message, operation finished without success
236 message = String.format(res.getString(R.string.sync_folder_failed_content),
237 folderPathName);
238 }
239 }
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);
245
246 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
247 message = res.getString(R.string.copy_file_invalid_overwrite);
248
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));
252
253 } else { // Generic error
254 // Show a Message, operation finished without success
255 message = res.getString(R.string.copy_file_error);
256 }
257 }
258
259 return message;
260 }
261
262 private static String getErrorMessage(RemoteOperationResult result, Resources res) {
263
264 String message = null;
265
266 if (!result.isSuccess()) {
267
268 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
269 message = res.getString(R.string.network_error_socket_exception);
270
271 } else if (result.getCode() == ResultCode.TIMEOUT) {
272 message = res.getString(R.string.network_error_socket_exception);
273
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);
278 }
279
280 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
281 message = res.getString(R.string.network_host_not_available);
282 }
283 }
284
285 return message;
286 }
287
288 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
289 if (code == ResultCode.WRONG_CONNECTION ||
290 code == ResultCode.TIMEOUT ||
291 code == ResultCode.HOST_NOT_AVAILABLE) {
292 return true;
293 } else
294 return false;
295 }
296 }