84a9113a68a848e436329555ceef9fe86db33e18
[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.UnshareLinkOperation;
40 import com.owncloud.android.operations.UploadFileOperation;
41
42 /**
43 * Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
44 *
45 * @author masensio
46 *
47 */
48
49 public class ErrorMessageAdapter {
50
51 public ErrorMessageAdapter() {
52
53 }
54
55 public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) {
56
57 String message = null;
58
59 if (operation instanceof UploadFileOperation) {
60
61 if (result.isSuccess()) {
62 message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
63 ((UploadFileOperation) operation).getFileName());
64 } else {
65 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
66 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
67 message = String.format(res.getString(R.string.error__upload__local_file_not_copied),
68 ((UploadFileOperation) operation).getFileName(),
69 res.getString(R.string.app_name));
70 /*
71 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
72 message = res.getString(R.string.failed_upload_quota_exceeded_text);
73 */
74
75 } else if (result.getCode() == ResultCode.FORBIDDEN) {
76 message = String.format(res.getString(R.string.forbidden_permissions),
77 res.getString(R.string.uploader_upload_forbidden_permissions));
78
79 } else {
80 message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
81 ((UploadFileOperation) operation).getFileName());
82 }
83 }
84
85 } else if (operation instanceof DownloadFileOperation) {
86
87 if (result.isSuccess()) {
88 message = String.format(res.getString(R.string.downloader_download_succeeded_content),
89 new File(((DownloadFileOperation) operation).getSavePath()).getName());
90
91 } else {
92 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
93 message = res.getString(R.string.downloader_download_file_not_found);
94
95 } else {
96 message = String.format(res.getString(R.string.downloader_download_failed_content), new File(
97 ((DownloadFileOperation) operation).getSavePath()).getName());
98 }
99 }
100
101 } else if (operation instanceof RemoveFileOperation) {
102 if (result.isSuccess()) {
103 message = res.getString(R.string.remove_success_msg);
104
105 } else {
106 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
107 // Error --> No permissions
108 message = String.format(res.getString(R.string.forbidden_permissions),
109 res.getString(R.string.forbidden_permissions_delete));
110 } else if (isNetworkError(result.getCode())) {
111 message = getErrorMessage(result, res);
112
113 } else {
114 message = res.getString(R.string.remove_fail_msg);
115 }
116 }
117
118 } else if (operation instanceof RenameFileOperation) {
119 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
120 message = res.getString(R.string.rename_local_fail_msg);
121
122 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
123 // Error --> No permissions
124 message = String.format(res.getString(R.string.forbidden_permissions),
125 res.getString(R.string.forbidden_permissions_rename));
126
127 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
128 message = res.getString(R.string.filename_forbidden_characters);
129
130 } else if (isNetworkError(result.getCode())) {
131 message = getErrorMessage(result, res);
132
133 } else {
134 message = res.getString(R.string.rename_server_fail_msg);
135 }
136
137 } else if (operation instanceof SynchronizeFileOperation) {
138 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
139 message = res.getString(R.string.sync_file_nothing_to_do_msg);
140 }
141
142 } else if (operation instanceof CreateFolderOperation) {
143 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
144 message = res.getString(R.string.filename_forbidden_characters);
145
146 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
147 message = String.format(res.getString(R.string.forbidden_permissions),
148 res.getString(R.string.forbidden_permissions_create));
149
150 } else if (isNetworkError(result.getCode())) {
151 message = getErrorMessage(result, res);
152
153 } else {
154 message = res.getString(R.string.create_dir_fail_msg);
155 }
156 } else if (operation instanceof CreateShareOperation) {
157 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
158 message = res.getString(R.string.share_link_file_no_exist);
159
160 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
161 // Error --> No permissions
162 message = String.format(res.getString(R.string.forbidden_permissions),
163 res.getString(R.string.share_link_forbidden_permissions));
164
165 } else if (isNetworkError(result.getCode())) {
166 message = getErrorMessage(result, res);
167
168 } else { // Generic error
169 // Show a Message, operation finished without success
170 message = res.getString(R.string.share_link_file_error);
171 }
172
173 } else if (operation instanceof UnshareLinkOperation) {
174
175 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
176 message = res.getString(R.string.unshare_link_file_no_exist);
177
178 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
179 // Error --> No permissions
180 message = String.format(res.getString(R.string.forbidden_permissions),
181 res.getString(R.string.unshare_link_forbidden_permissions));
182
183 } else if (isNetworkError(result.getCode())) {
184 message = getErrorMessage(result, res);
185
186 } else { // Generic error
187 // Show a Message, operation finished without success
188 message = res.getString(R.string.unshare_link_file_error);
189 }
190 } else if (operation instanceof MoveFileOperation) {
191
192 if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
193 message = res.getString(R.string.move_file_invalid_into_descendent);
194
195 } else { // Generic error
196 // Show a Message, operation finished without success
197 message = res.getString(R.string.move_file_error);
198 }
199 }
200
201 return message;
202 }
203
204 private static String getErrorMessage(RemoteOperationResult result , Resources res) {
205
206 String message = null;
207
208 if (!result.isSuccess()) {
209
210 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
211 message = res.getString(R.string.network_error_socket_exception);
212
213 } else if (result.getCode() == ResultCode.TIMEOUT) {
214 message = res.getString(R.string.network_error_socket_exception);
215
216 if (result.getException() instanceof SocketTimeoutException) {
217 message = res.getString(R.string.network_error_socket_timeout_exception);
218 } else if(result.getException() instanceof ConnectTimeoutException) {
219 message = res.getString(R.string.network_error_connect_timeout_exception);
220 }
221
222 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
223 message = res.getString(R.string.network_host_not_available);
224 }
225 }
226
227 return message;
228 }
229
230 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
231 if (code == ResultCode.WRONG_CONNECTION ||
232 code == ResultCode.TIMEOUT ||
233 code == ResultCode.HOST_NOT_AVAILABLE) {
234 return true;
235 }
236 else
237 return false;
238 }
239 }