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