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