Tweaks for source 1.5
[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.CreateShareOperation;
33 import com.owncloud.android.operations.DownloadFileOperation;
34 import com.owncloud.android.operations.MoveFileOperation;
35 import com.owncloud.android.operations.RemoveFileOperation;
36 import com.owncloud.android.operations.RenameFileOperation;
37 import com.owncloud.android.operations.SynchronizeFileOperation;
38 import com.owncloud.android.operations.SynchronizeFolderOperation;
39 import com.owncloud.android.operations.UnshareLinkOperation;
40 import com.owncloud.android.operations.UploadFileOperation;
41
42 import org.apache.commons.httpclient.ConnectTimeoutException;
43
44 import java.io.File;
45 import java.net.SocketTimeoutException;
46
47 /**
48 * Class to choose proper error messages to show to the user depending on the results of operations,
49 * always following the same policy
50 */
51
52 public class ErrorMessageAdapter {
53
54 public ErrorMessageAdapter() {
55
56 }
57
58 public static String getErrorCauseMessage(RemoteOperationResult result,
59 RemoteOperation operation, Resources res) {
60
61 String message = null;
62
63 if (operation instanceof UploadFileOperation) {
64
65 if (result.isSuccess()) {
66 message = String.format(
67 res.getString(R.string.uploader_upload_succeeded_content_single),
68 ((UploadFileOperation) operation).getFileName());
69 } else {
70 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
71 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
72 message = String.format(
73 res.getString(R.string.error__upload__local_file_not_copied),
74 ((UploadFileOperation) operation).getFileName(),
75 res.getString(R.string.app_name));
76 /*
77 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
78 message = res.getString(R.string.failed_upload_quota_exceeded_text);
79 */
80
81 } else if (result.getCode() == ResultCode.FORBIDDEN) {
82 message = String.format(res.getString(R.string.forbidden_permissions),
83 res.getString(R.string.uploader_upload_forbidden_permissions));
84
85 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
86 message = res.getString(R.string.filename_forbidden_charaters_from_server);
87
88 } else {
89 message = String.format(
90 res.getString(R.string.uploader_upload_failed_content_single),
91 ((UploadFileOperation) operation).getFileName());
92 }
93 }
94
95 } else if (operation instanceof DownloadFileOperation) {
96
97 if (result.isSuccess()) {
98 message = String.format(
99 res.getString(R.string.downloader_download_succeeded_content),
100 new File(((DownloadFileOperation) operation).getSavePath()).getName());
101
102 } else {
103 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
104 message = res.getString(R.string.downloader_download_file_not_found);
105
106 } else {
107 message = String.format(
108 res.getString(R.string.downloader_download_failed_content), new File(
109 ((DownloadFileOperation) operation).getSavePath()).getName());
110 }
111 }
112
113 } else if (operation instanceof RemoveFileOperation) {
114 if (result.isSuccess()) {
115 message = res.getString(R.string.remove_success_msg);
116
117 } else {
118 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
119 // Error --> No permissions
120 message = String.format(res.getString(R.string.forbidden_permissions),
121 res.getString(R.string.forbidden_permissions_delete));
122 } else if (isNetworkError(result.getCode())) {
123 message = getErrorMessage(result, res);
124
125 } else {
126 message = res.getString(R.string.remove_fail_msg);
127 }
128 }
129
130 } else if (operation instanceof RenameFileOperation) {
131 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
132 message = res.getString(R.string.rename_local_fail_msg);
133
134 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
135 // Error --> No permissions
136 message = String.format(res.getString(R.string.forbidden_permissions),
137 res.getString(R.string.forbidden_permissions_rename));
138
139 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
140 message = res.getString(R.string.filename_forbidden_characters);
141
142 } else if (isNetworkError(result.getCode())) {
143 message = getErrorMessage(result, res);
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 (isNetworkError(result.getCode())) {
166 message = getErrorMessage(result, res);
167
168 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
169 message = res.getString(R.string.filename_forbidden_charaters_from_server);
170 } else {
171 message = res.getString(R.string.create_dir_fail_msg);
172 }
173 } else if (operation instanceof CreateShareOperation) {
174 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> 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 if (isNetworkError(result.getCode())) {
183 message = getErrorMessage(result, res);
184
185 } else { // Generic error
186 // Show a Message, operation finished without success
187 message = res.getString(R.string.share_link_file_error);
188 }
189
190 } else if (operation instanceof UnshareLinkOperation) {
191
192 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> 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 if (isNetworkError(result.getCode())) {
201 message = getErrorMessage(result, res);
202
203 } else { // Generic error
204 // Show a Message, operation finished without success
205 message = res.getString(R.string.unshare_link_file_error);
206 }
207 } else if (operation instanceof MoveFileOperation) {
208
209 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
210 message = res.getString(R.string.move_file_not_found);
211
212 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
213 message = res.getString(R.string.move_file_invalid_into_descendent);
214
215 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
216 message = res.getString(R.string.move_file_invalid_overwrite);
217
218 } else if (result.getCode() == ResultCode.FORBIDDEN) {
219 message = String.format(res.getString(R.string.forbidden_permissions),
220 res.getString(R.string.forbidden_permissions_move));
221
222 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
223 message = res.getString(R.string.filename_forbidden_charaters_from_server);
224
225 } else { // Generic error
226 // Show a Message, operation finished without success
227 message = res.getString(R.string.move_file_error);
228 }
229 } else if (operation instanceof SynchronizeFolderOperation) {
230
231 if (!result.isSuccess()) {
232 String folderPathName = new File(
233 ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
234 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
235 message = String.format(res.getString(R.string.sync_current_folder_was_removed),
236 folderPathName);
237
238 } else { // Generic error
239 // Show a Message, operation finished without success
240 message = String.format(res.getString(R.string.download_folder_failed_content),
241 folderPathName);
242 }
243 }
244 } else if (operation instanceof CopyFileOperation) {
245
246 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
247 message = res.getString(R.string.copy_file_not_found);
248
249 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
250 message = res.getString(R.string.copy_file_invalid_into_descendent);
251
252 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
253 message = res.getString(R.string.copy_file_invalid_overwrite);
254
255 } else if (result.getCode() == ResultCode.FORBIDDEN) {
256 message = String.format(res.getString(R.string.forbidden_permissions),
257 res.getString(R.string.forbidden_permissions_copy));
258
259 } else { // Generic error
260 // Show a Message, operation finished without success
261 message = res.getString(R.string.copy_file_error);
262 }
263 }
264
265 return message;
266 }
267
268 private static String getErrorMessage(RemoteOperationResult result, Resources res) {
269
270 String message = null;
271
272 if (!result.isSuccess()) {
273
274 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
275 message = res.getString(R.string.network_error_socket_exception);
276
277 } else if (result.getCode() == ResultCode.TIMEOUT) {
278 message = res.getString(R.string.network_error_socket_exception);
279
280 if (result.getException() instanceof SocketTimeoutException) {
281 message = res.getString(R.string.network_error_socket_timeout_exception);
282 } else if (result.getException() instanceof ConnectTimeoutException) {
283 message = res.getString(R.string.network_error_connect_timeout_exception);
284 }
285
286 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
287 message = res.getString(R.string.network_host_not_available);
288 }
289 }
290
291 return message;
292 }
293
294 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
295 if (code == ResultCode.WRONG_CONNECTION ||
296 code == ResultCode.TIMEOUT ||
297 code == ResultCode.HOST_NOT_AVAILABLE) {
298 return true;
299 } else
300 return false;
301 }
302 }