Enable log into text file automatically when BuildConfig.DEBUG is true. Open log...
[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 if (result.getCode() == ResultCode.FORBIDDEN) {
75 message = String.format(res.getString(R.string.forbidden_permissions),
76 res.getString(R.string.uploader_upload_forbidden_permissions));
77
78 } else {
79 message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
80 ((UploadFileOperation) operation).getFileName());
81 }
82 }
83
84 } else if (operation instanceof DownloadFileOperation) {
85
86 if (result.isSuccess()) {
87 message = String.format(res.getString(R.string.downloader_download_succeeded_content),
88 new File(((DownloadFileOperation) operation).getSavePath()).getName());
89
90 } else {
91 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
92 message = res.getString(R.string.downloader_download_file_not_found);
93
94 } else {
95 message = String.format(res.getString(R.string.downloader_download_failed_content), new File(
96 ((DownloadFileOperation) operation).getSavePath()).getName());
97 }
98 }
99
100 } else if (operation instanceof RemoveFileOperation) {
101 if (result.isSuccess()) {
102 message = res.getString(R.string.remove_success_msg);
103
104 } else {
105 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
106 // Error --> No permissions
107 message = String.format(res.getString(R.string.forbidden_permissions),
108 res.getString(R.string.forbidden_permissions_delete));
109 } else if (isNetworkError(result.getCode())) {
110 message = getErrorMessage(result, res);
111
112 } else {
113 message = res.getString(R.string.remove_fail_msg);
114 }
115 }
116
117 } else if (operation instanceof RenameFileOperation) {
118 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
119 message = res.getString(R.string.rename_local_fail_msg);
120
121 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
122 // Error --> No permissions
123 message = String.format(res.getString(R.string.forbidden_permissions),
124 res.getString(R.string.forbidden_permissions_rename));
125
126 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
127 message = res.getString(R.string.filename_forbidden_characters);
128
129 } else if (isNetworkError(result.getCode())) {
130 message = getErrorMessage(result, res);
131
132 } else {
133 message = res.getString(R.string.rename_server_fail_msg);
134 }
135
136 } else if (operation instanceof SynchronizeFileOperation) {
137 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
138 message = res.getString(R.string.sync_file_nothing_to_do_msg);
139 }
140
141 } else if (operation instanceof CreateFolderOperation) {
142 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
143 message = res.getString(R.string.filename_forbidden_characters);
144
145 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
146 message = String.format(res.getString(R.string.forbidden_permissions),
147 res.getString(R.string.forbidden_permissions_create));
148
149 } else if (isNetworkError(result.getCode())) {
150 message = getErrorMessage(result, res);
151
152 } else {
153 message = res.getString(R.string.create_dir_fail_msg);
154 }
155 } else if (operation instanceof CreateShareOperation) {
156 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
157 message = res.getString(R.string.share_link_file_no_exist);
158
159 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
160 // Error --> No permissions
161 message = String.format(res.getString(R.string.forbidden_permissions),
162 res.getString(R.string.share_link_forbidden_permissions));
163
164 } else if (isNetworkError(result.getCode())) {
165 message = getErrorMessage(result, res);
166
167 } else { // Generic error
168 // Show a Message, operation finished without success
169 message = res.getString(R.string.share_link_file_error);
170 }
171
172 } else if (operation instanceof UnshareLinkOperation) {
173
174 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
175 message = res.getString(R.string.unshare_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.unshare_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.unshare_link_file_error);
188 }
189 }
190
191 return message;
192 }
193
194 private static String getErrorMessage(RemoteOperationResult result , Resources res) {
195
196 String message = null;
197
198 if (!result.isSuccess()) {
199
200 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
201 message = res.getString(R.string.network_error_socket_exception);
202
203 } else if (result.getCode() == ResultCode.TIMEOUT) {
204 message = res.getString(R.string.network_error_socket_exception);
205
206 if (result.getException() instanceof SocketTimeoutException) {
207 message = res.getString(R.string.network_error_socket_timeout_exception);
208 } else if(result.getException() instanceof ConnectTimeoutException) {
209 message = res.getString(R.string.network_error_connect_timeout_exception);
210 }
211
212 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
213 message = res.getString(R.string.network_host_not_available);
214 }
215 }
216
217 return message;
218 }
219
220 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
221 if (code == ResultCode.WRONG_CONNECTION ||
222 code == ResultCode.TIMEOUT ||
223 code == ResultCode.HOST_NOT_AVAILABLE) {
224 return true;
225 }
226 else
227 return false;
228 }
229 }