New class ErrorMessageAdapter to choose proper error message to show to the user...
[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
23 import android.content.res.Resources;
24
25 import com.owncloud.android.R;
26 import com.owncloud.android.lib.common.operations.RemoteOperation;
27 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
28 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
29 import com.owncloud.android.operations.DownloadFileOperation;
30 import com.owncloud.android.operations.UploadFileOperation;
31
32 /**
33 * Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
34 *
35 * @author masensio
36 *
37 */
38
39 public class ErrorMessageAdapter {
40
41 public ErrorMessageAdapter() {
42
43 }
44
45 public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) {
46
47 String message = null;
48
49 if (operation instanceof UploadFileOperation) {
50
51 if (result.isSuccess()) {
52 message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
53 ((UploadFileOperation) operation).getFileName());
54 } else {
55 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
56 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
57 message = String.format(res.getString(R.string.error__upload__local_file_not_copied),
58 ((UploadFileOperation) operation).getFileName());
59 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
60 message = res.getString(R.string.failed_upload_quota_exceeded_text);
61 } else {
62 message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
63 ((UploadFileOperation) operation).getFileName());
64 }
65 }
66
67 } else if (operation instanceof DownloadFileOperation) {
68
69 if (result.isSuccess()) {
70 message = String.format(res.getString(R.string.downloader_download_succeeded_content),
71 new File(((DownloadFileOperation) operation).getSavePath()).getName());
72 } else {
73 message = String.format(res.getString(R.string.downloader_download_failed_content),
74 new File(((DownloadFileOperation) operation).getSavePath()).getName());
75 }
76 }
77
78 return message;
79 }
80 }