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