Merge pull request #505 from owncloud/more_concrete_error_messages_session_expires
[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
67 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
68 message = res.getString(R.string.failed_upload_quota_exceeded_text);
69
70 } else {
71 message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
72 ((UploadFileOperation) operation).getFileName());
73 }
74 }
75
76 } else if (operation instanceof DownloadFileOperation) {
77
78 if (result.isSuccess()) {
79 message = String.format(res.getString(R.string.downloader_download_succeeded_content),
80 new File(((DownloadFileOperation) operation).getSavePath()).getName());
81
82 } else {
83 message = String.format(res.getString(R.string.downloader_download_failed_content),
84 new File(((DownloadFileOperation) operation).getSavePath()).getName());
85 }
86
87 } else if (operation instanceof RemoveFileOperation) {
88 if (result.isSuccess()) {
89 message = res.getString(R.string.remove_success_msg);
90
91 } else {
92 if (isNetworkError(result.getCode())) {
93 message = getErrorMessage(result, res);
94
95 } else {
96 message = res.getString(R.string.remove_fail_msg);
97 }
98 }
99
100 } else if (operation instanceof RenameFileOperation) {
101 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
102 message = res.getString(R.string.rename_local_fail_msg);
103
104 } if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
105 message = res.getString(R.string.filename_forbidden_characters);
106
107 } else if (isNetworkError(result.getCode())) {
108 message = getErrorMessage(result, res);
109
110 } else {
111 message = res.getString(R.string.rename_server_fail_msg);
112 }
113
114 } else if (operation instanceof SynchronizeFileOperation) {
115 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
116 message = res.getString(R.string.sync_file_nothing_to_do_msg);
117 }
118
119 } else if (operation instanceof CreateFolderOperation) {
120 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
121 message = res.getString(R.string.filename_forbidden_characters);
122
123 } else if (isNetworkError(result.getCode())) {
124 message = getErrorMessage(result, res);
125
126 } else {
127 message = res.getString(R.string.create_dir_fail_msg);
128 }
129 } else if (operation instanceof CreateShareOperation) {
130 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
131 message = res.getString(R.string.share_link_file_no_exist);
132
133 } else if (isNetworkError(result.getCode())) {
134 message = getErrorMessage(result, res);
135
136 } else { // Generic error
137 // Show a Message, operation finished without success
138 message = res.getString(R.string.share_link_file_error);
139 }
140
141 } else if (operation instanceof UnshareLinkOperation) {
142
143 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
144 message = res.getString(R.string.unshare_link_file_no_exist);
145
146 } else if (isNetworkError(result.getCode())) {
147 message = getErrorMessage(result, res);
148
149 } else { // Generic error
150 // Show a Message, operation finished without success
151 message = res.getString(R.string.unshare_link_file_error);
152 }
153 }
154
155 return message;
156 }
157
158 private static String getErrorMessage(RemoteOperationResult result , Resources res) {
159
160 String message = null;
161
162 if (!result.isSuccess()) {
163
164 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
165 message = res.getString(R.string.network_error_socket_exception);
166
167 } else if (result.getCode() == ResultCode.TIMEOUT) {
168 message = res.getString(R.string.network_error_socket_exception);
169
170 if (result.getException() instanceof SocketTimeoutException) {
171 message = res.getString(R.string.network_error_socket_timeout_exception);
172 } else if(result.getException() instanceof ConnectTimeoutException) {
173 message = res.getString(R.string.network_error_connect_timeout_exception);
174 }
175
176 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
177 message = res.getString(R.string.network_host_not_available);
178 }
179 }
180
181 return message;
182 }
183
184 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
185 if (code == ResultCode.WRONG_CONNECTION ||
186 code == ResultCode.TIMEOUT ||
187 code == ResultCode.HOST_NOT_AVAILABLE) {
188 return true;
189 }
190 else
191 return false;
192 }
193 }