Upgrade notifications on status bar when uploading to show proper message about fails...
[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 <<<<<<< HEAD
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 message = String.format(res.getString(R.string.downloader_download_failed_content),
93 new File(((DownloadFileOperation) operation).getSavePath()).getName());
94 }
95
96 } else if (operation instanceof RemoveFileOperation) {
97 if (result.isSuccess()) {
98 message = res.getString(R.string.remove_success_msg);
99
100 } else {
101 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
102 // Error --> No permissions
103 message = String.format(res.getString(R.string.forbidden_permissions),
104 res.getString(R.string.forbidden_permissions_delete));
105 } else if (isNetworkError(result.getCode())) {
106 message = getErrorMessage(result, res);
107
108 } else {
109 message = res.getString(R.string.remove_fail_msg);
110 }
111 }
112
113 } else if (operation instanceof RenameFileOperation) {
114 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
115 message = res.getString(R.string.rename_local_fail_msg);
116
117 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
118 // Error --> No permissions
119 message = String.format(res.getString(R.string.forbidden_permissions),
120 res.getString(R.string.forbidden_permissions_rename));
121
122 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
123 message = res.getString(R.string.filename_forbidden_characters);
124
125 } else if (isNetworkError(result.getCode())) {
126 message = getErrorMessage(result, res);
127
128 } else {
129 message = res.getString(R.string.rename_server_fail_msg);
130 }
131
132 } else if (operation instanceof SynchronizeFileOperation) {
133 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
134 message = res.getString(R.string.sync_file_nothing_to_do_msg);
135 }
136
137 } else if (operation instanceof CreateFolderOperation) {
138 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
139 message = res.getString(R.string.filename_forbidden_characters);
140
141 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
142 message = String.format(res.getString(R.string.forbidden_permissions),
143 res.getString(R.string.forbidden_permissions_create));
144
145 } else if (isNetworkError(result.getCode())) {
146 message = getErrorMessage(result, res);
147
148 } else {
149 message = res.getString(R.string.create_dir_fail_msg);
150 }
151 } else if (operation instanceof CreateShareOperation) {
152 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
153 message = res.getString(R.string.share_link_file_no_exist);
154
155 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
156 // Error --> No permissions
157 message = String.format(res.getString(R.string.forbidden_permissions),
158 res.getString(R.string.share_link_forbidden_permissions));
159
160 } else if (isNetworkError(result.getCode())) {
161 message = getErrorMessage(result, res);
162
163 } else { // Generic error
164 // Show a Message, operation finished without success
165 message = res.getString(R.string.share_link_file_error);
166 }
167
168 } else if (operation instanceof UnshareLinkOperation) {
169
170 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
171 message = res.getString(R.string.unshare_link_file_no_exist);
172
173 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
174 // Error --> No permissions
175 message = String.format(res.getString(R.string.forbidden_permissions),
176 res.getString(R.string.unshare_link_forbidden_permissions));
177
178 } else if (isNetworkError(result.getCode())) {
179 message = getErrorMessage(result, res);
180
181 } else { // Generic error
182 // Show a Message, operation finished without success
183 message = res.getString(R.string.unshare_link_file_error);
184 }
185 }
186
187 return message;
188 }
189
190 private static String getErrorMessage(RemoteOperationResult result , Resources res) {
191
192 String message = null;
193
194 if (!result.isSuccess()) {
195
196 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
197 message = res.getString(R.string.network_error_socket_exception);
198
199 } else if (result.getCode() == ResultCode.TIMEOUT) {
200 message = res.getString(R.string.network_error_socket_exception);
201
202 if (result.getException() instanceof SocketTimeoutException) {
203 message = res.getString(R.string.network_error_socket_timeout_exception);
204 } else if(result.getException() instanceof ConnectTimeoutException) {
205 message = res.getString(R.string.network_error_connect_timeout_exception);
206 }
207
208 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
209 message = res.getString(R.string.network_host_not_available);
210 }
211 }
212
213 return message;
214 }
215
216 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
217 if (code == ResultCode.WRONG_CONNECTION ||
218 code == ResultCode.TIMEOUT ||
219 code == ResultCode.HOST_NOT_AVAILABLE) {
220 return true;
221 }
222 else
223 return false;
224 }
225 }