25d3027a017a05c35995216a4718ee4d9613ce0f
[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.lib.resources.status.CapabilityBooleanType;
39 import com.owncloud.android.lib.resources.status.OCCapability;
40 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
41 import com.owncloud.android.ui.activity.ComponentsGetter;
42
43 /**
44 * Filters out the file actions available in a given {@link Menu} for a given {@link OCFile}
45 * according to the current state of the latest.
46 */
47 public class FileMenuFilter {
48
49 private OCFile mFile;
50 private ComponentsGetter mComponentsGetter;
51 private Account mAccount;
52 private Context mContext;
53
54 /**
55 * Constructor
56 *
57 * @param targetFile {@link OCFile} target of the action to filter in the {@link Menu}.
58 * @param account ownCloud {@link Account} holding targetFile.
59 * @param cg Accessor to app components, needed to access the
60 * {@link FileUploader} and {@link FileDownloader} services
61 * @param context Android {@link Context}, needed to access build setup resources.
62 */
63 public FileMenuFilter(OCFile targetFile, Account account, ComponentsGetter cg,
64 Context context) {
65 mFile = targetFile;
66 mAccount = account;
67 mComponentsGetter = cg;
68 mContext = context;
69 }
70
71
72 /**
73 * Filters out the file actions available in the passed {@link Menu} taken into account
74 * the state of the {@link OCFile} held by the filter.
75 *
76 * @param menu Options or context menu to filter.
77 */
78 public void filter(Menu menu) {
79 List<Integer> toShow = new ArrayList<Integer>();
80 List<Integer> toHide = new ArrayList<Integer>();
81
82 filter(toShow, toHide);
83
84 MenuItem item = null;
85 for (int i : toShow) {
86 item = menu.findItem(i);
87 if (item != null) {
88 item.setVisible(true);
89 item.setEnabled(true);
90 }
91 }
92
93 for (int i : toHide) {
94 item = menu.findItem(i);
95 if (item != null) {
96 item.setVisible(false);
97 item.setEnabled(false);
98 }
99 }
100 }
101
102
103 /**
104 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
105 *
106 * Decides what actions must be shown and hidden.
107 *
108 * @param toShow List to save the options that must be shown in the menu.
109 * @param toHide List to save the options that must be shown in the menu.
110 */
111 private void filter(List<Integer> toShow, List <Integer> toHide) {
112 boolean synchronizing = false;
113 if (mComponentsGetter != null && mFile != null && mAccount != null) {
114 OperationsServiceBinder opsBinder = mComponentsGetter.getOperationsServiceBinder();
115 FileUploaderBinder uploaderBinder = mComponentsGetter.getFileUploaderBinder();
116 FileDownloaderBinder downloaderBinder = mComponentsGetter.getFileDownloaderBinder();
117 synchronizing = (
118 // comparing local and remote
119 (opsBinder != null && opsBinder.isSynchronizing(mAccount, mFile.getRemotePath())) ||
120 // downloading
121 (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) ||
122 // uploading
123 (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile))
124 );
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.isSharedViaLink())) {
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 OCCapability capability = mComponentsGetter.getStorageManager().getCapability(mAccount.name);
206 boolean shareApiEnabled = capability != null &&
207 (capability.getFilesSharingApiEnabled().isTrue() || capability.getFilesSharingApiEnabled().isUnknown());
208 if (!shareAllowed || mFile == null || !shareApiEnabled ) {
209 toHide.add(R.id.action_share_with_users);
210 } else {
211 toShow.add(R.id.action_share_with_users);
212 }
213
214
215 // SEE DETAILS
216 if (mFile == null || mFile.isFolder()) {
217 toHide.add(R.id.action_see_details);
218 } else {
219 toShow.add(R.id.action_see_details);
220 }
221
222 // SEND
223 boolean sendAllowed = (mContext != null &&
224 mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
225 if (mFile == null || !sendAllowed || mFile.isFolder() || synchronizing) {
226 toHide.add(R.id.action_send_file);
227 } else {
228 toShow.add(R.id.action_send_file);
229 }
230
231 // FAVORITES
232 if (mFile == null || synchronizing || mFile.isFolder() || mFile.isFavorite()) {
233 toHide.add(R.id.action_favorite_file);
234 } else {
235 toShow.add(R.id.action_favorite_file);
236 }
237
238 // UNFAVORITES
239 if (mFile == null || synchronizing || mFile.isFolder() || !mFile.isFavorite()) {
240 toHide.add(R.id.action_unfavorite_file);
241 } else {
242 toShow.add(R.id.action_unfavorite_file);
243 }
244
245 }
246
247 }