1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.utils
;
22 import java
.net
.SocketTimeoutException
;
23 import org
.apache
.commons
.httpclient
.ConnectTimeoutException
;
24 import android
.content
.res
.Resources
;
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
.DownloadFileOperation
;
31 import com
.owncloud
.android
.operations
.UploadFileOperation
;
34 * Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
40 public class ErrorMessageAdapter
{
42 public ErrorMessageAdapter() {
46 public static String
getErrorCauseMessage(RemoteOperationResult result
, RemoteOperation operation
, Resources res
) {
48 String message
= null
;
50 if (operation
instanceof UploadFileOperation
) {
52 if (result
.isSuccess()) {
53 message
= String
.format(res
.getString(R
.string
.uploader_upload_succeeded_content_single
),
54 ((UploadFileOperation
) operation
).getFileName());
56 if (result
.getCode() == ResultCode
.LOCAL_STORAGE_FULL
57 || result
.getCode() == ResultCode
.LOCAL_STORAGE_NOT_COPIED
) {
58 message
= String
.format(res
.getString(R
.string
.error__upload__local_file_not_copied
),
59 ((UploadFileOperation
) operation
).getFileName());
60 } else if (result
.getCode() == ResultCode
.QUOTA_EXCEEDED
) {
61 message
= res
.getString(R
.string
.failed_upload_quota_exceeded_text
);
63 message
= String
.format(res
.getString(R
.string
.uploader_upload_failed_content_single
),
64 ((UploadFileOperation
) operation
).getFileName());
68 } else if (operation
instanceof DownloadFileOperation
) {
70 if (result
.isSuccess()) {
71 message
= String
.format(res
.getString(R
.string
.downloader_download_succeeded_content
),
72 new File(((DownloadFileOperation
) operation
).getSavePath()).getName());
74 message
= String
.format(res
.getString(R
.string
.downloader_download_failed_content
),
75 new File(((DownloadFileOperation
) operation
).getSavePath()).getName());
83 public static String
getErrorMessage(RemoteOperationResult result
, Resources res
) {
85 String message
= null
;
87 if (!result
.isSuccess()) {
89 switch (result
.getCode()) {
90 case WRONG_CONNECTION
:
91 message
= res
.getString(R
.string
.network_error_socket_exception
);
95 if (result
.getException() instanceof SocketTimeoutException
) {
96 message
= res
.getString(R
.string
.network_error_socket_timeout_exception
);
97 } else if(result
.getException() instanceof ConnectTimeoutException
) {
98 message
= res
.getString(R
.string
.network_error_connect_timeout_exception
);
103 message
= res
.getString(R
.string
.unexpected_exception
);