Fixed build after rebasing
[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 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> 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 if (isNetworkError(result.getCode())) {
185 message = getErrorMessage(result, res);
186
187 } else { // Generic error
188 // Show a Message, operation finished without success
189 message = res.getString(R.string.share_link_file_error);
190 }
191
192 } else if (operation instanceof UnshareOperation) {
193
194 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> 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 if (isNetworkError(result.getCode())) {
203 message = getErrorMessage(result, res);
204
205 } else { // Generic error
206 // Show a Message, operation finished without success
207 message = res.getString(R.string.unshare_link_file_error);
208 }
209 } else if (operation instanceof MoveFileOperation) {
210
211 if(isNetworkError(result.getCode())){
212 message = getErrorMessage(result, res);
213 } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
214 message = res.getString(R.string.move_file_not_found);
215 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
216 message = res.getString(R.string.move_file_invalid_into_descendent);
217
218 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
219 message = res.getString(R.string.move_file_invalid_overwrite);
220
221 } else if (result.getCode() == ResultCode.FORBIDDEN) {
222 message = String.format(res.getString(R.string.forbidden_permissions),
223 res.getString(R.string.forbidden_permissions_move));
224
225 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
226 message = res.getString(R.string.filename_forbidden_charaters_from_server);
227
228 } else { // Generic error
229 // Show a Message, operation finished without success
230 message = res.getString(R.string.move_file_error);
231 }
232 } else if (operation instanceof SynchronizeFolderOperation) {
233
234 if (!result.isSuccess()) {
235 String folderPathName = new File(
236 ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
237 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
238 message = String.format(res.getString(R.string.sync_current_folder_was_removed),
239 folderPathName);
240
241 } else { // Generic error
242 // Show a Message, operation finished without success
243 message = String.format(res.getString(R.string.sync_folder_failed_content),
244 folderPathName);
245 }
246 }
247 } else if (operation instanceof CopyFileOperation) {
248 if(isNetworkError(result.getCode())){
249 message = getErrorMessage(result, res);
250 } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
251 message = res.getString(R.string.copy_file_not_found);
252 } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
253 message = res.getString(R.string.copy_file_invalid_into_descendent);
254
255 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
256 message = res.getString(R.string.copy_file_invalid_overwrite);
257
258 } else if (result.getCode() == ResultCode.FORBIDDEN) {
259 message = String.format(res.getString(R.string.forbidden_permissions),
260 res.getString(R.string.forbidden_permissions_copy));
261
262 } else { // Generic error
263 // Show a Message, operation finished without success
264 message = res.getString(R.string.copy_file_error);
265 }
266 }
267
268 return message;
269 }
270
271 private static String getErrorMessage(RemoteOperationResult result, Resources res) {
272
273 String message = null;
274
275 if (!result.isSuccess()) {
276
277 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
278 message = res.getString(R.string.network_error_socket_exception);
279
280 } else if (result.getCode() == ResultCode.TIMEOUT) {
281 message = res.getString(R.string.network_error_socket_exception);
282
283 if (result.getException() instanceof SocketTimeoutException) {
284 message = res.getString(R.string.network_error_socket_timeout_exception);
285 } else if (result.getException() instanceof ConnectTimeoutException) {
286 message = res.getString(R.string.network_error_connect_timeout_exception);
287 }
288
289 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
290 message = res.getString(R.string.network_host_not_available);
291 }
292 }
293
294 return message;
295 }
296
297 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
298 if (code == ResultCode.WRONG_CONNECTION ||
299 code == ResultCode.TIMEOUT ||
300 code == ResultCode.HOST_NOT_AVAILABLE) {
301 return true;
302 } else
303 return false;
304 }
305 }