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