Merge branch 'develop' into forbidden_characters_from_server
[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 java.io.File;
25 import java.net.SocketTimeoutException;
26
27 import org.apache.commons.httpclient.ConnectTimeoutException;
28
29 import android.content.res.Resources;
30
31 import com.owncloud.android.R;
32 import com.owncloud.android.lib.common.operations.RemoteOperation;
33 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
34 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
35 import com.owncloud.android.operations.CreateFolderOperation;
36 import com.owncloud.android.operations.CreateShareOperation;
37 import com.owncloud.android.operations.DownloadFileOperation;
38 import com.owncloud.android.operations.MoveFileOperation;
39 import com.owncloud.android.operations.RemoveFileOperation;
40 import com.owncloud.android.operations.RenameFileOperation;
41 import com.owncloud.android.operations.SynchronizeFileOperation;
42 import com.owncloud.android.operations.SynchronizeFolderOperation;
43 import com.owncloud.android.operations.UnshareLinkOperation;
44 import com.owncloud.android.operations.UploadFileOperation;
45
46 /**
47 * Class to choose proper error messages to show to the user depending on the results of operations,
48 * always following the same policy
49 */
50
51 public class ErrorMessageAdapter {
52
53 public ErrorMessageAdapter() {
54
55 }
56
57 public static String getErrorCauseMessage(RemoteOperationResult result,
58 RemoteOperation operation, Resources res) {
59
60 String message = null;
61
62 if (operation instanceof UploadFileOperation) {
63
64 if (result.isSuccess()) {
65 message = String.format(
66 res.getString(R.string.uploader_upload_succeeded_content_single),
67 ((UploadFileOperation) operation).getFileName());
68 } else {
69 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
70 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
71 message = String.format(
72 res.getString(R.string.error__upload__local_file_not_copied),
73 ((UploadFileOperation) operation).getFileName(),
74 res.getString(R.string.app_name));
75 /*
76 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
77 message = res.getString(R.string.failed_upload_quota_exceeded_text);
78 */
79
80 } else if (result.getCode() == ResultCode.FORBIDDEN) {
81 message = String.format(res.getString(R.string.forbidden_permissions),
82 res.getString(R.string.uploader_upload_forbidden_permissions));
83
84 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
85 message = res.getString(R.string.filename_forbidden_charaters_from_server);
86
87 } else {
88 message = String.format(
89 res.getString(R.string.uploader_upload_failed_content_single),
90 ((UploadFileOperation) operation).getFileName());
91 }
92 }
93
94 } else if (operation instanceof DownloadFileOperation) {
95
96 if (result.isSuccess()) {
97 message = String.format(
98 res.getString(R.string.downloader_download_succeeded_content),
99 new File(((DownloadFileOperation) operation).getSavePath()).getName());
100
101 } else {
102 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
103 message = res.getString(R.string.downloader_download_file_not_found);
104
105 } else {
106 message = String.format(
107 res.getString(R.string.downloader_download_failed_content), new File(
108 ((DownloadFileOperation) operation).getSavePath()).getName());
109 }
110 }
111
112 } else if (operation instanceof RemoveFileOperation) {
113 if (result.isSuccess()) {
114 message = res.getString(R.string.remove_success_msg);
115
116 } else {
117 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
118 // Error --> No permissions
119 message = String.format(res.getString(R.string.forbidden_permissions),
120 res.getString(R.string.forbidden_permissions_delete));
121 } else if (isNetworkError(result.getCode())) {
122 message = getErrorMessage(result, res);
123
124 } else {
125 message = res.getString(R.string.remove_fail_msg);
126 }
127 }
128
129 } else if (operation instanceof RenameFileOperation) {
130 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
131 message = res.getString(R.string.rename_local_fail_msg);
132
133 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
134 // Error --> No permissions
135 message = String.format(res.getString(R.string.forbidden_permissions),
136 res.getString(R.string.forbidden_permissions_rename));
137
138 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
139 message = res.getString(R.string.filename_forbidden_characters);
140
141 } else if (isNetworkError(result.getCode())) {
142 message = getErrorMessage(result, res);
143
144 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
145 message = res.getString(R.string.filename_forbidden_charaters_from_server);
146
147 } else {
148 message = res.getString(R.string.rename_server_fail_msg);
149 }
150
151 } else if (operation instanceof SynchronizeFileOperation) {
152 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
153 message = res.getString(R.string.sync_file_nothing_to_do_msg);
154 }
155
156 } else if (operation instanceof CreateFolderOperation) {
157 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
158 message = res.getString(R.string.filename_forbidden_characters);
159
160 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
161 message = String.format(res.getString(R.string.forbidden_permissions),
162 res.getString(R.string.forbidden_permissions_create));
163
164 } else if (isNetworkError(result.getCode())) {
165 message = getErrorMessage(result, res);
166
167 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
168 message = res.getString(R.string.filename_forbidden_charaters_from_server);
169 } else {
170 message = res.getString(R.string.create_dir_fail_msg);
171 }
172 } else if (operation instanceof CreateShareOperation) {
173 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
174 message = res.getString(R.string.share_link_file_no_exist);
175
176 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
177 // Error --> No permissions
178 message = String.format(res.getString(R.string.forbidden_permissions),
179 res.getString(R.string.share_link_forbidden_permissions));
180
181 } else if (isNetworkError(result.getCode())) {
182 message = getErrorMessage(result, res);
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 UnshareLinkOperation) {
190
191 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
192 message = res.getString(R.string.unshare_link_file_no_exist);
193
194 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
195 // Error --> No permissions
196 message = String.format(res.getString(R.string.forbidden_permissions),
197 res.getString(R.string.unshare_link_forbidden_permissions));
198
199 } else if (isNetworkError(result.getCode())) {
200 message = getErrorMessage(result, res);
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 } else if (operation instanceof MoveFileOperation) {
207
208 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
209 message = res.getString(R.string.move_file_not_found);
210
211 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
212 message = res.getString(R.string.move_file_invalid_into_descendent);
213
214 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
215 message = res.getString(R.string.move_file_invalid_overwrite);
216
217 } else if (result.getCode() == ResultCode.FORBIDDEN) {
218 message = String.format(res.getString(R.string.forbidden_permissions),
219 res.getString(R.string.forbidden_permissions_move));
220
221 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
222 message = res.getString(R.string.filename_forbidden_charaters_from_server);
223
224 } else { // Generic error
225 // Show a Message, operation finished without success
226 message = res.getString(R.string.move_file_error);
227 }
228 } else if (operation instanceof SynchronizeFolderOperation) {
229
230 if (!result.isSuccess()) {
231 String folderPathName = new File(
232 ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
233 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
234 message = String.format(res.getString(R.string.sync_current_folder_was_removed),
235 folderPathName);
236
237 } else { // Generic error
238 // Show a Message, operation finished without success
239 message = String.format(res.getString(R.string.download_folder_failed_content),
240 folderPathName);
241 }
242 }
243 }
244
245 return message;
246 }
247
248 private static String getErrorMessage(RemoteOperationResult result , Resources res) {
249
250 String message = null;
251
252 if (!result.isSuccess()) {
253
254 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
255 message = res.getString(R.string.network_error_socket_exception);
256
257 } else if (result.getCode() == ResultCode.TIMEOUT) {
258 message = res.getString(R.string.network_error_socket_exception);
259
260 if (result.getException() instanceof SocketTimeoutException) {
261 message = res.getString(R.string.network_error_socket_timeout_exception);
262 } else if(result.getException() instanceof ConnectTimeoutException) {
263 message = res.getString(R.string.network_error_connect_timeout_exception);
264 }
265
266 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
267 message = res.getString(R.string.network_host_not_available);
268 }
269 }
270
271 return message;
272 }
273
274 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
275 if (code == ResultCode.WRONG_CONNECTION ||
276 code == ResultCode.TIMEOUT ||
277 code == ResultCode.HOST_NOT_AVAILABLE) {
278 return true;
279 }
280 else
281 return false;
282 }
283 }