9e077c0ab5671e2b9d5e1c3c4414facfcdcf80f8
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / ErrorMessageAdapter.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18
19 package com.owncloud.android.utils;
20
21 import java.io.File;
22 import java.net.SocketTimeoutException;
23
24 import org.apache.commons.httpclient.ConnectTimeoutException;
25
26 import android.content.res.Resources;
27
28 import com.owncloud.android.R;
29 import com.owncloud.android.lib.common.operations.RemoteOperation;
30 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
31 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
32 import com.owncloud.android.operations.CreateFolderOperation;
33 import com.owncloud.android.operations.CreateShareOperation;
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.UnshareLinkOperation;
41 import com.owncloud.android.operations.UploadFileOperation;
42
43 /**
44 * Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
45 *
46 * @author masensio
47 *
48 */
49
50 public class ErrorMessageAdapter {
51
52 public ErrorMessageAdapter() {
53
54 }
55
56 public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) {
57
58 String message = null;
59
60 if (operation instanceof UploadFileOperation) {
61
62 if (result.isSuccess()) {
63 message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
64 ((UploadFileOperation) operation).getFileName());
65 } else {
66 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
67 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
68 message = String.format(res.getString(R.string.error__upload__local_file_not_copied),
69 ((UploadFileOperation) operation).getFileName(),
70 res.getString(R.string.app_name));
71 /*
72 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
73 message = res.getString(R.string.failed_upload_quota_exceeded_text);
74 */
75
76 } else if (result.getCode() == ResultCode.FORBIDDEN) {
77 message = String.format(res.getString(R.string.forbidden_permissions),
78 res.getString(R.string.uploader_upload_forbidden_permissions));
79
80 } else {
81 message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
82 ((UploadFileOperation) operation).getFileName());
83 }
84 }
85
86 } else if (operation instanceof DownloadFileOperation) {
87
88 if (result.isSuccess()) {
89 message = String.format(res.getString(R.string.downloader_download_succeeded_content),
90 new File(((DownloadFileOperation) operation).getSavePath()).getName());
91
92 } else {
93 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
94 message = res.getString(R.string.downloader_download_file_not_found);
95
96 } else {
97 message = String.format(res.getString(R.string.downloader_download_failed_content), new File(
98 ((DownloadFileOperation) operation).getSavePath()).getName());
99 }
100 }
101
102 } else if (operation instanceof RemoveFileOperation) {
103 if (result.isSuccess()) {
104 message = res.getString(R.string.remove_success_msg);
105
106 } else {
107 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
108 // Error --> No permissions
109 message = String.format(res.getString(R.string.forbidden_permissions),
110 res.getString(R.string.forbidden_permissions_delete));
111 } else if (isNetworkError(result.getCode())) {
112 message = getErrorMessage(result, res);
113
114 } else {
115 message = res.getString(R.string.remove_fail_msg);
116 }
117 }
118
119 } else if (operation instanceof RenameFileOperation) {
120 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
121 message = res.getString(R.string.rename_local_fail_msg);
122
123 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
124 // Error --> No permissions
125 message = String.format(res.getString(R.string.forbidden_permissions),
126 res.getString(R.string.forbidden_permissions_rename));
127
128 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
129 message = res.getString(R.string.filename_forbidden_characters);
130
131 } else if (isNetworkError(result.getCode())) {
132 message = getErrorMessage(result, res);
133
134 } else {
135 message = res.getString(R.string.rename_server_fail_msg);
136 }
137
138 } else if (operation instanceof SynchronizeFileOperation) {
139 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
140 message = res.getString(R.string.sync_file_nothing_to_do_msg);
141 }
142
143 } else if (operation instanceof CreateFolderOperation) {
144 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
145 message = res.getString(R.string.filename_forbidden_characters);
146
147 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
148 message = String.format(res.getString(R.string.forbidden_permissions),
149 res.getString(R.string.forbidden_permissions_create));
150
151 } else if (isNetworkError(result.getCode())) {
152 message = getErrorMessage(result, res);
153
154 } else {
155 message = res.getString(R.string.create_dir_fail_msg);
156 }
157 } else if (operation instanceof CreateShareOperation) {
158 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
159 message = res.getString(R.string.share_link_file_no_exist);
160
161 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
162 // Error --> No permissions
163 message = String.format(res.getString(R.string.forbidden_permissions),
164 res.getString(R.string.share_link_forbidden_permissions));
165
166 } else if (isNetworkError(result.getCode())) {
167 message = getErrorMessage(result, res);
168
169 } else { // Generic error
170 // Show a Message, operation finished without success
171 message = res.getString(R.string.share_link_file_error);
172 }
173
174 } else if (operation instanceof UnshareLinkOperation) {
175
176 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
177 message = res.getString(R.string.unshare_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.unshare_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.unshare_link_file_error);
190 }
191 } else if (operation instanceof MoveFileOperation) {
192
193 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
194 message = res.getString(R.string.move_file_not_found);
195
196 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
197 message = res.getString(R.string.move_file_invalid_into_descendent);
198
199 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
200 message = res.getString(R.string.move_file_invalid_overwrite);
201
202 } else if (result.getCode() == ResultCode.FORBIDDEN) {
203 message = String.format(res.getString(R.string.forbidden_permissions),
204 res.getString(R.string.forbidden_permissions_move));
205
206 }else { // Generic error
207 // Show a Message, operation finished without success
208 message = res.getString(R.string.move_file_error);
209 }
210 } else if (operation instanceof SynchronizeFolderOperation) {
211
212 if (!result.isSuccess()) {
213 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
214 message = res.getString(R.string.download_folder_not_found);
215
216 } else { // Generic error
217 // Show a Message, operation finished without success
218 message = String.format(res.getString(R.string.downloader_download_folder_failed_content), new File(
219 ((SynchronizeFolderOperation) operation).getFolderPath()).getName());
220 }
221 }
222 }
223
224 return message;
225 }
226
227 private static String getErrorMessage(RemoteOperationResult result , Resources res) {
228
229 String message = null;
230
231 if (!result.isSuccess()) {
232
233 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
234 message = res.getString(R.string.network_error_socket_exception);
235
236 } else if (result.getCode() == ResultCode.TIMEOUT) {
237 message = res.getString(R.string.network_error_socket_exception);
238
239 if (result.getException() instanceof SocketTimeoutException) {
240 message = res.getString(R.string.network_error_socket_timeout_exception);
241 } else if(result.getException() instanceof ConnectTimeoutException) {
242 message = res.getString(R.string.network_error_connect_timeout_exception);
243 }
244
245 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
246 message = res.getString(R.string.network_host_not_available);
247 }
248 }
249
250 return message;
251 }
252
253 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
254 if (code == ResultCode.WRONG_CONNECTION ||
255 code == ResultCode.TIMEOUT ||
256 code == ResultCode.HOST_NOT_AVAILABLE) {
257 return true;
258 }
259 else
260 return false;
261 }
262 }