4802700814dc6d4b2830d093f1305cba6568d428
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / FileMenuFilter.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.files;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import android.accounts.Account;
27 import android.content.Context;
28 import android.view.Menu;
29 import android.view.MenuItem;
30
31 import com.owncloud.android.R;
32 import com.owncloud.android.datamodel.OCFile;
33 import com.owncloud.android.files.services.FileDownloader;
34 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
35 import com.owncloud.android.files.services.FileUploader;
36 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
37 import com.owncloud.android.services.OperationsService;
38 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
39 import com.owncloud.android.ui.activity.ComponentsGetter;
40
41 /**
42 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
43 * according to the current state of the latest.
44 */
45 public class FileMenuFilter {
46
47 private OCFile mFile;
48 private ComponentsGetter mComponentsGetter;
49 private Account mAccount;
50 private Context mContext;
51
52 /**
53 * Constructor
54 *
55 * @param targetFile {@link OCFile} target of the action to filter in the {@link Menu}.
56 * @param account ownCloud {@link Account} holding targetFile.
57 * @param cg Accessor to app components, needed to access the
58 * {@link FileUploader} and {@link FileDownloader} services
59 * @param context Android {@link Context}, needed to access build setup resources.
60 */
61 public FileMenuFilter(OCFile targetFile, Account account, ComponentsGetter cg, Context context) {
62 mFile = targetFile;
63 mAccount = account;
64 mComponentsGetter = cg;
65 mContext = context;
66 }
67
68
69 /**
70 * Filters out the file actions available in the passed {@link Menu} taken into account
71 * the state of the {@link OCFile} held by the filter.
72 *
73 * @param menu Options or context menu to filter.
74 */
75 public void filter(Menu menu) {
76 List<Integer> toShow = new ArrayList<Integer>();
77 List<Integer> toHide = new ArrayList<Integer>();
78
79 filter(toShow, toHide);
80
81 MenuItem item = null;
82 for (int i : toShow) {
83 item = menu.findItem(i);
84 if (item != null) {
85 item.setVisible(true);
86 item.setEnabled(true);
87 }
88 }
89
90 for (int i : toHide) {
91 item = menu.findItem(i);
92 if (item != null) {
93 item.setVisible(false);
94 item.setEnabled(false);
95 }
96 }
97 }
98
99
100 /**
101 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
102 *
103 * Decides what actions must be shown and hidden.
104 *
105 * @param toShow List to save the options that must be shown in the menu.
106 * @param toHide List to save the options that must be shown in the menu.
107 */
108 private void filter(List<Integer> toShow, List <Integer> toHide) {
109 boolean downloading = false;
110 boolean uploading = false;
111 if (mComponentsGetter != null && mFile != null && mAccount != null) {
112 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
113 downloading = (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile));
114 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
115 downloading |= (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath()));
116 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
117 uploading = (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile));
118 }
119
120 /// decision is taken for each possible action on a file in the menu
121
122 // DOWNLOAD
123 if (mFile == null || mFile.isDown() || downloading || uploading) {
124 toHide.add(R.id.action_download_file);
125
126 } else {
127 toShow.add(R.id.action_download_file);
128 }
129
130 // RENAME
131 if (mFile == null || downloading || uploading) {
132 toHide.add(R.id.action_rename_file);
133
134 } else {
135 toShow.add(R.id.action_rename_file);
136 }
137
138 // MOVE & COPY
139 if (mFile == null || downloading || uploading) {
140 toHide.add(R.id.action_move);
141 toHide.add(R.id.action_copy);
142 } else {
143 toShow.add(R.id.action_move);
144 toShow.add(R.id.action_copy);
145 }
146
147 // REMOVE
148 if (mFile == null || downloading || uploading) {
149 toHide.add(R.id.action_remove_file);
150
151 } else {
152 toShow.add(R.id.action_remove_file);
153 }
154
155 // OPEN WITH (different to preview!)
156 if (mFile == null || mFile.isFolder() || !mFile.isDown() || downloading || uploading) {
157 toHide.add(R.id.action_open_file_with);
158
159 } else {
160 toShow.add(R.id.action_open_file_with);
161 }
162
163
164 // CANCEL DOWNLOAD
165 if (mFile == null || !downloading) {
166 toHide.add(R.id.action_cancel_download);
167 } else {
168 toShow.add(R.id.action_cancel_download);
169 }
170
171 // CANCEL UPLOAD
172 if (mFile == null || !uploading || mFile.isFolder()) {
173 toHide.add(R.id.action_cancel_upload);
174 } else {
175 toShow.add(R.id.action_cancel_upload);
176 }
177
178 // SYNC FILE CONTENTS
179 if (mFile == null || (!mFile.isFolder() && !mFile.isDown()) || downloading || uploading) {
180 toHide.add(R.id.action_sync_file);
181 } else {
182 toShow.add(R.id.action_sync_file);
183 }
184
185 // SHARE FILE
186 // TODO add check on SHARE available on server side?
187 boolean shareAllowed = (mContext != null &&
188 mContext.getString(R.string.share_feature).equalsIgnoreCase("on"));
189 if (!shareAllowed || mFile == null) {
190 toHide.add(R.id.action_share_file);
191 } else {
192 toShow.add(R.id.action_share_file);
193 }
194
195 // UNSHARE FILE
196 // TODO add check on SHARE available on server side?
197 if ( !shareAllowed || (mFile == null || !mFile.isShareByLink())) {
198 toHide.add(R.id.action_unshare_file);
199 } else {
200 toShow.add(R.id.action_unshare_file);
201 }
202
203 // SEE DETAILS
204 if (mFile == null || mFile.isFolder()) {
205 toHide.add(R.id.action_see_details);
206 } else {
207 toShow.add(R.id.action_see_details);
208 }
209
210 // SEND
211 boolean sendAllowed = (mContext != null &&
212 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
213 if (mFile == null || !sendAllowed || mFile.isFolder() || uploading || downloading) {
214 toHide.add(R.id.action_send_file);
215 } else {
216 toShow.add(R.id.action_send_file);
217 }
218
219 // FAVORITES
220 if (mFile == null || downloading || uploading || mFile.isFolder() || mFile.isFavorite()) {
221 toHide.add(R.id.action_favorite_file);
222 } else {
223 toShow.add(R.id.action_favorite_file);
224 }
225
226 // UNFAVORITES
227 if (mFile == null || downloading || uploading || mFile.isFolder() || !mFile.isFavorite()) {
228 toHide.add(R.id.action_unfavorite_file);
229 } else {
230 toShow.add(R.id.action_unfavorite_file);
231 }
232
233 }
234
235 }