Add specific message(s) for network error(s) in strings.xml
[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.DownloadFileOperation;
31 import com.owncloud.android.operations.UploadFileOperation;
32
33 /**
34 * Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
35 *
36 * @author masensio
37 *
38 */
39
40 public class ErrorMessageAdapter {
41
42 public ErrorMessageAdapter() {
43
44 }
45
46 public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) {
47
48 String message = null;
49
50 if (operation instanceof UploadFileOperation) {
51
52 if (result.isSuccess()) {
53 message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
54 ((UploadFileOperation) operation).getFileName());
55 } else {
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);
62 } else {
63 message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
64 ((UploadFileOperation) operation).getFileName());
65 }
66 }
67
68 } else if (operation instanceof DownloadFileOperation) {
69
70 if (result.isSuccess()) {
71 message = String.format(res.getString(R.string.downloader_download_succeeded_content),
72 new File(((DownloadFileOperation) operation).getSavePath()).getName());
73 } else {
74 message = String.format(res.getString(R.string.downloader_download_failed_content),
75 new File(((DownloadFileOperation) operation).getSavePath()).getName());
76 }
77 }
78
79
80 return message;
81 }
82
83 public static String getErrorMessage(RemoteOperationResult result , Resources res) {
84
85 String message = null;
86
87 if (!result.isSuccess()) {
88
89 switch (result.getCode()) {
90 case WRONG_CONNECTION:
91 message = res.getString(R.string.network_error_socket_exception);
92 break;
93
94 case TIMEOUT:
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);
99 }
100 break;
101
102 default:
103 message = res.getString(R.string.unexpected_exception);
104 break;
105 }
106 }
107
108 return message;
109 }
110 }