01ca41fd598fa9c5e7c1c8879f6162c7c40f439f
[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.authentication.AccountUtils;
33 import com.owncloud.android.datamodel.OCFile;
34 import com.owncloud.android.files.services.FileDownloader;
35 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
36 import com.owncloud.android.files.services.FileUploader;
37 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
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,
62 Context context) {
63 mFile = targetFile;
64 mAccount = account;
65 mComponentsGetter = cg;
66 mContext = context;
67 }
68
69
70 /**
71 * Filters out the file actions available in the passed {@link Menu} taken into account
72 * the state of the {@link OCFile} held by the filter.
73 *
74 * @param menu Options or context menu to filter.
75 */
76 public void filter(Menu menu) {
77 List<Integer> toShow = new ArrayList<Integer>();
78 List<Integer> toHide = new ArrayList<Integer>();
79
80 filter(toShow, toHide);
81
82 MenuItem item = null;
83 for (int i : toShow) {
84 item = menu.findItem(i);
85 if (item != null) {
86 item.setVisible(true);
87 item.setEnabled(true);
88 }
89 }
90
91 for (int i : toHide) {
92 item = menu.findItem(i);
93 if (item != null) {
94 item.setVisible(false);
95 item.setEnabled(false);
96 }
97 }
98 }
99
100
101 /**
102 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
103 *
104 * Decides what actions must be shown and hidden.
105 *
106 * @param toShow List to save the options that must be shown in the menu.
107 * @param toHide List to save the options that must be shown in the menu.
108 */
109 private void filter(List<Integer> toShow, List <Integer> toHide) {
110 boolean shareWithUsersEnable = false;
111 boolean synchronizing = false;
112 if (mComponentsGetter != null && mFile != null && mAccount != null) {
113 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
114 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
115 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
116 synchronizing = (
117 // comparing local and remote
118 (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath())) ||
119 // downloading
120 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) ||
121 // uploading
122 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))
123 );
124 shareWithUsersEnable = AccountUtils.hasSearchUsersSupport(mAccount);
125 }
126
127 /// decision is taken for each possible action on a file in the menu
128
129 // DOWNLOAD
130 if (mFile == null || mFile.isDown() || mFile.isFolder() || synchronizing) {
131 toHide.add(R.id.action_download_file);
132
133 } else {
134 toShow.add(R.id.action_download_file);
135 }
136
137 // RENAME
138 if (mFile == null || synchronizing) {
139 toHide.add(R.id.action_rename_file);
140
141 } else {
142 toShow.add(R.id.action_rename_file);
143 }
144
145 // MOVE & COPY
146 if (mFile == null || synchronizing) {
147 toHide.add(R.id.action_move);
148 toHide.add(R.id.action_copy);
149 } else {
150 toShow.add(R.id.action_move);
151 toShow.add(R.id.action_copy);
152 }
153
154 // REMOVE
155 if (mFile == null || synchronizing) {
156 toHide.add(R.id.action_remove_file);
157
158 } else {
159 toShow.add(R.id.action_remove_file);
160 }
161
162 // OPEN WITH (different to preview!)
163 if (mFile == null || mFile.isFolder() || !mFile.isDown() || synchronizing) {
164 toHide.add(R.id.action_open_file_with);
165
166 } else {
167 toShow.add(R.id.action_open_file_with);
168 }
169
170 // CANCEL SYNCHRONIZATION
171 if (mFile == null || !synchronizing) {
172 toHide.add(R.id.action_cancel_sync);
173
174 } else {
175 toShow.add(R.id.action_cancel_sync);
176 }
177
178 // SYNC CONTENTS (BOTH FILE AND FOLDER)
179 if (mFile == null || (!mFile.isFolder() && !mFile.isDown()) || synchronizing) {
180 toHide.add(R.id.action_sync_file);
181
182 } else {
183 toShow.add(R.id.action_sync_file);
184 }
185
186 // SHARE FILE
187 // TODO add check on SHARE available on server side?
188 boolean shareAllowed = (mContext != null &&
189 mContext.getString(R.string.share_feature).equalsIgnoreCase("on"));
190 if (!shareAllowed || mFile == null) {
191 toHide.add(R.id.action_share_file);
192 } else {
193 toShow.add(R.id.action_share_file);
194 }
195
196 // UNSHARE FILE
197 // TODO add check on SHARE available on server side?
198 if ( !shareAllowed || (mFile == null || !mFile.isShareByLink())) {
199 toHide.add(R.id.action_unshare_file);
200 } else {
201 toShow.add(R.id.action_unshare_file);
202 }
203
204 // SHARE FILE, with Users
205 if (!shareAllowed || !shareWithUsersEnable || mFile == null) {
206 toHide.add(R.id.action_share_with_users);
207 } else {
208 toShow.add(R.id.action_share_with_users);
209 }
210
211 // UNSHARE FILE, with Users
212 if ( !shareAllowed || !shareWithUsersEnable || (mFile == null || !mFile.isShareWithUser())) {
213 toHide.add(R.id.action_unshare_with_users);
214 } else {
215 toShow.add(R.id.action_unshare_with_users);
216 }
217
218 // SEE DETAILS
219 if (mFile == null || mFile.isFolder()) {
220 toHide.add(R.id.action_see_details);
221 } else {
222 toShow.add(R.id.action_see_details);
223 }
224
225 // SEND
226 boolean sendAllowed = (mContext != null &&
227 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
228 if (mFile == null || !sendAllowed || mFile.isFolder() || synchronizing) {
229 toHide.add(R.id.action_send_file);
230 } else {
231 toShow.add(R.id.action_send_file);
232 }
233
234 // FAVORITES
235 if (mFile == null || synchronizing || mFile.isFolder() || mFile.isFavorite()) {
236 toHide.add(R.id.action_favorite_file);
237 } else {
238 toShow.add(R.id.action_favorite_file);
239 }
240
241 // UNFAVORITES
242 if (mFile == null || synchronizing || mFile.isFolder() || !mFile.isFavorite()) {
243 toHide.add(R.id.action_unfavorite_file);
244 } else {
245 toShow.add(R.id.action_unfavorite_file);
246 }
247
248 }
249
250 }