Merge branch 'develop' into thumbnailOOM
[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, RemoteOperation operation, Resources res) {
58
59 String message = null;
60
61 if (operation instanceof UploadFileOperation) {
62
63 if (result.isSuccess()) {
64 message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
65 ((UploadFileOperation) operation).getFileName());
66 } else {
67 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
68 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
69 message = String.format(res.getString(R.string.error__upload__local_file_not_copied),
70 ((UploadFileOperation) operation).getFileName(),
71 res.getString(R.string.app_name));
72 /*
73 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
74 message = res.getString(R.string.failed_upload_quota_exceeded_text);
75 */
76
77 } else if (result.getCode() == ResultCode.FORBIDDEN) {
78 message = String.format(res.getString(R.string.forbidden_permissions),
79 res.getString(R.string.uploader_upload_forbidden_permissions));
80
81 } else {
82 message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
83 ((UploadFileOperation) operation).getFileName());
84 }
85 }
86
87 } else if (operation instanceof DownloadFileOperation) {
88
89 if (result.isSuccess()) {
90 message = String.format(res.getString(R.string.downloader_download_succeeded_content),
91 new File(((DownloadFileOperation) operation).getSavePath()).getName());
92
93 } else {
94 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
95 message = res.getString(R.string.downloader_download_file_not_found);
96
97 } else {
98 message = String.format(res.getString(R.string.downloader_download_failed_content), new File(
99 ((DownloadFileOperation) operation).getSavePath()).getName());
100 }
101 }
102
103 } else if (operation instanceof RemoveFileOperation) {
104 if (result.isSuccess()) {
105 message = res.getString(R.string.remove_success_msg);
106
107 } else {
108 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
109 // Error --> No permissions
110 message = String.format(res.getString(R.string.forbidden_permissions),
111 res.getString(R.string.forbidden_permissions_delete));
112 } else if (isNetworkError(result.getCode())) {
113 message = getErrorMessage(result, res);
114
115 } else {
116 message = res.getString(R.string.remove_fail_msg);
117 }
118 }
119
120 } else if (operation instanceof RenameFileOperation) {
121 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
122 message = res.getString(R.string.rename_local_fail_msg);
123
124 } else 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_rename));
128
129 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
130 message = res.getString(R.string.filename_forbidden_characters);
131
132 } else if (isNetworkError(result.getCode())) {
133 message = getErrorMessage(result, res);
134
135 } else {
136 message = res.getString(R.string.rename_server_fail_msg);
137 }
138
139 } else if (operation instanceof SynchronizeFileOperation) {
140 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
141 message = res.getString(R.string.sync_file_nothing_to_do_msg);
142 }
143
144 } else if (operation instanceof CreateFolderOperation) {
145 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
146 message = res.getString(R.string.filename_forbidden_characters);
147
148 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
149 message = String.format(res.getString(R.string.forbidden_permissions),
150 res.getString(R.string.forbidden_permissions_create));
151
152 } else if (isNetworkError(result.getCode())) {
153 message = getErrorMessage(result, res);
154
155 } else {
156 message = res.getString(R.string.create_dir_fail_msg);
157 }
158 } else if (operation instanceof CreateShareOperation) {
159 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
160 message = res.getString(R.string.share_link_file_no_exist);
161
162 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
163 // Error --> No permissions
164 message = String.format(res.getString(R.string.forbidden_permissions),
165 res.getString(R.string.share_link_forbidden_permissions));
166
167 } else if (isNetworkError(result.getCode())) {
168 message = getErrorMessage(result, res);
169
170 } else { // Generic error
171 // Show a Message, operation finished without success
172 message = res.getString(R.string.share_link_file_error);
173 }
174
175 } else if (operation instanceof UnshareLinkOperation) {
176
177 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
178 message = res.getString(R.string.unshare_link_file_no_exist);
179
180 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
181 // Error --> No permissions
182 message = String.format(res.getString(R.string.forbidden_permissions),
183 res.getString(R.string.unshare_link_forbidden_permissions));
184
185 } else if (isNetworkError(result.getCode())) {
186 message = getErrorMessage(result, res);
187
188 } else { // Generic error
189 // Show a Message, operation finished without success
190 message = res.getString(R.string.unshare_link_file_error);
191 }
192 } else if (operation instanceof MoveFileOperation) {
193
194 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
195 message = res.getString(R.string.move_file_not_found);
196
197 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
198 message = res.getString(R.string.move_file_invalid_into_descendent);
199
200 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
201 message = res.getString(R.string.move_file_invalid_overwrite);
202
203 } else if (result.getCode() == ResultCode.FORBIDDEN) {
204 message = String.format(res.getString(R.string.forbidden_permissions),
205 res.getString(R.string.forbidden_permissions_move));
206
207 }else { // Generic error
208 // Show a Message, operation finished without success
209 message = res.getString(R.string.move_file_error);
210 }
211 } else if (operation instanceof SynchronizeFolderOperation) {
212
213 if (!result.isSuccess()) {
214 String folderPathName = new File(
215 ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
216 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
217 message = String.format(res.getString(R.string.sync_current_folder_was_removed),
218 folderPathName);
219
220 } else { // Generic error
221 // Show a Message, operation finished without success
222 message = String.format(res.getString(R.string.download_folder_failed_content),
223 folderPathName);
224 }
225 }
226 }
227
228 return message;
229 }
230
231 private static String getErrorMessage(RemoteOperationResult result , Resources res) {
232
233 String message = null;
234
235 if (!result.isSuccess()) {
236
237 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
238 message = res.getString(R.string.network_error_socket_exception);
239
240 } else if (result.getCode() == ResultCode.TIMEOUT) {
241 message = res.getString(R.string.network_error_socket_exception);
242
243 if (result.getException() instanceof SocketTimeoutException) {
244 message = res.getString(R.string.network_error_socket_timeout_exception);
245 } else if(result.getException() instanceof ConnectTimeoutException) {
246 message = res.getString(R.string.network_error_connect_timeout_exception);
247 }
248
249 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
250 message = res.getString(R.string.network_host_not_available);
251 }
252 }
253
254 return message;
255 }
256
257 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
258 if (code == ResultCode.WRONG_CONNECTION ||
259 code == ResultCode.TIMEOUT ||
260 code == ResultCode.HOST_NOT_AVAILABLE) {
261 return true;
262 }
263 else
264 return false;
265 }
266 }